<?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');
$location = (isset($_POST["location"]) && $_POST["location"] !== "") ? $_POST["location"] : "%";
$propertyidno = (isset($_POST["propertyidno"]) && $_POST["propertyidno"] !== "") ? $_POST["propertyidno"] : "%";

$tarray = array();
//credit
$resultmorefees = $property->executeByQuerySelector("SELECT * FROM morefees WHERE status LIKE '$status'");
if($propertyidno !== "%"){
    $resultpid = $property->executeByQuerySelector("SELECT id FROM property WHERE propertyidno = '$propertyidno'");
    if($resultpid){
        $propertyid = $resultpid[0]["id"];
        foreach($resultmorefees as $resultmorefee){
            $feename = $resultmorefee["feename"];
            $result = $property->executeByQuerySelector("SELECT *,'$feename' AS feename FROM accounts WHERE propertyid = $propertyid AND feenameid = " . $resultmorefee["id"]);
            if($result){
                $tarray[] = array(
                    "data" => $result
                    );
            }
        }
        //$result = $property->executeByQuerySelector("SELECT *,(SELECT CONCAT(lastname, ' ',firstname) FROM tenants WHERE tenants.id = accounts.ownerid) AS tenantname FROM accounts WHERE accountname = 'RECEIVABLES' AND status LIKE '$status' AND ownerid IN (SELECT tenantid FROM rentedproperty WHERE propertyid = $propertyid) ORDER BY id DESC");
        
    }else{
        exit(badRequest(204,'There may be no record to match this search filters'));
    }
}elseif($location !== "%"){
    //$resultpids = $property->executeByQuerySelector("SELECT id FROM property WHERE location LIKE '$location'");
    //$result = $property->executeByQuerySelector("SELECT *,(SELECT CONCAT(lastname, ' ',firstname) FROM tenants WHERE tenants.id = accounts.ownerid) AS tenantname FROM accounts WHERE accountname = 'RECEIVABLES' AND status LIKE '$status' AND ownerid IN (SELECT tenantid FROM rentedproperty INNER JOIN property ON rentedproperty.propertyid = property.id WHERE property.location = '$location')  ORDER BY id DESC");
    foreach($resultmorefees as $resultmorefee){
        $feename = $resultmorefee["feename"];
        $result = $property->executeByQuerySelector("SELECT *,'$feename' AS feename FROM accounts WHERE accountname = 'RECEIVABLES' AND propertyid IN (SELECT id FROM property WHERE property.location = '$location')  ORDER BY id DESC");
        if($result){
            $tarray[] = array(
                "data" => $result
                );
        }
        
    }
    //$result = $property->executeByQuerySelector("SELECT * FROM accounts WHERE accountname = 'RECEIVABLES' AND status LIKE '$status' AND ownerid IN (SELECT tenantid FROM rentedproperty INNER JOIN property ON rentedproperty.propertyid = property.id WHERE property.location = '$location')  ORDER BY id DESC");
}else{
    foreach($resultmorefees as $resultmorefee){
        $feename = $resultmorefee["feename"];
        
        $result = $property->executeByQuerySelector("SELECT *,'$feename' AS feename FROM accounts WHERE DATE(transactiondate) BETWEEN '$startdate' AND '$enddate' ORDER BY id DESC");
        if($result){
            $tarray[] = array(
                "data" => $result
                );
        }
    }        
    //$result = $property->executeByQuerySelector("SELECT *,(SELECT CONCAT(lastname, ' ',firstname) FROM tenants WHERE tenants.id = accounts.ownerid) AS tenantname FROM accounts WHERE accountname = 'RECEIVABLES' AND status LIKE '$status' AND DATE(transactiondate) BETWEEN '$startdate' AND '$enddate' ORDER BY id DESC");
}

//debit
/*
if($propertyidno !== "%"){
    $resultdebit = $property->executeByQuerySelector("SELECT *,(SELECT propertyname FROM property WHERE property.id = accounts.ownerid) AS propertyname FROM accounts WHERE accountname = 'PAYABLES' AND status LIKE '$status' AND ownerid IN (SELECT id FROM property WHERE propertyidno = '$propertyidno') ORDER BY id DESC");
}elseif($location !== "%"){
    $resultdebit = $property->executeByQuerySelector("SELECT *,(SELECT propertyname FROM property WHERE property.id = accounts.ownerid) AS propertyname FROM accounts WHERE accountname = 'PAYABLES' AND status LIKE '$status' AND ownerid IN (SELECT id FROM property WHERE location = '$location') ORDER BY id DESC");
}else{
    $resultdebit = $property->executeByQuerySelector("SELECT *,(SELECT propertyname FROM property WHERE property.id = accounts.ownerid) AS propertyname FROM accounts WHERE accountname = 'PAYABLES' AND status LIKE '$status' AND DATE(transactiondate) BETWEEN '$startdate' AND '$enddate' ORDER BY id DESC");
}

//if($result){

    $tarray[] = array(
        "inflow" => $result,
        "outflow" => $resultdebit
        );*/
	echo success($tarray,200, "Successful","Successful");
//}else{
	//exit(badRequest(204,'There may be no record to match this search filters'));
//}
?>