<?php
//include('../include/db.php');
include('db.php');

//    */1 * * * *     per minutes


/* ==============================================  FUNCTIONS =========================================================== */

function check_is_user_purchased_product($user_id){
	GLOBAL $conn;
	if(isset($user_id) && !empty($user_id))
	{
		$query="SELECT purchase_user_id, purchase_product_id, purchase_product_price, tax_amount, purchase_product_bv, purchase_date FROM product_purchase WHERE LOWER(purchase_user_id) = '".strtolower(trim($user_id))."' ORDER BY purchase_id DESC ";
		$con = mysqli_query($conn, $query);
		if(mysqli_num_rows($con)>0)
		{
			return mysqli_fetch_assoc($con);
		}
		else
		{
			return '';
		}
	}
}


function check_is_downline_user_active_and_purchased_product($user_id){
	GLOBAL $conn;
	if(isset($user_id) && !empty($user_id))
	{
		
		$sql = "SELECT R.unique_user_id, T.position_left, T.position_right FROM registration AS R 
			LEFT JOIN tree_data AS T ON T.position_id = R.unique_user_id
			WHERE 
			LOWER(R.unique_user_id) = '".strtolower(trim($user_id))."' AND
			R.type != 'admin' AND 
			R.active_deactive = 1 AND 
			R.active_status = 'Active' AND 
			(T.position_left != '' AND T.position_right != '')";
		//echo $sql; die;
		$con = mysqli_query($conn, $sql);
		$user_details = mysqli_fetch_assoc($con);
		if(check_is_user_purchased_product($user_details['position_left']) && check_is_user_purchased_product($user_details['position_right']))
		{
			return true;
		}
		//echo '<pre>'; print_r($user_details); die;
	}
}


function give_reward($data){
	GLOBAL $conn;
	$return = false;
	$today_date = date('Y-m-d H:i:s');
	if(isset($data['user_id']) && $data['user_id'] !='')
	{
	$reward = "insert into rewards(user_id,reward,reward_desc,respective_user ,reward_type, status, reward_date)
            value('".$data['user_id']."','".$data['reward']."','".$data['reward_desc']."','".$data['respective_user']."','".(int)$data['reward_type']."','".$data['status']."', '$today_date')";
   
	$reward = mysqli_query($conn,$reward);
		if($reward){
			return $return;
		}
	}
	return $return;
}

function get_unique_id($prefix){
	return $prefix.substr(str_shuffle(str_repeat("0123456789", 15)), 0, 15);
}






/* ============================================= LOGIC ============================================================ */

$COMISSION_AMOUNT = 0;

// /var/www/vhosts/fairyindia.com/member.fairyindia.com/cron_job/tree_structure_pair_reward.php
// Get all users those who are completed tree structure.
$sql = "SELECT R.unique_user_id, T.position_left, T.position_right FROM registration AS R 
			LEFT JOIN tree_data AS T ON T.position_id = R.unique_user_id
			WHERE 
			R.type != 'admin' AND 
			R.active_deactive = 1 AND 
			R.is_rewarded_spl_tree_pair = 0 AND 
			R.active_status = 'Active' AND 
			(T.position_left != '' AND T.position_right != '')
			 LIMIT 10";
//echo $sql; die;
$con = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($con)) {
	
	if($row && !empty($row)) {
	
		if(check_is_user_purchased_product($row['position_left']) && check_is_user_purchased_product($row['position_right']))
		{
			$check_left_user = check_is_downline_user_active_and_purchased_product($row['position_left']);
			$check_right_user = check_is_downline_user_active_and_purchased_product($row['position_right']);
			if($check_left_user && $check_right_user) {
			  
				// Registration table update
				$sql_update_user_data = "UPDATE registration SET is_rewarded_spl_tree_pair = 1 WHERE unique_user_id = '". $row['unique_user_id'] ."' ";
			mysqli_query($conn, $sql_update_user_data);
			
				// 4= special tree structure reward bonus
				$reward = array(
					'user_id'			=>	$row['unique_user_id'],
					'reward'			=>	'Rs. '.$COMISSION_AMOUNT,
					'reward_desc'		=>	'Congratulations, you have got your special pairing bonus',
					'respective_user'	=>	null,
					'reward_type'		=>  4, // 4= special tree structure reward bonus
					'status'			=>	1, //  received
					'remarks'			=>  'Congratulations, you have got your special pairing bonus',
					'status'			=>	1,
					'transaction_id'	=>  get_unique_id('r')
				);
				give_reward($reward);
				
				echo 'Success for - ' . $row['unique_user_id'] . '<br>';
			}
			
			
		}
	
	}
	
}

//echo 'DONE';




?>