<?php 
$csv_Deposit_header = ""; // header csv
$csv_Deposit_payment_table1 = "";// table deposits
$csv_Deposit_payment_table2 = ""; // table deposit_bills & deposit_payments
$csv_Deposit_payment_table3 = "";// table deposit_bill_other_receipts & deposit_payment_other_receipts

$csv_Deposit_header .= 'MAKLUMAT BAYARAN ' . $report_type . ' ' . $_SESSION["projectLocation"]  ;
$csv_Deposit_header .= "\n";
$csv_Deposit_header .= "\n";

//maklumat deposit Payment on 2024 and below - start
$csv_Deposit_payment_table1 .= "Deposit Payment (Payment on 2024 and below)";
$csv_Deposit_payment_table1 .= "\n";
$csv_Deposit_payment_table1 .= '#, Tenant Name , Unit No., Receipt No.  , Date Paid , Two Month Rental (RM), Utilities (RM), Management Fee (RM), Total (RM) ';
$csv_Deposit_payment_table1 .= "\n";

$no = 1;
$total2monthRental = 0;
$totalUtilities = 0;
$totalMgtFree = 0;
$totalAllDeposit = 0;
while($resultDeposit_oldRecord = mysqli_fetch_array($runDeposit_oldRecord)){
										
	$tenant_name = $resultDeposit_oldRecord["tenant_name"];
	$unit_no = $resultDeposit_oldRecord["unit_no"];
	$receipt_no = $resultDeposit_oldRecord["receipt_no"];
	$one_month_rental = $resultDeposit_oldRecord["one_month_rental"];
	$one_month_date = $resultDeposit_oldRecord["one_month_date"];
	$two_month_rental = $resultDeposit_oldRecord["two_month_rental"];
	$utilities = $resultDeposit_oldRecord["utilities"];
	$management_fee = $resultDeposit_oldRecord["management_fee"];
	$total_deposit = $resultDeposit_oldRecord["total_deposit"];
	$created_at = date("d/m/Y",strtotime($resultDeposit_oldRecord["created_at"]));
	$deposit_id = $resultDeposit_oldRecord["id"];
	$tenant_id = $resultDeposit_oldRecord["tenant_id"];
	$total_deposit = $two_month_rental + $utilities + $management_fee;
										
	$csv_Deposit_payment_table1 .= $no ."," . $tenant_name .",".  $unit_no  .",". $receipt_no ."," . $created_at ."," . $two_month_rental . "," . $utilities ."," . $management_fee ."," . $total_deposit ;
	$csv_Deposit_payment_table1 .= "\n";
	$total2monthRental += $two_month_rental;
	$totalUtilities += $utilities;
	$totalMgtFree += $management_fee;

	$totalAllDeposit += $total_deposit;
	$no++;	
}
$csv_Deposit_payment_table1 .= "TOTAL (RM),,,,," . $total2monthRental . "," . $totalUtilities . ",". $totalMgtFree . ",". $totalAllDeposit ;
$csv_Deposit_payment_table1 .= "\n";
$csv_Deposit_payment_table1 .= "\n";
//maklumat deposit Payment on 2024 and below - end

//maklumat deposit Payment on 2024 onwards - start

$csv_Deposit_payment_table2 .= "Deposit Payment (Payment on 2024 onwards)";
$csv_Deposit_payment_table2 .= "\n";
$csv_Deposit_payment_table2 .= '#, Tenant Name , Unit No., Receipt No.  , Date Paid , Two Month Rental (RM), Utilities (RM), Total (RM), Invoice No.';
$csv_Deposit_payment_table2 .= "\n";

