<?PHP 
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"];
  " BLOCK: " . $block =$_GET["block"];
$blockListing = "";
if(strpos($block,",")){
 "BLOCK EXPLODE: " .$blockListing = explode(",",$block);
}
else {
	$blockListing = $block;
}
//ECHO "BLOCK EXPLODE: " . $block = explode(",",$block);


  "COUNT BLOCK: " .$countBlock = count($blockListing);



  "date_statementOptionView"  . $date_statementOptionView = $_GET["date_statementOptionView"];

$tenant_status = $_GET["tenant_status"];
"TENANT STATUS: " .$_SESSION["tenant_status"] = $tenant_status;
$sqlTenantStatus = "";
if($tenant_status != ""){
	$sqlTenantStatus = " and tenants.status = '" .$tenant_status."' ";
}
if($statementby != ""){
	$displayStatementOption = "display:block";
	
	$sqlListBlock = "";
	$listBlock = "";
	  if(!empty($blockListing)) 
	  {
		for($i=0; $i < $countBlock; $i++)
		{
			($block[$i] . ",");
			if($listBlock == "")
				$listBlock = "'" . $blockListing[$i] . "'";
			else {
				$listBlock .= ",'" . $blockListing[$i]. "'";
			}
		}
	  }
	IF($listBlock != ""){
		$sqlListBlock = " and properties.block in (" . $listBlock .")";
	}
	
	
	
	if($statementby == "statement_year"){
		$statement_year = $date_statementOptionView;
		$_SESSION["statement_year"] = $statement_year;
		$displayYear = "display:block";
		$displayMonthYear = "display:none";
		$displayChooseDate = "display:none";
		
		
		
		
		//cari tunggakan based on year selected - contoh selected 2023
		//jan-dec 2023
		//jan-dec 2022
		//jan-dec 2021
		//jan-dec 2020
		//jan-dec 2019
		//jan-dec 2018
		//perlu ada start date & end date untuk semak dlm issue Date
		
		$yearEnd = $statement_year;
		$monthStart = "01";
		$dayStart = "01";
		$monthEnd = "12";
		$dayEnd = "31";
		$yearStart = $statement_year - 5;
		$dateStart = $yearStart ."-".$monthStart."-".$dayStart;
		$dateEnd = $yearEnd ."-".$monthEnd."-".$dayEnd;
		$dateStart = date("Y-m-d", strtotime($dateStart));
		$dateEnd = date("Y-m-d", strtotime($dateEnd));
		//currentdate
		$currentdate = date("Y-m-d");
		$current_year = date("Y",strtotime($currentdate));
		if($current_year == $yearEnd){
			$dateEnd = date("Y-m-d");
		}
		
		
		  $sql6YearsOutstanding_statementYear = "select 
												tenants.id as tenant_id,
												properties.unit_no,
												locations.location_name,
												projects.project_name,
												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
												 inner join projects on locations.project_id = projects.id
												where  bills.bill_type_id = 1 AND bills.issue_date >=  '$dateStart' and bills.issue_date <=  '$dateEnd'
												and locations.id = ".$_SESSION["location_id"]." and bills.amount_due > 0.00 and 
												(bills.payment_status = 'partial' or bills.payment_status = 'unpaid') $sqlListBlock $sqlTenantStatus
												group by tenants.id order by properties.id, bills.issue_date ASC
												";
		
		
		$run6YearsOutstanding_statementYear = mysqli_query($link,$sql6YearsOutstanding_statementYear);
		if($run6YearsOutstanding_statementYear === FALSE){
			die(mysqli_error($link));//TODO: better error handling
		}
		$date_statementOptionView = $statement_year;
	}
	else if($statementby == "statement_monthyear"){
		$statement_monthyear = $_GET["date_statementOptionView"];
		$_SESSION["statement_monthyear"] = $statement_monthyear;
		$displayYear = "display:none";
		$displayMonthYear = "display:block";
		$displayChooseDate = "display:none";
		$date_monthYear = date("Y-m-01",strtotime($statement_monthyear));
		
		$yearEnd = date("Y",strtotime($statement_monthyear));
		$month = date("m",strtotime($statement_monthyear));
		//$dayStart = "01";
		//$monthEnd = date("m",strtotime($statement_monthyear));
		//$dayEnd = "31";
		 "::yearStart::::" .$yearStart = $yearEnd - 5;
		$columnOutstanding = 7;
		$columnCount = 1;
		$year = $yearStart;
		$prevYear = $yearStart - 1;
		$prevMonth = $month + 1;
		if($prevMonth == 13)
			$prevMonth = 1; //Jan
		$prevYearDate = $prevYear . "-" . $prevMonth ."-01";
		$prevYearDate = date("Y-m-d",strtotime($prevYearDate));
		
		$dateStart = $prevYearDate;
		$dateEnd = date("Y-m-t",strtotime($statement_monthyear));
									
		 $sql6YearsOutstanding_statementMonthYear = "select tenants.id as tenant_id, 
													properties.unit_no, 
													locations.location_name,
													projects.project_name,
													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
													inner join projects on locations.project_id = projects.id 
													where bills.bill_type_id = 1 AND 
													bills.issue_date >=  '$dateStart' and bills.issue_date <=  '$dateEnd' and
													locations.id = ".$_SESSION["location_id"]." and 
													bills.amount_due > 0.00 and 
													(bills.payment_status = 'partial' or bills.payment_status = 'unpaid')  $sqlListBlock $sqlTenantStatus
													group by tenants.id order by properties.id";
													
		$run6YearsOutstanding_statementMonthYear = mysqli_query($link,$sql6YearsOutstanding_statementMonthYear);
		if($run6YearsOutstanding_statementMonthYear === FALSE){
			die(mysqli_error($link));//TODO: better error handling
		}
		
		
		
		
		 "::prevYearTest::" . $prevYearTest = date("Y", strtotime($yearStart,"-1 year")); 
		$dateStart = $yearStart ."-".$month;
		$dateEnd = $yearEnd ."-".$month;
		$dateStart = date("Y-m-01", strtotime($dateStart));
		$dateEnd = date("Y-m-t", strtotime($dateEnd));
		 " DATE MONTH YEAR::".$date_monthYear;
		 " DATE dateStart::".$dateStart;
		 " DATE dateEnd::".$dateEnd;
		
		
		
		$date_statementOptionView = $date_monthYear;
		
	}
	else if($statementby == "statement_ChooseDate"){
		$statement_ChooseDate = $_GET["date_statementOptionView"];
		$_SESSION["statement_ChooseDate"] = $statement_ChooseDate;
		
		$displayYear = "display:none";
		$displayMonthYear = "display:none";
		$displayChooseDate = "display:block";
		$statement_ChooseDate = date("Y-m-d",strtotime($statement_ChooseDate));
		
		
		$dateEnd = $statement_ChooseDate;
		
		//cari dateStart untuk 6 tahun kebelakang - mula
		$yearEnd = date("Y",strtotime($statement_ChooseDate));
		$yearStart = $yearEnd - 5;
		//semak tarikh yang dipilih tarikh hujung bulan atau tidak untuk tentukan month dan day
		$month = date("m",strtotime($statement_ChooseDate));
		$monthYearEnd = $yearEnd ."-". $month;
		 $lastMonthYearDate = date("Y-m-t", strtotime($monthYearEnd));
		$listMonthYear_td = array();
		if($dateEnd == $lastMonthYearDate){
			//echo "YES!!!";
			$dayStart = "01";
			$monthStart = $month + 1;
			$yearStart = $yearStart - 1; 
			if(strlen($monthStart) == 1){
				$monthStart = "0" . $monthStart;
			}
			
			for($i=1;$i<7;$i++){
				$dateFrom = $yearStart ."-". $monthStart ."-". $dayStart;
				//echo " DATEFROM2:: " .$dateFrom = date("Y-m-d",strtotime($dateStart));
				
				$yearTo = $yearStart + 1;
				$monthTo = $month;
				
				$dateTo = $yearTo ."-".$monthTo;
				$dateTo = date("Y-m-t",strtotime($dateTo));
				
				$listMonthYear_td[] = $dateFrom ."," .$dateTo;
				$yearStart++;
			}
		}
		else{
			//echo "NO!!!";
			$day = date("d",strtotime($statement_ChooseDate));
			$dayStart = $day + 1;
			$monthStart = $month;
			$yearStart = $yearStart - 1; 
			if(strlen($monthStart) == 1){
				$monthStart = "0" . $monthStart;
			}
			for($i=1;$i<7;$i++){
				$dateFrom = $yearStart ."-". $monthStart ."-". $dayStart;
				//$dateFrom = date("Y-m-d",strtotime($dateStart));
				
				$yearTo = $yearStart + 1;
				$monthTo = $month;
				$dayTo = $day;
				
				$dateTo = $yearTo ."-".$monthTo ."-". $day;
				$dateTo = date("Y-m-d",strtotime($dateTo));
				
				$listMonthYear_td[] = $dateFrom ."," .$dateTo;
				$yearStart++;
			}
			
		}
		
		$dateRange_start = explode(",", $listMonthYear_td[1]);
		$dateRange_end = explode(",", $listMonthYear_td[5]);
		
		$dateStart = $dateRange_start[0];
		$dateEnd =  $dateRange_end[1];
		$columnOutstanding = 7;
		$columnCount = 1;
		$year = $yearStart;
		
		  $sql6YearsOutstanding_statementChooseDate = "select tenants.id as tenant_id, 
													properties.unit_no, 
													locations.location_name,
													projects.project_name,
													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
													inner join projects on locations.project_id = projects.id 
													where bills.bill_type_id = 1 AND 
													bills.issue_date >=  '$dateStart' and bills.issue_date <=  '$dateEnd' and
													locations.id = ".$_SESSION["location_id"]." and 
													bills.amount_due > 0.00 and 
													(bills.payment_status = 'partial' or bills.payment_status = 'unpaid') $sqlListBlock $sqlTenantStatus
													group by tenants.id order by properties.id";
													
		$run6YearsOutstanding_statementChooseDate = mysqli_query($link,$sql6YearsOutstanding_statementChooseDate);
		if($run6YearsOutstanding_statementChooseDate === FALSE){
			die(mysqli_error($link));//TODO: better error handling
		}
		
		
		$date_statementOptionView = $statement_ChooseDate;
		//print_r($listMonthYear_td);
	}
		//print_r($listMonthYear_td);
}


