<?php
session_start();
ob_start();
 include "../config/config.php"; 
 include "../config/function.php"; 
 
 print_r($_REQUEST);
 $private_key=get_privatekey();
 $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
{
    $admin_per=get_admin();
    $tds_per=get_tds();
  
    $clossing_date=date("Y-m-d");
     $payment_date=date("Y-m-d"); //date("Y-m-d",strtotime($_POST['payment_date']));
     $emi_reference_no=trim($_POST['emi_reference_no']);
     $member_user_id=$_POST['member_user_id'];
     $member_name=$_POST['member_name'];
     $sponcer_id=$_POST['sponcer_id'];
     $sponcer_name=$_POST['sponcer_name'];
     $plot_code=$_POST['plot_code'];
     $plot_id=$_POST['plot_id'];
     $booking_random_no=$_POST['booking_random_no'];
     $paid_amount=trim($_POST['paid_amount']);
     $payment_mode=$_POST['payment_mode'];
     $bank_name=strtoupper(trim($_POST['bank_name']));
     $cheque_no=trim($_POST['cheque_no']);
     
     //For Project Name
     $prj="SELECT * FROM tbl_plot_registry_booking WHERE booking_random_no='$booking_random_no'";
     $resp=mysqli_query($connection,$prj);
     $rowp=mysqli_fetch_array($resp);
     $project_id=$rowp['project_id'];
     $bper=100;
    // if($project_id==16)
    //  {
    //      $bper=50;
    //  }
    //  echo $bper;
    // // die;
    $ssl="Select * from tbl_memberreg where member_user_id='$member_user_id'";
    $spres=mysqli_query($connection,$ssl);
    while($rowres=mysqli_fetch_array($spres))
    {
    	 $contact=$rowres['contact'];
    	 $member_id=$rowres['member_id'];
    }
    
    	$comm_business_amt=$paid_amount*$bper/100;
     //	total_comm_amt	comm_per
     echo "<br>". $str="Insert Into tbl_plot_emi_payment(emi_reference_no,plot_id,plot_code,booking_random_no,emi_date,entry_date,member_user_id,member_name,payment_amt,payment_mode,bank_name,cheque_no,total_comm_amt,comm_per,clossing_date ,comm_business_amt)
     values('$emi_reference_no','$plot_id','$plot_code','$booking_random_no','$payment_date',UTC_TIMESTAMP(),'$member_user_id','$member_name','$paid_amount','$payment_mode','$bank_name','$cheque_no',0,0,'$clossing_date',$comm_business_amt)";
    $res=mysqli_query($connection,$str) or die(mysqli_error($connection));
 
    	$next_due_date = date("Y-m-d",strtotime($payment_date. ' + 1 Month'));
    	 echo "<br>". $strb="Update tbl_plot_registry_booking set paid_amount=paid_amount+$paid_amount,balance_amt=balance_amt-$paid_amount,nest_emi_date='$next_due_date' where booking_random_no='$booking_random_no' and member_user_id='$member_user_id'";
    	$ress=mysqli_query($connection,$strb) or die(mysqli_error($connection));
    
    	//Set Total Plot Collectio Percentage
     echo "<br>". $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));
    	
    	//Deduct 20%
    	$business_amt=$paid_amount*$bper/100;
      //Update Direct Bussiness Amount
       mysqli_query($connection,"Update tbl_memberreg set direct_business=direct_business+$business_amt where member_user_id='$sponcer_id' and status!=3");
       
       //Calculate Direct Commssion
    	$dir_per=6;
       	$clossing_date=date("Y-m-d");
    
        $sstr="Select * from tbl_memberreg where  member_user_id='$sponcer_id' and status!=3";
        $ress=mysqli_query($connection,$sstr);
        while($rowsb=mysqli_fetch_array($ress))
        {
        	$direct_income=$business_amt*$dir_per/100;
        	$admin_amt=$direct_income*$admin_per/100;
        	$tds_amt=$direct_income*$tds_per/100;
         	$net_amt=$direct_income-$admin_amt-$tds_amt;
        	if($net_amt>0)
        	{
        	echo "<br>".	$stri="Insert Into tbl_member_income_dtails(member_user_id,calculate_date,income_amt,paid_status,income_type,b_type,income_member_user_id,tds_per,tds_amt,admin_per,admin_amt,net_amt,matching_business,booking_random_no,emi_reference_no,income_per,booking_amt,clossing_date)
        		values('$sponcer_id','$payment_date',$direct_income,0,'DIRECT INCOME','DIRECT INCOME','$member_user_id',$tds_per,$tds_amt,$admin_per,$admin_amt,$net_amt,$business_amt,'$booking_random_no','$emi_reference_no',$dir_per,$paid_amount,'$clossing_date')";
        		mysqli_query($connection,$stri) or die(mysqli_error($connection));
         	}
        }
    	cal_binary_busines($member_id,$business_amt);
    	calcaulate_binary_income($member_id,$business_amt,$booking_random_no,$payment_date,$emi_reference_no,$member_user_id,$paid_amount);
    	
    
    // 	$message=" Dear ".$member_name."(".$member_user_id."), Received Repayment of Rs ".$paid_amount." Aganist Of ". $plot_code." On Dated ". date("d-m-Y") ." .Your Transaction Id: ". $emi_reference_no ." For any query please contact us...Thanks!".$title_name;
    	//sendmysms($contact, $message);
    	//die();
    	
    	//For EMI Total Commssion
    	$total_comm_amt=get_total_comm_amt($emi_reference_no);
    	$stru="Update tbl_plot_emi_payment set total_comm_amt=$total_comm_amt,comm_per=($total_comm_amt*100)/payment_amt where emi_reference_no='$emi_reference_no'";
    	mysqli_query($connection,$stru);
    	
    	//For Plot Total Commssion Percentage
    	$strc="Update tbl_plot_registry_booking set comm_per=(total_comm_amt*100)/paid_amount,comm_amount=comm_amount+$total_comm_amt where booking_random_no='$booking_random_no'";
    	mysqli_query($connection,$strc);
    	
    
}
//	header("location:../EMIPaymentReceipt?emi_reference_no=$emi_reference_no");
 	header("location:../EMIPayment");

