<STYLE type="text/css">

/* Styles go here */

.page-header, .page-header-space {
  height: 50px;
}

.page-footer, .page-footer-space {
  height: 20px;

}

.page-footer {
  position: fixed;
  bottom: 0;
  width: 100%;
  border-top: 1px solid black; /* for demo */
  background: yellow; /* for demo */
}

.page-header {
  position: fixed;
  top: 0mm;
  width: 100%;
  /*border-bottom: 1px solid black;*/ /* for demo */
 /*background: yellow;*/ /* for demo */
}

.page {
  page-break-before: always;
}

.watermark{
	background-repeat: repeat;
	background-image: url("img/phssbwatermark.png");
}

@page {
  margin: 10mm
  
}

@media print {
   thead {display: table-header-group;} 
   tfoot {display: table-footer-group;}
   
   button {display: none;}
   
   body {
	margin: 0;
	/*background-repeat: repeat;
	background-image: url("img/phssbwatermark.png");*/
   }
   
}

hr.line {
  border-top: 1px dashed red;
}
}
</STYLE>

<?php
// Initialize the session
session_start();
error_reporting(0);
// Initialize the session
//session_start();
//include('function_bgcolorSS_byrLepas7hb.php');
require_once "config.php";
function dateDifference($start_date, $end_date)
{
    // calulating the difference in timestamps 
    $diff = strtotime($start_date) - strtotime($end_date);
     
    // 1 day = 24 hours 
    // 24 * 60 * 60 = 86400 seconds
    return ceil(abs($diff / 86400));
}



$statementby = $_GET["statementby"];
$statement_year = $_GET["statement_year"];
$statement_monthyear = $_GET["statement_monthyear"];
$dateFrom = $_GET["dateFrom"];
$dateTo = $_GET["dateTo"];
$statement_chooseDate = $_GET["statement_ChooseDate"];
$selected_cutOffPayment = $_GET["cutOffPayment"];

$kiraan30percent_fromBF = 0;
$totalBF_rental = 0;
$totalBF_water = 0;
$firstYearDate = "";
$tenant_id = $_GET["tenant_id"];
//GET TENANT INFO

	include ("tenantAndDeposit_statementInfo.php");
	
	//tenant status start--
	$tenant_status_show = "";
	if($tenant_status = 'enable')
		$tenant_status_show = "Active";
	else if($tenant_status = 'disable')
		$tenant_status_show = "Deactive";
	
	//tenant status END
	
	$cutOffDate = date("Y-m-d");
	
	//CUT OFF RENTAL STATEMENT INFO - START
	$issueDate_cutOff = date("Y-m-d");
	$sqlSearchDate_latestPayment_rental = "SELECT payments.created_at as payment_date from payments inner join bills on payments.bill_id = bills.id where bills.tenant_id = $tenant_id and bills.bill_type_id = 1 ORDER BY `payments`.`created_at` DESC limit 1";
	$runSearchDate_latestPayment_rental = mysqli_query($link, $sqlSearchDate_latestPayment_rental);
	if($runSearchDate_latestPayment_rental === FALSE){
		die(mysqli_error($link));
	}
	if($resultSearchDate_latestPayment_rental=mysqli_fetch_assoc($runSearchDate_latestPayment_rental)){
		$date_latestPayment_rental = $resultSearchDate_latestPayment_rental["payment_date"];
		$date_latestPayment_rental = date("Y-m-d",strtotime($date_latestPayment_rental));
		
		if($date_latestPayment_rental >  date("Y-m-d")){
			$issueDate_cutOff = $date_latestPayment_rental;
		}
		else if($date_latestPayment_rental <=  date("Y-m-d")){
			$issueDate_cutOff = date("Y-m-d");
		}
	}
	//CUT OFF RENTAL STATEMENT INFO - END
	
	if($_GET["statementby"] == ""){
	$sqlStatementRental ="SELECT * FROM ( 
						(SELECT payments.id as payment_id,payments.receipt_no, payments.amount_paid as amount, bills.issue_date as issue_date, payments.created_at as date_created,  bills.payment_status as status_payment, concat(payments.receipt_no,' payment ',payments.amount_paid) as info FROM payments
						inner join bills on payments.bill_id = bills.id 
						where bills.tenant_id = $tenant_id and bills.bill_type_id = 1 ) 
						UNION ALL (SELECT bills.id, bills.bill_no, bills.bill_amount as amount, bills.issue_date as issue_date, bills.issue_date as date_created, bills.payment_status as status_payment, concat(bills.bill_no,' bill ',bill_amount) as info  FROM bills 
						where bills.tenant_id = $tenant_id  and bills.bill_type_id = 1 and bills.issue_date <= '$issueDate_cutOff') ) 
						results ORDER BY date_created ASC";
	
	$sqlStatementWater ="SELECT * FROM ( 
						(SELECT payments.id as payment_id,payments.receipt_no, payments.amount_paid as amount, bills.issue_date as issue_date, payments.created_at as date_created,  bills.payment_status as status_payment, concat(payments.receipt_no,' payment ',payments.amount_paid) as info FROM payments
						inner join bills on payments.bill_id = bills.id
						where bills.tenant_id = $tenant_id and bills.bill_type_id = 2) 
						UNION ALL (SELECT bills.id, bills.bill_no, bills.bill_amount as amount, bills.issue_date as issue_date, bills.issue_date as date_created, bills.payment_status as status_payment, concat(bills.bill_no,' bill ',bill_amount) as info  FROM bills 
						where bills.tenant_id = $tenant_id  and bills.bill_type_id = 2) ) 
						results ORDER BY date_created ASC";
	
	  $sqlAgeing = " SELECT * FROM ( 
							(select  bills.bill_amount- sum( payments.amount_paid ) as total_outstanding , bills.*,payments.created_at as payment_date, properties.unit_no, tenants.tenant_name 
							from payments 
							inner join bills on payments.bill_id = bills.id 
							inner join tenants on bills.tenant_id =  tenants.id  
							
							inner join properties on tenants.property_id = properties.id
							inner join locations on properties.location_id = locations.id
							where bills.tenant_id =$tenant_id  and bills.issue_date <= '$cutOffDate' and bills.bill_type_id = 1 group by bills.issue_date having total_outstanding <> 0  ) 
							UNION  
								(select sum(bills.bill_amount) as total_outstanding, bills.* , bills.created_at as  payment_date,  properties.unit_no, tenants.tenant_name 
									from bills 
									inner join tenants on bills.tenant_id =  tenants.id  
									
									inner join properties on tenants.property_id = properties.id
									inner join locations on properties.location_id = locations.id
									where bills.issue_date  <= '$cutOffDate'
									and bills.id not in (select payments.bill_id from payments inner join bills on payments.bill_id = bills.id WHERE bills.tenant_id = $tenant_id )
									and tenant_id = $tenant_id  
									and bills.bill_type_id = 1
									group by issue_date having total_outstanding <> 0 and bills.issue_date < CURRENT_DATE)
							) results group by issue_date ORDER BY issue_date ASC";
	
	}
	ELSE { 
		include("query_statement.php");
	}
	
	//GET STATEMENT INFO FROM BILL AND PAYMENT TABLE 
	
		
		
			
	
			$runSqlStatementRental = mysqli_query($link, $sqlStatementRental);
			$runSqlStatementWater = mysqli_query($link, $sqlStatementWater);
			
			if($runSqlStatementRental === FALSE){
				die(mysqli_error($link));
			}
			if($runSqlStatementWater === FALSE){
				die(mysqli_error($link));
			}
	
