<?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_date = array();
// $container_purchase = array();
$container_sales = array();
$totalPurchase = 0;
$totalSales = 0;

$query = "SELECT 
    date,
    IFNULL(sales, 0) - IFNULL(sales_return, 0) AS sales
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 sales_date, sub_total-discount_amount+((sub_total-discount_amount)*tax/100)+transportation_charges AS sales 
        FROM sales 
        left join (select invoice_no,sum(quantity*price) as sub_total from sales_detail group by invoice_no) as sd on sales.invoice_no=sd.invoice_no
        WHERE branch_id = :branch_id 
          AND sales_date >= :df 
          AND sales_date <= :dt 
        GROUP BY sales_date
    ) AS b 
    ON a.date = b.sales_date
LEFT JOIN 
    (
        SELECT sales_return_date, r_sub_total-discount_amount+((r_sub_total-discount_amount)*tax/100) AS sales_return 
        FROM sales_return 
        left join (select invoice_no,sum(quantity*price) as r_sub_total from sales_return_detail group by invoice_no) as srd on sales_return.invoice_no=srd.invoice_no
        WHERE branch_id = :branch_id 
          AND sales_return_date >= :df 
          AND sales_return_date <= :dt 
        GROUP BY sales_return_date
    ) AS d 
    ON a.date = d.sales_return_date
ORDER 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,
        "sales" => number_format($sales,2)
    );

    $totalSales += $sales;
    array_push($container_date, $date);
    array_push($container_sales, number_format($sales,2));
}

$response = array(
    "container_date" => $container_date,
    "container_sales" => $container_sales,
    "totalSales" => number_format($totalSales,2)
);

echo json_encode($response);
?>
