<?php 
//ECHO "testing page!";
include("session_set.php");
//error_reporting(0); 

//set time zone
date_default_timezone_set("Asia/Kuala_Lumpur");


$tenant_id = $_POST["tenant_id"];
 " deposit_bill_id :: " .$deposit_bill_id = $_POST["deposit_bill_id"];
$deposit_bill_other_id = $_POST["deposit_bill_other_id"];
$staff_id = $_SESSION["staff_id"];
$location_id = $_SESSION["location_id"];
 " twoMonthRental_payNow : " . $twoMonthRental_payNow = $_POST["twoMonthRental_payNow"];
 "\n utility_payNow : " .  $utility_payNow = $_POST["utility_payNow"];


$location_code = $_SESSION["location_code"];
$project_code = $_SESSION["project_code"];
$depositPaymentID_print = "";
$depositPaymentOtherID_print = "";


if($twoMonthRental_payNow != "0.00" || $utility_payNow != "0.00"){
    
    //get bill_code from bill name start
    $receiptNoDeposit = "";
    $bill_name = "DEPOSIT";
    
	include('bill_code.php');
    //get bill_code from bill name end

    //Check running number from receipt_number table based on location_id, bill_type_id, and payment_type - start
    $serialNo = "";
     $sqlRunningNo = "select running_no from receipt_number where location_code = '" . $location_code . "' and  bill_type_id = " . $bill_type_id . " and payment_type = '" . $bill_code . "' order by running_no DESC limit 1";
    $runRunningNo = mysqli_query($link, $sqlRunningNo);
    if($runRunningNo === FALSE){
        die(mysqli_error($link));
    }

    //$running_no = $resultRunningNo["running_no"];
    if(mysqli_num_rows($runRunningNo) == 1){
        $resultRunningNo = mysqli_fetch_assoc($runRunningNo);
        $running_no = $resultRunningNo["running_no"];
   
        $running_no++;
        $strlenRunningNo = strlen($running_no);
        
        /* for($i=$strlenRunningNo;$i<=1;$i++){
            $serialNo = '0'. $running_no;
        } */
		if($strlenRunningNo == 1){
			$serialNo = '0'. $running_no;
		}
		ELSE
			$serialNo = $running_no;
		
    }
    else if(mysqli_num_rows($runRunningNo) == 0){
        $serialNo = '01';
        $running_no = 1;
    
    }
    //Check running number from receipt_number table based on location_id, bill_type_id, and payment_type - end

    " receiptNoDeposit:: ". $receiptNoDeposit =  $location_code . '/'.$bill_code . '/' . $serialNo;
    //running no class based on locations
    //check deposit_bills created or not - start
	 " sqlDepositBillsCheck :: " . $sqlDepositBillsCheck = "select id from deposit_bills where tenant_id = $tenant_id";
	$runDepositBillsCheck = mysqli_query($link, $sqlDepositBillsCheck);
    if($runDepositBillsCheck === FALSE){
        die(mysqli_error($link));
    }
	$countResultDepositBillCheck = mysqli_num_rows($runDepositBillsCheck);
	if($countResultDepositBillCheck > 0){ //deposit bills found
		//UPDATE table deposit_bills
		$resultDepositBillID = mysqli_fetch_assoc($runDepositBillsCheck);
		$depositBillID_db = $resultDepositBillID["id"];
		
		IF( $deposit_bill_id == $depositBillID_db){ //DEPOSIT BILL DAH CREATE
			// INSERT TABLE deposit_payments
			 " SQL INSERT DEPOSIT PAYMENT 2::".$sqlInsertDepositPayment = "insert into deposit_payments (receipt_no, two_months_amount_paid, utility_amount_paid, deposit_bill_id, created_by, created_at, updated_at) 
																			values (
																					'".$receiptNoDeposit."',
																					'".$twoMonthRental_payNow."',
																					'".$utility_payNow."',
																					'".$deposit_bill_id."',
																					'".$staff_id."',
																					'".date("Y-m-d H:i:s")."',
																					'".date("Y-m-d H:i:s")."'
																					)";

			$runInsertDepositPayment = mysqli_query($link, $sqlInsertDepositPayment);
			if($runInsertDepositPayment === FALSE){
				die(mysqli_error($link));
			}
			$depositPaymentID_print = mysqli_insert_id($link);
			//INSERT table receipt_number
			
			$sqlInsertReceiptNo = "insert into receipt_number (location_code, bill_type_id, payment_type, running_no, serial_no, updated_at, created_at) values (
									'".$location_code."',
									'".$bill_type_id."',
									'".$bill_code."',
									'".$running_no."',
									'".$receiptNoDeposit."',
									'".date("Y-m-d H:i:s")."',
									'".date("Y-m-d H:i:s")."'
									)";
			$runInsertReceiptNo = mysqli_query($link, $sqlInsertReceiptNo);
			if($runInsertReceiptNo === FALSE){
				die(mysqli_error($link));
			}
		}
	}
	else if($countResultDepositBillCheck == 0){ //deposit bills NOT found
			//INSERT TABLE deposit_bills
			//set deposit bill no start
		 $sqlDepositBill_runningNo = "select deposit_bill_no from deposit_bills 
									inner join tenants on deposit_bills.tenant_id = tenants.id
									inner join properties on tenants.property_id = properties.id
									inner join locations on properties.location_id = locations.id 
									inner join projects on locations.project_id = projects.id 
									where locations.id = $location_id order by deposit_bills.id DESC limit 1";
		$runDepositBill_runningNo = mysqli_query($link, $sqlDepositBill_runningNo);
		if($runDepositBill_runningNo === FALSE){
			die(mysqli_error($link));
		}
		if(mysqli_num_rows($runDepositBill_runningNo) == 1){
			$resultDepositBill_runningNo = mysqli_fetch_assoc($runDepositBill_runningNo);
			$deposit_bill_no = $resultDepositBill_runningNo["deposit_bill_no"];
			//split by '-'
			//split locationName with rental_desc
			
		   // $deposit_bill_no_split = substr($deposit_bill_no, strpos($deposit_bill_no, "/") + 1);   
			$deposit_bill_no_split = explode('/',$deposit_bill_no);   
			//susunan deposit_bill_no = [project_code]/[location_code]/[bill_code]/[running_no]
			//0->[project_code]
			//1->[location_code]
			//2->[bill_code]
			//3->[running_no]

			//$arr = explode("-", $deposit_bill_no, 2);
			$deposit_bill_no = $deposit_bill_no_split[3];
			$deposit_bill_no++;
			$strlenRunningNo = strlen($deposit_bill_no);
			
			if($strlenRunningNo == 1){
				$deposit_bill_no = '0'. $deposit_bill_no;
			}
			ELSE
				$deposit_bill_no = $deposit_bill_no;
			
			
			
			/* if($strlenRunningNo <=1 ){
				 for($i=$strlenRunningNo;$i<=1;$i++){
					 $deposit_bill_no = '0'. $deposit_bill_no;
				 }
			} */
				
		}
		else if(mysqli_num_rows($runDepositBill_runningNo) == 0){
			$deposit_bill_no =  '01';
			$running_no = 1;
			//$deposit_bill_no = $deposit_bill_no;
		   
		}

		$deposit_bill_no = "INV/". $location_code . '/'.$bill_code . '/' . $deposit_bill_no;
		//set deposit bill no end

		//set bill for 2 months rental and utility start
		//$twoMonthRental = $rental_rate*2;
		if( $deposit_bill_id == ""){ //DEPOSIT BILL BELUM CREATE LAGI
			$insert_depositBillSql = "insert into deposit_bills (deposit_bill_no, two_month_rental_due, two_month_rental_amount, utility_due, utility_amount, payment_status, tenant_id, created_by, created_at, updated_at ) 
										values (
													'".$deposit_bill_no."',
													'".$twoMonthRental."',
													'".$twoMonthRental."',
													'".$utility."',
													'".$utility."',
													'unpaid',
													'".$tenant_id."',
													'".$staff_id."',
													'".date('Y-m-d')."',
													'".date('Y-m-d')."'
													)";
			$runInsert_depositBillSql = mysqli_query($link, $insert_depositBillSql);
			if($runInsert_depositBillSql === FALSE){
				die(mysqli_error($link));
			}
			$deposit_bill_id = mysqli_insert_id($link);
			// INSERT TABLE deposit_payments
			 " SQL INSERT DEPOSIT PAYMENT 1::".$sqlInsertDepositPayment = "insert into deposit_payments (receipt_no, two_months_amount_paid, utility_amount_paid, deposit_bill_id, created_by, created_at, updated_at)
										values (
										'".$receiptNoDeposit."',
										'".$twoMonthRental_payNow."',
										'".$utility_payNow."',
										'".$deposit_bill_id."',
										'".$staff_id."',
										'".date("Y-m-d H:i:s")."',
										'".date("Y-m-d H:i:s")."'
										)";

			$runInsertDepositPayment = mysqli_query($link, $sqlInsertDepositPayment);
			if($runInsertDepositPayment === FALSE){
				die(mysqli_error($link));
			}
			$depositPaymentID_print = mysqli_insert_id($link);
			//INSERT table receipt_number
			
			$sqlInsertReceiptNo = "insert into receipt_number (location_code, bill_type_id, payment_type, running_no, serial_no, updated_at, created_at) values (
									'".$location_code."',
									'".$bill_type_id."',
									'".$bill_code."',
									'".$running_no."',
									'".$receiptNoDeposit."',
									'".date("Y-m-d H:i:s")."',
									'".date("Y-m-d H:i:s")."'
									)";
			$runInsertReceiptNo = mysqli_query($link, $sqlInsertReceiptNo);
			if($runInsertReceiptNo === FALSE){
				die(mysqli_error($link));
			}
		}
	}
    //check deposit_bills created or not - end
	
	
	
    // UPDATE TABLE deposit_bills
    //check all payment knock-off with deposit bills based on deposit bill id
    $sqlDepositPayments = "select sum(two_months_amount_paid) as total2MonthAmountPaid, sum(utility_amount_paid) as totalUtilityAmountPaid from deposit_payments where deposit_bill_id = " .$deposit_bill_id;
    $runDepositPayments = mysqli_query($link, $sqlDepositPayments);
    if($runDepositPayments === FALSE){
        die(mysqli_error($link));
    }
    $resultDepositPayments = mysqli_fetch_assoc($runDepositPayments);
    $total2MonthAmountPaid = $resultDepositPayments["total2MonthAmountPaid"];
    $totalUtilityAmountPaid = $resultDepositPayments["totalUtilityAmountPaid"];

    $sqlDepositBills = "select two_month_rental_amount, utility_amount, payment_status from deposit_bills where id = " .$deposit_bill_id;
    $runDepositBills = mysqli_query($link, $sqlDepositBills);
    if($runDepositBills === FALSE){
        die(mysqli_error($link));
    }
    $resultDepositBills = mysqli_fetch_assoc($runDepositBills);
    $two_month_rental_amount = $resultDepositBills["two_month_rental_amount"];
    $utility_amount = $resultDepositBills["utility_amount"];
    $payment_status = $resultDepositBills["payment_status"];

    $twoMonthRental_due = $two_month_rental_amount - $total2MonthAmountPaid;
    $utility_due = $utility_amount - $totalUtilityAmountPaid;
    $totalDepositAmount = $two_month_rental_amount + $utility_amount;
    $totalDepositPaid = $total2MonthAmountPaid + $totalUtilityAmountPaid;
    $totalDepositDue = $totalDepositAmount - $totalDepositPaid;
    //SET PAYMENT_STATUS IN DEPOSIT BILLS
    if($totalDepositDue == 0.00 && $totalDepositAmount == $totalDepositPaid)
        $payment_status = "paid";
    else if($totalDepositDue < $totalDepositAmount && $totalDepositDue != 0.00)
        $payment_status = "partial";   
    else if($totalDepositDue == $totalDepositAmount)
        $payment_status = "unpaid"; 
    // UPDATE TABLE deposit_bills
    $sqlUpdateDepositBills = "update deposit_bills set 
                                two_month_rental_due = " . $twoMonthRental_due . 
                                ", utility_due = " . $utility_due . 
                                ", payment_status = '" . $payment_status . "'" .
                                " where id = " . $deposit_bill_id;
    $runUpdateDepositBills = mysqli_query($link, $sqlUpdateDepositBills);
    if($runUpdateDepositBills === FALSE){
        die(mysqli_error($link));
    }
} // end deposit_payments