?>

<style>
table, th, td {
  border: 1px solid black;
  border-collapse: collapse;
}
</style>
<body height="100%" style="font-family:verdana;font-size:12px" onLoad="window.print();" >
	
		<table  width="100%" cellspacing="0" style="font-size: 12px">
			<?php if($statementby == "statement_year"){ ?>
			<thead>
				<tr colspan="11"><B>MAKLUMAT TUNGGAKAN</B></tr>
				<tr>
					<th  ><center>#</center></th>
					<th><center>Unit No.</center></th>
					<th><center>Name</center></th>
					<?php 
						for($i = $yearStart; $i<=$yearEnd; $i++){
					?>
					<th><center><?PHP echo $i?></center></th>
					<?php } ?>
					
					<th><center>Total (RM)</center></th>
				</tr>
			</thead>
			<tbody>
				<tr >
				<?php 
				$no=1;
				$tenantIdCheck = "";
				$year_td = $yearStart;
				$totalOutstanding = 0.00;
				$columnOutstanding = 7;
				$columnCount = 1;
				$totalOutstanding1 = 0.00;
				$totalOutstanding2 = 0.00;
				$totalOutstanding3 = 0.00;
				$totalOutstanding4 = 0.00;
				$totalOutstanding5 = 0.00;
				$totalOutstanding6 = 0.00;
				$totalOutstandingAll = 0.00;
				$year1 = "";
				$year2 = "";
				$year3 = "";
				$year4 = "";
				$year5 = "";
				$year6 = "";
				while($result6YearsOutstanding_statementYear = mysqli_fetch_array($run6YearsOutstanding_statementYear)){
				$unit_no = $result6YearsOutstanding_statementYear["unit_no"];
				$tenant_id = $result6YearsOutstanding_statementYear["tenant_id"];
				$tenant_name = $result6YearsOutstanding_statementYear["tenant_name"];
				$outstanding = $result6YearsOutstanding_statementYear["outstanding"];
				$yearOutstanding = $result6YearsOutstanding_statementYear["issue_date"];
				$location_name = $result6YearsOutstanding_statementYear["location_name"];
				$yearOutstanding = date("Y",strtotime($yearOutstanding));
				$totalOutstanding = 0.00;
				?>
					<td><?php echo $no ?></td>
					<td><?php echo strtoupper($unit_no .", ". $location_name)  ?></td>
					<td><?php echo strtoupper($tenant_name) ?></td>
				<?php
																		
				$listMonthYear_td = array();
				
				for($i = $yearStart; $i<=$yearEnd; $i++){
					$year = $i;
					$monthStart = "01";
					$monthEnd = "12";
					
					$dateStart = $year ."-01-01";
					$dateEnd = $year ."-12-31";
					$dateStart = date("Y-m-d", strtotime($dateStart));
					$dateEnd = date("Y-m-t", strtotime($dateEnd));
					
					 $sql6YearsOutstanding_statementYear_byTenant = "select sum(bills.amount_due) as outstanding
									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
									 inner join projects on locations.project_id = projects.id
									where  bills.bill_type_id = 1 AND bills.issue_date >=  '$dateStart' and bills.issue_date <=  '$dateEnd'
									and locations.id = ".$_SESSION["location_id"]." and bills.amount_due > 0.00 and 
									(bills.payment_status = 'partial' or bills.payment_status = 'unpaid') and tenants.id = $tenant_id
									group by tenants.id order by properties.unit_no, bills.issue_date ASC
									";
					$run6YearsOutstanding_statementYear_byTenant = mysqli_query($link, $sql6YearsOutstanding_statementYear_byTenant);
					if($run6YearsOutstanding_statementYear_byTenant === FALSE){
						die(mysqli_error($link));
					}
					$result6YearsOutstanding_statementYear_byTenant = mysqli_fetch_assoc($run6YearsOutstanding_statementYear_byTenant);
					$outstanding = $result6YearsOutstanding_statementYear_byTenant["outstanding"];
					if($outstanding == null || $outstanding == ""){
						?>
						<td align="right">-</td>
						<?php 
					}
					else{
						?>
						<td align="right"><?php echo number_format($outstanding,2,".",",")?></td>
						<?php 
						$totalOutstanding += $outstanding;
						
						if($totalMonthYear_td_array[$i] == "")
							$totalMonthYear_td_array[$i] = $outstanding ;
						else if($totalMonthYear_td_array[$i] != "")
							$totalMonthYear_td_array[$i] .= "," . $outstanding;
					}
					
				}
				
				?>
				
				<td align="right"><?php echo number_format($totalOutstanding,2,".",",")?></td>
				
				<?php
				
			$no++;
			?>
			</tr>
			
			<?php }
				?>
				</tr>
				<?PHP 
		//row Total bawah sekali
		$columnOutstanding = 6;
		$columnCount = 1;												
		$sqlTotalAmountOutstanding = "select sum(bills.amount_due) as bigTotalAmountOutstanding, YEAR(bills.issue_date) as yearBillIssue 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
										inner join projects on locations.project_id = projects.id 
										where  YEAR(bills.issue_date) >=  '$yearStart' and YEAR(bills.issue_date) <=  '$yearEnd' and 
										bills.bill_type_id = 1 and (bills.payment_status = 'partial' or bills.payment_status = 'unpaid')  and   bills.amount_due > 0.00 and 
										locations.id =  ".$_SESSION["location_id"]."  and 
										projects.id = 2 $sqlListBlock $sqlTenantStatus
										group by YEAR(bills.issue_date)
										order by YEAR(bills.issue_date) ASC";
		
		$runTotalAmountOutstanding = mysqli_query($link, $sqlTotalAmountOutstanding);
		if($runTotalAmountOutstanding === FALSE){
			die(mysqli_error($link));
		}
		$year_td = $yearStart;
		while($resultTotalAmountOutstanding = mysqli_fetch_array($runTotalAmountOutstanding)){
			
			$yearBillIssue = $resultTotalAmountOutstanding["yearBillIssue"];
			$bigTotalAmountOutstanding = $resultTotalAmountOutstanding["bigTotalAmountOutstanding"];
			
			if($yearBillIssue == $year_td){
				$tdBigTotalAmountOutstanding .= "<td align='right' ><b>".number_format($bigTotalAmountOutstanding,2,'.',',')."</b></td>";
			}
			else if($yearBillIssue > $year_td){
				
				$diffYear = $yearBillIssue - $year_td;
				for($i=1;$i<=$diffYear;$i++){
					$tdBigTotalAmountOutstanding .= "<td align='right' ><b> 0.00 </b></td>";
					$year_td++;
					$columnCount++;
				}
				$tdBigTotalAmountOutstanding .= "<td align='right' ><b>".number_format($bigTotalAmountOutstanding,2,'.',',')."</b></td>";
				
			}
			else if($yearBillIssue < $year_td){
				$diffYear = $yearEnd - $year_td;
			}
			$year_td++;
			$columnCount++;
		}
		
		 $sqlTotalAllAmountOutstanding = "select sum(bills.amount_due) as bigTotalAllAmountOutstanding, YEAR(bills.issue_date) as yearBillIssue 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
										inner join projects on locations.project_id = projects.id 
										where  YEAR(bills.issue_date) >=  '$yearStart' and YEAR(bills.issue_date) <=  '$yearEnd' and 
										bills.bill_type_id = 1 and (bills.payment_status = 'partial' or bills.payment_status = 'unpaid')  and   bills.amount_due > 0.00 and 
										locations.id =  ".$_SESSION["location_id"]."  and 
										projects.id = 2 $sqlListBlock $sqlTenantStatus
										group by projects.id";
		
		$runTotalAllAmountOutstanding = mysqli_query($link, $sqlTotalAllAmountOutstanding);
		if($runTotalAllAmountOutstanding === FALSE){
			die(mysqli_error($link));
		}
		
		while($resultTotalAllAmountOutstanding = mysqli_fetch_array($runTotalAllAmountOutstanding)){
			$bigTotalAllAmountOutstanding = $resultTotalAllAmountOutstanding["bigTotalAllAmountOutstanding"];
		}
				?>
			
				<tr>
					<td colspan="3"><b>TOTAL (RM)</b></td>
					<?php echo $tdBigTotalAmountOutstanding ?>
					<td align='right'><b><?php echo number_format($bigTotalAllAmountOutstanding,2) ?></b></td>
					
				</tr>
			</tbody>	

<?php } 
	else if($statementby == "statement_monthyear"){
														$listMonthYear_td = array();
														
														for($i=1;$i<7;$i++){
															
															$prevYear = $year - 1;
															$prevMonth = $month + 1;
															
															if($prevMonth == 13)
																$prevMonth = 1; //Jan
															
															$prevYearDate = $prevYear . "-" . $prevMonth ."-01";
															$prevYearDate = date("Y-m-d",strtotime($prevYearDate));
															
															$monthYear = $year ."-" .$month;
															$monthYearDate = date("Y-m-t",strtotime($monthYear));
															
															$listMonthYear_td[] = $prevYearDate ."," .$monthYearDate;
															
															$year++;
															
														}
														$countListMonthYear_td = count($listMonthYear_td);
														$th_dateRange = "";
														for($i=0;$i<$countListMonthYear_td;$i++){
															
															$dateRange = explode(",", $listMonthYear_td[$i]);
															
															$th_dateRange .= "<Th><center>". date("d/m/Y",strtotime($dateRange[0])) . " - " . date("d/m/Y",strtotime($dateRange[1])) . "</center></th>";
														}
														
														
															?>
													<tr bgcolor="#ebeced">
														<th  ><center>#</center></th>
														<th><center>Unit No.</center></th>
														<th><center>Name</center></th>
														<?php 
															echo $th_dateRange ;
														?>
														
														<th><center>Total (RM)</center></th>
													</tr>
												</thead>
												<tbody>
													<tr >
													<?php 
													$no=1;
													$tenantIdCheck = "";
													$year_td = $yearStart;
													$totalOutstanding = 0.00;
													$columnOutstanding = 7;
													$columnCount = 1;
													
													//dapatkan senarai tarikh start dan end bagi setiap 6 column (6 tahun tunggakan)
													
													$year = $yearStart;
													
													
													$listTenantInfo = array();
													$listTenantOutstanding = array();
													
													$no=1;
													 while($result6YearsOutstanding_statementMonthYear = mysqli_fetch_array($run6YearsOutstanding_statementMonthYear)){
														$unit_no = $result6YearsOutstanding_statementMonthYear["unit_no"];
														$tenant_id = $result6YearsOutstanding_statementMonthYear["tenant_id"];
														$tenant_name = $result6YearsOutstanding_statementMonthYear["tenant_name"];
														$outstanding = $result6YearsOutstanding_statementMonthYear["outstanding"];
														$monthYearOutstanding = $result6YearsOutstanding_statementMonthYear["issue_date"];
														$location_name = $result6YearsOutstanding_statementMonthYear["location_name"];
														$yearOutstanding = date("Y",strtotime($yearOutstanding));
														//$dateRange = array();
														$totalOutstanding = 0.00;
														
															?>
															<td><?php echo $no ?></td>
															<td><?php echo strtoupper($unit_no .", ". $location_name)  ?></td>
															<td><?php echo strtoupper($tenant_name) ?></td>
															<?php
															$totalMonthYear_td_array = array();
															$totalMonthYear = 0.00;
															for($i=0;$i<$countListMonthYear_td;$i++){
															
																$dateRange = explode(",", $listMonthYear_td[$i]);
																$dateStart = $dateRange[0];
																$dateEnd = $dateRange[1];
																//$th_dateRange .= "<Th><center>". date("d/m/Y",strtotime($dateRange[0])) . " - " . date("d/m/Y",strtotime($dateRange[1])) . "</center></th>";
																 $sql6YearsOutstanding_statementMonthYear_bytenant = "select 
																			sum(bills.amount_due) as outstanding

																			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
																			 inner join projects on locations.project_id = projects.id
																			where  bills.bill_type_id = 1 AND bills.issue_date >=  '$dateStart' and bills.issue_date <=  '$dateEnd'
                                                                            and locations.id = ".$_SESSION["location_id"]." and bills.amount_due > 0.00 and 
																			(bills.payment_status = 'partial' or bills.payment_status = 'unpaid')  and tenants.id = $tenant_id $sqlListBlock $sqlTenantStatus
																			
																			";
																$run6YearsOutstanding_statementMonthYear_bytenant = mysqli_query($link, $sql6YearsOutstanding_statementMonthYear_bytenant);
																if($run6YearsOutstanding_statementMonthYear_bytenant === FALSE){
																	die(mysqli_error($link));
																}
																$resul6YearsOutstanding_statementMonthYear_bytenant = mysqli_fetch_assoc($run6YearsOutstanding_statementMonthYear_bytenant);
																$outstanding = $resul6YearsOutstanding_statementMonthYear_bytenant["outstanding"];
																if($outstanding == null || $outstanding == ""){
																	?>
																	<td align="right">-</td>
																	<?php 
																}
																else{
																	?>
																	<td align="right"><?php echo number_format($outstanding,2,".",",")?></td>
																	<?php 
																	$totalOutstanding += $outstanding;
																	
																	if($totalMonthYear_td_array[$i] == "")
																		$totalMonthYear_td_array[$i] = $outstanding ;
																	else if($totalMonthYear_td_array[$i] != "")
																		$totalMonthYear_td_array[$i] .= "," . $outstanding;
																}
															}
															?>
															<td align="right"><?php echo number_format($totalOutstanding,2,".",",")?></td>
															<?php
														$no++;	
														?>
														</tr>
														
														<?php
													}
													?>
														<tr>
															<td colspan = "3" align="middle"><b>TOTAL (RM)</b></td>
															<?PHP 
															$totalAll = 0.00;
															for($i=0;$i<$countListMonthYear_td;$i++){
																$dateRange = explode(",", $listMonthYear_td[$i]);
																$dateStart = $dateRange[0];
																$dateEnd = $dateRange[1];
																 $sql6YearsOutstanding_statementMonthYear_total = "select 
																			sum(bills.amount_due) as total_outstanding

																			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
																			 inner join projects on locations.project_id = projects.id
																			where  bills.bill_type_id = 1 AND bills.issue_date >=  '$dateStart' and bills.issue_date <=  '$dateEnd'
                                                                            and locations.id = ".$_SESSION["location_id"]." and bills.amount_due > 0.00 and 
																			(bills.payment_status = 'partial' or bills.payment_status = 'unpaid')   $sqlListBlock $sqlTenantStatus
																			
																			";
																$run6YearsOutstanding_statementMonthYear_total = mysqli_query($link, $sql6YearsOutstanding_statementMonthYear_total);
																if($run6YearsOutstanding_statementMonthYear_total === FALSE){
																	die(mysqli_error($link));
																}
																$resul6YearsOutstanding_statementMonthYear_total = mysqli_fetch_assoc($run6YearsOutstanding_statementMonthYear_total);
																$total_outstanding = $resul6YearsOutstanding_statementMonthYear_total["total_outstanding"];
																if($total_outstanding == null || $total_outstanding == ""){
																	?>
																	<td align="right"><b>0.00</b></td>
																	<?php 
																}
																else{
																	?>
																	<td align="right"><b><?php echo number_format($total_outstanding,2,".",",")?></b></td>
																	<?php 
																	$totalOutstandingAll += $total_outstanding;
																	
																}
															}
															
															?>
															<td align="right"><b><?php echo number_format($totalOutstandingAll,2,".",",")?></b></td>
														</tr>
													<?php
													
												}
												else if($statementby == "statement_ChooseDate"){
													
													echo "countListMonthYear_td: ".$countListMonthYear_td = count($listMonthYear_td);
													$th_dateRange = "";
													for($i=0;$i<$countListMonthYear_td;$i++){
														
														$dateRange = explode(",", $listMonthYear_td[$i]);
														
														$th_dateRange .= "<Th><center>". date("d/m/Y",strtotime($dateRange[0])) . " - " . date("d/m/Y",strtotime($dateRange[1])) . "</center></th>";
													}
														
														
															?>
													<tr bgcolor="#ebeced">
														<th  ><center>#</center></th>
														<th><center>Unit No.</center></th>
														<th><center>Name</center></th>
														<?php 
															echo $th_dateRange ;
														?>
														
														<th><center>Total (RM)</center></th>
													</tr>
													<tr>
													<?php
													$no=1;
													while($result6YearsOutstanding_statementChooseDate = mysqli_fetch_array($run6YearsOutstanding_statementChooseDate)){
														$unit_no = $result6YearsOutstanding_statementChooseDate["unit_no"];
														$tenant_id = $result6YearsOutstanding_statementChooseDate["tenant_id"];
														$tenant_name = $result6YearsOutstanding_statementChooseDate["tenant_name"];
														$outstanding = $result6YearsOutstanding_statementChooseDate["outstanding"];
														$monthYearOutstanding = $result6YearsOutstanding_statementChooseDate["issue_date"];
														$location_name = $result6YearsOutstanding_statementChooseDate["location_name"];
														$yearOutstanding = date("Y",strtotime($yearOutstanding));
														//$dateRange = array();
														$totalOutstanding = 0.00;
														
															?>
															<td><?php echo $no ?></td>
															<td><?php echo strtoupper($unit_no .", ". $location_name)  ?></td>
															<td><?php echo strtoupper($tenant_name) ?></td>
															<?php
															$totalMonthYear_td_array = array();
															$totalMonthYear = 0.00;
															for($i=0;$i<$countListMonthYear_td;$i++){
															
																$dateRange = explode(",", $listMonthYear_td[$i]);
																$dateStart = $dateRange[0];
																$dateEnd = $dateRange[1];
																//$th_dateRange .= "<Th><center>". date("d/m/Y",strtotime($dateRange[0])) . " - " . date("d/m/Y",strtotime($dateRange[1])) . "</center></th>";
																 $sql6YearsOutstanding_statementMonthYear_bytenant = "select 
																			sum(bills.amount_due) as outstanding

																			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
																			 inner join projects on locations.project_id = projects.id
																			where  bills.bill_type_id = 1 AND bills.issue_date >=  '$dateStart' and bills.issue_date <=  '$dateEnd'
                                                                            and locations.id = ".$_SESSION["location_id"]." and bills.amount_due > 0.00 and 
																			(bills.payment_status = 'partial' or bills.payment_status = 'unpaid')  and tenants.id = $tenant_id
																			
																			";
																$run6YearsOutstanding_statementMonthYear_bytenant = mysqli_query($link, $sql6YearsOutstanding_statementMonthYear_bytenant);
																if($run6YearsOutstanding_statementMonthYear_bytenant === FALSE){
																	die(mysqli_error($link));
																}
																$resul6YearsOutstanding_statementMonthYear_bytenant = mysqli_fetch_assoc($run6YearsOutstanding_statementMonthYear_bytenant);
																$outstanding = $resul6YearsOutstanding_statementMonthYear_bytenant["outstanding"];
																if($outstanding == null || $outstanding == ""){
																	?>
																	<td align="right">-</td>
																	<?php 
																}
																else{
																	?>
																	<td align="right"><?php echo number_format($outstanding,2,".",",")?></td>
																	<?php 
																	$totalOutstanding += $outstanding;
																	
																	if($totalMonthYear_td_array[$i] == "")
																		$totalMonthYear_td_array[$i] = $outstanding ;
																	else if($totalMonthYear_td_array[$i] != "")
																		$totalMonthYear_td_array[$i] .= "," . $outstanding;
																}
															}
															?>
															<td align="right"><?php echo number_format($totalOutstanding,2,".",",")?></td>
															<?php
														$no++;	
													
														?>
													</tr>
														
														<?php
														}
														?>
														
														<tr>
															<td colspan = "3" align="middle"><b>TOTAL (RM)</b></td>
															<?PHP 
															$totalAll = 0.00;
															for($i=0;$i<$countListMonthYear_td;$i++){
																$dateRange = explode(",", $listMonthYear_td[$i]);
																$dateStart = $dateRange[0];
																$dateEnd = $dateRange[1];
																 $sql6YearsOutstanding_statementChooseDate_total = "select 
																			sum(bills.amount_due) as total_outstanding

																			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
																			 inner join projects on locations.project_id = projects.id
																			where  bills.bill_type_id = 1 AND bills.issue_date >=  '$dateStart' and bills.issue_date <=  '$dateEnd'
                                                                            and locations.id = ".$_SESSION["location_id"]." and bills.amount_due > 0.00 and 
																			(bills.payment_status = 'partial' or bills.payment_status = 'unpaid')  $sqlListBlock $sqlTenantStatus
																			
																			";
																$sql6YearsOutstanding_statementChooseDate_total = mysqli_query($link, $sql6YearsOutstanding_statementChooseDate_total);
																if($sql6YearsOutstanding_statementChooseDate_total === FALSE){
																	die(mysqli_error($link));
																}
																$resul6YearsOutstanding_statementChooseDate_total = mysqli_fetch_assoc($sql6YearsOutstanding_statementChooseDate_total);
																$total_outstanding = $resul6YearsOutstanding_statementChooseDate_total["total_outstanding"];
																if($total_outstanding == null || $total_outstanding == ""){
																	?>
																	<td align="right"><b>0.00</b></td>
																	<?php 
																}
																else{
																	?>
																	<td align="right"><b><?php echo number_format($total_outstanding,2,".",",")?></b></td>
																	<?php 
																	$totalOutstandingAll += $total_outstanding;
																	
																}
															}
															
															?>
															<td align="right"><b><?php echo number_format($totalOutstandingAll,2,".",",")?></b></td>
														</tr>
														
														<?php
												}
									
										


?>


</TABLE>
</body>