<?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) ? intval($_POST["id"]) : 0;
$tenantid = (isset($_POST["tenantid"]) && intval($_POST["tenantid"]) > 0) ? intval($_POST["tenantid"]) : 0;

if($id == 0){
    if($tenantid == 0){
        $result = $property->executeByQuerySelector("SELECT *,(SELECT firstname FROM tenants WHERE tenants.id = accounts.ownerid) AS firstname,(SELECT lastname FROM tenants WHERE tenants.id = accounts.ownerid) AS lastname FROM accounts WHERE accountname = 'RECEIVABLES' AND status LIKE '$status' AND DATE(transactiondate) BETWEEN '$startdate' AND '$enddate' ORDER BY id DESC");
    }else{
        $result = $property->executeByQuerySelector("SELECT *,(SELECT firstname FROM tenants WHERE tenants.id = accounts.ownerid) AS firstname,(SELECT lastname FROM tenants WHERE tenants.id = accounts.ownerid) AS lastname FROM accounts WHERE ownerid = $tenantid AND accountname = 'RECEIVABLES' AND status LIKE '$status' AND DATE(transactiondate) BETWEEN '$startdate' AND '$enddate' ORDER BY id DESC");
    }
}else{
    
        $result = $property->executeByQuerySelector("SELECT *,(SELECT firstname FROM tenants WHERE tenants.id = accounts.ownerid) AS firstname,(SELECT lastname FROM tenants WHERE tenants.id = accounts.ownerid) AS lastname FROM accounts WHERE accountname = 'RECEIVABLES' AND id = $id");
}
if($result){

	echo success($result,200, "Successful","Successful");
}else{
	exit(badRequest(204,'There may be no record to match this search filters'));
}
?>