?>
<body height="100%" style="font-family:verdana;font-size:12px" onLoad="window.print();" >
	  <div class="page-header" style="text-align: center">
		<table>
			<tr>
				<td width="100%" style="font-size: 100%;" bgcolor="yellow"><b>PERUMAHAN DAN HARTANAH SELANGOR SDN BHD</b></td>
				<td style="font-size: 30px" align="right" width="35%" >
					<i>STATEMENT</i>
					<br/>
					<font size="1px"><?php echo $penyata_id?></font>
				</td>
			</tr>
		</table>
		
	  </div>

	  <div class="page-footer" align="center">
		www.phssb.com
	  </div>
	<div class="watermark" align="center">
	  </div>
		

	<table width="100%" height="100%" style="position: absolute; top: 0; bottom: 0; left: 0; right: 0;" >
		<thead>
			<tr>
				<td>
				<!--place holder for the fixed-position header-->
				<div class="page-header-space"></div>
				</td>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td>
					<table valign="top" width="100%" >
						<tr>
							<td colspan="3"  rowspan="4" width="50%">
								<!--<div style="font-size: 100%;" ><b>PERUMAHAN DAN HARTANAH SELANGOR SDN BHD</b></div>-->
								<div style="font-size: 12px;" >
									Tingkat 5, Podium Utara, <br/>
									Bangunan Sultan Salahuddin Abdul Aziz Shah, <br/>
									Selangor Darul Ehsan <br/>
									Tel: 03 5524 0233 / 0253 (Pejabat)<br/>
									Fax: 03 5524 0243 <br/>
									Web: www.phssb.com <?php //echo $sqlStatement?>
								</div>
							</td>
							<td  valign="top" width="50%" align="right" STYLE="float:right">
								<table style="font-size: 12px;"  >
									<tr valign="top">
										<td colspan="3" style="font-size: 30px" align="right" >&nbsp;</i></td>
									</tr>
									<tr>
										<td  valign="top">
											Statement No. 
										</td>
										<td>:</td>
										<td valign="top">
											<?php echo $penyata_id?>
										</td>
									</tr>
									<tr>
										<td  valign="top">
											Date 
										</td>
										<td>:</td>
										<td valign="top">
											<?php echo date("d/m/Y");?>
										</td>
									</tr>
									<tr>
										<td  valign="top">
											Tenant ID 
										</td>
										<td>:</td>
										<td valign="top">
											<?php echo $tenant_id;?>
										</td>
									</tr>
								</table>
							</td>
						</tr>
					</table>
					<br/>
					<div style="border-collapse: collapse;border-bottom: 1px solid black;"  style="line-height: 3;">
						<table width="100%" style="font-size: 12px;" border='0' >
							<tr>
								<td bgcolor="#000000" colspan = "2" width="45%"><font color="white" style="font-size: 12px;"><b>BILL TO:</b></font> </td>
							</tr>
							<tr>
								<td>
									<table border='0'  width="100%" >
										<tr>
											<td width="55%" valign="top">
												<table style="font-size: 12px;" border='0'  width="100%">
													
													<tr>
														<td width='30%'><strong>NAME</strong></td>
														
														<td width='50%'><?php echo strtoupper($tenant_name)?></td>
													</tr>
													<tr>
														<td width='30%'><strong>IC NO.</strong></td>
														
														<td width='50%'><?php echo $tenant_ic?></td>
													</tr>
													<tr>
														<td width='30%'><strong>CONTACT NO.</strong></td>
														
														<td width='50%'><?php echo $contact_no?></td>
													</tr>
													
													<tr>
														<td width='30%'><strong>TYPE OF PROPERTY</strong></td>
														
														<td width='50%'><?php echo strtoupper($project_name)?></td>
													</tr>
													<tr>
														<td width='30%'><strong>PROPERTY LOCATION</strong></td>
														
														<td width='50%'><?php echo strtoupper($maklumatHartanah)?></td>
													</tr>
													<tr>
														<td width='30%'><strong>RENTAL START</strong></td>
														
														<td width='50%'><?php echo $start_rent?></td>
													</tr>
													<tr>
														<td width='30%'><strong>RENTAL END</strong></td>
														
														<td width='50%'><?php echo $end_rent?></td>
													</tr>
												</table>
											</td>
										</tr>
									</table>
								</td>
							</tr>
						</table>
					</div>
					<BR/>
					<div style="border-collapse: collapse;border-bottom: 1px solid black;"  style="line-height: 3;">
						<table width="100%" style="font-size: 12px;" border='0'>
							<tr>
								<td bgcolor="#000000" colspan = "2" width="45%"><font color="white" style="font-size: 12px;"><b>DEPOSIT INFO:</b</font></td>
							</tr>
							<tr>
								<td>
									<table class='tablebill' border='1' style="border-collapse: collapse;font-size: 12px;" width="100%" >
										<thead>
										<tr><th colspan="8">Deposit Payment (Payment below September 2024)</th></tr>
										  <tr>
											<td align="center"><b>#</b></td>
											<td align="center"><b>Receipt No.</b></td>
											<td align="center"><b>Two Months Rental(RM)</b></td>
											<td align="center"><b>Utilities(RM)</b></td>
											<td align="center"><b>Management Fee(RM)</b></td>
											<td align="center"><b>Total(RM)</b></td>
											<td align="center"><b>Date Paid</b></td>
											<td align="center"><b>Staff Incharge</b></td>
										  </tr>
										</thead>
										<tbody>
										  <?php 
											$no=1;
											$totalAllDepositPayment = 0.00;
											//$countRowDeposit =mysqli_num_rows($runDeposit);
											if($countRowOldRecord >= 1){
											 while($resultDeposit_oldRecord = mysqli_fetch_array($runDeposit_oldRecord)){
													$deposit_id = $resultDeposit_oldRecord["id"];
													$receipt_no = $resultDeposit_oldRecord["receipt_no"];
													$two_month_rental = $resultDeposit_oldRecord["two_month_rental"];
													$utilities = $resultDeposit_oldRecord["utilities"];
													$management_fee = $resultDeposit_oldRecord["management_fee"];
													$created_at = $resultDeposit_oldRecord["created_at"];
													$pic = $resultDeposit_oldRecord["name"];
													$total_deposit = $resultDeposit_oldRecord["total_deposit"];
													$totalAllDepositPayment += $total_deposit;
												
										?>
											<tr>
												<td align="center"><?php echo $no?></td>
												<td align="center"><a href="print_deposit.php?deposit_id=<?php echo $deposit_id?>" target="_blank"><?php echo $receipt_no?></a></td>
												<td align="right"><?php echo number_format($two_month_rental,2)?></td>
												<td align="right"><?php echo number_format($utilities,2)?></td>
												<td align="right"><?php echo number_format($management_fee,2)?></td>
												<td align="right"><?php echo number_format($total_deposit,2)?></td>
												<td align="center"><?php echo date("d/m/Y",strtotime($created_at))?></td>
												<td align="center"><?php echo ucfirst($pic)?></td>
											</tr>
											<?php 
											$no++;
											} ?>
											<tr>
												<td colspan="5"><b>TOTAL (RM)</b></td>
												<td align="right"><b><?php echo number_format($totalAllDepositPayment,2)?></b></td>
												<td colspan="2">&nbsp;</td>
											</tr>
											<?php } else if($countRowOldRecord == 0){
													?>
												<tr align="center"><th colspan="8">~ No Record Found ~</th></tr>	
												<?php
												}
												?>
									</TABLE>
									<br/>
									<table class='tablebill' border='1' style="border-collapse: collapse;font-size: 12px;" width="100%" >
									<tr><th colspan="8">Deposit Payment (Payment on September 2024 and above)</th></tr>
						<tr align="center">
							<td rowspan="2"><b>#</b></td>
							<td rowspan="2"><b>DATE</b></td>
							<td rowspan="2"><b>INVOICE / RECEIPT</b></td>
							<td rowspan="2"><b>DESCRIPTION</b></td>
							<td><b> (Db)</b></td>
							<td><b> (Cr)</b></td>
							<td><b>BALANCE</b></td>
						</tr>
						<tr align="center">
							<td><b>(RM)</b></td>
							<td><b>(RM)</b></td>
							<td><b>(RM)</b></td>
						</tr>
						
						<?php
						$no = 1;
						 if($countStatementDeposit_newRecord > 0){
							 $depo_bf = 0.00;
							 $depo_baki = 0.00;
							 $depo_totalDb = 0.00;
							 $depo_totalCr = 0.00;
							 ?>
							 
						<tr>
							<td align="center" valign="top"  ><?php echo $no;?></td>
							<td align="center" valign="top"  ></td>
							<td align="center" valign="top"  ></td>
							<td align="center" valign="top"  >B/F</td>
							<td align="center" valign="top"  ></td>
							<td align="center" valign="top"  ></td>
							<td align="right" valign="top"  >0.00</td>
						</tr>	 
							 <?php 
							while($resultStatementDeposit_newRecord = mysqli_fetch_array($runStatementDeposit_newRecord)){
								$no++;
								$deposit_id = $resultStatementDeposit_newRecord["id"];
								$receipt_no = $resultStatementDeposit_newRecord["receipt_no"];
								$two_months_amount_paid = $resultStatementDeposit_newRecord["two_months_amount_paid"];
								$two_month_rental_amount_bill = $resultStatementDeposit_newRecord["two_month_rental_amount_bill"];
								$utility_amount_paid = $resultStatementDeposit_newRecord["utility_amount_paid"];
								$utility_amount_bill = $resultStatementDeposit_newRecord["utility_amount_bill"];
								$created_at = $resultStatementDeposit_newRecord["created_at"];
								$deposit_bill_id = $resultStatementDeposit_newRecord["deposit_bill_id"];
								$deposit_bill_no = $resultStatementDeposit_newRecord["deposit_bill_no"];
								$payment_status = $resultStatementDeposit_newRecord["payment_status"];
								$info_two_months_amount = $resultStatementDeposit_newRecord["info_two_months_amount"];
								$info_utility_amount = $resultStatementDeposit_newRecord["info_utility_amount"];
								$total_deposit_amount = $two_month_rental_amount + $utility_amount;
								$total_deposit_paid = $two_months_amount_paid + $utility_amount_paid;
								$paymentType = "";
								//$totalAllDepositPayment += $total_deposit;
								
								
								$description_2MonthRental = "";
								$description_utility = "";
								if($two_month_rental_amount_bill != "" ||  $two_month_rental_amount_bill != null || $two_months_amount_paid != "" ||  $two_months_amount_paid != null){
									$description_2MonthRental = "Two Month Rental";
								}
								if($utility_amount_bill != "" ||  $utility_amount_bill != null || $utility_amount_paid != "" ||  $utility_amount_paid != null ){
									$description_utility = "Utility";
								}
								
								if(strpos($info_two_months_amount, 'deposit_bill') !== false  ) {
									$depo_baki += $two_month_rental_amount_bill;
									$depo_totalDb += $two_month_rental_amount_bill;
									//echo " DEPO BAKI: IF 1 >> " . $depo_baki;
								}
								if(strpos($info_utility_amount, 'deposit_bill') !== false){
									$depo_baki += $utility_amount_bill;
									$depo_totalDb += $utility_amount_bill;
									//echo " DEPO BAKI: IF 2 >> " . $depo_baki;
								}
								if(strpos($info_two_months_amount, 'deposit_payment') !== false ) {
									$depo_baki -= $two_months_amount_paid;
									$depo_totalCr += $two_months_amount_paid;
									//echo " DEPO BAKI: IF 3 >> " . $depo_baki;
								}
								if(strpos($info_utility_amount, 'deposit_payment') !== false){
									$depo_baki -= $utility_amount_paid;
									$depo_totalCr += $utility_amount_paid;
									//echo " DEPO BAKI: IF 4 >> " . $depo_baki;
								}
								
								
							?>
							<tr>
								<td align="center" valign="top" rowspan="2" ><?php echo $no;?></td>
								<td align="center" valign="top" rowspan="2"><?php echo date("d/m/Y",strtotime($created_at));?></td>
								<td align="center" valign="top" rowspan="2"><?php echo $receipt_no;?></td>
								<?php if (strpos($info_two_months_amount, 'deposit_bill') !== false) { ?>
								<td align="center" valign="top" ><?php echo $description_2MonthRental;?></td>
								<td align="right" valign="top" ><?php echo number_format($two_month_rental_amount_bill,2);?></td>
								<td align="center" valign="top" ></td>
								<td align="right" valign="top" rowspan="2"><?php echo number_format($depo_baki,2);?></td>
								</tr>
								
								<?php }  if (strpos($info_utility_amount, 'deposit_bill') !== false) { ?>
								<tr>
								<td align="center" valign="top" ><?php echo $description_utility;?></td>
								<td align="right" valign="top" ><?php echo number_format($utility_amount_bill,2);?></td>
								<td align="center" valign="top" ></td>
								
								<?php }  if (strpos($info_two_months_amount, 'deposit_payment') !== false) { ?>
								<td align="center" valign="top" ><?php echo $description_2MonthRental;?></td>
								<td align="center" valign="top" ></td>
								<td align="right" valign="top" ><?php echo number_format($two_months_amount_paid,2);?></td>
								<td align="right" valign="top" rowspan="2"><?php echo number_format($depo_baki,2);?></td>
								</tr>
								<?php }  if (strpos($info_utility_amount, 'deposit_payment') !== false) { ?>
								<td align="center" valign="top" ><?php echo $description_utility;?></td>
								<td align="center" valign="top" ></td>
								<td align="right" valign="top" ><?php echo number_format($utility_amount_paid,2);?></td>
								
								<?php } ?>
							</tr>
							<?php
							}?>
							<tr>
								<td colspan="4" align="right"><b>Total (RM)</b></td>
								<td align="right"><b><?php echo number_format($depo_totalDb,2)?></b></td>
								<td align="right"><b><?php echo number_format($depo_totalCr,2)?></b></td>
								<td align="right"><b><?php echo number_format($depo_baki,2)?></b></td>
							</tr>
							<?php } 
							else{
								?>
							<tr>
								<td colspan="7" align="center"><b>~No Record Found~</b></td>
							</tr>	
								
								<?PHP
							}?>
					</table>
					<br/>
					<table class='tablebill' border='1' style="border-collapse: collapse;font-size: 12px;" width="100%" >
						<tr><th colspan="8">Others Payment</th></tr>
						<tr align="center">
							<td rowspan="2"><b>#</b></td>
							<td rowspan="2"><b>DATE</b></td>
							<td rowspan="2"><b>INVOICE / RECEIPT</b></td>
							<td rowspan="2"><b>DESCRIPTION</b></td>
							<td><b> (Db)</b></td>
							<td><b> (Cr)</b></td>
							<td><b>BALANCE</b></td>
						</tr>
						<tr align="center">
							<td><b>(RM)</b></td>
							<td><b>(RM)</b></td>
							<td><b>(RM)</b></td>
						</tr>
						
						<?php
						$no = 1;
						 if($countRowPaymentOthers > 0){
							 $depoOthers_bf = 0.00;
							 $depoOthers_baki = 0.00;
							 $depoOthers_totalDb = 0.00;
							 $depoOthers_totalCr = 0.00;
							 ?>
							 
						<tr>
							<td align="center" valign="top"  ><?php echo $no;?></td>
							<td align="center" valign="top"  ></td>
							<td align="center" valign="top"  ></td>
							<td align="center" valign="top"  >B/F</td>
							<td align="center" valign="top"  ></td>
							<td align="center" valign="top"  ></td>
							<td align="right" valign="top"  >0.00</td>
						</tr>	 
							 <?php 
							 
							while($resultDepositPaymentOthers = mysqli_fetch_array($runDepositPaymentOthers)){
								$no++;
								$deposit_id = $resultDepositPaymentOthers["id"];
								$receipt_no = $resultDepositPaymentOthers["receipt_no"];
								$yp_paid = $resultDepositPaymentOthers["yp_paid"];
								$yp_charges_amount = $resultDepositPaymentOthers["yp_charges_amount"];
								$sst_paid = $resultDepositPaymentOthers["sst_paid"];
								$sst_charges_amount = $resultDepositPaymentOthers["sst_charges_amount"];
								$stamping_paid = $resultDepositPaymentOthers["stamping_paid"];
								$stamping_charges_amount = $resultDepositPaymentOthers["stamping_charges_amount"];
								$created_at = $resultDepositPaymentOthers["created_at"];
								$deposit_bill_other_id = $resultDepositPaymentOthers["deposit_bill_other_id"];
								$deposit_other_bill_no = $resultDepositPaymentOthers["deposit_other_bill_no"];
								$payment_status = $resultDepositPaymentOthers["payment_status"];
								$info_yp_paid = $resultDepositPaymentOthers["info_yp_paid"];
								$info_sst_paid = $resultDepositPaymentOthers["info_sst_paid"];
								$info_stamping_paid = $resultDepositPaymentOthers["info_stamping_paid"];
								$total_depositOthers_amount = $yp_charges_amount + $sst_charges_amount + $stamping_charges_amount;
								$total_depositOthers_paid = $yp_paid + $sst_paid + $stamping_paid;
								$paymentType = "";
								//$totalAllDepositPayment += $total_deposit;
								
								
								$description_YP = "";
								$description_SST = "";
								$description_Stamping = "";
								if($yp_charges_amount != "" ||  $yp_charges_amount != null || $yp_paid != "" ||  $yp_paid != null){
									$description_YP = "Yuran Pelbagai";
								}
								if($sst_charges_amount != "" ||  $sst_charges_amount != null || $sst_paid != "" ||  $sst_paid != null ){
									$description_SST = "SST Charges";
								}
								if($stamping_charges_amount != "" ||  $stamping_charges_amount != null || $stamping_paid != "" ||  $stamping_paid != null ){
									$description_Stamping = "Stamping Charges";
								}
								
								if(strpos($info_yp_paid, 'deposit_other_bill') !== false  ) {
									$depo_others_baki += $yp_charges_amount;
									$depo_others_totalDb += $yp_charges_amount;
									$description_YP = "Yuran Pelbagai";
									//echo " DEPO BAKI: IF 1 >> " . $depo_others_baki;
								}
								if(strpos($info_sst_paid, 'deposit_other_bill') !== false){
									$depo_others_baki += $sst_charges_amount;
									$depo_others_totalDb += $sst_charges_amount;
									$description_SST = "SST Charges";
									//echo " DEPO BAKI: IF 2 >> " . $depo_others_baki;
								}
								if(strpos($info_stamping_paid, 'deposit_other_bill') !== false){
									$depo_others_baki += $stamping_charges_amount;
									$depo_others_totalDb += $stamping_charges_amount;
									$description_Stamping = "Stamping Charges";
									//echo " DEPO BAKI: IF 2 >> " . $depo_others_baki;
								}
								if(strpos($info_yp_paid, 'deposit_other_payment') !== false ) {
									$depo_others_baki -= $yp_paid;
									$depo_others_totalCr += $yp_paid;
									$description_YP = "Yuran Pelbagai";
									//echo " DEPO BAKI: IF 3 >> " . $depo_others_baki;
								}
								if(strpos($info_sst_paid, 'deposit_other_payment') !== false){
									$depo_others_baki -= $sst_paid;
									$depo_others_totalCr += $sst_paid;
									$description_SST = "SST Charges";
									//echo " DEPO BAKI: IF 4 >> " . $depo_others_baki;
								}
								if(strpos($info_stamping_paid, 'deposit_other_payment') !== false){
									$depo_others_baki -= $stamping_paid;
									$depo_others_totalCr += $stamping_paid;
									$description_Stamping = "Stamping Charges";
									//echo " DEPO BAKI: IF 4 >> " . $depo_others_baki;
								}
								
							?>
							<tr>
								<td align="center" valign="top" rowspan="3" ><?php echo $no;?></td>
								<td align="center" valign="top" rowspan="3"><?php echo date("d/m/Y",strtotime($created_at));?></td>
								<td align="center" valign="top" rowspan="3"><?php echo $receipt_no;?></td>
								<?php if (strpos($info_yp_paid, 'deposit_other_bill') !== false) { ?>
								<td align="center" valign="top" ><?php echo $description_YP;?></td>
								<td align="right" valign="top" ><?php echo number_format($yp_charges_amount,2);?></td>
								<td align="center" valign="top" ></td>
								<td align="right" valign="top" rowspan="3"><?php echo number_format($depo_others_baki,2);?></td>
								</tr>
								
								<?php }  if (strpos($info_sst_paid, 'deposit_other_bill') !== false) { ?>
								<tr>
								<td align="center" valign="top" ><?php echo $description_SST;?></td>
								<td align="right" valign="top" ><?php echo number_format($sst_charges_amount,2);?></td>
								<td align="center" valign="top" ></td>
								</tr>
								<?php }  if (strpos($info_stamping_paid, 'deposit_other_bill') !== false) { ?>
								<tr>
								<td align="center" valign="top" ><?php echo $description_Stamping;?></td>
								<td align="right" valign="top" ><?php echo number_format($stamping_charges_amount,2);?></td>
								<td align="center" valign="top" ></td>
								<tr/>
								<?php }  if (strpos($info_yp_paid, 'deposit_other_payment') !== false) { ?>
								<td align="center" valign="top" ><?php echo $description_YP;?></td>
								<td align="center" valign="top" ></td>
								<td align="right" valign="top" ><?php echo number_format($yp_paid,2);?></td>
								<td align="right" valign="top" rowspan="3"><?php echo number_format($depo_others_baki,2);?></td>
								</tr>
								<?php }  if (strpos($info_sst_paid, 'deposit_other_payment') !== false) { ?>
								<tr>
								<td align="center" valign="top" ><?php echo $description_SST;?></td>
								<td align="center" valign="top" ></td>
								<td align="right" valign="top" ><?php echo number_format($sst_paid,2);?></td>
								</tr>
								<?php }  if (strpos($info_stamping_paid, 'deposit_other_payment') !== false) { ?>
								<tr>
								<td align="center" valign="top" ><?php echo $description_Stamping;?></td>
								<td align="center" valign="top" ></td>
								<td align="right" valign="top" ><?php echo number_format($stamping_paid,2);?></td>
								<?php } ?>
							</tr>
							<?php
								
							}?>
							<tr>
								<td colspan="4" align="right"><b>Total (RM)</b></td>
								<td align="right"><b><?php echo number_format($depo_others_totalDb,2)?></b></td>
								<td align="right"><b><?php echo number_format($depo_others_totalCr,2)?></b></td>
								<td align="right"><b><?php echo number_format($depo_others_baki,2)?></b></td>
							</tr>
							<?php } 
							else{
								?>
							<tr>
								<td colspan="7" align="center"><b>~No Record Found~</b></td>
							</tr>	
								
								<?PHP
							}?>
					</table>
								</td>
							</tr>
						</table>
					</div>
					<div style="border-collapse: collapse;border-bottom: 1px solid black;"  style="line-height: 3;">
						<table class='tablebill' border='1' style="border-collapse: collapse;font-size: 12px;" width="100%" >
							<tr>
								<td bgcolor="#000000" colspan = "7" width="45%"><font color="white" style="font-size: 12px;"><b>RENTAL INFO</b</font></td>
							</tr>
							<tr align="center">
								<td rowspan="2"><b>#</b></td>
								<td rowspan="2"><b>DATE</b></td>
								<td rowspan="2"><b>INVOICE / RECEIPT NO</b></td>
								<td rowspan="2"><b>DESCRIPTION</b></td>
								<td><b> (Db)</b></td>
								<td><b>(Cr)</b></td>
								<td><b>BALANCE</b></td>
								
							</tr>
							<tr align="center">
								<td><b>(RM)</b></td>
								<td><b>(RM)</b></td>
								<td><b>(RM)</b></td>
								
							</tr>
							<?php 
							$no=1;
							$showBFDate = "";
							if($firstYearDate != ""){
								$showBFDate = date("d/m/Y",strtotime($firstYearDate));
							}
							?>
							<tr>
								<td align="center"><?php echo $no?></td>
								<td align="center"><?php echo $showBFDate;?></td>
								<td>&nbsp;</td>
								<td>B/F</td>
								<td>&nbsp;</td>
								<td>&nbsp;</td>
								<td align="right"><?php echo number_format($totalBF_rental,2);?></td>
								
							</tr>
							<?php 
							//$no=1;
							$totalPaid = 0;
							$totalUnpaid = 0;
							$totalPaidList = "";
							$totalUnpaidList = "";
							$totalBaki = $totalBF_rental;
							$bill = 0;
							$payment = 0;
							$kiraan30Percent = 0;
							$total_kiraan30Percent = 0;
							$month = "";
							$year = "";
							$slashPosition = 0;
							$RP_rentalMonth = "";
							
							$layak = "";
							$bill_id_smartsewa = "";
							while($resultStatement = mysqli_fetch_array($runSqlStatementRental)){
								include("statement_resultQuery.php");
								
								?>
							<tr>
								<td align="center"><?php echo $no?></td>
								<td align="center"><?php echo $date_created?></td>
								<td><?php echo $id?></td>
								<td><?php echo $pay_info?></td>
								<?php if (strpos($info, 'bill') !== false) { 
										
								?>
								<td align="right"><?php echo number_format($amount,2)?></td>
								<td align="right"></td>
								<td align="right"><?php echo number_format($totalBaki,2)?></td>
								
								<?php 
										
								} if (strpos($info, 'payment') !== false) { 
										
								?>
								<td align="right"></td>
								<td align="right"><?php echo number_format($amount,2)?></td>
								<td align="right"><?php echo number_format($totalBaki,2)?></td>
								
							</tr>
							<?php
							
							}
								}
							?>
							<tr>
								<td align="right" colspan = "4"><b>TOTAL (RM)</b></td>
								<td align="right"><b><?php echo number_format($totalUnpaid,2) ?></b></td>
								<td align="right"><b><?php echo number_format($totalPaid,2) ?></b></td>
								<td align="right"><b><?php echo number_format($totalBaki,2)?></b></td>
								
							</tr>
						</table>
					</div>
			</div>
			<div style="border-collapse: collapse;border-bottom: 1px solid black;"  style="line-height: 3;">
				<table class='tablebill' border='1' style="border-collapse: collapse;font-size: 12px;" width="100%" >
					<tr>
						<td bgcolor="#000000" colspan = "7" width="45%"><font color="white" style="font-size: 12px;"><b>WATER INFO</b</font></td>
					</tr>
					<tr align="center">
						<td rowspan="2"><b>#</b></td>
						<td rowspan="2"><b>DATE</b></td>
						<td rowspan="2"><b>INVOICE / RECEIPT NO</b></td>
						<td rowspan="2"><b>DESCRIPTION</b></td>
						<td><b> (Db)</b></td>
						<td><b>(Cr)</b></td>
						<td><b>BALANCE</b></td>
						
					</tr>
					<tr align="center">
						<td><b>(RM)</b></td>
						<td><b>(RM)</b></td>
						<td><b>(RM)</b></td>
						
					</tr>
					<?php 
					$no=1;
					$showBFDate = "";
					if($firstYearDate != ""){
						$showBFDate = date("d/m/Y",strtotime($firstYearDate));
					}
					?>
					<tr>
						<td align="center"><?php echo $no?></td>
						<td align="center"><?php echo $showBFDate;?></td>
						<td>&nbsp;</td>
						<td>B/F</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td align="right"><?php echo number_format($totalBF_water,2);?></td>
						
					</tr>
					<?php 
					//$no=1;
					$totalPaid = 0;
					$totalUnpaid = 0;
					$totalPaidList = "";
					$totalUnpaidList = "";
					$totalBaki = $totalBF_water;
					$bill = 0;
					$payment = 0;
					$kiraan30Percent = 0;
					$total_kiraan30Percent = 0;
					$month = "";
					$year = "";
					$slashPosition = 0;
					$RP_rentalMonth = "";
					
					$layak = "";
					$bill_id_smartsewa = "";
					while($resultStatement = mysqli_fetch_array($runSqlStatementWater)){
						include("statement_resultQuery.php");
						?>
					<tr>
						<td align="center"><?php echo $no?></td>
						<td align="center"><?php echo $date_created?></td>
						<td><?php echo $id?></td>
						<td><?php echo $pay_info?></td>
						<?php if (strpos($info, 'bill') !== false) { 
								
						?>
						<td align="right"><?php echo number_format($amount,2)?></td>
						<td align="right"></td>
						<td align="right"><?php echo number_format($totalBaki,2)?></td>
						
						<?php 
								
						} if (strpos($info, 'payment') !== false) { 
								
						?>
						<td align="right"></td>
						<td align="right"><?php echo number_format($amount,2)?></td>
						<td align="right"><?php echo number_format($totalBaki,2)?></td>
						
					</tr>
					<?php
						}
					
					}
					?>
					<tr>
						<td align="right" colspan = "4"><b>TOTAL (RM)</b></td>
						<td align="right"><b><?php echo number_format($totalUnpaid,2) ?></b></td>
						<td align="right"><b><?php echo number_format($totalPaid,2) ?></b></td>
						<td align="right"><b><?php echo number_format($totalBaki,2)?></b></td>
						
					</tr>
				</table>
			</div>
			<div style="border-collapse: collapse;border-bottom: 1px solid black;"  style="line-height: 3;">
				<table class='tablebill' border='1' style="border-collapse: collapse;font-size: 12px;" width="100%" >
					<tr>
						<td bgcolor="#000000" colspan = "8" width="45%"><font color="white" style="font-size: 12px;"><b>OUTSTANDING INFO <?PHP echo " UNTIL " . date("d/m/Y",strtotime($cutOffDate))?></b</font></td>
					</tr>
						<tr align="center">
							<td ><b>#</b></td>
							<td><b>INVOICE</b></td>
							<td><b>DATE</b></td>
							<td><b>TOTAL</b></td>
							<td><b>0-30 DAYS</b></td>
							<td><b>31-60 DAYS</b></td>
							<td><b>61-90 DAYS</b></td>
							<td><b>90+ DAYS</b></td>
						</tr>
						<?php 
						
								$runSqlAgeing = mysqli_query($link, $sqlAgeing);
								if($runSqlAgeing === FALSE){
									die(mysqli_error($link));
								}
								
								$bil = 1;
								$bill_ball30 = 0;
								$bill_ball3160 = 0;
								$bill_ball6190 = 0;
								$bill_ball91 = 0;
								$total_billBal = 0;
								$resultSqlAgeing = array();
								while($resultSqlAgeing = mysqli_fetch_array($runSqlAgeing)){
									if($resultSqlAgeing['id'] != null || $resultSqlAgeing['id'] != ""){
										$bill_bal = $resultSqlAgeing['total_outstanding'];
										$invDate = $resultSqlAgeing['issue_date'];
										$invDate = date("Y-m-d",strtotime($resultSqlAgeing['issue_date']));
										$aged_date = date("Y-m-d", strtotime($ageingDate));
										$start_date = $invDate;
										$end_date = $cutOffDate;
										// call dateDifference() function to find the number of days between two dates
										$diff = dateDifference($start_date, $end_date);
										$invDate = date("d/m/Y",strtotime($resultSqlAgeing['issue_date']));
									
								?>
						<tr>
							<td align="center"><b><?php echo $bil; ?></b></td>
							<td align="center"><b><?php echo $resultSqlAgeing['bill_no']; ?></b></td>
							<td align="center"><b><?php echo $invDate; ?></b></td>
							<td align="center"><b><?php echo number_format($bill_bal,2); ?></b></td>
							<td align="center">
								<b><?php 
										if($diff <= 30){
											echo number_format($bill_bal,2);
											$bill_ball30 += $bill_bal;
										}
									?>
								</b>
							</td>
							<td align="center">
								<b><?php 
										if($diff >= 31 && $diff <= 60){
											echo number_format($bill_bal,2);
											$bill_ball3160 += $bill_bal;
										}
									?>
								</b>
							</td>
							<td align="center">
								<b><?php 
										if($diff >= 61 && $diff <= 90){
											echo number_format($bill_bal,2);
											$bill_ball6190 += $bill_bal;
										}
									?>
								</b>
							</td>
							<td align="center">
								<b><?php 
										if($diff > 90){
											echo number_format($bill_bal,2);
											$bill_ball91 += $bill_bal;
										}
									?>
								</b>
							</td>
						</tr>
							<?php
										$bil++;
										$total_billBal += $bill_bal;
									}
								}
							?>
						<tr>
							<td align="center" colspan = "3"><b>TOTAL (RM): </b></td>
							<td align="center"><b><?php echo number_format($total_billBal,2);?></b></td>
							<td align="center"><b><?php echo number_format($bill_ball30,2);?></b></td>
							<td align="center"><b><?php echo number_format($bill_ball3160,2);?></b></td>
							<td align="center"><b><?php echo number_format($bill_ball6190,2);?></b></td>
							<td align="center"><b><?php echo number_format($bill_ball91,2);?></b></td>
						</tr>
					</table>
			</div>
		</td>
	</tr>
</tbody>
<tfoot>
	<tr>
		<td>
		  <!--place holder for the fixed-position footer-->
		  <div class="page-footer-space"></div>
		</td>
	</tr>
</tfoot>
</table>
</body>