[FIX]:When we make a payment on a Customer refund, the field amount stay to zero...
authorksa (OpenERP) <ksa@tinyerp.com>
Tue, 20 Mar 2012 07:20:19 +0000 (12:50 +0530)
committerksa (OpenERP) <ksa@tinyerp.com>
Tue, 20 Mar 2012 07:20:19 +0000 (12:50 +0530)
bzr revid: ksa@tinyerp.com-20120320072019-zn3hxbjun98sy047

addons/account_voucher/account_voucher.py

index 2e2e613..f382641 100644 (file)
@@ -474,7 +474,8 @@ class account_voucher(osv.osv):
             ids = context['move_line_ids']
         ids.reverse()
         moves = move_line_pool.browse(cr, uid, ids, context=context)
-
+        move_line_found = False
+        invoice_id = context.get('invoice_id', False)
         company_currency = journal.company_id.currency_id.id
         if company_currency != currency_id and ttype == 'payment':
             total_debit = currency_pool.compute(cr, uid, currency_id, company_currency, total_debit, context=context_multi_currency)
@@ -486,8 +487,28 @@ class account_voucher(osv.osv):
                 continue
             if line.debit and line.reconcile_partial_id and ttype == 'payment':
                 continue
-            total_credit += line.credit or 0.0
-            total_debit += line.debit or 0.0
+            if invoice_id:
+                if line.invoice.id == invoice_id:
+                    #if the invoice linked to the voucher line is equal to the invoice_id in context
+                    #then we assign the amount on that line, whatever the other voucher lines
+                    move_line_found = line.id
+                    break
+            elif currency_id == company_currency:
+                #otherwise treatments is the same but with other field names
+                if line.amount_residual == price:
+                    #if the amount residual is equal the amount voucher, we assign it to that voucher
+                    #line, whatever the other voucher lines
+                    move_line_found = line.id
+                    break
+                #otherwise we will split the voucher amount on each line (by most old first)
+                total_credit += line.credit or 0.0
+                total_debit += line.debit or 0.0
+            elif currency_id == line.currency_id.id:
+                if line.amount_residual_currency == price:
+                    move_line_found = line.id
+                    break
+                total_credit += line.credit and line.amount_currency or 0.0
+                total_debit += line.debit and line.amount_currency or 0.0
         for line in moves:
             if line.credit and line.reconcile_partial_id and ttype == 'receipt':
                 continue
@@ -495,25 +516,29 @@ class account_voucher(osv.osv):
                 continue
             original_amount = line.credit or line.debit or 0.0
             amount_unreconciled = currency_pool.compute(cr, uid, line.currency_id and line.currency_id.id or company_currency, currency_id, abs(line.amount_residual_currency), context=context_multi_currency)
+            line_currency_id = line.currency_id and line.currency_id.id or company_currency
             rs = {
                 'name':line.move_id.name,
                 'type': line.credit and 'dr' or 'cr',
                 'move_line_id':line.id,
                 'account_id':line.account_id.id,
+                'amount': (move_line_found == line.id) and min(price, amount_unreconciled) or 0.0,
                 'amount_original': currency_pool.compute(cr, uid, line.currency_id and line.currency_id.id or company_currency, currency_id, line.currency_id and abs(line.amount_currency) or original_amount, context=context_multi_currency),
                 'date_original':line.date,
                 'date_due':line.date_maturity,
                 'amount_unreconciled': amount_unreconciled,
+                'currency_id': line_currency_id,
             }
-
-            if line.credit:
-                amount = min(amount_unreconciled, currency_pool.compute(cr, uid, company_currency, currency_id, abs(total_debit), context=context_multi_currency))
-                rs['amount'] = amount
-                total_debit -= amount
-            else:
-                amount = min(amount_unreconciled, currency_pool.compute(cr, uid, company_currency, currency_id, abs(total_credit), context=context_multi_currency))
-                rs['amount'] = amount
-                total_credit -= amount
+            if not move_line_found:
+                if currency_id == line_currency_id:
+                    if line.credit:
+                        amount = min(amount_unreconciled, abs(total_debit))
+                        rs['amount'] = amount
+                        total_debit -= amount
+                    else:
+                        amount = min(amount_unreconciled, abs(total_credit))
+                        rs['amount'] = amount
+                        total_credit -= amount
 
             default['value']['line_ids'].append(rs)
             if rs['type'] == 'cr':