[FIX] Pay Wizard and reconcilation on amount currency and multi-currency
authorStephane Wirtel <stephane@openerp.com>
Thu, 16 Dec 2010 16:06:42 +0000 (17:06 +0100)
committerStephane Wirtel <stephane@openerp.com>
Thu, 16 Dec 2010 16:06:42 +0000 (17:06 +0100)
lp bug: https://launchpad.net/bugs/511104 fixed

bzr revid: stephane@openerp.com-20101216160642-a0ezshr9ublccfio

addons/account/account_move_line.py
addons/account/invoice.py

index 7bfa536..9bcd71e 100644 (file)
@@ -606,15 +606,20 @@ class account_move_line(osv.osv):
             if 'comment' in context and context['comment']:
                 libelle=context['comment']
             else:
-                libelle='Write-Off'
+                libelle= _('Write-Off')
 
             cur_obj = self.pool.get('res.currency')
             cur_id = False
-            amount_cur = 0.00
-
+            amount_currency_writeoff = 0.0
             if context.get('company_currency_id',False) != context.get('currency_id',False):
-                amount_cur = cur_obj.compute(cr, uid, context.get('company_currency_id',False), context.get('currency_id',False), abs(writeoff), context=context)
                 cur_id = context.get('currency_id',False)
+                for line in unrec_lines:
+                    #tmp_amount = cur_obj.compute(cr, uid, invoice.company_id.currency_id.id, invoice.currency_id.id, abs(line.debit-line.credit), context={'date': line.date})
+                    if line.currency_id and line.currency_id.id == context.get('currency_id',False):
+                        amount_currency_writeoff += line.amount_currency
+                    else:
+                        tmp_amount = cur_obj.compute(cr, uid, line.account_id.company_id.currency_id.id, context.get('currency_id',False), abs(line.debit-line.credit), context={'date': line.date})
+                        amount_currency_writeoff += (line.debit > 0) and tmp_amount or -tmp_amount
 
             writeoff_lines = [
                 (0, 0, {
@@ -625,7 +630,7 @@ class account_move_line(osv.osv):
                     'date':date,
                     'partner_id':partner_id,
                     'currency_id': cur_id or (account.currency_id.id or False),
-                    'amount_currency': amount_cur or (account.currency_id.id and -currency or 0.0)
+                    'amount_currency': amount_currency_writeoff and -1 * amount_currency_writeoff or (account.currency_id.id and -1 * currency or 0.0)
                 }),
                 (0, 0, {
                     'name':libelle,
@@ -636,10 +641,9 @@ class account_move_line(osv.osv):
                     'date':date,
                     'partner_id':partner_id,
                     'currency_id': cur_id or (account.currency_id.id or False),
-                    'amount_currency': amount_cur or (account.currency_id.id and -currency or 0.0)
+                    'amount_currency': amount_currency_writeoff and amount_currency_writeoff or (account.currency_id.id and currency or 0.0)
                 })
             ]
-
             writeoff_move_id = self.pool.get('account.move').create(cr, uid, {
                 'period_id': writeoff_period_id,
                 'journal_id': writeoff_journal_id,
@@ -650,17 +654,6 @@ class account_move_line(osv.osv):
             writeoff_line_ids = self.search(cr, uid, [('move_id', '=', writeoff_move_id), ('account_id', '=', account_id)])
             if account_id == writeoff_acc_id:
                 writeoff_line_ids = [writeoff_line_ids[1]]
-
-            if invoice:
-                if (invoice.type == 'out_invoice') or (invoice.type == 'in_invoice'):
-                    condition_1 = (account_type == 'payable' and writeoff > 0.0)
-                    condition_2 = (account_type == 'receivable' and writeoff < 0.0)
-                else:
-                    condition_1 = (account_type == 'payable' and writeoff < 0.0)
-                    condition_2 = (account_type == 'receivable' and writeoff > 0.0)
-                if condition_1 or condition_2:
-                    writeoff_line_ids = self.search(cr, uid, [('move_id', '=', writeoff_move_id), ('account_id', 'in', (account_id,writeoff_acc_id))])
-                    self.pool.get('account.move.line').write(cr ,uid, writeoff_line_ids, {'name':_('Currency Profit/Loss')})
             ids += writeoff_line_ids
 
         r_id = self.pool.get('account.move.reconcile').create(cr, uid, {
index be08078..e4a6f81 100644 (file)
@@ -108,40 +108,23 @@ class account_invoice(osv.osv):
         data_inv = self.browse(cr, uid, ids)
         cur_obj = self.pool.get('res.currency')
         for inv in data_inv:
-            debit = credit = 0.0
+            if inv.reconciled: 
+                res[inv.id] = 0.0
+                continue
+            inv_total = inv.amount_total
             context_unreconciled = context.copy()
-            # If one of the invoice line is not in the currency of the invoice,
-            # we use the currency of the company to compute the residual amount.
-            # All the lines must use the same currency source (company or invoice)
-            fromcompany = False
-            
-            for lines in inv.move_lines:
-                if lines.currency_id and lines.currency_id.id <> inv.currency_id.id:
-                    fromcompany = True
-                    break
             for lines in inv.move_lines:
-                # If currency conversion needed
-                if fromcompany:
-                    context_unreconciled.update({'date':lines.date})
-                    # If amount currency setted, compute for debit and credit in company currency
-                    if lines.credit:
-                        credit += abs(cur_obj.compute(cr, uid, inv.company_id.currency_id.id, inv.currency_id.id, lines.credit, round=False,context=context_unreconciled))
-                    else:
-                        debit += abs(cur_obj.compute(cr, uid, inv.company_id.currency_id.id, inv.currency_id.id, lines.debit, round=False,context=context_unreconciled))
+                if lines.currency_id and lines.currency_id.id == inv.currency_id.id:
+                   if inv.type in ('out_invoice','in_refund'):
+                        inv_total += lines.amount_currency
+                   else:
+                        inv_total -= lines.amount_currency
                 else:
-                    if lines.amount_currency:
-                        debit += lines.debit > 0 and abs(lines.amount_currency) or 0.00
-                        credit += lines.credit > 0 and abs(lines.amount_currency) or 0.00
-                    else:
-                        debit += lines.debit or 0.00
-                        credit += lines.credit or 0.00
-            
-            if inv.type in ('out_invoice','in_refund'):
-                amount = credit-debit
-            else:
-                amount = debit-credit
-            
-            result = inv.amount_total - amount
+                   context_unreconciled.update({'date': lines.date})
+                   amount_in_invoice_currency = cur_obj.compute(cr, uid, inv.company_id.currency_id.id, inv.currency_id.id,abs(lines.debit-lines.credit),round=False,context=context_unreconciled)
+                   inv_total -= amount_in_invoice_currency
+
+            result = inv_total 
             res[inv.id] =  self.pool.get('res.currency').round(cr, uid, inv.currency_id, result)
         return res