<?php
// 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));
}


$_SESSION["statementby"] = ""; 
$statementby = "";
$statement_year = "";
$statement_monthyear = "";
$statement_date = "";
$statement_ByChooseDate = "";
$selected_cutOffPayment = "";
$displayYear = "display:none";
$displayMonthYear = "display:none";
$displayDateFromTo = "display:none";
$displayChooseDate = "display:none";
$displayCheckBox = "display:none";
$displayStatementOption = "display:none";


//DISPLAY BEFORE SUBMIT START
$statementby = $_GET["statementby"];
IF($statementby != "" || $statementby != null){
	if($statementby == 'statement_year'){
		$displayYear = "display:block";
		$displayMonthYear = "display:none";
		$displayDateFromTo = "display:none";
		$displayChooseDate = "display:none";
		$displayCheckBox = "display:block";
		$displayStatementOption = "display:none";
		//$_GET["statement_monthyear"] = "";
		//$_GET["dateFrom"] = "";
		//$_GET["dateTo"] = "";
		//$_GET["statement_ChooseDate"] = "";
		
	}
	else if($statementby == 'statement_monthyear'){
		$displayYear = "display:none";
		$displayMonthYear = "display:block";
		$displayDateFromTo = "display:none";
		$displayChooseDate = "display:none";
		$displayCheckBox = "display:block";
		$displayStatementOption = "display:none";
	}
	else if($statementby == 'statement_date'){
		$displayYear = "display:none";
		$displayMonthYear = "display:none";
		$displayDateFromTo = "display:block";
		$displayChooseDate = "display:none";
		$displayCheckBox = "display:block";
		$displayStatementOption = "display:none";
	}
	else if($statementby == 'statement_ChooseDate'){
		$displayYear = "display:none";
		$displayMonthYear = "display:none";
		$displayDateFromTo = "display:none";
		$displayChooseDate = "display:block";
		$displayCheckBox = "display:block";
		$displayStatementOption = "display:none";
	}
}
//END DISPLAY BEFORE SUBMIT




	$kiraan30percent_fromBF = 0;
	$totalBF_rental = 0;
	$totalBF_water = 0;
	$firstYearDate = "";
	$_SESSION["cutOffPayment"] = "";
	$tenant_id = $_GET["tenant_id"];
	//GET TENANT INFO
	
	include ("tenantAndDeposit_statementInfo.php");
	
	//tenant status start--
	$tenant_status_show = "";
	if($tenant_status = 'enable')
		$tenant_status_show = "Active";
	else if($tenant_status = 'disable')
		$tenant_status_show = "Deactive";
	
	//tenant status END
	
	$cutOffDate = date("Y-m-d");
	//GET STATEMENT INFO FROM BILL AND PAYMENT TABLE 
	//echo "projectName111: ".$_SESSION["projectName"];
	
	//CUT OFF RENTAL STATEMENT INFO - START
	$issueDate_cutOff = date("Y-m-d");
	$sqlSearchDate_latestPayment_rental = "SELECT payments.created_at as payment_date from payments inner join bills on payments.bill_id = bills.id where bills.tenant_id = $tenant_id and bills.bill_type_id = 1 ORDER BY `payments`.`created_at` DESC limit 1";
	$runSearchDate_latestPayment_rental = mysqli_query($link, $sqlSearchDate_latestPayment_rental);
	if($runSearchDate_latestPayment_rental === FALSE){
		die(mysqli_error($link));
	}
	if($resultSearchDate_latestPayment_rental=mysqli_fetch_assoc($runSearchDate_latestPayment_rental)){
		$date_latestPayment_rental = $resultSearchDate_latestPayment_rental["payment_date"];
		$date_latestPayment_rental = date("Y-m-d",strtotime($date_latestPayment_rental));
		
		if($date_latestPayment_rental >  date("Y-m-d")){
			$issueDate_cutOff = $date_latestPayment_rental;
		}
		else if($date_latestPayment_rental <=  date("Y-m-d")){
			$issueDate_cutOff = date("Y-m-d");
		}
	}
	//CUT OFF RENTAL STATEMENT INFO - END
	
	
	

 	 " sqlStatementRental:: " . $sqlStatementRental ="SELECT * FROM ( 
						(SELECT payments.id as payment_id,payments.receipt_no, payments.amount_paid as amount, bills.issue_date as issue_date, payments.created_at as date_created,  bills.payment_status as status_payment, concat(payments.receipt_no,' payment ',payments.amount_paid) as info FROM payments
						inner join bills on payments.bill_id = bills.id 
						where bills.tenant_id = $tenant_id and bills.bill_type_id = 1 ) 
						UNION ALL (SELECT bills.id, bills.bill_no, bills.bill_amount as amount, bills.issue_date as issue_date, bills.issue_date as date_created, bills.payment_status as status_payment, concat(bills.bill_no,' bill ',bill_amount) as info  FROM bills 
						where bills.tenant_id = $tenant_id  and bills.bill_type_id = 1 and bills.issue_date <= '$issueDate_cutOff') ) 
						results ORDER BY date_created ASC";
						
	$sqlStatementWater_old ="SELECT payments.id as payment_id,payments.receipt_no, payments.amount_paid as amount, bills.issue_date as issue_date, payments.created_at as date_created,  bills.payment_status as status_payment, 
							concat(payments.receipt_no,' payment ',payments.amount_paid) as info,concat(payments.created_at,' tarikh_keyin ',payments.updated_at) as period
							FROM payments
							inner join bills on payments.bill_id = bills.id
							where bills.tenant_id = $tenant_id and bills.bill_type_id = 2  and payments.created_at < '2025-01-01' ORDER BY date_created ASC";
			
	$sqlStatementWater_new ="SELECT * FROM ( 
				(SELECT payments.id as payment_id,payments.receipt_no, payments.amount_paid as amount, bills.issue_date as issue_date, payments.created_at as date_created,  bills.payment_status as status_payment, 
					concat(payments.receipt_no,' payment ',payments.amount_paid) as info,concat(payments.created_at,' tarikh_keyin ',payments.updated_at) as period
					FROM payments
					inner join bills on payments.bill_id = bills.id
					where bills.tenant_id = $tenant_id and bills.bill_type_id = 2  and payments.created_at > '2024-12-31') 
				UNION ALL 	
					(SELECT payments_water.id as payment_id,payments_water.receipt_no, payments_water.amount_paid as amount, bills_water.issue_date as issue_date, payments_water.created_at as date_created,  bills_water.payment_status as status_payment, 
					concat(payments_water.receipt_no,' payment ',payments_water.amount_paid) as info,concat(bills_water.date_reading_current,'/',bills_water.date_reading_before,'/',bills_water.reading_current,'/',bills_water.reading_before) as period 
					FROM payments_water
					inner join bills_water on payments_water.water_bill_id = bills_water.id
					where bills_water.tenant_id = $tenant_id )
					
				UNION ALL 
					(SELECT bills_water.id, bills_water.bill_invoice, bills_water.bill_amount as amount, bills_water.issue_date as issue_date, bills_water.issue_date as date_created, bills_water.payment_status as status_payment, 
					concat(bills_water.bill_invoice,' bill ',bill_amount) as info, concat(bills_water.date_reading_current,'/',bills_water.date_reading_before,'/',bills_water.reading_current,'/',bills_water.reading_before) as period
					FROM bills_water 
					where bills_water.tenant_id = $tenant_id ) 
				)	
				results ORDER BY date_created ASC";
	
	
						
	$sqlStatementOthers ="SELECT * FROM ( 
						(SELECT payments.id as payment_id,payments.receipt_no, payments.amount_paid as amount, bills.issue_date as issue_date, payments.created_at as date_created,  bills.payment_status as status_payment, concat(payments.receipt_no,' payment ',payments.amount_paid) as info FROM payments
						inner join bills on payments.bill_id = bills.id
						where bills.tenant_id = $tenant_id and bills.bill_type_id not in (1,2)) 
						UNION ALL (SELECT bills.id, bills.bill_no, bills.bill_amount as amount, bills.issue_date as issue_date, bills.issue_date as date_created, bills.payment_status as status_payment, concat(bills.bill_no,' bill ',bill_amount) as info  FROM bills 
						where bills.tenant_id = $tenant_id  and bills.bill_type_id not in (1,2)) ) 
						results ORDER BY date_created ASC";
	
	  $sqlAgeing = " SELECT * FROM ( 
							(select  bills.bill_amount- sum( payments.amount_paid ) as total_outstanding , bills.*,payments.created_at as payment_date, properties.unit_no, tenants.tenant_name 
							from payments 
							inner join bills on payments.bill_id = bills.id 
							inner join tenants on bills.tenant_id =  tenants.id  
							
							inner join properties on tenants.property_id = properties.id
							inner join locations on properties.location_id = locations.id
							where bills.tenant_id =$tenant_id  and bills.issue_date <= '$cutOffDate' and bills.bill_type_id = 1 group by bills.issue_date having total_outstanding <> 0  ) 
							UNION  
								(select sum(bills.bill_amount) as total_outstanding, bills.* , bills.created_at as  payment_date,  properties.unit_no, tenants.tenant_name 
									from bills 
									inner join tenants on bills.tenant_id =  tenants.id  
									
									inner join properties on tenants.property_id = properties.id
									inner join locations on properties.location_id = locations.id
									where bills.issue_date  <= '$cutOffDate'
									and bills.id not in (select payments.bill_id from payments inner join bills on payments.bill_id = bills.id WHERE bills.tenant_id = $tenant_id )
									and tenant_id = $tenant_id  
									and bills.bill_type_id = 1
									group by issue_date having total_outstanding <> 0 and bills.issue_date < CURRENT_DATE)
							) results group by issue_date ORDER BY issue_date ASC";
	
	
	
	$sqlYear = "select YEAR(bills.issue_date) AS year from bills where tenant_id = $tenant_id group by year order by year desc";
	$runSqlYear = mysqli_query($link, $sqlYear);
	if($runSqlYear === FALSE){
		die(mysqli_error($link));
	}
	$sqlMonthYear = "select issue_date AS monthyear from bills where tenant_id = $tenant_id  order by issue_date desc";
	$runSqlMonthYear = mysqli_query($link, $sqlMonthYear);
	if($runSqlMonthYear === FALSE){
		die(mysqli_error($link));
	}
	
	if($_SESSION["projectName"] != "PPR"){
		//GET DATE FOR TENANT NOT ELIGABLE FOR 30% OF MONTLY RENTAL
		$queryDateStop30Percent = "";
		$sqlInfo30Percent = "select * from infostop30percent where tenant_id = $tenant_id and infostatus = 'enable'";
		$runInfo30Percent = mysqli_query($link, $sqlInfo30Percent);
		if($runInfo30Percent === FALSE){
			die(mysqli_error($link));
		}
		$countRowInfo30Percent = mysqli_num_rows($runInfo30Percent);
		if($countRowInfo30Percent == 1){
			$resultInfo30Percent = mysqli_fetch_assoc($runInfo30Percent);
			$dateStop30Percent = $resultInfo30Percent["month_stop30percent"];
			$queryDateStop30Percent = "and bill.issue_date < '$dateStop30Percent'";
		}
	}
	
	//untuk dapatkan b/f berakhir 2024 
	include("water_bf_end2024.php");
	
	
	if(isset($_GET["searchStatement"])){
		include("query_statement.php");
	}//end submit button
	//ECHO $sqlStatementRental;
	$runSqlStatementRental = mysqli_query($link, $sqlStatementRental);
	$runSqlStatementWater_old = mysqli_query($link, $sqlStatementWater_old);
	$runSqlStatementWater_new = mysqli_query($link, $sqlStatementWater_new);
	$runSqlStatementOthers = mysqli_query($link, $sqlStatementOthers);
	
	if($runSqlStatementRental === FALSE){
		die(mysqli_error($link));
	}
	if($runSqlStatementWater_old === FALSE){
		die(mysqli_error($link));
	}
	if($runSqlStatementWater_new === FALSE){
		die(mysqli_error($link));
	}
	if($runSqlStatementOthers === FALSE){
		die(mysqli_error($link));
	}
	$countRowOthers = mysqli_num_rows($runSqlStatementOthers);
	$countRowWater_old = mysqli_num_rows($runSqlStatementWater_old);
	$countRowWater_new = mysqli_num_rows($runSqlStatementWater_new);
	$countRowRental = mysqli_num_rows($runSqlStatementRental);
	
	if($countRowRental == 0){
		 $sqlStatementRental ="SELECT bills.id, bills.bill_no, bills.bill_amount as amount, bills.issue_date as issue_date, bills.issue_date as date_created, bills.payment_status as status_payment, concat(bills.bill_no,' bill ',bill_amount) as info  FROM bills 
								where bills.tenant_id = $tenant_id  and bills.bill_type_id = 1 and bills.issue_date <= '$issueDate_cutOff'
									 ORDER BY date_created ASC";
		$runSqlStatementRental = mysqli_query($link, $sqlStatementRental);							
		if($runSqlStatementRental === FALSE){
			die(mysqli_error($link));
		}
	}
	if($countRowOthers == 0){
		$sqlStatementOthers ="SELECT bills.id, bills.bill_no, bills.bill_amount as amount, bills.issue_date as issue_date, bills.issue_date as date_created, bills.payment_status as status_payment, concat(bills.bill_no,' bill ',bill_amount) as info  FROM bills 
									where bills.tenant_id = $tenant_id  and bills.bill_type_id not in (1,2)
									 ORDER BY date_created ASC";
		$runSqlStatementOthers = mysqli_query($link, $sqlStatementOthers);							
		if($runSqlStatementRental === FALSE){
			die(mysqli_error($link));
		}
	}
	if($countRowWater_new == 0){
		$runSqlStatementWater_new ="SELECT bills_water.id, bills_water.bill_invoice, bills_water.bill_amount as amount, bills_water.issue_date as issue_date, bills_water.issue_date as date_created, bills_water.payment_status as status_payment, 
										concat(bills_water.bill_invoice,' bill ',bill_amount) as info, concat(bills_water.date_reading_current,'/',bills_water.date_reading_before,'/',bills_water.reading_current,'/',bills_water.reading_before) as period
										FROM bills_water 
										where bills_water.tenant_id = $tenant_id
									 ORDER BY date_created ASC";
		$runSqlStatementWater_new = mysqli_query($link, $sqlStatementWater_new);							
		if($runSqlStatementRental === FALSE){
			die(mysqli_error($link));
		}
	}
	
	//include("sql_ageingPart.php");
