[FIX] account_voucher: computed writeoff and invoices selected were not right on...
authorDenis Ledoux <dle@openerp.com>
Tue, 24 Dec 2013 10:35:35 +0000 (11:35 +0100)
committerDenis Ledoux <dle@openerp.com>
Tue, 24 Dec 2013 10:35:35 +0000 (11:35 +0100)
Due to the algorithm which did not handle the fact that an invoice could have several move lines

bzr revid: dle@openerp.com-20131224103535-rhtje7dnq1ie7jtb

addons/account_voucher/account_voucher.py

index ef2b861..e4aa535 100644 (file)
@@ -755,7 +755,7 @@ class account_voucher(osv.osv):
             ids = context['move_line_ids']
         invoice_id = context.get('invoice_id', False)
         company_currency = journal.company_id.currency_id.id
-        move_line_found = False
+        move_lines_found = []
 
         #order the lines by most old first
         ids.reverse()
@@ -770,21 +770,20 @@ class account_voucher(osv.osv):
                 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
+                    move_lines_found.append(line.id)
             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
+                    move_lines_found.append(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
+                    move_lines_found.append(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
@@ -809,15 +808,16 @@ class account_voucher(osv.osv):
                 'move_line_id':line.id,
                 'account_id':line.account_id.id,
                 'amount_original': amount_original,
-                'amount': (move_line_found == line.id) and min(abs(price), amount_unreconciled) or 0.0,
+                'amount': (line.id in move_lines_found) and min(abs(price), amount_unreconciled) or 0.0,
                 'date_original':line.date,
                 'date_due':line.date_maturity,
                 'amount_unreconciled': amount_unreconciled,
                 'currency_id': line_currency_id,
             }
+            price -= rs['amount']
             #in case a corresponding move_line hasn't been found, we now try to assign the voucher amount
             #on existing invoices: we split voucher amount by most old first, but only for lines in the same currency
-            if not move_line_found:
+            if not move_lines_found:
                 if currency_id == line_currency_id:
                     if line.credit:
                         amount = min(amount_unreconciled, abs(total_debit))