How to Create Qweb Reports In Odoo 12

How to Create Qweb Reports In Odoo 12

Reports play a vital role in business as it gives insight to crucial assessments and reflections of business operations carried out. Moreover, reports can be called as the backbone of the business, as it underpins the success and failure nodes of business. Furthermore, it will provide an insight to the real time aspects of the company operations. Odoo offers an inquisitive platform to frame reports giving comprehensive analysis for business developments.
This blog will describe to you how to create Qweb reports in Odoo 12.
To create a report in Odoo, the end user need a report template and report action. 

Report action
To create a report in Odoo, the end user needs a report template and report action. Down below described the both aspects and thiccal functions involved with it.
a) id
Described the generated record’s ‘external id’
b) name (mandatory)
Only useful as a mnemonic/description of the report when looking for one in a list.
c) model (mandatory)
Describes the model your report will be about.
d) report_type(mandatory)
The report type can either be “qweb-pdf” for PDF reports or “qweb-html” for HTML
e) print_report_name
Describes the name of your report (which will be the name of the PDF output).
f) Groups
Describes the groups allowed to view/use the current report.
g) 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 (legal reasons, for example)
h) attachment
Describes the python expression that defines the name of the report; the record is accessible as the variable “object”
i) Paper format
Describes an external id of the paper format you wish to use.
Here is an example of the same:
Example::
<report
   id="action_report_pro_forma_invoice"
   string="PRO-FORMA Invoice"
   model="sale.order"
   report_type="qweb-pdf"
   groups="sale.group_proforma_sales"
   file="sale.report_saleorder_pro_forma"
   name="sale.report_saleorder_pro_forma"
   print_report_name="'PRO-FORMA - %s' % (object.name)"
/>
As we have an understanding on the report action let’s now move on to the report template.
Report template
There are various report templates that are available of which each of them have been explained in detail down below.
Minimal viable template
There are various report templates that are available of which each of them have been explained in detail down below.
 <template id="report_invoice">
   <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.
Here are some specific variables:

a) docs
Describes the records for the current report.
b) doc_ids
Describes the list of ids for the “docs” records.
c) doc_model
Describes the model for the docs records.
d) time
A reference to :mod: ‘python:time’ from the Python standard library.
e) user
Describes the res.user record for the user printing the report.
f) res_company
Record for the current user’s company

Translatable Templates
Another type of template available is the transferable templates which provides you with a tool that will be helpful to you when dealing with international business. Moreover, there may be situations where we deal with foreign partners, in that case, we should take reports in their language, for that purpose we need to define mainly two templates.

a) The main report template
b) The translatable document

In addition, by using the “t-lang” attribute you can call the translatable document from your main template.
Furthermore, you can have an understanding of both these types by analyzing an example. Therefore, let’s look at the Sale Order report from the Sale module:

1)Main template 

Here is an example of the command used to generate sales order report template:  

   <template id="report_saleorder">
       <t t-call="web.html_container">
           <t t-foreach="docs" t-as="doc">
               <t t-call="sale.report_saleorder_document" t-lang="doc.partner_id.lang"/>
           </t>
       </t>
   </template>

2) Translatable template
Here is an example of the command used to generate the transferable document template:
 <template id="report_saleorder_document">
       <!-- Re-browse of the record with the partner lang -->
       <t t-set="doc" t-value="doc.with_context({'lang':doc.partner_id.lang})" />
       <t t-call="web.external_layout">
           <div class="page">
               <div class="oe_structure"/>
               <div class="row">
                   <div class="col-6">
                       <strong t-if="doc.partner_shipping_id == doc.partner_invoice_id">Invoice and shipping address:</strong>
                       <strong t-if="doc.partner_shipping_id != doc.partner_invoice_id">Invoice address:</strong> 
                   <...>
               <div class="oe_structure"/>
           </div>
       </t>
   </template>
Paper Format
As we have understood the different templates used in report generation let’s now move onto the aspects of generation of the paper format for the report. Moreover, we can define a paper format for our report according to our convenience. 
Here is an example command used for the paper for mat generation for the report:
<record id="paperformat_hrsummary" model="report.paperformat">
   <field name="name">Leaves Summary</field>
   <field name="default" eval="True"/>
   <field name="format">custom</field>
   <field name="page_height">297</field>
   <field name="page_width">210</field>
   <field name="orientation">Landscape</field>
   <field name="margin_top">30</field>
   <field name="margin_bottom">23</field>
   <field name="margin_left">5</field>
   <field name="margin_right">5</field>
   <field name="header_line" eval="False"/>
   <field name="header_spacing">20</field>
   <field name="dpi">90</field>
</record>
Now let’s move on to understating certain key aspects of the command used for the paper format generation.
a) Name (mandatory) 
Name of the report is mandatory and you can provide a custom name for each.
b) Description
Provide a small description of your format
c) Format
You can either provide a predefined format (A0 to A9, B0 to B10, Legal, Letter, Tabloid,…) or use a custom one such as; A4 by default. 
d) dpi
Output DPI which is set as 90 by default
e) margin_top, margin_bottom, margin_left, margin_right
You can describe the margin sizes in Millimeters.
f) page_height, page_width
Provide the page dimensions in Millimeters.
g) Orientation
Out of the two page layout orientation available Landscape or Portrait you can use one
h) Header_line
You can use a boolean expression to display a header line.
header_spacing
header spacing in Millimeters.
Now as we have an understanding on how to use various templates and provide a format for the reports generation let’s move on to understand how to generate custom reports in Odoo.

Custom Reports
In our day to day business activities, we need reports according to our business process. Therefore, there is a vital need to customize the reports. Moreover, in these aspects, we will require additional records, functions and many more data generation tools in order to generate the required and customized report. Furthermore, we need to use Abstract Model class and overwrite the function _get_report_values and pass objects in the dictionary for these aspects.
Down below is an example code on how to generate a customized report for your business operations.
Here is an example from payroll
class Report Hr Salary Employee By month(models.AbstractModel):
   _name = 'report.l10n_in_hr_payroll.report_hrsalarybymonth'
   _description = "Indian Salary by Month Report"
@api.model
def _get_report_values(self, docids, data=None):
   model = self.env.context.get('active_model')
   docs = self.env[model].browse(self.env.context.get('active_id'))
   get_periods, months, total_mnths = self.get_periods(data['form'])
   get_employee = self.get_employee(data['form'], months, total_mnths)
   get_months_tol = self.get_months_tol()
   get_total = self.get_total(get_months_tol)
   return {
       'doc_ids': docids,
       'doc_model': model,
       'data': data,
       'docs': docs,
       'get_periods': get_periods,
       'get_employee': get_employee,
       'get_months_tol': get_months_tol,
       'get_total': get_total,
       'month_len': len(total_mnths) + 1
   }
Above is the mode code used for report generation. Furthermore, you can declare the functions that we need to use for it.
In this, we can declare the functions that we need to use, You can refer our blog related to Qweb where the blog explains about the Advanced Qweb Operations in Odoo 13

Similar Posts

Leave a Reply

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