$no = 1;
$total2monthRental_paid = 0;
$totalUtilities_paid = 0;
$totalAllDeposit = 0;
while($resultDeposit_newRecord = mysqli_fetch_array($runDeposit_newRecord)){
	$deposit_id = $resultDeposit_newRecord["deposit_id"];
	$receipt_no = $resultDeposit_newRecord["receipt_no"];
	$two_months_amount_paid = $resultDeposit_newRecord["two_months_amount_paid"];
	$utility_amount_paid = $resultDeposit_newRecord["utility_amount_paid"];
	$created_at = date("d/m/Y",strtotime($resultDeposit_newRecord["created_at"]));
	$tenant_name = $resultDeposit_newRecord["tenant_name"];
	$unit_no = $resultDeposit_newRecord["unit_no"];
	$tenant_id = $resultDeposit_newRecord["tenant_id"];
	$deposit_bill_no = $resultDeposit_newRecord["deposit_bill_no"];
	$total_deposit = $two_months_amount_paid + $utility_amount_paid ;

	$csv_Deposit_payment_table2 .= $no ."," . $tenant_name .",".  $unit_no  .",". $receipt_no ."," . $created_at ."," . $two_month_rental . "," . $utilities ."," . $total_deposit ."," . $deposit_bill_no ;
	$csv_Deposit_payment_table2 .= "\n";
	$total2monthRental_paid += $two_months_amount_paid;
	$totalUtilities_paid += $utility_amount_paid;
	$totalAllDeposit += $total_deposit;
	$no++;

}
$csv_Deposit_payment_table2 .= "TOTAL (RM),,,,," . $total2monthRental_paid . "," . $totalUtilities_paid . "," . $totalAllDeposit;
$csv_Deposit_payment_table2 .= "\n";
$csv_Deposit_payment_table2 .= "\n";
//maklumat deposit Payment on 2024 onwards - end

//maklumat Others Payment (Pelbagai) - start

$csv_Deposit_payment_table3 .= "Deposit Payment (Payment on 2024 onwards)";
$csv_Deposit_payment_table3 .= "\n";
$csv_Deposit_payment_table3 .= '#, Tenant Name , Unit No., Receipt No.  , Date Paid , Yuran Pelbagai (RM), SST (RM), Stamping (RM), Total (RM), Invoice No.';
$csv_Deposit_payment_table3 .= "\n";

$no = 1;
$total_yp_paid = 0;
$total_sst_paid = 0;
$total_stamping_paid = 0;
$totalAllDepositOthers = 0;
while($resultDepositPaymentOthers = mysqli_fetch_array($runDepositPaymentOthers)){
	$deposit_id = $resultDepositPaymentOthers["deposit_id"];
	$receipt_no = $resultDepositPaymentOthers["receipt_no"];
	$yp_paid = $resultDepositPaymentOthers["yp_paid"];
	$sst_paid  = $resultDepositPaymentOthers["sst_paid"];
	$stamping_paid  = $resultDepositPaymentOthers["stamping_paid"];
	$tenant_name = $resultDepositPaymentOthers["tenant_name"];
	$unit_no = $resultDepositPaymentOthers["unit_no"];
	$tenant_id = $resultDepositPaymentOthers["tenant_id"];
	$deposit_other_bill_no = $resultDepositPaymentOthers["deposit_other_bill_no"];
	$deposit_bill_id = $resultDepositPaymentOthers["deposit_bill_id"];
	$created_at = date("d/m/Y",strtotime($resultDepositPaymentOthers["created_at"]));
	$total_deposit = $yp_paid + $sst_paid + $stamping_paid;

	$csv_Deposit_payment_table3 .= $no ."," . $tenant_name .",".  $unit_no  .",". $receipt_no ."," . $created_at ."," . $yp_paid . "," . $sst_paid ."," . $stamping_paid ."," . $total_deposit ."," . $deposit_other_bill_no ;
	$csv_Deposit_payment_table3 .= "\n";
	$yp_paid_total += $yp_paid;
	$sst_paid_total += $sst_paid;
	$stamping_paid_total += $stamping_paid;
	$totalAllDepositOthers += $total_deposit;
	$no++;

}
$csv_Deposit_payment_table3 .= "TOTAL (RM),,,,," . $yp_paid_total . "," . $sst_paid_total .  "," .  $stamping_paid_total .  "," .  $totalAllDepositOthers  ;
$csv_Deposit_payment_table3 .= "\n";
$csv_Deposit_payment_table3 .= "\n";
//maklumat Others Payment (Pelbagai)  - end

//combine
echo  $csv_Deposit_header . $csv_Deposit_payment_table1 . $csv_Deposit_payment_table2 . $csv_Deposit_payment_table3;
?>