Odoo 12 num2words Сумма к тексту

Я новичок в этом сообществе, и я настраиваю версию сообщества Odoo для моей маленькой компании. Я делаю все, что не знаю, как настроить num2words для отображения общей суммы в отчетах по счетам!

Я нашел API num2words в res_currency.py в Base/Modules, но я провожу два дня, изучая, как подключиться и ничего. Что я должен унаследовать и как, а также что положить в документ счета-фактуры 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)"/> 

Заранее спасибо!

1 ответ

Пожалуйста, используйте приведенный ниже код

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

Вы получаете сообщение об ошибке, потому что в базовом отчете используется o вместо doc. см. приведенную ниже часть кода из базы.

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

Поэтому попробуйте использовать o вместо doc

В вашем случае "currency_id" - это поле Many2one. Модель "res.currency" не содержит классовой функцииamount_to_text.

Вы написали функцию amount_to_text в моделиaccount.invoice. Так что измени свой так,

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

ИЛИ (если у вас нет поля currency_id в вашем объекте)

 <span t-if="doc.amount_to_text" t-esc="doc.amount_to_text(doc.amount_total)"/> 
Другие вопросы по тегам