QWeb Report in Odoo 8

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.

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

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:

3.Declare a paper format

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

  1. A template (ir.ui.view record)
  2. A report record (ir.actions.report.xml record)
  3. 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>

Comments

Sylvester: Could you please elaborate more on the parser class? I need to use fields from different models to generate my report. Will appreciate an example like the one above. Thank you".

Leave a Reply

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

  1. Carmelo Carrillo says:

    Hi,
    If I modify the account_report.xml for add a new invoice, how I do to view this new record on odoo? Have I restart the odoo server?
    Thank you.

    1. Anonymous says:

      Yes. You have to restart and upgrade the module.

    2. Anonymous says:

      Yes. You have to restart and upgrade the module.

  2. Nehemias says:

    Very incomplete guide, not helpful. 🙁

  3. Samba says:

    Thanks Anju,
    This Post help me allot. I need some more explanation on the pdf report development. I have an issue
    https://www.odoo.com/forum/help-1/question/how-can-i-change-report-file-name-in-odoo-8-87059
    . I want to change the pdf report file name to sale.report_sale to “quotation XXXX”, here XXXX would be the quotation number. Is it posiible?

    1. Anonymous says:

      def print_report(self,cr, uid, ids, context=None):
      if context is None:
      context = {}
      data = self.read(cr, uid, ids)[0]
      self_browse = self.browse(cr, uid, ids)
      datas = {
      ‘ids’: [data.get(‘id’)],
      ‘model’: ‘sale.order’,
      ‘form’: data
      }
      return {
      ‘type’: ‘ir.actions.report.xml’,
      ‘report_name’: ‘sale.order.report’,
      ‘datas’: datas,
      ‘name’: ‘Quotation ‘ + self_browse[0].name
      }

  4. Sylvester says:

    Could you please elaborate more on the parser class? I need to use fields from different models to generate my report. Will appreciate an example like the one above. Thank you

© 2020 Zesty Beanz Pvt Ltd All Rights Reserved.