0
votes

I have a created module called 'custom_report_module', where I make a personalized invoice ... The invoice works well but I want to add the total amount in letters .. for example .. if the purchase reached 500,000 that I printed in letters "are: five hundred thousand guaranies "or" are: five hundred thousand "

class account_invoice(models.Model): 
_inherit = "account.invoice"
@api.multi 
def amount_to_text(self, amount, currency='rupee'):
    return amount_to_text(amount, currency)

my xml

<span t-esc="l.amount_to_text(l.price_unit, l.price_unit)"/>
1

1 Answers

0
votes

Please have a look at the this code

@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