<?xml version="1.0" encoding="utf-8"?>
<odoo>

    <template id="report_poynt_receipt_document">
        <t t-call="web.html_container">
            <t t-foreach="docs" t-as="tx">
                <t t-set="receipt" t-value="tx._get_poynt_receipt_values()"/>
                <t t-set="company" t-value="tx.company_id or tx.env.company"/>
                <t t-set="is_refund" t-value="tx.operation == 'refund' or tx.amount &lt; 0"/>
                <t t-call="web.external_layout">
                    <div class="page" style="font-family: 'Courier New', Courier, monospace;">

                        <div class="text-center mb-3">
                            <h4>
                                <strong t-if="is_refund">REFUND RECEIPT</strong>
                                <strong t-else="">PAYMENT RECEIPT</strong>
                            </h4>
                        </div>

                        <!-- Transaction details table -->
                        <table class="table table-sm table-borderless" style="font-size: 13px;">
                            <tbody>
                                <tr>
                                    <td class="text-muted" style="width: 40%;">Date</td>
                                    <td>
                                        <t t-if="receipt.get('created_at')">
                                            <t t-esc="receipt['created_at']"/>
                                        </t>
                                        <t t-else="">
                                            <span t-field="tx.create_date" t-options="{'widget': 'datetime'}"/>
                                        </t>
                                    </td>
                                </tr>
                                <tr>
                                    <td class="text-muted">Reference</td>
                                    <td><strong t-field="tx.reference"/></td>
                                </tr>
                                <tr t-if="receipt.get('transaction_id')">
                                    <td class="text-muted">Transaction ID</td>
                                    <td style="font-size: 11px;"><t t-esc="receipt['transaction_id']"/></td>
                                </tr>
                                <tr t-if="is_refund and tx.source_transaction_id">
                                    <td class="text-muted">Original Transaction</td>
                                    <td style="font-size: 11px;"><t t-esc="tx.source_transaction_id.reference"/></td>
                                </tr>
                                <tr>
                                    <td class="text-muted">Type</td>
                                    <td>
                                        <strong t-if="is_refund" style="color: #dc3545;">REFUND</strong>
                                        <strong t-else="" style="color: #28a745;">SALE</strong>
                                    </td>
                                </tr>
                            </tbody>
                        </table>

                        <hr style="border-top: 1px solid #999;"/>

                        <!-- Card info -->
                        <table class="table table-sm table-borderless" style="font-size: 13px;">
                            <tbody>
                                <tr t-if="receipt.get('card_type')">
                                    <td class="text-muted" style="width: 40%;">Card Type</td>
                                    <td><t t-esc="receipt['card_type']"/></td>
                                </tr>
                                <tr t-if="receipt.get('card_last4')">
                                    <td class="text-muted">Card Number</td>
                                    <td>**** **** **** <t t-esc="receipt['card_last4']"/></td>
                                </tr>
                                <tr t-if="receipt.get('card_holder')">
                                    <td class="text-muted">Cardholder</td>
                                    <td><t t-esc="receipt['card_holder']"/></td>
                                </tr>
                                <tr t-if="receipt.get('entry_mode')">
                                    <td class="text-muted">Entry Mode</td>
                                    <td><t t-esc="receipt['entry_mode']"/></td>
                                </tr>
                            </tbody>
                        </table>

                        <hr style="border-top: 1px solid #999;"/>

                        <!-- Amounts -->
                        <table class="table table-sm table-borderless" style="font-size: 14px;">
                            <tbody>
                                <tr t-if="receipt.get('tip_amount') and not is_refund">
                                    <td class="text-muted" style="width: 40%;">Subtotal</td>
                                    <td class="text-end">
                                        <t t-esc="receipt.get('currency', 'CAD')"/>
                                        <t t-esc="'%.2f' % (receipt.get('transaction_amount', 0) - receipt.get('tip_amount', 0))"/>
                                    </td>
                                </tr>
                                <tr t-if="receipt.get('tip_amount') and not is_refund">
                                    <td class="text-muted">Tip</td>
                                    <td class="text-end">
                                        <t t-esc="receipt.get('currency', 'CAD')"/>
                                        <t t-esc="'%.2f' % receipt['tip_amount']"/>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <strong t-if="is_refund">REFUND TOTAL</strong>
                                        <strong t-else="">TOTAL</strong>
                                    </td>
                                    <td class="text-end">
                                        <strong t-attf-style="color: {{ 'dc3545' if is_refund else '000' }};">
                                            <t t-if="is_refund">- </t>
                                            <t t-esc="receipt.get('currency', 'CAD')"/>
                                            <t t-esc="'%.2f' % abs(receipt.get('transaction_amount', 0) or abs(tx.amount))"/>
                                        </strong>
                                    </td>
                                </tr>
                            </tbody>
                        </table>

                        <hr style="border-top: 1px solid #999;"/>

                        <!-- Approval -->
                        <table class="table table-sm table-borderless" style="font-size: 13px;">
                            <tbody>
                                <tr t-if="receipt.get('approval_code')">
                                    <td class="text-muted" style="width: 40%;">Approval Code</td>
                                    <td><strong><t t-esc="receipt['approval_code']"/></strong></td>
                                </tr>
                                <tr t-if="receipt.get('status')">
                                    <td class="text-muted">Status</td>
                                    <td><t t-esc="receipt['status']"/></td>
                                </tr>
                                <tr t-if="receipt.get('processor')">
                                    <td class="text-muted">Processor</td>
                                    <td><t t-esc="receipt['processor']"/></td>
                                </tr>
                            </tbody>
                        </table>

                        <hr style="border-top: 2px dashed #333;"/>

                        <!-- Footer -->
                        <div class="text-center mt-3" style="font-size: 12px;">
                            <p class="mb-1">
                                <t t-if="is_refund">Credit Note: </t>
                                <t t-else="">Invoice: </t>
                                <strong t-esc="', '.join(tx.invoice_ids.mapped('name'))" />
                            </p>
                            <p class="mb-1">
                                Customer: <strong t-field="tx.partner_id.name"/>
                            </p>
                            <p class="text-muted mt-3" t-if="is_refund">
                                Refund processed. The amount will be credited to your
                                card within 3-5 business days.
                            </p>
                            <p class="text-muted mt-3" t-else="">
                                Thank you for your payment.
                            </p>
                        </div>

                    </div>
                </t>

                <!-- Page 2: Original Sale Receipt (only for refunds) -->
                <t t-if="is_refund and tx.source_transaction_id">
                    <t t-set="src_tx" t-value="tx.source_transaction_id"/>
                    <t t-set="src_receipt" t-value="tx._get_source_receipt_values()"/>
                    <t t-if="src_receipt">
                        <t t-call="web.external_layout">
                            <div class="page" style="font-family: 'Courier New', Courier, monospace;">

                                <div class="text-center mb-3">
                                    <h4><strong>ORIGINAL SALE RECEIPT</strong></h4>
                                    <p class="text-muted" style="font-size: 12px;">
                                        Reference for refund <strong t-field="tx.reference"/>
                                    </p>
                                </div>

                                <!-- Transaction details -->
                                <table class="table table-sm table-borderless" style="font-size: 13px;">
                                    <tbody>
                                        <tr>
                                            <td class="text-muted" style="width: 40%;">Date</td>
                                            <td>
                                                <t t-if="src_receipt.get('created_at')">
                                                    <t t-esc="src_receipt['created_at']"/>
                                                </t>
                                                <t t-else="">
                                                    <span t-field="src_tx.create_date" t-options="{'widget': 'datetime'}"/>
                                                </t>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td class="text-muted">Reference</td>
                                            <td><strong t-field="src_tx.reference"/></td>
                                        </tr>
                                        <tr t-if="src_receipt.get('transaction_id')">
                                            <td class="text-muted">Transaction ID</td>
                                            <td style="font-size: 11px;"><t t-esc="src_receipt['transaction_id']"/></td>
                                        </tr>
                                        <tr>
                                            <td class="text-muted">Type</td>
                                            <td><strong style="color: #28a745;">SALE</strong></td>
                                        </tr>
                                    </tbody>
                                </table>

                                <hr style="border-top: 1px solid #999;"/>

                                <!-- Card info -->
                                <table class="table table-sm table-borderless" style="font-size: 13px;">
                                    <tbody>
                                        <tr t-if="src_receipt.get('card_type')">
                                            <td class="text-muted" style="width: 40%;">Card Type</td>
                                            <td><t t-esc="src_receipt['card_type']"/></td>
                                        </tr>
                                        <tr t-if="src_receipt.get('card_last4')">
                                            <td class="text-muted">Card Number</td>
                                            <td>**** **** **** <t t-esc="src_receipt['card_last4']"/></td>
                                        </tr>
                                        <tr t-if="src_receipt.get('card_holder')">
                                            <td class="text-muted">Cardholder</td>
                                            <td><t t-esc="src_receipt['card_holder']"/></td>
                                        </tr>
                                        <tr t-if="src_receipt.get('entry_mode')">
                                            <td class="text-muted">Entry Mode</td>
                                            <td><t t-esc="src_receipt['entry_mode']"/></td>
                                        </tr>
                                    </tbody>
                                </table>

                                <hr style="border-top: 1px solid #999;"/>

                                <!-- Amounts -->
                                <table class="table table-sm table-borderless" style="font-size: 14px;">
                                    <tbody>
                                        <tr t-if="src_receipt.get('tip_amount')">
                                            <td class="text-muted" style="width: 40%;">Subtotal</td>
                                            <td class="text-end">
                                                <t t-esc="src_receipt.get('currency', 'CAD')"/>
                                                <t t-esc="'%.2f' % (src_receipt.get('transaction_amount', 0) - src_receipt.get('tip_amount', 0))"/>
                                            </td>
                                        </tr>
                                        <tr t-if="src_receipt.get('tip_amount')">
                                            <td class="text-muted">Tip</td>
                                            <td class="text-end">
                                                <t t-esc="src_receipt.get('currency', 'CAD')"/>
                                                <t t-esc="'%.2f' % src_receipt['tip_amount']"/>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td><strong>TOTAL</strong></td>
                                            <td class="text-end">
                                                <strong>
                                                    <t t-esc="src_receipt.get('currency', 'CAD')"/>
                                                    <t t-esc="'%.2f' % abs(src_receipt.get('transaction_amount', 0) or abs(src_tx.amount))"/>
                                                </strong>
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>

                                <hr style="border-top: 1px solid #999;"/>

                                <!-- Approval -->
                                <table class="table table-sm table-borderless" style="font-size: 13px;">
                                    <tbody>
                                        <tr t-if="src_receipt.get('approval_code')">
                                            <td class="text-muted" style="width: 40%;">Approval Code</td>
                                            <td><strong><t t-esc="src_receipt['approval_code']"/></strong></td>
                                        </tr>
                                        <tr t-if="src_receipt.get('status')">
                                            <td class="text-muted">Status</td>
                                            <td><t t-esc="src_receipt['status']"/></td>
                                        </tr>
                                        <tr t-if="src_receipt.get('processor')">
                                            <td class="text-muted">Processor</td>
                                            <td><t t-esc="src_receipt['processor']"/></td>
                                        </tr>
                                    </tbody>
                                </table>

                                <hr style="border-top: 2px dashed #333;"/>

                                <!-- Footer -->
                                <div class="text-center mt-3" style="font-size: 12px;">
                                    <p class="mb-1">
                                        Invoice: <strong t-esc="', '.join(src_tx.invoice_ids.mapped('name'))"/>
                                    </p>
                                    <p class="mb-1">
                                        Customer: <strong t-field="src_tx.partner_id.name"/>
                                    </p>
                                    <p class="text-muted mt-3">
                                        This is the original sale transaction associated
                                        with the refund on Page 1.
                                    </p>
                                </div>

                            </div>
                        </t>
                    </t>
                </t>

            </t>
        </t>
    </template>

</odoo>
