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

<!DOCTYPE html>
<html>
<?php 

    $trans_code = $_GET['trans_code'];
	$from_page = $_GET['from_page'];
	$activation_charge = $_GET['activation_charge'];
	$AP_status_id = '1';

	if($AP_status_id == '1') //If success payment
	{
		$status = 1;
	
		$sql = "SELECT * 
		FROM transactions 
		WHERE trans_code='$trans_code'
		And type_of_transaction = 'PW' ";
		$result = $mysqli-> query($sql);
		if ($result -> num_rows > 0)
		{
			$row = $result ->fetch_assoc();
			$user_ref_code = $row['user_ref_code'];
		}

		//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 > 0)
		{
			$row_user = $result_user ->fetch_assoc();
			$My_ref_code = $row_user['ref_code'];
			$My_sponsorlvl1_code = $row_user['sponsor_code'];
			$Name = $row_user['name'];
			$Email = $row_user['email'];
		}

		if ($from_page == "user_detail.php")
		{
			$page_success = "user_detail.php?user_ref_code=".$user_ref_code;
			$page_fail = "user_detail.php?user_ref_code=".$user_ref_code;
		}
		else
		if ($from_page == "activate_account.php")
		{
		   $page_success = "logout.php";
		   $page_fail = "activate_account.php";
		}
		else
		if ($from_page == "user_main_dashboard.php")
		{
		   $page_success = "user_main_dashboard.php";
		   $page_fail = "user_main_dashboard.php";
		}

		$trans_code = randomRefferalCode();
		

		if ($activation_charge == "Not_Free")
		{
			
			/******************************* Comission 30% For HQ Start Here ***********************************************/
			$Hq_ref_code = "MGP10000"; 
			$Level_commission = '0';

			commission($Level_commission, $Hq_ref_code , $Name, $Email, $trans_code);
			//commission(Level_commission, User_Ref_Code, New_Agent_Name, New_Agent_Email);
			/******************************* Comission 5% from 20% For HQ Start 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 status = '1'";
				$result_sponsorlvl1 = $mysqli-> query($sql_sponsorlvl1);
				if ($result_sponsorlvl1 -> num_rows > 0)
				{
					$row_sponsorlvl1 = $result_sponsorlvl1 ->fetch_assoc();
					$My_sponsorlvl1_code = $row_sponsorlvl1['ref_code'];
					$My_sponsorlvl1_activation_status = $row_sponsorlvl1['activation_status'];
					$My_sponsorlvl2_code = $row_sponsorlvl1['sponsor_code'];
					$Level_commission = '1';

					if ($My_sponsorlvl1_activation_status == "1")
					{
						commission($Level_commission, $My_sponsorlvl1_code , $Name, $Email, $trans_code);
						//commission(Level_commission, User_Ref_Code, New_Agent_Name, New_Agent_Email);
					}
					else
					{
						$Hq_ref_code = "MGP10000";
						$Level_commission = '1';

						commission($Level_commission, $Hq_ref_code , $Name, $Email. $trans_code);    
					}
				}
			}
			else
			{
				$Hq_ref_code = "MGP10000";
				$Level_commission = '1';

				commission($Level_commission, $Hq_ref_code , $Name, $Email. $trans_code);    
			}
			/******************************* 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 status = '1'";
				$result_sponsorlvl2 = $mysqli-> query($sql_sponsorlvl2);
				if ($result_sponsorlvl2 -> num_rows > 0)
				{
					$row_sponsorlvl2 = $result_sponsorlvl2 ->fetch_assoc();
					$My_sponsorlvl2_code = $row_sponsorlvl2['ref_code'];
					$My_sponsorlvl2_activation_status = $row_sponsorlvl2['activation_status'];
					$My_sponsorlvl3_code = $row_sponsorlvl2['sponsor_code'];
					$Level_commission = '2';

					if ($My_sponsorlvl2_activation_status == "1")
					{
						commission($Level_commission, $My_sponsorlvl2_code , $Name, $Email, $trans_code);
						//commission(Level_commission, User_Ref_Code, New_Agent_Name, New_Agent_Email);
					}
					else
					{
						$Hq_ref_code = "MGP10000";
						$Level_commission = '2';

						commission($Level_commission, $Hq_ref_code , $Name, $Email, $trans_code);    
					}
				}
			}
			else
			{
				$Hq_ref_code = "MGP10000";
				$Level_commission = '2';

				commission($Level_commission, $Hq_ref_code , $Name, $Email, $trans_code);    
			}
			/******************************* 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 status = '1'";
				$result_sponsorlvl3 = $mysqli-> query($sql_sponsorlvl3);
				if ($result_sponsorlvl3 -> num_rows > 0)
				{
					$row_sponsorlvl3 = $result_sponsorlvl3 ->fetch_assoc();
					$My_sponsorlvl3_code = $row_sponsorlvl3['ref_code'];
					$My_sponsorlvl3_activation_status = $row_sponsorlvl3['activation_status'];
					$My_sponsorlvl4_code = $row_sponsorlvl3['sponsor_code'];
					$Level_commission = '3';

					if ($My_sponsorlvl3_activation_status == "1")
					{
						commission($Level_commission, $My_sponsorlvl3_code , $Name, $Email, $trans_code);
						//commission(Level_commission, User_Ref_Code, New_Agent_Name, New_Agent_Email);
					}
					else
					{
						$Hq_ref_code = "MGP10000";
						$Level_commission = '3';

						commission($Level_commission, $Hq_ref_code , $Name, $Email, $trans_code);    
					}
				}
			}
			else
			{
				$Hq_ref_code = "MGP10000";
				$Level_commission = '3';

				commission($Level_commission, $Hq_ref_code , $Name, $Email, $trans_code);    
			}
			/******************************* 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 status = '1'";
				$result_sponsorlvl4 = $mysqli-> query($sql_sponsorlvl4);
				if ($result_sponsorlvl4 -> num_rows > 0)
				{
					$row_sponsorlvl4 = $result_sponsorlvl4 ->fetch_assoc();
					$My_sponsorlvl4_code = $row_sponsorlvl4['ref_code'];
					$My_sponsorlvl4_activation_status = $row_sponsorlvl4['activation_status'];
					$My_sponsorlvl5_code = $row_sponsorlvl4['sponsor_code'];
					$Level_commission = '4';

					if ($My_sponsorlvl4_activation_status == "1")
					{
						commission($Level_commission, $My_sponsorlvl4_code , $Name, $Email, $trans_code);
						//commission(Level_commission, User_Ref_Code, New_Agent_Name, New_Agent_Email);
					}
					else
					{
						$Hq_ref_code = "MGP10000";
						$Level_commission = '4';

						commission($Level_commission, $Hq_ref_code , $Name, $Email, $trans_code);    
					}
				}
			}
			else
			{
				$Hq_ref_code = "MGP10000";
				$Level_commission = '4';

				commission($Level_commission, $Hq_ref_code , $Name, $Email, $trans_code);    
			}
			/******************************* 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 status = '1'";
				$result_sponsorlvl5 = $mysqli-> query($sql_sponsorlvl5);
				if ($result_sponsorlvl5 -> num_rows > 0)
				{
					$row_sponsorlvl5 = $result_sponsorlvl5 ->fetch_assoc();
					$My_sponsorlvl5_code = $row_sponsorlvl5['ref_code'];
					$My_sponsorlvl5_activation_status = $row_sponsorlvl5['activation_status'];
					$My_sponsorlvl6_code = $row_sponsorlvl5['sponsor_code'];
					$Level_commission = '5';

					if ($My_sponsorlvl5_activation_status == "1")
					{
						commission($Level_commission, $My_sponsorlvl5_code , $Name, $Email, $trans_code);
						//commission(Level_commission, User_Ref_Code, New_Agent_Name, New_Agent_Email);
					}
					else
					{
						$Hq_ref_code = "MGP10000";
						$Level_commission = '5';

						commission($Level_commission, $Hq_ref_code , $Name, $Email, $trans_code);
					}
				}
			}
			else
			{
				$Hq_ref_code = "MGP10000";
				$Level_commission = '5';

				commission($Level_commission, $Hq_ref_code , $Name, $Email, $trans_code);    
			}
			/******************************* 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 status = '1'";
				$result_sponsorlvl6 = $mysqli-> query($sql_sponsorlvl6);
				if ($result_sponsorlvl6 -> num_rows > 0)
				{
					$row_sponsorlvl6 = $result_sponsorlvl6 ->fetch_assoc();
					$My_sponsorlvl6_code = $row_sponsorlvl6['ref_code'];
					$My_sponsorlvl6_activation_status = $row_sponsorlvl6['activation_status'];
					$Level_commission = '6';

					if ($My_sponsorlvl6_activation_status == "1")
					{
						commission($Level_commission, $My_sponsorlvl6_code , $Name, $Email, $trans_code);
						//commission(Level_commission, User_Ref_Code, New_Agent_Name, New_Agent_Email);
					}
					else
					{
						$Hq_ref_code = "MGP10000";
						$Level_commission = '6';

						commission($Level_commission, $Hq_ref_code , $Name, $Email, $trans_code);    
					}
				}	
			}
			else
			{
				$Hq_ref_code = "MGP10000";
				$Level_commission = '6';

				commission($Level_commission, $Hq_ref_code , $Name, $Email, $trans_code);    
			}
			/******************************* Comission For My Sponsor LV6 End **********************************/
		}

        

		/********************** Email To HQ *************************************************/
			$hq_email = "mygoldplt_websystemmambrur@gmail.com";
			$to_hq = $hq_email;
			$subject_hq = "Hi $hq_email, mygoldsdnbhd.com Activation Of Members $My_ref_code";
        
			$message_hq = "
			<html>
			<head>
			<title>Hi $hq_email, mygoldsdnbhd.com Activation Of Members $My_ref_code</title>
			</head>
			<body>
			<p>

			Hai $hq_email,<br><br>

			mygoldsdnbhd.com Activation Of Members $My_ref_code | $Email  <br><br>
			</p>

			</body>
			</html>
			";
        
			// Always set content-type when sending HTML email
			$headers_hq = "MIME-Version: 1.0" . "\r\n";
			$headers_hq .= 'Content-type:text/html;charset=UTF-8' . "\r\n";
        
			// More headers
			$headers_hq .= 'From: <no-reply@mygoldsdnbhd.com>' . "\r\n";
			$headers_hq .= "Reply-To: no-reply@mygoldsdnbhd.com\r\n";
			$headers_hq .= "Return-Path: no-reply@mygoldsdnbhd.com\r\n";
			mail($to_hq, $subject_hq, $message_hq, $headers_hq);
		/********************** Email To HQ *************************************************/

		/********************** Email To User *************************************************/
			$to      = $Email;
			$subject = "mygoldsdnbhd.com | Hi $Name, mygoldsdnbhd.com Account Activation";
        
			$message = "
			<html>
			<head>
			<title>Hi $Name,  mygoldsdnbhd.com Account Activation</title>
			</head>
			<body>
			<p>

			Hi $Name,<br><br>

			Well Done. Your account succesfully activated!
			</p>

			</body>
			</html>
			";
        
			// Always set content-type when sending HTML email
			$headers = "MIME-Version: 1.0" . "\r\n";
			$headers .= 'Content-type:text/html;charset=UTF-8' . "\r\n";
        
			// More headers
			$headers .= 'From: <mygoldplt_websystemmambrur@gmail.com >' . "\r\n";
			$headers .= "Reply-To: mygoldplt_websystemmambrur@gmail.com \r\n";
			$headers .= "Return-Path: mygoldplt_websystemmambrur@gmail.com \r\n";
			mail($to, $subject, $message, $headers);
		/********************** Email To User *************************************************/

		echo "<script type='text/javascript'>alert('Succesfully Activate Account');</script>";
		echo "<script type='text/javascript'> document.location='$page_success'; </script>";
		exit();
								
	}
	else // If not success
	{									
		echo "<script type='text/javascript'>alert('Payment Fail');</script>";
		echo "<script type='text/javascript'> document.location='$page_fail'; </script>";
		exit();
	}	


