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



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

$kiraan30percent_fromBF = 0;
$totalBF_rental = 0;
$totalBF_water = 0;
$firstYearDate = "";
$tenant_id = $_GET["tenant_id"];
	//GET TENANT INFO
	include ("tenantAndDeposit_statementInfo.php");
	
	$cutOffDate = date("Y-m-d");
	//GET STATEMENT INFO FROM BILL AND PAYMENT TABLE 
	
	//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
	
	include("query_statement.php");
	
	
	//untuk dapatkan b/f berakhir 2024 
	include("water_bf_end2024.php");

	
	$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($runSqlStatementOthers === FALSE){
		die(mysqli_error($link));
	}
	
	if($runSqlStatementRental === FALSE){
		die(mysqli_error($link));
	}
	if($runSqlStatementWater_old === FALSE){
		die(mysqli_error($link));
	}
	if($runSqlStatementWater_new === 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){
		 "STATEMENT OTHERS SINI:: " . $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));
		}
	}
	

	
	$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'";
		}
	}
	
	
$timestamp = date('Y-m-d');
$csv_header = "";
$csv_headerPenyata = "";
$csv_headerDeposit = "";
$csv_headerSewaBulanan = "";
$csv_headerTunggakan = "";
$csv_headerTotalBF = "";
$filename = 'Export_excel_' . strtoupper($location). "_" . strtoupper($tenant_name) . strtoupper($company_name) . "_" . strtoupper($unit_no). "_" . $timestamp . '.csv';
header("Content-Type: application/csv");
header("Content-Disposition: attachment; filename=\"$filename\"");

//maklumat penyewa
$csv_headerPenyata .= 'TENANT INFO';
$csv_headerPenyata .= "\n";
$csv_headerPenyata .= 'Statement No.: ' . $penyata_id;
$csv_headerPenyata .= "\n";
$csv_headerPenyata .= 'Date: ' . date("d/m/Y");
$csv_headerPenyata .= "\n";
$csv_headerPenyata .= 'Tenant ID: ' . $tenant_id;
$csv_headerPenyata .= "\n";
$csv_headerPenyata .= 'Tenant Name: ' . $tenant_name;
$csv_headerPenyata .= "\n";
$csv_headerPenyata .= 'NRIC: ' . $tenant_ic;
$csv_headerPenyata .= "\n";
$csv_headerPenyata .= 'Contact No.: ' . $contact_no;
$csv_headerPenyata .= "\n";
$csv_headerPenyata .= 'Type Of Property: ' . $project_name;
$csv_headerPenyata .= "\n";
$csv_headerPenyata .= 'Property Location: ' . $maklumatHartanah;
$csv_headerPenyata .= "\n";
$csv_headerPenyata .= 'Rental Start: ' . $start_rent;
$csv_headerPenyata .= "\n";
$csv_headerPenyata .= 'Rental End: ' . $end_rent;
$csv_headerPenyata .= "\n";

$csv_headerPenyata .='--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------';
$csv_headerPenyata .= "\n";
//end maklumat penyewa
//maklumat deposit
$csv_headerDeposit .= 'MAKLUMAT DEPOSIT';
$csv_headerDeposit .= "\n";
$csv_headerDeposit .= ' Deposit Payment (Payment below September 2024) ';
$csv_headerDeposit .= "\n";
$csv_headerDeposit .= '#, Date Paid, Receipt No., Two Months Rental , Utilities,  Management Fee, Total  ';
$csv_headerDeposit .= "\n";

$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;
		
		
		$csv_headerDeposit .= $no . ',';
		$csv_headerDeposit .= date("d/m/Y",strtotime($created_at)) . ',';
		$csv_headerDeposit .= $receipt_no . ',';
		$csv_headerDeposit .= $two_month_rental . ',';
		$csv_headerDeposit .= $utilities . ',';
		$csv_headerDeposit .= $management_fee . ',';
		$csv_headerDeposit .= $total_deposit . '';

		$csv_headerDeposit .= "\n";




	}
}
else if($countRowOldRecord == 0){
	$csv_headerDeposit .= "~ No Record Found ~";
	$csv_headerDeposit .= "\n";
	$csv_headerDeposit .= "\n";
}
$no = 1;
if($countStatementDeposit_newRecord > 0){
 $depo_bf = 0.00;
 $depo_baki = 0.00;
 $depo_totalDb = 0.00;
 $depo_totalCr = 0.00;
	
	$csv_headerDeposit .= "\n";
	$csv_headerDeposit .= ' Deposit Payment (Payment on September 2024 and above) ';
	$csv_headerDeposit .= "\n";
	$csv_headerDeposit .= '#, Date, Invoice / Receipt , Description,  (Db), (Cr) , Balance ';
	$csv_headerDeposit .= "\n";
	$csv_headerDeposit .= ', , , , RM, RM , RM ';
	$csv_headerDeposit .= "\n";
	$csv_headerDeposit .=  $no . ' , , , B/F , , , 0.00';
	$csv_headerDeposit .= "\n";
 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;
	}
	
	$csv_headerDeposit .= $no . ',';
	$csv_headerDeposit .= date("d/m/Y",strtotime($created_at)) . ',';
	$csv_headerDeposit .= $receipt_no . ',';
	if (strpos($info_two_months_amount, 'deposit_bill') !== false) {
		$csv_headerDeposit .= $description_2MonthRental . ',';
		$csv_headerDeposit .= $two_month_rental_amount_bill . ',';
		$csv_headerDeposit .=  ',';
		$csv_headerDeposit .= $depo_baki . ',';
		$csv_headerDeposit .= "\n";
	}
	if (strpos($info_utility_amount, 'deposit_bill') !== false) {
		$csv_headerDeposit .=  ',';
		$csv_headerDeposit .=  ',';
		$csv_headerDeposit .=  ',';
		$csv_headerDeposit .= $description_utility . ',';
		$csv_headerDeposit .= $utility_amount_bill. ',';
		$csv_headerDeposit .=  ',';
		$csv_headerDeposit .=  ',';
		$csv_headerDeposit .= "\n";
	}
	if (strpos($info_two_months_amount, 'deposit_payment') !== false) {
		$csv_headerDeposit .= $description_2MonthRental . ',';
		$csv_headerDeposit .=  ',';
		$csv_headerDeposit .= $two_month_rental_amount_bill . ',';
		$csv_headerDeposit .= $depo_baki . ',';
		$csv_headerDeposit .= "\n";
	 }
	if (strpos($info_utility_amount, 'deposit_payment') !== false) {
		$csv_headerDeposit .=  ',';
		$csv_headerDeposit .=  ',';
		$csv_headerDeposit .=  ',';
		$csv_headerDeposit .= $description_utility . ',';
		$csv_headerDeposit .=  ',';
		$csv_headerDeposit .= $utility_amount_bill. ',';
		$csv_headerDeposit .=  ',';
		$csv_headerDeposit .= "\n"; 
	  }
	
	$csv_headerDeposit .= ",";
	$csv_headerDeposit .=  ',';
	$csv_headerDeposit .=  ',';
	$csv_headerDeposit .=  'Total (RM)' . ',';;
	$csv_headerDeposit .= $depo_totalDb . ',';
	$csv_headerDeposit .= $depo_totalCr . ',';
	$csv_headerDeposit .= $depo_baki. ',';
	$csv_headerDeposit .= "\n";
 }	
}
else{
	$csv_headerDeposit .= "~ No Record Found ~";
	$csv_headerDeposit .= "\n";
}
$no = 1;
if($countRowPaymentOthers > 0){
 $depoOthers_bf = 0.00;
 $depoOthers_baki = 0.00;
 $depoOthers_totalDb = 0.00;
 $depoOthers_totalCr = 0.00;

$csv_headerDeposit .= "\n";
$csv_headerDeposit .= ' Others Payment ';
$csv_headerDeposit .= "\n";
$csv_headerDeposit .= '#, Date, Invoice / Receipt , Description,  (Db), (Cr) , Balance ';
$csv_headerDeposit .= "\n";
$csv_headerDeposit .= ', , , , RM, RM , RM ';
$csv_headerDeposit .= "\n";
$csv_headerDeposit .=  $no . ' , , , B/F , , , 0.00';
$csv_headerDeposit .= "\n";

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;
	}
	
	$csv_headerDeposit .= $no . ',';
	$csv_headerDeposit .= date("d/m/Y",strtotime($created_at)) . ',';
	$csv_headerDeposit .= $receipt_no . ',';
	if (strpos($info_yp_paid, 'deposit_other_bill') !== false) {
		$csv_headerDeposit .= $description_YP . ',';
		$csv_headerDeposit .= $yp_charges_amount . ',';
		$csv_headerDeposit .=  ',';
		$csv_headerDeposit .= $depo_others_baki . ',';
		$csv_headerDeposit .= "\n";
	}
	if (strpos($info_sst_paid, 'deposit_other_bill') !== false) {
		$csv_headerDeposit .=  ',';
		$csv_headerDeposit .=  ',';
		$csv_headerDeposit .=  ',';
		$csv_headerDeposit .= $description_SST . ',';
		$csv_headerDeposit .= $sst_charges_amount . ',';
		$csv_headerDeposit .=  ',';
		$csv_headerDeposit .= ',';
		$csv_headerDeposit .= "\n";
	}
	if (strpos($info_stamping_paid, 'deposit_other_bill') !== false) {
		$csv_headerDeposit .=  ',';
		$csv_headerDeposit .=  ',';
		$csv_headerDeposit .=  ',';
		$csv_headerDeposit .= $description_Stamping . ',';
		$csv_headerDeposit .= $stamping_charges_amount . ',';
		$csv_headerDeposit .=  ',';
		$csv_headerDeposit .= ',';
		$csv_headerDeposit .= "\n";
	}
	 if (strpos($info_yp_paid, 'deposit_other_payment') !== false) {
		$csv_headerDeposit .= $description_YP . ',';
		$csv_headerDeposit .=  ',';
		$csv_headerDeposit .= $yp_paid . ',';
		$csv_headerDeposit .= $depo_others_baki;
		$csv_headerDeposit .= "\n";
	 }
	 if (strpos($info_sst_paid, 'deposit_other_payment') !== false) {
		$csv_headerDeposit .=  ',';
		$csv_headerDeposit .=  ',';
		$csv_headerDeposit .=  ',';
		$csv_headerDeposit .= $description_SST . ',';
		$csv_headerDeposit .=  ',';
		$csv_headerDeposit .= $sst_paid . ',';
		$csv_headerDeposit .=  ',';
		$csv_headerDeposit .= "\n";
	 }
	 if (strpos($info_stamping_paid, 'deposit_other_payment') !== false) {
		$csv_headerDeposit .=  ',';
		$csv_headerDeposit .=  ',';
		$csv_headerDeposit .=  ',';
		$csv_headerDeposit .= $description_Stamping . ',';
		$csv_headerDeposit .=  ',';
		$csv_headerDeposit .= $stamping_paid . ',';
		$csv_headerDeposit .=  ',';
		$csv_headerDeposit .= "\n";
	 }
}
$csv_headerDeposit .= ",";
$csv_headerDeposit .=  ',';
$csv_headerDeposit .=  ',';
$csv_headerDeposit .=  'Total (RM)'. ',';
$csv_headerDeposit .= $depo_others_totalDb. ',';
$csv_headerDeposit .= $depo_others_totalCr. ',';
$csv_headerDeposit .= $depo_others_baki. ',';	
$csv_headerDeposit .= "\n";

}
else{
	$csv_headerDeposit .= "~ No Record Found ~";
	$csv_headerDeposit .= "\n";
}

