<?php
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");
session_start();
include_once '../config/database.php';
include_once '../objects/sales.php';
date_default_timezone_set('Asia/Rangoon'); 

$database = new Database();
$db = $database->getConnection();

$data = json_decode(file_get_contents("php://input"));

$branch_id = $data->branch_id; 
$df = $data->df; // expecting '2024-10' format from the input
$dt = $data->dt; 

$container_date2 = array();
$container_cashin = array();
$container_cashout = array();
$totalCashIn = 0;
$totalCashOut = 0;

$query = "SELECT a.date,SUM(IFNULL(sales,0)+IFNULL(income,0)+IFNULL(purchase_return,0)) as cashin,SUM(IFNULL(purchase,0)+IFNULL(sales_return,0)+IFNULL(expense,0)) as cashout  FROM (SELECT ADDDATE('1970-01-01', t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) AS date FROM 
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t0,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t1,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t2,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t3,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t4
WHERE 
ADDDATE('1970-01-01', t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) BETWEEN :df AND :dt
ORDER BY DATE) AS a 
LEFT JOIN
(SELECT date,SUM(amount) AS sales FROM payment 
WHERE branch_id=:branch_id AND DATE>=:df AND DATE<=:dt GROUP BY DATE) AS sales ON a.date=sales.date
LEFT JOIN
(SELECT date,SUM(amount) AS purchase FROM purchase_payment 
WHERE branch_id=:branch_id AND DATE>=:df AND DATE<=:dt GROUP BY DATE) AS purchase ON a.date=purchase.date
LEFT JOIN
(SELECT date,SUM(amount) AS sales_return FROM sales_return_payment 
WHERE branch_id=:branch_id AND DATE>=:df AND DATE<=:dt GROUP BY DATE) AS sales_return ON a.date=sales_return.date
LEFT JOIN
(SELECT date,SUM(amount) AS purchase_return FROM purchase_return_payment 
WHERE branch_id=:branch_id AND DATE>=:df AND DATE<=:dt GROUP BY DATE) AS purchase_return ON a.date=purchase_return.date
LEFT JOIN
(SELECT date,SUM(quantity*unit_price) AS expense FROM expense 
WHERE branch_id=:branch_id AND DATE>=:df AND DATE<=:dt GROUP BY DATE) AS expense ON a.date=expense.date
LEFT JOIN
(SELECT date,SUM(quantity*unit_price) AS income FROM income 
WHERE branch_id=:branch_id AND DATE>=:df AND DATE<=:dt GROUP BY DATE) AS income ON a.date=income.date
GROUP BY a.date
";

$stmt = $db->prepare($query);
$stmt->bindParam(':branch_id', $branch_id);
$stmt->bindParam(':df', $df);
$stmt->bindParam(':dt', $dt);
$stmt->execute();

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    extract($row);
    $category_data = array(
        "date" => $date,
        "cashin" => number_format($cashin,2),
        "cashout" => number_format($cashout,2)
    );

    $totalCashIn += $cashin;
    $totalCashOut += $cashout;
    array_push($container_date2, $date);
    array_push($container_cashin, number_format($cashin,2));
    array_push($container_cashout, number_format($cashout,2));
}

$response = array(
    "container_date2" => $container_date2,
    "container_cashin" => $container_cashin,
    "container_cashout" => $container_cashout,
    "totalCashIn" => number_format($totalCashIn,2),
    "totalCashOut" => number_format($totalCashOut,2)
);

echo json_encode($response);
?>
