<?php
session_start();
require_once '../utils/errorhandler.php';
require_once '../utils/response.php';
require_once '../model/dbclass.php';
require_once '../model/model.php';
require_once '../model/Property.php';

if ($_SESSION['wuseremail'] === null || !isset($_SESSION['wuseremail'])) {
     exit(badRequest(204,'Invalid session data. Proceed to login'));
}

$property = new Property();

$status = (isset($_POST["status"]) && $_POST["status"] !== "") ? $_POST["status"] : "%";
$startdate = (isset($_POST["startdate"]) && $_POST["startdate"] !== "") ? $_POST["startdate"] : date('Y-m-d');
$enddate = (isset($_POST["enddate"]) && $_POST["enddate"] !== "") ? $_POST["enddate"] : date('Y-m-d');
$id = (isset($_POST["id"]) && intval($_POST["id"]) > 0) ? $_POST["id"] : 0;

$currentdate = date('Y-m-d');
$currentdatetime = strtotime($currentdate);

$resunits = $property->executeByQuerySelector("SELECT id,rent,unitname FROM propertyunit WHERE rented = 'YES'");
if($resunits){
    $duerentarray = array();
    foreach($resunits as $resunit){
        $resrented = $property->executeByQuerySelector("SELECT * FROM rentedproperty WHERE rent > 0 AND unitid = " . intval($resunit["id"]) . " ORDER BY id DESC limit 1");
        if($resrented){
            
            $currentdatetimebefore = strtotime('-30 days', $currentdatetime);
            foreach($resrented as $arented){
                $rentalid = $arented["id"];
                $expirationdatetime = strtotime($arented["expirationdate"]);
                if($expirationdatetime >= $currentdatetimebefore){
                    $resultap = $property->getPropertyInfo("id = " . intval($arented["propertyid"]));
                    $restenant = $property->getTenantInfo("id = " . intval($arented["tenantid"]));
                    
                    if($expirationdatetime >= $currentdatetimebefore && $expirationdatetime < $currentdatetime){
                        $rentstatus = "DUE";
                    }elseif($expirationdatetime <= $currentdatetime){
                        $rentstatus = "OVERDUE";
                    }else{
                        $rentstatus = "NOT YET DUE";
                    }
                    if($rentstatus === "DUE" || $rentstatus === "OVERDUE"){
                        
                        $resotherdata = $property->executeByQuerySelector("SELECT SUM(amount) AS tamount FROM rentalfees WHERE rentalid = $rentalid AND renewable = 'YES' AND feenameid <> (SELECT id FROM morefees WHERE feename = 'ANNUAL RENT')");
                        $duerentarray[] = array(
                            "unitid" => $resunit["id"],
                            "unitname" => $resunit["unitname"],
                            "rent" => $arented["rent"],
                            "otherrenewablefees" => $resotherdata[0]["tamount"],
                            "propertydata" => $resultap,
                            "lastrentdata" => $arented,
                            "tenantdata" => $restenant,
                            "rentstatus" => $rentstatus
                            );
                    }
                }
            }
        }
        
    }
    if($duerentarray){
    	echo success($duerentarray,200, "Successful","Successful");
    }else{
    	exit(badRequest(204,'There may be no due rents'));
    }

}else{
    exit(badRequest(204,'There may be no rented units'));
}


?>