?>
<script>

function submitFunc(){ //alert("SUBMIT BUTTON!!!");
	if(document.getElementById("statementby").value== 'statement_year'){
		
		document.getElementById("statement_year").setAttribute("required","required");
		document.getElementById("statement_monthyear").removeAttribute("required");
		document.getElementById("dateFrom").removeAttribute("required");
		document.getElementById("dateTo").removeAttribute("required");
		document.getElementById("statement_ChooseDate").removeAttribute("required");
		
		if(document.getElementById("statement_year").value =='')
			alert("Please select Year");
		else{
			//document.getElementById("statement_year").value ='';
			document.getElementById("statement_monthyear").value ='';
			document.getElementById("dateFrom").value ='';
			document.getElementById("dateTo").value ='';
			document.getElementById("statement_ChooseDate").value ='';
			document.getElementById("form_tenantStatement").submit();
		}
	}
	else if(document.getElementById("statementby").value== 'statement_monthyear'){
		
		document.getElementById("statement_year").removeAttribute("required");
		document.getElementById("statement_monthyear").setAttribute("required","required");
		document.getElementById("dateFrom").removeAttribute("required");
		document.getElementById("dateTo").removeAttribute("required");
		document.getElementById("statement_ChooseDate").removeAttribute("required");
		
		if(document.getElementById("statement_monthyear").value =='')
			alert("Please select Month/Year");
		else{
			document.getElementById("statement_year").value ='';
			//document.getElementById("statement_monthyear").value ='';
			document.getElementById("dateFrom").value ='';
			document.getElementById("dateTo").value ='';
			document.getElementById("statement_ChooseDate").value ='';
			document.getElementById("form_tenantStatement").submit();
		}
	}
	else if(document.getElementById("statementby").value== 'statement_date'){
		
		document.getElementById("statement_year").removeAttribute("required");
		document.getElementById("statement_monthyear").removeAttribute("required");
		document.getElementById("dateFrom").setAttribute("required","required");
		document.getElementById("dateTo").setAttribute("required","required");
		document.getElementById("statement_ChooseDate").removeAttribute("required");
		
		if(document.getElementById("dateFrom").value =='' || document.getElementById("dateTo").value =='')
			alert("Please choose date");
		else{
			document.getElementById("statement_year").value ='';
			document.getElementById("statement_monthyear").value ='';
			//document.getElementById("dateFrom").value ='';
			//document.getElementById("dateTo").value ='';
			document.getElementById("statement_ChooseDate").value ='';
			document.getElementById("form_tenantStatement").submit();
		}
	}
	else if(document.getElementById("statementby").value== 'statement_ChooseDate'){
		document.getElementById("statement_year").removeAttribute("required");
		document.getElementById("statement_monthyear").removeAttribute("required");
		document.getElementById("dateFrom").removeAttribute("required");
		document.getElementById("dateTo").removeAttribute("required");
		document.getElementById("statement_ChooseDate").setAttribute("required","required");
		
		if(document.getElementById("statement_ChooseDate").value =='')
			alert("Please select date");
		else{
			document.getElementById("statement_year").value ='';
			document.getElementById("statement_monthyear").value ='';
			document.getElementById("dateFrom").value ='';
			document.getElementById("dateTo").value ='';
			//document.getElementById("statement_ChooseDate").value ='';
			document.getElementById("form_tenantStatement").submit();
		}
	} 
}
function resetFunc(){ //alert();
	document.getElementById("form_tenantStatement").reset();
	 
	document.getElementById('statement_ByYear').style.display ='none';
	document.getElementById('statement_ByMonthYear').style.display ='none';
	document.getElementById('statement_ByDate').style.display ='none';
	document.getElementById('statement_cutOff').style.display ='none';
	document.getElementById('statement_ByChooseDate').style.display ='none';
	document.getElementById('statementby').value ='';
	
	document.getElementById("statement_year").value ='';
	document.getElementById("statement_monthyear").value ='';
	document.getElementById("dateFrom").value ='';
	document.getElementById("dateTo").value ='';
	document.getElementById("statement_ChooseDate").value ='';
	
	document.getElementById("form_tenantStatement").submit()
}
function printStatement(tenant_id, statementby,statement_year,statement_monthyear, dateFrom,dateTo, statement_ChooseDate, cutOffPayment){
	window.open("print_statementInfo.php?tenant_id="+tenant_id+"&statementby="+statementby+"&statement_year="+statement_year+"&statement_monthyear="+statement_monthyear+"&dateFrom="+dateFrom+"&dateTo="+dateTo+"&statement_ChooseDate="+statement_ChooseDate+"&cutOffPayment="+cutOffPayment,"_blank");
}
function exportToCSV(tenant_id, statementby,statement_year,statement_monthyear, dateFrom,dateTo, statement_ChooseDate,cutOffPayment){

	window.open("exportToCSV_viewStatement.php?tenant_id="+tenant_id+"&statementby="+statementby+"&statement_year="+statement_year+"&statement_monthyear="+statement_monthyear+"&dateFrom="+dateFrom+"&dateTo="+dateTo+"&statement_ChooseDate="+statement_ChooseDate+"&cutOffPayment="+cutOffPayment,"_blank");
}
</script>
<div align="center">
	<form action="<?php $_SERVER['PHP_SELF']; ?>" method="get" class="form-horizontal" id="form_tenantStatement">
		<input type="hidden" name="tenant_id" value="<?php echo $tenant_id?>">
		<input type="hidden" name="login" value="<?php echo $_SESSION["username"]?>">
		<div class="card">
			<div class="card-header py-3">
				<h5 class="m-0 font-weight-bold text-primary">STATEMENT OPTION</h5>
			</div>
			<div class="card-body">
				<div class="row form-group">
					<div class="col-12 col-md-12" >
					<?php //include("statement_option.php"); ?>
						<table width="100%">
							<tr align="center">
								<td width="30%">
									<?php
									
									$_SESSION["statementby"] = $_GET["statementby"];
											$_SESSION["statement_year"] = $_GET["statement_year"];
											$_SESSION["statement_monthyear"] = $_GET["statement_monthyear"];
											$_SESSION["dateFrom"] = $_GET["dateFrom"];
											$_SESSION["dateTo"] = $_GET["dateTo"];
											$_SESSION["statement_ChooseDate"] = $_GET["statement_ChooseDate"];?>
									
									<select name="statementby" id="statementby" onchange="statementby_change()" class="form-control" >
										<option value="">Select Statement Option</option>
										<option value="statement_year" <?php echo ($_SESSION["statementby"] ==  'statement_year')  ? "selected" : "" ?>>STATEMENT BY YEAR</option>
										<option value="statement_monthyear" <?php echo ($_SESSION["statementby"] ==  'statement_monthyear')  ? "selected" : "" ?>>STATEMENT BY MONTH AND YEAR</option>
										<option value="statement_date" <?php echo ($_SESSION["statementby"] ==  'statement_date')  ? "selected" : "" ?>>STATEMENT BY DATE (BETWEEN TWO DATES)</option>
										<option value="statement_ChooseDate" <?php echo ($_SESSION["statementby"] ==  'statement_ChooseDate')  ? "selected" : "" ?> >STATEMENT BY CUT OFF DATE</option>
									</select>
								</td>
								<td id="statement_ByYear" style='<?php echo $displayYear ?>;'>
									<?php //if($_SESSION["statement_year"])?>
									<select required="" name="statement_year" id="statement_year" class="form-control">
										<option value="">Please Select Year</option>
										<?php while($resultYear = mysqli_fetch_array($runSqlYear)){?>
												<option value="<?php echo $resultYear["year"]?>" <?php echo ($_SESSION["statement_year"] ==  $resultYear["year"])  ? "selected" : "" ?> ><?php echo $resultYear["year"]?></option>
										<?php } ?>
									</select>
								</td>
								<td id="statement_ByMonthYear" style='<?php echo $displayMonthYear  ?>;'>
									<input required="" type="month" class="form-control" name="statement_monthyear" id="statement_monthyear" value="<?php echo $_SESSION["statement_monthyear"]?>" size="20"/>
								</td>
								<td id="statement_ByDate" style='<?php echo $displayDateFromTo ?>;' >
								<b>	From: </b><input required="" id='dateFrom' name='dateFrom' type='date' size='10' value="<?php echo $_SESSION["dateFrom"]?>"> 
								<b>	To:  </b><input  required="" id='dateTo' name='dateTo' type='date' size='10' value="<?php echo $_SESSION["dateTo"]?>">
								</td>
								<td id="statement_ByChooseDate" style='<?php echo $displayChooseDate ?>;' >
									<input required="" id="statement_ChooseDate" name="statement_ChooseDate" class="form-control" type='date' size='10' value="<?php echo $_SESSION["statement_ChooseDate"]?>"> 
								</td>
							</tr>
							<?php if($page != "report_all_payments.php"){?>
							<tr >
								<td>&nbsp;</td>
								<td align="center" id="statement_cutOff" style='<?php  echo $displayCheckBox  ?>;font-size:12px' >
									<div>
										<input type="checkbox" id="cutOffPayment" name="cutOffPayment" checked value="true" READONLY>
										<label for="cutOffPayment" style="font-size:16px"><b> View Ageing Record based on cut off payments same as selected Month / Year </b></label>
									</div>
								</td>
							</tr>
							<?php } ?>
						</table>
					</div>
				</div>
			</div>
			<div class="card-footer">
				<button type="submit" name="searchStatement" onclick="submitFunc()" class="btn btn-primary btn-sm" >
					<i class="fa fa-dot-circle-o"></i> Submit
				</button>
				<button  onclick="resetFunc()" name="resetStatement" class="btn btn-danger btn-sm">
					<i class="fa fa-ban"></i> Reset
				</button>
			</div>
		</div>
	</form>
