简体   繁体   中英

Odoo - Invoke python function from the qweb report

<tbody>
  <tr t-foreach="o.line_ids.filtered(lambda line: line.appears_on_payslip)" t-as="line">
    <t t-if="line.code in ('BASIC','OT','DED','GROSS','NET')">
       <td><span t-field="line.code"/></td>
       <td><span t-field="line.name"/></td>
       <td><span t-field="line.quantity"/></td>
       <td><span t-field="line.amount" t-esc-options='{"widget": "monetary", "display_currency": o.company_id.currency_id}'/></td>
       <td><span t-field="line.total" t-esc-options='{"widget": "monetary", "display_currency": o.company_id.currency_id}'/></td>
    </t>
  </tr>
</tbody> 

The above code is body of a table in qweb report. Instead of "line.quantity", I want to invoke a python function "o.compute_overtime()" and write as:

<t t-if="line.code=='OT'">
 <td><span t-esc="i['ot_total']"/></td>
</t>

How can I invoke the function for just 1 field?

You need to create one parser class and in that you need to define one function which you can access from it's report.

from openerp import models
from openerp.report import report_sxw

class report_invoice_parser(report_sxw.rml_parse):
    def __init__(self, cr, uid, name, context=None):
        super(report_invoice_parser, self).__init__(cr,uid,name,context=context)
        self.localcontext.update({ 
                'get_name':self._get_name,
                })
        self.context=context

    def _get_name(self,line):

        if line.invoice_id.write_description == True:
            return line.name

        if line.product_id:
            if line.product_id.default_code:
                return "[%s] %s"%(line.product_id.default_code,line.product_id.name)
            else:
                return line.product_id.name
        else:
            line.name

class report_invoice(models.TransientModel):
    _name = "report.account.report_invoice"
    _inherit ="report.abstract_report"
    _template="account.report_invoice"
    _wrapped_report_class =report_invoice_parser

And in xml you need to call it as follow.

<tr t-foreach="o.invoice_line" t-as="l">
    <td><span t-esc="get_name(l)"/></td>
</tr>

You need to set your business logic in function, I have just share idea here. By this way you can call your method instead of field you can display function result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM