<?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/inventory.php';
require_once '../model/transaction.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'));
}

$organisation = new OrganisationInfo();
$resorganisation = $organisation->getOrganisationInfo("1");
$transaction = new Transaction();
$inventory = new Inventory();
$startdate = (isset($_POST["startdate"]) && $_POST["startdate"] !== "") ? $_POST["startdate"] : date('Y-m-d');
$enddate = (isset($_POST["enddate"]) && $_POST["enddate"] !== "") ? $_POST["enddate"] : date('Y-m-d');
$status = (isset($_POST["status"]) && $_POST["status"] !== "") ? $_POST["status"] : "OPEN";
$salespoint = (isset($_POST["category"]) && $_POST["category"] !== "") ? $_POST["category"] : "%";
$user = (isset($_POST["user"]) && $_POST["user"] !== "") ? $_POST["user"] : "%";
$location = $_SESSION["location_id"]; //(isset($_POST["location"]) && $_POST["location"] > 0) ? $_POST["location"] : $_SESSION["location_id"];
$role = $_SESSION["role"];
//$user = $_SESSION["hemsemail"];
//$salespoint = ($salespoint === "ALL") ? "%" : $salespoint;

//exit("Location: " . $location . " startdate: " . $startdate . " enddate: " . $enddate);
$permissions = explode("|",$_SESSION["permissions"]);
if($location > 0){
    if($role === "SUPERADMIN" || in_array("VIEW ALL USER SALES",$permissions)){
        if($salespoint === "%"){
            $query = "SELECT * FROM tdetail WHERE status = 'POSTED' AND accountnumber = '" . $resorganisation["default_glsalesaccount"] . "' AND user LIKE '$user' AND location = $location AND DATE(transactiondate) BETWEEN '$startdate' AND '$enddate' ORDER BY id";
            //exit("query: " . $query);
            $resultinsert = $transaction->executeTransactionByQuerySelector($query);
        }elseif($salespoint === "Booking/Reservation"){
            $resultinsert = $transaction->executeTransactionByQuerySelector("SELECT * FROM tdetail WHERE status = 'POSTED' AND accountnumber = '" . $resorganisation["default_glsalesaccount"] . "' AND user LIKE '$user' AND salespoint = '$salespoint' AND location = $location AND DATE(transactiondate) BETWEEN '$startdate' AND '$enddate' ORDER BY id");
        }elseif($salespoint === "Food/Beverages"){
            $resultinsert = $transaction->executeTransactionByQuerySelector("SELECT * FROM tdetail WHERE status = 'POSTED' AND accountnumber = '" . $resorganisation["default_glsalesaccount"] . "' AND user LIKE '$user' AND salespoint IN (SELECT department FROM department WHERE applyforsales = 'STOCK') AND location = $location AND DATE(transactiondate) BETWEEN '$startdate' AND '$enddate' ORDER BY id");
        }elseif($salespoint === "Sundry Sales"){
            $resultinsert = $transaction->executeTransactionByQuerySelector("SELECT * FROM tdetail WHERE status = 'POSTED' AND accountnumber = '" . $resorganisation["default_glsalesaccount"] . "' AND user LIKE '$user' AND salespoint IN (SELECT department FROM department WHERE applyforsales = 'NON STOCK') AND location = $location AND DATE(transactiondate) BETWEEN '$startdate' AND '$enddate' ORDER BY id");
        }
    }else{
        if($salespoint === "%"){
            $resultinsert = $transaction->executeTransactionByQuerySelector("SELECT * FROM tdetail WHERE status = 'POSTED' AND accountnumber = '" . $resorganisation["default_glsalesaccount"] . "' AND user = '$user' AND location = $location AND DATE(transactiondate) BETWEEN '$startdate' AND '$enddate' ORDER BY id");
        }elseif($salespoint === "Booking/Reservation"){
            $resultinsert = $transaction->executeTransactionByQuerySelector("SELECT * FROM tdetail WHERE status = 'POSTED' AND accountnumber = '" . $resorganisation["default_glsalesaccount"] . "' AND user = '$user' AND salespoint = '$salespoint' AND location = $location AND DATE(transactiondate) BETWEEN '$startdate' AND '$enddate' ORDER BY id");
        }elseif($salespoint === "Food/Beverages"){
            $resultinsert = $transaction->executeTransactionByQuerySelector("SELECT * FROM tdetail WHERE status = 'POSTED' AND accountnumber = '" . $resorganisation["default_glsalesaccount"] . "' AND user = '$user' AND salespoint IN (SELECT department FROM department WHERE applyforsales = 'STOCK') AND location = $location AND DATE(transactiondate) BETWEEN '$startdate' AND '$enddate' ORDER BY id");
        }elseif($salespoint === "Sundry Sales"){
            $resultinsert = $transaction->executeTransactionByQuerySelector("SELECT * FROM tdetail WHERE status = 'POSTED' AND accountnumber = '" . $resorganisation["default_glsalesaccount"] . "' AND user = '$user' AND salespoint IN (SELECT department FROM department WHERE applyforsales = 'NON STOCK') AND location = $location AND DATE(transactiondate) BETWEEN '$startdate' AND '$enddate' ORDER BY id");
        }
    }
}else{
    exit(badRequest(204,'Invalid location data. Proceed to login'));
}

$data = array();
if($resultinsert){
    foreach($resultinsert as $resultinserta){
        $ref = $resultinserta["reference"];
        $credit = $resultinserta["credit"];
        $salespoint = $resultinserta["salespoint"];
        $description = $resultinserta["description"] . " | " . $resultinserta["tlog"];
        $transactiondate = date("Y-m-d", strtotime($resultinserta["transactiondate"]));
        $default_vat = $resorganisation["default_vattaxaccount"];
        $default_servicecharge = $resorganisation["default_glservicecharge"];
        $default_consumption = $resorganisation["default_glconsumptioncharge"];
        $resultvat = $transaction->executeTransactionByQuerySelector("SELECT * FROM tdetail WHERE status = 'POSTED' AND accountnumber = '$default_vat' AND location = $location AND reference = '$ref' AND transactiondate = '$transactiondate' limit 1");
        $resultsc = $transaction->executeTransactionByQuerySelector("SELECT * FROM tdetail WHERE status = 'POSTED' AND accountnumber = '$default_servicecharge' AND location = $location AND reference = '$ref' AND transactiondate = '$transactiondate' limit 1");
        $resultconsumption = $transaction->executeTransactionByQuerySelector("SELECT * FROM tdetail WHERE status = 'POSTED' AND accountnumber = '$default_consumption' AND location = $location AND reference = '$ref' AND transactiondate = '$transactiondate' limit 1");
        
        //$resultdetail = $inventory->executeByQuerySelector("SELECT * FROM intakeouttake WHERE status LIKE '$status' AND location = $location AND reference = '$ref' ORDER BY id");
        $data[] = array(
            "date" => $transactiondate,
            "salespoint" => $salespoint,
            "description" => $description,
            "Amount" => $credit,
            "Consumption" => $resultconsumption[0]["debit"],
            "VAT" => $resultvat[0]["debit"],
            "Service Charge" => $resultsc[0]["debit"],
            "Total" => round(($credit + $resultconsumption[0]["debit"] + $resultvat[0]["debit"] + $resultsc[0]["debit"]),3)
            );
    }
	echo success($data,200, "Successful","Successful");
}else{
	echo badRequest(204, "Not successful");
}
?>