【问题标题】:Odoo 12 num2words Amount To TextOdoo 12 num2words 数量到文本
【发布时间】:2019-09-30 12:39:47
【问题描述】:

我是这个社区的新手,我为我的小公司设置了 Odoo 社区版本。我做了所有的事情,只是不知道如何设置 num2words 以在发票报告中显示总金额!

我在 Base/Modules 的 res_currency.py 中找到了 num2words api,但我花了两天时间研究如何连接,却一无所获。我必须继承什么以及如何以及将什么放入发票文档 qweb?

我做了这样的模块:

from num2words import num2words

class account_invoice(models.Model):
_inherit = "account.invoice"

 @api.multi
def amount_to_text(self, amount):
    self.ensure_one()
    def _num2words(number, lang):
        try:
            return num2words(number, lang=lang).title()
        except NotImplementedError:
            return num2words(number, lang='en').title()

if num2words is None:
        logging.getLogger(__name__).warning("The library 'num2words' is missing, cannot render textual amounts.")
        return ""

formatted = "%.{0}f".format(self.decimal_places) % amount
    parts = formatted.partition('.')
    integer_value = int(parts[0])
    fractional_value = int(parts[2] or 0)

    lang_code = self.env.context.get('lang') or self.env.user.lang
    lang = self.env['res.lang'].search([('code', '=', lang_code)])
    amount_words = tools.ustr('{amt_value} {amt_word}').format(
                    amt_value=_num2words(integer_value, lang=lang.iso_code),
                    amt_word=self.currency_unit_label,
                    )
    if not self.is_zero(amount - integer_value):
        amount_words += ' ' + _('and') + tools.ustr(' {amt_value} {amt_word}').format(
                    amt_value=_num2words(fractional_value, lang=lang.iso_code),
                    amt_word=self.currency_subunit_label,
                    )
    return amount_words

出现如下错误:

Error to render compiling AST
AttributeError: 'NoneType' object has no attribute 'currency_id'
Template: account.report_invoice_document_with_payments
Path: /templates/t/t/div/p[1]/span
Node: <span t-if="doc.currency_id" t-esc="doc.currency_id.amount_to_text(doc.amount_total)"/>

我在 QWeb 中这样写:

<span t-if="doc.currency_id" t-esc="doc.currency_id.amount_to_text(doc.amount_total)"/> 

提前谢谢你!

【问题讨论】:

    标签: odoo odoo-12 odoo-accounting


    【解决方案1】:

    在您的情况下,“currency_id”是一个 Many2one 字段。 'res.currency' 模型不包含类 'amount_to_text' 函数。

    您已经在 'account.invoice' 模型中编写了 amount_to_text 函数。所以改变你的样子,

    <span t-if="doc.currency_id" t-esc="doc.amount_to_text(doc.amount_total)"/> 
    

    OR(如果您的对象中没有 currency_id 字段)

     <span t-if="doc.amount_to_text" t-esc="doc.amount_to_text(doc.amount_total)"/> 
    

    【讨论】:

    • 我将 QWeb 更改为: 再次出现同样的错误
    • 您在哪个对象中有此报告?如果您没有 currency_id 字段,请将其添加到您的模型中,或者使用公司的货币(请参阅我编辑的答案)
    • 当我现在放新代码时出现错误:渲染编译 AST AttributeError 时出错:'NoneType' 对象没有属性'amount_to_text'
    【解决方案2】:

    请使用以下代码

    <span t-if="o.currency_id" t-esc="o.amount_to_text(o.amount_total)"/>
    

    您收到错误是因为在基本报告中他们使用的是 o 而不是 doc。请参阅 base 的以下代码部分。

    <t t-foreach="docs" t-as="o">
    

    所以尽量用o代替doc

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-01
      • 2020-06-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多