<?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
	
	//echo "projectName111: ".$_SESSION["projectName"];
if($statementby  == ""){
	$sqlStatementRental ="SELECT * FROM ( 
						(SELECT payments.id as payment_id,payments.receipt_no, payments.amount_paid as amount, bills.issue_date as issue_date, payments.created_at as date_created,  bills.payment_status as status_payment, concat(payments.receipt_no,' payment ',payments.amount_paid) as info FROM payments
						inner join bills on payments.bill_id = bills.id
						where bills.tenant_id = $tenant_id and bills.bill_type_id = 1) 
						UNION ALL (SELECT bills.id, bills.bill_no, bills.bill_amount as amount, bills.issue_date as issue_date, bills.issue_date as date_created, bills.payment_status as status_payment, concat(bills.bill_no,' bill ',bill_amount) as info  FROM bills 
						where bills.tenant_id = $tenant_id  and bills.bill_type_id = 1 and bills.issue_date <= '$issueDate_cutOff') ) 
						results ORDER BY date_created ASC";
	
	$sqlStatementWater ="SELECT * FROM ( 
						(SELECT payments.id as payment_id,payments.receipt_no, payments.amount_paid as amount, bills.issue_date as issue_date, payments.created_at as date_created,  bills.payment_status as status_payment, concat(payments.receipt_no,' payment ',payments.amount_paid) as info FROM payments
						inner join bills on payments.bill_id = bills.id
						where bills.tenant_id = $tenant_id and bills.bill_type_id = 2) 
						UNION ALL (SELECT bills.id, bills.bill_no, bills.bill_amount as amount, bills.issue_date as issue_date, bills.issue_date as date_created, bills.payment_status as status_payment, concat(bills.bill_no,' bill ',bill_amount) as info  FROM bills 
						where bills.tenant_id = $tenant_id  and bills.bill_type_id = 2) ) 
						results ORDER BY date_created ASC";
	
	$sqlAgeing = " SELECT * FROM ( 
							(select  bills.bill_amount- sum( payments.amount_paid ) as total_outstanding , bills.*,payments.created_at as payment_date, properties.unit_no, tenants.tenant_name 
							from payments 
							inner join bills on payments.bill_id = bills.id 
							inner join tenants on bills.tenant_id =  tenants.id  
							
							inner join properties on tenants.property_id = properties.id
							inner join locations on properties.location_id = locations.id
							where bills.tenant_id =$tenant_id  and bills.issue_date <= '$cutOffDate' and bills.bill_type_id = 1 group by bills.issue_date having total_outstanding <> 0  ) 
							UNION  
								(select sum(bills.bill_amount) as total_outstanding, bills.* , bills.created_at as  payment_date,  properties.unit_no, tenants.tenant_name 
									from bills 
									inner join tenants on bills.tenant_id =  tenants.id  
									
									inner join properties on tenants.property_id = properties.id
									inner join locations on properties.location_id = locations.id
									where bills.issue_date  < '$cutOffDate'
									and bills.id not in (select payments.bill_id from payments inner join bills on payments.bill_id = bills.id WHERE bills.tenant_id = $tenant_id )
									and tenant_id = $tenant_id  
									and bills.bill_type_id = 1
									group by issue_date having total_outstanding <> 0 and bills.issue_date < CURRENT_DATE)
							) results group by issue_date ORDER BY issue_date ASC";
	
}
ELSE{
	include("query_statement.php");
}	
	$runSqlStatementRental = mysqli_query($link, $sqlStatementRental);
	$runSqlStatementWater = mysqli_query($link, $sqlStatementWater);
	
	if($runSqlStatementRental === FALSE){
		die(mysqli_error($link));
	}
	if($runSqlStatementWater === 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 .= number_format($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 .= number_format($totalBaki) . ',';	
}else {
	$csv_headerSewaBulanan .= $totalBaki . ',';	
}


$csv_headerSewaBulanan .= "\n";
$csv_headerSewaBulanan .= "--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------";
$csv_headerSewaBulanan .= "\n";
//end maklumat sewa
//maklumat bill air
$csv_headerWaterBulanan .= 'WATER INFO' ;
$csv_headerWaterBulanan .= "\n";
$csv_headerWaterBulanan .= '#, Date, Invoice / Receipt , Description,  (Db), (Cr) , Balance ';

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

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

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

$csv_headerWaterBulanan .= "\n";



$totalPaid = 0;
$totalUnpaid = 0;
$totalPaidList = "";
$totalUnpaidList = "";
$totalBaki = $totalBF_water;
$bill = 0;
$payment = 0;
$kiraan30Percent = 0;
$total_kiraan30Percent = 0;
$month = "";
$year = "";
$slashPosition = 0;
$RP_rentalMonth = "";

$layak = "";
$bill_id_smartsewa = "";
while($resultStatement = mysqli_fetch_array($runSqlStatementWater)){
	include("statement_resultQuery.php");
	if($totalBaki == 0){
		$totalBaki = number_format($totalBaki);
	}
$csv_headerWaterBulanan .= $no . ',';
$csv_headerWaterBulanan .= $date_created . ',';
$csv_headerWaterBulanan .= $id . ',';
$csv_headerWaterBulanan .= $pay_info . ',';

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

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



$csv_headerWaterBulanan .=  ',';
$csv_headerWaterBulanan .=  ',';
$csv_headerWaterBulanan .=  ',';
$csv_headerWaterBulanan .=  'Total (RM),';
$csv_headerWaterBulanan .= $totalUnpaid . ',';
$csv_headerWaterBulanan .= $totalPaid . ',';
//$csv_headerWaterBulanan .= $totalBaki . ',';
if($totalBaki == 0){
	$csv_headerWaterBulanan .= number_format($totalBaki) . ',';	
}else {
	$csv_headerWaterBulanan .= $totalBaki . ',';	
}
$csv_headerWaterBulanan .= "\n";
$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;
?>

