<?php
session_start();
ob_start();
 include "../config/config.php"; 
 include "../config/function.php"; 
 
//  print_r($_REQUEST);
$private_key=get_privatekey($_SESSION['admin_name']);
 $request_from_server=$_SERVER['HTTP_REFERER'];
$this_server=$_SERVER['HTTP_HOST'];
if($request_from_server!=''){
	check_access_url($request_from_server,$this_server);
}

// echo $_SESSION['login_status'];echo "<br>";
// echo $_SESSION['private_key'];echo "<br>";
// echo $_SESSION['admin_name'];echo "<br>";

if($_SESSION['login_status']!='YES' || $_SESSION['private_key']!=$private_key || $_SESSION['private_key']=='' || $_SESSION['admin_name']=='')
{
    echo "Blank";
	header("Location:../AppLogin");
	exit();
}
else
{
 
   echo "<br>". $emi_reference_no=trim($_REQUEST['emi_reference_no']);
    echo "<br>". $booking_random_no=$_REQUEST['booking_random_no'];
    
    $resGet=mysqli_query($connection,"Select * from tbl_plot_emi_payment where  	emi_reference_no='$emi_reference_no' and booking_random_no='$booking_random_no'");
    while($rowGet=mysqli_fetch_array($resGet))
    {
        $member_user_id=$rowGet['member_user_id'];
        $payment_amt=$rowGet['payment_amt'];
        $businessper=$rowGet['businessper'];
        $business_amt=$payment_amt*$businessper/100;
  
     mysqli_query($connection,"Delete From tbl_plot_emi_payment where emi_reference_no='$emi_reference_no'");
    
     
   echo "<br>". $ssl="Select * from tbl_memberreg where member_user_id='$member_user_id'";
    $spres=mysqli_query($connection,$ssl);
  
    while($rowres=mysqli_fetch_array($spres))
    {
    	 $member_id=$rowres['member_id'];
    	 $sponcer_id=$rowres['sponcer_id'];
	     mysqli_query($connection,"Update tbl_memberreg set direct_business=direct_business-$business_amt where member_user_id='$sponcer_id' ");
    }
    
	$strb="Update tbl_plot_registry_booking set paid_amount=paid_amount-$business_amt,balance_amt=balance_amt+$business_amt where booking_random_no='$booking_random_no' and member_user_id='$member_user_id'";
    mysqli_query($connection,$strb) or die(mysqli_error($connection));
    
  	//Set Total Plot Collectio Percentage
    $strp="Update tbl_plot_registry_booking set payment_per=(paid_amount*100)/net_amount where booking_random_no='$booking_random_no' and member_user_id='$member_user_id'";
    mysqli_query($connection,$strp) or die(mysqli_error($connection));
    	

        $bistr="Select * from tbl_member_income_dtails where emi_reference_no='$emi_reference_no' and income_type='BINARY INCOME'";
        $resw=mysqli_query($connection,$bistr);
        while($rowb=mysqli_fetch_array($resw))
        {
            $upline_Id=$rowb['member_user_id'];
            $matching_business=$rowb['matching_business'];
            $str_u="Update  tbl_memberreg set paid_business=paid_business-$matching_business where member_user_id='$upline_Id'";
        	mysqli_query($connection,$str_u);
        }
        $stri="Delete From  tbl_member_income_dtails where emi_reference_no='$emi_reference_no'";
        mysqli_query($connection,$stri);
 
    $totalMember=gettotal();
   deduct_busines($totalMember,$business_amt,$member_user_id);
    	
    }
    
}
header("location:../ConfirmEMIPayment");


function deduct_busines($totalMember,$topup_amt,$member_user_id)
{
  	global $connection;
	$parrent_id="MKM0000001"; 
	$MemberIdn=$member_user_id;
	for($cnt=1;$cnt<=$totalMember;$cnt++)
	{
       $mstr="Select * from tbl_memberreg where member_user_id='$MemberIdn'"; 
		$mresult=mysqli_query($connection,$mstr);
		while($mrow=mysqli_fetch_array($mresult))
		{
			$left_business=0;$right_business=0;
			$promoter_id=$mrow['promoter_id'];
			$postion=$mrow['position'];
			if($postion=="LEFT")
			{
				$left_business=$topup_amt;
			}
			else if($postion=="RIGHT")
			{
				$right_business=$topup_amt;
			}
		    $sql1="Update tbl_memberreg set left_business=left_business-$left_business,right_business=right_business-$right_business where member_user_id='$promoter_id' and status<2 and is_customer=0"; 
		   mysqli_query($connection,$sql1) or die(mysqli_error($connection));
		   //mysqli_query($connection,$sql1) or die(mysqli_error($connection));
		}
		$MemberIdn=$promoter_id;
		if($MemberIdn== "MKM0000001")
		{
		    $cnt=$cnt*10;
		    break;
		}
	}

}

function gettotal()
{
    global $connection;
    $resGet=mysqli_query($connection,"Select count(member_user_id) as cnt from tbl_memberreg");
    $rowGet=mysqli_fetch_array($resGet);
    return $cntTotal=$rowGet['cnt'];
}
?>