ERP software contains many modules for performing various tasks to drive the flow of business operations smoothly. However, all organizations want to know their progress, and they should demonstrate the growth to the boards or authorities in the form of reports for easy handling. Odoo is one of the best business management software that is widely used by millions of users worldwide. In addition, Odoo provides the speculation of various reports such as PDF, Graphical presentation, Pivot table, XLSX report, etc.
Qweb is used as a templating engine that converts XML to HTML. It is used for defining the report templates that are in HTML format. The reports are worked based on Report Actions and Report Templates. The report actions generate the reports which are either based on a python function or a print button. We can set up a customized report using the report action in odoo v15.
This blog will define you how to create a PDF report in Odoo 15,
Report Actions (ir.actions.report):
The following code is an example of Report actions in Odoo 15. Create this XML file in the reports directory of the module and add this file to a manifest.
<record id="action_report_vehicle_order" model="ir.actions.report">
<field name="name">Vehicle Order</field>
<field name="model">vehicle.report</field>
<field name="report_type">qweb-pdf</field>
<field name="report_name">vehicle.report_vehicle_order</field>
<field name="report_file">vehicle.report_vehicle_order</field>
<field name="print_report_name">’Vehicle Report - %s' % (object.name))</field>
<field name="binding_model_id" ref="model_vehcicle_report"/>
<field name="binding_type">report</field>
</record>
The following describes the definition of each field in Report actions.
name: It is a mandatory field used to name the report.
model: model is a mandatory field, It contains the corresponding model where the data taken from.
report_type: It is used to select two types of reports qweb-pdf and qweb-html.
report_name: It is a mandatory field that represents the external_id of the report template.
print_report_name: It is not a mandatory field that is used to name the generated report file using the python expressions as per the need.
binding_model_id: If you need to print the report from view, you need to define the binding_model_id, for example, the model is sale.order then the binding_model_id is model_sale_order.
How to Create a Report Template?
Templates are created in HTML format. The layout will be written inside the <template> tags. You can use more templates in a single template using t-call properties. The corresponding is an example of a sample report template.
<template id="report_vehicle_order">
<t t-call="web.html_container">
<t t-foreach="docs" t-as="o">
<t t-call="web.external_layout">
<div class="page">
<h2>Sample Report</h2>
<p>span t-field="o.name"/></p>
</div>
</t>
</t>
</t>
</template>
In the example, the report_vehicle_order is the external ID that needs to be defined in the report action. Further, the t-call=web.external_layout is used to add the default web layout of Odoo in the report. Also used t-call=web.internal_layout for minimized header and a footer view. We can customize the report corresponding to our priority.
How to Print PDF Report from Button or wizard?
In certain conditions, we need to print the report from a button or wizard. In that condition, we use the print button inside the wizard or form to call the report action that is mentioned in the previous example. The following code is an example of the print function.
def print_sample_report(self):
data = {
'model_id': self.id,
'to_date': self.to_date,
'from_date': self.from_date,
'vehicle_id': self.vehicle_id.id,
'vehicle_name': self.vehicle_id.vehicle_name
}
# docids = self.env['purchase.order'].search([]).ids
return self.env.ref('module_name.action_report_vehicle_order).report_action(None, data=data)
The print_sample_report is the button function and the data contains the values that pass to the template. The data part is not mandatory, if no values are passed we can neglect this. If we are passing docids we can use that according to the model. After that, we need to create an “Abstract model” and define _get_report_values as shown in the below code snippet.
from odoo import models,api
class VehicleReport(models.AbstractModel):
_name = 'report.vehicle.report_vehicle_order’
@api.model
def _get_report_values(self, docids, data=None):
docs = self.env[model.model].browse(docids)
return {
'doc_ids': docids,
'doc_model': model.model,
'docs': docs,
'data': data,
}
The format of the model name is “report.report_name”. Then using the return statement you can pass the value to the report template. The “docs” contains the records and you can use them in the report template by looping in to “o” as seen in the previous template example.