<style>
    .receipt {
        width: 400px;
        background-color: #fff;
        color: #000;
        margin: auto;
        border: 1px solid #000;
        font-family: sans-serif;
        /* font-family: "Courier New", monospace; */
        white-space: normal;
        letter-spacing: 0.5px;
    }

    .receipt table {
        width: 100%;
        border-collapse: collapse;
    }

    .receipt td {
        padding: 2px 0;
        font-size: 15px;
    }

    @media print {
        body {
            margin: 0;
        }

        .receipt {
            border: none;
            width: 100%;
        }
    }
</style>


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

$ent_id = isset($_GET["ent"]) ? $_GET["ent"] : "";
$pos_id = isset($_GET["pos"]) ? $_GET["pos"] : "";
$from_date = isset($_GET["from"]) ? $_GET["from"] : "";
$to_date = isset($_GET["to"]) ? $_GET["to"] : "";
$pay_det_created_by = isset($_GET["cashier"]) ? $_GET["cashier"] : "112";

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

$entInfo = getEnterpriseData($ent_id);
$posInfo = getBranchData($pos_id);

// USER INTERFACE TRANSLATE//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);

if (empty(trim((string) $from_date)) || empty(trim((string) $to_date))) exit();

$from_date = "{$from_date} {$posInfo['pos_day_start_time']}";
$to_date_end = date("Y-m-d", strtotime("+1 day", strtotime($to_date))) . " {$posInfo['pos_day_end_time']}";

//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_formated = date("d-M-Y", strtotime($from_date));
$end_date_formated = date("d-M-Y", strtotime($to_date));

?>