//deposit_payment_other_receipts

 "\n stamping_charges_payNow : " . $stampingCharges = $_POST["stamping_charges_payNow"];
 "\n yp_charges_payNow : " . $yuranPelbagai = $_POST["yp_charges_payNow"];
 "\n sst_charges_payNow : " . $sstCharges = $_POST["sst_charges_payNow"];

if($stampingCharges != "0.00" || $yuranPelbagai != "0.00" || $sstCharges != "0.00"){
    
    //get bill_code from bill name start
    $receipt_no = "";
    $bill_name = "PELBAGAI";
	//search bill_code based on bill_name
	include('bill_code.php');
     //Check running number from receipt_number table based on location_id, bill_type_id, and payment_type - start
     $serialNo = "";
       $sqlRunningNo = "select running_no from receipt_number where location_code = '" . $location_code . "' and  bill_type_id = " . $bill_type_id . " and payment_type = '" . $bill_code . "' order by running_no DESC limit 1";
     $runRunningNo = mysqli_query($link, $sqlRunningNo);
     if($runRunningNo === FALSE){
         die(mysqli_error($link));
     }
	 ":: COUNT RESULT:: ".$countResult = mysqli_num_rows($runRunningNo);
     //$running_no = $resultRunningNo["running_no"];
     if( $countResult > 0 ){
		  "IF STATEMENT RUNNING NO::";
         $resultRunningNo = mysqli_fetch_assoc($runRunningNo);
         $running_no = $resultRunningNo["running_no"];
        
         $running_no++;
         $strlenRunningNo = strlen($running_no);
         if($strlenRunningNo == 1){
			$serialNo = '0'. $running_no;
		}
		ELSE
			$serialNo = $running_no;
		 
		 
		 
		 
		 /* if($strlenRunningNo <= 1){
			 for($i=$strlenRunningNo;$i<=1;$i++){
				 $serialNo = '0'. $running_no;
			 }
		 }
		 else{
			 $serialNo = $running_no;
		 } */
     }
     else if($countResult == 0){
         $serialNo = '01';
         $running_no = 1;
     
     }
    //get bill_code from bill name end

    $receipt_no =  $location_code . '/'.$bill_code . '/' . $serialNo;
	
	//check bill created or not based on tenant id
	 $sqlDepositBillOtherCheck = "select id,stamping_charges_due,sst_charges_due,yp_charges_due   from deposit_bill_other_receipts where tenant_id = $tenant_id";
	$runDepositBillOtherCheck = mysqli_query($link, $sqlDepositBillOtherCheck);
     if($runDepositBillOtherCheck === FALSE){
         die(mysqli_error($link));
     } 
	 $countDepositBillOtherCheck = mysqli_num_rows($runDepositBillOtherCheck);
	 if($countDepositBillOtherCheck > 0 ){
		
		 $resultDepositBillOther = mysqli_fetch_assoc($runDepositBillOtherCheck);
		 $deposit_bill_other_id = $resultDepositBillOther["id"];
		 $stamping_charges_due = $resultDepositBillOther["stamping_charges_due"];
		 $sst_charges_due = $resultDepositBillOther["sst_charges_due"];
		 $yp_charges_due = $resultDepositBillOther["yp_charges_due"];
		 
		 $stamping_charges_due = $stamping_charges_due - $stampingCharges;
		 $sst_charges_due = $sst_charges_due - $sstCharges;
		 $yp_charges_due = $yp_charges_due - $yuranPelbagai;
		 $sqlUpdateDepositBillOther = "update deposit_bill_other_receipts set stamping_charges_due = '".$stamping_charges_due."' ,
										sst_charges_due = '".$sst_charges_due."' ,
										yp_charges_due = '".$yp_charges_due."' WHERE id = $deposit_bill_other_id
										";
		$runUpdateDepositBillOther= mysqli_query($link, $sqlUpdateDepositBillOther);
		 if($runUpdateDepositBillOther === FALSE){
			 die(mysqli_error($link));
		 } 
		//insert table deposit_payment_other_receipts
		 // INSERT TABLE deposit_payment_other_receipts
		$totalChargesPaid_depositOthersPayment = $stampingCharges + $sstCharges + $yuranPelbagai;
		 $sqlInsertDepositOtherPayment = "insert into deposit_payment_other_receipts  (deposit_other_payment_no, stamping_paid, sst_paid, yp_paid, total_charges_paid, created_by, deposit_bill_other_id, created_at, updated_at)
											values (
											'".$receipt_no."',
											'".$stampingCharges."',
											'".$sstCharges."',
											'".$yuranPelbagai."',
											'".$totalChargesPaid_depositOthersPayment."',
											'".$staff_id."',
											'".$deposit_bill_other_id."',
											'".date("Y-m-d H:i:s")."',
											'".date("Y-m-d H:i:s")."'
											)";

		$runInsertDepositOtherPayment = mysqli_query($link, $sqlInsertDepositOtherPayment);
		if($runInsertDepositOtherPayment === FALSE){
		die(mysqli_error($link));
		}
		$depositPaymentOtherID_print = mysqli_insert_id($link);
		//INSERT table receipt_number
		$sqlInsertReceiptNo = "insert into receipt_number (location_code, bill_type_id, payment_type, running_no, serial_no, updated_at, created_at) values (
			'".$location_code."',
			'".$bill_type_id."',
			'".$bill_code."',
			'".$running_no."',
			'".$receipt_no."',
			'".date("Y-m-d H:i:s")."',
			'".date("Y-m-d H:i:s")."'
			)";
		$runInsertReceiptNo = mysqli_query($link, $sqlInsertReceiptNo);
		if($runInsertReceiptNo === FALSE){
			die(mysqli_error($link));
		}
		 
	 }
	else if($countDepositBillOtherCheck == 0 ){ // data belum create dalam bill
		//get bill_code from bill name start
		$bill_name = "PELBAGAI";
		include('bill_code.php');
		//get bill_code from bill name end
		
		 $sqlDepositBillOtherReceipt_runningNo = "select deposit_other_bill_no from deposit_bill_other_receipts 
									inner join tenants on deposit_bill_other_receipts.tenant_id = tenants.id
									inner join properties on tenants.property_id = properties.id
									inner join locations on properties.location_id = locations.id 
									where locations.id = $location_id order by deposit_bill_other_receipts.id DESC limit 1";
		$runDepositBillOtherReceipt_runningNo = mysqli_query($link, $sqlDepositBillOtherReceipt_runningNo);
		if($runDepositBillOtherReceipt_runningNo === FALSE){
			die(mysqli_error($link));
		}
		if(mysqli_num_rows($runDepositBillOtherReceipt_runningNo) == 1){
			$resultDepositBillOtherReceipt_runningNo = mysqli_fetch_assoc($runDepositBillOtherReceipt_runningNo);
			$deposit_other_bill_no = $resultDepositBillOtherReceipt_runningNo["deposit_other_bill_no"];
			//split by '-'
			//split locationName with rental_desc
			
		   // $deposit_bill_no_split = substr($deposit_bill_no, strpos($deposit_bill_no, "/") + 1);   
			$deposit_other_bill_no_split = explode('/',$deposit_other_bill_no);   
			//susunan deposit_bill_no = [project_code]/[location_code]/[bill_code]/[running_no]
			//0->[project_code]
			//1->[location_code]
			//2->[bill_code]
			//3->[running_no]

			//$arr = explode("-", $deposit_bill_no, 2);
			$deposit_other_bill_no = $deposit_other_bill_no_split[3];
			$deposit_other_bill_no++;
			$strlenRunningNo = strlen($deposit_other_bill_no);
			
			if($strlenRunningNo == 1){
				$deposit_other_bill_no = '0'. $deposit_other_bill_no;
			}
			ELSE
				$deposit_other_bill_no = $deposit_other_bill_no; 
			 
			 
			 
			 /* for($i=$strlenRunningNo;$i<=1;$i++){
				 $deposit_other_bill_no = '0'. $deposit_other_bill_no;
			   
			 } */
			// $deposit_bill_no = $location_code . $deposit_bill_no;
			
		}
		else if(mysqli_num_rows($runDepositBillOtherReceipt_runningNo) == 0){
			$deposit_other_bill_no =  '01';
			//$deposit_bill_no = $deposit_bill_no;
		   
		}
		$deposit_other_bill_no =  'INV/'. $location_code . '/'.$bill_code . '/' . $deposit_other_bill_no;
		//set deposit bill other no end

		//set deposit_bill_other_receipts start

		if($stampingCharges != 0.00 || $yuranPelbagai != 0.00 || $sstCharges != 0.00){
			$insert_depositBillOtherReceiptSql = "insert into deposit_bill_other_receipts  (deposit_other_bill_no, stamping_charges_due, stamping_charges_amount, sst_charges_due, sst_charges_amount,
																							yp_charges_due, yp_charges_amount, payment_status, yp_id, sst_id, tenant_id, created_by, created_at, updated_at)
																values (
																	'".$deposit_other_bill_no."',
																	'".number_format($stampingCharges,2)."',
																	'".number_format($stampingCharges,2)."',
																	'".number_format($sstCharges,2)."',
																	'".number_format($sstCharges,2)."',
																	'".number_format($yuranPelbagai,2)."',
																	'".number_format($yuranPelbagai,2)."',
																	'unpaid',
																	'".$yp_id."',
																	'".$sst_id."',
																	'".$tenant_id."',
																	'".$staff_id."',
																	'".date('Y-m-d')."',
																	'".date('Y-m-d')."'
																	)";
			$runInsert_depositBillOtherReceiptSql = mysqli_query($link, $insert_depositBillOtherReceiptSql);
			if($runInsert_depositBillOtherReceiptSql === FALSE){
				die(mysqli_error($link));
			}
			
			 // INSERT TABLE deposit_payment_other_receipts
			$totalChargesPaid_depositOthersPayment = $stampingCharges + $sstCharges + $yuranPelbagai;
			 $sqlInsertDepositOtherPayment = "insert into deposit_payment_other_receipts  (deposit_other_payment_no, stamping_paid, sst_paid, yp_paid, total_charges_paid, created_by, deposit_bill_other_id, created_at, updated_at) values (
				'".$receipt_no."',
				'".$stampingCharges."',
				'".$sstCharges."',
				'".$yuranPelbagai."',
				'".$totalChargesPaid_depositOthersPayment."',
				'".$staff_id."',
				'".$deposit_bill_other_id."',
				'".date("Y-m-d H:i:s")."',
				'".date("Y-m-d H:i:s")."'
				)";

			$runInsertDepositOtherPayment = mysqli_query($link, $sqlInsertDepositOtherPayment);
			if($runInsertDepositOtherPayment === FALSE){
			die(mysqli_error($link));
			}
			$depositPaymentOtherID_print = mysqli_insert_id($link);
			//INSERT table receipt_number
			$sqlInsertReceiptNo = "insert into receipt_number (location_code, bill_type_id, payment_type, running_no, serial_no, updated_at, created_at) values (
				'".$location_code."',
				'".$bill_type_id."',
				'".$bill_code."',
				'".$running_no."',
				'".$receipt_no."',
				'".date("Y-m-d H:i:s")."',
				'".date("Y-m-d H:i:s")."'
				)";
			$runInsertReceiptNo = mysqli_query($link, $sqlInsertReceiptNo);
			if($runInsertReceiptNo === FALSE){
				die(mysqli_error($link));
			}
			 
			
			
			
			
		}
		
		
	}
   

    // UPDATE TABLE deposit_bill_other_receipts
    // UPDATE TABLE deposit_bills
    //check all payment knock-off with deposit bills based on deposit bill id
     $sqlDepositOtherPayments = "select sum(stamping_paid) as totalStampingPaid, sum(sst_paid) as totalSSTPaid, sum(yp_paid) as totalYpPaid from deposit_payment_other_receipts where deposit_bill_other_id = " .$deposit_bill_other_id;
    $runDepositOtherPayments = mysqli_query($link, $sqlDepositOtherPayments);
    if($runDepositOtherPayments === FALSE){
        die(mysqli_error($link));
    }
    $resultDepositOtherPayments = mysqli_fetch_assoc($runDepositOtherPayments);
    $totalStampingPaid = $resultDepositOtherPayments["totalStampingPaid"];
    $totalSSTPaid = $resultDepositOtherPayments["totalSSTPaid"];
    $totalYpPaid = $resultDepositOtherPayments["totalYpPaid"];

     $sqlDepositOtherBills = "select stamping_charges_amount, sst_charges_amount, yp_charges_amount, payment_status from deposit_bill_other_receipts where id = " .$deposit_bill_other_id;
    $runDepositOtherBills = mysqli_query($link, $sqlDepositOtherBills);
    if($runDepositOtherBills === FALSE){
        die(mysqli_error($link));
    }
    $resultDepositOtherBills = mysqli_fetch_assoc($runDepositOtherBills);
    $stamping_charges_amount = $resultDepositOtherBills["stamping_charges_amount"];
    $sst_charges_amount = $resultDepositOtherBills["sst_charges_amount"];
    $yp_charges_amount = $resultDepositOtherBills["yp_charges_amount"];
    $otherPayment_status = $resultDepositOtherBills["payment_status"];

	if($stamping_charges_amount == 0.00 && $stampingCharges > 0.00)
		$stamping_charges_amount = $stamping_charges_amount + $stampingCharges;

    $stamping_due = $stamping_charges_amount - $totalStampingPaid;
    $sst_due = $sst_charges_amount - $totalSSTPaid;
    $yp_due = $yp_charges_amount - $totalYpPaid;
    $totalDepositOtherAmount = $stamping_charges_amount + $sst_charges_amount + $yp_charges_amount;
    $totalDepositOtherPaid = $totalStampingPaid + $totalSSTPaid + $totalYpPaid;
    $totalDepositOtherDue = $totalDepositOtherAmount - $totalDepositOtherPaid;
    //SET PAYMENT_STATUS IN DEPOSIT BILLS
    if($totalDepositOtherDue == 0.00 && $totalDepositOtherAmount == $totalDepositOtherPaid)
        $payment_status = "paid";
    else if($totalDepositOtherDue < $totalDepositOtherAmount && $totalDepositOtherDue != 0.00)
        $payment_status = "partial";   
    else if($totalDepositOtherDue == $totalDepositOtherAmount)
        $payment_status = "unpaid"; 
    // UPDATE TABLE deposit_bills
     $sqlUpdateDepositBills = "update deposit_bill_other_receipts set 
                                stamping_charges_due = " . $stamping_due . 
                                ", stamping_charges_amount = " . $stamping_charges_amount . 
                                ", sst_charges_due = " . $sst_due . 
                                ", yp_charges_due = " . $yp_due . 
                                ", payment_status = '" . $payment_status . "'" .
                                " where id = " . $deposit_bill_other_id;
    $runUpdateDepositOtherBills = mysqli_query($link, $sqlUpdateDepositBills);
    if($runUpdateDepositOtherBills === FALSE){
        die(mysqli_error($link));
    }
} // end  deposit_payment_other_receipts


// INSERT TABLE payments for first month rental
// UPDATE TABLE bills for first month rental




//echo "<script>window.close();</script>";
//header("Location:viewTenantDashboard.php?tenant_id=".$tenant_id."&menu=".$_SESSION["menu"]."&submenu=".$_SESSION["submenu"]);
//header("Location:print_deposit.php?tenant_id=".$tenant_id."&depositPaymentID_print=".$depositPaymentID_print. "&depositPaymentOtherID_print=".$depositPaymentOtherID_print. "&menu=".$_SESSION["menu"]."&submenu=".$_SESSION["submenu"]);


$_SESSION["menu"] = $_GET["menu"];
$_SESSION["submenu"] = $_GET["submenu"];
//echo "<script>window.close();</script>";
//header("Location:viewTenantDashboard.php?tenant_id=".$tenant_id."&menu=".$_SESSION["menu"]."&submenu=".$_SESSION["submenu"]);

header("Location:print_deposit.php?tenant_id=".$tenant_id."&depositPaymentID_print=".$depositPaymentID_print. "&depositPaymentOtherID_print=".$depositPaymentOtherID_print. "&menu=".$_SESSION["menu"]."&submenu=".$_SESSION["submenu"]);


?>