<?php 
include("sql_payment.php");


if($report_type == "Deposit"){ //REPORT TYPE : DEPOSIT START?>
<table  width="100%" cellspacing="0" style="font-size: 12px">
	<?PHP
	//echo $report_type;
	include("report_deposit.php");
	?>
</table>
<?php } // REPORT TYPE : DEPOSIT END
else if($report_type == "Rental" || $report_type == "Water"){ // REPORT TYPE : RENTAL OR WATER START?>
<table  width="100%" cellspacing="0" style="font-size: 12px">
		
		<?PHP
			
			if($statementby == 'statement_date' && ($_SESSION['dateFrom'] != "" && $_SESSION["dateTo"] != "") ){
					if($_SESSION['radio_viewReport'] == 'viewPlainReport'){
						include("checkbox_plainReport.php");
					}else{
					//UNTUK table -start
						
					$colspanSemasa = 2;
					$colspanTunggakan = 2;
					$colspanTotalSemua = 13;
					$colspan_terimaan  = 5;
					$listYear =array();
					
					include("bill_types.php");
					
					
					
					   "[sqlCountYearAdv] - " .$sqlCountYearAdv = "select YEAR(bills.issue_date) as year, count(YEAR(bills.issue_date)) 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
																	inner join projects on locations.project_id = projects.id
																	where  bills.bill_type_id in ($bill_types_id,$bill_type_Outstanding) 
																	and bills.issue_date > '$dateTo'  
																	and (payments.created_at >= '$dateFrom' and payments.created_at < '$date_cutOFF')  
																	and locations.id = ". $_SESSION['location_id'] ."  
																	
																	group by YEAR(bills.issue_date)";
					$runCountYearAdv = mysqli_query($link, $sqlCountYearAdv);
					if($runCountYearAdv === FALSE){
						die(mysqli_error($link));
					}
					$rowYearAdv = mysqli_num_rows($runCountYearAdv);
					if($rowYearAdv > 0){
						$colspanTotalSemua += $rowYearAdv;
						$colspan_terimaan += $rowYearAdv;
					}
					if($report_type == "Rental" && $bill_type_Outstanding != 0){
						$colspanTunggakan = 3;
						$colspanTotalSemua+=3;
						$colspan_terimaan++;
					}
					if(strtoupper($_SESSION["project_name"]) == "SKIM SMART SEWA"){
						$colspanSemasa = 3;
						$colspan_terimaan++;
					}
					
					//UTK TABLE - END	
				
				?>
			<table class="table table-bordered table-hover table-sm" id="dataTable" width="70%" cellspacing="0" style="font-size: 12px">
				<thead>
				<tr>
					<td colspan="<?php echo $colspanTotalSemua?>"><B><?php echo "MAKLUMAT TERIMAAN BAYARAN SEWA : " .strtoupper($infoTarikhTunggakan)?><span style="float:right"><?php echo "LOKASI: " . $_SESSION["projectLocation"] ?></span></B></td>
				</tr>
				<tr>
					<td ROWSPAN="4" valign="top" align="center" ><b>#</b></td>
					<td ROWSPAN="4" valign="top" align="center" ><b>UNIT</b></td>
					<td ROWSPAN="4" valign="top" align="center" ><b>NAMA PENYEWA</b></td>
					<td ROWSPAN="4" valign="top" align="center" ><b>KADAR SEWA PATUT KUTIP (RM)</b></td>
					<td ROWSPAN="4" valign="top" align="center" ><b>'OPENING' SETAKAT <?php echo date('d/m/Y',strtotime($dateOpening))?></b></td>
					<td ROWSPAN="4" valign="top" align="center" ><b>JUMLAH TUNGGAKAN TERKUMPUL SETAKAT <?php echo date('d/m/Y',strtotime($dateOpening))?></b></td>
					<td colspan="<?php echo $colspan_terimaan?>" align="center"><b>TERIMAAN UNTUK <?php echo strtoupper($bulanTerimaan)?></b></td>
					<td ROWSPAN="3" valign="top" align="center" ><b>'CLOSING' SEHINGGA <?php echo date('d/m/Y',strtotime($dateTo))?></b></td>
					<td ROWSPAN="3" valign="top" align="center" ><b>JUMLAH TUNGGAKAN SEHINGGA <?php echo date('d/m/Y',strtotime($dateTo))?></b></td>
				</tr>
				<tr>
					<td align="center"><b>TELAH BAYAR BULAN SEBELUM</b></td>
					<td align="center" colspan="<?php echo $colspanSemasa?>"><b>SEMASA</b></td>
					<td align="center" colspan="<?php echo $colspanTunggakan?>"><b>TUNGGAKAN</b> </td>
					<?php if($rowYearAdv > 0){?>
					<td align = "center" colspan="<?php echo $rowYearAdv ?>"><b>ADVANCE</b></td>
					<?php } ?>
				</tr>
				<tr>
					<td align="center"><b>JUMLAH  </b></td>
					<td align="center" ROWSPAN="2"><b>TARIKH </b></td>
					<td align="center"><b>JUMLAH </b></td>
					<?PHP 
					if(strtoupper($_SESSION["project_name"]) == "SKIM SMART SEWA"){
						?>
						<td align="center"><b>30% </b></td>
						<?PHP
					}
					?>
					<td align="center"><b>TAHUN SEBELUM </b></td>
					<td align="center"><b>TAHUN SEMASA </b></td>
					<?php if($bill_type_Outstanding != 0){?>
					<td align="center"><b> <?php echo $bill_type_name ?> </b></td>
					<?php } 
					$loop = 0;
					while($resultYearAdv = mysqli_fetch_array($runCountYearAdv)){ ?>
					<td align="center"><b>TAHUN <?PHP ECHO  $resultYearAdv["year"]; ?>  </b></td> 
					<?php 
					$listYear[] = $resultYearAdv["year"];
					$loop++;	}
					?>
				</tr>
				<tr>
					<td align="center"><b>(RM) </b></td>
					<td align="center"><b>(RM) </b></td>
					<?PHP 
					if(strtoupper($_SESSION["project_name"]) == "SKIM SMART SEWA"){
						?>
						<td align="center"><b>(RM)</b></td>
						<?PHP
					}
					?>
					<td align="center"><b>(RM) </b></td>
					<td align="center"><b>(RM) </b></td>
					<?php for($i=1;$i<=$loop;$i++){?>
					<td align="center"><b>(RM) </b></td>
					<?php } ?>
					<td align="center"><b>(RM) </b></td>
					<td align="center"><b>(RM) </b></td>
				</tr>
				</thead>
				
				<?php 
				$no = 1;
				$total_bayaranBulanSebelum = 0.00;
				$total_bayaranBulanSemasa = 0.00;
				$total_bayaranAdvance = 0.00;
				$total_bayaranTahunSebelum = 0.00;
				$total_bayaranTahunSebelumList = "";
				$total_bayaranTahunSebelumTenantID = "";
				$total_bayaranTahunSemasa = 0.00;
				$total_bayaranTunggakanNotPHSSB = 0.00;
				$bigTotalOpening = 0.00;
				$bigTotalTunggakan = 0.00;
				$bigTotalBillAmount = 0.00;
				$bigTotalAdvance = 0.00;
				
				
				
				while($resultRental = mysqli_fetch_array($runReport)){
					$totalOpening = 0.00;
					$totalTunggakan = 0.00;
					$totalTunggakan_end = 0.00;
					$closing = 0.00;
					$bill_amount = 0.00;
					$payment_bulanSebelum = 0.00;
					$payment_semasa = 0.00;
					$payment_TahunSebelum = 0.00;
					$payment_TahunSemasa = 0.00;
					$payment_tunggakanBukanPHSSB = 0.00;
					$payment_advance = 0.00;
					$totalADV_Set = 0.00;
					
					$rowYearAdv2 = 0;
							
					$property_id = $resultRental["property_id"];
					$unit_no = $resultRental["unit_no"];
					
					
					$sqlTenant = "select tenants.id as tenant_id, tenants.tenant_name, tenants.rental_rate from tenants 
											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 properties.id = $property_id $sqlTenantStatus  $sqlListBlock
											";
					$runTenant = mysqli_query($link, $sqlTenant);
					if($runTenant === FALSE){
						die(mysqli_error($link));
					}
					
					
					while($resultTenant = mysqli_fetch_array($runTenant)){
						$tenant_id = $resultTenant["tenant_id"];
						$tenant_name = $resultTenant["tenant_name"];
						$rental_rate = $resultTenant["rental_rate"];
					
						//1. column bayaran semasa
						$monthYear_created_at = "";
						$monthYear_issue_date = "";
						$monthYear_dateFrom = "";
						//2. column bayaran tunggakan tahun sebelum 
						$year_created_at = "";
						$year_issue_date = "";
						$year_dateFrom ="";
						
						//start kiraan total opening atau B/F
						$sqlTotalBillAmountBeforeSelectedYear_rental = "SELECT sum(bill_amount) as totalBillAmountBeforeSelectedYear FROM `bills` where tenant_id = $tenant_id and bills.bill_type_id = $bill_types_id and bills.issue_date < '$dateFrom'";
						$sqlTotalAmountPaidBeforeSelectedYear_rental = "SELECT sum(amount_paid) as totalAmountPaidBeforeSelectedYear FROM `payments` inner join bills on payments.bill_id = bills.id 
																		where bills.tenant_id = '$tenant_id' and bills.bill_type_id = $bill_types_id and payments.created_at < '$dateFrom' ";
						$runTotalBillAmountBeforeSelectedYear_rental = mysqli_query($link, $sqlTotalBillAmountBeforeSelectedYear_rental);
						if($runTotalBillAmountBeforeSelectedYear_rental === FALSE){
						die(mysqli_error($link));
						}
						$runTotalAmountPaidBeforeSelectedYear_rental = mysqli_query($link, $sqlTotalAmountPaidBeforeSelectedYear_rental);
						if($runTotalAmountPaidBeforeSelectedYear_rental === FALSE){
						die(mysqli_error($link));
						}

						$resultTotalBillAmountBeforeSelectedYear_rental = mysqli_fetch_assoc($runTotalBillAmountBeforeSelectedYear_rental);
						$resultTotalAmountPaidBeforeSelectedYear_rental = mysqli_fetch_assoc($runTotalAmountPaidBeforeSelectedYear_rental);

						$totalBillAmountBeforeSelectedYear_rental = $resultTotalBillAmountBeforeSelectedYear_rental["totalBillAmountBeforeSelectedYear"];
						$totalAmountPaidBeforeSelectedYear_rental = $resultTotalAmountPaidBeforeSelectedYear_rental["totalAmountPaidBeforeSelectedYear"];
						$totalBF = abs($totalBillAmountBeforeSelectedYear_rental) - abs($totalAmountPaidBeforeSelectedYear_rental);
						//end kiraan total opening atau B/F
						
						$totalOpening = $totalBF;
						if ($totalOpening > 0)
						{$totalTunggakan = $totalOpening;}
						else if ($totalOpening <= 0)
						{$totalTunggakan = 0.00;}
						
						$bigTotalOpening += $totalOpening;
						$bigTotalTunggakan += $totalTunggakan;
						$bigTotalBillAmount += $bill_amount;
						
						$sqlTelahBayarBulanSebelum = "select sum(payments.amount_paid) as payment_bulanSebelum 
														from payments inner join bills on payments.bill_id = bills.id 
														where bills.tenant_id = $tenant_id and bills.bill_type_id = $bill_types_id and bills.issue_date = '$dateFrom' and payments.created_at < '$dateFrom'";
						$runTelahBayarBulanSebelum = mysqli_query($link, $sqlTelahBayarBulanSebelum);
						if($runTelahBayarBulanSebelum === FALSE){
							die(mysqli_error($link));
						}
						$resultTelahBayarBulanSebelum = mysqli_fetch_assoc($runTelahBayarBulanSebelum);
						$payment_bulanSebelum = $resultTelahBayarBulanSebelum["payment_bulanSebelum"];
						$total_bayaranBulanSebelum += $payment_bulanSebelum;
						
						
						//QUERY UNTUK KELUARKAN PAYMENT 
						  
						  $sqlTenantPayment = "select sum(payments.amount_paid) as amount_paid, bills.issue_date, payments.created_at, bills.bill_type_id from payments 
										inner join bills on payments.bill_id = bills.id 
										where bills.tenant_id = $tenant_id and ($sqlDate) and bills.bill_type_id = $bill_types_id and bills.issue_date <= '$dateTo' 
										group by bills.issue_date
										";
						$runTenantPayment = mysqli_query($link, $sqlTenantPayment);
						if($runTenantPayment === FALSE){
							die(mysqli_error($link));
						} 
						//$payment_advance = 0.00;
						$totalAdvanceFor = array();
						while($resultTenantPayment= mysqli_fetch_array($runTenantPayment)){
							$bill_types_id  = $resultTenantPayment["bill_type_id"];
							$created_at  = $resultTenantPayment["created_at"];
							$issue_date  = $resultTenantPayment["issue_date"];
							$amount_paid  = $resultTenantPayment["amount_paid"];
							$total_tunggakanPHSSB = 0.00;
							//}
							if($bill_types_id == 1 || $bill_types_id == 2){ //rental and water
								//compare created_at dengan issue_date untuk letak kedudukan amount_paid dalam column yg mana
								//1. column bayaran semasa
								$monthYear_created_at = date("Y-m", strtotime($created_at));
								$monthYear_issue_date = date("Y-m", strtotime($issue_date));
								//$monthYear_dateFrom = date("Y-m",strtotime($dateFrom));
								$monthYear_dateFrom = date("Y-m",strtotime($created_at));
								//2. column bayaran tunggakan tahun sebelum 
								$year_created_at = date("Y", strtotime($created_at));
								$year_issue_date = date("Y", strtotime($issue_date));
								//$year_dateFrom = date("Y",strtotime($dateFrom));
								$year_dateFrom = date("Y",strtotime($created_at));
								
								
								if($monthYear_created_at == $monthYear_issue_date){ //semasa
									$payment_semasa = $amount_paid;
									$total_bayaranBulanSemasa += $amount_paid;
								}
								else if($monthYear_dateFrom > $monthYear_issue_date){ //tunggakan
									if($year_dateFrom > $year_issue_date){ //tunggakan tahun sebelum
										$payment_TahunSebelum += $amount_paid;
										$total_bayaranTahunSebelum += $amount_paid;
									}
									else if($year_dateFrom == $year_issue_date){ //tunggakan tahun semasa
										$payment_TahunSemasa += $amount_paid;
										$total_bayaranTahunSemasa += $amount_paid;
									}
								}
							}
							else{ //tunggakan selain phssb
								$payment_tunggakanBukanPHSSB = $amount_paid;
								$total_bayaranTunggakanNotPHSSB += $amount_paid;
								//$total_bayaranTunggakanBukanPHSSB = 0.00;
							}
						}
						//query advance
							 " :: ADVANCE QUERY :: " .$sqlListPaymentAdv = "select YEAR(bills.issue_date) as yearAdv, sum(payments.amount_paid) as totalAmountPaidAdv, bills.issue_date from payments 
														inner join bills on payments.bill_id = bills.id 
														where bills.bill_type_id = ".$_SESSION["bill_types_id"]." and bills.issue_date >= '$dateTo' 
														and bills.tenant_id = $tenant_id
														and (payments.created_at >= '$dateFrom' and payments.created_at < '$date_cutOFF' )
														group by YEAR(bills.issue_date) ";
								$tdAdv_cells = "";
								
								$runListPaymentAdv = mysqli_query($link, $sqlListPaymentAdv);
								if($runListPaymentAdv === FALSE){
									die(mysqli_error($link));
								}
								$rowListPaymentAdv = mysqli_num_rows($runListPaymentAdv);
								if($rowListPaymentAdv == 0){
									for($i=0;$i<count($listYear);$i++){
										$tdAdv_cells .=  "<td> &nbsp</td>";
									}
									
								}
								else if($rowListPaymentAdv > 0){
									$tdAdv_cells = "";
									 " [countListAdvance $tenant_id] :: " .$countListAdvance = mysqli_num_rows($runListPaymentAdv);
									 " [listPayments $tenant_id] :: " .count($listYear);
									$listADV_yearAndPayment = array();
									$i = 0;
									while($resultListPaymentAdv = mysqli_fetch_array($runListPaymentAdv)){
										$yearAdv = $resultListPaymentAdv["yearAdv"];
										$yearList = $listYear[$i];
										if($yearList < $yearAdv){
											$totalAmountPaidAdv = "&nbsp;";
											$listADV_yearAndPayment[] = $yearList . "," . $totalAmountPaidAdv;
											$yearList = $yearAdv;
											$i++;
										}
										if($yearList == $yearAdv){
											$totalAmountPaidAdv = $resultListPaymentAdv["totalAmountPaidAdv"];
											$listADV_yearAndPayment[] = $yearList . "," . $totalAmountPaidAdv;
											$i++;
										}
										if($yearAdv > $yearList){
											$yearDiff = $yearAdv - $yearList;
											for($y=0;$y<$yeardiff;$y++){
												$totalAmountPaidAdv = "&nbsp;";
												$listADV_yearAndPayment[] = $listYear[$y] . "," . $totalAmountPaidAdv;
												$i++;
											}
											
										}
									}
									$listDiff = $loop - $i;
									for($y=$i;$y<$loop;$y++){
										$totalAmountPaidAdv = "&nbsp;";
										$listADV_yearAndPayment[] = $listYear[$y] . "," . $totalAmountPaidAdv;
									}
									foreach($listADV_yearAndPayment as $value){
										//[$yearADV_Set, $totalADV_Set] = explode(',', $value);
										list($yearADV_Set, $totalADV_Set) = explode(',', $value);
										$totalADV_Set = (float)($totalADV_Set);
										$payment_advance += $totalADV_Set;
										$tdAdv_cells .=  "<td align='right'>".number_format($totalADV_Set,2,'.',',')."</td>";
										//$payment_advance += $totalADV_Set;
									}
								}
					
					//kiraan 30%
				
					?>
					<tbody>
					<tr>
						<td><?php echo $no?></td>
						<td><?php echo $unit_no?></td>
						<td rowspan="<?php echo $rowspanByPayment?>"><a href="viewTenantDashboard.php?tenant_id=<?php echo $tenant_id?>"><?php echo $tenant_name?></a></td>
						<td align="right"><?php echo number_format($bill_amount,2,".",",")?></td>
						<td align="right"><?php echo number_format($totalOpening,2,".",",")?></td>
						<td align="right"><?php echo number_format($totalTunggakan,2,".",",")?></td>
						<td align="right"><?php echo number_format($payment_bulanSebelum,2,".",",")?></td>
						<td ><?php echo ($payment_semasa != 0.00 ? date("d/m/Y",strtotime($created_at)) : '&nbsp;'); ?></td>
						<td align="right"><?php echo number_format($payment_semasa,2,".",",")?></td>
						<?PHP 
						if(strtoupper($_SESSION["project_name"]) == "SKIM SMART SEWA"){
							
							?>
							<td align="center"><b>30% </b></td>
							<?PHP
						}
						?>
						<td align="right"><?php echo number_format($payment_TahunSebelum,2,".",",")?></td>
						<td align="right"><?php echo number_format($payment_TahunSemasa,2,".",",")?></td>
						<?php if($bill_type_Outstanding != 0){?>
						<td align="center"><b> <?php echo  number_format($payment_tunggakanBukanPHSSB,2,".",",") ?> </b></td>
						<?php } 
						
						//kiraan closing
						$total_terimaan = $payment_bulanSebelum + $payment_semasa;
						$total_tunggakanPHSSB = $payment_TahunSebelum + $payment_TahunSemasa; 
						$closing = $bill_amount + $totalOpening - $payment_semasa - $total_tunggakanPHSSB - $payment_advance;
						$totalTunggakan_end = $closing + $payment_advance;
						if ($totalTunggakan_end <= 0)
						{$totalTunggakan_end = 0.00;}
						?>
						<?php echo $tdAdv_cells?>
						<!--<td align="right"><?php echo "advance" . number_format($payment_advance,2,".",",")?></td>
						<td align="right"><?php echo "advance" . number_format($payment_advance,2,".",",")?></td>
						<td align="right"><?php echo "advance" . number_format($payment_advance,2,".",",")?></td>-->
						<td align="right"><?php echo number_format($closing,2,".",",")?></td>
						<td align="right"><?php echo number_format($totalTunggakan_end,2,".",",")?></td>
					</tr>
					<?php
						$bigTotalClosing += $closing;
						$bigTotalTunggakan_end += $totalTunggakan_end;
						$no++;
					}
				}
				
				$total_semua = $total_bayaranBulanSemasa + $total_bayaranTahunSebelum + $total_bayaranTahunSemasa + $total_bayaranTunggakanNotPHSSB  ;
				
				?>
				</tbody>
				<tr>
					<td colspan="3">JUMLAH (RM)</td>
					<td align="right"><?php echo number_format($bigTotalBillAmount,2,".",",")?></td>
					<td align="right"><?php echo number_format($bigTotalOpening,2,".",",")?></td>
					<td align="right"><?php echo number_format($bigTotalTunggakan,2,".",",")?></td>
					<td align="right"><?php echo number_format($total_bayaranBulanSebelum,2,".",",")?></td>
					<td colspan="2" align="right"><?php echo number_format($total_bayaranBulanSemasa,2,".",",")?></td>
					<td align="right"><?php echo number_format($total_bayaranTahunSebelum,2,".",",") ?></td>
					<td align="right"><?php echo number_format($total_bayaranTahunSemasa,2,".",",")?></td>
					<?php if($bill_type_Outstanding != 0){?>
					<td align="right" ><?php echo number_format($total_bayaranTunggakanNotPHSSB,2,".",",")?></td>
					<?php } 
					
					
					  " ADV PAYMENT SQL: " .$sqlTotalPaymentAdv = "select YEAR(bills.issue_date) as yearAdv, sum(payments.amount_paid) as totalAmountPaidAdv, count(payments.id) 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
													inner join projects on locations.project_id = projects.id
													where bills.bill_type_id = ".$_SESSION["bill_types_id"]." 
													and locations.id = ". $_SESSION['location_id'] ."  
													and bills.issue_date > '$dateTo' 
													and (payments.created_at >= '$dateFrom' and payments.created_at < '$date_cutOFF' ) $sqlTenantStatus $sqlListBlock
													group by YEAR(bills.issue_date) order by YEAR(bills.issue_date) asc";
						//$runTotalPaymentAdv = ;
						$runTotalPaymentAdv = mysqli_query($link, $sqlTotalPaymentAdv);
						if($runTotalPaymentAdv === FALSE){
							die(mysqli_error($link));
						}
						
						$rowTotalPaymentAdv = mysqli_num_rows($runTotalPaymentAdv);
						if($rowTotalPaymentAdv == 0){
							for($i=0;$i<count($listYear);$i++){
								$tdTotalAdv_cells .=  "<td> &nbsp</td>";
							}
							
						}
						else if($rowTotalPaymentAdv > 0){
							$tdTotalAdv_cells = "";
							 " [countListAdvance $tenant_id] :: " .$countTotalAdvance = mysqli_num_rows($runTotalPaymentAdv);
							 " [listPayments $tenant_id] :: " .count($listYear);
							$totalADV_yearAndPayment = array();
							$i = 0;
							while($resultTotalPaymentAdv = mysqli_fetch_array($runTotalPaymentAdv)){
								$yearAdv = $resultTotalPaymentAdv["yearAdv"];
								$yearTotal = $listYear[$i];
								if($yearTotal < $yearAdv){
									$totalAmountPaidAdv = "&nbsp;";
									$totalADV_yearAndPayment[] = $yearTotal . "," . $totalAmountPaidAdv;
									$yearTotal = $yearAdv;
									$i++;
								}
								if($yearTotal == $yearAdv){
									$totalAmountPaidAdv = $resultTotalPaymentAdv["totalAmountPaidAdv"];
									$totalADV_yearAndPayment[] = $yearTotal . "," . $totalAmountPaidAdv;
									$i++;
									$bigTotalAdvance += $totalAmountPaidAdv;
								}
								if($yearAdv > $yearTotal){
									$yearDiff = $yearAdv - $yearTotal;
									for($y=0;$y<$yeardiff;$y++){
										$totalAmountPaidAdv = "&nbsp;";
										$totalADV_yearAndPayment[] = $listYear[$y] . "," . $totalAmountPaidAdv;
										$i++;
									}
									
								}
							}
							//$listDiff = $loop - $i;
							for($y=$i;$y<$loop;$y++){
								$totalAmountPaidAdv = "&nbsp;";
								$totalADV_yearAndPayment[] = $listYear[$y] . "," . $totalAmountPaidAdv;
							}
							foreach($totalADV_yearAndPayment as $value){
								//[$yearADV_Set, $totalADV_Set] = explode(',', $value);
								list($yearADV_Set, $totalADV_Set) = explode(',', $value);
								$tdTotalAdv_cells .=  "<td align='right'>".number_format($totalADV_Set,2,'.',',')."</td>";
								
							}
						}
					echo $tdTotalAdv_cells;
					//$totalTerimaan = $total_bayaranBulanSebelum + $total_bayaranBulanSemasa + $total_bayaranTahunSebelum + $total_bayaranTahunSemasa + $total_bayaranTunggakanNotPHSSB;
					$totalTerimaan =  $total_bayaranBulanSemasa + $total_bayaranTahunSebelum + $total_bayaranTahunSemasa + $total_bayaranTunggakanNotPHSSB + $bigTotalAdvance;
					?>
					
					<td align="right"><?php echo number_format($bigTotalClosing,2,".",",")?></td>
					<td align="right"><?php echo number_format($bigTotalTunggakan_end,2,".",",")?></td>
				</tr>
				<tr>
					<td colspan="7">JUMLAH BAYARAN  <?PHP echo strtoupper($bulanTerimaan)?> (RM)</td>
					<td align="center" colspan="<?php echo $colspan_terimaan - 1 ?>"><?php echo number_format($totalTerimaan,2,".",",")?></td>
					<td></td>
				</tr>
			</table>
				<?php
			}
		}
			else if($statementby == 'statement_monthyear'){?>
			<table class="table table-bordered table-hover table-sm" id="dataTable" width="70%" cellspacing="0" style="font-size: 12px">
				<thead>
				<tr>
					<td colspan="11"><B><?php echo "MAKLUMAT BAYARAN SEWA : " .$payment_monthYear?> <span style="float:right"><?php echo "LOKASI: " . $_SESSION["projectLocation"] ?></span></B></td>
				</tr>
				<tr>
					<td valign="top" align="center" ><b>#</b></td>
					<td valign="top" align="center" ><b>NAMA</b></td>
					<td valign="top" align="center" ><b>UNIT</b></td>
					<td valign="top" align="center" ><b>JUMLAH PATUT KUTIP (RM)</b></td>
					<td valign="top" align="center" ><b>TARIKH BAYARAN</b></td>
					<td valign="top" align="center" ><b>JENIS BAYARAN</b></td>
					<td valign="top" align="center" ><b>REF NO. / RESIT BANK</b></td>
					<td valign="top" align="center" ><b>JUMLAH BAYARAN</b></td>
					<td valign="top" align="center" ><b>KETERANGAN BAYARAN</b></td>
					<td valign="top" align="center" ><b>STATUS BAYARAN</b></td>
					<td valign="top" align="center" ><b>INVOICE NO</b></td>
				</tr>
				</thead>
				<?php 
				$no = 1;
				$total = 0;
				$totalOustanding = 0;
				$jumlah_patutKutip = 0;
				$jumlah_telahKutip = 0;
				$percentKutipan = 0;
				$keterangan = "";
				while($resultRental = mysqli_fetch_array($runReport)){ 
					$count_payment = $resultRental["count_payment"];
					$rowspanByPayment = 1;
					if($count_payment > 1)
						$rowspanByPayment = $count_payment;
					
					$tenant_id = $resultRental["tenant_id"];
					$tenant_name = $resultRental["tenant_name"];
					$unit_no = $resultRental["unit_no"];
					$bill_id = $resultRental["bill_id"];
					$bill_amount = $resultRental["bill_amount"];
					?>
					<tr>
						<td rowspan="<?php echo $rowspanByPayment?>"><?php echo $no?></td>
						<td rowspan="<?php echo $rowspanByPayment?>"><a href="viewTenantDashboard.php?tenant_id=<?php echo $tenant_id?>"><?php echo $tenant_name?></a></td>
						<td rowspan="<?php echo $rowspanByPayment?>"><?php echo $unit_no?></td>
						
					<?php
					
					if($report_type == 'Rental'){
						// "keterangan :" . $keterangan = " BAYARAN SEWA ";
						?>
						<td rowspan="<?php echo $rowspanByPayment?>" align="right"><?php echo $bill_amount?></td>
					<?php 
					    $sqlPaymentsDetailsByTenantID = "select payments.created_at, payments.receipt_no, payments.amount_paid, payments.bill_id, bills.issue_date, bills.payment_status from payments 
														inner join bills on payments.bill_id = bills.id
														where bills.tenant_id = $tenant_id and payments.bill_id = $bill_id
														";
					}
					
					else if($report_type == 'Water'){
						//$keterangan = " BAYARAN BILL AIR ";
						$sqlPaymentsDetailsByTenantID = "select payments.created_at, payments.receipt_no, payments.amount_paid, payments.bill_id,bills.bill_amount, bills.issue_date, bills.payment_status from payments 
														inner join bills on payments.bill_id = bills.id
														where bills.tenant_id = $tenant_id and $sqlDate and bills.bill_type_id = 2
														";
					}
					$runPaymentsDetailsByTenantID = mysqli_query($link, $sqlPaymentsDetailsByTenantID);
					if($runPaymentsDetailsByTenantID === FALSE){
						die(mysqli_error($link));
					} 
					$i=1;
					if($report_type != "Deposit"){ //report type rental and warter
						while($resultPaymentsDetailsByTenantID = mysqli_fetch_array($runPaymentsDetailsByTenantID)){
							
							$created_at = $resultPaymentsDetailsByTenantID["created_at"];
							$receipt_no = $resultPaymentsDetailsByTenantID["receipt_no"];
							$amount_paid = $resultPaymentsDetailsByTenantID["amount_paid"];
							$bill_id = $resultPaymentsDetailsByTenantID["bill_id"];
							$issue_date = $resultPaymentsDetailsByTenantID["issue_date"];
							$payment_status = $resultPaymentsDetailsByTenantID["payment_status"];
							
							//if($reportTypeShow == 'Water'){
							if($report_type == 'Water'){
								$bill_amount = $resultPaymentsDetailsByTenantID["bill_amount"];
								$jumlah_patutKutip += $bill_amount;
								?>
							<td align="right"><?php echo $bill_amount?></td>
							<?php } ?>
							<td><?php echo date("d/m/Y",strtotime($created_at))?></td>
							<td>-</td>
							<td>-</td>
							<td align="right"><?php echo number_format($amount_paid,2,".",","); ?></td>
							<td><?php echo $keterangan . " " . date("M Y", strtotime($issue_date)); ?></td>
							<td><?php echo $payment_status; ?></td>
							<td><?php echo $bill_id; ?></td>
							</tr>
							<?php
							
							$jumlah_telahKutip += $amount_paid;
						}
					}
					//if($reportTypeShow == 'Rental'){
					if($report_type == 'Rental'){
						$jumlah_patutKutip += $bill_amount;
					}
					$no++;
					?>
					</tr>
					<?php
				}
					$percentKutipan = ($jumlah_telahKutip/$jumlah_patutKutip) * 100;
					?>
					<tr>
						<td colspan="3"><b>JUMLAH PATUT KUTIP (RM)</b></td>
						<td align="right"><b><?php echo number_format($jumlah_patutKutip,2,'.',',')?></b></td>
						<td colspan="3" ><b>JUMLAH TELAH KUTIP (RM)</b></td>
						<td align="right"><b><?php echo number_format($jumlah_telahKutip,2,'.',',')?></b></td>
						<td colspan="3" align="right"><b>PERCENT KUTIPAN : <?PHP echo number_format($percentKutipan,2)?>%</b></td>
					</tr>
			</table>
			<?php
			}
			
			
			else if($statementby == 'statement_year' ){
				$listYear =array();
				$rmLoop = 6; 
				$dateTo = $_SESSION['statement_year'] . "-12-31";
				$dateTo = date("Y-m-d",strtotime($dateTo));
				$colReportYear = 21;
				
				
				$sqlCountYearAdv = "select YEAR(bills.issue_date) as yearADV, sum(payments.amount_paid) 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
												inner join projects on locations.project_id = projects.id
												where  bills.bill_type_id in ($bill_types_id) and 
												locations.id = ". $_SESSION['location_id'] ." 
												$sqlDateADV $sqlDatePayments group by YEAR(bills.issue_date)
												";
				$runCountYearAdv = mysqli_query($link, $sqlCountYearAdv);
				if($runCountYearAdv === FALSE){
					die(mysqli_error($link));
				}
				$colAdv = mysqli_num_rows($runCountYearAdv);
				$colReportYear = 21 + $colAdv;
				?>
			<table class="table table-bordered table-hover table-sm" id="dataTable" width="70%" cellspacing="0" style="font-size: 9px">
				<thead>
					<tr>
						<td colspan="<?php echo $colReportYear?>"><B><?php echo "MAKLUMAT BAYARAN SEWA : " .$statement_year?> <span style="float:right"><?php echo "LOKASI: " . $_SESSION["projectLocation"] ?></span></B></td>
					</tr>
					<tr>
						<td rowspan="3" valign="top" align="center" ><b>#</b></td>
						<td rowspan="3"valign="top" align="center" ><b>UNIT</b></td>
						<td rowspan="3"valign="top" align="center" ><b>NAMA</b></td>
						<td rowspan="2"valign="top" align="center" ><b>KADAR SEWA </b></td>
						<td rowspan="2"valign="top" align="center" ><b>TUNGGAKAN PHSSB </b></td>
						<?php if($bill_type_name != ""){ $rmLoop++;?>
						<td rowspan="2"valign="top" align="center" ><b>TUNGGAKAN (<?PHP echo $bill_type_name; ?>)</b></td>
						<?php
						}
						?>
						<td colspan = "12" valign="top" align="center"><b><?php echo $statement_year?></b></td>
						<td rowspan="2" valign="top" align="center" ><b>JUMLAH BAYARAN</b></td>
						<td rowspan="2" valign="top" align="center" ><b> BAYARAN TUNGGAKAN PHSSB</b></td>
						<td rowspan="2" valign="top" align="center" ><b> BAKI TUNGGAKAN PHSSB</b></td>
						<?php if($bill_type_name != "") {$rmLoop += 2; ?>
						<td rowspan="2" valign="top" align="center" ><b>BAYARAN TUNGGAKAN (<?PHP echo $bill_type_name; ?>) </b></td>
						<td rowspan="2" valign="top" align="center" ><b>BAKI TUNGGAKAN (<?PHP echo $bill_type_name; ?>) </b></td>
						<?php }
						if($_SESSION["statement_year"] != ""){
							  
							if($colAdv > 0){ 
						?>
						<td valign="top"  colspan="<?php echo $colAdv?>"align="center" ><b>BAYARAN ADVANCE </b></td>
						
						<?php }
						}	?>
						<td rowspan="2" valign="top" align="center" ><b>JUMLAH KESELURUHAN BAYARAN TAHUN <?php echo $statement_year?></b></td>
					</tr>
					<tr>
						<?php 
						$listMonth_fulldate = array();
							for($i = 1; $i<=12; $i++){
								$rmLoop++;
								$monthNum = $i;
								$monthName = date("M", mktime(0, 0, 0, $monthNum, 10));
								$monthInNumber = date("m", mktime(0, 0, 0, $monthNum, 10));
								$yearName = $_SESSION['statement_year'];
								$listMonthYearFull = $_SESSION['statement_year'] . "-" . $monthInNumber . "-" ."01";
								$listMonthYearFull = date("Y-m-d",strtotime($listMonthYearFull));
								$listMonth_fulldate[] = $listMonthYearFull;
								?>
								<td valign="top" align="center" ><b><?php echo STRTOUPPER($monthName) ?></b></td>
								<?php
							}
							
						?>
						<?php $loop = 0;
							while($resultCountYearADV = mysqli_fetch_array($runCountYearAdv)){
								$yearADV = $resultCountYearADV["yearADV"];
								$listYear[] = $yearADV;
								?>
								<td><b><?php echo $yearADV?></b></td>
								<?php
								$rmLoop++;
								$loop++;
							}
						?>
					</tr>
					<TR>
					<?php 
					for($i = 1; $i<=$rmLoop; $i++){
					?>
						<td align="center"><b>(RM)</b></td>
					<?php } ?>
					</TR>
				</thead>
				<tbody>
				<?php 
				
				$no = 0;
				$total = 0;
				$totalOustanding = 0;
				$bigTotal_kadarsewa = 0.00;
				$rentalRateArray = array();
				$rentalRateList = "";
				while($resultRental = mysqli_fetch_array($runReport)){ 
					//$tenant_name = $resultRental["tenant_name"];
					$unit_no = $resultRental["unit_no"];
					$property_id = $resultRental["property_id"];
					$countTenant = $resultRental["countTenant"];
					
					$sqlTenant = "select tenants.id as tenant_id, tenants.tenant_name, tenants.rental_rate from tenants 
											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 properties.id = $property_id $sqlTenantStatus  $sqlListBlock
											";
					$runTenant = mysqli_query($link, $sqlTenant);
					if($runTenant === FALSE){
						die(mysqli_error($link));
					}
					
					$rental_rate = 0.00;
					
					while($resultTenant = mysqli_fetch_array($runTenant)){
						$no++;
						$tdMonth_cells = "";
						$tenant_id = $resultTenant["tenant_id"];
						$tenant_name = $resultTenant["tenant_name"];
						$rental_rate = $resultTenant["rental_rate"];
						$rentalRateArray[] = $rental_rate;
						$rentalRateList .= $rental_rate . ",";
						
						$bigTotal_kadarsewa += $rental_rate;
						
						
						$amountDueOutstandingNotPHSSB = 0.00;
						$amount_paidBills = "";
						//echo  " sqlOutstandingNotPHSSB:: ".$sqlOutstandingNotPHSSB = "select sum(amount_due) as totalOutstandingNotPHSSB from bills where tenant_id = $tenant_id and bill_type_id = $bill_type_Outstanding ";
						   " sqlOutstandingNotPHSSB:: ".$sqlOutstandingNotPHSSB = "select amount_due as totalOutstandingNotPHSSB from bills where tenant_id = $tenant_id and bill_type_id = $bill_type_Outstanding ";
						$runOutstandingNotPHSSB = mysqli_query($link, $sqlOutstandingNotPHSSB);
						if($runOutstandingNotPHSSB === FALSE){
							die(mysqli_error($link));
						}
						$countOutstandingNotPHSSB = mysqli_num_rows($runOutstandingNotPHSSB);
						if($countOutstandingNotPHSSB > 0){
							WHILE($resultOustandingNotPHSSB = mysqli_fetch_array($runOutstandingNotPHSSB)){
								$amountDueOutstandingNotPHSSB += $resultOustandingNotPHSSB["totalOutstandingNotPHSSB"];
							}
						}
						$jumlahByYear = 0.00;
						
						//start kiraan total opening atau B/F
						$totalOpening = 0.00;
						$totalTunggakanPHSSB = 0.00;
						 "\n sqlTotalBillAmountBeforeSelectedYear_rental $tenant_id ::". $sqlTotalBillAmountBeforeSelectedYear_rental = "SELECT sum(bill_amount) as totalBillAmountBeforeSelectedYear FROM `bills` where tenant_id = $tenant_id and bills.bill_type_id = $bill_types_id and bills.issue_date < '$firstYearDate'";
						 "\n sqlTotalAmountPaidBeforeSelectedYear_rental $tenant_id ::". $sqlTotalAmountPaidBeforeSelectedYear_rental = "SELECT sum(amount_paid) as totalAmountPaidBeforeSelectedYear FROM `payments` inner join bills on payments.bill_id = bills.id 
																		where bills.tenant_id = '$tenant_id' and bills.bill_type_id = $bill_types_id and payments.created_at < '$firstYearDate' ";
						$runTotalBillAmountBeforeSelectedYear_rental = mysqli_query($link, $sqlTotalBillAmountBeforeSelectedYear_rental);
						if($runTotalBillAmountBeforeSelectedYear_rental === FALSE){
						die(mysqli_error($link));
						}
						$runTotalAmountPaidBeforeSelectedYear_rental = mysqli_query($link, $sqlTotalAmountPaidBeforeSelectedYear_rental);
						if($runTotalAmountPaidBeforeSelectedYear_rental === FALSE){
						die(mysqli_error($link));
						}

						$resultTotalBillAmountBeforeSelectedYear_rental = mysqli_fetch_assoc($runTotalBillAmountBeforeSelectedYear_rental);
						$resultTotalAmountPaidBeforeSelectedYear_rental = mysqli_fetch_assoc($runTotalAmountPaidBeforeSelectedYear_rental);

						$totalBillAmountBeforeSelectedYear_rental = $resultTotalBillAmountBeforeSelectedYear_rental["totalBillAmountBeforeSelectedYear"];
						$totalAmountPaidBeforeSelectedYear_rental = $resultTotalAmountPaidBeforeSelectedYear_rental["totalAmountPaidBeforeSelectedYear"];
						$totalBF = abs($totalBillAmountBeforeSelectedYear_rental) - abs($totalAmountPaidBeforeSelectedYear_rental);
						//end kiraan total opening atau B/F
						
						$totalOpening = $totalBF;
						if ($totalOpening > 0)
						{$totalTunggakanPHSSB = $totalOpening;}
						else if ($totalOpening <= 0)
						{$totalTunggakanPHSSB = 0.00;}
					
				?>
				
				<tr>
					<td><?php echo $no; ?></td>
					<td><?php echo $unit_no; ?></td>	
					<td><a href="viewTenantDashboard.php?tenant_id=<?php echo $tenant_id?>"><?php echo strtoupper($tenant_name); ?></a></td>
					<td ALIGN='RIGHT'><?php echo number_format($rental_rate,2); ?></td>
					<td ALIGN='RIGHT'><?php echo number_format($totalTunggakanPHSSB,2); ?></td>
					<?PHP if($bill_type_name != ""){?>
					<td ALIGN='RIGHT'><?php echo number_format($amountDueOutstandingNotPHSSB,2); ?></td>
					<?PHP } ?>
					<?php 
					 	     "\n sqlPaymentsBills >> " . $sqlPaymentsBills = "select sum(payments.amount_paid) as amount_paid, bills.issue_date from payments inner join bills on payments.bill_id = bills.id 
																				where bills.tenant_id = $tenant_id and YEAR(bills.issue_date) = $statement_year and bills.bill_type_id  in ($bill_types_id) group by bills.issue_date order by bills.issue_date ASC";
						$runPaymentsBills = mysqli_query($link, $sqlPaymentsBills);
						if($runPaymentsBills === FALSE){
							die(mysqli_error($link));
						}
						$listPayments = "";
						$monthCount = 1;
						$totalPayment_issuedate = 0.00;
						
						$rowPaymentsBills = mysqli_num_rows($runPaymentsBills);
						if($rowPaymentsBills == 0){
							for($i=0;$i<count($listMonth_fulldate);$i++){
								$tdMonth_cells .=  "<td ALIGN='CENTER'> -</td>";
							}
							
						}
						else if($rowPaymentsBills > 0){
							
							   "\n [countListAdvance $tenant_id] :: " .$countTotalAdvance = mysqli_num_rows($runPaymentsBills);
							   "\n [listPayments $tenant_id] :: " .count($listMonth_fulldate);
							$listing_monthAndPayment = array();
							$i = 0;
							$monthYear_Set = "";
							while($resultTotalPaymentAdv = mysqli_fetch_array($runPaymentsBills)){
								 "\nmonthYear_issue_date $tenant_id:  " .$monthYear_issue_date = date("Y-m",strtotime($resultTotalPaymentAdv["issue_date"]));
								 "\nmonthYear_listing $tenant_id: " . $monthYear_listing = date("Y-m", strtotime($listMonth_fulldate[$i]));
								
									if($monthYear_listing < $monthYear_issue_date){
										//$amount_paid = "-";
										//$listing_monthAndPayment[] = $monthYear_listing . "," . $amount_paid;
										$num_monthYear_listing = date('m', strtotime($monthYear_listing));
										$num_monthYear_issue_date = date('m', strtotime($monthYear_issue_date));
										 "\n MONTH DIFF $tenant_id: ".$month_diff = abs($num_monthYear_listing  - $num_monthYear_issue_date);
										 "\n i $tenant_id = $i";
										for($y=0;$y<$month_diff;$y++){
											//echo "\n FOR < $tenant_id";
											$amount_paid = "-";
											$listing_monthAndPayment[] = $listMonth_fulldate[$y] . "," . $amount_paid;
											$i++;
										}
										$monthYear_listing = $monthYear_issue_date;
										//$i++;
									}
									if($monthYear_listing == $monthYear_issue_date){
										$amount_paid = $resultTotalPaymentAdv["amount_paid"];
										$listing_monthAndPayment[] = $monthYear_listing . "," . $amount_paid;
										$totalPayment_issuedate += $amount_paid;
										$i++;
									}
									if($monthYear_listing > $monthYear_issue_date){
										$num_monthYear_listing = date('m', strtotime($monthYear_listing));
										$num_monthYear_issue_date = date('m', strtotime($monthYear_issue_date));
										$month_diff = $num_monthYear_listing  - $num_monthYear_issue_date;
										
										for($y=$i;$y<$month_diff;$y++){
											$amount_paid = "-";
											$listing_monthAndPayment[] = $listMonth_fulldate[$y] . "," . $amount_paid;
											$i++;
										}
										
									}
								//}
							}
							
							for($y=$i;$y<count($listMonth_fulldate);$y++){
								$amount_paid = "-";
								$listing_monthAndPayment[] = $listMonth_fulldate[$y] . "," . $amount_paid;
							}
							 "TENANT ID BEFORE PRINT_R: $tenant_id ====";
								//print_r($listing_monthAndPayment);
								 "TENANT ID AFTER PRINT_R: $tenant_id ====";
							foreach($listing_monthAndPayment as $value){
								
								//[$monthYear_Set, $amountPaid_Set] = explode(',', $value);
								list($monthYear_Set, $amountPaid_Set) = explode(',', $value);
								if(is_numeric($amountPaid_Set)){
									$amountPaid_Set = number_format($amountPaid_Set,2,'.',',');
									$tdMonth_cells .=  "<td align='right'>".$amountPaid_Set."</td>";
								}
								else{
									$tdMonth_cells .=  "<td align='center'>".$amountPaid_Set."</td>";
								}
								
							}
						}
					echo $tdMonth_cells;
					?>
					<td ALIGN='RIGHT'><?php echo number_format($totalPayment_issuedate,2); ?></td>
					<?php 
					 "\n sqlTotalBayaranTunggakanPHSSB $tenant_id :: " . $sqlTotalBayaranTunggakanPHSSB = "SELECT sum(amount_paid) as totalBayaranTunggakanPHSSB FROM `payments` inner join bills on payments.bill_id = bills.id 
													where bills.tenant_id = '$tenant_id' and bills.bill_type_id = $bill_types_id and payments.created_at >= '$firstYearDate' and  payments.created_at < '$cutOffDate' and bills.issue_date < '$firstYearDate'";
					$runTotalBayaranTunggakanPHSSB = mysqli_query($link, $sqlTotalBayaranTunggakanPHSSB);
					if($runTotalBayaranTunggakanPHSSB === FALSE){
						die(mysqli_error($link));
					}
					$resultTotalBayaranTunggakanPHSSB = mysqli_fetch_assoc($runTotalBayaranTunggakanPHSSB);
					$totalBayaranTunggakanPHSSB = $resultTotalBayaranTunggakanPHSSB["totalBayaranTunggakanPHSSB"];
					$bakiTunggakanPHSSB = $totalTunggakanPHSSB - $totalBayaranTunggakanPHSSB;
					
					?>
					<td ALIGN='RIGHT'><?php echo number_format($totalBayaranTunggakanPHSSB,2); ?></td>
					<td ALIGN='RIGHT'><?php echo number_format($bakiTunggakanPHSSB,2); ?></td>
					<?php if($bill_type_name != "") { 
						$sqltotalPaymentOutstandingNotPHSSB = "select sum(amount_paid) as totalPaymentOutstandingNotPHSSB from payments inner join bills on payments.bill_id = bills.id where bills.tenant_id = $tenant_id and bills.bill_type_id = $bill_type_Outstanding ";
						$runtotalPaymentOutstandingNotPHSSB = mysqli_query($link, $sqltotalPaymentOutstandingNotPHSSB);
						if($runtotalPaymentOutstandingNotPHSSB === FALSE){
							die(mysqli_error($link));
						}
						$countOutstandingNotPHSSB = mysqli_num_rows($runtotalPaymentOutstandingNotPHSSB);
						if($countOutstandingNotPHSSB > 0){
							$resultTotalOustandingNotPHSSB = mysqli_fetch_assoc($runtotalPaymentOutstandingNotPHSSB);
							$totalPaymentOutstandingNotPHSSB = $resultTotalOustandingNotPHSSB["totalPaymentOutstandingNotPHSSB"];
						}
						$bakiTunggakanNotPHSSB = $amountDueOutstandingNotPHSSB - $totalPaymentOutstandingNotPHSSB;
					?>
					<td ALIGN='RIGHT'><?php echo number_format($totalPaymentOutstandingNotPHSSB,2); ?></td>
					<td ALIGN='RIGHT'><?php echo number_format($bakiTunggakanNotPHSSB,2); ?></td>
					<?php } ?>
					<!--COLUMN ADVANCE START - STATEMENT YEAR-->
					<?PHP
					//query advance
				//	ECHO $sqlDatePayments;
						  " :: ADVANCE QUERY $tenant_id :: " . $sqlListPaymentAdv = "select YEAR(bills.issue_date) as yearAdv, sum(payments.amount_paid) as totalAmountPaidAdv, bills.issue_date from payments 
													inner join bills on payments.bill_id = bills.id 
													where bills.bill_type_id = ".$_SESSION["bill_types_id"]." and bills.issue_date >= '$dateTo' 
													and bills.tenant_id = $tenant_id
													$sqlDatePayments
													$sqlDateADV
													group by YEAR(bills.issue_date) ";
							$tdAdv_cells = "";
							
							$runListPaymentAdv = mysqli_query($link, $sqlListPaymentAdv);
							if($runListPaymentAdv === FALSE){
								die(mysqli_error($link));
							}
						 ":: rowListPaymentAdv :: ".	$rowListPaymentAdv = mysqli_num_rows($runListPaymentAdv);
							if($rowListPaymentAdv == 0){
								for($i=0;$i<count($listYear);$i++){
									$tdAdv_cells .=  "<td> &nbsp</td>";
								}
								
							}
							else if($rowListPaymentAdv > 0){
								$tdAdv_cells = "";
								  " [countListAdvance $tenant_id] :: " .$countListAdvance = mysqli_num_rows($runListPaymentAdv);
								  " [listPayments $tenant_id] :: " .count($listYear);
								$listADV_yearAndPayment = array();
								$i = 0;
								while($resultListPaymentAdv = mysqli_fetch_array($runListPaymentAdv)){
								 ":: YEAR ADV::" .	$yearAdv = $resultListPaymentAdv["yearAdv"];
									$yearList = $listYear[$i];
									if($yearList < $yearAdv){
										$totalAmountPaidAdv = "&nbsp;";
										$listADV_yearAndPayment[] = $yearList . "," . $totalAmountPaidAdv;
										$yearList = $yearAdv;
										$i++;
									}
									if($yearList == $yearAdv){
										$totalAmountPaidAdv = $resultListPaymentAdv["totalAmountPaidAdv"];
										$listADV_yearAndPayment[] = $yearList . "," . $totalAmountPaidAdv;
										$i++;
									}
									if($yearAdv > $yearList){
										$yearDiff = $yearAdv - $yearList;
										for($y=0;$y<$yeardiff;$y++){
											$totalAmountPaidAdv = "&nbsp;";
											$listADV_yearAndPayment[] = $listYear[$y] . "," . $totalAmountPaidAdv;
											$i++;
										}
									}
								}
								//ECHO " end while loop=$loop";
								$listDiff = $loop - $i;
								for($y=$i;$y<$loop;$y++){
								   // ECHO "::FOR LOOP::";
									$totalAmountPaidAdv = "&nbsp;";
									$listADV_yearAndPayment[] = $listYear[$y] . "," . $totalAmountPaidAdv;
								}
								//print_r($listADV_yearAndPayment);
								foreach($listADV_yearAndPayment as $value){
									//[$yearADV_Set, $totalADV_Set] = explode(',', $value);
									list($yearADV_Set, $totalADV_Set) = explode(',', $value);
									
									
									if(is_numeric($totalADV_Set)){
									    $payment_advance += $totalADV_Set;   
									    $tdAdv_cells .=  "<td align='right'>".number_format($totalADV_Set,2,'.',',')."</td>";
									}
									else{
									    $tdAdv_cells .=  "<td align='right'></td>";
									}
									//$payment_advance += $totalADV_Set;
								}
							}
					echo  $tdAdv_cells;
					 //}
							$totalBayaran = $totalPayment_issuedate + $totalBayaranTunggakanPHSSB + $totalPaymentOutstandingNotPHSSB;

					?>
					<td ALIGN='RIGHT'><?php echo number_format($totalBayaran,2,'.',','); ?></td>
					
				<?php
						//$bigTotal_kadarsewa += $rental_rate;
						$bigTotal_tunggakanPHSSB += $totalTunggakanPHSSB;
						$bigTotal_tunggakanNotPHSSB += $amountDueOutstandingNotPHSSB;
						 
						$bigTotal_paymentIssueDate += $totalPayment_issuedate;
						$bigTotal_BayaranTunggakanPHSSB += $totalBayaranTunggakanPHSSB;
						$bigTotal_bakiTunggakanPHSSB += $bakiTunggakanPHSSB;
						if($bill_type_name != "") { 
							$bigTotalOutstandingNotPHSSB += $totalPaymentOutstandingNotPHSSB;
							$bigTotalBakiTunggakanNotPHSSB+= $bakiTunggakanNotPHSSB;
						}
						$bigTotal_Bayaran += $totalBayaran;
					} //end while loop tenant - end row by tenant id?>
					</tr>
					<?PHP 
					
				//echo	"END WHILE TENANT ID resultTenant:: " .$tenant_id;
					
				} 
				//echo " count rentalRateArray :: " . count($rentalRateArray);
				//print_r($rentalRateArray);
				//echo $rentalRateList;
				?>
				</tr>
				</TBODY>
				<?php 
				if($bill_types_id != "" && $_SESSION["statement_year"] != ""){
				  
					  " >> sqlBigTotalByMonth >>".  $sqlBigTotalByMonth = "select sum(payments.amount_paid) as bigTotalAmountPaid, bills.issue_date 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
												inner join projects on locations.project_id = projects.id 
												where  YEAR(bills.issue_date) = ".$_SESSION["statement_year"]." and 
												bills.bill_type_id = $bill_types_id and 
												locations.id = ".$_SESSION["location_id"]." $sqlTenantStatus  $sqlListBlock
												group by bills.issue_date
												order by bills.issue_date ASC";
					$runBigTotalByMonth = mysqli_query($link, $sqlBigTotalByMonth);
					if($runBigTotalByMonth === FALSE){
						die(mysqli_error($link));
					}
					
					while($resultBigTotalByMonth = mysqli_fetch_array($runBigTotalByMonth)){
						$bigTotalAmountPaid = $resultBigTotalByMonth["bigTotalAmountPaid"];
						$tdBigTotalByMonth .= "<td align='right' ><b>".number_format($bigTotalAmountPaid,2,'.',',')."</b></td>";
					}
				
				}
				
				
					$tdTotalAdv_cells = "";
					  "ADV PAYMENT 2: " . $sqlTotalPaymentAdv = "select YEAR(bills.issue_date) as yearAdv, sum(payments.amount_paid) as totalAmountPaidAdv, count(payments.id) 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
													inner join projects on locations.project_id = projects.id
													where bills.bill_type_id = ".$_SESSION["bill_types_id"]." 
													and locations.id = ". $_SESSION['location_id'] ." 
													$sqlDateADV
													$sqlDatePayments
													$sqlTenantStatus  $sqlListBlock
													group by YEAR(bills.issue_date) order by YEAR(bills.issue_date) asc";
						//$runTotalPaymentAdv = ;
						$runTotalPaymentAdv = mysqli_query($link, $sqlTotalPaymentAdv);
						if($runTotalPaymentAdv === FALSE){
							die(mysqli_error($link));
						}
						
						$rowTotalPaymentAdv = mysqli_num_rows($runTotalPaymentAdv);
						if($rowTotalPaymentAdv == 0){
							for($i=0;$i<count($listYear);$i++){
								$tdTotalAdv_cells .=  "<td> &nbsp</td>";
							}
							
						}
						else if($rowTotalPaymentAdv > 0){
							$tdTotalAdv_cells = "";
							 " [countListAdvance $tenant_id] :: " .$countTotalAdvance = mysqli_num_rows($runTotalPaymentAdv);
							 " [listPayments $tenant_id] :: " .count($listYear);
							$totalADV_yearAndPayment = array();
							$i = 0;
							while($resultTotalPaymentAdv = mysqli_fetch_array($runTotalPaymentAdv)){
								$yearAdv = $resultTotalPaymentAdv["yearAdv"];
								$yearTotal = $listYear[$i];
								if($yearTotal < $yearAdv){
									$totalAmountPaidAdv = "&nbsp;";
									$totalADV_yearAndPayment[] = $yearTotal . "," . $totalAmountPaidAdv;
									$yearTotal = $yearAdv;
									$i++;
								}
								if($yearTotal == $yearAdv){
									$totalAmountPaidAdv = $resultTotalPaymentAdv["totalAmountPaidAdv"];
									$totalADV_yearAndPayment[] = $yearTotal . "," . $totalAmountPaidAdv;
									$i++;
								}
								if($yearAdv > $yearTotal){
									$yearDiff = $yearAdv - $yearTotal;
									for($y=0;$y<$yeardiff;$y++){
										$totalAmountPaidAdv = "&nbsp;";
										$totalADV_yearAndPayment[] = $listYear[$y] . "," . $totalAmountPaidAdv;
										$i++;
									}
									
								}
							}
							//$listDiff = $loop - $i;
							for($y=$i;$y<$loop;$y++){
								$totalAmountPaidAdv = "&nbsp;";
								$totalADV_yearAndPayment[] = $listYear[$y] . "," . $totalAmountPaidAdv;
							}
							foreach($totalADV_yearAndPayment as $value){
							//	[$yearADV_Set, $totalADV_Set] = explode(',', $value); 
							list($yearADV_Set, $totalADV_Set) = explode(',', $value);
								$tdTotalAdv_cells .=  "<td align='right'><b>".number_format($totalADV_Set,2,'.',',')."</b></td>";
								
							}
						}
					
				
				
				?>
				<tr>
					<td colspan="3"><b>JUMLAH (RM)</b></td>
					<td align="right"><b><?PHP ECHO number_format($bigTotal_kadarsewa,2,'.',',')?></b></td>
					<td align="right"><b><?PHP ECHO number_format($bigTotal_tunggakanPHSSB,2,'.',',')?></b></td>
					<?php if($bill_type_name != ""){?>
					<td align="right"><b><?PHP ECHO number_format($bigTotal_tunggakanNotPHSSB,2,'.',',')?></b></td>
					<?php } 
					echo $tdBigTotalByMonth;
					?>
					<td align="right"><b><?PHP ECHO number_format($bigTotal_paymentIssueDate,2,'.',',')?></b></td>
					<td align="right"><b><?PHP ECHO number_format($bigTotal_BayaranTunggakanPHSSB,2,'.',',')?></b></td>
					<td align="right"><b><?PHP ECHO number_format($bigTotal_bakiTunggakanPHSSB,2,'.',',')?></b></td>
					<?php if($bill_type_name != ""){?>
					<td align="right"><b><?PHP ECHO number_format($bigTotalOutstandingNotPHSSB,2,'.',',')?></b></td>
					<td align="right"><b><?PHP ECHO number_format($bigTotalBakiTunggakanNotPHSSB,2,'.',',')?></b></td>
					<?php }?>
					<?php if($tdTotalAdv_cells != "")
						echo $tdTotalAdv_cells;?>
					<td align="right"><b><?PHP ECHO number_format($bigTotal_Bayaran,2,'.',',')?></b></td>
				</tr>
			</table>
			<?php } //end statement_year
			
			else if($statementby == "statement_ChooseDate" && $_SESSION['chooseDate'] != "" ){
				if($_SESSION['checkboc_plainReport'] == 'true'){
						include("checkbox_plainReport.php");
				}else{
				$colspanTunggakan = 2;
				$colspanTotalSemua = 10;
				$yearChooseDate = date("Y",strtotime($_SESSION['chooseDate']));
			 						
				$sqlCountYearAdv = "select YEAR(bills.issue_date) as year, count(payments.id) 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.bill_type_id in ($bill_types_id,$bill_type_Outstanding) and 
									YEAR(bills.issue_date) >= '$yearChooseDate' and 
									locations.id = ".$_SESSION["location_id"]." and payments.created_at like '%".$_SESSION['chooseDate']."%'
									group by YEAR(bills.issue_date);";
									
										
										
				$runCountYearAdv = mysqli_query($link, $sqlCountYearAdv);
				if($runCountYearAdv === FALSE){
					die(mysqli_error($link));
				}
				$rowYearAdv = mysqli_num_rows($runCountYearAdv);
				if($rowYearAdv > 0){
					$colspanTotalSemua += $rowYearAdv;
				}
				if($report_type == "Rental" && $bill_type_Outstanding != 0){
					$colspanTunggakan = 3;
					$colspanTotalSemua++;
				}
				
				?>
				<table class="table table-bordered table-hover table-sm" id="dataTable" width="70%" cellspacing="0" style="font-size: 12px">
					<thead>
					<tr>
						<td colspan="13" ><b>REPORT FOR <?php echo date("d/m/Y",strtotime($_SESSION['chooseDate']));?><span style="float:right"><?php echo "LOKASI: " . $_SESSION["projectLocation"]?></span></b></td>
					</tr>
					<tr >
						<td valign="top" align="center" rowspan="2"><b>#</b></td>
						<td valign="middle" align="center" rowspan="2"><b>NAMA</b></td>
						<td valign="middle" align="center" rowspan="2"><b>NO UNIT</b></td>
						<td rowspan="2"><b>NO RESIT</td>
						<td rowspan="2"><b>PEMBAYARAN UNTUK BULAN </b></td>
						<td align = "center" rowspan="2"><b>TERIMAAN SEMASA</b></td>
						<td align = "center" colspan="<?php echo $colspanTunggakan ?>"><b>TUNGGAKAN</b></td>
						<?php if($rowYearAdv > 0){?>
						<td align = "center" colspan="<?php echo $rowYearAdv ?>"><b>ADVANCE</b></td>
						<?php } ?>
						<td align = "center" rowspan="2"><b>TERIMAAN DI KAUNTER</b></td>
						<td align = "center" rowspan="2"><b>TERIMAAN ZAKAT / PENYEWA</b></td>
					</tr>
					<tr>
						<td ><b>TAHUN SEBELUM </b></td>
						<td ><b>TAHUN SEMASA </b></td>
						<?php if($bill_type_Outstanding != 0){?>
						<td><b>TUNGGAKAN <?php echo $bill_type_name?> </b></td>
						<?php } 
						while($resultYearAdv = mysqli_fetch_array($runCountYearAdv)){ ?>
						<td><b>TAHUN <?PHP ECHO  $resultYearAdv["year"]; ?> </b></td> 
						<?php	}?>
					</tr>
					</thead>
					<?PHP 
					$no = 1;
					
					$total_bayaranBulanSemasa = 0;
					$total_bayaranTahunSebelum = 0;
					$total_bayaranTahunSemasa = 0;
					$total_bayaranAdvance = array();
					//$total_bayaranAdvanceYear = array();
					$total_bayaranTunggakanNotPHSSB = 0;
					$total_semua = 0;
					while($resultRental = mysqli_fetch_array($runReport)){
						$tenant_id = $resultRental["tenant_id"];
						
								$rowYearAdv2 = 0;
								$tenant_name = $resultRental["tenant_name"];
								$unit_no = $resultRental["unit_no"];
								$rental_rate = $resultRental["rental_rate"];
								$receipt_no = $resultRental["receipt_no"];
								$issue_date = $resultRental["issue_date"];
								$amount_paid = $resultRental["amount_paid"];
								$created_at = $resultRental["created_at"];
								$bill_types_id = $resultRental["bill_type_id"];
								
								//$bayaranBulanSemasa = "&nbsp;"; //bulan bayaran sama dengan bulan selected
								//$bayaranTahunSebelum = "&nbsp;"; //bulan dan tahun bayaran sebelum dari selected tarikh
								//$bayaranTahunSemasa = "&nbsp;"; //bulan dan tahun sama dari selected tarikh
								//$bayaranTunggakanNotPHSSB = "&nbsp;"; //tunggakan selain PHSSB
								//$bayaranAdvance = "&nbsp;"; // advance selepas bulan dan tahun dari selected tarikh
								
								$bayaranBulanSemasa = 0; //bulan bayaran sama dengan bulan selected
								$bayaranTahunSebelum = 0; //bulan dan tahun bayaran sebelum dari selected tarikh
								$bayaranTahunSemasa = 0; //bulan dan tahun sama dari selected tarikh
								$bayaranTunggakanNotPHSSB = 0; //tunggakan selain PHSSB
								$bayaranAdvance = 0; // advance selepas bulan dan tahun dari selected tarikh
								
								if($bill_types_id == 1 || $bill_types_id == 2){ //rental
									//compare created_at dengan issue_date untuk letak kedudukan amount_paid dalam column yg mana
									//1. column bayaran semasa
									$monthYear_created_at = date("Y-m", strtotime($created_at));
									$monthYear_issue_date = date("Y-m", strtotime($issue_date));
									//2. column bayaran tunggakan tahun sebelum 
									$year_created_at = date("Y", strtotime($created_at));
									$year_issue_date = date("Y", strtotime($issue_date));
									
									if($monthYear_created_at == $monthYear_issue_date){ //semasa
										$bayaranBulanSemasa = $amount_paid;
										$total_bayaranBulanSemasa += $amount_paid;
									}
									else if($monthYear_created_at > $monthYear_issue_date){ //tunggakan
										if($year_created_at > $year_issue_date){
											$bayaranTahunSebelum = $amount_paid;
											$total_bayaranTahunSebelum += $amount_paid;
										}
										else if($year_created_at == $year_issue_date){
											$bayaranTahunSemasa = $amount_paid;
											$total_bayaranTahunSemasa += $amount_paid;
										}
									}
									else if($monthYear_created_at < $monthYear_issue_date){ //advance
										$bayaranAdvance = $amount_paid;
										//$total_bayaranAdvance += $bayaranAdvance;
										  $sqlCountYearAdv2 = "select YEAR(bills.issue_date) as year, count(YEAR(bills.issue_date)) from payments inner join bills on payments.bill_id = bills.id 
																where  bills.bill_type_id in ($bill_types_id,$bill_type_Outstanding) and YEAR(bills.issue_date) >= '$yearChooseDate' and bills.tenant_id = $tenant_id group by YEAR(bills.issue_date)";
										$runCountYearAdv2 = mysqli_query($link, $sqlCountYearAdv2);
										if($runCountYearAdv2 === FALSE){
											die(mysqli_error($link));
										}
										$rowYearAdv2 = mysqli_num_rows($runCountYearAdv2);
										
									}
								}
								else{ //tunggakan selain phssb
									$bayaranTunggakanNotPHSSB = $amount_paid;
									$total_bayaranTunggakanNotPHSSB += $amount_paid;
								}
								
								?>
								<tbody>
								<tr>
									<td><?php echo $no; ?></td>
									<td><a href="viewTenantDashboard.php?tenant_id=<?php echo $tenant_id?>"><?php echo strtoupper($tenant_name); ?></a></td>
									<td><?php echo $unit_no; ?></td>	
									<td><?php echo $receipt_no; ?></td>
									<td><?php echo date("m/Y", strtotime($issue_date)); ?></td>
									<td align="right"><?php echo number_format($bayaranBulanSemasa,2,".",","); ?></td>
									<td align="right"><?php echo number_format($bayaranTahunSebelum,2,".",","); ?></td>
									<td align="right"><?php echo number_format($bayaranTahunSemasa,2,".",","); ?></td>
									<?php if($report_type == "Rental" && $bill_type_Outstanding != 0){?>
									<td align="right"><?php echo number_format($bayaranTunggakanNotPHSSB,2,".",","); ?></td>
									<?php } 
									
									if($rowYearAdv2 > 0){
										$tdCount = 0;
										while($resultYearAdv2 = mysqli_fetch_array($runCountYearAdv2)){  ?>
											<td align="right"><?PHP 
											
											//$total_bayaranAdvanceYear[] = array_unique($resultYearAdv2["year"]);
											
											if($year_issue_date ==  $resultYearAdv2["year"]){
												echo number_format($bayaranAdvance,2,".",",");
											}
											else 
												echo "&nbsp;";
											?></td> 
											<?php	
											$tdCount++;
										}
										if($tdCount == 1)
											?>
										<td>&nbsp;</td>
										<?php
									} 
									else if($rowYearAdv2 == 0){ ?>
									<td>&nbsp;</td>
									<td>&nbsp;</td>
									<?php } ?>
									<td>&nbsp;</td>
									<td>&nbsp;</td>
									
								</tr>
								<?php
								$no++;
							//}
						?>
						
						<?PHP //}
					} 
					//kiraan untuk total bayaran Advance
					$chooseDate = date("Y-m-d",strtotime($_SESSION['chooseDate']));
					 $sqlTotalBayaranAdvPerYear = "select YEAR(bills.issue_date) as year, sum(payments.amount_paid) as sum_amount_paid, count(payments.id) 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
													inner join projects on locations.project_id = projects.id
													where bills.bill_type_id = ".$_SESSION["bill_types_id"]." and  locations.id = ".$_SESSION["location_id"]." 
													  and (bills.issue_date >= '$chooseDate' and payments.created_at like '%$chooseDate%') $sqlTenantStatus $sqlListBlock group by YEAR(bills.issue_date)";
					$runTotalBayaranAdvPerYear = mysqli_query($link, $sqlTotalBayaranAdvPerYear);
					if($runTotalBayaranAdvPerYear === FALSE){
						die(mysqli_error($link));
					}
					$total_semua = $total_bayaranBulanSemasa + $total_bayaranTahunSebelum + $total_bayaranTahunSemasa + $total_bayaranTunggakanNotPHSSB  ;
					?>
					</tbody>
					<tr>
						<td colspan = "5">JUMLAH (RM) </td>
						<td  align="right"><?php echo number_format($total_bayaranBulanSemasa,2,".",",");?> </td>
						<td align="right"><?php echo number_format($total_bayaranTahunSebelum,2,".",",");?> </td>
						<td align="right"><?php echo number_format($total_bayaranTahunSemasa,2,".",",");?> </td>
						<?php if($report_type == "Rental" && $bill_type_Outstanding != 0){?>
						<td align="right"><?php echo number_format($total_bayaranTunggakanNotPHSSB,2,".",",");?> </td>
						<?php } ?>
						<?php while($resultTotalBayaranAdvPerYear = mysqli_fetch_array($runTotalBayaranAdvPerYear)){
							$total_semua += $resultTotalBayaranAdvPerYear["sum_amount_paid"];
							?>
						<td align="right"><?php echo number_format($resultTotalBayaranAdvPerYear["sum_amount_paid"],2,".",",");?> </td>
						<?php } ?>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
					</tr>
					<tr>
						<td align="center" colspan = "5"><b>JUMLAH KESELURUHAN(RM) </b></td>
						<td align="center" colspan="<?php echo $colspanTotalSemua?>"><b><?php echo number_format($total_semua,2,".",",")?></b></td>
					</tr>
				</table>
				<?php
				}
			}
			 
			ELSE{?>
			<table class="table table-bordered table-hover table-sm" id="dataTable" width="70%" cellspacing="0" style="font-size: 12px">
				<thead>
					<tr >
						<td valign="top" align="center" rowspan="4"><b>BIL</b></td>
						<td valign="middle" align="center" rowspan="4"><b>NAMA</b></td>
						<td valign="middle" align="center" rowspan="4"><b>NO UNIT</b></td>
						<td rowspan="4"><b>NO RESIT</b></td>
						<td rowspan="4"><b>TARIKH / MASA</b></td>
						<td align = "center" colspan="7"><b>KUTIPAN SEWA</b></td>
					</tr>
					<tr>
						<td rowspan="3"><b>SEMASA (RM) </b></td>
						<td colspan="4"><b>TUNGGAKAN </b></td>
						<td rowspan="2" colspan="2"><b>BAYARAN BULAN HADAPAN </b></td>
					</tr>
					<tr>
						<td colspan="2"><b>SELAIN PHSSB</b></td>
						<td colspan="2"><b>PHSSB</b> </td>
					</tr>
					<tr>
						<td><b>BULAN / TAHUN</b></td>
						<td><b>RM</b></td>
						<td><b>BULAN / TAHUN</b></td>
						<td><b>RM</b></td>
						<td><b>BULAN / TAHUN</b></td>
						<td><b>RM</b></td>
					</tr>
				</thead>
				<?PHP 
				$no = 1;
				$sumCurrentMontd = 0.00;
				$sumOutstandingNotPHSSB = 0.00;
				$sumOutstandingPHSSB = 0.00;
				$sumAdvance = 0.00;
				$sumALL = 0.00;
				$tenantIDCompare = "";
				while($resultRental = mysqli_fetch_array($runReport)){
					$tenant_name = $resultRental["tenant_name"];
					$unit_no = $resultRental["unit_no"];
					$receipt_no = $resultRental["receipt_no"];
					$tenant_id = $resultRental["tenant_id"];
					$payment_id = $resultRental["id"];
					$amount_paid = $resultRental["amount_paid"];
					$bill_type_id = $resultRental["bill_type_id"];
					
					
					//set payment type = Outstanding / Normal / Advance
					$payment_type = "";
					$issue_date = date("Y-m", strtotime($resultRental["issue_date"]));
					$created_at = date("Y-m", strtotime($resultRental["created_at"]));
					
					//if bill_type_id not in (1,2), payment type = Outstanding (what ever reason), 1 = rental, 2 = water
					if($bill_type_id != 1 && $bill_type_id != 2)
						$payment_type = "Outstanding";
					else{
						//1. Outstanding
						if($created_at > $issue_date){
							 $payment_type = "Outstanding";
						}
						//2. Normal
						else if($created_at == $issue_date){
							 $payment_type = "Normal";
						}
						//3. Advance
						else if($created_at < $issue_date){
							 $payment_type = "Advance";
						}
					}
					
					
				?>
				 <tbody>
				 <?PHP if($tenantIDCompare != $tenant_id){?>
				<tr>
					<td><?php echo $no; ?></td>
					<td><a href="viewTenantDashboard.php?tenant_id=<?php echo $tenant_id?>"><?php echo strtoupper($tenant_name); ?></a></td>
					<td><?php echo $unit_no; ?></td>
					<td><a href="receipt/print.php?report_type=<?php echo $report_type?>&tenant_id=<?php echo $tenant_id?>&id=<?php echo $payment_id?>"><?php echo $receipt_no; ?></a></td>
					<td><?php echo date("d-m-Y h:i:s A",strtotime($resultRental["created_at"])); ?></td>
					<?php if($created_at == $issue_date && $bill_type_id == 1){ ?>
					<td><?php echo number_format($amount_paid,2,".",","); ?></td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<?php 
						$sumCurrentMontd += $amount_paid;
					} 
					?>
					<?php if($bill_type_id != 1 && $bill_type_id != 2 && $payment_type = "Outstanding"){?>
					
					<td>&nbsp;</td>
					<td><?php echo date("M / Y",strtotime($issue_date)); ?></td>
					<td><?php echo number_format($amount_paid,2,".",","); ?></td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<?php 
						$sumOutstandingNotPHSSB += $amount_paid;
					} ?>
					<?php if($created_at > $issue_date && $bill_type_id == 1){ ?>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td><?php echo date("M / Y",strtotime($issue_date)); ?></td>
					<td><?php echo number_format($amount_paid,2,".",","); ?></td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<?php 
						$sumOutstandingPHSSB += $amount_paid;
						} ?>
					<?php if($created_at < $issue_date && $bill_type_id == 1){ ?>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td><?php echo date("M / Y",strtotime($issue_date)); ?></td>
					<td><?php echo number_format($amount_paid,2,".",","); ?></td>
					<?php 
						$sumAdvance += $amount_paid;
					} 
					?>
				</tr>
				
				<?php 
				 }
				 else if($tenantIDCompare == $tenant_id){
				 
				 }
				$no++;
				} 
				?>
				<tfoot>
				<tr>
					<td colspan="5"><b>TOTAL (RM)</b></td>
					<td><b><?php echo number_format($sumCurrentMontd,2,".",",");?></b></td>
					<td></td>
					<td><b><?php echo number_format($sumOutstandingNotPHSSB,2,".",",");?></b></td>
					<td></td>
					<td><b><?php echo number_format($sumOutstandingPHSSB,2,".",",");?></b></td>
					<td></td>
					<td><b><?php echo number_format($sumAdvance,2,".",",");?></b></td>
				</tr>
				<?PHP 
				$sumALL  = $sumCurrentMontd + $sumOutstandingNotPHSSB + $sumOutstandingPHSSB + $sumAdvance;
				?>
				<tr>
					<td colspan="5"><b>TOTAL COLLECTIONS(RM)</b></td>
					<td colspan="7" align="center"><b><?php echo number_format($sumALL,2,".",",");?></b></td>
				</tr>
				</tfoot>
				 </tbody>
			</table>
			<?php } ?>
			
	</table>
				<?php } ?>
<!--REPORT TYPE : RENTAL OR WATER END-->