$csv_headerDeposit .= "\n";
$csv_headerDeposit .= '--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------';
$csv_headerDeposit .= "\n";
//end maklumat deposit
//maklumat bayaran sewa bulanan
$csv_headerSewaBulanan .= 'RENTAL INFO' ;
$csv_headerSewaBulanan .= "\n";
$csv_headerSewaBulanan .= '#, Date, Invoice / Receipt , Description,  (Db), (Cr) , Balance ';

 $no=1;
 $showBFDate = "";
 
if($firstYearDate != ""){
	$showBFDate = date("d/m/Y",strtotime($firstYearDate));
}

$csv_headerSewaBulanan .= "\n";
$csv_headerSewaBulanan .= ",";
$csv_headerSewaBulanan .= ",";
$csv_headerSewaBulanan .= ",";
$csv_headerSewaBulanan .= ",";
$csv_headerSewaBulanan .= "(RM) , (RM), (RM)";
$csv_headerSewaBulanan .= "\n";

$csv_headerSewaBulanan .= "\n";
$csv_headerSewaBulanan .= $no . ',';
$csv_headerSewaBulanan .= $showBFDate . ',';
$csv_headerSewaBulanan .= ",";
$csv_headerSewaBulanan .= 'B/F,';
$csv_headerSewaBulanan .= ",";
$csv_headerSewaBulanan .= ",";
$csv_headerSewaBulanan .= $totalBF_rental . ',';

