diff --git a/addons/account/report/account_aged_partner_balance.py b/addons/account/report/account_aged_partner_balance.py index b7bc1c32586..cb77e0f578c 100644 --- a/addons/account/report/account_aged_partner_balance.py +++ b/addons/account/report/account_aged_partner_balance.py @@ -178,9 +178,20 @@ class aged_trial_report(report_sxw.rml_parse, common_report_header): partners_amount = dict((i[0],0) for i in partners_partial) for partner_info in partners_partial: if partner_info[2]: - # in case of partial reconciliation, we want to keep the left amount in the oldest period - self.cr.execute('''SELECT MIN(COALESCE(date_maturity,date)) FROM account_move_line WHERE reconcile_partial_id = %s''', (partner_info[2],)) + # in case of partial reconciliation, we want to keep the remaining amount in the + # period corresponding to the maturity date of the invoice. + self.cr.execute(''' + SELECT MAX(COALESCE(l.date_maturity, l.date)) + FROM account_move_line AS l + JOIN account_account AS a ON l.account_id = a.id + WHERE reconcile_partial_id = %s + AND a.type IN %s + ''', (partner_info[2], tuple(self.ACCOUNT_TYPE),)) date = self.cr.fetchall() + # Just in case date is not defined (but it should be defined) + if date and not date[0][0]: + self.cr.execute('''SELECT MIN(COALESCE(date_maturity,date)) FROM account_move_line WHERE reconcile_partial_id = %s''', (partner_info[2],)) + date = self.cr.fetchall() partial = False if 'BETWEEN' in dates_query: partial = date and args_list[-3] <= date[0][0] <= args_list[-2]