function calcaulate_binary_income($member_id,$topup_amount,$booking_random_no,$payment_date,$emi_reference_no,$income_member_user_id,$booking_amt)
{
	global $connection;
	echo $parrent_id=get_parrent_member_id();
	$binary_per=6;
    $admin_per=get_admin();
    $tds_per=get_tds();
// 	$str="Select * from tbl_dayclosed order by record_no desc";
//     $ress=mysqli_query($connection,$str);
//     $rows=mysqli_fetch_array($ress);
//     $clossing_date=date("Y-m-d",strtotime($rows['open_date']));
     
	while($member_id >= $parrent_id)
	{
		echo $mstr="Select * from tbl_memberreg where member_id='$member_id'"; 
		$mresult=mysqli_query($connection,$mstr);
			while($row=mysqli_fetch_array($mresult))
			{
				$max=0;$min=0;$matching_amt=0;$binary_income=0;$admin_amt=0;$tds_amt=0;$net_amt=0;
				$pramoter_id=$row['promoter_id'];
				$member_user_id=$row['member_user_id']; 
				$left_business=$row['left_business'];		
				$right_business=$row['right_business'];	
				$status=$row['status'];	
				$paid_business=$row['paid_business'];		
				if($left_business>$right_business)
				{
					$min=$right_business;
				}
				else if($left_business<$right_business)
				{
					$min=$left_business;
				}
				else
				{
					$min=$left_business;
				}
			
				$matching_amt=$min-$paid_business;		
				$binary_income=$matching_amt*$binary_per/100;	
				$admin_amt=$binary_income*$admin_per/100;
				$tds_amt=$binary_income*$tds_per/100;
				$net_amt=$binary_income-$admin_amt-$tds_amt;
				if($binary_income>0)
				{
    				 if($status==1)
    				 {
				echo 	$stri="Insert Into tbl_member_income_dtails(member_user_id,calculate_date,income_amt,paid_status,income_type,b_type,income_member_user_id,tds_per,tds_amt,admin_per,admin_amt,net_amt,matching_business,booking_random_no,emi_reference_no,income_per,booking_amt,clossing_date)
					values('$member_user_id','$payment_date',$binary_income,0,'BINARY INCOME','BINARY INCOME','$income_member_user_id',$tds_per,$tds_amt,$admin_per,$admin_amt,$net_amt,$matching_amt,'$booking_random_no','$emi_reference_no',$binary_per,$booking_amt,'$clossing_date')";
					mysqli_query($connection,$stri);
				
				echo	$str_u="Update  tbl_memberreg set paid_business=paid_business+$matching_amt where member_user_id='$member_user_id'";
					mysqli_query($connection,$str_u);
                    }
				}
				
			}
			$member_id=get_member_id($pramoter_id); echo "<br/>";
			if($member_id== $parrent_id)
    		{
    		    break;
    		}
    		
	}
}

function get_total_comm_amt($emi_reference_no)
{
	global $connection;
	$total_amt=0;
	$str="Select * from tbl_member_income_dtails where emi_reference_no='$emi_reference_no'";
	$result=mysqli_query($connection,$str);
	while($row=mysqli_fetch_array($result))
	{
		$total_amt=$total_amt+$row['income_amt'];
	}
	return $total_amt;
}

function get_direct_member($member_user_id,$position)
{
    global $connection;
    $str="Select * from tbl_memberreg where sponcer_id='$member_user_id' and postion='$position'";
    $result=mysqli_query($connection,$str);
    $cnt=mysqli_num_rows($result);
    return $cnt;

}

function cal_binary_busines($member_id,$topup_amt)
{
  	global $connection;
echo	$parrent_id=get_parrent_member_id(); echo "</br>";
echo $member_id;
//	die();
	while($member_id>= $parrent_id)
	{
         echo $mstr="Select * from tbl_memberreg where member_id='$member_id'"; 
		$mresult=mysqli_query($connection,$mstr);
		while($mrow=mysqli_fetch_array($mresult))
		{
			$left_business=0;$right_business=0;
		echo	$promoter=$mrow['promoter_id'];
			$postion=$mrow['position'];
			$mem_id=get_member_id($promoter); echo "<br/>";
			if($postion=="LEFT")
			{
				$left_business=$topup_amt;
			}
			else if($postion=="RIGHT")
			{
				$right_business=$topup_amt;
			}
		    echo $sql1="Update tbl_memberreg set left_business=left_business+$left_business,right_business=right_business+$right_business where member_user_id='$promoter' and status<2 and is_customer=0"; 
		   mysqli_query($connection,$sql1); 
		}
		echo $member_id=$mem_id;
		if($member_id== $parrent_id)
		{
		    break;
		}
	}

}

?>