$csv_headerSewaBulanan .= "\n";



$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");
	
	$csv_headerSewaBulanan .= $no . ',';
	$csv_headerSewaBulanan .= $date_created . ',';
	$csv_headerSewaBulanan .= $id . ',';
	$csv_headerSewaBulanan .= $pay_info . ',';
	 if (strpos($info, 'bill') !== false) { 
		
		$csv_headerSewaBulanan .= $amount . ',';
		$csv_headerSewaBulanan .=  ',';
		$csv_headerSewaBulanan .= $totalBaki . ',';
											
	} if (strpos($info, 'payment') !== false) { 
		
		$csv_headerSewaBulanan .= ',';
		$csv_headerSewaBulanan .=  $amount .',';
		if($totalBaki == 0){
			$csv_headerSewaBulanan .= $totalBaki . ',';	
		}else {
			$csv_headerSewaBulanan .= $totalBaki . ',';	
		}
		if($totalBF_rental != 0)
			$totalBF_rental =0; 
		}
	$csv_headerSewaBulanan .= "\n";
	}//END WHILE						
		
$csv_headerSewaBulanan .=  ',';
$csv_headerSewaBulanan .=  ',';
$csv_headerSewaBulanan .=  ',';
$csv_headerSewaBulanan .=  'Total (RM),';
$csv_headerSewaBulanan .= $totalUnpaid . ',';
$csv_headerSewaBulanan .= $totalPaid . ',';
//$csv_headerSewaBulanan .= $totalBaki . ',';

if($totalBaki == 0){
	$csv_headerSewaBulanan .= $totalBaki . ',';	
}else {
	$csv_headerSewaBulanan .= $totalBaki . ',';	
}


$csv_headerSewaBulanan .= "\n";
$csv_headerSewaBulanan .= "--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------";
$csv_headerSewaBulanan .= "\n";
$csv_headerSewaBulanan .= "\n";
//end maklumat sewa
//maklumat bill air
$csv_headerWaterBulanan .= 'WATER INFO' ;
$csv_headerWaterBulanan .= "\n";

