Enable Dark Mode!
how-to-create-export-xml-reports-in-odoo-15.jpg
By: Anagha NK

How to Create & Export XML Reports in Odoo 15

Technical Odoo 15

For any business organization, it is mandatory to submit the records periodically. The records are based on sales accounts-related activities and are given to the government. In that case, XML records are preferred to offer the changing data content that is flexible whenever necessary. In Odoo, ERP does not support XML reports by default. So, with the help of an extra module named report_xml, we can make this possible. This is preferred to offer the changing data content that is flexible whenever it is necessary. In Odoo, ERP does not support XML reports by default. So, with the help of an extra module named report_xml, we can make this possible.
This blog explains how to create and export XML reports in Odoo 15.
Required
1. From the following link: http://lxml.de/  install XML in Odoo's $PYTHONPATH
2. Don't forget to make the module report_xml dependent on the custom module.
Now let's see that create a detailed sales XML report in the sales module. The report includes the details like a salesperson, number and total amount of the sales on the given period. For that, create a wizard that reads the start and end date input.
Python file
from Odoo import models, fields, API
class SaleReportWizard(models.Model):
_inherit = 'sale.report'
start_date = fields.Date(string='Start Date', required=True)
end_date = fields.Date(string='End Date', required=True)
def print_report_xml(self):
    event = self.env['sale.report'].search_read([])
    print("event", event)
    data = {
        'form_data': self.read()[0],
        'start_date': self.start_date,
        'end_date': self.end_date,
    }
    print(data)
    return self.env.ref('demo_xml_report.sale_xml_report'). \
        report_action(self, data=data)
View of wizard
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="sales_report_wizard" model="ir.ui.view">
    <field name="name">Sales Order Report</field>
    <field name="model">sale.report</field>
    <field name="arch" type="xml">
        <form string="Vendor Bill XML Report">
    <group>
                <field name="start_date"/>
                <field name="end_date"/>
            </group>
            <footer>
                <button name="print_report_xml" type="object" string="Print XML" class="oe_highlight"/>
                <button string="Cancel" class="btn btn-default" special="cancel"/>
            </footer>

        </form>
    </field>
</record>
<record id="action_sales_xml_report" model="ir.actions.act_window">
    <field name="name">Sales XML Report</field>
    <field name="name">Sales XML Report</field>
    <field name="type">ir.actions.act_window</field>
    <field name="res_model">sale.report</field>
    <field name="view_mode">form</field>
    <field name="view_mode">form</field>
    <field name="view_id" ref="sales_report_wizard"/>
    <field name="target">new</field>
</record>
<menuitem id="menu_fiscal_sales_xml_report"
          name="Sales XML Report"
          parent="sale.menu_sale_report"
          action="action_sales_xml_report"/>
</odoo>
This creates a new menu item in sales reporting and when clicking that to get a wizard. Here we can choose the start and end date of the report.
The function below returns the values we selected in the wizard for the report file.
The return function specifies return self.env.ref(‘module_name.report_tag_id’).
	def print_report_xml(self):
    event = self.env['sale.report'].search_read([])
    print("event", event)
    data = {
        'form_data': self.read()[0],
        'start_date': self.start_date,
        'end_date': self.end_date,
    }
    print(data)
    return self.env.ref('demo_xml_report.sale_xml_report'). \
        report_action(self, data=data)
Now we can check how to define the report tag on XML. So, we have to specify the wizard model in our model and  report_type as “qweb-xml” to be defined.
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<record id="sale_xml_report" model="ir.actions.report">
    <field name="name">sale_xml_report</field>
    <field name="model">sale.report</field>
    <field name="report_type">qweb-xml</field>
    <field name="report_name">demo_xml_report.report_sale_docs</field>
    <field name="report_file">demo_xml_report.report_sale_docs</field>
</record>
</odoo>
Now we have to pass the data to the report template by overriding the _get_report method.
class VendorBillXmlReport(models.TransientModel):
_name = "report.demo_xml_report.report_sale_docs"
@api.model
def _get_report_values(self, docids, data=None):
    start_date = data['start_date']
    end_date = data['end_date']
    cr = self._cr
    query = """select so.name as sale_sequence,so.amount_total as total_amount,rp.name as sales_person_name
    from sale_order so
    join res_users ru
    on ru.id = so.user_id
    join res_partner rp
    on rp.id = ru.partner_id
    where so.date_order >= '%s' and so.date_order <= '%s'" % (start_date, end_date)
    """
    cr.execute(query)
    dat = cr.dictfetchall()
    return {
        'start_date': start_date,
        'end_date': end_date,
        'dat': dat
    }
Now we return the start date and end date here. So from the wizard, the data that fetch through using the query.
Here we can define the template with the required tags.
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<template id="report_sale_docs">
<HEADER>
    <DETAILS ABOUT DATE>
        <START_DATE t-esc="start_date"/>
        <END_DATE t-esc="end_date"/>
    </DATE_DETAILS>
    <SALE_DETAILS t-foreach="dat" t-as="sales">
        <NUMBER t-esc="sales['sale_sequence']"/>
        <AMOUNT t-esc="sales['total_amount']"/>
        <SALESPERSON t-esc="sales['sales_person_name']"/>
    </SALE_DETAILS>
</HEADER>
</template>
</odoo>
The output is .xml file.
You can download report_xml, the  dependent module, through the following link:  https://www.odoo.com/apps/modules/15.0/report_xml/


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message