+ for aml in invoice.move_id.line_id:
+ if aml.account_id.type in ('receivable','payable'):
+ if aml.currency_id and aml.currency_id.id == invoice.currency_id.id:
+ result[invoice.id] += aml.amount_residual_currency
+ 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)
+
+ 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)