$csv_headerWaterBulanan_old = "";
if($countRowWater_old > 0){
	$csv_headerWaterBulanan_old .= "\n";
	$csv_headerWaterBulanan_old .= '#, Date, Receipt No., Description Payments Water for 2024 and Below,  Amount Paid ';
	$csv_headerWaterBulanan_old .= "\n";
	$csv_headerWaterBulanan_old .= ",";
	$csv_headerWaterBulanan_old .= ",";
	$csv_headerWaterBulanan_old .= ",";
	$csv_headerWaterBulanan_old .= ",";
	$csv_headerWaterBulanan_old .= "(RM)";
	$csv_headerWaterBulanan_old .= "\n";
	 $no=1;
	while($resultStatement = mysqli_fetch_array($runSqlStatementWater_old)){
	include("statement_resultQuery.php");
		if($totalBaki == 0){
			$totalBaki = number_format($totalBaki);
		}
	$csv_headerWaterBulanan_old .= $no . ',';
	$csv_headerWaterBulanan_old .= $date_created . ',';
	$csv_headerWaterBulanan_old .= $id . ',';
	$csv_headerWaterBulanan_old .= $pay_info . ',';

	if (strpos($info, 'payment') !== false) { 
		
		$csv_headerWaterBulanan_old .=  $amount . ',';
		

		
	//	if($totalBF_water != 0)
			//$totalBF_water =0; 
		
	}
	$csv_headerWaterBulanan_old .= "\n";
	}//END WHILE


	$csv_headerWaterBulanan_old .=  ',';
	$csv_headerWaterBulanan_old .=  ',';
	$csv_headerWaterBulanan_old .=  ',';
	$csv_headerWaterBulanan_old .=  'Total (RM),';
	$csv_headerWaterBulanan_old .= $totalPaid . ',';
	$csv_headerWaterBulanan_old .= "\n";
	$csv_headerWaterBulanan_old .= "\n";
}








if($countRowWater_new > 0){
	$csv_headerWaterBulanan_new = "";
	$csv_headerWaterBulanan_new .= "\n";
	$csv_headerWaterBulanan_new .= '#, Date, Invoice / Receipt No., Description Water Billing And Payments For 2025 & Above,   (Db), (Cr) , Balance '; 
	
	 $no=1;
	 $showBFDate = "";
	 
	if($firstYearDate != ""){
		$showBFDate = date("d/m/Y",strtotime($firstYearDate));
	}

	$csv_headerWaterBulanan_new .= "\n";
	$csv_headerWaterBulanan_new .= ",";
	$csv_headerWaterBulanan_new .= ",";
	$csv_headerWaterBulanan_new .= ",";
	$csv_headerWaterBulanan_new .= ",";
	$csv_headerWaterBulanan_new .= "(RM),(RM),(RM)";
	$csv_headerWaterBulanan_new .= "\n";


	$csv_headerWaterBulanan_new .= "\n";
	$csv_headerWaterBulanan_new .= $no . ',';
	$csv_headerWaterBulanan_new .= $showBFDate . ',';
	$csv_headerWaterBulanan_new .= ",";
	$csv_headerWaterBulanan_new .= 'B/F,';
	$csv_headerWaterBulanan_new .= ",";
	$csv_headerWaterBulanan_new .= ",";
	//$csv_headerWaterBulanan_new .= number_format($totalBF_water_new) . ',';
	$csv_headerWaterBulanan_new .= $totalBF_water_new . ',';

	$csv_headerWaterBulanan_new .= "\n";



	$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)";
			}
		}
		if($totalBaki == 0){
			$totalBaki = number_format($totalBaki);
		}
	$csv_headerWaterBulanan_new .= $no . ',';
	$csv_headerWaterBulanan_new .= $date_created . ',';
	$csv_headerWaterBulanan_new .= $id . ',';
	$csv_headerWaterBulanan_new .= $pay_info  . $paymentInfoShow . $rekod_bacaan . ',';

	if (strpos($info, 'bill') !== false) { 
		
		$csv_headerWaterBulanan_new .= $amount . ',';
		$csv_headerWaterBulanan_new .=  ',';
		$csv_headerWaterBulanan_new .= $totalBaki . ',';
				
	} if (strpos($info, 'payment') !== false) { 
		
		$csv_headerWaterBulanan_new .=  ',';
		$csv_headerWaterBulanan_new .=  $amount . ',';
		//$csv_headerWaterBulanan .= $totalBaki . ',';
		if($totalBaki == 0){
			$csv_headerWaterBulanan_new .= $totalBaki . ',';	
		}else {
			$csv_headerWaterBulanan_new .= $totalBaki . ',';	
		}

		
	}
	$csv_headerWaterBulanan_new .= "\n";
	}//END WHILE



	$csv_headerWaterBulanan_new .=  ',';
	$csv_headerWaterBulanan_new .=  ',';
	$csv_headerWaterBulanan_new .=  ',';
	$csv_headerWaterBulanan_new .=  'Total (RM),';
	$csv_headerWaterBulanan_new .= $totalUnpaid . ',';
	$csv_headerWaterBulanan_new .= $totalPaid . ',';
	//$csv_headerWaterBulanan .= $totalBaki . ',';
	if($totalBaki == 0){
		$csv_headerWaterBulanan_new .= $totalBaki . ',';	
	}else {
		$csv_headerWaterBulanan_new .= $totalBaki . ',';	
	}
}
$csv_headerWaterBulanan_new .= "\n";

