<?xml version="1.0" encoding="UTF-8"?>
<!--
    Fusion Claims - March of Dimes Invoice Report
    Meets PCA invoice requirements:
    - Billed to March of Dimes Canada
    - Vendor name, address, HST Number
    - Client details (delivery address)
    - Approved devices/equipment list with sections and notes
    - HVMP contribution amount with taxes
    - HVMP Reference Number
-->
<odoo>
    <template id="report_mod_invoice">
        <t t-call="web.html_container">
            <t t-foreach="docs" t-as="doc">
                <t t-call="web.external_layout">
                    <t t-set="doc" t-value="doc.with_context(lang=doc.partner_id.lang)"/>
                    <!-- Get sale order for MOD fields -->
                    <t t-set="so" t-value="doc.x_fc_source_sale_order_id"/>

                    <style>
                        .fc-mod-inv { font-family: Arial, sans-serif; font-size: 10pt; }
                        .fc-mod-inv table { width: 100%; border-collapse: collapse; margin-bottom: 10px; }
                        .fc-mod-inv table.bordered, .fc-mod-inv table.bordered th, .fc-mod-inv table.bordered td { border: 1px solid #000; }
                        .fc-mod-inv th { background-color: #1a5276; color: white; padding: 6px 8px; font-weight: bold; text-align: center; }
                        .fc-mod-inv td { padding: 6px 8px; vertical-align: top; }
                        .fc-mod-inv .text-center { text-align: center; }
                        .fc-mod-inv .text-end { text-align: right; }
                        .fc-mod-inv .text-start { text-align: left; }
                        .fc-mod-inv .section-row { background-color: #f0f0f0; font-weight: bold; }
                        .fc-mod-inv .note-row { font-style: italic; color: #555; font-size: 9pt; }
                        .fc-mod-inv h4 { color: #1a5276; margin: 0 0 15px 0; font-size: 16pt; }
                        .fc-mod-inv .req-box { border: 2px solid #1a5276; padding: 8px 12px; margin: 6px 0; background-color: #fafafa; }
                    </style>

                    <div class="fc-mod-inv">
                        <div class="page">

                            <!-- Title -->
                            <h4>
                                INVOICE <span t-field="doc.name"/>
                            </h4>

                            <!-- Billed To / Vendor Info -->
                            <table class="bordered">
                                <thead>
                                    <tr>
                                        <th style="width: 50%;">BILLED TO</th>
                                        <th style="width: 50%;">VENDOR</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr>
                                        <td style="height: 80px; font-size: 11pt;">
                                            <div t-field="doc.partner_id"
                                                 t-options="{'widget': 'contact', 'fields': ['name', 'address'], 'no_marker': True}"/>
                                        </td>
                                        <td style="height: 80px; font-size: 11pt;">
                                            <strong t-field="doc.company_id.name"/><br/>
                                            <span t-if="doc.company_id.street" t-field="doc.company_id.street"/><br/>
                                            <t t-if="doc.company_id.street2">
                                                <span t-field="doc.company_id.street2"/><br/>
                                            </t>
                                            <span t-if="doc.company_id.city" t-field="doc.company_id.city"/>,
                                            <span t-if="doc.company_id.state_id" t-field="doc.company_id.state_id.name"/>
                                            <span t-if="doc.company_id.zip" t-field="doc.company_id.zip"/><br/>
                                            <t t-if="doc.company_id.vat">
                                                <strong>HST #:</strong> <span t-field="doc.company_id.vat"/>
                                            </t>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>

                            <!-- Client (Delivery Address) and HVMP Details -->
                            <table class="bordered">
                                <thead>
                                    <tr>
                                        <th style="width: 50%;">DELIVERY ADDRESS (CLIENT)</th>
                                        <th style="width: 50%;">HVMP REFERENCE</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr>
                                        <td style="font-size: 11pt;">
                                            <div t-if="doc.partner_shipping_id" t-field="doc.partner_shipping_id"
                                                 t-options="{'widget': 'contact', 'fields': ['name', 'address', 'phone'], 'no_marker': True}"/>
                                            <div t-elif="so" t-field="so.partner_id"
                                                 t-options="{'widget': 'contact', 'fields': ['name', 'address', 'phone'], 'no_marker': True}"/>
                                        </td>
                                        <td style="font-size: 11pt;">
                                            <t t-if="so and so.x_fc_case_reference">
                                                <strong>HVMP Ref #:</strong>
                                                <span t-esc="so.x_fc_case_reference"/><br/>
                                            </t>
                                            <strong>Invoice Date:</strong>
                                            <span t-field="doc.invoice_date" t-options="{'widget': 'date'}"/><br/>
                                            <strong>Sale Order:</strong>
                                            <span t-if="so" t-esc="so.name"/>
                                            <span t-if="not so">-</span><br/>
                                            <t t-if="so and so.x_fc_case_worker">
                                                <strong>Case Worker:</strong>
                                                <span t-esc="so.x_fc_case_worker.name"/><br/>
                                            </t>
                                            <t t-if="so and so.x_fc_mod_payment_commitment">
                                                <strong>Payment Commitment:</strong>
                                                <span t-esc="'${:,.2f}'.format(so.x_fc_mod_payment_commitment)"/>
                                            </t>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>

                            <!-- Approved Equipment / Invoice Lines (including sections and notes) -->
                            <table class="bordered">
                                <thead>
                                    <tr>
                                        <th style="width: 10%;">SKU</th>
                                        <th style="width: 35%;">APPROVED EQUIPMENT / DESCRIPTION</th>
                                        <th style="width: 8%;">QTY</th>
                                        <th style="width: 13%;">UNIT PRICE</th>
                                        <th style="width: 14%;">TAXES</th>
                                        <th style="width: 20%;">AMOUNT</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <t t-foreach="doc.invoice_line_ids.sorted('sequence')" t-as="line">
                                        <!-- Skip payment term lines -->
                                        <t t-if="line.display_type == 'payment_term'"/>
                                        <!-- Section lines -->
                                        <t t-elif="line.display_type == 'line_section'">
                                            <tr class="section-row">
                                                <td colspan="6" style="padding: 4px 8px;">
                                                    <strong t-esc="line.name"/>
                                                </td>
                                            </tr>
                                        </t>
                                        <!-- Note lines -->
                                        <t t-elif="line.display_type == 'line_note'">
                                            <tr class="note-row">
                                                <td colspan="6" style="padding: 2px 8px;">
                                                    <em t-esc="line.name"/>
                                                </td>
                                            </tr>
                                        </t>
                                        <!-- Product lines -->
                                        <t t-else="">
                                            <tr>
                                                <td class="text-center" style="font-size: 9pt;">
                                                    <t t-if="line.product_id and line.product_id.default_code"
                                                       t-esc="line.product_id.default_code"/>
                                                </td>
                                                <td>
                                                    <strong t-if="line.product_id" t-esc="line.product_id.name"/>
                                                    <t t-if="line.name and (not line.product_id or line.name != line.product_id.name)">
                                                        <t t-set="clean_desc" t-value="line.name"/>
                                                        <t t-if="'] ' in (line.name or '')">
                                                            <t t-set="clean_desc" t-value="line.name.split('] ', 1)[1]"/>
                                                        </t>
                                                        <t t-if="not line.product_id or clean_desc != line.product_id.name">
                                                            <br t-if="line.product_id"/>
                                                            <span t-esc="clean_desc" style="font-size: 9pt; color: #555;"/>
                                                        </t>
                                                    </t>
                                                </td>
                                                <td class="text-center">
                                                    <span t-field="line.quantity"/>
                                                </td>
                                                <td class="text-end">
                                                    <span t-field="line.price_unit"/>
                                                </td>
                                                <td class="text-center" style="font-size: 9pt;">
                                                    <t t-foreach="line.tax_ids" t-as="tax">
                                                        <span t-esc="tax.invoice_label or tax.name"/>
                                                        <t t-if="not tax_last">, </t>
                                                    </t>
                                                </td>
                                                <td class="text-end">
                                                    <span t-field="line.price_subtotal"/>
                                                </td>
                                            </tr>
                                        </t>
                                    </t>
                                </tbody>
                            </table>

                            <!-- Totals -->
                            <div class="row">
                                <div class="col-6"/>
                                <div class="col-6">
                                    <table class="bordered">
                                        <tr>
                                            <td class="text-end" style="width: 60%;"><strong>Subtotal</strong></td>
                                            <td class="text-end" style="width: 40%;"><span t-field="doc.amount_untaxed"/></td>
                                        </tr>
                                        <tr>
                                            <td class="text-end">Taxes</td>
                                            <td class="text-end"><span t-field="doc.amount_tax"/></td>
                                        </tr>
                                        <tr style="font-size: 12pt;">
                                            <td class="text-end" style="background-color: #eaf2f8;">
                                                <strong>HVMP Contribution Amount</strong>
                                            </td>
                                            <td class="text-end" style="background-color: #eaf2f8;">
                                                <strong><span t-field="doc.amount_total"/></strong>
                                            </td>
                                        </tr>
                                    </table>
                                </div>
                            </div>

                            <!-- Payment Note -->
                            <div class="req-box mt-3">
                                <strong>Payment Schedule (as per PCA):</strong>
                                <ul style="margin: 4px 0 0 0;">
                                    <li>Initial Payment (90%): Upon receipt of this invoice</li>
                                    <li>Final Payment (10%): Upon receipt of signed proof of delivery and installation</li>
                                </ul>
                            </div>

                        </div>
                    </div>
                </t>
            </t>
        </t>
    </template>
</odoo>