function commission($Level_commission, $User_ref_code , $Name, $Email, $trans_code)
{		
	global $mysqli;
	
	date_default_timezone_set("Asia/Kuala_Lumpur");
	$created_date = date('Y-m-d H:i:s');

	$sql_reg_price = "SELECT *
	FROM registration_price_setting
	Where status='1'
	order by id Asc";
	$result_reg_price = $mysqli-> query($sql_reg_price);
	if ($result_reg_price -> num_rows > 0)
	{
		$row_reg_price = $result_reg_price ->fetch_assoc();
		$full_total_price = $row_reg_price['price'];
	}

	//Check Referral Bonus Based On Level
	$sql_referral_bonus = "SELECT * 
						   FROM referral_bonus 
						   WHERE level_number = '$Level_commission'
						   AND status = '1'";
	$result_referral_bonus = $mysqli-> query($sql_referral_bonus);
	if ($result_referral_bonus -> num_rows > 0)
	{
		$row_referral_bonus = $result_referral_bonus ->fetch_assoc();
		$commission_percent_display = $row_referral_bonus['commission_percent'];
	}

	$commission_percent = $commission_percent_display/100;

	//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'];
	}

	$total_my_commission = $commission_percent * $full_total_price;
	$new_my_cash_wallet = $my_cash_wallet + $total_my_commission ;

	//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 ;
	$description_commission = "Level $Level_commission Referral Bonus Commission $commission_percent_display% | $total_my_commission$ For Registration Of New Member $Email | $Name " ;
	$balance_commission = $new_my_cash_wallet;
	$type_of_transaction_commission = "CW" ;

	if($Level_commission == '0')
	{
	  $type_of_process_commission = "HQ Referral Bonus";
	}
	else
	{
      $type_of_process_commission = "Member Referral Bonus";
	}
	

	$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);
}
?>
</html>