<?php include("../Header/Session.php"); ?>
<?php include("../Header/Connection.php"); ?>

<?php
if (isset($_POST['Process_Profit_Sharing_By_Package_Setting'])) 
{	
	$ROI = $_POST['ROI'];
	$package_id = str_replace("PCK", "",$_POST['package_id']);
	$profit_date = date('Y-m-d H:i:s');

	$status = 1 ;
	date_default_timezone_set("Asia/Kuala_Lumpur");
    $created_date = date('Y-m-d H:i:s');

	$query = "SELECT SUM(package_price) As package_price, ref_code
	FROM package_sale
	Where id = '$package_id'
	And status = '1'
	And withdraw_status !='1'
	And user_id != '0'
	Order By user_id Asc";
	$result = $mysqli-> query($query);
	if ($result -> num_rows > 0)
	{
		while($record = $result -> fetch_assoc())
		{   
			$user_ref_code = $record['ref_code'];
			$buyer_ref_code = $record['ref_code'];
			$package_price = $record['package_price'];

			$profit_for_members_80 = 0.8 * (($ROI/100)*$package_price);
			$profit_for_others_20 = 0.2 * ($ROI/100)*$package_price;

			$trans_code = randomRefferalCode();

			//I will get 80%

			//Hq will get 5% from 20% 

			/******************************* Comission 5% from 20% For HQ Start Here ***********************************************/
			$Hq_ref_code = "MGP10000"; 
			$Level_commission = 'L01';

			commission($Level_commission, $Hq_ref_code, $buyer_ref_code, $package_price, $profit_for_others_20, $ROI, $trans_code, $profit_date);
			//commission(Level_commission, User_Ref_Code, Package_id, Package_name, Package_price, $Profit);
			/******************************* Comission 5% from 20% For HQ Start Here ***********************************************/

			/******************************* Comission 80% For Member Start Here ***********************************************/
			//Find Me And Update My Wallet
			$sql_user = "SELECT *
			FROM user
			Where ref_code = '$user_ref_code'";
			$result_user = $mysqli-> query($sql_user);
			if ($result_user -> num_rows == 1)
			{
				$row_user = $result_user ->fetch_assoc();
				$My_ref_code = $row_user['ref_code'];
				$My_sponsorlvl1_code = $row_user['sponsor_code'];	
				$Level_commission = '0';
			}

			$sql_activation = "SELECT * 
            FROM transactions
            WHERE type_of_process = 'Member Activate Account'
            AND user_ref_code = '$user_ref_code'
            AND status='1'
            ORDER BY id Desc
            LIMIT 1";
			$result_activation = $mysqli-> query($sql_activation);
			if ($result_activation -> num_rows == 1)
			{
				$row_activation = $result_activation ->fetch_assoc();
				$created_date = $row_activation['created_date'];
			}
			$date_12_month = date("Y-m-d", strtotime(date("Y-m-d", strtotime($created_date)) . " + 365 day"));

			if ($today < $date_12_month)
            {
				commission($Level_commission, $My_ref_code, $buyer_ref_code, $package_price, $profit_for_members_80, $ROI, $trans_code, $profit_date);
			}
			/******************************* Comission 80% For Member End Here ***********************************************/
		
			/******************************* Comission For My Sponsor LV1 start ***********************************************/
			if ( $My_sponsorlvl1_code != "" )
			{
				//Find my sponsor and check his Rank. If rank better than me, so give comission
				$sql_sponsorlvl1 = "SELECT *
				FROM user
				Where ref_code = '$My_sponsorlvl1_code'
				And activation_status = '1'
				And status = '1'";
				$result_sponsorlvl1 = $mysqli-> query($sql_sponsorlvl1);
				if ($result_sponsorlvl1 -> num_rows == 1)
				{
					$row_sponsorlvl1 = $result_sponsorlvl1 ->fetch_assoc();
					$My_sponsorlvl1_code = $row_sponsorlvl1['ref_code'];
					$My_sponsorlvl2_code = $row_sponsorlvl1['sponsor_code'];
					$Level_commission = '1';

					$sql_activation = "SELECT * 
					FROM transactions
					WHERE type_of_process = 'Member Activate Account'
					AND user_ref_code = '$My_sponsorlvl1_code'
					AND status='1'
					ORDER BY id Desc
					LIMIT 1";
					$result_activation = $mysqli-> query($sql_activation);
					if ($result_activation -> num_rows == 1)
					{
						$row_activation = $result_activation ->fetch_assoc();
						$created_date = $row_activation['created_date'];
					}

					$date_12_month = date("Y-m-d", strtotime(date("Y-m-d", strtotime($created_date)) . " + 365 day"));

					if ($today < $date_12_month)
					{
						commission($Level_commission, $My_sponsorlvl1_code, $buyer_ref_code, $package_price, $profit_for_others_20, $ROI, $trans_code, $profit_date);
					    //commission(Level_commission, User_Ref_Code, Package_id, Package_name, Package_price, $Profit);
					}
				}
			}
			else
			{
				$Hq_ref_code = "MGP10000";
				$Level_commission = '1';

				commission($Level_commission, $Hq_ref_code, $buyer_ref_code, $package_price, $profit_for_others_20, $ROI, $trans_code, $profit_date);
				//commission(Level_commission, User_Ref_Code, Package_id, Package_name, Package_price, $Profit);
			}
			/******************************* Comission For My Sponsor LV1 End **********************************/


			/******************************* Comission For My Sponsor LV2 start ***********************************************/
			if ( $My_sponsorlvl2_code != "" )
			{
				//Find my sponsor and check his Rank. If rank better than me, so give comission
				$sql_sponsorlvl2 = "SELECT *
				FROM user
				Where ref_code = '$My_sponsorlvl2_code'
				And activation_status = '1'
				And status = '1'";
				$result_sponsorlvl2 = $mysqli-> query($sql_sponsorlvl2);
				if ($result_sponsorlvl2 -> num_rows == 1)
				{
					$row_sponsorlvl2 = $result_sponsorlvl2 ->fetch_assoc();
					$My_sponsorlvl2_code = $row_sponsorlvl2['ref_code'];
					$My_sponsorlvl3_code = $row_sponsorlvl2['sponsor_code'];
					$Level_commission = '2';

					$sql_activation = "SELECT * 
					FROM transactions
					WHERE type_of_process = 'Member Activate Account'
					AND user_ref_code = '$My_sponsorlvl2_code'
					AND status='1'
					ORDER BY id Desc
					LIMIT 1";
					$result_activation = $mysqli-> query($sql_activation);
					if ($result_activation -> num_rows == 1)
					{
						$row_activation = $result_activation ->fetch_assoc();
						$created_date = $row_activation['created_date'];
					}

					$date_12_month = date("Y-m-d", strtotime(date("Y-m-d", strtotime($created_date)) . " + 365 day"));

					if ($today < $date_12_month)
					{
						commission($Level_commission, $My_sponsorlvl2_code, $buyer_ref_code, $package_price, $profit_for_others_20, $ROI, $trans_code, $profit_date);
					    //commission(Level_commission, User_Ref_Code, Package_id, Package_name, Package_price, $Profit);
					}
				}
			}
			else
			{
				$Hq_ref_code = "MGP10000";
				$Level_commission = '2';

				commission($Level_commission, $Hq_ref_code, $buyer_ref_code, $package_price, $profit_for_others_20, $ROI, $trans_code, $profit_date);
				//commission(Level_commission, User_Ref_Code, Package_id, Package_name, Package_price, $Profit);
			}
			/******************************* Comission For My Sponsor LV2 End **********************************/

		
			/******************************* Comission For My Sponsor LV3 start ***********************************************/
			if ( $My_sponsorlvl3_code != "" )
			{			
				//Find my sponsor and check his Rank. If rank better than me, so give comission
				$sql_sponsorlvl3 = "SELECT *
				FROM user
				Where ref_code = '$My_sponsorlvl3_code'
				And activation_status = '1'
				And status = '1'";
				$result_sponsorlvl3 = $mysqli-> query($sql_sponsorlvl3);
				if ($result_sponsorlvl3 -> num_rows == 1)
				{
					$row_sponsorlvl3 = $result_sponsorlvl3 ->fetch_assoc();
					$My_sponsorlvl3_code = $row_sponsorlvl3['ref_code'];
					$My_sponsorlvl4_code = $row_sponsorlvl3['sponsor_code'];
					$Level_commission = '3';

					$sql_activation = "SELECT * 
					FROM transactions
					WHERE type_of_process = 'Member Activate Account'
					AND user_ref_code = '$My_sponsorlvl3_code'
					AND status='1'
					ORDER BY id Desc
					LIMIT 1";
					$result_activation = $mysqli-> query($sql_activation);
					if ($result_activation -> num_rows == 1)
					{
						$row_activation = $result_activation ->fetch_assoc();
						$created_date = $row_activation['created_date'];
					}

					$date_12_month = date("Y-m-d", strtotime(date("Y-m-d", strtotime($created_date)) . " + 365 day"));

					if ($today < $date_12_month)
					{
						commission($Level_commission, $My_sponsorlvl3_code, $buyer_ref_code, $package_price, $profit_for_others_20, $ROI, $trans_code, $profit_date);
					    //commission(Level_commission, User_Ref_Code, Package_id, Package_name, Package_price, $Profit);
					}
				}	
			}
			else
			{
			    
				$Hq_ref_code = "MGP10000";
				$Level_commission = '3';

				commission($Level_commission, $Hq_ref_code, $buyer_ref_code, $package_price, $profit_for_others_20, $ROI, $trans_code, $profit_date);
				//commission(Level_commission, User_Ref_Code, Package_id, Package_name, Package_price, $Profit);
			}
			/******************************* Comission For My Sponsor LV3 End **********************************/


			/******************************* Comission For My Sponsor LV4 start ***********************************************/
			if ( $My_sponsorlvl4_code != "" )
			{
				//Find my sponsor and check his Rank. If rank better than me, so give comission
				$sql_sponsorlvl4 = "SELECT *
				FROM user
				Where ref_code = '$My_sponsorlvl4_code'
				And activation_status = '1'
				And status = '1'";
				$result_sponsorlvl4 = $mysqli-> query($sql_sponsorlvl4);
				if ($result_sponsorlvl4 -> num_rows == 1)
				{
					$row_sponsorlvl4 = $result_sponsorlvl4 ->fetch_assoc();
					$My_sponsorlvl4_code = $row_sponsorlvl4['ref_code'];
					$My_sponsorlvl5_code = $row_sponsorlvl4['sponsor_code'];
					$Level_commission = '4';

					$sql_activation = "SELECT * 
					FROM transactions
					WHERE type_of_process = 'Member Activate Account'
					AND user_ref_code = '$My_sponsorlvl4_code'
					AND status='1'
					ORDER BY id Desc
					LIMIT 1";
					$result_activation = $mysqli-> query($sql_activation);
					if ($result_activation -> num_rows == 1)
					{
						$row_activation = $result_activation ->fetch_assoc();
						$created_date = $row_activation['created_date'];
					}

					$date_12_month = date("Y-m-d", strtotime(date("Y-m-d", strtotime($created_date)) . " + 365 day"));

					if ($today < $date_12_month)
					{
						commission($Level_commission, $My_sponsorlvl4_code, $buyer_ref_code, $package_price, $profit_for_others_20, $ROI, $trans_code, $profit_date);
					    //commission(Level_commission, User_Ref_Code, Package_id, Package_name, Package_price, $Profit);
					}
				}	
			}
			else
			{
				$Hq_ref_code = "MGP10000";
				$Level_commission = '4';

				commission($Level_commission, $Hq_ref_code, $buyer_ref_code, $package_price, $profit_for_others_20, $ROI, $trans_code, $profit_date);
				//commission(Level_commission, User_Ref_Code, Package_id, Package_name, Package_price, $Profit);
			}
			/******************************* Comission For My Sponsor LV4 End **********************************/


			/******************************* Comission For My Sponsor LV5 start ***********************************************/
			if ( $My_sponsorlvl5_code != "" )
			{
				//Find my sponsor and check his Rank. If rank better than me, so give comission
				$sql_sponsorlvl5 = "SELECT *
				FROM user
				Where ref_code = '$My_sponsorlvl5_code'
				And activation_status = '1'
				And status = '1'";
				$result_sponsorlvl5 = $mysqli-> query($sql_sponsorlvl5);
				if ($result_sponsorlvl5 -> num_rows == 1)
				{
					$row_sponsorlvl5 = $result_sponsorlvl5 ->fetch_assoc();
					$My_sponsorlvl5_code = $row_sponsorlvl5['ref_code'];
					$My_sponsorlvl6_code = $row_sponsorlvl5['sponsor_code'];
					$Level_commission = '5';

					$sql_activation = "SELECT * 
					FROM transactions
					WHERE type_of_process = 'Member Activate Account'
					AND user_ref_code = '$My_sponsorlvl5_code'
					AND status='1'
					ORDER BY id Desc
					LIMIT 1";
					$result_activation = $mysqli-> query($sql_activation);
					if ($result_activation -> num_rows == 1)
					{
						$row_activation = $result_activation ->fetch_assoc();
						$created_date = $row_activation['created_date'];
					}

					$date_12_month = date("Y-m-d", strtotime(date("Y-m-d", strtotime($created_date)) . " + 365 day"));

					if ($today < $date_12_month)
					{
						commission($Level_commission, $My_sponsorlvl5_code, $buyer_ref_code, $package_price, $profit_for_others_20, $ROI, $trans_code, $profit_date);
					    //commission(Level_commission, User_Ref_Code, Package_id, Package_name, Package_price, $Profit);
					}
				}	
			}
			else
			{
				$Hq_ref_code = "MGP10000";
				$Level_commission = '5';

				commission($Level_commission, $Hq_ref_code, $buyer_ref_code, $package_price, $profit_for_others_20, $ROI, $trans_code, $profit_date);
				//commission(Level_commission, User_Ref_Code, Package_id, Package_name, Package_price, $Profit);
			}
			/******************************* Comission For My Sponsor LV5 End **********************************/


			/******************************* Comission For My Sponsor LV6 start ***********************************************/
			if ( $My_sponsorlvl6_code != "" )
			{
				//Find my sponsor and check his Rank. If rank better than me, so give comission
				$sql_sponsorlvl6 = "SELECT *
				FROM user
				Where ref_code = '$My_sponsorlvl6_code'
				And activation_status = '1'
				And status = '1'";
				$result_sponsorlvl6 = $mysqli-> query($sql_sponsorlvl6);
				if ($result_sponsorlvl6 -> num_rows == 1)
				{
					$row_sponsorlvl6 = $result_sponsorlvl6 ->fetch_assoc();
					$My_sponsorlvl6_code = $row_sponsorlvl6['ref_code'];
					$Level_commission = '6';

					$sql_activation = "SELECT * 
					FROM transactions
					WHERE type_of_process = 'Member Activate Account'
					AND user_ref_code = '$My_sponsorlvl6_code'
					AND status='1'
					ORDER BY id Desc
					LIMIT 1";
					$result_activation = $mysqli-> query($sql_activation);
					if ($result_activation -> num_rows == 1)
					{
						$row_activation = $result_activation ->fetch_assoc();
						$created_date = $row_activation['created_date'];
					}

					$date_12_month = date("Y-m-d", strtotime(date("Y-m-d", strtotime($created_date)) . " + 365 day"));

					if ($today < $date_12_month)
					{
						commission($Level_commission, $My_sponsorlvl6_code, $buyer_ref_code, $package_price, $profit_for_others_20, $ROI, $trans_code, $profit_date);
					    //commission(Level_commission, User_Ref_Code, Package_id, Package_name, Package_price, $Profit);
					}
				}	
			}
			else
			{
				$Hq_ref_code = "MGP10000";
				$Level_commission = '6';

				commission($Level_commission, $Hq_ref_code, $buyer_ref_code, $package_price, $profit_for_others_20, $ROI, $trans_code, $profit_date);
				//commission(Level_commission, User_Ref_Code, Package_id, Package_name, Package_price, $Profit);
			}
			/******************************* Comission For My Sponsor LV6 End **********************************/
		}
	}

	echo "<script type='text/javascript'>alert('Successfully Process Profit Sharing');</script>";
	echo "<script type='text/javascript'> document.location='../U/profit_sharing_setting_1.php'; </script>";
}