$csv_headerWaterBulanan = $csv_headerWaterBulanan . $csv_headerWaterBulanan_old . $csv_headerWaterBulanan_new;


$csv_headerWaterBulanan .= "--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------";
$csv_headerWaterBulanan .= "\n";
//end maklumat bill air
//maklumat tunggakan
$csv_headerTunggakan .= "OUTSTANDING INFO UNTIL " . date("d/m/Y", strtotime($cutOffDate));
$csv_headerTunggakan .= "\n";
$csv_headerTunggakan .= 'Bil., Invois ID#, Tarikh, Jumlah, 0-30 Hari, 31-60 Hari, 61-90 Hari, 90+ Hari';
$csv_headerTunggakan .= "\n";


$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']));
		 
		$start_date = $invDate;

		$end_date = $cutOffDate;
		 
		// call dateDifference() function to find the number of days between two dates
		$diff = dateDifference($start_date, $end_date);
		 

		//start - kira bilangan hari antara tarikh
		$invDate = date("d/m/Y",strtotime($resultSqlAgeing['issue_date']));
		$csv_headerTunggakan .= $bil. ',';
		$csv_headerTunggakan .= $resultSqlAgeing['id']. ',';
		$csv_headerTunggakan .= $invDate. ',';
		$csv_headerTunggakan .= $bill_bal. ',';
		if($diff <= 30){
			$csv_headerTunggakan .= $bill_bal. ',';
			$bill_ball30 += $bill_bal;
		}else{
			$csv_headerTunggakan .=  ',';
		}
		if($diff >= 31 && $diff <= 60){
			$csv_headerTunggakan .= $bill_bal. ',';
			$bill_ball3160 += $bill_bal;
		}
		else{
			$csv_headerTunggakan .=  ',';
		}
		if($diff >= 61 && $diff <= 90){
			$csv_headerTunggakan .= $bill_bal. ',';
			$bill_ball6190 += $bill_bal;
		}
		else{
			$csv_headerTunggakan .=  ',';
		}
		if($diff > 90){
			$csv_headerTunggakan .= $bill_bal. ',';
			$bill_ball91 += $bill_bal;
		}
		else{
			$csv_headerTunggakan .=  ',';
		}
		$bil++;
		$total_billBal += $bill_bal;
		$csv_headerTunggakan .= "\n";
	 }
}
$csv_headerTunggakan .= ",";
$csv_headerTunggakan .= ",";
$csv_headerTunggakan .= "Jumlah : " . ',';
$csv_headerTunggakan .= $total_billBal. ',';
$csv_headerTunggakan .= $bill_ball30. ',';
$csv_headerTunggakan .= $bill_ball3160. ',';
$csv_headerTunggakan .= $bill_ball6190. ',';
$csv_headerTunggakan .= $bill_ball91. ',';
$csv_headerTunggakan .= "\n";

//echo $echoOutput . $csv_headerPenyata . $csv_headerDeposit . $csv_headerSewaBulanan  . $csv_headerWaterBulanan . $csv_headerTunggakan ;
echo  $csv_headerPenyata . $csv_headerDeposit . $csv_headerSewaBulanan  . $csv_headerWaterBulanan . $csv_headerTunggakan ;
exit;
?>

