<?php 
//session_start();
error_reporting(0); 
			
session_start();
require_once "config.php";
//set time zone
date_default_timezone_set("Asia/Kuala_Lumpur");
$tenant_id = $_GET["tenant_id"];

//deposit tenant info terbahagi pada 2
//1. Tenant lama (sep 2024 and lower)
//semak bayaran deposit dalam table deposits 
//2. Tenant baru (september 2024 and above)
//semak bayaran dalam 2 table -> deposit_payments & deposit_payment_other_receipts

//semak penyewa lama atau baru
//penyewa lama - table deposits
/* $sqlDepositsCheck = "select  two_month_rental,utilities, management_fee, total_deposit from deposits where tenant_id = $tenant_id ";
$runDepositsCheck = mysqli_query($link, $sqlDepositsCheck);
if($runDepositsCheck === FALSE){
	die(mysqli_error($link));
}
$countDepositExist = mysqli_num_rows($runDepositsCheck); 
if($countDepositExist > 0){ */
//based on tenant id, penyewa lama
	 $sqlDeposit_oldRecord = "Select deposits.receipt_no,deposits.two_month_rental, deposits.utilities, deposits.management_fee, 
						deposits.total_deposit , tenants.tenant_name, tenants.rental_rate, properties.unit_no, 
						locations.location_name, users.name, deposits.created_at, deposits.description 
						from deposits 
						inner join tenants on deposits.tenant_id = tenants.id 
						inner join properties on tenants.property_id = properties.id 
						inner join locations on properties.location_id = locations.id 
						inner join users on deposits.created_by = users.id
						where tenant_id = $tenant_id "; 
	$runDeposit_oldRecord = mysqli_query($link, $sqlDeposit_oldRecord);
	if($runDeposit_oldRecord === FALSE){
		die(mysqli_error($link));
	}
	$countRowOldRecord = mysqli_num_rows($runDeposit_oldRecord);
/* }
else{  *///based on tenant id, penyewa baru
	/* $sqlDepositBillsCheck = "select * from deposit_bills where tenant_id = $tenant_id";
	$runDepositBillsCheck = mysqli_query($link, $sqlDepositBillsCheck);
	if($runDepositBillsCheck === FALSE){
		die(mysqli_error($link));
	}
	$countDepositBillsExist = mysqli_num_rows($runDepositBillsCheck);
	if($countDepositBillsExist > 0){ */
		//check payment DEPOSIT exist or not first
	/* $sqlDepositPaymentsCheck = "select * from deposit_payments inner join deposit_bills on deposit_payments.deposit_bill_id = deposit_bills.id where deposit_bills.tenant_id = $tenant_id";
	$runDepositPaymentsCheck = mysqli_query($link, $sqlDepositPaymentsCheck);
	if($runDepositPaymentsCheck === FALSE){
		die(mysqli_error($link));
	}
	$countDepositPaymentsCheck = mysqli_num_rows($runDepositPaymentsCheck);
	if($countDepositPaymentsCheck > 0){ */
		//query untuk cari payment dalam deposit dan others receipt
		//deposit_payments table
		 "   sqlDepositPayments: " .$sqlDepositPayments_newRecord = "select  deposit_payments.receipt_no, deposit_payments.two_months_amount_paid,  deposit_payments.utility_amount_paid, deposit_payments.created_by, deposit_payments.created_at, deposit_bills.id, 
								deposit_bills.two_month_rental_amount, deposit_bills.utility_amount, tenants.tenant_name, tenants.rental_rate, properties.unit_no, locations.location_name, users.name
								from deposit_payments inner join deposit_bills on deposit_payments.deposit_bill_id = deposit_bills.id 
								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 users on deposit_payments.created_by = users.id
								where deposit_bills.tenant_id = $tenant_id";
		$runDepositPayments_newRecord = mysqli_query($link, $sqlDepositPayments_newRecord);
		if($runDepositPayments_newRecord === FALSE){
			die(mysqli_error($link));
		}
		$countRowNewRecord = mysqli_num_rows($runDepositPayments_newRecord);
	//}
	//}
	
	/* $sqlDepositBillsOthersReceiptCheck = "select * from deposit_bill_other_receipts where tenant_id = $tenant_id";
	$runDepositBillsOthersReceiptCheck = mysqli_query($link, $sqlDepositBillsOthersReceiptCheck);
	if($runDepositBillsOthersReceiptCheck === FALSE){
		die(mysqli_error($link));
	}
	$countDepositBillsOthersReceiptExist = mysqli_num_rows($runDepositBillsOthersReceiptCheck);
	
	if($countDepositBillsOthersReceiptExist > 0 ){
		//check payment others receipt exist or not
		$sqlDepositPaymentOthersCheck = "select * 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 tenant_id = $tenant_id";
		$runDepositPaymentOthersCheck = mysqli_query($link, $sqlDepositPaymentOthersCheck);
		if($runDepositPaymentOthersCheck === FALSE){
			die(mysqli_error($link));
		}	
		$countDepositPaymentOthersCheck = mysqli_num_rows($runDepositPaymentOthersCheck);
		if($countDepositPaymentOthersCheck > 0){ */
			//deposit_payment_other_receipts table
			  "   sqlDepositPaymentOthers: ".$sqlDepositPaymentOthers = "select deposit_payment_other_receipts.deposit_other_payment_no as receipt_no, deposit_payment_other_receipts.stamping_paid, 
																			deposit_payment_other_receipts.sst_paid, deposit_payment_other_receipts.yp_paid, deposit_payment_other_receipts.total_charges_paid, 
																			deposit_payment_other_receipts.created_at, deposit_payment_other_receipts.created_by, deposit_bill_other_receipts.stamping_charges_amount,
																			deposit_bill_other_receipts.sst_charges_amount,deposit_bill_other_receipts.yp_charges_amount , tenants.tenant_name, tenants.rental_rate, 
																			properties.unit_no, locations.location_name, users.name 
																			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 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 
																			inner join users on deposit_payment_other_receipts.created_by = users.id
																			where tenant_id = $tenant_id";
			$runDepositPaymentOthers = mysqli_query($link, $sqlDepositPaymentOthers);
			if($runDepositPaymentOthers === FALSE){
				die(mysqli_error($link));
			}
			$countDepositPaymentOthers = mysqli_num_rows($runDepositPaymentOthers);
			
		/* }
	}
	
} */
?>

