how-to-create-and-export-xml-reports-in-odoo.png
Blogger_636752445163501004.png
By: Sayooj ao

How To Create And Export XML Report In Odoo

Some countries make it mandatory for the business organizations to submit their periodical records on sales and accounts related activities to the government. And XML records are the preferred in this case, as it offers the flexibility to change the data content whenever it is necessary.

Manipulation of the data is possible in XML kind of reports. By default, Odoo ERP does not support XML report, however, one can make this possible with the help of an extra module called report_xml.
 
In this blog, am explaining about the creation of XML report in Odoo 12.

Configurations required
1. Install lxml in Odoo's $PYTHONPATH From the following link: http://lxml.de/
2. Make the module report_xml dependent to our custom module.

Here, we are going to make an XML report on sales details, including details like sales number, salesperson and the total amount from the sales during a period of time.
Firstly, we have to create a wizard for reading the inputs start date and end date.

In Python file

from odoo import models, fields, API
class SaleReportWizard(models.Model):
    _name = 'sale.report.wizard'
    start_date = fields.Date(string='Start Date',required=True)
    end_date = fields.Date(string='End Date',required=True)
    @api.multi
    def print_report_xml(self):
        data = {'start_date': self.start_date, 'end_date': self.end_date}
        return self.env.ref('demo_xml_report.sale_xml_report').report_action(self, data=data)

View of the 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.wizard</field>
       <field name="arch" type="xml">
           <form string="Vendor Bill XML Report">
               <group col="4" colspan="4">
                   <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="res_model">sale.report.wizard</field>
       <field name="view_type">form</field>
       <field name="view_mode">form</field>
       <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 will produce a menu item in the reporting menu of sales and upon clicking that we get a wizard which can choose the start date and end date for our report purpose.

The following function will return the values selected in the wizard to our report file. The return of this function specifies return self.env.ref(‘module name.report tag id’)

@api.multi
def print_report_xml(self):
   data = {'start_date': self.start_date, 'end_date': self.end_date}
   return self.env.ref('demo_xml_report.sale_xml_report').report_action(self, data=data)


Now we have to define the report tag in XML. Here we have to specify the wizard model in the model and define the report_type as “qweb-xml”

<?xml version="1.0" encoding="UTF-8"?>
<odoo>
   <report id="sale_xml_report"
           string="sale_xml_report"
           model="sale.report.wizard"
           report_type="qweb-xml"
           file="demo_xml_report.report_sale_docs"
           name="demo_xml_report.report_sale_docs"/>
</odoo>

Now we have to override the method _get_report_values for passing our required data to the report template.

from odoo import models, api
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
       }

Here we return the start date and end date which is from the wizard and the data that we fetch using the query.

Now we can define the template with required tags.

<?xml version="1.0" encoding="UTF-8"?>
<odoo>
   <template id="report_sale_docs">
       <MAINHEADER>
           <DATEDETAILS>
               <STARTDATE t-esc="start_date"/>
               <ENDDATE t-esc="end_date"/>
           </DATEDETAILS>
           <SALEDETAILS 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']"/>
           </SALEDETAILS>
       </MAINHEADER>
   </template>
</odoo>

And the output will be following .xml file.

You can download the dependent module report_xml from the following link: 
https://www.odoo.com/apps/modules/12.0/report_xml/




Comments

0


Leave a comment

 
Calicut

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

London

Cybrosys Limited
Alpha House,
100 Borough High Street, London,
SE1 1LB, United Kingdom

Kochi

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

Bangalore

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

Send Us A Message