Spellbound - Odoo Implementation, Customization & Training

Qweb Report in odoo8 blog

By: SpellBound Posted on: May 14, 2016 Tags:

QWeb is the primary templating engine used by Odoo. It is an XML templating engine and used mostly to generate HTML fragments and pages.
It’s implemented fully in javascript and rendered in the browser.
Each template file (XML files) contains multiple templates, where template engine usually have a 1:1 mapping between template files and templates.

The rationale behind using QWeb instead of a more popular template syntax is that its extension mechanism is very similar to the openerp view inheritance mechanism. Like openerp views a QWeb template is an xml tree and therefore xpath or dom manipulations are easy to performs on it.

Creating a new XML file

The first step to create your own new report is to make a new XML file. This file should be placed under yourModuleName/views and you can name it as you wish. In this file you will define the template id’s and names, which will later on be used by your Odoo to find the correct report.
The minimal code looks like this:

Image-1

The template’s id must be the name specified in the report declaration; for example account.report_invoice for the above report. Since this is a QWeb template, you can access all the fields of the docs objects received by the template. Calling external_layout will add the default header and footer on your report. The PDF body will be the content inside the <div class=”page”>.
There are some specific variables accessible in reports, mainly:

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 time from the Python standard library
translate_doc
a function to translate a part of a report.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
You can then call translate_doc from your main template to obtain the translated
document.
It must be used as follow:
The name of the template id (from the first record) should be the same as the name for the t-raw that handles translations. In the t-raw you need to add the module name before this name with a dot. The main template calls translate_doc with partner_id.lang as a parameter, which means it uses a custom report model to access a res.partner record.
user
res.user record for the user printing the report
res_company
record for the current user‘s company

 

2.Add the report to the XML file responsible for reports

Every module has a file that makes a reference to every single report in the module. This is done by a item, which can be found in the XML file.
This file always has the same name structure. It starts with the name of your module, an underscore and then report.xml. Its found in the first level of the directory of your module.
The filename is always yourModuleName_report.xml. Now open up this file and you will see a tag for every report that exists in this module.

The next step for us is to add a new tag for our own report. The code looks like this:

Image-2

 

3.Declare a paper format

Image-3

Paper formats are records of report.paperformat and can contain the following attributes:
name (mandatory)
only useful as a mnemonic/description of the report when looking for one in a list of some sort
description
a small description of your format
format
either a predefined format (A0 to A9, B0 to B10, Legal, Letter, Tabloid,…) or custom; A4 by default. You cannot use a non-custom format if you define the page dimensions.
dpi
output DPI; 90 by default
margin_top, margin_bottom, margin_left, margin_right
margin sizes in mm
page_height, page_width
page dimensions in mm
orientation
Landscape or Portrait
header_line
boolean to display a header line

header_spacing

header spacing in mm

You have now everything to create reports

  • A template (ir.ui.view record)
  • A report record (ir.actions.report.xml record)
  • And maybe a specific paper format (report.paperformat record)

Parser Class:
To create parser class you need to import report_sxw.rml_parse.
The methods you want to access in report must be updated in localcontext in parser class.
Create another class which which inherits features of osv.AbstractModel and it creates bond between parser class and template engine.
This new class have few parameters and each of the parameters have fixed pattern so must follow it.
_name = ‘report.<module_name>.<report_name>’
_inherit = ‘report.abstract_report’
_template = ‘<module_name>.<report_name>’
_wrapped_report_class = <parser_class_name>

Leave a Reply

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

Overall client rating is 5.0 out of 5.0 for Spellbound Soft Solution. by 20+ clients on over 50+ projects.