<?php 
error_reporting(0); 
include("session_set.php");
require_once "config.php";
 $tenant_id = $_GET['tenant_id'];
 $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'];
$bill_type_id_others = $_GET['bill_type_id_others'];
 " RENTAL PAYMENT TYPE:: ".$rental_payment_type = $_GET['rental_payment_type'];
$water_payment_type = $_GET['water_payment_type'];
$others_payment_type = $_GET['others_payment_type'];
$refNo_rental = $_GET['refNo_rental'];
$refNo_water = $_GET['refNo_water'];
$refNo_other = $_GET['refNo_other'];

$type="";
$rentalInfo = "<td colspan='2' class='border-right' valign='top'>";
$waterInfo = "<td colspan='2' class='border-right' valign='top'>";
$othersInfo ="";

//staff incharge
$sqlStaff = "select name from users where id = " . $_SESSION["staff_id"];
$runStaff = mysqli_query($link, $sqlStaff);
	if($runStaff === FALSE){
		die(mysqli_error($link));
	}
$resultStaff = mysqli_fetch_assoc($runStaff);
$staff_name = $resultStaff["name"];


//echo "test";
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;
	
	//rental type description
	if($rental_payment_type !="" && $rental_payment_type != null){
		 " rental_payment_type:: " .$sqlPaymentType = "select payment_type_desc from payment_type where id = $rental_payment_type";
		$runPaymentType = mysqli_query($link, $sqlPaymentType);
		if($runPaymentType === FALSE){
			die(mysqli_error($link));
		}
		$resultPaymentType = mysqli_fetch_assoc($runPaymentType);
		$payment_type_desc = $resultPaymentType["payment_type_desc"];
	}
	//rental type description
	
	$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>Payment Type</th>
						
						";
						
	if($rental_payment_type > 1 )	{
		$rentalInfo .= "<th>Ref. No.</th> ";
	}				
	$rentalInfo .="<th>Amount Due</th>
					<th>Staff Incharge</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) . "," . $payment_type_desc . "," . number_format($amount_due,2);
			}
			else if($paidBalance < 0){
				$amount_due =   abs($paidBalance);
				$amount_paid = $resultBills["amount_due"] - abs($paidBalance);
				
			}
			if($rental_payment_type > 1 )	{
				$arrayData_rentalBills[] = $bill_id . "," . $countData . "," . $monthYear_issuedate . "," . $type . "," . number_format($amount_paid,2) . "," . $payment_type_desc . ",". $refNo_rental . "," . number_format($amount_due,2) . "," . $staff_name;
			}	
			else{
				$arrayData_rentalBills[] = $bill_id . "," . $countData . "," . $monthYear_issuedate . "," . $type . "," . number_format($amount_paid,2) . "," . $payment_type_desc . "," . number_format($amount_due,2). "," . $staff_name;
			}
		}
		
		$totalAmountPaid += $amount_paid;
		$countData++;
		
	}
	$arrayLength = count($arrayData_rentalBills);
	$listOut = "";
	  $arrayData_rentalBills[0];
	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 .= "<td>".$listOut[6]."</td>";
		$rentalInfo .= "<td>".$listOut[7]."</td>";
		$rentalInfo .= "<td>".$listOut[8]."</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";
	}
	
	//water type description
	if($water_payment_type !="" && $water_payment_type != null){
		 " ::water_payment_type ::" . $sqlPaymentType = "select payment_type_desc from payment_type where id = $water_payment_type";
		$runPaymentType = mysqli_query($link, $sqlPaymentType);
		if($runPaymentType === FALSE){
			die(mysqli_error($link));
		}
		$resultPaymentType = mysqli_fetch_assoc($runPaymentType);
		$payment_type_desc = $resultPaymentType["payment_type_desc"];
		//water type description
	}
	
	$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>Payment Type</th> ";
						
	if($water_payment_type > 1 )	{
		$waterInfo .= "<th>Ref. No.</th> ";
	}				
	$waterInfo .="<th>Amount Due</th>
					<th>Staff Incharge</th>
					</tr>";
	$waterInfo .= "<tr class='border-bottom'>
				<td>1</td>
				<td>".$monthYear_water."</td>
				<td>".$type."</td>
				<td>".number_format($water_amount_paid,2,'.',',')."</td>
				<td>".$payment_type_desc."</td>";
	if($water_payment_type > 1 )	{
		$waterInfo .= "<td>".$refNo_water."</td>";
	}		
	$waterInfo .="<td>0.00</td>
			<td>".$staff_name."</td>
			</tr>";
	$waterInfo .= "</table>";
	$waterInfo .= "</td>";
}
else{
	$waterInfo .= "&nbsp;";
	$waterInfo .= "</table>";
	$waterInfo .= "</td>";
}
if($others_amount_paid > 0){
	//$othersInfo .= "<td colspan='2' class='border-right'>";
	$othersInfo = "<td colspan='2' class='border-right'>";
	$type="Outstanding";
	
	$sqlBills_outstanding = "SELECT * FROM bills 
						WHERE tenant_id = $tenant_id and (payment_status = 'unpaid' or payment_status = 'partial' )  and bill_type_id = ".$bill_type_id_others." AND amount_due > 0
						";
	$runBills_outstanding = mysqli_query($link, $sqlBills_outstanding);
	if($runBills_outstanding === FALSE){
		die(mysqli_error($link));
	}
	
	//others type description
	if($others_payment_type !="" && $others_payment_type != null){
	  $sqlPaymentType = "select payment_type_desc from payment_type where id = $others_payment_type";
	$runPaymentType = mysqli_query($link, $sqlPaymentType);
	if($runPaymentType === FALSE){
		die(mysqli_error($link));
	}
	$resultPaymentType = mysqli_fetch_assoc($runPaymentType);
	$payment_type_desc = $resultPaymentType["payment_type_desc"];
	//others type description
	}
	
	$balance_outstanding = 0.00;
	$amount_paid_outstanding = 0.00;
	$paid_outstanding = $others_amount_paid;
	$amount_due_balance = 0.00;
	while($resultBills_outstanding=mysqli_fetch_array($runBills_outstanding)){
		$amount_due = $resultBills_outstanding["amount_due"];
		$monthYear_others = $resultBills_outstanding["issue_date"];;
	}
	$amount_due_balance = $amount_due - $others_amount_paid;
	
			
	$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>Payment Type</th> 
						";
						
	if($others_payment_type > 1 )	{
		$othersInfo .= "<th>Ref. No.</th> ";
	}				
	$othersInfo .="<th>Amount Due</th>
					<th>Staff Incharge</th> 
					</tr>";
					
	$othersInfo .= "<tr class='border-bottom'>
				<td>1</td>
				<td>".date("Y-m", strtotime($monthYear_others))."</td>
				<td>".$type."</td>
				<td>".number_format($others_amount_paid,2,'.',',')."</td>
				<td>".$payment_type_desc."</td>";
	if($others_payment_type > 1 )	{
		$othersInfo .= "<td>".$refNo_other."</td> ";
	}	
		
		$othersInfo .=		"<td>".number_format($amount_due_balance,2,'.',',')."</td>";
		$othersInfo .=		"<td>".$staff_name."</td>";					
				
				
	$othersInfo .=	"</tr>";
	$othersInfo .= "</table>";
	$othersInfo .= "</td>";
} 





echo $rentalInfo;
echo $waterInfo;
echo $othersInfo;

mysqli_close($link);
?>