Inheriting and modifying QWeb reports

< / / / / / >
Beginner
QWeb
V12.0
V 12.0
+/- 8 minutes
Written by Yenthe Van Ginneken
(0)

Quick scroll

1. Introduction

Almost every report in Odoo is a QWeb report. Odoo comes with quite a lot of reports that are available out of the box but what if they're not really fulfilling to your needs? Odoo allows you to inherit existing reports and to modify anything on the report. In this tutorial you will learn how to inherit an existing report and how to change it. We'll change sale order report and we'll add the customer his phone number and e-mail on the report and we'll add a default warning on the bottom of the report.

2. Adding the dependency

Before you can start inheriting QWeb reports you will need to create a new module and configure the manifest.py correctly. Open up your manifest.py and add the dependency for the module where you want to change the report from. In our example this will be the 'sale' module:

                    'depends': ['sale'],                                    

Without this dependency Odoo would not be able to find the existing report in order to inherit it.

3. Creating your XML file

The next step is to create a new XML file and to create a new XML record to inherit the existing report. Create a new file named 'sale_order_report.xml' under a new folder named 'report' in your custom module. Don't forget to add this new file in your manifest.py file (under the data key).

3.1 Finding the XML ID to inherit

To inherit a report you have to know in which module it is and what the original XML id of the report is. So, how do you find this?The easiest way is to go to Settings > Technical > Reports > Reports and to search the report you want to modify. In this tutorial this will be the quotation/order report so search for 'order' here:

Reports overview form

Now open up the report that we've found (the second one). This will open up a new formview where you will find all the technical information you need. For example with our order report:

Report details

At the right top you will see a smartbutton named 'QWeb views'. Click on it. This will show you a list of all records that are associated to this specific report. In the example of the quotation report:

Report view records

So this usually shows you more than one XML record. How do you know which one you need? The one that ends with _document is the correct XML record that you need to inherit. Under the column 'External ID' you will see there is an unique name, in this example sale.report_saleorder_document. The first part of this text (sale) is the module where the report is from, the second part (report_saleorder_document) is the name of the report that you want to inherit and modify.

Remember this value (sale.report_saleorder_document) and open your newly created XML file named sale_order_report.xml

3.2 inheriting the existing report

To inherit a QWeb report you'll need two things: an unique template id and the inherit_id. The template id can be chosen by yourself, just make sure its unique. The inherit_id should contain the value you've just found on your report (sale.report_saleorder_document):

                                                                                

This is literally all the code you need to inherit an existing QWeb report! Now we can add our custom code into the existing report with some xpath expressions.

3.3 Adding our custom code in the existing report

Let us now add the phone number and the e-mailaddress of the customer at the top of the sale order report where the quotation date etcetera are shown. These details are wrapped within a div element that has the class 'mb32' set so let us create an xpath expression on this class and let us add our custom code inside of this element:

                                                                                

Does this make sense to you? This will tell Odoo that we want to add two divs within the div that has the class 'mb32'. If there is a phone filled in on the customer form it'll be printed on the report. If there is no phone filled in the div will not be used on the report. The same happens for the email. The class 'col-auto mw-100 mb-2' will add the two divs behind the existing ones and will make sure they take up two columns of width on the report.

Finally, let us add a warning at the bottom of the report that if there is something wrong with the customer his quotation that they can reach out to the salesperson:

                                                                                

That's if! If you now save your files and install your custom module you'll see the default sale report has been changed:

Result inherited and modified report

4. Conclusion

Inheriting QWeb reports in Odoo is quite easy to do. The hardest part is to find the external ID of the existing report but after finding this you can inherit an existing report in just a few lines of code. QWeb reports are very powerful in combination with xpath expressions. If you understand and know both principles you can inherit and modify any report to your exact wishes. It is really worth it to master these two skills in Odoo as you'll need to do this quite often.