function commission($Level_commission, $User_ref_code, $buyer_ref_code, $Package_Price, $Profit_Amount, $ROI, $trans_code, $profit_date)
{			
	/*
	echo "<script type='text/javascript'>alert('Level_commission: $Level_commission');</script>";
	echo "<script type='text/javascript'>alert('User_ref_code: $User_ref_code');</script>";
	echo "<script type='text/javascript'>alert('Package_Id: $Package_Id');</script>";
	echo "<script type='text/javascript'>alert('Profit_Amount: $Profit_Amount');</script>";
	*/
	global $mysqli;
	
	date_default_timezone_set("Asia/Kuala_Lumpur");
	$created_date = $profit_date;

	//Check Profit Sharing Bonus Based On Level
	$sql_sharing_bonus = "SELECT * 
						  FROM profit_sharing 
						  WHERE level_number = '$Level_commission'
						  AND status = '1'";
	$result_sharing_bonus = $mysqli-> query($sql_sharing_bonus);
	if ($result_sharing_bonus -> num_rows > 0)
	{
		$row_sharing_bonus = $result_sharing_bonus ->fetch_assoc();
		$commission_percent_display = $row_sharing_bonus['commission_percent'];
		$commission_percent = $commission_percent_display/100;
	}

	//Check Package Buyer Detail
	$sql_buyer = "SELECT * FROM user 
	WHERE ref_code = '$buyer_ref_code'
	And status='1'";
	$result_buyer = $mysqli-> query($sql_buyer);
	if ($result_buyer -> num_rows > 0)
	{
		$row_buyer = $result_buyer ->fetch_assoc();
		$buyer_ref_code = $row_buyer['ref_code'];
		$buyer_name = $row_buyer['name'];
	}

	//Check Agent Detail
	$sql_user = "SELECT * FROM user 
	WHERE ref_code = '$User_ref_code'
	And status='1'";
	$result_user = $mysqli-> query($sql_user);
	if ($result_user -> num_rows > 0)
	{
		$row_user = $result_user ->fetch_assoc();
		$agent_ref_code = $row_user['ref_code'];
		$agent_name = $row_user['name'];
	}

	//Get Agent Wallet Info
	$sql_my_wallet = "SELECT * FROM wallet
	WHERE ref_code='$User_ref_code' LIMIT 1";
	$result_my_wallet = $mysqli-> query($sql_my_wallet);
	if ($result_my_wallet -> num_rows > 0)
	{
		$row_my_wallet = $result_my_wallet ->fetch_assoc();
		$my_cash_wallet = $row_my_wallet['cash_wallet'];
	}

	if($Level_commission == 'L01')//5% of 20% to HQ
	{
		$commission_percent = 0.05 ;
		$total_my_commission = $commission_percent * $Profit_Amount;
	}
	else
	if($Level_commission == '0')//80% to Member
	{
		$total_my_commission = $Profit_Amount;
	}
	else //Selected % of 20% to Upline
	{
		$total_my_commission = $commission_percent * $Profit_Amount;
	}

	//$total_my_commission = number_format($total_my_commission,2);
				
	$new_my_cash_wallet = $my_cash_wallet + $total_my_commission ;

	//$new_my_cash_wallet = number_format($new_my_cash_wallet,2);

	//Update My Wallet Info
	$Update_my_wallet = "UPDATE wallet SET
	cash_wallet='$new_my_cash_wallet'
	WHERE ref_code = '$User_ref_code' ";
	$result_my_wallet = $mysqli->query($Update_my_wallet);

	//Insert  Penaja Wallet Info
	$status_commission = '1';
	$in_transactions_commission = $total_my_commission;
	$out_transactions_commission = 0 ;

	if($Level_commission == 'L01')//5% of 20% to HQ
	{
		$description_commission = "HQ Profit Sharings Commission 5% | USD $total_my_commission For Total Package Price USD $Package_Price From $buyer_ref_code | $buyer_name" ;
		$type_of_process_commission = "HQ Profit Sharings";
	}
	else
	if($Level_commission == '0')//80% to Member
	{
		$description_commission = "Investment Profit 80% from ROI $ROI% | USD $total_my_commission For Total Package Price USD $Package_Price From $buyer_ref_code | $buyer_name" ;
		$type_of_process_commission = "Member Investment Profit";
	}
	else //Selected % of 20% to Upline
	{
		$description_commission = "Level $Level_commission Profit Sharing Commission $commission_percent_display% from ROI $ROI% | USD $total_my_commission For Total Package Price USD $Package_Price From $buyer_ref_code | $buyer_name" ;
		$type_of_process_commission = "Member Profit Sharings";
	}
							
	$balance_commission = $new_my_cash_wallet;
	$type_of_transaction_commission = "CW" ;
	
	$query_my_transactions_commission  = "INSERT INTO transactions
	(
	user_ref_code,
	trans_code,
	type_of_process,
	type_of_transaction,
	in_transactions,
	out_transactions,
	balance,
	description,
	status,
	created_date
	)
	VALUES
	(
	'$User_ref_code',
	'$trans_code',
	'$type_of_process_commission',
	'$type_of_transaction_commission',
	'$in_transactions_commission',
	'$out_transactions_commission',
	'$balance_commission',
	'$description_commission',
	'$status_commission',
	'$created_date'
	)";
	$res_my_transactions_commission = $mysqli->query($query_my_transactions_commission);
}
?>