[FIX] Avoid display write-off in pay invoice wizard. Put the right date and currency...
authorJoel Grand-Guillaume <joel.grandguillaume@camptocamp.com>
Mon, 2 Nov 2009 08:52:13 +0000 (09:52 +0100)
committerJoel Grand-Guillaume <joel.grandguillaume@camptocamp.com>
Mon, 2 Nov 2009 08:52:13 +0000 (09:52 +0100)
lp bug: https://launchpad.net/bugs/453030 fixed

bzr revid: joel.grandguillaume@camptocamp.com-20091102085213-d3w0kwk10sj6gyb8

addons/account/invoice.py
addons/account/wizard/wizard_pay_invoice.py

index 3d098bf..606b42e 100644 (file)
@@ -878,6 +878,16 @@ class account_invoice(osv.osv):
             date=context['date_p']
         else:
             date=time.strftime('%Y-%m-%d')
+            
+        # Take the amount in currency and the currency of the payment
+        if 'amount_currency' in context and context['amount_currency'] and 'currency_id' in context and context['currency_id']:
+            amount_currency = context['amount_currency']
+            currency_id = context['currency_id']
+        else:
+            amount_currency = False
+            currency_id = False
+            
+        # Pay attention to the sign for both debit/credit AND amount_currency
         l1 = {
             'debit': direction * pay_amount>0 and direction * pay_amount,
             'credit': direction * pay_amount<0 and - direction * pay_amount,
@@ -885,6 +895,8 @@ class account_invoice(osv.osv):
             'partner_id': invoice.partner_id.id,
             'ref':invoice.number,
             'date': date,
+            'currency_id':currency_id,
+            'amount_currency':amount_currency and direction * amount_currency or 0.0,
         }
         l2 = {
             'debit': direction * pay_amount<0 and - direction * pay_amount,
@@ -893,6 +905,8 @@ class account_invoice(osv.osv):
             'partner_id': invoice.partner_id.id,
             'ref':invoice.number,
             'date': date,
+            'currency_id':currency_id,
+            'amount_currency':amount_currency and - direction * amount_currency or 0.0,
         }
 
         if not name:
index c7781d1..c51fe6b 100644 (file)
@@ -25,6 +25,7 @@ import netsvc
 import pooler
 import time
 from tools.translate import _
+import tools
 
 pay_form = '''<?xml version="1.0"?>
 <form string="Pay invoice">
@@ -37,7 +38,7 @@ pay_form = '''<?xml version="1.0"?>
 </form>'''
 
 pay_fields = {
-    'amount': {'string': 'Amount paid', 'type':'float', 'required':True},
+    'amount': {'string': 'Amount paid', 'type':'float', 'required':True, 'digits': (16,int(tools.config['price_accuracy']))},
     'name': {'string': 'Entry Name', 'type':'char', 'size': 64, 'required':True},
     'date': {'string': 'Payment date', 'type':'date', 'required':True, 'default':lambda *args: time.strftime('%Y-%m-%d')},
     'journal_id': {'string': 'Journal/Payment Mode', 'type': 'many2one', 'relation':'account.journal', 'required':True, 'domain':[('type','=','cash')]},
@@ -57,10 +58,15 @@ def _pay_and_reconcile(self, cr, uid, data, context):
 
     invoice = pool.get('account.invoice').browse(cr, uid, data['id'], context)
     journal = pool.get('account.journal').browse(cr, uid, data['form']['journal_id'], context)
+    # Compute the amount in company's currency, with the journal currency (which is equal to payment currency) 
+    # when it is needed :  If payment currency (according to selected journal.currency) is <> from company currency
     if journal.currency and invoice.company_id.currency_id.id<>journal.currency.id:
         ctx = {'date':data['form']['date']}
         amount = cur_obj.compute(cr, uid, journal.currency.id, invoice.company_id.currency_id.id, amount, context=ctx)
-
+        currency_id = journal.currency.id
+        # Put the paid amount in currency, and the currency, in the context if currency is different from company's currency
+        context.update({'amount_currency':form['amount'],'currency_id':currency_id})
+        
     # Take the choosen date
     if form.has_key('comment'):
         context.update({'date_p':form['date'],'comment':form['comment']})
@@ -79,12 +85,23 @@ def _wo_check(self, cr, uid, data, context):
     pool = pooler.get_pool(cr.dbname)
     invoice = pool.get('account.invoice').browse(cr, uid, data['id'], context)
     journal = pool.get('account.journal').browse(cr, uid, data['form']['journal_id'], context)
-    if invoice.company_id.currency_id.id <> invoice.currency_id.id:
-        return 'addendum'
-    if journal.currency and (journal.currency.id <> invoice.currency_id.id):
-        return 'addendum'
-    if pool.get('res.currency').is_zero(cr, uid, invoice.currency_id,
-            (data['form']['amount'] - invoice.amount_total)):
+    cur_obj = pool.get('res.currency')
+    # Here we need that:
+    #    The invoice total amount in company's currency <> paid amount in company currency
+    #    (according to the correct day rate, invoicing rate and payment rate are may be different)
+    #    => Ask to a write-off of the difference. This could happen even if both amount are equal,
+    #    because if the currency rate
+    # Get the amount in company currency for the invoice (according to move lines)
+    inv_amount_company_currency=invoice.move_id.amount
+    # Get the amount paid in company currency
+    if journal.currency and invoice.company_id.currency_id.id<>journal.currency.id:
+        ctx = {'date':data['form']['date']}
+        amount_paid = cur_obj.compute(cr, uid, journal.currency.id, invoice.company_id.currency_id.id, data['form']['amount'], round=True, context=ctx)
+    else:
+        amount_paid = data['form']['amount']
+    # Test if there is a difference according to currency rouding setting
+    if pool.get('res.currency').is_zero(cr, uid, invoice.company_id.currency_id,
+            (amount_paid - inv_amount_company_currency)):
         return 'reconcile'
     return 'addendum'