diff --git a/addons/account/models/account_payment.py b/addons/account/models/account_payment.py index 0a3668467d5..4e9656febc2 100644 --- a/addons/account/models/account_payment.py +++ b/addons/account/models/account_payment.py @@ -182,20 +182,26 @@ class AccountPayment(models.Model): ''' self.ensure_one() - liquidity_lines = self.env['account.move.line'] - counterpart_lines = self.env['account.move.line'] - writeoff_lines = self.env['account.move.line'] - + # liquidity_lines, counterpart_lines, writeoff_lines + lines = [self.env['account.move.line'] for _dummy in range(3)] valid_account_types = self._get_valid_payment_account_types() for line in self.move_id.line_ids: if line.account_id in self._get_valid_liquidity_accounts(): - liquidity_lines += line - elif line.account_id.account_type in valid_account_types or line.account_id == self.company_id.transfer_account_id: - counterpart_lines += line + lines[0] += line # liquidity_lines + elif line.account_id.account_type in valid_account_types or line.account_id == line.company_id.transfer_account_id: + lines[1] += line # counterpart_lines else: - writeoff_lines += line + lines[2] += line # writeoff_lines - return liquidity_lines, counterpart_lines, writeoff_lines + # In some case, there is no liquidity or counterpart line (after changing an outstanding account on the journal for example) + # In that case, and if there is one writeoff line, we take this line and set it as liquidity/counterpart line + if len(lines[2]) == 1: + for i in (0, 1): + if not lines[i]: + lines[i] = lines[2] + lines[2] -= lines[2] + + return lines def _get_valid_liquidity_accounts(self): return ( diff --git a/addons/account/tests/test_account_payment.py b/addons/account/tests/test_account_payment.py index ea63e59b4a4..6d93452d5ac 100644 --- a/addons/account/tests/test_account_payment.py +++ b/addons/account/tests/test_account_payment.py @@ -1002,3 +1002,32 @@ class TestAccountPayment(AccountTestInvoicingCommon): {'account_id': bank_2.inbound_payment_method_line_ids.payment_account_id.id}, {'account_id': transfer_account.id}, ]) + + def test_reconciliation_with_old_oustanding_account(self): + """ + Test the reconciliation of an invoice with a payment after changing the outstanding account of the journal. + """ + outstanding_account_1 = self.company_data['company'].account_journal_payment_debit_account_id.copy() + outstanding_account_2 = outstanding_account_1.copy() + + self.company_data['default_journal_bank'].inbound_payment_method_line_ids.payment_account_id = outstanding_account_1 + + payment = self.env['account.payment'].create({ + 'payment_type': 'inbound', + 'partner_type': 'customer', + 'partner_id': self.partner_a.id, + 'journal_id': self.company_data['default_journal_bank'].id, + 'amount': 1150, + }) + payment.action_post() + + self.company_data['default_journal_bank'].inbound_payment_method_line_ids.payment_account_id = outstanding_account_2 + invoice = self.init_invoice('out_invoice', post=True, amounts=[1000.0], taxes=self.env['account.tax']) + + credit_line = payment.line_ids.filtered(lambda l: l.credit and l.account_id == self.company_data['default_account_receivable']) + + invoice.js_assign_outstanding_line(credit_line.id) + self.assertTrue(invoice.payment_state in ('in_payment', 'paid'), "Invoice should be paid") + invoice.button_draft() + self.assertTrue(invoice.payment_state == 'not_paid', "Invoice should'nt be paid anymore") + self.assertTrue(invoice.state == 'draft', "Invoice should be draft")