12 Mar

What is qweb?

 

Qweb is a reporting engine or template engine used by odoo. By using this (qweb) we can create reports in odoo. 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.

python-faq-670x335

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

Odoo-Qweb- P1

Step 2 : Define the report template.

 

For that we have to add template file with the name which is declared in ‘file’ attribute of <report> tag.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 class="page">

<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 class="page">.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>

  1. 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 class="page">

<div class="row" style="font-size:14px">

<div class="col-12">

<table border="0" style="width:100%;"><tr>

<td colspan="5" style="border: 1px solid black;">

<center><h3>Quotation</h3></center>

</td>

</tr>

</table>

</div></div></div></t>

</tempalte>

 

Inheriting and modifying QWeb reports

Almost every report in Odoo is a QWeb report. Odoo allows you to inherit existing reports and 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 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.

Creating 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 with .xml and add this new file in your manifest.py file (under the data key).

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.

Odoo-Qweb- P2
Odoo-Qweb- P3

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.

Odoo-Qweb- P4

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

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):

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:

<xpath expr="//div[hasclass('mb32')]" position="inside">

<div t-if="doc.partner_id.phone" class="col-auto mw-100 mb-2">

<strong>Your phone number:</strong>

<p class="m-0" t-field="doc.partner_id.phone"></p>

</div>

<div t-if="doc.partner_id.email" class="col-auto mw-100 mb-2">

<strong>Your email:</strong>

<p class="m-0" t-field="doc.partner_id.email"></p></div> </xpath>

<xpath expr="//div[hasclass('clearfix')]" position="after">

<div class="alert alert-warning">

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'. 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:

 

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

2 comments on “Qweb Operations in Odoo

  1. Hello, thank you for the tutorial. My case is slightly different. I extended the model of a certain module by creating another module which inherited and extended the parent module, now I want to add the value of the new field I added via the child module to the PDF report of the parent module. Could you give me some leads on how to achieve this?

    1. Hi,
      For Example first i create the module ‘sales_report’,in this module i inherit the default report
      __manifest__.py
      ‘depends’=[‘sale’]
      view.xml

      Description
      Price

      then i create 2nd module with name ‘sales_management_report’

      __manifest__.py

      ‘depends’=[‘sales_report’]

      view.xml

      if your new field in another module then you have to add that in the manifest depends

Leave a Reply

Your email address will not be published. Required fields are marked *