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

date_default_timezone_set('Africa/Lagos');

if ($_SESSION['wuseremail'] === null || !isset($_SESSION['wuseremail'])) {
     exit(badRequest(204,'Invalid session data. Proceed to login'));
}
$permissions = explode("|",$_SESSION["permissions"]);
if(in_array("NO POSTING",$permissions)){
    exit(badRequest(204,'No posting enabled'));
}

$glaccount = new GLAccount();
$res = $glaccount->getGLAccountInfo("accountnumber = '" . $_POST['accountnumber'] . "'");

if ($res) {
    $id = $res['id'];
    $query = "description = '" . ((isset($_REQUEST["description"]) && $_REQUEST["description"] !== "") ? strtoupper(htmlentities($_REQUEST["description"],ENT_QUOTES)) : $res["description"]) . "',
        groupname = '" . ((isset($_REQUEST["groupname"]) && $_REQUEST["groupname"] !== "") ? strtoupper(htmlentities($_REQUEST["groupname"],ENT_QUOTES)) : $res["groupname"]) . "',
        subgroup = '" . ((isset($_REQUEST["subgroup"]) && $_REQUEST["subgroup"] !== "") ? strtoupper(htmlentities($_REQUEST["subgroup"],ENT_QUOTES)) : $res["subgroup"]) . "',
        accounttype = '" . ((isset($_REQUEST["accounttype"]) && $_REQUEST["accounttype"] !== "") ? strtoupper($_POST["accounttype"]) : $res["accounttype"]) . "'";

    //exit("Query: " . $query);
    $result = $glaccount->updateGLAccountDetails($query, $id);

    if ($result) {
        $user = new User();
        $user->addUserActivity($_SESSION["hemsemail"], "GL account: " . $_POST['accountnumber'] . " updated at" . date("Y-m-d H:i:s"), date("Y-m-d H:i:s"), "GL");
        
        echo success($result, 200, "Successful", "Successful");
    }else {
        echo badRequest(204, "Not successful");
    }
    
}else {

    $glaccountnumber = getNewGLAccount();
    if($glaccountnumber === "ERROR"){
        exit(badRequest(204, "Not successful. Account number limit exceeded"));
    }
    
    $fields = "accountnumber,description,accounttype,groupname,status,tlog,subgroup";
    
    // values for requests
    $values = "'". $glaccountnumber . "',
    '" . ((isset($_REQUEST["description"]) && $_REQUEST["description"] !== "") ? strtoupper(htmlentities($_REQUEST["description"],ENT_QUOTES)) : "-" ) . "',
    '" . ((isset($_REQUEST["accounttype"]) && $_REQUEST["accounttype"] !== "") ? strtoupper($_REQUEST["accounttype"]) : "-" ) . "',
    '" . ((isset($_REQUEST["groupname"]) && $_REQUEST["groupname"] !== "") ? strtoupper(htmlentities($_REQUEST["groupname"],ENT_QUOTES)) : "-" ) . "',
    'OPEN',
    '" . $_SESSION["hemsemail"] . " " . date('Y-m-d H:i:s') . "',
    '" . ((isset($_REQUEST["subgroup"]) && $_REQUEST["subgroup"] !== "") ? strtoupper(htmlentities($_REQUEST["subgroup"],ENT_QUOTES)) : "-" ) . "'";

    $result = $glaccount->registerGLAccount($fields, $values);

    if ($result) {
        $user = new User();
        $user->addUserActivity($_SESSION["hemsemail"], "GL account: $glaccountnumber updated at" . date("Y-m-d H:i:s"), date("Y-m-d H:i:s"), "GL");
        
        echo success($result, 200, "Successful", "Successful");
    }else {
        echo badRequest(204, "Not successful");
    }
}
function getNewGLAccount(){
    $glaccount = new GLAccount();
    do{
        $flag = true;
        $accountnumber = "99";
        $accountnumber .= mt_rand(100000,999999);    
        $resultacct = $glaccount->executeByQuerySelector("SELECT * FROM glaccount WHERE accountnumber = '$accountnumber'");
        if(!$resultacct || $resultacct === null){
            $flag = false;
        }
    }while($flag === true);
    return $accountnumber;    
}
function getNewGLAccountold(){
    $glaccount = new GLAccount();
    $resultglids = $glaccount->executeByQuerySelector("select id from glaccount ORDER BY id DESC");
    $glaccountnumber = "";
    $lastid = 0;
    if($resultglids){
        $lastid = $resultglids[0]["id"];
    }
    $newid = $lastid + 1;
    if(strlen($newid) === 1){
        $glaccountnumber = "990000000" . $newid;
    }elseif(strlen($newid) === 2){
        $glaccountnumber = "99000000" . $newid;
    }elseif(strlen($newid) === 3){
        $glaccountnumber = "9900000" . $newid;
    }elseif(strlen($newid) === 4){
        $glaccountnumber = "990000" . $newid;
    }elseif(strlen($newid) === 5){
        $glaccountnumber = "99000" . $newid;
    }elseif(strlen($newid) === 6){
        $glaccountnumber = "9900" . $newid;
    }elseif(strlen($newid) === 7){
        $glaccountnumber = "990" . $newid;
    }elseif(strlen($newid) === 8){
        $glaccountnumber = "99" . $newid;
    }else{
        $glaccountnumber = "ERROR";
    }
    return $glaccountnumber;
}

?>