<?php
require_once "config.php";

// Retrieve data from Query String
$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'];
$tenant_id = $_GET['tenant_id'];
	
// Escape User Input to help prevent SQL Injection
$rental_amount_paid = mysql_real_escape_string($rental_amount_paid);
$monthYear_water = mysql_real_escape_string($monthYear_water);
$water_amount_paid = mysql_real_escape_string($water_amount_paid);
$monthYear_others = mysql_real_escape_string($monthYear_others);
$others_amount_paid = mysql_real_escape_string($others_amount_paid);
$tenant_id = mysql_real_escape_string($tenant_id);

//build query
$date = date("Y-m-01");
echo $sqlBills = "SELECT * FROM bills 
					WHERE tenant_id = $tenant_id and (payment_status = 'unpaid' or payment_status = 'partial')  
					order by CASE WHEN MONTH(issue_date) = MONTH(CURDATE()) AND YEAR(issue_date) = YEAR(CURDATE()) THEN 0 ELSE 1 END, issue_date";
$runBills = mysqli_query($link, $sqlBills);
if($runBills === FALSE){
	die(mysqli_error($link));
}
$balance = 0.00;
$amount_paid = 0.00;
$type="";
$countData = 1;
$paidBalance = $rental_amount_paid;
$arrayData_rentalBills = array();
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";
	}
	
	$paidBalance = $paidBalance - $amount_due;
	if($paidBalance >= 0){
		$amount_due = 0.00;
		$amount_paid = $resultBills["amount_due"];
	}
	$arrayData_rentalBills[] = $bill_id . "," . $monthYear_issuedate . "," . $type . "," . $amount_paid . "," . $amount_due;
	
	
	*/
	
	
	
	
	
	
	
	
	
	
	
	
	/*
	
	if($countData == 1){
		$balance = $rental_amount_paid - $amount_due;
		if($balance < 0){
			$amount_paid = $rental_amount_paid;
			$amount_due = 0.00;
		}
	}
	else if($countData > 1){
		
	}
	
	$rental_amount_paid = $rental_amount_paid - $amount_due;
	
	if($rental_amount_paid < 0)
		$amount_due = abs($rental_amount_paid);
	else if($rental_amount_paid >= 0)
		$amount_due = $rental_amount_paid;
	
	
	*/
	
	
	
	$amount_paid = $rental_amount_paid;
	$amount_due = $balance;
	//count based on paid amount
	//1. check 1st current month and year exist or not in listing
	
	$countData++;
	
	echo "<tr class='border-bottom'>";
	echo "<th>#</th>";
	echo "<th>Month / Year</th>"
	echo "<th>Type</th>"
	echo "<th>Amount Paid</th>"
	echo "<th>Amount Due</th>"
	echo "</tr>";
}



if($rental_amount_paid != "" && ($monthYear_water != "" && $water_amount_paid != "") && ($monthYear_others != "" && $others_amount_paid != "")){
	
}

else if($rental_amount_paid != "" && ($monthYear == "" && $water_amount_paid == "" ) && ($monthYear_others == "" && $others_amount_paid == "")){
	
	echo "<tr class='border-bottom'>";
	echo "<th>#</th>";
	echo "<th>Month / Year</th>"
	echo "<th>Type</th>"
	echo "<th>Amount Paid</th>"
	echo "<th>Amount Due</th>"
	echo "</tr>";
}
else if($rental_amount_paid == "" && ($monthYear != "" && $water_amount_paid != "") && ($monthYear_others == "" && $others_amount_paid == "")){
	
	
}
else if($rental_amount_paid == "" && ($monthYear == "" && $water_amount_paid == "") && ($monthYear_others != "" && $others_amount_paid != "")){
	
	
}
?>