Created
October 8, 2022 17:04
-
-
Save 3mrdev/965cb18108449b05f531d235600877f2 to your computer and use it in GitHub Desktop.
How to make a custom report with a wizard in Odoo ?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<odoo> | |
<record model="report.paperformat" id="paperformat_a4"> | |
<field name="name">paperformat.custom.report</field> | |
<field name="default" eval="True"/> | |
<field name="format">A4</field> | |
<field name="page_width">0</field> | |
<field name="page_width">0</field> | |
<field name="orientation">Portrait</field> | |
<field name="margin_top">30</field> | |
<field name="margin_right">5</field> | |
<field name="margin_bottom">10</field> | |
<field name="margin_left">5</field> | |
<field name="header_line" eval="False"/> | |
<field name="header_spacing">20</field> | |
<field name="dpi">90</field> | |
</record> | |
<report id="your_report_name_action" | |
model="your_model_name.report.wizard" | |
string="Custom Report" | |
report_type="qweb-pdf" | |
name="your_module_name.your_report_name_view" | |
print_report_name="'%s' % (object.title)" | |
paperformat="paperformat_a4" | |
menu="False"/> | |
<template id="your_report_name_view"> | |
<t t-call="web.html_container"> | |
<t t-call="web.external_layout"> | |
<div class="page"> | |
<h2 class="text-center"><t t-esc="title"/></h2> | |
<t t-foreach="docs" t-as="doc"> | |
<t t-esc="doc['first_variable']"/> | |
</t> | |
</div> | |
</t> | |
</t> | |
</template> | |
</odoo> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from odoo import models, fields, api | |
from odoo.exceptions import ValidationError | |
import logging | |
_logger = logging.getLogger(__name__) | |
class ReportWizard(models.TransientModel): | |
_name = 'your_model_name.report.wizard' | |
title = fields.Char(required=True) | |
def print_report(self): | |
"""Call when button 'Get Report' clicked. | |
""" | |
docs = [] | |
docs.append({ | |
'first_variable': "anything that goes to the report", | |
}) | |
data = { | |
'doc_ids': self.ids, | |
'doc_model': self._name, | |
'title': title, | |
'docs': docs, | |
} | |
# use `module_name.report_id` as reference. | |
# `report_action()` will call `get_report_values()` and pass `data` automatically. | |
return self.env.ref('your_module_name.your_report_name_action').report_action(self, data=data) | |
class ReportTemplate(models.AbstractModel): | |
"""Abstract Model for report template. | |
for `_name` model, please use `report.` as prefix then add `module_name.report_name`. | |
""" | |
_name = 'report.your_module_name.your_report_name_view' | |
@api.model | |
def _get_report_values(self, docids, data=None): | |
return data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<odoo> | |
<record model="ir.ui.view" id="custom_report_wizard"> | |
<field name="name">Custom Report</field> | |
<field name="model">your_model_name.report.wizard</field> | |
<field name="type">form</field> | |
<field name="arch" type="xml"> | |
<form string="Custom Report"> | |
<group> | |
<field name="title"/> | |
</group> | |
<footer> | |
<button name="get_report" string="Get Report" type="object" class="oe_highlight"/> | |
<button string="Cancel" special="cancel"/> | |
</footer> | |
</form> | |
</field> | |
</record> | |
<!-- active_ids will give you a list of the selected records from the tree view of your binindg model ex. sale.order --> | |
<act_window id="action_custom_report_wizard" | |
name="Custom Report" | |
res_model="your_model_name.report.wizard" | |
context="{'default_your_m2m_or_o2m_field': active_ids}" | |
view_mode="form" | |
binding_model="your_binding_model" | |
binding_views="list" | |
target="new"/> | |
</odoo> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment