This question has been flagged
3 Replies
6095 Views

 

Hi,

I need to add a field in amount in words account.invoice model .... need a help

class account_invoice(osv.osv):
  _inherit = 'account.invoice'
  _description = "Account invoice"
  def _amount_in_words(self, cr, uid, ids, field_name, arg, context=None):
    cur_obj = self.pool.get('res.currency')
    res = {}
    for order in self.browse(cr, uid, ids, context=context):
        taxed = untaxed = 0.0
        res[order.id] = {
            'amount_words_invoice': '0.0',
                        }
        val = val1 = 0.0
        cur = order.pricelist_id.currency_id  #How to change code  here @@@@@Error from this line
        for line in order.order_line:
            val1 += line.price_subtotal
            val += self._amount_line_tax(cr, uid, line, context=context)
        taxed = cur_obj.round(cr, uid, cur, val)
        untaxed = cur_obj.round(cr, uid, cur, val1)
        res[order.id] = amount_to_text_en.amount_to_text(float(taxed + untaxed))
    return res

  _columns = {
              'amount_words_invoice': fields.function(_amount_in_words, string='In Words', type="char", store=True, help="The amount in words"),

             }  
account_invoice()

-------------------.xml-----------

 <record model="ir.ui.view" id='view_account_order_form' >
      <field name="name">account.order.form</field>
      <field name="model">account.invoice</field>
      <field name="inherit_id" ref="account.invoice_form"/>
      <field name="arch" type="xml">
          <field name="amount_total" position="after">
            <field name="amount_words_invoice"/>
          </field>
       </field>
    </record>

Avatar
Discard
Best Answer

amount_word.py

from openerp.osv import fields, osv
import amount_to_text_in
from amount_to_text_in import amount_to_text 

class account_order(osv.osv):
  _inherit = 'account.invoice'
  _description = "Account invoice"
  def _amount_in_words(self, cr, uid, ids, field_name, arg, context=None):
      cur_obj = self.pool.get('res.currency')
      res = {}
      for invoice in self.browse(cr, uid, ids, context=context):
            res[invoice.id] = {
                'amount_untaxed': 0.0,
                'amount_tax': 0.0,
                'amount_total': 0.0
            }
            for line in invoice.invoice_line:
                res[invoice.id]['amount_untaxed'] += line.price_subtotal
            for line in invoice.tax_line:
                res[invoice.id]['amount_tax'] += line.amount
            res[invoice.id]['amount_total'] = res[invoice.id]['amount_tax'] + res[invoice.id]['amount_untaxed']
            res[invoice.id] = amount_to_text_in.amount_to_text (res[invoice.id]['amount_total'])
      return res 

  
  _columns = {
              'amount_words': fields.function(_amount_in_words, string='In Words', type="char", store=True, help="The amount in words"),                       }  
    
account_order()

              amount_word_view.xml

<?xml version="1.0" encoding="UTF-8"?>
<openerp>
  <data> 
    <record model="ir.ui.view" id='invoice_form' >
      <field name="name">account.invoice.form1</field>
      <field name="model">account.invoice</field>
      <field name="inherit_id" ref="account.invoice_form"/>
      <field name="arch" type="xml">    
          <field name="amount_total" position="after">
            <field name="amount_words"/> 
          </field>     
       </field>
    </record>

    <record model="ir.actions.act_window" id="action_orders">
    <field name="name">Accountinvoice1</field>
    <field name="res_model">account.invoice</field>
    <field name="view_type">form</field>
    <field name="view_mode">tree,form</field>
   </record>   


  </data>
</openerp>

amount_to_text_in.py

import logging
from openerp.tools.translate import _

_logger = logging.getLogger(__name__)

#-------------------------------------------------------------
#ENGLISH
#-------------------------------------------------------------

to_19 = ( 'Zero',  'One',   'Two',  'Three', 'Four',   'Five',   'Six',
          'Seven', 'Eight', 'Nine', 'Ten',   'Eleven', 'Twelve', 'Thirteen',
          'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen' )
tens  = ( 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety')
denom = ( '',
          'Thousand',     'Million',         'Billion',       'Trillion',       'Quadrillion',
          'Quintillion',  'Sextillion',      'Septillion',    'Octillion',      'Nonillion',
          'Decillion',    'Undecillion',     'Duodecillion',  'Tredecillion',   'Quattuordecillion',
          'Sexdecillion', 'Septendecillion', 'Octodecillion', 'Novemdecillion', 'Vigintillion' )

def _convert_nn(val):
    """convert a value < 100 to English.
    """
    if val < 20:
        return to_19[val]
    for (dcap, dval) in ((k, 20 + (10 * v)) for (v, k) in enumerate(tens)):
        if dval + 10 > val:
            if val % 10:
                return dcap + '-' + to_19[val % 10]
            return dcap

def _convert_nnn(val):
    """
        convert a value < 1000 to english, special cased because it is the level that kicks 
        off the < 100 special case.  The rest are more general.  This also allows you to
        get strings in the form of 'forty-five hundred' if called directly.
    """
    word = ''
    (mod, rem) = (val % 100, val // 100)
    if rem > 0:
        word = to_19[rem] + ' Hundred'
        if mod > 0:
            word += ' '
    if mod > 0:
        word += _convert_nn(mod)
    return word

def english_number(val):
    if val < 100:
        return _convert_nn(val)
    if val < 1000:
         return _convert_nnn(val)
    for (didx, dval) in ((v - 1, 1000 ** v) for v in range(len(denom))):
        if dval > val:
            mod = 1000 ** didx
            l = val // mod
            r = val - (l * mod)
            ret = _convert_nnn(l) + ' ' + denom[didx]
            if r > 0:
                ret = ret + ', ' + english_number(r)
            return ret

def amount_to_text(number, currency):
    number = '%.2f' % number
    units_name = currency
    list = str(number).split('.')
    start_word = english_number(int(list[0]))
    if int(list[1]) == '00':
      end_word = ''
      
    else:
      end_word = english_number(int(list[1]))
      
    cents_number = int(list[1])
    cents_name = 'Only'

    return ' '.join(filter(None, [start_word, units_name, (start_word or units_name) and (end_word or cents_name) and end_word, cents_name]))


#-------------------------------------------------------------
# Generic functions
#-------------------------------------------------------------

_translate_funcs = {'en' : amount_to_text}
    
#TODO: we should use the country AND language (ex: septante VS soixante dix)
#TODO: we should use en by default, but the translation func is yet to be implemented
def amount_to_text(nbr, lang='en', currency=''):
    """ Converts an integer to its textual representation, using the language set in the context if any.
    
        Example::
        
            1654: thousands six cent cinquante-quatre.
    """
    import openerp.loglevels as loglevels
#    if nbr > 10000000:
#        _logger.warning(_("Number too large '%d', can not translate it"))
#        return str(nbr)
    
    if not _translate_funcs.has_key(lang):
        _logger.warning(_("no translation function found for lang: '%s'"), lang)
        #TODO: (default should be en) same as above
        lang = 'en'
    return _translate_funcs[lang](abs(nbr), currency)

if __name__=='__main__':
    from sys import argv
    
    lang = 'nl'
    if len(argv) < 2:
        for i in range(1,200):
            print i, ">>", int_to_text(i, lang)
        for i in range(200,999999,139):
            print i, ">>", int_to_text(i, lang)
    else:
        print int_to_text(int(argv[1]), lang)

 

thus you can create amount word in account_invoice.

Avatar
Discard
Author Best Answer

thank you remya for your great support Also i need the currency eg:12.35 Rs Twelve rupees thirtyfive paise

Avatar
Discard