Sunday, 14 April 2013

How to Inherit Custom RML Report Parser Class in OpenERP

How to Inherit Custom RML Report Parser Class in OpenERP


RML parser classes are registered globally as Services. For example the Sale Order parser class is registered in addons/sale/report/sale_order.py with :
report_sxw.report_sxw('report.sale.order', 'sale.order','addons/sale/report/sale_order.rml', parser=order, header="external")
If you try to create another parser with the same name, you get an error : The report "sale.order" already exists!

A simple way to replace the sale.order parser and use a custom parser class is to remove it from the global service registry with :

from netsvc import Service
del Service._services['report.sale.order']
Here is a full example we used to convert amount into text in the sale order report :

from sale.report import sale_order
from tools import amount_to_text_en
from report import report_sxw
# create a custom parser inherited from sale order parser:
class new_order_report(sale_order.order):
    '''Custom parser with an additional method
    '''
    def __init__(self, cr, uid, name, context):
        super(new_order_report, self).__init__(cr, uid, name, context)
        self.localcontext.update({
            'convert': self.convert,
        }) 
 def convert(self, amount, cur):
        return amount_to_text_en.amount_to_text(amount,'en',cur)
# remove previous sale.report service :
from netsvc import Service
del Service._services['report.sale.order'] 
# register the new report service :
report_sxw.report_sxw(
 'report.sale.order',
 'sale.order',
 'addons/path/report/sale_order.rml',
 parser=new_order_report)

Copy and past the RML file and add following code :
In Words: [[ convert(o.amount_total, o.company_id.currency_id.name) ]]

Now, you need to overwrite report's record in xml :
<report auto="False" id="sale.report_sale_order" model="sale.order" name="sale.order" rml="addons/path/report/sale_order.rml" string="Quotation / Order" usage="default"/>