<?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';
require_once '../model/organisationinfo.php';
require_once '../model/glaccount.php';

date_default_timezone_set('Africa/Lagos');

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

$property = new Property();

$organisation = new OrganisationInfo();
$organisationres = $organisation->retrieveOrganisationByQuery("companyname NOT LIKE '-'", 1, 1);
$expaccount = $organisationres["default_glexpenseaccount"];

$glaccount = new GLAccount();
$glres = $glaccount->getGLAccountInfo("accountnumber = '$expaccount'");

$propertyid = ((isset($_POST["ownerid"]) && $_POST["ownerid"] > 0) ? intval($_POST["ownerid"]) : -1);
$debit = ((isset($_POST["debit"]) && $_POST["debit"] > 0) ? $_POST["debit"] : 0);
$feenameid = ((isset($_POST["feenameid"]) && $_POST["feenameid"] > 0) ? $_POST["feenameid"] : 0);

if($propertyid == -1 || $debit == 0){
    exit(badRequest(204,'Not successful. Tenant or credit amount may not be available'));
}
if($feenameid == 0){
    exit(badRequest(204,'Not successful. Feename ID is not valid'));
}

$ref = (isset($_POST["reference"]) && $_POST["reference"] !== "") ? htmlentities($_POST["reference"],ENT_QUOTES) : mt_rand(100000000,999999999999 ) ;
if(isset($_POST['id'])){
    $res = $property->getAccountInfo("id = " . $_POST['id']);
}
if($res){
    $id = $res['id'];
    $resref = $res['reference'];
    $query = "
        ownerid = " . ((isset($_POST["ownerid"]) && $_POST["ownerid"] > 0) ? $_POST["ownerid"] : $res['ownerid'] ) . ",
        transactiondate = '" . ((isset($_POST["transactiondate"]) && $_POST["transactiondate"] !== "") ? $_POST["transactiondate"] : $res['transactiondate'] ) . "',
        debit = " . ((isset($_POST["debit"]) && $_POST["debit"] > 0) ? $_POST["debit"] : $res['debit'] ) . ",
        feenameid = " . ((isset($_POST["feenameid"]) && $_POST["feenameid"] > 0) ? $_POST["feenameid"] : $res['feenameid'] ) . ",
        description = '" . ((isset($_POST["description"]) && $_POST["description"] !== "") ? htmlentities($_POST["description"],ENT_QUOTES) : $res['description'] ) . "',
        tlog = 'Updated: " . $_SESSION["wuseremail"] . " | " . date("Y-m-d H:i:s") . "',
        status = '" . ((isset($_POST["status"]) && $_POST["status"] !== "") ? $_POST["status"] : $res['status'] ) . "'";

    $result = $property->updateAccountDetails($query, $id);

    if ($result) {
        echo success($result, 200, "Successful", "Successful");
    }else {
        echo badRequest(204, "Not successful");
    }
    
}else{
    // fields names
    $fields2 = "accountnumber,accountname,description,ownerid,debit,credit,transactiondate,reference,feenameid,propertyid,status,tlog,user";
    
    $values2 = "'$expaccount','" . $glres["accountname"] . "',
    '" . ((isset($_POST["description"]) && $_POST["description"] !== "") ? htmlentities($_POST["description"],ENT_QUOTES) : "Payment" ) . "', 
    -1,$debit,0,
    '" . ((isset($_POST["transactiondate"]) && $_POST["transactiondate"] !== "") ? $_POST["transactiondate"] : date('Y-m-d H:i:s')) . "',
    '$ref',
    $feenameid,
    $propertyid,
    '" . ((isset($_POST["status"]) && $_POST["status"] !== "") ? $_POST["status"] : "OPEN" ) . "',
    '" . $_SESSION["wuseremail"] . " | " . date("Y-m-d H:i:s") . "',
    '" . $_SESSION['wuseremail'] . "'";
    
    $resultaccounts = $property->registerAccount($fields2, $values2);
        
    if($resultaccounts){
        echo success($resultaccounts, 200, "Successful", "Successful");
    }else{
        echo badRequest(204, "Not successful");
    }
}
