Report Actions In Odoo
17
Enterprise
Introduction
Enterprise
In Odoo 17, report actions allow us to define and manage various
reports within the system. These reports can be generated in
different formats such as PDF, Excel, and HTML, and can be triggered
from different parts of the application.
That is, Report actions are basically used for triggering the printing
of a report.
Enterprise
Let’s create a module named sale_report_custom which simply
creates a new sub menu in Sales module’s Reporting menu, opens a
wizard which asks for the customer and a sale amount. With the
entered details, a PDF report will be generated for the selected
customer’s all Sale orders which has the sale value above the amount
we enter. The module structure is as
Enterprise
First, to create a menu, type the xml code in the file sale_order_form.xml
under views directory.
<menuitem id="menu_show_wizard" sequence="5"
action="sale_report_wizard_action"
name="Sale Report PDF" parent="sale.menu_sale_report"/>
The menu can be seen as
Enterprise
For defining the action sale_report_wizard_action for the menu, create a
wizard using Transient model, define its views and make the wizard open
on this menu button click. All the python and xml codes for the wizard is
saved in the wizards directory and the python files must be imported
inside the __init__ under it. The python file for defining the class can be as
from odoo import fields, models
class SaleReportWizard(models.TransientModel):
_name = 'sale.report.wizard'
sale_amount = fields.Float("Sale Amount Above")
partner_id = fields.Many2one('res.partner', "Customer")
Enterprise
Now, create the basic form view for the wizard which displays those two
fields sale_amount and partner_id. Also give this form view’s id as the
view_id for the action that we have given for the previous menu added in
the Sale module. The xml code for the action can be as
<record id="sale_report_wizard_action" model="ir.actions.act_window">
<field name="name">Custom Wizard</field>
<field name="res_model">sale.report.wizard</field>
<field name="type">ir.actions.act_window</field>
<field name="view_mode">tree,form</field>
<field name="view_id" ref="sale_report_wizard_view_form"/>
<field name="context">{}</field>
<field name="target">new</field>
</record>
The id of the form view is sale_report_wizard_view_form here. This will
make the wizard to open.
Enterprise
On clicking the menu, the wizard will be shown as
We need to select the marked two fields and click on the Print PDF
button. For that button, we set the type as object, just name it as
print_report and define a python method with the same name which will
generate the PDF report.
Enterprise
In Odoo, ir.actions.report is used to define and manage reports. This
model allows us to create, configure, and customize reports that can be
rendered in various formats such as PDF, HTML, and XLSX. The
parameters for ir.actions.report include several fields that define the
behavior and appearance of the reports.
For a PDF report to function, the report action we created in xml code is
as
<record id="action_custom_report_saleorder" model="ir.actions.report">
<field name="name">Sale Details Custom Report</field>
<field name="model">sale.order</field>
<field name="report_type">qweb-pdf</field>
<field name="report_name">sale_report_custom.custom_sale_report</field>
<field name="report_file">sale_report_customcustom_sale_report</field>
<field name="print_report_name">'Custom Sale Report'</field>
</record>
Enterprise
Here are the key parameters:
● id give a unique id for the record
● name specifies the name of the report action. It is of char type.
● model is the Odoo model on which the report is based.
● report_type is the type of the report. Common types include "qweb-
pdf" for PDF reports, "qweb-html" for HTML reports and "qweb-text" for
plain text reports
● report_name is the technical name of the report template.
● print_report_name can be set which is a Python expression to
dynamically set the report's file name when printed. Or simply we can set
its value of char type in single quote.
● binding_model_id is the model to which the report is bound
● binding_type determines where the report action will appear.
● paperformat_id can also be set which specifies the paper format for the
report, such as A4, A5, etc
● attachment uses A Python expression for the report attachment file
name. If set, the report will be stored as an attachment.
Enterprise
Now, define the method print_report in the wizard’s model as
def print_report(self):
vals = []
sale_order = self.env['sale.order'].search([(
'partner_id', '=',self.partner_id.id),('amount_total','>=',self.sale_amount)])
for order in sale_order:
vals.append({'name': order.name,'date': order.date_order,
'amount_total': order.amount_total,'sales_person':order.user_id.name,
})
data = {
'ids': self.ids,
'model': self._name,
'vals': vals,
'sale_amount': self.sale_amount,
'partner_id': self.partner_id.name
}
return self.env.ref('sale_report_custom.
action_custom_report_saleorder').report_action(self, data=data)
Enterprise
Here what we are doing is:
● Set up a list vals as blank initially
● Searched all the orders with the condition matched with the values
given in wizard for the Customer and Amount
● Then appended all those order’s details to vals using append() with the
key details name, date, amount_total and sales_person.
● Added the data dictionary with vals as one key and other details such
as the model, sale_amount, etc
● At last, we took the reference of the xml report action using
self.env.ref() with the parameter
sale_report_custom.action_custom_report_saleorder where the first
part before dot(.) is the module name and the second part after it is the
xml id of the report action. This will check for the report_name
custom_sale_report specified in the action.
● Then using the report_action() method, we passed the data dictionary
Enterprise
Next, we need to create a Qweb Template which designs the PDF
report content. For that, start typing the template content using
<template id="custom_sale_report">
<t t-call="web.html_container">
<t t-call="web.external_layout">
A unique id is needed for the template. Then call the
web.html_container to handle and render HTML content within
QWeb templates securely and efficiently.
web.external_layout serves as a standard layout for reports.
This layout typically includes elements like the company logo, header,
footer, and general styling that should be consistent across all
documents.
Enterprise
Give the heading for the Report using any of the heading tags as
<center>
<h4><u>Customized Sale Report</u></h4>
</center>
Now, give the Customer Name and Sale Amount we gave in wizard to
the left as
<div style="text-align: left;">
<strong><p>Customer Name: <span t-esc="partner_id"/></p></strong>
</div>
<div style="text-align: left;">
<strong><p>Sale Amount Greater than
<span t-esc="sale_amount"/></p></strong></div>
</div>
Enterprise
And design a table with Headings and Body part as
<table class="table table-sm o_main_table table-striped mt-4">
<thead style="display: table-row-group">
<tr>
<th name="th_order_no" class="text-start">Order No</th>
<th name="th_brand" class="text-start">Order Date</th>
<th name="th_brand" class="text-start">Amount</th>
<th name="th_quantity" class="text-end">Sales person</th>
</tr>
</thead>
<tbody class="sale_tbody">
<t t-foreach="vals" t-as="order">
<tr t-att-class="'bg-200 fw-bold o_line_section'">
<td name="order_name"><span t-att-style="style" t-esc="order['name']"/></td>
<td name="order_date"><span t-att-style="style" t-esc="order['date']"/></td>
<td name="order_total"><span t-att-style="style" t-esc="order['amount_total']"/></td>
<td name="order_salesperson"><span t-att-style="style"
t-esc="order['sales_person']"/></td>
</tr>
</t></tbody>
</table>
Enterprise
Now, when we fill the wizard as below and click on Print PDF button,
Enterprise
The PDF Report will get downloaded/ printed as
For More Info.
Check our company website for related blogs
and Odoo book.
Check our YouTube channel for
functional and technical videos in Odoo.
Enterprise
www.cybrosys.com

Report Actions In Odoo 17 - Odoo 17 Slides

  • 1.
    Report Actions InOdoo 17 Enterprise
  • 2.
    Introduction Enterprise In Odoo 17,report actions allow us to define and manage various reports within the system. These reports can be generated in different formats such as PDF, Excel, and HTML, and can be triggered from different parts of the application. That is, Report actions are basically used for triggering the printing of a report.
  • 3.
    Enterprise Let’s create amodule named sale_report_custom which simply creates a new sub menu in Sales module’s Reporting menu, opens a wizard which asks for the customer and a sale amount. With the entered details, a PDF report will be generated for the selected customer’s all Sale orders which has the sale value above the amount we enter. The module structure is as
  • 4.
    Enterprise First, to createa menu, type the xml code in the file sale_order_form.xml under views directory. <menuitem id="menu_show_wizard" sequence="5" action="sale_report_wizard_action" name="Sale Report PDF" parent="sale.menu_sale_report"/> The menu can be seen as
  • 5.
    Enterprise For defining theaction sale_report_wizard_action for the menu, create a wizard using Transient model, define its views and make the wizard open on this menu button click. All the python and xml codes for the wizard is saved in the wizards directory and the python files must be imported inside the __init__ under it. The python file for defining the class can be as from odoo import fields, models class SaleReportWizard(models.TransientModel): _name = 'sale.report.wizard' sale_amount = fields.Float("Sale Amount Above") partner_id = fields.Many2one('res.partner', "Customer")
  • 6.
    Enterprise Now, create thebasic form view for the wizard which displays those two fields sale_amount and partner_id. Also give this form view’s id as the view_id for the action that we have given for the previous menu added in the Sale module. The xml code for the action can be as <record id="sale_report_wizard_action" model="ir.actions.act_window"> <field name="name">Custom Wizard</field> <field name="res_model">sale.report.wizard</field> <field name="type">ir.actions.act_window</field> <field name="view_mode">tree,form</field> <field name="view_id" ref="sale_report_wizard_view_form"/> <field name="context">{}</field> <field name="target">new</field> </record> The id of the form view is sale_report_wizard_view_form here. This will make the wizard to open.
  • 7.
    Enterprise On clicking themenu, the wizard will be shown as We need to select the marked two fields and click on the Print PDF button. For that button, we set the type as object, just name it as print_report and define a python method with the same name which will generate the PDF report.
  • 8.
    Enterprise In Odoo, ir.actions.reportis used to define and manage reports. This model allows us to create, configure, and customize reports that can be rendered in various formats such as PDF, HTML, and XLSX. The parameters for ir.actions.report include several fields that define the behavior and appearance of the reports. For a PDF report to function, the report action we created in xml code is as <record id="action_custom_report_saleorder" model="ir.actions.report"> <field name="name">Sale Details Custom Report</field> <field name="model">sale.order</field> <field name="report_type">qweb-pdf</field> <field name="report_name">sale_report_custom.custom_sale_report</field> <field name="report_file">sale_report_customcustom_sale_report</field> <field name="print_report_name">'Custom Sale Report'</field> </record>
  • 9.
    Enterprise Here are thekey parameters: ● id give a unique id for the record ● name specifies the name of the report action. It is of char type. ● model is the Odoo model on which the report is based. ● report_type is the type of the report. Common types include "qweb- pdf" for PDF reports, "qweb-html" for HTML reports and "qweb-text" for plain text reports ● report_name is the technical name of the report template. ● print_report_name can be set which is a Python expression to dynamically set the report's file name when printed. Or simply we can set its value of char type in single quote. ● binding_model_id is the model to which the report is bound ● binding_type determines where the report action will appear. ● paperformat_id can also be set which specifies the paper format for the report, such as A4, A5, etc ● attachment uses A Python expression for the report attachment file name. If set, the report will be stored as an attachment.
  • 10.
    Enterprise Now, define themethod print_report in the wizard’s model as def print_report(self): vals = [] sale_order = self.env['sale.order'].search([( 'partner_id', '=',self.partner_id.id),('amount_total','>=',self.sale_amount)]) for order in sale_order: vals.append({'name': order.name,'date': order.date_order, 'amount_total': order.amount_total,'sales_person':order.user_id.name, }) data = { 'ids': self.ids, 'model': self._name, 'vals': vals, 'sale_amount': self.sale_amount, 'partner_id': self.partner_id.name } return self.env.ref('sale_report_custom. action_custom_report_saleorder').report_action(self, data=data)
  • 11.
    Enterprise Here what weare doing is: ● Set up a list vals as blank initially ● Searched all the orders with the condition matched with the values given in wizard for the Customer and Amount ● Then appended all those order’s details to vals using append() with the key details name, date, amount_total and sales_person. ● Added the data dictionary with vals as one key and other details such as the model, sale_amount, etc ● At last, we took the reference of the xml report action using self.env.ref() with the parameter sale_report_custom.action_custom_report_saleorder where the first part before dot(.) is the module name and the second part after it is the xml id of the report action. This will check for the report_name custom_sale_report specified in the action. ● Then using the report_action() method, we passed the data dictionary
  • 12.
    Enterprise Next, we needto create a Qweb Template which designs the PDF report content. For that, start typing the template content using <template id="custom_sale_report"> <t t-call="web.html_container"> <t t-call="web.external_layout"> A unique id is needed for the template. Then call the web.html_container to handle and render HTML content within QWeb templates securely and efficiently. web.external_layout serves as a standard layout for reports. This layout typically includes elements like the company logo, header, footer, and general styling that should be consistent across all documents.
  • 13.
    Enterprise Give the headingfor the Report using any of the heading tags as <center> <h4><u>Customized Sale Report</u></h4> </center> Now, give the Customer Name and Sale Amount we gave in wizard to the left as <div style="text-align: left;"> <strong><p>Customer Name: <span t-esc="partner_id"/></p></strong> </div> <div style="text-align: left;"> <strong><p>Sale Amount Greater than <span t-esc="sale_amount"/></p></strong></div> </div>
  • 14.
    Enterprise And design atable with Headings and Body part as <table class="table table-sm o_main_table table-striped mt-4"> <thead style="display: table-row-group"> <tr> <th name="th_order_no" class="text-start">Order No</th> <th name="th_brand" class="text-start">Order Date</th> <th name="th_brand" class="text-start">Amount</th> <th name="th_quantity" class="text-end">Sales person</th> </tr> </thead> <tbody class="sale_tbody"> <t t-foreach="vals" t-as="order"> <tr t-att-class="'bg-200 fw-bold o_line_section'"> <td name="order_name"><span t-att-style="style" t-esc="order['name']"/></td> <td name="order_date"><span t-att-style="style" t-esc="order['date']"/></td> <td name="order_total"><span t-att-style="style" t-esc="order['amount_total']"/></td> <td name="order_salesperson"><span t-att-style="style" t-esc="order['sales_person']"/></td> </tr> </t></tbody> </table>
  • 15.
    Enterprise Now, when wefill the wizard as below and click on Print PDF button,
  • 16.
    Enterprise The PDF Reportwill get downloaded/ printed as
  • 17.
    For More Info. Checkour company website for related blogs and Odoo book. Check our YouTube channel for functional and technical videos in Odoo. Enterprise www.cybrosys.com