← Back to team overview

openerp-canada team mailing list archive

[Merge] lp:~savoirfairelinux-openerp/openerp-canada/7.0-bug-missing-amount_in_word-proforma_voucher into lp:openerp-canada

 

You have been requested to review the proposed merge of lp:~savoirfairelinux-openerp/openerp-canada/7.0-bug-missing-amount_in_word-proforma_voucher into lp:openerp-canada.

For more details, see:
https://code.launchpad.net/~savoirfairelinux-openerp/openerp-canada/7.0-bug-missing-amount_in_word-proforma_voucher/+merge/207570



-- 
https://code.launchpad.net/~savoirfairelinux-openerp/openerp-canada/7.0-bug-missing-amount_in_word-proforma_voucher/+merge/207570
Your team OpenERP Canada Team is requested to review the proposed merge of lp:~savoirfairelinux-openerp/openerp-canada/7.0-bug-missing-amount_in_word-proforma_voucher into lp:openerp-canada.
=== modified file 'l10n_ca_account_check_writing/account_voucher.py'
--- l10n_ca_account_check_writing/account_voucher.py	2013-06-11 12:24:41 +0000
+++ l10n_ca_account_check_writing/account_voucher.py	2014-02-21 18:26:48 +0000
@@ -21,6 +21,7 @@
 
 from openerp.osv import orm, fields
 from openerp.tools.translate import _
+from openerp.tools import config
 # OpenERP's built-in routines for converting numbers to words is pretty bad, especially in French
 # This is why we use the library below. You can get it at:
 # https://pypi.python.org/pypi/num2words
@@ -30,6 +31,7 @@
 # picks them up during .pot generation
 _("and")
 
+
 def custom_translation(s, lang):
     # OpenERP uses the current stack frame, yes, the *stack frame* to determine which language _()
     # should translate a string in. If we want to translate a string in another language, such as
@@ -38,6 +40,7 @@
     context = {'lang': lang}
     return _(s)
 
+
 def get_amount_line(amount, currency, lang):
     try:
         amount_in_word = num2words(int(amount), lang=lang[:2])
@@ -56,6 +59,7 @@
         amount_line_fmt = '{amount_in_word} {currency_name} {AND} {cents}/100 {stars}'
     return amount_line_fmt.format(**vars())
 
+
 class account_voucher(orm.Model):
     _inherit = 'account.voucher'
 
@@ -63,25 +67,35 @@
                         journal_id, currency_id, ttype, date,
                         payment_rate_currency_id, company_id, context=None):
         """ Inherited - add amount_in_word and allow_check_writting in returned value dictionary """
-        if not context:
+        if context is None:
             context = {}
+        if isinstance(ids, (int, long)):
+            ids = [ids]
         default = super(account_voucher, self).onchange_amount(
             cr, uid, ids, amount, rate, partner_id, journal_id, currency_id,
             ttype, date, payment_rate_currency_id, company_id, context=context)
+
         if 'value' in default:
-            amount = 'amount' in default['value'] and default['value']['amount'] or amount
-            if ids:
-                supplier_lang = self.browse(cr, uid, ids[0], context=context).partner_id.lang
+            amount = default['value'].get('amount', amount)
+            amount_in_word = None
+            if len(ids):
+                i_id = ids[0]
+                amount_in_word = self._get_amount_in_word(cr, uid, i_id=i_id, amount=amount,
+                                                          context=context)
             else:
-                # It's a new record and we don't have access to our supplier lang yet
-                supplier_lang = 'en_US'
-            supplier_context = context.copy()
-            # for some calls, such as the currency browse() call, we want to separate our user's
-            # language from our supplier's. That's why we need a separate context.
-            supplier_context['lang'] = supplier_lang
-            currency = self.pool.get('res.currency').browse(cr, uid, currency_id, context=supplier_context)
-            amount_line = get_amount_line(amount, currency, supplier_lang)
-            default['value'].update({'amount_in_word':amount_line})
+                line_cr_ids = default["value"].get("line_cr_ids")
+                if line_cr_ids and len(line_cr_ids):
+                    line_cr_id = line_cr_ids[0]
+                    currency_id = line_cr_id.get("currency_id")
+                    if currency_id is not None:
+                        amount_in_word = self._get_amount_in_word(cr,
+                                                                  uid,
+                                                                  currency_id=currency_id,
+                                                                  amount=amount,
+                                                                  context=context)
+
+            if amount_in_word is not None:
+                default['value'].update({'amount_in_word': amount_in_word})
             if journal_id:
                 allow_check_writing = self.pool.get('account.journal').browse(
                     cr, uid, journal_id, context=context).allow_check_writing
