<?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
]);

// pur_code

if (empty(intval($_GET["pur_id"]))) {
    exit();
} else {
    $pur_id = intval($_GET["pur_id"]);
}

// PURCHASE INFORMATION
$sqlPurchaseInfo = "SELECT pur.pur_id,
ent.ent_name,
ent.ent_document,
ent.ent_document_number,
pos.pos_name,
pos.pos_address,
pos.pos_city,
pos.pos_phone,
pos.pos_currency,
prov.prov_name,
prov.prov_fiscal_number
FROM tb_purchases pur
LEFT JOIN tb_enterprises ent ON pur.pur_ent_id = ent.ent_id
LEFT JOIN tb_pos pos ON pur.pur_pos_id = pos.pos_id
LEFT JOIN tb_providers prov ON pur.pur_provider_id = prov.prov_id
WHERE pur.pur_id = {$pur_id};";
$purchaseInfo = executePDOFetchOne($sqlPurchaseInfo);

// POS INFORMATION
$posInfo = getBranchData($purchaseInfo['pur_pos_id']);

//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));

$purDate = date("d-M-Y", strtotime("{$purchaseInfo['pur_date']}"));


//Obtenemos todos los detalles de la compra actual
$sql = "SELECT
pro_id
,tb_products.pro_description AS pro_description
,pur_det_pro_cost
,pur_det_pro_price
,pur_det_pro_qty
FROM tb_purchases_details
LEFT JOIN tb_products ON pro_id = pur_det_pro_id
WHERE pur_det_pur_id={$pur_id};";
// exit($sql);
$purchaseDetailsInfo = executePDOFetchAll($sql);

$purchaseTotal = 0;

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


$mpdf->SetProtection(array('print'));
$mpdf->SetTitle("{$pur_id}-Purchase-{$purDate}");
$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("{$pur_id}-Purchase-{$purDate}.pdf", 'I');
// $mpdf->Output();
