0

How to use the num2words library in Odoo 12 to show Total Amount in Invoice reports?

I found num2words function in /base/models/res_currency.py

@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
arshovon
  • 13,270
  • 9
  • 51
  • 69
Uros Kalajdzic
  • 349
  • 1
  • 6
  • 19

1 Answers1

0

Basically if the method is returning the text >> return amount_words << and require to pass the parameter as number >> def amount_to_text(self, amount): << what you can do as

Considering the method is working! <<

@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)
Jameel
  • 53
  • 6
  • Another example in odoo 11 def test_amount_to_text_10(self): """ verify that amount_to_text works as expected """ 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, "Amount in text should not depend on float representation") – Jameel Dec 01 '18 at 21:17