<?php
// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

include_once '../config/database.php';
include_once '../objects/sales.php';
date_default_timezone_set('Asia/Rangoon');
session_start();
$database = new Database();
$db = $database->getConnection();
 
$sales = new Sales($db);
$data = json_decode(file_get_contents("php://input"));

$sales->ym = $data->ym;
$sales->city = $data->city;
$sales->accounting_view = isset($_SESSION['accounting_view'])?$_SESSION['accounting_view']:"";
$opbalance=0;
// $opbalance= (int) $sales->SearchOpeningBalanceOfficeS();

$stmt = $sales->searchCashInS();
$num = $stmt->rowCount();

$arr=array();
$arr["records_in"]=array();
$arr["records_out"]=array();
if($num>0){
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        extract($row);
        $CI=array( 
            "description" => ($description)?$description:"",	
			"cash_in" => number_format($p)
        );  
        $cash_in_total += $p ;
        array_push($arr["records_in"], $CI);
    }
}
$arr["cash_in_total"] = number_format($cash_in_total);

$stmt = $sales->searchCashOutS();
$num = $stmt->rowCount();

if($num>0){
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        extract($row);
        $CO=array( 
            "description" => ($description)?$description:"",	
			"cash_out" => number_format($p),
            // "ascd" => (int)$ascd,
            "t" => $t
        );  
        $cash_out_total += $p ;
        array_push($arr["records_out"], $CO);
    }
}
$arr["cash_out_total"] = number_format($cash_out_total+$opbalance);
$arr["cash_balance"] = number_format($cash_in_total-$cash_out_total-$opbalance);

$arr["opening_balance"] = number_format($opbalance);

echo json_encode($arr);
?>