<!DOCTYPE html>
<html>
<head>

</head>
<body>

<?php
$q = intval($_GET['q']);
require_once "config.php";
$tenant_id = $_GET['q'];
$rental_amount_paid = $_GET['rental_amount_paid'];
$monthYear_water = $_GET['monthYear_water'];
$water_amount_paid = $_GET['water_amount_paid'];
$monthYear_others = $_GET['monthYear_others'];
$others_amount_paid = $_GET['others_amount_paid'];
$type="";
$rentalInfo = "<td colspan='2' class='border-right'>";
$waterInfo = "<td colspan='2' class='border-right'>";
$othersInfo = "<td colspan='2' class='border-right'>";

echo "<tr>";


if($rental_amount_paid > 0){
	//$rentalInfo .= "<td colspan='2' class='border-right'>";
	$sqlBills = "SELECT * FROM bills 
						WHERE tenant_id = $tenant_id and (payment_status = 'unpaid' or payment_status = 'partial' and bill_type_id = 1)  
						order by CASE WHEN MONTH(issue_date) = MONTH(CURDATE()) AND YEAR(issue_date) = YEAR(CURDATE()) THEN 0 ELSE 1 END, issue_date asc";
	$runBills = mysqli_query($link, $sqlBills);
	if($runBills === FALSE){
		die(mysqli_error($link));
	}

	$balance = 0.00;
	$amount_paid = 0.00;
	
	$countData = 1;
	$paidBalance = $rental_amount_paid;
	$arrayData_rentalBills = array();
	$totalAmountPaid = 0.00;

	$rentalInfo .= "<table class='table  table-hover table-sm' id='dataTable' width='70%' cellspacing='0' style='font-size: 12px'>
			<tr class='border-bottom'>
				<th>#</th>
				<th>Month / Year</th>
				<th>Type</th>
				<th>Amount Paid</th>
				<th>Amount Due</th>
			</tr>";
	while($resultBills = mysqli_fetch_array($runBills)){
		$issue_date = $resultBills["issue_date"];
		$monthYear_issuedate = date("Y-m",strtotime($issue_date));
		$amount_due = $resultBills["amount_due"];
		$bill_id = $resultBills["id"];
		$tenant_id = $resultBills["tenant_id"];
		$bill_no = $resultBills["bill_no"];
		
		if($monthYear_issuedate == date("Y-m")){
			$type = "Current";
		}
		else if($monthYear_issuedate < date("Y-m")){
			$type = "Outstanding";
		}
		else if($monthYear_issuedate > date("Y-m")){
			$type = "Advance";
		}
		if($totalAmountPaid < $rental_amount_paid && $totalAmountPaid >= 0){
			$paidBalance = $paidBalance - $amount_due;
			if($paidBalance >= 0){
				$amount_due = 0.00;
				$amount_paid = $resultBills["amount_due"];
				$arrayData_rentalBills[] = $bill_id . "," . $countData . "," . $monthYear_issuedate . "," . $type . "," . number_format($amount_paid,2) . "," . number_format($amount_due,2);
			}
			else if($paidBalance < 0){
				$amount_due =   abs($paidBalance);
				$amount_paid = $resultBills["amount_due"] - abs($paidBalance);
				$arrayData_rentalBills[] = $bill_id . "," . $countData . "," . $monthYear_issuedate . "," . $type . "," . number_format($amount_paid,2) . "," . number_format($amount_due,2);
			}
		}
		
		$totalAmountPaid += $amount_paid;
		$countData++;
		
	}
	$arrayLength = count($arrayData_rentalBills);
	$listOut = "";
	for($y=0;$y<$arrayLength;$y++){
		$listOut = explode(',',$arrayData_rentalBills[$y]);
		$rentalInfo .= "<tr class='border-bottom'>";
		$rentalInfo .= "<td>".$listOut[1]."</td>";
		$rentalInfo .= "<td>".$listOut[2]."</td>";
		$rentalInfo .= "<td>".$listOut[3]."</td>";
		$rentalInfo .= "<td>".$listOut[4]."</td>";
		$rentalInfo .= "<td>".$listOut[5]."</td>";
		$rentalInfo .= "</tr>";
	}
	$rentalInfo .= "</table>";
	$rentalInfo .= "</td>";
}
else{
	$rentalInfo .= "&nbsp;";
	$rentalInfo .= "</table>";
	$rentalInfo .= "</td>";
}
if($monthYear_water != "" && $water_amount_paid > 0 ){
	//$waterInfo .= "<td colspan='2' class='border-right'>";
	if($monthYear_water == date("Y-m")){
		$type = "Current";
	}
	else if($monthYear_water < date("Y-m")){
		$type = "Outstanding";
	}
	else if($monthYear_water > date("Y-m")){
		$type = "Advance";
	}
	$waterInfo .= "<table class='table  table-hover table-sm' id='dataTable' width='70%' cellspacing='0' style='font-size: 12px'>
			<tr class='border-bottom'>
				<th>#</th>
				<th>Month / Year</th>
				<th>Type</th>
				<th>Amount Paid</th>
				<th>Amount Due</th>
			</tr>";
	$waterInfo .= "<tr class='border-bottom'>
				<td>1</td>
				<td>".$monthYear_water."</td>
				<td>".$type."</td>
				<td>".$water_amount_paid."</td>
				<td>0.00</td>
			</tr>";
	$waterInfo .= "</table>";
	$waterInfo .= "</td>";
}
else{
	$waterInfo .= "&nbsp;";
	$waterInfo .= "</table>";
	$waterInfo .= "</td>";
}
if($monthYear_others != "" && $others_amount_paid > 0){
	//$othersInfo .= "<td colspan='2' class='border-right'>";
	$type="Outstanding";
	$othersInfo .= "<table class='table  table-hover table-sm' id='dataTable' width='70%' cellspacing='0' style='font-size: 12px'>
			<tr class='border-bottom'>
				<th>#</th>
				<th>Month / Year</th>
				<th>Type</th>
				<th>Amount Paid</th>
				<th>Amount Due</th>
			</tr>";
	$othersInfo .= "<tr class='border-bottom'>
				<td>1</td>
				<td>".$monthYear_others."</td>
				<td>".$type."</td>
				<td>".$others_amount_paid."</td>
				<td>0.00</td>
			</tr>";
	$othersInfo .= "</table>";
	$othersInfo .= "</td>";
}
else{
	$othersInfo .= "&nbsp;";
	$othersInfo .= "</table>";
	$othersInfo .= "</td>";
}
echo $rentalInfo;
echo $waterInfo;
echo $othersInfo;

mysqli_close($link);
?>
</body>
</html>