<?php 
session_start();
error_reporting(0); 

require_once "config.php";
//set time zone
date_default_timezone_set("Asia/Kuala_Lumpur");

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


$tenant_id = $_GET["tenant_id"];
$deposit_id = $_GET["deposit_id"]; //deposit tenant lama
 " depositPaymentID_print : ". $depositPaymentID_print = $_GET["depositPaymentID_print"]; //deposit tenant baru
 " depositPaymentOtherID_print : ". $depositPaymentOtherID_print = $_GET["depositPaymentOtherID_print"]; //deposit tenant baru

if($deposit_id != ""){
	$sqlDeposit = "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 extra_locations on locations.id = extra_locations.location_id 
						inner join users on extra_locations.user_id = users.id
						where deposits.id = $deposit_id "; 
	$runDeposit = mysqli_query($link, $sqlDeposit);
	if($runDeposit === FALSE){
		die(mysqli_error($link));
	}
}

if($depositPaymentID_print != ""){
	//query untuk cari payment dalam deposit dan others receipt
	//deposit_payments table
	  "   sqlDepositPayments: " .$sqlDepositPayments = "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_payments.id = $depositPaymentID_print";
	$runDepositPayments = mysqli_query($link, $sqlDepositPayments);
	if($runDepositPayments === FALSE){
		die(mysqli_error($link));
	}
}

if($depositPaymentOtherID_print != ""){
	//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 deposit_payment_other_receipts.id = $depositPaymentOtherID_print";
	$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($deposit_id != ""){
	?>
<div style="page-break-after: always;">
	<?php
	
	 while($resultDeposit = mysqli_fetch_assoc($runDeposit)){
		$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"];

		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 -->
<?php } ?>
<!-- resit penyewa baru start -->
<!-- resit DEPOSIT start -->

<?php 
if($depositPaymentID_print != ""){
	?>
	<div style="page-break-after: always;">
	<?php
		while($resultDepositPayments = mysqli_fetch_assoc($runDepositPayments)){
			$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;

			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 -->
<?php } ?>
<!-- resit Others start -->

<?php 
if($depositPaymentOtherID_print != ""){
	?>
	<div style="page-break-after: always;">
	<?php
		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;
			
			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>
	
	//window.close();
    window.onunload = refreshParent;
    function refreshParent() {
        window.opener.location.reload();
    }
</script >
