<?xml version="1.0" encoding="UTF-8" ?>
<templates>
    <t t-name="fusion_accounting.JournalReportLine" t-inherit="fusion_accounting.AccountReportLineCustomizable">
        <xpath expr="//tr[@data-id='line']" position="attributes">
            <attribute name="t-if">!('is_tax_section_line' in props.line)</attribute>
        </xpath>

        <xpath expr="//tr[@data-id='line']" position="after">
            <t t-else="">
                <tr id="line" class="subtable" t-att-class="lineClasses">
                    <td t-att-colspan="2">
                        <t t-if="'tax_report_lines' in props.line">
                            <t t-set="taxesByCountry" t-value="props.line.tax_report_lines"/>
                            <t t-set="hasTaxesSeveralCountries" t-value="Object.keys(taxesByCountry).length > 1"/>
                            <t t-set="extraColumns" t-value="props.line.extra_columns"/>

                            <table class="table">
                                <thead>
                                    <tr>
                                        <th t-att-colspan="(taxesByCountry.length > 1 ? 4 : 3) + extraColumns">
                                            Taxes Applied
                                            <t t-if="controller.groups.account_user">
                                                <a
                                                    class="btn btn_action"
                                                    t-on-click="(ev) => this.openTaxJournalItems(ev, 'Taxes Applied', 'tax')"
                                                >
                                                    Audit
                                                </a>
                                            </t>
                                        </th>
                                    </tr>
                                    <tr>
                                        <t t-if="hasTaxesSeveralCountries">
                                            <th>Country</th>
                                        </t>
                                        <th>Name</th>
                                        <th class="text-end">Base Amount</th>
                                        <th class="text-end">Tax Amount</th>
                                        <th t-if="props.line.tax_non_deductible_column" class="text-end">Non-Deductible</th>
                                        <th t-if="props.line.tax_deductible_column" class="text-end">Deductible</th>
                                        <th t-if="props.line.tax_due_column" class="text-end">Due</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <t t-foreach="taxesByCountry" t-as="country" t-key="country_index">
                                        <t t-foreach="props.line.tax_report_lines[country]" t-as="tax" t-key="tax_index">
                                            <tr>
                                                <t t-if="hasTaxesSeveralCountries">
                                                    <td t-out="(tax_index == 0) ? country : ''"/>
                                                </t>

                                                <td>
                                                    <a class="link"
                                                        t-on-click="(ev) => controller.reportAction(ev,
                                                            'journal_report_action_dropdown_audit_default_tax_report', {
                                                                line_id: tax.line_id,
                                                                date_form: props.line.date_from,
                                                                date_to: props.line.date_to,
                                                        })"
                                                    >
                                                        <t t-out="tax.name"/>
                                                    </a>
                                                </td>

                                                <td class="text-end" t-out="tax.base_amount"/>
                                                <td class="text-end" t-out="tax.tax_amount"/>
                                                <td t-if="props.line.tax_non_deductible_column" class="text-end" t-out="tax.tax_non_deductible"/>
                                                <td t-if="props.line.tax_deductible_column" class="text-end" t-out="tax.tax_deductible"/>
                                                <td t-if="props.line.tax_due_column" class="text-end" t-out="tax.tax_due"/>
                                            </tr>
                                        </t>
                                    </t>
                                </tbody>
                            </table>
                        </t>
                    </td>

                    <td t-att-colspan="5">
                        <t t-set="taxGrids" t-value="props.line.tax_grid_summary_lines"/>

                        <t t-if="'tax_grid_summary_lines' in props.line and Object.keys(taxGrids).length">
                            <t t-set="hasTaxesGridsSeveralCountries" t-value="Object.keys(taxGrids).length > 1"/>
                            <table class="table">
                                <thead>
                                    <tr>
                                        <th t-att-colspan="(hasTaxesGridsSeveralCountries) ? 5 : 4">
                                            Impacted Tax Grids
                                            <t t-if="controller.groups.account_user">
                                                <a
                                                    class="btn btn_action"
                                                    t-on-click="(ev) => this.openTaxJournalItems(ev, 'Impacted Tax Grids', 'tag')"
                                                >
                                                    Audit
                                                </a>
                                            </t>
                                        </th>
                                    </tr>
                                    <tr>
                                        <t t-if="hasTaxesGridsSeveralCountries">
                                            <th>Country</th>
                                        </t>
                                        <th>Grid</th>
                                        <th class="text-end">+</th>
                                        <th class="text-end">-</th>
                                        <th class="text-end">Impact On Grid</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <t t-foreach="taxGrids" t-as="country" t-key="country_index">
                                        <t t-foreach="taxGrids[country]" t-as="grid" t-key="grid_index">
                                            <tr>
                                                <t t-if="hasSeveralGrids">
                                                    <td t-out="(grid_index == 0) ? country : ''"/>
                                                </t>

                                                <td>
                                                    <a class="link"
                                                        t-on-click="(ev) => controller.reportAction(ev,
                                                            'journal_report_tax_tag_template_open_aml', {
                                                                tag_id: taxGrids[country][grid]['tag_ids'],
                                                                date_form: props.line.date_from,
                                                                date_to: props.line.date_to,
                                                        })"
                                                    >
                                                        <t t-out="grid"/>
                                                    </a>
                                                </td>

                                                <td class="text-end" t-out="taxGrids[country][grid]['+'] || 0"/>
                                                <td class="text-end" t-out="taxGrids[country][grid]['-'] || 0"/>
                                                <td class="text-end" t-out="taxGrids[country][grid]['impact']"/>
                                            </tr>
                                        </t>
                                    </t>
                                </tbody>
                            </table>
                        </t>
                    </td>
                </tr>
            </t>
        </xpath>
    </t>
</templates>
