Odoo 12 num2words Amount To Text

2.4k views Asked by At

I'm new in this community and I setting up Odoo Community version for my little comapny. I does all the things just doesn't know how to set up num2words to show Total Amount in Invoice Reports!

I found num2words api in res_currency.py in Base/Modules but I spend two days researching how to connect and nothing. What I have to inherite and how and also what to put in invoice document qweb?

I made module like this:

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

Got error like this:

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)"/>

In QWeb I put this:

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

Thank you in advance!

2

There are 2 answers

3
sfx On

In your case "currency_id" is a Many2one field. The 'res.currency' model does not contain the class 'amount_to_text' function.

You have written amount_to_text function in 'account.invoice' model. So change your like this,

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

OR(if you don't have currency_id field in your object)

 <span t-if="doc.amount_to_text" t-esc="doc.amount_to_text(doc.amount_total)"/> 
0
Anitha Das B On

Please use the below code

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

You are getting the error because in the base report they are using o instead of doc. please see the below part of code from base.

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

So try to use o instead of doc