PDF Report from Wizard

Steps to follow when creating a report from wizard:
  • Create a wizard
  • Create a parser for the report template
  • Create a template for report
Create a wizard: In the previous page we've already discussed how to create a wizard in Odoo. We'll be using the print_report in that wizard to call our print function.
Rewrite the print_report method as follows:

code:
# -*- coding: utf-8 -*-
from odoo import api, fields, models


class CustomerFeedbackReportWizard(models.TransientModel):
    _name = 'customer.feedback.report.wizard'
    _description = 'Customer Feedback Report Wizard'

    customer_id = fields.Many2one('res.partner', string='Customer')

    @api.multi
    def print_report(self):
        context = dict(self._context)
        if context is None:
            context = {}
        data = self.read()[0] or {}
        datas = {
            'ids': self._ids,
            'data': data,
            'model': 'customer.feedback.report.wizard'
        }
        print('datas: ', datas)
        return self.env.ref('customer_feedback.action_customer_feedback_wizard_report').report_action(self, data=datas)


  • here data=self.read()[0] or {} will pass all values we selected in the wizard to report parser(which we'll create).
  • action_customer_feedback_wizard_report is the id of the report action in which we'll define the report template.
 Create a parser for report template: A parser is used assign variables to the report. Inside the directory report create a file named wizard_report.py. This file will be the parser for our report.

code:
# -*- coding: utf-8 -*-
from odoo import models, fields, api, _


class CustomerFeedbackWizardReport(models.AbstractModel):
    _name = 'report.customer_feedback.wizard_report_template'

    @api.model
    def _get_report_values(self, docids, data=None):
        docs = self.env['customer.feedback'].search([('customer_id', '=', data['data']['customer_id'][0])])
        return {
            'doc_model': 'customer.feedback',
            'docs': docs,
        }


import wizard.py in init file
  • here naming of class should be like report.module_name.template_name
Create a template for the wizard: Now lets create a template for printing the wizard. Create a file named wizard_report.xml inside report directory

Code:
<odoo>
    <data>
        <template id="wizard_report_template">
            <t t-call="web.html_container">
                <t t-foreach="docs" t-as="o">
                    <t t-call="web.external_layout">
                        <div class="page">
                            <h2>Customer Feedback</h2>
                            <p>Customer Name <span t-field="o.customer_id.name"/></p>

                            <table width="100%">
                                <tr>
                                    <th>
                                        Feedback Type
                                    </th>
                                    <th>
                                        Rating
                                    </th>
                                    <th>
                                        Description
                                    </th>
                                </tr>
                                <tr>
                                    <td>
                                        <span t-field="o.feedback_type.name"/>
                                    </td>
                                    <td>
                                        <span t-field="o.rating"/>
                                    </td>
                                    <td>
                                        <span t-field="o.customer_description"/>
                                    </td>
                                </tr>
                            </table>

                        </div>
                    </t>
                </t>
            </t>
        </template>
    </data>
</odoo>


Now lets define this template in report_action.xml file we already created.
Modified file will look like
Code:
<odoo>
  <data>

        <report
            id="action_customer_feedback_report"
            model="customer.feedback"
            string="Customer Feedback"
            report_type="qweb-pdf"
            name="customer_feedback.customer_feedback_report_template"
            file="customer_feedback.customer_feedback_report_template"
            attachment_use="False"
        />

      <report
            id="action_customer_feedback_wizard_report"
            model="customer.feedback.report.wizard"
            string="customer.feedback.report.wizard"
            report_type="qweb-pdf"
            name="customer_feedback.wizard_report_template"
            file="customer_feedback.wizard_report_template"
            attachment_use="False"
        />


  </data>
</odoo>


Final manifest file will look like:
# -*- coding: utf-8 -*-
{
    'name': "Customer Feedback",
    'version': '1.0',
    'summary': """Record Your Customer feedback""",
    'description': """
        Customer Feedback Module
            -create feedback to customers
    """,
    'author': "My Company",
    'category': 'Test',

    # any module necessary for this one to work correctly
    'depends': ['base'],
    'data': [
        'security/ir.model.access.csv',
        'views/customer_feedback_view.xml',
        'wizard/customer_feedback_wizard.xml',
        'report/report_action.xml',
        'report/customer_feedback_report.xml',
        'report/wizard_report.xml',
    ],
    'application': True,
}











No comments:

Post a Comment

How to create XLS report in Odoo 12

In this tutorial we'll be creating a simple XLS report that can be generated from wizard in Sales/ Report menu . XLS reports are made u...