<div class="receipt">

    <div style="margin:20px;">
        <div>&nbsp;</div>
        <table width="100%">
            <tr>
                <td style="text-align: center">
                    <div style="font-size: 22px; font-weight: bold; text-transform: uppercase"><?= $entInfo['ent_name']; ?></div>
                    <div><?= $posInfo['pos_address']; ?>.</div>
                    <div><?= "{$posInfo['pos_city']}, {$posInfo['pos_country']}"; ?>.</div>
                </td>
            </tr>
        </table>

        <div style="margin-top: 10px;"></div>

        <table width="100%">
            <tr>
                <td style="text-align: center;">
                    <div style="font-weight: bold; text-transform: uppercase"><?= $posTranslate['cash_summary']; ?></div>
                    <div><?= "{$posTranslate['from_date']}: {$start_date_formated} - {$posTranslate['to_date']}:{$end_date_formated}"; ?></div>
                </td>
            </tr>
        </table>

        <?php

        $user_fullName = "";
        $userQuery = "";
        if (!empty($pay_det_created_by)) {
            $userInfo = getUserData($pay_det_created_by);
            $user_fullName = $userInfo['user_firstname'] . ' ' . $userInfo['user_lastname'];
            $userQuery = "AND pay_det_created_by = {$pay_det_created_by}";
        }

        $sql = "SELECT
        pay_det_sale_type
        ,pay_det_payment_type
        ,IFNULL(SUM(pay_det_income),0) AS pay_det_income
        FROM tb_payments_details 
        WHERE pay_det_pos_id = {$pos_id}
        AND pay_det_date BETWEEN '$from_date' AND '$to_date_end'
        $userQuery
        AND pay_det_status = 'Enabled'
        GROUP BY pay_det_payment_type,pay_det_sale_type
        ORDER BY pay_det_id ASC;";

        $answerPaymentDetail = executePDOFetchAll($sql, "No se pudo listar el reporte de ventas por empleado");

        ?>

        <div style="border-bottom: 1.5px dashed; margin-top: 10px; margin-bottom: 10px;"></div>

        <table width="100%">
            <tr>
                <td colspan="2" style="text-align: center">
                    <div style="font-weight: bold; text-transform: uppercase"><?= "{$posTranslate['branch']}: {$posInfo['pos_name']}"; ?></div>
                </td>
            </tr>
            <tr>
                <td colspan="2" style="text-align: center">
                    <div style="font-weight: bold; text-transform: uppercase"><?= $user_fullName; ?></div>
                </td>
            </tr>
        </table>

        <div style="border-bottom: 1.5px dashed; margin-top: 10px; margin-bottom: 10px;"></div>

        <table width="100%">

            <?php

            $TotalCash = 0;
            $TotalCreditCard = 0;
            $TotalUber = 0;
            $TotalPedidosYa = 0;
            $TotalDidiFood = 0;
            $TotalCashApp = 0;
            $TotalZelle = 0;
            $TotalBankDeposit = 0;
            $TotalMonCash = 0;
            $Totalfinal = 0;

            foreach ($answerPaymentDetail as $key => $value) {

                $sale_pay_type = $value['pay_det_payment_type'];

                if ($value['pay_det_sale_type'] == "Uber Eats") $sale_pay_type = "Uber Eats";
                if ($value['pay_det_sale_type'] == "Pedidos Ya") $sale_pay_type = "Pedidos Ya";
                if ($value['pay_det_sale_type'] == "Didi Food") $sale_pay_type = "Didi Food";

                if ($sale_pay_type == "Uber Eats") $TotalUber += $value['pay_det_income'];
                if ($sale_pay_type == "Pedidos Ya") $TotalPedidosYa += $value['pay_det_income'];
                if ($sale_pay_type == "Didi Food") $TotalDidiFood += $value['pay_det_income'];
                if ($sale_pay_type == "CASH") $TotalCash += $value['pay_det_income'];
                if ($sale_pay_type == "CREDIT CARD") $TotalCreditCard += $value['pay_det_income'];
                if ($sale_pay_type == "CASHAPP") $TotalCashApp += $value['pay_det_income'];
                if ($sale_pay_type == "ZELLE") $TotalZelle += $value['pay_det_income'];
                if ($sale_pay_type == "BANK DEPOSIT") $TotalBankDeposit += $value['pay_det_income'];
                if ($sale_pay_type == "MONCASH") $TotalMonCash += $value['pay_det_income'];

                $Totalfinal += $value['pay_det_income'];
            }

            $posCurrency = $posInfo['pos_currency'];

            if ($posCurrency == "USD") $posCurrency = "$";
            if ($posCurrency == "EUR") $posCurrency = "€";
            if ($posCurrency == "HTG") $posCurrency = "G";
            if ($posCurrency == "DOP") $posCurrency = "RD$";
            if ($posCurrency == "AUD") $posCurrency = "A$";

            if ($TotalCash > 0) { ?>

                <tr>
                    <td style="text-align: left;"><?= mb_strtoupper($posTranslate['cash']); ?></td>
                    <td style="text-align: right;"><?= $posCurrency . ' ' . number_format($TotalCash, 2); ?></td>
                </tr>

            <?php }

            if ($TotalCreditCard > 0) { ?>
                <tr>
                    <td style="text-align: left;"><?= mb_strtoupper($posTranslate['credit_card']); ?></td>
                    <td style="text-align: right;"> <?= $posCurrency . ' ' .  number_format($TotalCreditCard, 2); ?></td>
                </tr>
            <?php }

            if ($TotalUber > 0) { ?>
                <tr>
                    <td style="text-align: left;">UBER</td>
                    <td style="text-align: right;"> <?= $posCurrency . ' ' .  number_format($TotalUber, 2); ?></td>
                </tr>
            <?php }

            if ($TotalPedidosYa > 0) { ?>
                <tr>
                    <td style="text-align: left;">PEDIDOS YA</td>
                    <td style="text-align: right;"> <?= $posCurrency . ' ' .  number_format($TotalPedidosYa, 2); ?></td>
                </tr>
            <?php }

            if ($TotalDidiFood > 0) { ?>
                <tr>
                    <td style="text-align: left;">DIDI FOOD</td>
                    <td style="text-align: right;"> <?= $posCurrency . ' ' .  number_format($TotalDidiFood, 2); ?></td>
                </tr>
            <?php }

            if ($TotalCashApp > 0) { ?>
                <tr>
                    <td style="text-align: left;">CASHAPP</td>
                    <td style="text-align: right;"> <?= $posCurrency . ' ' .  number_format($TotalCashApp, 2); ?></td>
                </tr>
            <?php }

            if ($TotalZelle > 0) { ?>
                <tr>
                    <td style="text-align: left;">ZELLE</td>
                    <td style="text-align: right;"> <?= $posCurrency . ' ' .  number_format($TotalZelle, 2); ?></td>
                </tr>
            <?php }

            if ($TotalBankDeposit > 0) { ?>
                <tr>
                    <td style="text-align: left;"><?= mb_strtoupper($posTranslate['bank_deposit']); ?></td>
                    <td style="text-align: right;"> <?= $posCurrency . ' ' .  number_format($TotalBankDeposit, 2); ?></td>
                </tr>
            <?php }

            if ($TotalMonCash > 0) { ?>
                <tr>
                    <td style="text-align: left;">MONCASH</td>
                    <td style="text-align: right;"> <?= $posCurrency . ' ' .  number_format($TotalMonCash, 2); ?></td>
                </tr>
            <?php } ?>

        </table>

        <div style="border-bottom: 1.5px dashed; margin-top: 10px;"></div>

        <div style="margin-top: 10px;">
            <table width="100%" style="text-transform: uppercase; font-weight: bold;">
                <tr>
                    <td style="text-align: left;"><?= mb_strtoupper($posTranslate['grand_total']); ?></td>
                    <td style="text-align: right;"><?= $posCurrency . ' ' .  number_format($Totalfinal, 2); ?></td>
                </tr>
            </table>
        </div>


    </div>

</div>