How to print Report from Wizard in Odoo

December 4, 2023 by
How to print Report from Wizard in Odoo
Mindphin Technologies Pvt. Ltd.

To print a report from a wizard in Odoo, you can follow these general steps:


1. Create the wizard: Define a new model for your wizard by inheriting from the TransientModel class. This model will be used to collect input from the user before generating the report. Make sure to add necessary fields to capture the required data.

# -*- coding: utf-8 -*-
# Part of Mindphin. See LICENSE file for full copyright and licensing details.
from odoo import models, fields
from datetime import date
class FindCourseWizard(models.TransientModel):  # Class Structure
    _name = “find.course.wizard”
    _description = “find course wizard”
    name = fields.Char(string=”Course Name”, required=False)
    start_date = fields.Date(string=”Start Date”)
    end_date = fields.Date(string=”End Date”)
    responsible_id = fields.Many2one(“res.partner”, string=”Responsible”)

2. Create the report: Define a new report model that inherits from AbstractModel. This model will be responsible for generating the report based on the input provided by the user through the wizard.

<?xml version=”1.0″ encoding=”utf-8″?>
<odoo>
    <record id=”courses_report” model=”ir.actions.report”>
        <field name=’name’>openacademy.course</field>
        <field name=’model’>openacademy.course</field>
        <field name=’report_type’>qweb-pdf</field>
        <field name=’report_name’>openacademy.courses_report_card_id</field>
        <field name=’report_file’>openacademy.courses_report_card_id</field>
        <field name=’binding_model_id’ ref=’model_openacademy_course’></field>
        <field name=”binding_model_id” eval=”False”/>
    </record>
</odoo>

  3. Define the report template: Create an XML file that defines the report template. This file typically contains the layout and structure of the report, including placeholders for dynamic data.

<?xml version=”1.0″ encoding=”utf-8″?>
<odoo>
    <data>
        <template id=”courses_report_card_id”>
            <t t-call=”web.html_container”>
                <t t-call=”web.external_layout”>
                    <t class=”page”>
                        <div class=”oe-structure”>
                            <div class=”row”>
                                <div class=”col-xs-8″>
                                    <html>
                                        <body>
                                            <table class=”table”>
                                                <tr>
                                                    <th>Course Name</th>
                                                    <th>Start Date</th>
                                                    <th>End Date</th>
                                                    <th>Total Attendees</th>
                                                </tr>
                                                <t t-foreach=”docs” t-as=”o”>
                                                    <tr class=”text-center”>
                                                        <td>
                                                            <span t-field=”o.name” />
                                                        </td>
                                                        <td>
                                                            <span t-field=”o.start_date” />
                                                        </td>
                                                        <td>
                                                            <span t-field=”o.end_date” />
                                                        </td>
                                                        <td>
                                                            <span t-field=”o.total_attendees” />
                                                        </td>
                                                    </tr>
                                                </t>
                                            </table>
                                        </body>
                                    </html>
                                </div>
                            </div>
                        </div>
                    </t>
                </t>
            </t>
        </template>
    </data>
</odoo>

4. Define the wizard actions: In the wizard model, define an action method that will be triggered when the user clicks the print button. This method should gather the input from the wizard fields, pass it to the report model, and return an action that will trigger the report generation and display it to the user.

def report(self):
        domain = [
            # (‘name’, ‘=’, self.name),
            (‘start_date’, ‘>=’, self.start_date),
            (‘start_date’, ‘<=’, self.end_date),
        ]
        name = self.env[‘openacademy.course’].search(domain)
        return self.env.ref(‘openacademy.courses_report’).report_action(name)

5. Register the report and wizard: Finally, you need to register the report and wizard models in the Odoo system. This can be done by adding the respective model references in the __init__.py file of your module.

Illustration of printing report from wizard:

Click on report button to print the report.

Open the download file to view report.

Here’s the report.

TAGS: ODOOODOO DEVELOPMENTPRINT REPORT FROM WIZARD IN ODOO  

 

Share this post
Our blogs
Archive