<style>
    body {
        font-family: sans-serif;
        /* font-family: "Courier New", monospace; */
    }

    table thead td {
        text-align: left;
        border-top: 1.2px dashed;
        border-bottom: 1.2px dashed;
    }

    .description {
        font-size: 13px;
    }

    .pos_ticket_footer {
        font-size: 12px;
        text-transform: uppercase;
    }

    .product_title {
        font-size: 13px;
        text-transform: uppercase;
    }

    .summary_title {
        font-size: 13px;
        font-weight: bold;
        text-transform: uppercase;
    }

    .summary_total {
        font-size: 13px;
        text-transform: uppercase;
    }

    .copyright {
        font-size: 11px;
    }
</style>

<div class="table_summary">

    <table width="100%">
        <tr>
            <td style="text-align: center">

                <?php if (!empty($entInfo['ent_logo'])) { ?>
                    <img src="<?= htmlspecialchars(!empty($entInfo['ent_logo']) ? $entInfo['ent_logo'] : 'assets/images/no-image.png'); ?>"
                        width="70"
                        style="margin-bottom: 10px;"
                        alt="<?= htmlspecialchars($entInfo['ent_name']); ?>"
                        onerror="this.onerror=null;this.src='assets/images/no-image.png';">
                <?php } ?>

                <div style="font-size: 17px; font-weight: bold; text-transform: uppercase;"><?= $entInfo['ent_name']; ?></div>
                <div class="description"><?= $posInfo['pos_address']; ?>.</div>
                <div class="description"><?= "{$posInfo['pos_city']}, {$posInfo['pos_country']}"; ?>.</div>
                <div class="description"><?= "{$posInfo['pos_phone']}" ?>
                    <?php if (!empty($posInfo['pos_phone2'])) {
                        echo " / {$posInfo['pos_phone2']}";
                    } ?></div>
                <div class="description"><?= $ent_document; ?></div>
            </td>
        </tr>
    </table>

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

    <?php if (!empty($sale_ncf_type)) { ?>
        <table width="100%">
            <tr>
                <td style="text-align: center;">
                    <div class="description"><?= "{$fiscal_entry_description}: {$sale_ncf_type}{$sale_ncf_number}"; ?></div>
                    <div class="description"><?= $posTranslate['valid_until']; ?>: <?= $fiscal_entry_expire_date; ?></div>
                </td>
            </tr>
        </table>
        <div style="border-bottom: 1.2px dashed; margin-bottom: 5px; margin-top: 5px"></div>
    <?php } ?>

    <table width="100%">
        <tr>
            <td style="text-align: left;">
                <div class="description" style="text-transform: uppercase;">POS: <?= $posInfo['pos_name']; ?></div>
                <div class="description"><?= "{$posTranslate['sale_date']}: {$sale_date}"; ?></div>

                <?php
                if ($posInfo["pos_business_category"] == "Restaurant" || $posInfo["pos_business_category"] == "Nightclub") { ?>

                    <div class="description"><?= "{$posTranslate['sale_number']}: {$saleInfo['sale_id']}"; ?> - <?= "{$posTranslate['type']}: {$saleInfo['sale_type']}"; ?>
                        <?php if ($saleInfo['sale_type'] == "Local") {
                            echo " / {$posTranslate['table']}: {$saleInfo['sale_table_id']}";
                        } ?></div>

                <?php } else { ?>

                    <div class="description"><?= "{$posTranslate['sale_number']}: {$saleInfo['sale_id']} - {$posTranslate['type']}: {$saleInfo['sale_type']}"; ?></div>

                <?php } ?>

                <div class="description"><?= "{$posTranslate['attended_by']}: {$waiterShortName}"; ?>
                    <?php if (!empty($cashierShortName)) {
                        echo " - {$posTranslate['cashier']}: {$cashierShortName}";
                    } ?>
                </div>

                <div class="description"><?= "{$posTranslate['customer']}: {$cusInfo['cus_full_name']}"; ?></div>
                <?php if (!empty($sale_ncf_type) && $sale_ncf_type == "B01") { ?>
                    <div class="description"><?= "{$posTranslate['customer_document']}: {$cusInfo['cus_fiscal_number']}"; ?></div>
                <?php } ?>

                <?php if ($saleInfo['sale_type'] == "Delivery") { ?>
                    <div class="description"><?= $posTranslate['address']; ?>: <?= $cusInfo['cus_address']; ?></div>
                <?php } ?>
            </td>
        </tr>
    </table>

    <?php
    if ($saleInfo['sale_status'] == "Disabled") { ?>
        <table width="100%">
            <tr>
                <td style="text-align: center">
                    <div style="font-size: 15px; font-weight: bold; text-transform: uppercase;"><?= "** {$posTranslate['canceled']} **"; ?></div>
                </td>
            </tr>
        </table>
    <?php } ?>

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

    <table width="100%" cellpadding="8">
        <thead>
            <tr>
                <td class="product_title" width="50%" style="text-align: left;"><?= $posTranslate['product']; ?></td>
                <td class="product_title" width="25%" style="text-align: left;"><?= $posTranslate['price']; ?></td>
                <td class="product_title" width="25%" style="text-align: right;"><?= $posTranslate['total']; ?></td>
            </tr>
        </thead>
    </table>

    <?php
    $final_total = 0;
    $subtotal = 0;
    $grandSubTotal = 0;
    $sale_tax_value = 0;
    $sale_tax_law_value = 0;
    ?>

    <table width="100%" cellpadding="5">
        <tbody>
            <!-- LOOP ITEMS HERE -->
            <?php foreach ($saleDetInfo as $key => $get_value) {

                $pro_description = getProductsData($get_value['sale_det_pro_id'])['pro_description'];
                $sale_det_pro_qty = $get_value['sale_det_pro_qty'];
                $sale_det_pro_price = $get_value['sale_det_pro_price'];
                $subtotal = $sale_det_pro_qty * $sale_det_pro_price;
                $grandSubTotal += $subtotal;
            ?>
                <tr>
                    <td class="description" width="50%" style="text-align: left;"><?= "{$sale_det_pro_qty} {$pro_description}"; ?></td>
                    <td class="description" width="25%" style="text-align: left;"><?= number_format($sale_det_pro_price, 2); ?></td>
                    <td class="description" width="25%" style="text-align: right;"><?= number_format($subtotal, 2); ?></td>
                </tr>
            <?php } ?>

            <!-- END LOOP ITEMS HERE -->
        </tbody>
    </table>

    <div style="border-bottom: 1.2px dashed; margin-bottom: 15px"></div>

    <table width="100%">
        <tbody>
            <tr>
                <td class="summary_total" style="text-align: right;"><?= $posTranslate['subtotal']; ?>:</td>
                <td class="summary_total" style="text-align: right;"><?= number_format($grandSubTotal, 2); ?></td>
            </tr>

            <?php if ($saleInfo['sale_discount_value'] > 0) { ?>
                <tr>
                    <td class="summary_total" style="text-align: right;"><?= $posTranslate['discount']; ?>:</td>
                    <td class="summary_total" style="text-align: right;"><?= number_format($saleInfo['sale_discount_value'], 2); ?></td>
                </tr>
            <?php } ?>

            <?php if ($saleInfo['sale_tax_value'] > 0) { ?>
                <tr>
                    <td class="summary_total" style="text-align: right;"><?= $posTranslate['tax']; ?> (<?= number_format($saleInfo['sale_tax_percent'], 2); ?>%):</td>
                    <td class="summary_total" style="text-align: right;"><?= number_format($saleInfo['sale_tax_value'], 2); ?></td>
                </tr>
            <?php } ?>

            <?php if ($saleInfo['sale_tax_law_value'] > 0) { ?>
                <tr>
                    <td class="summary_total" style="text-align: right;"><?= $posTranslate['tipping']; ?> (<?= number_format($saleInfo['sale_tax_law_percent'], 2); ?>%):</td>
                    <td class="summary_total" style="text-align: right;"><?= number_format($saleInfo['sale_tax_law_value'], 2); ?></td>
                </tr>
            <?php } ?>

            <?php if ($saleInfo['sale_type'] == "Delivery") { ?>
                <tr>
                    <td class="summary_total" style="text-align: right;"><?= $posTranslate['delivery']; ?>:</td>
                    <td class="summary_total" style="text-align: right;"><?= number_format($saleInfo['sale_cost_delivery'], 2); ?></td>
                </tr>
            <?php }

            $final_total = ($grandSubTotal + $saleInfo['sale_tax_value'] + $saleInfo['sale_tax_law_value'] + $saleInfo['sale_cost_delivery']) - $saleInfo['sale_discount_value'];
            $final_total_usd = 0;

            if ($posInfo['pos_currency'] != "USD" && $posInfo['pos_usd_exchange_rate'] > 0) {
                $final_total_usd = $final_total / $posInfo['pos_usd_exchange_rate'];
            }

            // $debtChange = $totalIncome - $final_total;
            $debtChange = $totalPaid - $final_total;

            if ($debtChange < 0) {
                $debtChangeText = $posTranslate['debt'];
                $sale_debtValue = $debtChange * (-1);
                $sale_debtValue = number_format($sale_debtValue, 2, '.', ',');
            } else {
                $debtChangeText = $posTranslate['change'];
                $sale_debtValue = $debtChange;
                $sale_debtValue = number_format($sale_debtValue, 2, '.', ',');
            }

            ?>

            <tr>
                <td class="summary_title" style="text-align: right;"><?= $posTranslate['total'] . " " . $posInfo["pos_currency"]; ?>:</td>
                <td class="summary_title" style="text-align: right;"><?= number_format($final_total, 2); ?></td>
            </tr>

            <?php
            if ($final_total_usd > 0) { ?>
                <tr>
                    <td class="summary_title" style="text-align: right;"><?= $posTranslate['total'] . " USD"; ?>:</td>
                    <td class="summary_title" style="text-align: right;"><?= number_format($final_total_usd, 2); ?></td>
                </tr>
            <?php } ?>

            <tr>
                <td class="summary_total" style="text-align: right;"><?= $posTranslate['paid']; ?>:</td>
                <td class="summary_total" style="text-align: right;"><?= number_format($totalPaid, 2); ?></td>
            </tr>

            <tr>
                <td class="summary_total" style="text-align: right;"><?= $debtChangeText; ?>:</td>
                <td class="summary_total" style="text-align: right;"><?= $sale_debtValue; ?></td>
            </tr>

            <!-- <tr>
                <td class="summary_total" style="text-align: right;"><?= $posTranslate['charged']; ?>:</td>
                <td class="summary_total" style="text-align: right;"><?= number_format($totalIncome, 2); ?></td>
            </tr> -->

            <!-- <?php if ($debtChange < 0) { ?>
                <tr>
                    <td class="summary_total" style="text-align: right;"><?= $debtChangeText; ?>:</td>
                    <td class="summary_total" style="text-align: right;"><?= $sale_debtValue; ?></td>
                </tr>
            <?php } else { ?>
                <tr>
                    <td class="summary_total" style="text-align: right;"><?= $debtChangeText; ?>:</td>
                    <td class="summary_total" style="text-align: right;"><?= $lastPaymentChange; ?></td>
                </tr>
            <?php }
            ?> -->

        </tbody>
    </table>

    <?php
    if ($totalIncome > 0) {
        $formattedString = implode(" & ", $paymentTypes);
    ?>

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

        <table width="100%">
            <tbody>
                <tr>
                    <td class="description" style="text-align: left;"><?= $posTranslate['payment_method']; ?>:</td>
                    <td class="description" style="text-align: right;"><?= ucwords(strtolower($formattedString)); ?></td>
                </tr>
            </tbody>
        </table>
    <?php } ?>

    <?php
    if ($posInfo['pos_cash_back_status'] == "On" && $posInfo['pos_cash_back_percentage'] > 0) { ?>

        <div style="border-bottom: 1.2px dashed; margin-top: 15px; margin-bottom: 5px"></div>

        <table width="100%">
            <tbody>
                <?php
                if ($cusInfo['cus_current_cashback'] > 0) { ?>

                    <tr>
                        <td class="description" style="text-align: left;">CashBack:</td>
                        <td class="description" style="text-align: right;"><?= number_format($cusInfo['cus_current_cashback'], 2); ?></td>
                    </tr>
                <?php } ?>
                <tr>
                    <td class="description" style="text-align: left;"><?= $posTranslate['available_cashback']; ?>:</td>
                    <td class="description" style="text-align: right;"><?= number_format($cusInfo['cus_available_cashback'], 2); ?></td>
                </tr>
            </tbody>
        </table>

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

    <?php } ?>

    <div class="pos_ticket_footer" style="margin-top: 15px; margin-bottom: 20px; text-align: center;"><?= $posInfo['pos_ticket_footer']; ?></div>

    <div style="font-size: 11px; text-align: center;"> Software Developped by <b>ITECEVO LLC</b></div>
    <div style="font-size: 11px; text-align: center;"> +1 809-838-9538 / info@itecevo.com</div>
</div>