如何关联国家和发票以打印自定义文本?

问题描述

在发票视图中,我有一个自定义布尔字段和一个自定义文本字段。

 odoo Server Error
Traceback (most recent call last):
  File "/home/ubuntu/odooAddons/odoo/odoo/addons/base/models/qweb.py",line 348,in _compiled_fn
    return compiled(self,append,new,options,log)
  File "<template>",line 1,in template_account_report_invoice_document_with_payments_104
  File "<template>",line 2,in body_call_content_103
  File "/home/ubuntu/odooAddons/odoo/addons-extra/emb_account/models/account_invoice.py",line 13,in country_group_country_ids
    return self.env['country.group'].search([('name','=',group)]).mapped('country_ids.id')
  File "/home/ubuntu/odooAddons/odoo/odoo/api.py",line 831,in __getitem__
    return self.registry[model_name]._browse((),self)
  File "/home/ubuntu/odooAddons/odoo/odoo/modules/registry.py",line 177,in __getitem__
    return self.models[model_name]
KeyError: 'country.group'

During handling of the above exception,another exception occurred:

Traceback (most recent call last):
  File "/home/ubuntu/odooAddons/odoo/addons/web/controllers/main.py",line 1686,in report_download
    response = self.report_routes(reportname,docids=docids,converter=converter)
  File "/home/ubuntu/odooAddons/odoo/odoo/http.py",line 519,in response_wrap
    response = f(*args,**kw)
  File "/home/ubuntu/odooAddons/odoo/addons/web/controllers/main.py",line 1627,in report_routes
    pdf = report.with_context(context).render_qweb_pdf(docids,data=data)[0]
  File "/home/ubuntu/odooAddons/odoo/odoo/addons/base/models/ir_actions_report.py",line 727,in render_qweb_pdf
    html = self.with_context(context).render_qweb_html(res_ids,line 767,in render_qweb_html
    return self.render_template(self.report_name,data),'html'
  File "/home/ubuntu/odooAddons/odoo/odoo/addons/base/models/ir_actions_report.py",line 540,in render_template
    return view_obj.render_template(template,values)
  File "/home/ubuntu/odooAddons/odoo/odoo/addons/base/models/ir_ui_view.py",line 1338,in render_template
    return self.browse(self.get_view_id(template)).render(values,engine)
  File "/home/ubuntu/odooAddons/odoo/addons/web_editor/models/ir_ui_view.py",line 29,in render
    return super(IrUiView,self).render(values=values,engine=engine,minimal_qcontext=minimal_qcontext)
  File "/home/ubuntu/odooAddons/odoo/odoo/addons/base/models/ir_ui_view.py",line 1347,in render
    return self.env[engine].render(self.id,qcontext)
  File "/home/ubuntu/odooAddons/odoo/odoo/addons/base/models/ir_qweb.py",line 59,in render
    result = super(IrQWeb,self).render(id_or_xml_id,values=values,**context)
  File "/home/ubuntu/odooAddons/odoo/odoo/addons/base/models/qweb.py",line 275,in render
    self.compile(template,options)(self,body.append,values or {})
  File "/home/ubuntu/odooAddons/odoo/odoo/addons/base/models/qweb.py",line 350,in _compiled_fn
    raise e
  File "/home/ubuntu/odooAddons/odoo/odoo/addons/base/models/qweb.py",in template_967_50
  File "<template>",in body_call_content_49
  File "<template>",line 3,in foreach_48
  File "/home/ubuntu/odooAddons/odoo/odoo/addons/base/models/qweb.py",line 355,in _compiled_fn
    raise QWebException("Error to render compiling AST",e,path,node and etree.tostring(node[0],encoding='unicode'),name)
odoo.addons.base.models.qweb.QWebException: 'country.group'
Traceback (most recent call last):
  File "/home/ubuntu/odooAddons/odoo/odoo/addons/base/models/qweb.py",in __getitem__
    return self.models[model_name]
KeyError: 'country.group'

Error to render compiling AST
KeyError: 'country.group'
Template: account.report_invoice_document_with_payments
Path: /templates/t/t/div/t[2]
Node: <t t-set="is_for_emb_message_country" t-value="o.partner_id.country_id and o.partner_id.country_id.id in o.country_group_country_ids('Exportaciones')"/>
            

我正在尝试在报告中打印我的自定义字段的文本,但只有当发票将转到国家/地区组时,我才知道它的 ID。 我该如何做这个功能? 如何关联国家/地区和发票以创建此功能

解决方法

最简单的方法是在账户发票模型中添加一个函数并从qweb报告中调用它。您可以定义一个 qweb report parser 并在那里定义它,以便它在报告上下文中可用。

示例

我想国家组的定义如下:

class CountryGroup(models.Model):
    _name = 'country.group'

    name = fields.Char()
    country_ids = fields.Many2many("res.country")

您只需要在帐户发票模型中定义一个函数来返回特定国家/地区组的国家/地区 ID:

class AccountInvoice(models.Model):
    _inherit = 'account.invoice'

    def country_group_country_ids(self,group):
        return self.env['country.group'].search([('name','=',group)]).mapped('country_ids.id')

以下代码扩展了发票报告并在发票状态之后显示参考字段以供演示:

<template id="report_invoice_document" inherit_id="account.report_invoice_document">
    <xpath expr="//h2" position="after">
        <t t-if="o.partner_id.country_id and o.partner_id.country_id.id in o.country_group_country_ids('export')">
            <p t-field="o.reference"/>
        </t>
    </xpath>
</template>