<body height="90%" style="font-family:arial;" onLoad="window.print();">


<!-- resit penyewa lama start -->
<?php 
if($countRowOldRecord > 0 ){
	while($resultDeposit = mysqli_fetch_assoc($runDeposit_oldRecord)){
		$description = $resultDeposit["description"];
		$two_month_rental = $resultDeposit["two_month_rental"];
		$utilities = $resultDeposit["utilities"];
		$mgt_fee = $resultDeposit["management_fee"];
		$total_deposit = $resultDeposit["total_deposit"];
		$receipt_no = $resultDeposit["receipt_no"];
		$tenant_name = $resultDeposit["tenant_name"];
		$unit_no = $resultDeposit["unit_no"];
		$rental_rate = $resultDeposit["rental_rate"];
		$PICname = $resultDeposit["name"];
		$created_at = $resultDeposit["created_at"];
		$location_name = $resultDeposit["location_name"];


?>
<div style="page-break-after: always;">
<?php

	include("print_receipt_deposit.php");
	?>
	<table style="border-color:#FFFFFF;font-family:'Times New Roman', Times, serif" align="center" width="100%">
		
		<tr>
			<td align="right" >Salinan Kewangan</td>
		</tr>
		<tr>
			<td align="center" style="font-size:10px">
				-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --potong disini-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
			</td>
		</tr>
	</table>

	<?php 
	include("print_receipt_deposit.php");
	?>
	<table style="border-color:#FFFFFF;font-family:'Times New Roman', Times, serif" align="center" width="100%">
		
		<tr>
			<td align="right">Salinan PHSSB</td>
		</tr>
		<tr>
			<td align="center" style="font-size:10px">
				-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --potong disini-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
			</td>
		</tr>
	</table>

	<?php 
	include("print_receipt_deposit.php");
	?>
	<table style="border-color:#FFFFFF;font-family:'Times New Roman', Times, serif" align="center" width="100%">
		
		<tr>
			<td>Sila simpan untuk tujuan rekod<br />
			Cop pengesahan</td>
			<td align="right" valign="top">Salinan Penyewa / Pembayar</td>
		</tr>
	</table>

	</div>
	<?php 
		}
} ?>
<!-- resit penyewa lama end -->

<!-- resit penyewa baru start -->
<!-- resit DEPOSIT start -->

<?php 
if($countRowNewRecord > 0){
	while($resultDepositPayments = mysqli_fetch_assoc($runDepositPayments_newRecord)){
		$two_months_amount_paid = $resultDepositPayments["two_months_amount_paid"];
		$utility_amount_paid = $resultDepositPayments["utility_amount_paid"];
		$receipt_no = $resultDepositPayments["receipt_no"];
		$tenant_name = $resultDepositPayments["tenant_name"];
		$unit_no = $resultDepositPayments["unit_no"];
		$rental_rate = $resultDepositPayments["rental_rate"];
		$PICname = $resultDepositPayments["name"];
		$created_at = $resultDepositPayments["created_at"];
		$location_name = $resultDepositPayments["location_name"];
		$total_deposit = $two_months_amount_paid + $utility_amount_paid;

?>
<div style="page-break-after: always;">
<?php

	include("print_receipt_deposit_new_tenant.php");
	?>
	<table style="border-color:#FFFFFF;font-family:'Times New Roman', Times, serif" align="center" width="100%">
		
		<tr>
			<td align="right" >Salinan Kewangan</td>
		</tr>
		<tr>
			<td align="center" style="font-size:10px">
				-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --potong disini-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
			</td>
		</tr>
	</table>

	<?php 
	include("print_receipt_deposit_new_tenant.php");
	?>
	<table style="border-color:#FFFFFF;font-family:'Times New Roman', Times, serif" align="center" width="100%">
		
		<tr>
			<td align="right">Salinan PHSSB</td>
		</tr>
		<tr>
			<td align="center" style="font-size:10px">
				-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --potong disini-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
			</td>
		</tr>
	</table>

	<?php 
	include("print_receipt_deposit_new_tenant.php");
	?>
	<table style="border-color:#FFFFFF;font-family:'Times New Roman', Times, serif" align="center" width="100%">
		
		<tr>
			<td>Sila simpan untuk tujuan rekod<br />
			Cop pengesahan</td>
			<td align="right" valign="top">Salinan Penyewa / Pembayar</td>
		</tr>
	</table>

	</div>
<?php 
	}
} ?>
<!-- resit DEPOSIT end -->
<!-- resit Others start -->

<?php 
if($countDepositPaymentOthers > 0){
	while($resultDepositPaymentOthers = mysqli_fetch_assoc($runDepositPaymentOthers)){
		$stamping_paid = $resultDepositPaymentOthers["stamping_paid"];
		$sst_paid = $resultDepositPaymentOthers["sst_paid"];
		$yp_paid = $resultDepositPaymentOthers["yp_paid"];
		$receipt_no = $resultDepositPaymentOthers["receipt_no"];
		$tenant_name = $resultDepositPaymentOthers["tenant_name"];
		$unit_no = $resultDepositPaymentOthers["unit_no"];
		$rental_rate = $resultDepositPaymentOthers["rental_rate"];
		$PICname = $resultDepositPaymentOthers["name"];
		$created_at = $resultDepositPaymentOthers["created_at"];
		$location_name = $resultDepositPaymentOthers["location_name"];
		
		$total_deposit_others = $stamping_paid + $sst_paid + $yp_paid;


?>
<div style="page-break-after: always;">
<?php
	include("print_other_receipt_deposit_new_tenant.php");
	?>
	<table style="border-color:#FFFFFF;font-family:'Times New Roman', Times, serif" align="center" width="100%">
		
		<tr>
			<td align="right" >Salinan Kewangan</td>
		</tr>
		<tr>
			<td align="center" style="font-size:10px">
				-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --potong disini-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
			</td>
		</tr>
	</table>

	<?php 
	include("print_other_receipt_deposit_new_tenant.php");
	?>
	<table style="border-color:#FFFFFF;font-family:'Times New Roman', Times, serif" align="center" width="100%">
		
		<tr>
			<td align="right">Salinan PHSSB</td>
		</tr>
		<tr>
			<td align="center" style="font-size:10px">
				-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --potong disini-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
			</td>
		</tr>
	</table>

	<?php 
	include("print_other_receipt_deposit_new_tenant.php");
	?>
	<table style="border-color:#FFFFFF;font-family:'Times New Roman', Times, serif" align="center" width="100%">
		
		<tr>
			<td>Sila simpan untuk tujuan rekod<br />
			Cop pengesahan</td>
			<td align="right" valign="top">Salinan Penyewa / Pembayar</td>
		</tr>
	</table>

	</div>
<?php 
	} 
} ?>

<!-- resit Others end -->
<!-- resit penyewa baru end -->



</body>
<script>
	
	function CloseWin()
	{
		//alert("MASUK_hr");
		window.close();
	}
</script >
