<?php 
	include ("queryTenant.php");
	
	$resultTenant = mysqli_fetch_assoc($runTenant);
	$tenant_name = $resultTenant["tenant_name"];
	$tenant_ic = $resultTenant["ic_no"];
	$company_name = $resultTenant["company"];
	$company_ssm = $resultTenant["ssm_no"];
	$unit_no = $resultTenant["unit_no"];
	$location = $resultTenant["location_name"];
	$tenant_telNo = $resultTenant["phone"];
	$tenant_phoneNo = $resultTenant["mobile"];
	$start_rent = $resultTenant["start_date"];
	$end_rent = $resultTenant["end_date"];
	$tenant_status = $resultTenant["status"];
	$rental_program = $resultTenant["project_name"];
	
	if($start_rent != "0000-00-00 00:00:00" && $end_rent != "0000-00-00 00:00:00"){
		$start_rent = date("d/m/Y",strtotime($start_rent));
		$end_rent = date("d/m/Y",strtotime($end_rent));	
	}
	if($start_rent != "0000-00-00 00:00:00" && $end_rent == "0000-00-00 00:00:00"){
		$start_rent = date("d/m/Y",strtotime($start_rent));
		$end_rent = "No Record";
	}
	
	else if($start_rent == "0000-00-00 00:00:00" && $end_rent == "0000-00-00 00:00:00"){
		$start_rent = "No Record";
		$end_rent = "No Record";
	}
	else if($start_rent == "0000-00-00 00:00:00" || $end_rent == "0000-00-00 00:00:00"){
		if($start_rent == "0000-00-00 00:00:00"){
			$start_rent = "No Record";
		}
		if($end_rent == "0000-00-00 00:00:00"){
			$end_rent = "No Record";
		}
	}
	$pic_syarikat = "";
	$contact_no = "";
	if($tenant_telNo != "" && $tenant_phoneNo == "")
		$contact_no = $tenant_telNo;
	else if($tenant_telNo == "" && $tenant_phoneNo != "")
		$contact_no = $tenant_phoneNo;
	else if($tenant_telNo != "" && $tenant_phoneNo != "")
		$contact_no = $tenant_telNo . " / " . $tenant_phoneNo;
	else if($tenant_telNo == "" && $tenant_phoneNo == "")
		$contact_no = "-";
	
	if($ref_name != "" && $ref_contact == "")
		$pic_syarikat = $tenant_telNo;
	else if($ref_name == "" && $ref_contact != "")
		$pic_syarikat = $tenant_phoneNo;
	else if($ref_name != "" && $ref_contact != "")
		$pic_syarikat = $pic_syarikat . " (" . $ref_contact .")";
	else if($ref_name == "" && $ref_contact == "")
		$pic_syarikat = "-";
	
	$maklumatHartanah = $unit_no . ", " . $location;
	
	//GET DEPOSIT INFO
	 $no=1;
	$totalDepo=0;
	//$countRowDeposit = 0;
	//$countRowDeposit = mysqli_num_rows($runSqlDeposit);
	
	
	
	//check table deposits (penyewa lama)
	$sqlDeposit_oldRecord = "select deposits.*, users.name from deposits inner join users on deposits.created_by = users.id
					where deposits.tenant_id = $tenant_id "; //RETURN NULL IN ROW - SHOULD CHECK THE PROBLEM
	$runDeposit_oldRecord = mysqli_query($link, $sqlDeposit_oldRecord);
	if($runDeposit_oldRecord === FALSE){
		die(mysqli_error($link));
	}
	$countRowOldRecord = mysqli_num_rows($runDeposit_oldRecord);

	//check table deposit_bills (penyewa baru)
	$sqlStatementDeposit_newRecord ="SELECT * FROM ( 
										(SELECT deposit_payments.id as deposit_id,
												deposit_payments.receipt_no as receipt_no, 
												deposit_payments.two_months_amount_paid as two_months_amount_paid,
												deposit_bills.two_month_rental_amount as two_month_rental_amount_bill, 
												deposit_payments.utility_amount_paid as utility_amount_paid, 
												deposit_bills.utility_amount as utility_amount_bill, 
												deposit_payments.created_at as created_at, 
												deposit_bills.id as deposit_bill_id, 
												deposit_bills.deposit_bill_no as deposit_bill_no, 
												deposit_bills.payment_status, 
												concat(deposit_payments.receipt_no,' deposit_payment ',deposit_payments.two_months_amount_paid) as info_two_months_amount ,
												concat(deposit_payments.receipt_no,' deposit_payment ',deposit_payments.utility_amount_paid) as info_utility_amount
												FROM deposit_payments
												inner join deposit_bills on deposit_payments.deposit_bill_id = deposit_bills.id
												where deposit_bills.tenant_id = $tenant_id  ) 
										UNION ALL 
											(SELECT deposit_bills.id as deposit_id, 
											deposit_bills.deposit_bill_no as receipt_no, 
											deposit_bills.two_month_rental_due as two_months_amount_paid, 
											deposit_bills.two_month_rental_amount as two_month_rental_amount_bill, 
											deposit_bills.utility_due as utility_amount_paid, 
											deposit_bills.utility_amount as utility_amount_bill, 
											deposit_bills.created_at as created_at, 
											deposit_bills.id as deposit_bill_id, 
											deposit_bills.deposit_bill_no as deposit_bill_no, 
											deposit_bills.payment_status, 
											concat(deposit_bills.deposit_bill_no,' deposit_bill ',deposit_bills.two_month_rental_amount) as info_two_months_amount  ,
											concat(deposit_bills.deposit_bill_no,' deposit_bill ',deposit_bills.utility_amount) as info_utility_amount  
											FROM deposit_bills 
											where deposit_bills.tenant_id = $tenant_id   ) 
											) 
										results ORDER BY created_at ASC";
	
	$runStatementDeposit_newRecord = mysqli_query($link, $sqlStatementDeposit_newRecord);
	if($runStatementDeposit_newRecord === FALSE){
		die(mysqli_error($link));
	}
	$countStatementDeposit_newRecord = mysqli_num_rows($runStatementDeposit_newRecord);
	
	
	
	$colspanShow_header = 7;
	$colspanShow_bottom = 4;

	//deposit_payment_other_receipts table
	 "   sqlDepositPaymentOthers: ".$sqlDepositPaymentOthers = "select deposit_payment_other_receipts.* , users.name, deposit_bill_other_receipts.stamping_charges_amount,
																	deposit_bill_other_receipts.sst_charges_amount,deposit_bill_other_receipts.yp_charges_amount 
																	from deposit_payment_other_receipts 
																	inner join deposit_bill_other_receipts on deposit_payment_other_receipts.deposit_bill_other_id = deposit_bill_other_receipts.id 
																	inner join users on deposit_payment_other_receipts.created_by = users.id
																	where tenant_id = $tenant_id";
	$sqlDepositPaymentOthers ="SELECT * FROM ( 
										(SELECT deposit_payment_other_receipts.id as deposit_id,
												deposit_payment_other_receipts.deposit_other_payment_no as receipt_no, 
												deposit_payment_other_receipts.yp_paid as yp_paid,
												deposit_bill_other_receipts.yp_charges_amount as yp_charges_amount, 
												deposit_payment_other_receipts.sst_paid as sst_paid, 
												deposit_bill_other_receipts.sst_charges_amount as sst_charges_amount, 
												deposit_payment_other_receipts.stamping_paid as stamping_paid, 
												deposit_bill_other_receipts.stamping_charges_amount as stamping_charges_amount, 
												deposit_payment_other_receipts.created_at as created_at, 
												deposit_bill_other_receipts.id as deposit_bill_id, 
												deposit_bill_other_receipts.deposit_other_bill_no as deposit_other_bill_no, 
												deposit_bill_other_receipts.payment_status, 
												concat(deposit_payment_other_receipts.deposit_other_payment_no,' deposit_other_payment ',deposit_payment_other_receipts.yp_paid) as info_yp_paid ,
												concat(deposit_payment_other_receipts.deposit_other_payment_no,' deposit_other_payment ',deposit_payment_other_receipts.sst_paid) as info_sst_paid,
												concat(deposit_payment_other_receipts.deposit_other_payment_no,' deposit_other_payment ',deposit_payment_other_receipts.stamping_paid) as info_stamping_paid
												FROM deposit_payment_other_receipts
												inner join deposit_bill_other_receipts on deposit_payment_other_receipts.deposit_bill_other_id = deposit_bill_other_receipts.id
												where deposit_bill_other_receipts.tenant_id = $tenant_id  ) 
										UNION ALL 
										(SELECT deposit_bill_other_receipts.id as deposit_id, 
											deposit_bill_other_receipts.deposit_other_bill_no as receipt_no, 
											deposit_bill_other_receipts.yp_charges_due as yp_paid, 
											deposit_bill_other_receipts.yp_charges_amount as yp_charges_amount, 
											deposit_bill_other_receipts.sst_charges_due as sst_paid, 
											deposit_bill_other_receipts.sst_charges_amount as sst_charges_amount, 
											deposit_bill_other_receipts.stamping_charges_due as stamping_paid, 
											deposit_bill_other_receipts.stamping_charges_amount as stamping_charges_amount, 
											deposit_bill_other_receipts.created_at as created_at, 
											deposit_bill_other_receipts.id as deposit_bill_other_id, 
											deposit_bill_other_receipts.deposit_other_bill_no as deposit_other_bill_no, 
											deposit_bill_other_receipts.payment_status, 
											concat(deposit_bill_other_receipts.deposit_other_bill_no,' deposit_other_bill ',deposit_bill_other_receipts.yp_charges_amount) as info_yp_paid  ,
											concat(deposit_bill_other_receipts.deposit_other_bill_no,' deposit_other_bill ',deposit_bill_other_receipts.sst_charges_amount) as info_sst_paid,  
											concat(deposit_bill_other_receipts.deposit_other_bill_no,' deposit_other_bill ',deposit_bill_other_receipts.stamping_charges_amount) as info_stamping_paid  
											FROM deposit_bill_other_receipts 
											where deposit_bill_other_receipts.tenant_id = $tenant_id   ) 
											) 
										results ORDER BY created_at ASC";
	
	
	
	$runDepositPaymentOthers = mysqli_query($link, $sqlDepositPaymentOthers);
	if($runDepositPaymentOthers === FALSE){
		die(mysqli_error($link));
	}
	$countRowPaymentOthers = mysqli_num_rows($runDepositPaymentOthers);

?>