Skip to content

Instantly share code, notes, and snippets.

@mrvdb
Created November 3, 2014 14:23
Show Gist options
  • Save mrvdb/c0167fc197fb0c5e1fd9 to your computer and use it in GitHub Desktop.
Save mrvdb/c0167fc197fb0c5e1fd9 to your computer and use it in GitHub Desktop.
diff --git a/addons/account/account_invoice.py b/addons/account/account_invoice.py
index 1e4bc7a..e478106 100644
--- a/addons/account/account_invoice.py
+++ b/addons/account/account_invoice.py
@@ -94,46 +94,43 @@ class account_invoice(osv.osv):
def _get_reference_type(self, cr, uid, context=None):
return [('none', _('Free Reference'))]
+ # An invoice's residual amount is the sum of its unreconciled move lines and,
+ # for partially reconciled move lines, their residual amount divided by the
+ # number of times this reconciliation is used in an invoice (so we split
+ # the residual amount between all invoice lines)
def _amount_residual(self, cr, uid, ids, name, args, context=None):
- """Function of the field residua. It computes the residual amount (balance) for each invoice"""
+ """Function of the field residual. It computes the residual amount (balance) for each invoice"""
if context is None:
context = {}
ctx = context.copy()
result = {}
currency_obj = self.pool.get('res.currency')
for invoice in self.browse(cr, SUPERUSER_ID, ids, context=context):
- nb_inv_in_partial_rec = max_invoice_id = 0
result[invoice.id] = 0.0
if invoice.move_id:
for aml in invoice.move_id.line_id:
if aml.account_id.type in ('receivable','payable'):
+ nb_inv_in_partial_rec = 0
+ line_amount_field = aml.currency_id and aml.amount_residual_currency or aml.amount_residual
+
+ # Get the correct line residual amount
if aml.currency_id and aml.currency_id.id == invoice.currency_id.id:
- result[invoice.id] += aml.amount_residual_currency
+ line_amount = line_amount_field
else:
ctx['date'] = aml.date
- result[invoice.id] += currency_obj.compute(cr, uid, aml.company_id.currency_id.id, invoice.currency_id.id, aml.amount_residual, context=ctx)
+ line_amount = currency_obj.compute(cr, uid, aml.company_id.currency_id.id, invoice.currency_id.id, aml.amount_residual, context=ctx)
+ # For partially reconciled lines, split the residual amount
if aml.reconcile_partial_id.line_partial_ids:
#we check if the invoice is partially reconciled and if there are other invoices
#involved in this partial reconciliation (and we sum these invoices)
for line in aml.reconcile_partial_id.line_partial_ids:
if line.invoice and invoice.type == line.invoice.type:
- nb_inv_in_partial_rec += 1
- #store the max invoice id as for this invoice we will make a balance instead of a simple division
- max_invoice_id = max(max_invoice_id, line.invoice.id)
- if nb_inv_in_partial_rec:
- #if there are several invoices in a partial reconciliation, we split the residual by the number
- #of invoice to have a sum of residual amounts that matches the partner balance
- new_value = currency_obj.round(cr, uid, invoice.currency_id, result[invoice.id] / nb_inv_in_partial_rec)
- if invoice.id == max_invoice_id:
- #if it's the last the invoice of the bunch of invoices partially reconciled together, we make a
- #balance to avoid rounding errors
- result[invoice.id] = result[invoice.id] - ((nb_inv_in_partial_rec - 1) * new_value)
- else:
- result[invoice.id] = new_value
-
- #prevent the residual amount on the invoice to be less than 0
- result[invoice.id] = max(result[invoice.id], 0.0)
+ nb_inv_in_partial_rec +=1
+ line_amount = currency_obj.round(cr, uid, invoice.currency_id, line_amount / nb_inv_in_partial_rec)
+ result[invoice.id] += line_amount
+ # prevent the residual amount on the invoice to be less than 0
+ result[invoice.id] = max(result[invoice.id], 0.0)
return result
# Give Journal Items related to the payment reconciled to this invoice
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment