Odoo Latest Blogs | Qweb Operations in Odoo
What is qweb?
Qweb is a reporting engine or template engine used by odoo. By using this (qweb) we can create reports in odoo. Also It uses XML to create design of reports as per your need. We can also say that qweb is XML based reporting engine for OpenERP/Odoo.By using Qweb, we can manipulate the data very easily.
Create Qweb Reports In Odoo 12
Step 1 : Define the Report through <report> tag with following configuration.
Report tag is in the file whose name should be <module_name>_report.xml which we have to add in ‘data’ list of __manifest__.py.
sample_report.xml
<report
id=”sample_report” model=”sale.order” string=”Quotation Print” report_type=”qweb-pdf” file=”module_name.report_quotation“ name=”module_name .report_quotation“ /> |
Id
Unique identifier of report which is declared in ir.model.data.
String
It is name of the report which is displayed in view of the list of print button.
Model(mandatory)
Model for which we want to create report.
Report_type(mandatory)
Either qweb-pdf for PDF reports or qweb-html for HTML.”HTML format” means that the content is displayed as a web page, which is good for browsing an article on screen.A PDF(portable document format) is better for printing because it will preserve the layout that was presented in the print journal, including illustrations. You can also save a PDF to your computer’s hard drive.
print_report_name
The name of our report (which will be the name of the PDF generated)
File
It is our qweb-template view file. The name of this file should be <module_name>.report_<type_of_report>.
Groups
groups allowed to view/use the current report
Attachment_use
if set to True, the report will be stored as an attachment of the record using the name generated by the “attachment“ expression; you can use this if you need your report to be generated only once (for legal reasons, for example)
attachment
python expression that defines the name of the report; the record is accessible as the variable “object“
Paper format
an external id of the paper format you wish to use
Step 2 : Define the report template.
For that we have to add template file with the name which is declared in ‘file’ in addition attribute of <report> tag. Therefore here is the name that should be in views/sample_report (No need to add module name here.). We have to add this file in ‘data’ list of __manifest__.py file.
<template id=”report_quotation”>
<t t-call=”web.html_container”> <t t-foreach=”docs” t-as=”o”> <t t-call=”web.external_layout”> <div> <h2>Report title</h2> <p>This object’s name is <span t-field=”o.name”/></p> </div> </t> </t> </t> </template> |
When we call the external_layout it will take the default header and footer. The contents inside the PDF will reside in the <div>.The template id
must be the same that we defined in the report declared.
docs
Records for the current report
doc_ids
List of ids for the “docs“ records
doc_model
Model for the docs records
time
A reference to :mod:`python:time` from the Python standard library
user
res.user record for the user printing the report
res_company
Record for the current user’s company
Translatable Templates
If you wish to translate reports (to the language of a partner, for example), you need to define two templates:
- The main report template
- The translatable document
by using the “t-lang” attribute you can call the translatable document from your main template.
1.Main template
<template id=”report_saleorder”> <t t-call=”web.html_container”> <t t-foreach=”docs” t-as=”o”> <t t-call=”sale.report_saleorder_document“ t-lang=”o.partner_id.lang”/> </t> </t> </template> |
- Translatable template
<template id=”report_saleorder_document“>
<t t-call=”web.external_layout”> <t t-set=”o” t-value=”o.with_context({‘lang’:o.partner_id.lang})”/> <div> <div> <div> <table border=”0″><tr> <td colspan=”5″> <center><h3>Quotation</h3></center> </td> </tr> </table> </div></div></div></t> </tempalte> |
Inheriting and modifying QWeb reports
Since almost every report in Odoo is a QWeb report. Odoo allows you to inherit existing reports and also to modify anything on the report.
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 also add the dependency for the module where you want to change the report from. However 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.
Creating XML file
The next step is to create a new XML file and also to create a new XML record to inherit the existing report. Create a new file with .xml and besides add this new file in your manifest.py file (under the data key).
Finding the XML ID to inherit
Surely 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.
At the right top you will see a smartbutton named ‘QWeb views’. Click on it. And also this will show you a list of all records that are associated to this specific report.
So this usually shows you more than one XML record. How do you know which one you need? Because 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, ofcourse although 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 also modify.
Remember this value (sale.report_saleorder_document) and also open your newly created XML file
Inheriting the existing report
To inherit a QWeb report you’ll need two things: an unique template id and also the inherit_id. The template id can be chosen by yourself, just make sure its unique. In addition the inherit_id should contain the value you’ve just found on your report (sale.report_saleorder_document):
Adding our custom code in the existing report
Let us now add the phone number and also the e-mail address 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:
<xpath expr=”//div[hasclass(‘mb32’)]” position=”inside”>
<div t-if=”doc.partner_id.phone”> <strong>Your phone number:</strong> <p t-field=”doc.partner_id.phone”></p> </div> <div t-if=”doc.partner_id.email”> <strong>Your email:</strong> <p t-field=”doc.partner_id.email”></p></div> </xpath> |
<xpath expr=”//div[hasclass(‘clearfix’)]” position=”after”>
<div> If there is something wrong then please reach out to your salesperson as soon as possible. </div> </xpath |
will tell Odoo that we want to add two divs within the div that has the class ‘mb32’. Because 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. In addition the same happens for the email. The class ‘col-auto mw-100 mb-2’ will add the two divs behind the existing ones and also 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:
If you now save your files and also install your custom module again you’ll see the default sale report has been changed