<?php 
include("session_set.php");
error_reporting(0); 
date_default_timezone_set("Asia/Kuala_Lumpur");
 "TENANT ID: ".$tenant_id = $_POST["tenant_id"];
$startRent = $_POST["startRent"];
$twoMonthRental = $_POST["twoMonthRental"];
$utility = $_POST["utility"];
$deposit_bill_id = $_POST["deposit_bill_id"];
$deposit_other_receipt_id = $_POST["deposit_other_receipt_id"];
$bill_id = $_POST["id"];

//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"])){
	if($deposit_bill_id != ""){
		//update query
		$sqlUpdateDepositBill = "update deposit_bills set 
								two_month_rental_due = '".$twoMonthRental."', 
								two_month_rental_amount = '".$twoMonthRental."', 
								utility_due = '".$utility."', 
								utility_amount = '".$utility."', 
								updated_at = '".date("Y-m-d H:i:s")."' 
								where id = $deposit_bill_id";
		$runUpdateDepositBill = mysqli_query($link, $sqlUpdateDepositBill);
		if($runUpdateDepositBill === FALSE){
			die(mysqli_error($link));
		}
	}
	else{
		//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"];
			 
			$deposit_bill_no_split = explode('/',$deposit_bill_no);   
			//susunan deposit_bill_no_split =INV/[location_code]/[bill_code]/[running_no]
			//0->[INV]
			//1->[location_code]
			//2->[bill_code]
			//3->[running_no]

			$deposit_bill_no = $deposit_bill_no_split[2];
			$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 = "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 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



//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
	
	//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

	
	
	
	IF($deposit_other_receipt_id != ""){
		
		
		//update query
		 $sqlUpdateDepositBillOtherReceipt = "update deposit_bill_other_receipts set 
													stamping_charges_due = '".$stampingCharges."', 
													stamping_charges_amount = '".$stampingCharges."', 
													yp_charges_due = '".$yuranPelbagai."', 
													yp_charges_amount = '".$yuranPelbagai."', 
													sst_charges_due = '".$sstCharges."', 
													sst_charges_amount = '".$sstCharges."', 
													updated_at = '".date("Y-m-d H:i:s")."' 
													where id = $deposit_other_receipt_id";
		$runUpdateDepositBillOtherReceipt = mysqli_query($link, $sqlUpdateDepositBillOtherReceipt);
		if($runUpdateDepositBillOtherReceipt === FALSE){
			die(mysqli_error($link));
		}
	}
	else{
		 $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"];
			
			$deposit_other_bill_no_split = explode('/',$deposit_other_bill_no);   
			//susunan deposit_bill_no = INV/[location_code]/[bill_code]/[running_no]
			//0->INV
			//1->[location_code]
			//2->[bill_code]
			//3->[running_no]

			$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;
			   
			 }
		}
		else if(mysqli_num_rows($runDepositBillOtherReceipt_runningNo) == 0){
			$deposit_other_bill_no =  '01';
		}
		$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));
			}
		}
	}
}
//set deposit_bill_other_receipts end

//set first month rental start
/* if(ISSET($_POST["checkboxFirstMonthRental"])){
	
	$startRent = $_POST["startRent"];
	echo " firstMonthRental :: " .$firstMonthRental = $_POST["firstMonthRental"];
	$firstMonthYear = $_POST["firstMonthYear"];
	
	if($bill_id != ""){
		//update query
		$sqlUpdateBills = "update bills set bill_amount = '".$firstMonthRental."', issue_date = '".$firstMonthYear."', updated_at = '".date("Y-m-d H:i:s")."' where id = $bill_id";
		$runUpdateBill = mysqli_query($link, $sqlUpdateBills);
		if($runUpdateBill === FALSE){
			die(mysqli_error($link));
		}
		//update tenant start rental
		$sqlUpdateTenant = "update tenants set start_date = '".date('Y-m-01 00:00:00',strtotime($startRent))."' where id = $tenant_id";
		$runUpdateTenant = mysqli_query($link, $sqlUpdateTenant);
		if($runUpdateTenant === FALSE){
			die(mysqli_error($link));
		}
		//update bill_first_month_rental
		echo $sqlUpdateBill1stMonthRental = "update bill_first_month_rental set bill_amount = '".$firstMonthRental."', issue_date = '".$firstMonthYear."', updated_at = '".date("Y-m-d H:i:s")."' where bill_id = $bill_id";
		$runUpdateBill1stMonthRental = mysqli_query($link, $sqlUpdateBill1stMonthRental);
		if($runUpdateBill1stMonthRental === FALSE){
			die(mysqli_error($link));
		}
	}
	else{
		//set running no based on bill_no  start
		echo $sqlSearchBillNo = "SELECT max(bill_no) as maxBill_no FROM `bills` WHERE `bill_no` LIKE '".$location_id."-%' ORDER BY `bills`.`bill_no` ASC";
		$runSearchBillNo = mysqli_query($link, $sqlSearchBillNo);
		if($runSearchBillNo === FALSE){
			die(mysqli_error($link));
		}
		$resultBillNo = mysqli_fetch_assoc($runSearchBillNo);
		$maxBill_no = $resultBillNo["maxBill_no"];
		$maxBill_no_split = explode('-',$maxBill_no);   
		$maxBill_no = $maxBill_no_split[1];
		$maxBill_no++;
		
		$bill_no = $location_id . "-" . $maxBill_no;
		//set running no based on bill_no end
		echo $insert_BillsSql = "insert into bills values (
							'',
							'".$bill_no."',
							'".number_format($firstMonthRental,2)."',
							'".number_format($firstMonthRental,2)."',
							'unpaid',
							'".date('Y-m-01 00:00:00',strtotime($startRent))."',
							'1',
							'".$staff_id."',
							'".$tenant_id."',
							'".date('Y-m-d')."',
							'".date('Y-m-d')."'
							)";
		$runinsert_BillsSql = mysqli_query($link, $insert_BillsSql);
		if($runinsert_BillsSql === FALSE){
			die(mysqli_error($link));
		}
		
		//update tenant start rental
		$sqlUpdateTenant = "update tenants set start_date = '".date('Y-m-01 00:00:00',strtotime($startRent))."' where id = $tenant_id";
		$runUpdateTenant = mysqli_query($link, $sqlUpdateTenant);
		if($runUpdateTenant === FALSE){
			die(mysqli_error($link));
		}
		
		//select max id from bills table
		echo $sqlMaxBillId = "SELECT MAX(ID) as max_id FROM bills";
		$runMaxBillId = mysqli_query($link, $sqlMaxBillId);
		if($runMaxBillId === FALSE){
			die(mysqli_error($link));
		}
		$resultMaxBillId = mysqli_fetch_assoc($runMaxBillId);
		$maxBill_id = $resultMaxBillId["max_id"];
		//insert same infor into table bill_first_month_rental
		$sqlBills1stMonthRental = "insert into bill_first_month_rental values (
			'',
			'".$maxBill_id."',
			'".number_format($firstMonthRental,2)."',
			'".number_format($firstMonthRental,2)."',
			'unpaid',
			'".date('Y-m-01 00:00:00',strtotime($startRent))."',
			'6',
			'".$staff_id."',
			'".$tenant_id."',
			'".date('Y-m-d')."',
			'".date('Y-m-d')."'
			)";
		$runBills1stMonthRental = mysqli_query($link, $sqlBills1stMonthRental);
		if($runBills1stMonthRental === FALSE){
			die(mysqli_error($link));
		}
		
	}
	
	
} */
//set first month rental end



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

?>