</div>

<br/>
<div align="center">
	<div class="card">
		<div class="card-header py-3">
			<form id="formToPrintExport" method="get" action="print_statementInfo.php" target="_blank">
				<h5 class="m-0 font-weight-bold text-primary">SHOWING RESULT</h5>
				<input type="hidden" name="tenant_id" value="<?php echo $tenant_id?>">
				<input type="hidden" name="statementby" value="<?php echo $_SESSION["statementby"]?>">
				<input type="hidden" name="statement_year" value="<?php echo $_SESSION["statement_year"]?>">
				<input type="hidden" name="statement_monthyear" value="<?php echo $_SESSION["statement_monthyear"]?>">
				<input type="hidden" name="dateFrom" value="<?php echo $_SESSION["dateFrom"]?>">
				<input type="hidden" name="dateTo" value="<?php echo $_SESSION["dateTo"]?>">
				<input type="hidden" name="statement_ChooseDate" value="<?php echo $_SESSION["statement_ChooseDate"]?>">
				<input type="hidden" name="cutOffPayment" value="<?php echo $_SESSION["cutOffPayment"]?>">
				<button type="button" class="btn btn-outline-primary btn-sm" onclick="printStatement('<?php echo $tenant_id;?>','<?php echo $_SESSION["statementby"]?>','<?php echo $_SESSION["statement_year"]?>','<?php echo $_SESSION["statement_monthyear"]?>','<?php echo $_SESSION["dateFrom"]?>','<?php echo $_SESSION["dateTo"]?>','<?php echo $_SESSION["statement_ChooseDate"]?>','<?php echo $_SESSION["cutOffPayment"]?>')" class="btn btn-outline-warning btn-sm">
					<i class="fa fa-print"></i>&nbsp; PRINT STATEMENT
				</button>
				<button type="button" class="btn btn-outline-primary btn-sm" onclick="exportToCSV('<?php echo $tenant_id;?>','<?php echo $_SESSION["statementby"]?>','<?php echo $_SESSION["statement_year"]?>','<?php echo $_SESSION["statement_monthyear"]?>','<?php echo $_SESSION["dateFrom"]?>','<?php echo $_SESSION["dateTo"]?>','<?php echo $_SESSION["statement_ChooseDate"]?>','<?php echo $_SESSION["cutOffPayment"]?>')" class="btn btn-outline-warning btn-sm">
					<i class="fa fa-table"></i>&nbsp; EXPORT TO CSV
				</button>
				
				<button type="submit" class="btn btn-outline-primary btn-sm"  class="btn btn-outline-warning btn-sm">
					<i class="fa fa-print"></i>&nbsp; PRINT STATEMENT
				</button>
				<button type="button" class="btn btn-outline-primary btn-sm" onclick="exportToCSV('<?php echo $tenant_id;?>','<?php echo $_SESSION["statementby"]?>','<?php echo $_SESSION["statement_year"]?>','<?php echo $_SESSION["statement_monthyear"]?>','<?php echo $_SESSION["dateFrom"]?>','<?php echo $_SESSION["dateTo"]?>','<?php echo $_SESSION["statement_ChooseDate"]?>','<?php echo $_SESSION["cutOffPayment"]?>')" class="btn btn-outline-warning btn-sm">
					<i class="fa fa-table"></i>&nbsp; EXPORT TO CSV
				</button>
			</form>
		</div>
		<div class="card-body">
			<div class="card-header py-3">
				<h5 class="m-0 font-weight-bold text-primary">TENANT INFO | <a href='Up_tenant_detail.php?name = <?php echo $name?>&tenant_id=<?php echo $tenant_id?>'>EDIT</a></h5>
			</div>
			<div class="card-body">
				<div class="table-responsive">
					<table class="table table-bordered table-hover table-sm" id="dataTable" width="100%" cellspacing="0" style="font-size: 12px">
						<tr>
							<td width='30%'><strong>NAME</strong></td>
							<td width='50%'><?php echo strtoupper($tenant_name)?></td>
						</tr>
						<tr>
							<td width='30%'><strong>IC NO.</strong></td>
							
							<td width='50%'><?php echo $tenant_ic?></td>
						</tr>
						<tr>
							<td width='30%'><strong>CONTACT NO.</strong></td>
							
							<td width='50%'><?php echo $contact_no?></td>
						</tr>
						
						<tr>
							<td width='30%'><strong>TYPE OF PROPERTY</strong></td>
							
							<td width='50%'><?php echo strtoupper($rental_program)?></td>
						</tr>
						<tr>
							<td width='30%'><strong>PROPERTY LOCATION</strong></td>
							
							<td width='50%'><?php echo strtoupper($maklumatHartanah)?></td>
						</tr>
						<tr>
							<td width='30%'><strong>RENTAL STATUS</strong></td>
							
							<td width='50%'><?php echo strtoupper($tenant_status_show)?></td>
						</tr>
						<tr>
							<td width='30%'><strong>RENTAL START</strong></td>
							
							<td width='50%'><?php echo $start_rent?></td>
						</tr>
						<tr>
							<td width='30%'><strong>RENTAL END</strong></td>
							
							<td width='50%'><?php echo $end_rent?></td>
						</tr>
						
					</table>
				</div>
			</div>
			<div class="card-header py-3">
				<h5 class="m-0 font-weight-bold text-primary">DEPOSIT INFO</h5>
			</div>
			<div class="card-body">
				<div class="table-responsive">
					
						
						<?php 
						
						?>
						
						<table class="table table-bordered table-hover table-sm" id="dataTable" width="70%" cellspacing="0" style="font-size: 12px">
							<thead>
							<tr><th colspan="8">Deposit Payment (Payment below September 2024)</th></tr>
							  <tr>
								<td align="center"><b>#</b></td>
								<td align="center"><b>Receipt No.</b></td>
								<td align="center"><b>Two Months Rental(RM)</b></td>
								<td align="center"><b>Utilities(RM)</b></td>
								<td align="center"><b>Management Fee(RM)</b></td>
								<td align="center"><b>Total(RM)</b></td>
								<td align="center"><b>Date Paid</b></td>
								<td align="center"><b>Staff Incharge</b></td>
							  </tr>
							</thead>
							<tbody>
							  <?php 
								$no=1;
								$totalAllDepositPayment = 0.00;
								//$countRowDeposit =mysqli_num_rows($runDeposit);
								if($countRowOldRecord >= 1){
								 while($resultDeposit_oldRecord = mysqli_fetch_array($runDeposit_oldRecord)){
										$deposit_id = $resultDeposit_oldRecord["id"];
										$receipt_no = $resultDeposit_oldRecord["receipt_no"];
										$two_month_rental = $resultDeposit_oldRecord["two_month_rental"];
										$utilities = $resultDeposit_oldRecord["utilities"];
										$management_fee = $resultDeposit_oldRecord["management_fee"];
										$created_at = $resultDeposit_oldRecord["created_at"];
										$pic = $resultDeposit_oldRecord["name"];
										$total_deposit = $resultDeposit_oldRecord["total_deposit"];
										$totalAllDepositPayment += $total_deposit;
									
							?>
								<tr>
									<td align="center"><?php echo $no?></td>
									<td align="center"><a href="print_deposit.php?deposit_id=<?php echo $deposit_id?>" target="_blank"><?php echo $receipt_no?></a></td>
									<td align="right"><?php echo number_format($two_month_rental,2)?></td>
									<td align="right"><?php echo number_format($utilities,2)?></td>
									<td align="right"><?php echo number_format($management_fee,2)?></td>
									<td align="right"><?php echo number_format($total_deposit,2)?></td>
									<td align="center"><?php echo date("d/m/Y",strtotime($created_at))?></td>
									<td align="center"><?php echo ucfirst($pic)?></td>
								</tr>
								<?php 
								$no++;
								} ?>
								<tr>
									<td colspan="5"><b>TOTAL (RM)</b></td>
									<td align="right"><b><?php echo number_format($totalAllDepositPayment,2)?></b></td>
									<td colspan="2">&nbsp;</td>
								</tr>
								<?php } else if($countRowOldRecord == 0){
										?>
									<tr align="center"><th colspan="8">~ No Record Found ~</th></tr>	
									<?php
									}
									?>
						</table>
						<br/>
					<table class="table table-bordered table-hover table-sm" id="dataTable" width="100%" cellspacing="0" style="font-size: 12px">
						<tr><th colspan="8">Deposit Payment (Payment on September 2024 and above)</th></tr>
						<tr align="center">
							<td rowspan="2"><b>#</b></td>
							<td rowspan="2"><b>DATE</b></td>
							<td rowspan="2"><b>INVOICE / RECEIPT</b></td>
							<td rowspan="2"><b>DESCRIPTION</b></td>
							<td><b> (Db)</b></td>
							<td><b> (Cr)</b></td>
							<td><b>BALANCE</b></td>
						</tr>
						<tr align="center">
							<td><b>(RM)</b></td>
							<td><b>(RM)</b></td>
							<td><b>(RM)</b></td>
						</tr>
						
						<?php
						$no = 1;
						 if($countStatementDeposit_newRecord > 0){
							 $depo_bf = 0.00;
							 $depo_baki = 0.00;
							 $depo_totalDb = 0.00;
							 $depo_totalCr = 0.00;
							 ?>
							 
						<tr>
							<td align="center" valign="top"  ><?php echo $no;?></td>
							<td align="center" valign="top"  ></td>
							<td align="center" valign="top"  ></td>
							<td align="center" valign="top"  >B/F</td>
							<td align="center" valign="top"  ></td>
							<td align="center" valign="top"  ></td>
							<td align="right" valign="top"  >0.00</td>
						</tr>	 
							 <?php 
							while($resultStatementDeposit_newRecord = mysqli_fetch_array($runStatementDeposit_newRecord)){
								$no++;
								$deposit_id = $resultStatementDeposit_newRecord["id"];
								$receipt_no = $resultStatementDeposit_newRecord["receipt_no"];
								$two_months_amount_paid = $resultStatementDeposit_newRecord["two_months_amount_paid"];
								$two_month_rental_amount_bill = $resultStatementDeposit_newRecord["two_month_rental_amount_bill"];
								$utility_amount_paid = $resultStatementDeposit_newRecord["utility_amount_paid"];
								$utility_amount_bill = $resultStatementDeposit_newRecord["utility_amount_bill"];
								$created_at = $resultStatementDeposit_newRecord["created_at"];
								$deposit_bill_id = $resultStatementDeposit_newRecord["deposit_bill_id"];
								$deposit_bill_no = $resultStatementDeposit_newRecord["deposit_bill_no"];
								$payment_status = $resultStatementDeposit_newRecord["payment_status"];
								$info_two_months_amount = $resultStatementDeposit_newRecord["info_two_months_amount"];
								$info_utility_amount = $resultStatementDeposit_newRecord["info_utility_amount"];
								$total_deposit_amount = $two_month_rental_amount + $utility_amount;
								$total_deposit_paid = $two_months_amount_paid + $utility_amount_paid;
								$paymentType = "";
								//$totalAllDepositPayment += $total_deposit;
								
								
								$description_2MonthRental = "";
								$description_utility = "";
								if($two_month_rental_amount_bill != "" ||  $two_month_rental_amount_bill != null || $two_months_amount_paid != "" ||  $two_months_amount_paid != null){
									$description_2MonthRental = "Two Month Rental";
								}
								if($utility_amount_bill != "" ||  $utility_amount_bill != null || $utility_amount_paid != "" ||  $utility_amount_paid != null ){
									$description_utility = "Utility";
								}
								
								if(strpos($info_two_months_amount, 'deposit_bill') !== false  ) {
									$depo_baki += $two_month_rental_amount_bill;
									$depo_totalDb += $two_month_rental_amount_bill;
									//echo " DEPO BAKI: IF 1 >> " . $depo_baki;
								}
								if(strpos($info_utility_amount, 'deposit_bill') !== false){
									$depo_baki += $utility_amount_bill;
									$depo_totalDb += $utility_amount_bill;
									//echo " DEPO BAKI: IF 2 >> " . $depo_baki;
								}
								if(strpos($info_two_months_amount, 'deposit_payment') !== false ) {
									$depo_baki -= $two_months_amount_paid;
									$depo_totalCr += $two_months_amount_paid;
									//echo " DEPO BAKI: IF 3 >> " . $depo_baki;
								}
								if(strpos($info_utility_amount, 'deposit_payment') !== false){
									$depo_baki -= $utility_amount_paid;
									$depo_totalCr += $utility_amount_paid;
									//echo " DEPO BAKI: IF 4 >> " . $depo_baki;
								}
								
								
							?>
							<tr>
								<td align="center" valign="top" rowspan="2" ><?php echo $no;?></td>
								<td align="center" valign="top" rowspan="2"><?php echo date("d/m/Y",strtotime($created_at));?></td>
								<td align="center" valign="top" rowspan="2"><?php echo $receipt_no;?></td>
								<?php if (strpos($info_two_months_amount, 'deposit_bill') !== false) { ?>
								<td align="center" valign="top" ><?php echo $description_2MonthRental;?></td>
								<td align="right" valign="top" ><?php echo number_format($two_month_rental_amount_bill,2);?></td>
								<td align="center" valign="top" ></td>
								<td align="right" valign="top" rowspan="2"><?php echo number_format($depo_baki,2);?></td>
								</tr>
								
								<?php }  if (strpos($info_utility_amount, 'deposit_bill') !== false) { ?>
								<tr>
								<td align="center" valign="top" ><?php echo $description_utility;?></td>
								<td align="right" valign="top" ><?php echo number_format($utility_amount_bill,2);?></td>
								<td align="center" valign="top" ></td>
								
								<?php }  if (strpos($info_two_months_amount, 'deposit_payment') !== false) { ?>
								<td align="center" valign="top" ><?php echo $description_2MonthRental;?></td>
								<td align="center" valign="top" ></td>
								<td align="right" valign="top" ><?php echo number_format($two_months_amount_paid,2);?></td>
								<td align="right" valign="top" rowspan="2"><?php echo number_format($depo_baki,2);?></td>
								</tr>
								<?php }  if (strpos($info_utility_amount, 'deposit_payment') !== false) { ?>
								<td align="center" valign="top" ><?php echo $description_utility;?></td>
								<td align="center" valign="top" ></td>
								<td align="right" valign="top" ><?php echo number_format($utility_amount_paid,2);?></td>
								
								<?php } ?>
							</tr>
							<?php
							}?>
							<tr>
								<td colspan="4" align="right"><b>Total (RM)</b></td>
								<td align="right"><b><?php echo number_format($depo_totalDb,2)?></b></td>
								<td align="right"><b><?php echo number_format($depo_totalCr,2)?></b></td>
								<td align="right"><b><?php echo number_format($depo_baki,2)?></b></td>
							</tr>
							<?php } 
							else{
								?>
							<tr>
								<td colspan="7" align="center"><b>~No Record Found~</b></td>
							</tr>	
								
								<?PHP
							}?>
					</table>
					<br/>
					<table class="table table-bordered table-hover table-sm" id="dataTable" width="100%" cellspacing="0" style="font-size: 12px">
						<tr><th colspan="8">Others Payment</th></tr>
						<tr align="center">
							<td rowspan="2"><b>#</b></td>
							<td rowspan="2"><b>DATE</b></td>
							<td rowspan="2"><b>INVOICE / RECEIPT</b></td>
							<td rowspan="2"><b>DESCRIPTION</b></td>
							<td><b> (Db)</b></td>
							<td><b> (Cr)</b></td>
							<td><b>BALANCE</b></td>
						</tr>
						<tr align="center">
							<td><b>(RM)</b></td>
							<td><b>(RM)</b></td>
							<td><b>(RM)</b></td>
						</tr>
						
						<?php
						$no = 1;
						 if($countRowPaymentOthers > 0){
							 $depoOthers_bf = 0.00;
							 $depoOthers_baki = 0.00;
							 $depoOthers_totalDb = 0.00;
							 $depoOthers_totalCr = 0.00;
							 ?>
							 
						<tr>
							<td align="center" valign="top"  ><?php echo $no;?></td>
							<td align="center" valign="top"  ></td>
							<td align="center" valign="top"  ></td>
							<td align="center" valign="top"  >B/F</td>
							<td align="center" valign="top"  ></td>
							<td align="center" valign="top"  ></td>
							<td align="right" valign="top"  >0.00</td>
						</tr>	 
							 <?php 
							 
							while($resultDepositPaymentOthers = mysqli_fetch_array($runDepositPaymentOthers)){
								$no++;
								$deposit_id = $resultDepositPaymentOthers["id"];
								$receipt_no = $resultDepositPaymentOthers["receipt_no"];
								$yp_paid = $resultDepositPaymentOthers["yp_paid"];
								$yp_charges_amount = $resultDepositPaymentOthers["yp_charges_amount"];
								$sst_paid = $resultDepositPaymentOthers["sst_paid"];
								$sst_charges_amount = $resultDepositPaymentOthers["sst_charges_amount"];
								$stamping_paid = $resultDepositPaymentOthers["stamping_paid"];
								$stamping_charges_amount = $resultDepositPaymentOthers["stamping_charges_amount"];
								$created_at = $resultDepositPaymentOthers["created_at"];
								$deposit_bill_other_id = $resultDepositPaymentOthers["deposit_bill_other_id"];
								$deposit_other_bill_no = $resultDepositPaymentOthers["deposit_other_bill_no"];
								$payment_status = $resultDepositPaymentOthers["payment_status"];
								$info_yp_paid = $resultDepositPaymentOthers["info_yp_paid"];
								$info_sst_paid = $resultDepositPaymentOthers["info_sst_paid"];
								$info_stamping_paid = $resultDepositPaymentOthers["info_stamping_paid"];
								$total_depositOthers_amount = $yp_charges_amount + $sst_charges_amount + $stamping_charges_amount;
								$total_depositOthers_paid = $yp_paid + $sst_paid + $stamping_paid;
								$paymentType = "";
								//$totalAllDepositPayment += $total_deposit;
								
								
								$description_YP = "";
								$description_SST = "";
								$description_Stamping = "";
								if($yp_charges_amount != "" ||  $yp_charges_amount != null || $yp_paid != "" ||  $yp_paid != null){
									$description_YP = "Yuran Pelbagai";
								}
								if($sst_charges_amount != "" ||  $sst_charges_amount != null || $sst_paid != "" ||  $sst_paid != null ){
									$description_SST = "SST Charges";
								}
								if($stamping_charges_amount != "" ||  $stamping_charges_amount != null || $stamping_paid != "" ||  $stamping_paid != null ){
									$description_Stamping = "Stamping Charges";
								}
								
								if(strpos($info_yp_paid, 'deposit_other_bill') !== false  ) {
									$depo_others_baki += $yp_charges_amount;
									$depo_others_totalDb += $yp_charges_amount;
									$description_YP = "Yuran Pelbagai";
									//echo " DEPO BAKI: IF 1 >> " . $depo_others_baki;
								}
								if(strpos($info_sst_paid, 'deposit_other_bill') !== false){
									$depo_others_baki += $sst_charges_amount;
									$depo_others_totalDb += $sst_charges_amount;
									$description_SST = "SST Charges";
									//echo " DEPO BAKI: IF 2 >> " . $depo_others_baki;
								}
								if(strpos($info_stamping_paid, 'deposit_other_bill') !== false){
									$depo_others_baki += $stamping_charges_amount;
									$depo_others_totalDb += $stamping_charges_amount;
									$description_Stamping = "Stamping Charges";
									//echo " DEPO BAKI: IF 2 >> " . $depo_others_baki;
								}
								if(strpos($info_yp_paid, 'deposit_other_payment') !== false ) {
									$depo_others_baki -= $yp_paid;
									$depo_others_totalCr += $yp_paid;
									$description_YP = "Yuran Pelbagai";
									//echo " DEPO BAKI: IF 3 >> " . $depo_others_baki;
								}
								if(strpos($info_sst_paid, 'deposit_other_payment') !== false){
									$depo_others_baki -= $sst_paid;
									$depo_others_totalCr += $sst_paid;
									$description_SST = "SST Charges";
									//echo " DEPO BAKI: IF 4 >> " . $depo_others_baki;
								}
								if(strpos($info_stamping_paid, 'deposit_other_payment') !== false){
									$depo_others_baki -= $stamping_paid;
									$depo_others_totalCr += $stamping_paid;
									$description_Stamping = "Stamping Charges";
									//echo " DEPO BAKI: IF 4 >> " . $depo_others_baki;
								}
								
							?>
							<tr>
								<td align="center" valign="top" rowspan="3" ><?php echo $no;?></td>
								<td align="center" valign="top" rowspan="3"><?php echo date("d/m/Y",strtotime($created_at));?></td>
								<td align="center" valign="top" rowspan="3"><?php echo $receipt_no;?></td>
								<?php if (strpos($info_yp_paid, 'deposit_other_bill') !== false) { ?>
								<td align="center" valign="top" ><?php echo $description_YP;?></td>
								<td align="right" valign="top" ><?php echo number_format($yp_charges_amount,2);?></td>
								<td align="center" valign="top" ></td>
								<td align="right" valign="top" rowspan="3"><?php echo number_format($depo_others_baki,2);?></td>
								</tr>
								
								<?php }  if (strpos($info_sst_paid, 'deposit_other_bill') !== false) { ?>
								<tr>
								<td align="center" valign="top" ><?php echo $description_SST;?></td>
								<td align="right" valign="top" ><?php echo number_format($sst_charges_amount,2);?></td>
								<td align="center" valign="top" ></td>
								</tr>
								<?php }  if (strpos($info_stamping_paid, 'deposit_other_bill') !== false) { ?>
								<tr>
								<td align="center" valign="top" ><?php echo $description_Stamping;?></td>
								<td align="right" valign="top" ><?php echo number_format($stamping_charges_amount,2);?></td>
								<td align="center" valign="top" ></td>
								<tr/>
								<?php }  if (strpos($info_yp_paid, 'deposit_other_payment') !== false) { ?>
								<td align="center" valign="top" ><?php echo $description_YP;?></td>
								<td align="center" valign="top" ></td>
								<td align="right" valign="top" ><?php echo number_format($yp_paid,2);?></td>
								<td align="right" valign="top" rowspan="3"><?php echo number_format($depo_others_baki,2);?></td>
								</tr>
								<?php }  if (strpos($info_sst_paid, 'deposit_other_payment') !== false) { ?>
								<tr>
								<td align="center" valign="top" ><?php echo $description_SST;?></td>
								<td align="center" valign="top" ></td>
								<td align="right" valign="top" ><?php echo number_format($sst_paid,2);?></td>
								</tr>
								<?php }  if (strpos($info_stamping_paid, 'deposit_other_payment') !== false) { ?>
								<tr>
								<td align="center" valign="top" ><?php echo $description_Stamping;?></td>
								<td align="center" valign="top" ></td>
								<td align="right" valign="top" ><?php echo number_format($stamping_paid,2);?></td>
								<?php } ?>
							</tr>
							<?php
								
							}?>
							<tr>
								<td colspan="4" align="right"><b>Total (RM)</b></td>
								<td align="right"><b><?php echo number_format($depo_others_totalDb,2)?></b></td>
								<td align="right"><b><?php echo number_format($depo_others_totalCr,2)?></b></td>
								<td align="right"><b><?php echo number_format($depo_others_baki,2)?></b></td>
							</tr>
							<?php } 
							else{
								?>
							<tr>
								<td colspan="7" align="center"><b>~No Record Found~</b></td>
							</tr>	
								
								<?PHP
							}?>
					</table>
				</div>
			</div>
			<div class="card-header py-3">
				<h5 class="m-0 font-weight-bold text-primary">RENTAL INFO</h5>
			</div>
			<div class="card-body">
				<div class="table-responsive">
					<table class="table table-bordered table-hover table-sm" id="dataTable" width="100%" cellspacing="0" style="font-size: 12px">
						<tr align="center">
							<td rowspan="2"><b>#</b></td>
							<td rowspan="2"><b>DATE</b></td>
							<td rowspan="2"><b>INVOICE / RECEIPT NO</b></td>
							<td rowspan="2"><b>DESCRIPTION</b></td>
							<td><b> (Db)</b></td>
							<td><b>(Cr)</b></td>
							<td><b>BALANCE</b></td>
							
						</tr>
						<tr align="center">
							<td><b>(RM)</b></td>
							<td><b>(RM)</b></td>
							<td><b>(RM)</b></td>
							
						</tr>
						<?php 
						$no=1;
						$showBFDate = "";
						if($firstYearDate != ""){
							$showBFDate = date("d/m/Y",strtotime($firstYearDate));
						}
						?>
						<tr>
							<td align="center"><?php echo $no?></td>
							<td align="center"><?php echo $showBFDate;?></td>
							<td>&nbsp;</td>
							<td>B/F</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							<td align="right"><?php echo number_format($totalBF_rental,2);?></td>
							
						</tr>
						<?php 
						//$no=1;
						$totalPaid = 0;
						$totalUnpaid = 0;
						$totalPaidList = "";
						$totalUnpaidList = "";
						$totalBaki = $totalBF_rental;
						$bill = 0;
						$payment = 0;
						$kiraan30Percent = 0;
						$total_kiraan30Percent = 0;
						$month = "";
						$year = "";
						$slashPosition = 0;
						$RP_rentalMonth = "";
						
						$layak = "";
						$bill_id_smartsewa = "";
						while($resultStatement = mysqli_fetch_array($runSqlStatementRental)){
							include("statement_resultQuery.php");
							//check ref no if exist
							if (strpos($info, 'payment') !== false) {
								include("paymentRefNoInfo.php");
							}
							else if(strpos($info, 'bill') !== false) {
								$paymentInfoShow = "";
							}
							
							?>
						<tr>
							<td align="center"><?php echo $no?></td>
							<td align="center"><?php echo $date_created?></td>
							<td><?php echo $id?></td>
							<td><?php echo $pay_info  . $paymentInfoShow ?></td>
							<?php if (strpos($info, 'bill') !== false) { ?>
							<td align="right"><?php echo number_format($amount,2)?></td>
							<td align="right"></td>
							<td align="right"><?php echo number_format($totalBaki,2)?></td>
							<?php 
							} if (strpos($info, 'payment') !== false) {
									?>
							<td align="right"></td>
							<td align="right"><?php echo number_format($amount,2)?></td>
							<td align="right"><?php echo number_format($totalBaki,2)?></td>
						</tr>
						<?php }
							}
						?>
						<tr>
							<td align="right" colspan = "4"><b>TOTAL (RM)</b></td>
							<td align="right"><b><?php echo number_format($totalUnpaid,2) ?></b></td>
							<td align="right"><b><?php echo number_format($totalPaid,2) ?></b></td>
							<td align="right"><b><?php echo number_format($totalBaki,2)?></b></td>
							
						</tr>
					</table>
				</div>
			</div>
			<div class="card-header py-3">
				<h5 class="m-0 font-weight-bold text-primary">WATER BILL INFO</h5>
			</div>
			<!--2024 dan ke bawah start-->
			<?php if($countRowWater_old > 0){?>
			<div class="card-body">
				<div class="table-responsive">
					<table class="table table-bordered table-hover table-sm" id="dataTable" width="100%" cellspacing="0" style="font-size: 12px">
						<tr align="center">
							<td rowspan="2"><b>#</b></td>
							<td rowspan="2"><b>DATE</b></td>
							<td rowspan="2"><b>RECEIPT NO</b></td>
							<td rowspan="2"><b>DESCRIPTION PAYMENT'S WATER FOR 2024 AND BELOW</b></td>
							<td><b>AMOUNT PAID</b></td>
							<!--<td><b>(Cr)</b></td>
							<td><b>BALANCE</b></td>-->
						</tr>
						<tr align="center">
							<td><b>(RM)</b></td>
							<!--<td><b>(RM)</b></td>
							<td><b>(RM)</b></td>-->
							
						</tr>
						<?php 
						$no=1;
						$showBFDate = "";
						if($firstYearDate != ""){
							$showBFDate = date("d/m/Y",strtotime($firstYearDate));
						}
						$totalBF_water_old = 0.00;
					
						?>
						<!-- <tr>
							<td align="center"><?php echo $no?></td>
							<td align="center"><?php echo $showBFDate;?></td>
							<td>&nbsp;</td>
							<td>B/F</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							<td align="right"><?php echo number_format($totalBF_water_old,2);?></td>
							
						</tr> -->
						<?php 
						//$no=1;
						$totalPaid = 0;
						$totalUnpaid = 0;
						$totalPaidList = "";
						$totalUnpaidList = "";
						$totalBaki = $totalBF_water_old;
						$bill = 0;
						$payment = 0;
						$kiraan30Percent = 0;
						$total_kiraan30Percent = 0;
						$month = "";
						$year = "";
						$slashPosition = 0;
						$RP_rentalMonth = "";
						
						$layak = "";
						$bill_id_smartsewa = "";
						$rekod_bacaan = "";
						while($resultStatement = mysqli_fetch_array($runSqlStatementWater_old)){
							include("statement_resultQuery.php");
							//check ref no if exist
							if (strpos($info, 'payment') !== false) {
								include("paymentRefNoInfo.php");
								$rekod_bacaan = "";
							}
							else if(strpos($info, 'bill') !== false) {
								$paymentInfoShow = "";
								if (strpos($period, '/') !== false) {
									 "rekod_bacaan:: " . $rekod_bacaan = " (Tarikh Bacaan: $date_reading_before - $date_reading_current, Kegunaan: $bacaan m3)";
								}
							}
							
							
							
							?>
						<tr>
							<td align="center"><?php echo $no?></td>
							<td align="center"><?php echo $date_created?></td>
							<td><?php echo $id?></td>
							<td><?php echo $pay_info  . $paymentInfoShow . $rekod_bacaan?></td>
							<?php /*if (strpos($info, 'bill') !== false) { 
									
							?>
							<td align="right"><?php echo number_format($amount,2)?></td>
							<td align="right"></td>
							<td align="right"><?php echo number_format($totalBaki,2)?></td>
							
							<?php 
									
							}*/ if (strpos($info, 'payment') !== false) { 
									
							?>
							<!--<td align="right"></td>-->
							<td align="right"><?php echo number_format($amount,2)?></td>
							<!--<td align="right"><?php echo number_format($totalBaki,2)?></td>-->
							
						</tr>
						<?php
							//if($totalBF_water != 0)
								//$totalBF_water =0; 
							}
						
					}
						?>
						<tr>
							<td align="right" colspan = "4"><b>TOTAL (RM)</b></td>
							<!--<td align="right"><b><?php echo number_format($totalUnpaid,2) ?></b></td>-->
							<td align="right"><b><?php echo number_format($totalPaid,2) ?></b></td>
							<!--<td align="right"><b><?php echo number_format($totalBaki,2)?></b></td>-->
							
						</tr>
					</table>
				</div>
			</div>
			<?php } ?>
			<!--2024 dan ke bawah end-->
			
			<!--2025 dan ke ATAS START-->
			<?php if($countRowWater_new > 0){?>
			<div class="card-body">
				<div class="table-responsive">
					<table class="table table-bordered table-hover table-sm" id="dataTable" width="100%" cellspacing="0" style="font-size: 12px">
						<tr align="center">
							<td rowspan="2"><b>#</b></td>
							<td rowspan="2"><b>DATE</b></td>
							<td rowspan="2"><b>INVOICE / RECEIPT NO</b></td>
							<td rowspan="2"><b>DESCRIPTION WATER BILLING AND PAYMENTS FOR 2025 AND ABOVE</b></td>
							<td><b>(Db)</b></td>
							<td><b>(Cr)</b></td>
							<td><b>BALANCE</b></td>
						</tr>
						<tr align="center">
							<td><b>(RM)</b></td>
							<td><b>(RM)</b></td>
							<td><b>(RM)</b></td>
							
						</tr>
						<?php 
						$no=1;
						$showBFDate = "";
						if($firstYearDate != ""){
							$showBFDate = date("d/m/Y",strtotime($firstYearDate));
						}
						
					
						?>
						<tr>
							<td align="center"><?php echo $no?></td>
							<td align="center"><?php echo $showBFDate;?></td>
							<td>&nbsp;</td>
							<td>B/F</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							<td align="right"><?php echo number_format($totalBF_water_new,2);?></td>
							
						</tr>
						<?php 
						//$no=1;
						$totalPaid = 0;
						$totalUnpaid = 0;
						$totalPaidList = "";
						$totalUnpaidList = "";
						$totalBaki = $totalBF_water_new;
						$bill = 0;
						$payment = 0;
						$kiraan30Percent = 0;
						$total_kiraan30Percent = 0;
						$month = "";
						$year = "";
						$slashPosition = 0;
						$RP_rentalMonth = "";
						
						$layak = "";
						$bill_id_smartsewa = "";
						$rekod_bacaan = "";
						while($resultStatement = mysqli_fetch_array($runSqlStatementWater_new)){
							include("statement_resultQuery.php");
							//check ref no if exist
							if (strpos($info, 'payment') !== false) {
								include("paymentRefNoInfo.php");
								$rekod_bacaan = "";
							}
							else if(strpos($info, 'bill') !== false) {
								$paymentInfoShow = "";
								if (strpos($period, '/') !== false) {
									 "rekod_bacaan:: " . $rekod_bacaan = " (Tarikh Bacaan: $date_reading_before - $date_reading_current, Kegunaan: $bacaan m3)";
								}
							}
							
							
							
							?>
						<tr>
							<td align="center"><?php echo $no?></td>
							<td align="center"><?php echo $date_created?></td>
							<td><?php echo $id?></td>
							<td><?php echo $pay_info  . $paymentInfoShow . $rekod_bacaan?></td>
							<?php if (strpos($info, 'bill') !== false) { 
									
							?>
							<td align="right"><?php echo number_format($amount,2)?></td>
							<td align="right"></td>
							<td align="right"><?php echo number_format($totalBaki,2)?></td>
							
							<?php 
									
							} if (strpos($info, 'payment') !== false) { 
									
							?>
							<td align="right"></td>
							<td align="right"><?php echo number_format($amount,2)?></td>
							<td align="right"><?php echo number_format($totalBaki,2)?></td>
							
						</tr>
						<?php
							//if($totalBF_water != 0)
								//$totalBF_water =0; 
							}
						
					}
						?>
						<tr>
							<td align="right" colspan = "4"><b>TOTAL (RM)</b></td>
							<td align="right"><b><?php echo number_format($totalUnpaid,2) ?></b></td>
							<td align="right"><b><?php echo number_format($totalPaid,2) ?></b></td>
							<td align="right"><b><?php echo number_format($totalBaki,2)?></b></td>
							
						</tr>
					</table>
				</div>
			</div>
			<?php } 
			else{
				?>
				<table class="table table-bordered table-hover table-sm" id="dataTable" width="100%" cellspacing="0" style="font-size: 12px">
					<tr>
						<td align="center" colspan = "7" ><b>~No Record Found~</b></td>
						
					</tr>
				</table>
				<?PHP
			}?>
			<!--2025 dan ke ATAS END-->
			<?PHP
			if($countRowOthers > 0){
			?>
			<div class="card-header py-3">
				<h5 class="m-0 font-weight-bold text-primary">OTHERS PAYMENT INFO (<?php echo $bill_type_name?>)</h5>
			</div>
			<div class="card-body">
				<div class="table-responsive">
					<table class="table table-bordered table-hover table-sm" id="dataTable" width="100%" cellspacing="0" style="font-size: 12px">
						<tr align="center">
							<td rowspan="2"><b>#</b></td>
							<td rowspan="2"><b>DATE</b></td>
							<td rowspan="2"><b>INVOICE / RECEIPT NO</b></td>
							<td rowspan="2"><b>DESCRIPTION</b></td>
							<td><b> (Db)</b></td>
							<td><b>(Cr)</b></td>
							<td><b>BALANCE</b></td>
							
						</tr>
						<tr align="center">
							<td><b>(RM)</b></td>
							<td><b>(RM)</b></td>
							<td><b>(RM)</b></td>
							
						</tr>
						<?php 
						$no=1;
						$showBFDate = "";
						if($firstYearDate != ""){
							$showBFDate = date("d/m/Y",strtotime($firstYearDate));
						}
						?>
						<tr>
							<td align="center"><?php echo $no?></td>
							<td align="center"><?php echo $showBFDate;?></td>
							<td>&nbsp;</td>
							<td>B/F</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							<td align="right"><?php echo number_format($totalBF_water,2);?></td>
							
						</tr>
						<?php 
						//$no=1;
						$totalPaid = 0;
						$totalUnpaid = 0;
						$totalPaidList = "";
						$totalUnpaidList = "";
						$totalBaki = $totalBF_water;
						$bill = 0;
						$payment = 0;
						$kiraan30Percent = 0;
						$total_kiraan30Percent = 0;
						$month = "";
						$year = "";
						$slashPosition = 0;
						$RP_rentalMonth = "";
						
						$layak = "";
						$bill_id_smartsewa = "";
						while($resultStatement = mysqli_fetch_array($runSqlStatementOthers)){
							include("statement_resultQuery.php");
							//check ref no if exist
							if (strpos($info, 'payment') !== false) {
								include("paymentRefNoInfo.php");
							}
							else if(strpos($info, 'bill') !== false) {
								$paymentInfoShow = "";
							}
							?>
						<tr>
							<td align="center"><?php echo $no?></td>
							<td align="center"><?php echo $date_created?></td>
							<td><?php echo $id?></td>
							<td><?php echo $pay_info  . $paymentInfoShow ?></td>
							<?php if (strpos($info, 'bill') !== false) { 
									
							?>
							<td align="right"><?php echo number_format($amount,2)?></td>
							<td align="right"></td>
							<td align="right"><?php echo number_format($totalBaki,2)?></td>
							
							<?php 
									
							} if (strpos($info, 'payment') !== false) { 
									
							?>
							<td align="right"></td>
							<td align="right"><?php echo number_format($amount,2)?></td>
							<td align="right"><?php echo number_format($totalBaki,2)?></td>
							
						</tr>
						<?php
							//if($totalBF_water != 0)
								//$totalBF_water =0; 
							}
						
					}
						?>
						<tr>
							<td align="right" colspan = "4"><b>TOTAL (RM)</b></td>
							<td align="right"><b><?php echo number_format($totalUnpaid,2) ?></b></td>
							<td align="right"><b><?php echo number_format($totalPaid,2) ?></b></td>
							<td align="right"><b><?php echo number_format($totalBaki,2)?></b></td>
							
						</tr>
					</table>
				</div>
			</div>
			<?php }
			?>
			<div class="card-header py-3">
				<h5 class="m-0 font-weight-bold text-primary">OUTSTANDING INFO <?PHP echo " UNTIL " . date("d/m/Y",strtotime($cutOffDate))?></h5>
			</div>
			<div class="card-body">
				<div class="table-responsive">
					<table class="table table-bordered table-hover table-sm" id="dataTable" width="100%" cellspacing="0" style="font-size: 12px">
						<tr align="center">
							<td ><b>#</b></td>
							<td><b>INVOICE</b></td>
							<td><b>DATE</b></td>
							<td><b>TOTAL</b></td>
							<td><b>0-30 DAYS</b></td>
							<td><b>31-60 DAYS</b></td>
							<td><b>61-90 DAYS</b></td>
							<td><b>90+ DAYS</b></td>
						</tr>
						<?php 
						
								$runSqlAgeing = mysqli_query($link, $sqlAgeing);
								if($runSqlAgeing === FALSE){
									die(mysqli_error($link));
								}
								
								$bil = 1;
								$bill_ball30 = 0;
								$bill_ball3160 = 0;
								$bill_ball6190 = 0;
								$bill_ball91 = 0;
								$total_billBal = 0;
								$resultSqlAgeing = array();
								while($resultSqlAgeing = mysqli_fetch_array($runSqlAgeing)){
									if($resultSqlAgeing['id'] != null || $resultSqlAgeing['id'] != ""){
										$bill_bal = $resultSqlAgeing['total_outstanding'];
										$invDate = $resultSqlAgeing['issue_date'];
										$invDate = date("Y-m-d",strtotime($resultSqlAgeing['issue_date']));
										$aged_date = date("Y-m-d", strtotime($ageingDate));
										$start_date = $invDate;
										$end_date = $cutOffDate;
										// call dateDifference() function to find the number of days between two dates
										$diff = dateDifference($start_date, $end_date);
										$invDate = date("d/m/Y",strtotime($resultSqlAgeing['issue_date']));
									
								?>
						<tr>
							<td align="center"><b><?php echo $bil; ?></b></td>
							<td align="center"><b><?php echo $resultSqlAgeing['bill_no']; ?></b></td>
							<td align="center"><b><?php echo $invDate; ?></b></td>
							<td align="center"><b><?php echo number_format($bill_bal,2); ?></b></td>
							<td align="center">
								<b><?php 
										if($diff <= 30){
											echo number_format($bill_bal,2);
											$bill_ball30 += $bill_bal;
										}
									?>
								</b>
							</td>
							<td align="center">
								<b><?php 
										if($diff >= 31 && $diff <= 60){
											echo number_format($bill_bal,2);
											$bill_ball3160 += $bill_bal;
										}
									?>
								</b>
							</td>
							<td align="center">
								<b><?php 
										if($diff >= 61 && $diff <= 90){
											echo number_format($bill_bal,2);
											$bill_ball6190 += $bill_bal;
										}
									?>
								</b>
							</td>
							<td align="center">
								<b><?php 
										if($diff > 90){
											echo number_format($bill_bal,2);
											$bill_ball91 += $bill_bal;
										}
									?>
								</b>
							</td>
						</tr>
							<?php
										$bil++;
										$total_billBal += $bill_bal;
									}
								}
							?>
						<tr>
							<td align="center" colspan = "3"><b>TOTAL (RM): </b></td>
							<td align="center"><b><?php echo number_format($total_billBal,2);?></b></td>
							<td align="center"><b><?php echo number_format($bill_ball30,2);?></b></td>
							<td align="center"><b><?php echo number_format($bill_ball3160,2);?></b></td>
							<td align="center"><b><?php echo number_format($bill_ball6190,2);?></b></td>
							<td align="center"><b><?php echo number_format($bill_ball91,2);?></b></td>
						</tr>
					</table>
				</div>
			</div>
		</div>
			
		<div class="card-footer">
			<button type="button" class="btn btn-outline-primary btn-sm" onclick="printStatement('<?php echo $tenant_id;?>','<?php echo $_SESSION["statementby"]?>','<?php echo $_SESSION["statement_year"]?>','<?php echo $_SESSION["statement_monthyear"]?>','<?php echo $_SESSION["dateFrom"]?>','<?php echo $_SESSION["dateTo"]?>','<?php echo $_SESSION["statement_ChooseDate"]?>','<?php echo $_SESSION["cutOffPayment"]?>')" class="btn btn-outline-warning btn-sm">
				<i class="fa fa-print"></i>&nbsp; PRINT STATEMENT
			</button>
			<button type="button" class="btn btn-outline-primary btn-sm" onclick="exportToCSV('<?php echo $tenant_id;?>','<?php echo $_SESSION["statementby"]?>','<?php echo $_SESSION["statement_year"]?>','<?php echo $_SESSION["statement_monthyear"]?>','<?php echo $_SESSION["dateFrom"]?>','<?php echo $_SESSION["dateTo"]?>','<?php echo $_SESSION["statement_ChooseDate"]?>','<?php echo $_SESSION["cutOffPayment"]?>')" class="btn btn-outline-warning btn-sm">
				<i class="fa fa-table"></i>&nbsp; EXPORT TO CSV
			</button>
		</div>
	</div>