@@ -90,7 +104,7 @@
 
     def print_check(self, cr, uid, ids, context=None):
         if not ids:
-            return  {}
+            return {}
 
         check_layout_report = {
             'top': 'account.print.check.top',
@@ -106,13 +120,55 @@
             'type': 'ir.actions.report.xml',
             'report_name': check_layout_report[check_layout],
             'datas': {
-                    'model': 'account.voucher',
-                    'id': ids and ids[0] or False,
-                    'ids': ids and ids or [],
-                    'report_type': 'pdf'
-                },
+                'model': 'account.voucher',
+                'id': ids and ids[0] or False,
+                'ids': ids and ids or [],
+                'report_type': 'pdf'
+            },
             'nodestroy': True
-            }
+        }
+
+    def proforma_voucher(self, cr, uid, ids, context=None):
+        # update all amount in word when perform a voucher
+        if context is None:
+            context = {}
+        if isinstance(ids, (int, long)):
+            ids = [ids]
+        for i_id in ids:
+            amount_in_word = self._get_amount_in_word(cr, uid, i_id, context=context)
+            self.write(cr, uid, i_id, {'amount_in_word': amount_in_word}, context=context)
+
+        return super(account_voucher, self).proforma_voucher(cr, uid, ids, context=context)
+
+    def _get_amount_in_word(self, cr, uid, i_id=None, currency_id=None, amount=None, context=None):
+        if amount is None:
+            amount = self.browse(cr, uid, i_id, context=context).amount
+
+        # get lang
+        supplier_lang = None
+        if i_id is not None:
+            partner_id = self.browse(cr, uid, i_id, context=context).partner_id
+            if partner_id:
+                supplier_lang = partner_id.lang
+        if supplier_lang is None:
+            if context is not None:
+                supplier_lang = context.get('lang')
+            else:
+                supplier_lang = config.get('lang')
+
+        supplier_context = context.copy()
+        # for some calls, such as the currency browse() call, we want to separate our user's
+        # language from our supplier's. That's why we need a separate context.
+        supplier_context['lang'] = supplier_lang
+        if currency_id is None:
+            if i_id is None:
+                return None
+            currency_id = self._get_current_currency(cr, uid, i_id, context=context)
+
+        currency = self.pool.get('res.currency').browse(cr, uid, currency_id,
+                                                        context=supplier_context)
+        # get the amount_in_word
+        return get_amount_line(amount, currency, supplier_lang)
 
 # By default, the supplier reference number is not so easily accessible from a voucher line because
 # there's no direct link between the voucher and the invoice. Fortunately, there was this recently
@@ -120,21 +176,24 @@
 # https://code.launchpad.net/~elbati/account-payment/adding_account_voucher_supplier_invoice_number_7/+merge/165622
 # which solves this exact problem and I shamelessely copied that code, which works well.
 
+
 class voucher_line(orm.Model):
     _inherit = 'account.voucher.line'
-    
+
     def get_suppl_inv_num(self, cr, uid, move_line_id, context=None):
         move_line = self.pool.get('account.move.line').browse(cr, uid, move_line_id, context)
-        return (move_line.invoice and move_line.invoice.supplier_invoice_number or '')
+        return move_line.invoice and move_line.invoice.supplier_invoice_number or ''
 
     def _get_supplier_invoice_number(self, cr, uid, ids, name, args, context=None):
-        res={}
+        res = {}
         for line in self.browse(cr, uid, ids, context):
             res[line.id] = ''
             if line.move_line_id:
-                res[line.id] = self.get_suppl_inv_num(cr, uid, line.move_line_id.id, context=context)
+                res[line.id] = self.get_suppl_inv_num(cr, uid, line.move_line_id.id,
+                                                      context=context)
         return res
-    
+
     _columns = {
-        'supplier_invoice_number': fields.function(_get_supplier_invoice_number, type='char', size=64, string="Supplier Invoice Number"),
+        'supplier_invoice_number': fields.function(_get_supplier_invoice_number, type='char',
+                                                   size=64, string="Supplier Invoice Number"),
     }