From 6c35ed8346a3471a17196fdca92524bd0818130f Mon Sep 17 00:00:00 2001 From: "Guillaume (guva)" Date: Mon, 18 Oct 2021 15:58:38 +0000 Subject: [PATCH] [FIX] account: Fix unbalanced journal entry when editing payment Steps to reproduce: - On bank journal set the outstanding payments accounts and the outstanding receipts accounts, respectively to 101403 and 101402 - In Accounting > Configuration > Settings > Default Accounts, let the two outstanding accounts empty. - Customer > payment > create a payment : payment type "Receive", amount 5 for example. Save it. - Edit the payment and change the payment type to "Send". - Click on Save. Issue Got an UserError "Cannot create unbalanced journal entry" Since the payment_type has changed, the computed outstanding_account_id on the payment is recomputed. When splitting journal items using the _seek_for_lines() method, odoo is not able to retrieve the liquidity lines since outstanding_account_id has changed. opw-2620046 closes odoo/odoo#78866 X-original-commit: 865c5d7741ca1bf003b4181d06484cc4ecaf8fcb Signed-off-by: Laurent Smet Signed-off-by: guva-odoo --- addons/account/models/account_payment.py | 2 + addons/account/tests/test_account_payment.py | 44 ++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/addons/account/models/account_payment.py b/addons/account/models/account_payment.py index 8d0f828bc0d..ef955af8e52 100644 --- a/addons/account/models/account_payment.py +++ b/addons/account/models/account_payment.py @@ -194,6 +194,8 @@ class AccountPayment(models.Model): self.payment_method_line_id.payment_account_id, self.journal_id.company_id.account_journal_payment_debit_account_id, self.journal_id.company_id.account_journal_payment_credit_account_id, + self.journal_id.inbound_payment_method_line_ids.payment_account_id, + self.journal_id.outbound_payment_method_line_ids.payment_account_id, ) def _prepare_move_line_default_vals(self, write_off_line_vals=None): diff --git a/addons/account/tests/test_account_payment.py b/addons/account/tests/test_account_payment.py index 1e0f09543c0..772ed6dd159 100644 --- a/addons/account/tests/test_account_payment.py +++ b/addons/account/tests/test_account_payment.py @@ -769,3 +769,47 @@ class TestAccountPayment(AccountTestInvoicingCommon): self.assertEqual(payment.name, '/') payment.action_post() self.assertRegex(payment.name, r'BNK1/\d{4}/\d{2}/0002') + + def test_payment_without_default_company_account(self): + """ The purpose of this test is to check the specific behavior when duplicating an inbound payment, then change + the copy to an outbound payment when we set the outstanding accounts (payments and receipts) on a journal but + not on the company level. + """ + company = self.company_data['company'] + bank_journal = self.company_data['default_journal_bank'] + + bank_journal.outbound_payment_method_line_ids.payment_account_id = company.account_journal_payment_credit_account_id + bank_journal.inbound_payment_method_line_ids.payment_account_id = company.account_journal_payment_debit_account_id + company.account_journal_payment_debit_account_id = False + company.account_journal_payment_credit_account_id = False + + payment = self.env['account.payment'].create({ + 'amount': 5.0, + 'payment_type': 'inbound', + 'partner_type': 'customer', + 'journal_id': bank_journal.id, + }) + self.assertRecordValues(payment, [{ + 'amount': 5.0, + 'payment_type': 'inbound', + 'partner_type': 'customer', + 'payment_reference': False, + 'is_reconciled': False, + 'currency_id': self.company_data['currency'].id, + 'partner_id': False, + 'payment_method_line_id': self.inbound_payment_method_line.id, + 'partner_bank_id': False, + }]) + + payment.payment_type = 'outbound' + self.assertRecordValues(payment, [{ + 'amount': 5.0, + 'payment_type': 'outbound', + 'partner_type': 'customer', + 'payment_reference': False, + 'is_reconciled': False, + 'currency_id': self.company_data['currency'].id, + 'partner_id': False, + 'payment_method_line_id': self.outbound_payment_method_line.id, + 'partner_bank_id': False, + }])