</div>

<SCRIPT>
function statementby_change(){
	
	var statementby = document.getElementById("statementby").value;
	
	//alert(statementby);
	 if(statementby != ""){
		//document.getElementById("statement_option").style.display ='block';
		if(statementby == "statement_year"){
			document.getElementById('statement_ByYear').style.display ='block';
			document.getElementById('statement_ByMonthYear').style.display ='none';
			document.getElementById('statement_ByDate').style.display ='none';
			document.getElementById('statement_ByChooseDate').style.display ='none';
			document.getElementById('statement_cutOff').style.display ='block';
			
		}
		else if(statementby == "statement_monthyear"){
			document.getElementById('statement_ByYear').style.display ='none';
			document.getElementById('statement_ByMonthYear').style.display ='block';
			document.getElementById('statement_ByDate').style.display ='none';
			document.getElementById('statement_ByChooseDate').style.display ='none';
			document.getElementById('statement_cutOff').style.display ='block';
			
		}
		else if(statementby === 'statement_date'){
			//alert("masuk >> " + statementby);
			document.getElementById('statement_ByYear').style.display ='none';
			document.getElementById('statement_ByMonthYear').style.display ='none';
			document.getElementById('statement_ByDate').style.display ='block';
			document.getElementById('statement_ByChooseDate').style.display ='none';
			document.getElementById('statement_cutOff').style.display ='block';
		}
		else if(statementby == "statement_ChooseDate"){
			document.getElementById('statement_ByYear').style.display ='none';
			document.getElementById('statement_ByMonthYear').style.display ='none';
			document.getElementById('statement_ByDate').style.display ='none';
			document.getElementById('statement_ByChooseDate').style.display ='block';
			document.getElementById('statement_cutOff').style.display ='block';
			
		} 
		
	} 
	
	
	
}
</SCRIPT>