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

date_default_timezone_set('Africa/Lagos');

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

$user = new User();
$glaccount = new GLAccount();
$transaction = new Transaction();
$organisationinfo = new OrganisationInfo();
$resorganisation = $organisationinfo->getOrganisationInfo("1");


$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"] > 0) ? $_POST["location"] : $_SESSION["location_id"];
//$year = (isset($_POST["year"]) && $_POST["year"] > 0) ? $_POST["year"] : date('Y');
//$month = (isset($_POST["month"]) && $_POST["month"] > 0) ? $_POST["month"] : date('m');
//$endday = cal_days_in_month(CAL_GREGORIAN, $month, $year); 	

//$startdate = $year . "-" . $month . "-01";
//$enddate = $year . "-" . $month . "-" . $endday;


//debit
$expensearray = array();
$resultgls = $glaccount->executeByQuerySelector("SELECT * FROM glaccount WHERE accounttype = 'EXPENSE' ORDER BY description ASC");
if($resultgls){
    $expensetotal = 0;
    foreach($resultgls as $resultgl){
        $accountname = $resultgl["description"];
        $debit = 0;
        $credit = 0;
        $resulttrans = $transaction->executeTransactionByQuerySelector("SELECT SUM(credit) AS credit, SUM(debit) AS debit FROM tdetail WHERE accountnumber = '" . $resultgl["accountnumber"] . "' AND DATE(transactiondate) BETWEEN '$startdate' AND '$enddate'");
        if($resulttrans){
            $bal = round(($resulttrans[0]["debit"] - $resulttrans[0]["credit"]),2);
            $expensetotal += round($bal,2);
            if($bal >= 0){
                $debit = $bal;
            }else{
                $credit = -1 * $bal;
            }
        }
        $expensearray[] = array(
            "accountname" => $accountname,
            "debit" => $debit,
            "credit" => $credit,
            "expensetotal" => $expensetotal
            );
    }
}


//credit
$incomearray = array();
$resultgls = $glaccount->executeByQuerySelector("SELECT * FROM glaccount WHERE accounttype = 'INCOME' ORDER BY description ASC");
if($resultgls){
    $incometotal = 0;
    foreach($resultgls as $resultgl){
        $accountname = $resultgl["description"];
        $debit = 0;
        $credit = 0;
        $resulttrans = $transaction->executeTransactionByQuerySelector("SELECT SUM(credit) AS credit, SUM(debit) AS debit FROM tdetail WHERE accountnumber = '" . $resultgl["accountnumber"] . "' AND DATE(transactiondate) BETWEEN '$startdate' AND '$enddate'");
        if($resulttrans){
            $bal = round(($resulttrans[0]["credit"] - $resulttrans[0]["debit"]),2);
            $incometotal += round($bal,2);
            if($bal >= 0){
                $credit = $bal;
            }else{
                $debit = -1 * $bal;
            }
        }
        $incomearray[] = array(
            "accountname" => $accountname,
            "debit" => $debit,
            "credit" => $credit,
            "incometotal" => $incometotal
            );
    }
}   


$pldata = array(
    "expense" => $expensearray,
    "income" =>$incomearray
    );


if($pldata){
    $user->addUserActivity($_SESSION["hemsemail"], "Viewed income statement at" . date("Y-m-d H:i:s"), date("Y-m-d H:i:s"), "INCOME STATEMENT");
    echo success($pldata, 200, "Successful", "Successful");
}else{
    $user->addUserActivity($_SESSION["hemsemail"], "Failed attempt to view income statement at" . date("Y-m-d H:i:s"), date("Y-m-d H:i:s"), "INCOME STATEMENT");
    echo badRequest(204, "Some went wrong. Try again");
}

?>