<?php
require_once "../../models/Operations.php";

require '../../vendor/autoload.php';
// require_once __DIR__ . '/vendor/autoload.php';

$mpdf = new \Mpdf\Mpdf([
    'mode' => 'utf-8',
    'format' => 'Letter',
    'orientation' => 'P',
    'margin_left' => 15,
    'margin_right' => 15,
    'margin_top' => 12,
    'margin_bottom' => 20,
    'margin_header' => 10,
    'margin_footer' => 10
]);

$pos_id = $_GET["pos"];
$pro_id = $_GET["proId"];
$fromDateGet = $_GET["from"];
$toDateGet = $_GET["to"];

if (empty(intval($pos_id))) exit();

$posInfo = getBranchData($pos_id);
$entInfo = getEnterpriseData($posInfo['pos_ent_id']);

$whereProId = "";
if (!empty($pro_id)) $whereProId = "AND sale_det_pro_id = '$pro_id'";

$DayStartTime = $posInfo["pos_day_start_time"] ?? "07:00:00";
$DayEndTime = $posInfo["pos_day_end_time"] ?? "06:59:59";

$fromDate = date("{$fromDateGet} {$DayStartTime}");
$toDate = date("Y-m-d", strtotime("{$toDateGet} +1 day")) . " {$DayEndTime}";

$whereDate = "AND sale_det_sale_date BETWEEN '{$DayStartTime}' AND '{$DayEndTime}'";
if (!empty(trim((string) $fromDate)) && !empty(trim((string) $toDate))) {
    $whereDate = "AND sale_det_sale_date BETWEEN '{$fromDate}' AND '{$toDate}'";
}

$sql = "SELECT
    DATE_FORMAT( sale_det_sale_date, '%d-%b-%Y' ) AS sale_det_sale_date
    ,(select pro_description FROM tb_products WHERE pro_id = sale_det_pro_id) AS pro_description
    ,IFNULL(SUM(sale_det_pro_qty),0) AS product_quantity

    ,IF(pro_type = 'Service','FULL',(SELECT IFNULL(sum(pur_det_pro_qty),0) FROM tb_purchases_details WHERE pur_det_pro_id=pro_id AND pur_det_status = 'Enabled')-(SELECT IFNULL(sum(sale_det_pro_qty),0) FROM tb_sales_details WHERE sale_det_pro_id=pro_id AND sale_det_status = 'Enabled')+(SELECT IFNULL(SUM(adjust_quantity),0) FROM tb_stock_adjustments WHERE adjust_product_id = pro_id AND adjust_type = 'Increase' AND adjust_status = 'Enabled')-(SELECT IFNULL(SUM(adjust_quantity),0) FROM tb_stock_adjustments WHERE adjust_product_id = pro_id AND adjust_type = 'Decrease' AND adjust_status = 'Enabled')) AS stock_actual

    FROM tb_sales_details
    LEFT JOIN tb_products ON pro_id = sale_det_pro_id
    WHERE sale_det_pos_id = '$pos_id' $whereProId $whereDate
    AND sale_det_status = 'Enabled'
    GROUP BY sale_det_pro_id,DATE_FORMAT(sale_det_sale_date, '%d-%b-%Y')
    ORDER BY sale_det_id ASC;";

// exit($sql);

$productsDetails = executePDOFetchAllAPI($sql);

//Establecemos la fecha actual de la impresion
$timeZone = $posInfo['pos_time_zone'] ?? "America/New_York";
date_default_timezone_set($timeZone);

$currentDateTimeByZone = mdlGetDateTimeByZone($timeZone);
$currentDateTimeByZone = date("d-M-Y H:i:s", strtotime($currentDateTimeByZone));

$start = date_create($fromDateGet);
$end = date_create($toDateGet);
$start_date_format = date_format($start, 'd-M-Y');
$end_date_format = date_format($end, 'd-M-Y');

//POS TRANSLATE
$posLang = $posInfo['pos_lang'];

if ($posLang == "es") {
    $getPosLanguage = file_get_contents("../../lang/pos_es.json");
} else if ($posLang == "fr") {
    $getPosLanguage = file_get_contents("../../lang/pos_fr.json");
} else {
    $getPosLanguage = file_get_contents("../../lang/pos_en.json");
}
$posTranslate = json_decode($getPosLanguage, true);

ob_start();
include(dirname(__FILE__) . '/products-report-html.php');
$html = ob_get_clean();

$mpdf->SetProtection(array('print'));
$mpdf->SetTitle("{$posInfo['pos_name']}-products-report-{$start_date_format}-to-{$end_date_format}");
$mpdf->SetAuthor("EVOSYS POS LLC");
$mpdf->SetWatermarkText("");
$mpdf->showWatermarkText = true;
$mpdf->watermark_font = 'DejaVuSansCondensed';
$mpdf->watermarkTextAlpha = 0.1;
$mpdf->SetDisplayMode('fullpage');

$mpdf->WriteHTML($html);
$mpdf->Output("{$posInfo['pos_name']}-products-report-{$start_date_format}-to-{$end_date_format}.pdf", 'I');
// $mpdf->Output();
