This question has been flagged
5 Replies
22811 Views

I'm trying to customize a report_invoice_document qweb report. Here is the source code:

<?xml version="1.0"?>
<t t-name="account.report_invoice_document">
<t t-call="report.external_layout">
<t t-set="o" t-value="o.with_context({'lang':o.partner_id.lang})"/>
<div class="page">
    <div class="row">
        <div name="invoice_address" class="col-xs-5 col-xs-offset-7">
            <address t-field="o.partner_id" t-options="{&quot;widget&quot;: &quot;contact&quot;, &quot;fields&quot;: [&quot;address&quot;, &quot;name&quot;], &quot;no_marker&quot;: True}"/>
            <span t-if="o.partner_id.vat">TIN: <span t-field="o.partner_id.vat"/></span>
        </div>
    </div>
<h2>
    <span t-if="o.type == 'out_invoice' and (o.state == 'open' or o.state == 'paid')">Invoice</span>
    <span t-if="o.type == 'out_invoice' and o.state == 'proforma2'">PRO-FORMA</span>
    <span t-if="o.type == 'out_invoice' and o.state == 'draft'">Draft Invoice</span>
    <span t-if="o.type == 'out_invoice' and o.state == 'cancel'">Cancelled Invoice</span>
    <span t-if="o.type == 'out_refund'">Refund</span>
    <span t-if="o.type == 'in_refund'">Vendor Refund</span>
    <span t-if="o.type == 'in_invoice'">Vendor Bill</span>
    <span t-field="o.number"/>
</h2>
...

So I'm trying to replace "div[@class='page']" and build my own report with the following code and expect to see just the "Invoice_test_2" in my report when I print it out:

<odoo>	
    <data>
        <template id="practichem_report_invoice_layout" inherit_id="account.report_invoice_document">
            <xpath expr="//div[@class='page']" position="replace">
                <div class="page">
                    <h2>Invoice_test_2</h2>
                </div>
            </xpath>
        </template>
    </data>
</odoo>

But I'm getting the following error:

ValueError: Can't validate view:
Element '<xpath expr="//div[@name='invoice_address']">' cannot be located in parent view

What am I doing wrong?


Avatar
Discard
Author

However, I can replace the <h2> tag with the following code and it works:

<template id="practichem_report_invoice_layout" inherit_id="account.report_invoice_document">

<xpath expr="//div[@class='page']//h2" position="replace">

<h2>Customer Invoice Report</h2>

</xpath>

</template>

Best Answer

The reason for the error is that other templates ("report_invoice_document_inherit_sale", "account_invoice_report_duplicate", "report_invoice_document_inherit_sale" and "report_invoice_layouted") are already inheriting the account.report_invoice_document template.
When you inherit it yourself and remove the code that the inheriting templates are looking for, Odoo throws an error.. because it can't find the code.

The solution I found was to first make a new template inheriting "account.report_invoice" (this is the template calling the "account.report_invoice_document" on every document), and make this new template call my custom report_invoice_document template/report.

Something like this:

Inherit account.report_invoice to make it call my custom template/report:

<template id="report_invoice_inherit_mymodule" inherit_id="account.report_invoice">
<xpath expr="//t[@t-call='account.report_invoice_document']" position="replace">
            <t t-call="mymodule.report_invoice_document" t-lang="o.partner_id.lang"/>
</xpath>
</template>

Create new template for report_invoice_document

<template id="report_invoice_document">
<t t-call="report.external_layout">
<t t-set="o" t-value="o.with_context({'lang':o.partner_id.lang})" />
<div class="page">
My Custom Report Here...
</div>
</t>
</template>
Avatar
Discard

+1 for including a code-example of the suggested solution ;)

Thanks for saving my day!! :)

Don't work on odoo 12, i have this error message:

Error to render compiling AST

AttributeError: 'dict' object has no attribute 'type'

Template: 1151

Path: /templates/t/t/t/t[1]

Node: <t t-set="lang" t-value="o.user_id.sudo().lang if o.type in ('in_invoice', 'in_refund')

Author Best Answer

So I added attribute primary=”True” inside the <template> tag and error was gone. But now no matter what I do with the report customization it doesn't affect anything and it prints out the original report.

So I still have a problem with the report inheritance.

Avatar
Discard

primary="True" Means your report does not inherit from any other report. So Xpath expressions are of no use here since you just told Odoo this report is a new one.

This could be good, but then you will need to update the action (clicking the print invoice button for example) to call your new report and not the original invoice-layout.

Best Answer

Hello Art

The problem you are facing here is because you are replacing the entire page and another installed module inherits a part of this page you are replacing.

I checked the github odoo10 code and searched for <xpath expr="//div[@name='invoice_address']">
This made me notice the file addons/sale/report/invoice_report_templates.xml

In this file they inherit the report you are trying to inherit. When you update your module the invoice report gets reloaded and all it's 'children' as well. Since you remove everything from the report, Odoo is unable to validate the view in the sale addon above, since the xpath is looking for something inside the page and that is gone now, since the xml only consists of the page and the h2.

SO, you can test if I am right or not by adding a div with name invoice_address to your page, then install or update your module, this should solve your error, but you have an unwanted div in the report now.

What I would suggest is that you create a whole new report and an action, or find out how to make the existing action link to another xml file (your new xml file).

The problem with creating a whole new action would be that when you send the invoice by mail, the default layout would still be called and not your new one, so if you decide to go this road, bear in mind that there are references that should also be updated.


I hope I helped you or at least gave you a push in the right direction.

Sincerely

Sonny V.
ERP-consultant @ Cats and Dogs, BE


Avatar
Discard

Wow.. we replied at exactly the same time :-D

Check my answer for a solution to the "new action" problem.