<?php 
include("session_set.php");
//error_reporting(0); 

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

 "TENANT ID: ".$tenant_id = $_POST["tenant_id"];
$startRent = $_POST["startRent"];
$twoMonthRental = $_POST["twoMonthRental"];
$utility = $_POST["utility"];


//get bill_code from bill name start
$bill_name = "DEPOSIT";
include('bill_code.php');
//get bill_code from bill name end

$staff_id = $_SESSION["staff_id"];
$location_id = $_SESSION["location_id"];
$location_code = $_SESSION["location_code"];
$project_code = $_SESSION["project_code"];
//get tenant info start
include("queryTenant.php");
$resultTenant = mysqli_fetch_array($runTenant);
$rental_rate = $resultTenant["rental_rate"];
//get tenant info end

if(ISSET($_POST["checkboxDepositBill"])){
	//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);
		
		 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';
		//$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($twoMonthRental != 0.00 || $utility != 0.00){
		 "insert_depositBillSql:: ". $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));
		}
	}
}
//set bill for 2 months rental and utility end

//get yuran pelbagai start 
$yuranPelbagaiSql = "select id, yp_charges from yuran_pelbagais where location_id = $location_id";
$runYuranPelbagaiSql = mysqli_query($link, $yuranPelbagaiSql);
	if($runYuranPelbagaiSql === FALSE){
		die(mysqli_error($link));
	}
$resultYuranPelbagaiSql = mysqli_fetch_array($runYuranPelbagaiSql);
$yp_id = $resultYuranPelbagaiSql["id"];
$yp_charges = $resultYuranPelbagaiSql["yp_charges"];

//get yuran pelbagai end

//get sst start 
$sstSql = "select id, sst_percent from sst where sst_status = 'enable'";
$runSstSql = mysqli_query($link, $sstSql);
if($runSstSql === FALSE){
    die(mysqli_error($link));
}
$resultSstSql = mysqli_fetch_array($runSstSql);		
$sst_id = 	$resultSstSql["id"];
$sst_percent = 	$resultSstSql["sst_percent"];
$sst_charges = $sst_percent*$yp_charges;
//get sst end



//set deposit bill other no start

if(ISSET($_POST["checkboxOtherBill"])){
	$stampingCharges = $_POST["stampingCharges"];
	$yuranPelbagai = $_POST["yuranPelbagai"];
	$sstCharges = $_POST["sstCharges"];
	//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);
		
		 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_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));
		}
	}
}
//set deposit_bill_other_receipts end




$_SESSION["menu"] = $_GET["menu"];
$_SESSION["submenu"] = $_GET["submenu"];
 echo "<script>
	window.close();
    window.onunload = refreshParent;
    function refreshParent() {
        window.opener.location.reload();
    }
</script>"; 
//echo "<script>window.close();</script>";
//header("Location:viewTenantDashboard.php?tenant_id=".$tenant_id."&menu=".$_SESSION["menu"]."&submenu=".$_SESSION["submenu"]);
?>
