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

if($id == 0){
    $result = $property->executeByQuerySelector("SELECT *,(SELECT propertyname FROM property WHERE property.id = accounts.ownerid) AS propertyname,(SELECT propertyidno FROM property WHERE property.id = accounts.ownerid) AS propertyidno FROM accounts WHERE accountname = 'PAYABLES' AND status LIKE '$status' AND DATE(transactiondate) BETWEEN '$startdate' AND '$enddate' ORDER BY id DESC");
}else{
    $result = $property->executeByQuerySelector("SELECT *,(SELECT propertyname FROM property WHERE property.id = accounts.ownerid) AS propertyname,(SELECT propertyidno FROM property WHERE property.id = accounts.ownerid) AS propertyidno FROM accounts WHERE accountname = 'PAYABLES' 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'));
}
?>