Home > Enterprise >  Why can't I use main QWeb template variables in subtemplate in Odoo 15?
Why can't I use main QWeb template variables in subtemplate in Odoo 15?

Time:11-10

In Odoo 15, if you download an invoice PDF document (among others), you will see taxed quantities grouped by tax. I want to replace them and show only the total taxed amount.

So in that section, the invoice template (account.report_invoice) calls a subtemplate (account.document_tax_totals). So I've just replaced the content of the subtemplate, this way:

<template id="document_tax_totals" inherit_id="account.document_tax_totals">
    <xpath expr="//t[@t-call='account.tax_groups_totals']" position="replace">
        <tr>
            <td><strong>Taxes</strong></td>
            <td >
                <span t-esc="o.amount_tax" t-options='{"widget": "monetary", "display_currency": o.currency_id}'/>
            </td>
        </tr>
    </xpath>
</template>

This works exactly as I want... only in the main company. I don't realize why, but after this modification, if I print an invoice report in a multicompany environment, with other company selected different from the main one, I get this error:

KeyError: 'o'

I've made some tests and the variable o does not exist in the subtemplate. But if I print an invoice report from the main company, it exists.

Can anyone remember or explain me why?

CodePudding user response:

I find the solution searching for t-call in the code of the Odoo modules. I must pass the variables from the template to the subtemplate, this way:

<xpath expr="//t[@t-call='account.document_tax_totals']" position="before">
    <t t-set="amount_tax" t-value="o.amount_tax"/>
    <t t-set="currency_id" t-value="o.currency_id"/>
</xpath>

So the resultant invoice report code when calling the subtemplate is:

<t t-set="tax_totals" t-value="json.loads(o.tax_totals_json)"/>
<t t-set="amount_tax" t-value="o.amount_tax"/>
<t t-set="currency_id" t-value="o.currency_id"/>
<t t-call="account.document_tax_totals"/>

It would be also OK if we declare the parameters inside the <t t-call> tag:

<t t-call="account.document_tax_totals">
    <t t-set="tax_totals" t-value="json.loads(o.tax_totals_json)"/>
    <t t-set="amount_tax" t-value="o.amount_tax"/>
    <t t-set="currency_id" t-value="o.currency_id"/>
</t>

This way I can use them inside the subtemplate (note that now I removed the o. since I'm using the parameters names):

<template id="document_tax_totals" inherit_id="account.document_tax_totals">
    <xpath expr="//t[@t-call='account.tax_groups_totals']" position="replace">
        <tr>
            <td><strong>Taxes</strong></td>
            <td >
                <span t-esc="amount_tax" t-options='{"widget": "monetary", "display_currency": currency_id}'/>
            </td>
        </tr>
    </xpath>
</template>

What I still don't know is why the first code works in the main company. But it doesn't really matter after applying this solution.

  • Related