[FIX] Residual amount into invoice is now correct in every case and avoid rounding...
authorJoel Grand-Guillaume <joel.grandguillaume@camptocamp.com>
Mon, 2 Nov 2009 15:53:05 +0000 (16:53 +0100)
committerJoel Grand-Guillaume <joel.grandguillaume@camptocamp.com>
Mon, 2 Nov 2009 15:53:05 +0000 (16:53 +0100)
bzr revid: joel.grandguillaume@camptocamp.com-20091102155305-swj8zxhsey8wjhue

addons/account/invoice.py

index 606b42e..a4cede5 100644 (file)
@@ -104,22 +104,34 @@ class account_invoice(osv.osv):
         res = {}
         data_inv = self.browse(cr, uid, ids)
         cur_obj = self.pool.get('res.currency')
+        import pdb
+        pdb.set_trace()
         for inv in data_inv:
             debit = credit = 0.0
             context.update({'date':inv.date_invoice})
+            context_unreconciled=context.copy()
             for lines in inv.move_lines:
+                debit_tmp = lines.debit
+                credit_tmp = lines.credit
                 # If currency conversion needed
                 if inv.company_id.currency_id.id <> inv.currency_id.id:
                     # If invoice paid, compute currency amount according to invoice date
                     # otherwise, take the line date
                     if not inv.reconciled:
                         context.update({'date':lines.date})
-                    # Compute amount in currency
-                    debit += cur_obj.compute(cr, uid, inv.company_id.currency_id.id, inv.currency_id.id, lines.debit, round=False,context=context)
-                    credit += cur_obj.compute(cr, uid, inv.company_id.currency_id.id, inv.currency_id.id, lines.credit, round=False,context=context)
+                    context_unreconciled.update({'date':lines.date})
+                    # If amount currency setted, compute for debit and credit in company currency
+                    if lines.amount_currency < 0:
+                        credit_tmp=abs(cur_obj.compute(cr, uid, lines.currency_id.id, inv.company_id.currency_id.id, lines.amount_currency, round=False,context=context_unreconciled))
+                    elif lines.amount_currency > 0:
+                        debit_tmp=abs(cur_obj.compute(cr, uid, lines.currency_id.id, inv.company_id.currency_id.id, lines.amount_currency, round=False,context=context_unreconciled))
+                    # Then, recomput into invoice currency to avoid rounding trouble !
+                    debit += cur_obj.compute(cr, uid, inv.company_id.currency_id.id, inv.currency_id.id, debit_tmp, round=False,context=context)
+                    credit += cur_obj.compute(cr, uid, inv.company_id.currency_id.id, inv.currency_id.id, credit_tmp, round=False,context=context)
                 else:
-                    debit += lines.debit
-                    credit += lines.credit
+                    debit+=debit_tmp
+                    credit+=credit_tmp
+                    
             if not inv.amount_total:
                 result = 0.0
             elif inv.type in ('out_invoice','in_refund'):
@@ -128,7 +140,9 @@ class account_invoice(osv.osv):
             else:
                 amount = debit-credit
                 result = inv.amount_total - amount
-            res[inv.id] = result
+            # Use is_zero function to avoid rounding trouble => should be fixed into ORM
+            res[inv.id] = not self.pool.get('res.currency').is_zero(cr, uid, inv.company_id.currency_id,result) and result or 0.0
+            
         return res
 
     def _get_lines(self, cr, uid, ids, name, arg, context=None):