<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <template id="l10n_in_report_invoice_document_inherit" inherit_id="account.report_invoice_document" primary="True">
        <xpath expr="//address[@t-field='o.partner_id']" position="after">
            <span t-field="o.l10n_in_gstin" t-if="o.company_id.account_fiscal_country_id.code == 'IN'"/>
        </xpath>
        <xpath expr="//t[@t-set='address']" position="inside">
            <t t-if="o.company_id.account_fiscal_country_id.code == 'IN' and o.l10n_in_state_id" class="mt16">
                Place of supply: <span t-esc="o.l10n_in_state_id.name"/>
            </t>
        </xpath>

        <xpath expr="//table[@name='invoice_line_table']/thead/tr/th[1]" position="after">
            <t t-if="o.company_id.account_fiscal_country_id.code == 'IN'">
                <th>HSN/SAC</th>
            </t>
        </xpath>

        <xpath expr="//t[@name='account_invoice_line_accountable']/td[1]" position="after">
            <td t-if="o.company_id.account_fiscal_country_id.code == 'IN'">
              <span t-if="line.product_id.l10n_in_hsn_code" t-field="line.product_id.l10n_in_hsn_code"></span>
            </td>
        </xpath>

        <xpath expr="//div[@id='qrcode_info']" position="attributes">
            <attribute name="t-if" add="and o.company_id.account_fiscal_country_id.code != 'IN'" separator=" "/>
        </xpath>

        <xpath expr="//div[@id='qrcode_info']" position="after">
            <t t-if="o.company_id.account_fiscal_country_id.code == 'IN'">
                <div style="display:-webkit-flex;" class="flex-column">
                    <strong>PAYMENT QR CODE</strong>
                    <div t-if="o.company_id.l10n_in_upi_id" class="mt-1 mb-1">
                        <p class="mb-0">UPI ID:</p>
                        <span class="mb-0" t-field="o.company_id.l10n_in_upi_id"/>
                    </div>
                    <div class="d-flex flex-row" t-attf-style="#{'-webkit-transform:translateX(-0.5rem);' if report_type != 'html' else '-webkit-transform:translate(-0.5rem,-0.8rem);'}">
                        <img src="/l10n_in/static/src/img/PhonePe-Logo.svg" style="width:4.5rem;"/>
                        <img src="/l10n_in/static/src/img/Google_Pay-Logo.svg" style="width:3.5rem;"/>
                        <img src="/l10n_in/static/src/img/Paytm-Logo.svg" style="width:4rem;"/>
                        <img src="/l10n_in/static/src/img/Upi-logo.svg" t-attf-style="#{'' if report_type != 'html' else 'padding:0.5rem;'} width:4rem;"/>
                    </div>
                </div>
            </t>
        </xpath>

        <xpath expr="//h2" position="replace">
            <h2>
                <span t-if="o.move_type == 'out_invoice' and o.state == 'posted'" t-field="o.journal_id.name"/>
                <span t-elif="o.move_type == 'out_invoice' and o.state == 'draft'">Draft <span t-field="o.journal_id.name"/></span>
                <span t-elif="o.move_type == 'out_invoice' and o.state == 'cancel'">Cancelled <span t-field="o.journal_id.name"/></span>
                <span t-elif="o.move_type == 'out_refund' and o.state == 'posted'">Credit Note</span>
                <span t-elif="o.move_type == 'out_refund' and o.state == 'draft'">Draft Credit Note</span>
                <span t-elif="o.move_type == 'out_refund' and o.state == 'cancel'">Cancelled Credit Note</span>
                <span t-elif="o.move_type == 'in_refund'">Vendor Credit Note</span>
                <span t-elif="o.move_type == 'in_invoice'">Vendor Bill</span>
                <span t-if="o.name != '/'" t-field="o.name"/>
            </h2>
        </xpath>

        <xpath expr="//div[@id='payment_term']" position="before">
            <t t-if="o.company_id.account_fiscal_country_id.code == 'IN'">
                <t t-set="hsn_summary" t-value="o._l10n_in_get_hsn_summary_table()"/>
                <t t-if="hsn_summary">
                    <div name="l10n_in_hsn_summary" class="mt-3">
                        <table class="table table-sm table-borderless col-6" style="page-break-inside: avoid;">
                            <thead>
                                <th t-att-colspan="hsn_summary['nb_columns']"><h3>HSN Summary</h3></th>
                            </thead>
                            <thead>
                                <th>HSN/SAC</th>
                                <th class="text-end">Quantity</th>
                                <th class="text-end">Rate %</th>
                                <th class="text-end">Taxable Value</th>
                                <th class="text-end" t-if="hsn_summary['has_gst']">SGST</th>
                                <th class="text-end" t-if="hsn_summary['has_gst']">CGST</th>
                                <th class="text-end" t-if="hsn_summary['has_igst']">IGST</th>
                                <th class="text-end" t-if="hsn_summary['has_cess']">CESS</th>
                            </thead>
                            <tr t-foreach="hsn_summary['items']" t-as="item">
                                <td t-esc="item['l10n_in_hsn_code']"/>
                                <td class="text-end">
                                    <span t-esc="item['quantity']"/>
                                    <span t-if="hsn_summary['display_uom']">(<t t-esc="item['uom'].name"/>)</span>
                                </td>
                                <td class="text-end" t-esc="item['rate']"/>
                                <td class="text-end">
                                    <span t-esc="item['amount_untaxed']"
                                          t-options="{'widget': 'monetary', 'display_currency': o.currency_id}"/>
                                </td>
                                <td class="text-end" t-if="hsn_summary['has_gst']">
                                    <span t-esc="item['tax_amount_sgst']"
                                          t-options="{'widget': 'monetary', 'display_currency': o.currency_id}"/>
                                </td>
                                <td class="text-end" t-if="hsn_summary['has_gst']">
                                    <span t-esc="item['tax_amount_cgst']"
                                          t-options="{'widget': 'monetary', 'display_currency': o.currency_id}"/>
                                </td>
                                <td class="text-end" t-if="hsn_summary['has_igst']">
                                    <span t-esc="item['tax_amount_igst']"
                                          t-options="{'widget': 'monetary', 'display_currency': o.currency_id}"/>
                                </td>
                                <td class="text-end" t-if="hsn_summary['has_cess']">
                                    <span t-esc="item['tax_amount_cess']"
                                          t-options="{'widget': 'monetary', 'display_currency': o.currency_id}"/>
                                </td>
                            </tr>
                        </table>
                    </div>
                </t>
            </t>
        </xpath>

    </template>

    <!-- Workaround for Studio reports, see odoo/odoo#60660 -->
    <template id="report_invoice" inherit_id="account.report_invoice">
        <xpath expr='//t[@t-call="account.report_invoice_document"]' position="after">
            <t t-elif="o._get_name_invoice_report() == 'l10n_in.l10n_in_report_invoice_document_inherit'"
               t-call="l10n_in.l10n_in_report_invoice_document_inherit"
               t-lang="lang"/>
        </xpath>
    </template>

</odoo>
