<?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) && $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 '';
		}
	}
}

//  Wallet details create after transaction
function add_wallet_history($data){
	GLOBAL $conn;
	if(isset($data) && $data['sender_id'] !='' && $data['receiver_id'] !='' && $data['amount'] !='')
	{
		$date = date('Y-m-d H:i:s');
		$query = "INSERT INTO `wallet_new`(`sender_id`, `receiver_id`, `amount`, `respective_user`, `transaction_type`, `remarks`, `status`, `transaction_id`, `transaction_date`) 
		VALUES ('".$data['sender_id']."','".$data['receiver_id']."','".$data['amount']."','".$data['respective_user']."','".$data['transaction_type']."','".$data['remarks']."', '".$data['status']."','".$data['transaction_id']."','".$date."')";
		$connection=mysqli_query($conn,$query);
		$last_id = mysqli_insert_id($conn);
		if($connection)
		{
			if(isset($data['affiliate_id']) && $data['affiliate_id'] !='')
			{
				$upd_affiliate_id = "UPDATE wallet_new SET affiliate_id =  '".$data['affiliate_id']."' WHERE id = '$last_id' ";
				mysqli_query($conn,$upd_affiliate_id);
			}
			
			return true;
		}
	}
}








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



// /var/www/vhosts/fairyindia.com/member.fairyindia.com/cron_job/virtual_amount_transfer.php
// Get all users whose downline unsers are active and virtual amount not transfer yet
$sql = "SELECT R.unique_user_id, T.position_left, T.position_right FROM registration AS R 
			LEFT JOIN wallet_balance AS WB ON WB.balance_user_id = R.unique_user_id 
			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_virtual_amount_transfer = 0 AND 
			R.active_status = 'Active' AND 
			WB.total_balance_v > 0 AND 
			(T.position_left != '' AND T.position_right != '')
			 LIMIT 50";
//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']))
		{
		
			// Registration table update
			$sql_update_user_data = "UPDATE registration SET is_virtual_amount_transfer = 1 WHERE unique_user_id = '". $row['unique_user_id'] ."' ";
			mysqli_query($conn, $sql_update_user_data);
			
			// Wallet balance update
			$sql_update_wallet_data = "UPDATE wallet_balance SET total_balance = total_balance + 3000, total_balance_v = total_balance_v - 3000 WHERE balance_user_id = '". $row['unique_user_id'] ."' ";
			mysqli_query($conn, $sql_update_wallet_data);
			
			$wallet_arr = array(
				'sender_id'			=>	'B00001',
				'receiver_id'		=>	$row['unique_user_id'],
				'amount'			=>	'3000',
				'respective_user'	=>	$row['unique_user_id'],
				'transaction_type'	=>	1,  //  admin transffer to user
				'remarks'			=>  'First time joining bonus for '. $row['unique_user_id'],
				'status'			=>	1,
				'transaction_id'	=>  get_unique_id('t')
			);
			add_wallet_history($wallet_arr);
			
			echo 'Virtual amount has transfer of ' . $row['unique_user_id'];
			
		
		}
	
	}
	
}

//echo 'DONE';




?>