<?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();

$paymentdate = (isset($_POST["paymentdate"]) && $_POST["paymentdate"] !== "") ? $_POST["paymentdate"] : date('Y-m-d H:i:s');
$amountpaid = (isset($_POST["amountpaid"]) && $_POST["amountpaid"] !== "") ? $_POST["amountpaid"] : 0;
$id = (isset($_POST["id"]) && intval($_POST["id"]) > 0) ? intval($_POST["id"]) : 0;

if($amountpaid == 0){
    exit(badRequest(204,'Amount paid should not be zero'));
}
$ref = (isset($_POST["reference"]) && $_POST["reference"] !== "") ? htmlentities($_POST["reference"],ENT_QUOTES) : mt_rand(100000000,999999999999 ) ;
$resrented = $property->executeByQuerySelector("SELECT * FROM rentedproperty WHERE unitid = " . $id . " ORDER BY id DESC limit 1");
if($resrented){
    $begindate = $resrented[0]["expirationdate"];
    $begindatetime = strtotime($resrented[0]["expirationdate"]);
    $expirationdatetime = strtotime('+1 year', $begindatetime);
    $expirationdate = date('Y-m-d', $begindatetime);

    $fields = "propertyid,unitid,tenantid,begindate,expirationdate,paymentdate,annualrent,otherfees,
    otherfeesdescription,amountpaid,reference,status,tlog";

    $propertyid = $resrented[0]["propertyid"];
    $unitid = $resrented[0]["unitid"];
    $tenantid = $resrented[0]["tenantid"];
    
    $values = "
    " . $resrented[0]["propertyid"] . ",
    " . $resrented[0]["unitid"] . ",
    " . $resrented[0]["tenantid"] . ",
    '" . $begindate . "',
    '" . $expirationdate . "',
    '" . $paymentdate . "',
    
    " . $resrented[0]["annualrent"] . ",
    0,
    '-',
    " . $amountpaid . ",
    '$ref',
    '" . ((isset($_POST["status"]) && $_POST["status"] !== "") ? $_POST["status"] : "OPEN" ) . "',
    '" . $_SESSION["wuseremail"] . " | " . date("Y-m-d H:i:s") . "'";

    $result = $property->registerRentAProperty($fields, $values);
    if($result){
        $pres = $property->getPropertyInfo("id = $propertyid");
        $presunit = $property->getPropertyUnitInfo("id = $unitid");
        $description = "Rent: " . $pres["propertyname"] . " unit: " . $presunit["unitname"];
        $fields2 = "accountname,description,ownerid,debit,credit,transactiondate,reference,status,tlog";
        $debit = $resrented[0]["annualrent"];
        $values2 = "'RECEIVABLES','$description',$tenantid,$debit,$amountpaid,
        '" . $paymentdate . "',
        '$ref',
        '" . ((isset($_POST["status"]) && $_POST["status"] !== "") ? $_POST["status"] : "OPEN" ) . "',
        '" . $_SESSION["wuseremail"] . " | " . date("Y-m-d H:i:s") . "'";
        
        $resultaccounts = $property->registerAccount($fields2, $values2);
        $resproperty = $property->executeByQuerySelector("UPDATE propertyunit SET rented = 'YES' WHERE id = $unitid");
        
        echo success($result, 200, "Successful", "Successful");
    }else{
        echo badRequest(204, "Not successful");
    }

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


?>