【问题标题】:Odoo 12 Amount in WordsOdoo 12 单词数量
【发布时间】:2019-05-01 16:09:26
【问题描述】:

如何使用Odoo 12 中的num2words 库在发票报告中显示Total Amount

我在/base/models/res_currency.py中找到了num2words函数

@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

【问题讨论】:

    标签: python-3.x python-3.5 odoo odoo-12


    【解决方案1】:

    基本上如果方法返回文本 >> return amount_words > def amount_to_text(self, amount):

    考虑到方法有效!

    @api.one
    def amount_in_words_from_number(self):
        amount_in_number = 12345
        amount_in_words = ''
        #Pass the amount in number as parameter which should return number in text
        amount_in_words = self.amount_to_text(amount_in_number)
        print("Amount in words", amount_in_words)
    

    【讨论】:

    • odoo 11 def test_amount_to_text_10(self) 中的另一个示例:""" 验证 amount_to_text 是否按预期工作 """ currency = self.env.ref('base.EUR') amount_target = currency.amount_to_text (0.29) amount_test = currency.amount_to_text(0.28) self.assertNotEqual(amount_test, amount_target, "文本中的金额不应依赖于浮点表示")
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-01
    • 2020-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多