<?php
session_start();
ob_start();
 include "../config/config.php"; 
 include "../config/function.php"; 
 
 $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
{
    $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=RemoveSpecialChar($_POST['emi_reference_no']);
     $member_user_id=RemoveSpecialChar($_POST['member_user_id']);
     $member_name=RemoveSpecialChar($_POST['member_name']);
     $sponcer_id=RemoveSpecialChar($_POST['sponcer_id']);
     $sponcer_name=RemoveSpecialChar($_POST['sponcer_name']);
     $plot_code=RemoveSpecialChar($_POST['plot_code']);
     $booking_random_no=RemoveSpecialChar($_POST['booking_random_no']);
     $paid_amount=RemoveSpecialChar($_POST['paid_amount']);
     $plotTotalAmt=RemoveSpecialChar($_POST['net_amount']);
     $payment_mode=RemoveSpecialChar($_POST['payment_mode']);
     $businessper=RemoveSpecialChar($_POST['businessper']);
     $bank_name=strtoupper(RemoveSpecialChar($_POST['bank_name']));
     $cheque_no=RemoveSpecialChar($_POST['cheque_no']);
     $remark=RemoveSpecialChar($_POST['remark']);
     
     //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);
     $businessper=$rowp['businessper'];
     $booking_registred=$rowp['booking_registred'];

    $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*$businessper/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,comm_business_amt,businessper,remark,plotTotalAmt,booking_registred,isverify)
     values('$emi_reference_no','$plot_code','$plot_code','$booking_random_no','$payment_date',UTC_TIMESTAMP(),'$member_user_id','$member_name','$paid_amount','$payment_mode','$bank_name','$cheque_no',0,0,$comm_business_amt,$businessper,'$remark','$plotTotalAmt',$booking_registred,1)";
    $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));
    	
    // 	$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);
   

}
	header("location:../PrintReceipt?emi_reference_no=$emi_reference_no");


?>