From ff6f8b29e3e178fa9d84461f9a0a554a21f76365 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nshimiyimana=20S=C3=A9na?= Date: Tue, 16 Aug 2022 12:05:46 +0000 Subject: [PATCH] [FIX] account: ignore non-posted invoices when computing an account's balance MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently, computing an account's current balance takes canceled and draft invoices into account. Only posted invoices should be considered. opw-2896728 closes odoo/odoo#98843 X-original-commit: d3a7115feab9ca1bd8372f4450d3abc3eb52cdb0 Signed-off-by: Grazioso Andrea (agr) Signed-off-by: Nshimiyimana Serge Séna (sesn) --- addons/account/models/account_account.py | 2 +- addons/account/tests/test_account_account.py | 42 ++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/addons/account/models/account_account.py b/addons/account/models/account_account.py index 0c884506ab7..b9a9e2354a8 100644 --- a/addons/account/models/account_account.py +++ b/addons/account/models/account_account.py @@ -331,7 +331,7 @@ class AccountAccount(models.Model): balances = { read['account_id'][0]: read['balance'] for read in self.env['account.move.line']._read_group( - domain=[('account_id', 'in', self.ids)], + domain=[('account_id', 'in', self.ids), ('parent_state', '=', 'posted')], fields=['balance', 'account_id'], groupby=['account_id'], ) diff --git a/addons/account/tests/test_account_account.py b/addons/account/tests/test_account_account.py index 665c67d4e2e..2ec0976fffc 100644 --- a/addons/account/tests/test_account_account.py +++ b/addons/account/tests/test_account_account.py @@ -186,3 +186,45 @@ class TestAccountAccount(AccountTestInvoicingCommon): prefix=alternate_account.code[:-1]) new_account.code = alternate_code self.assertEqual(new_account.account_type, existing_account.account_type) + + def test_compute_current_balance(self): + """ Test if an account's current_balance is computed correctly """ + + account_payable = self.company_data['default_account_payable'] + account_receivable = self.company_data['default_account_receivable'] + + payable_debit_move = { + 'line_ids': [ + (0, 0, {'name': 'debit', 'account_id': account_payable.id, 'debit': 100.0, 'credit': 0.0}), + (0, 0, {'name': 'credit', 'account_id': account_receivable.id, 'debit': 0.0, 'credit': 100.0}), + ], + } + payable_credit_move = { + 'line_ids': [ + (0, 0, {'name': 'credit', 'account_id': account_payable.id, 'debit': 0.0, 'credit': 100.0}), + (0, 0, {'name': 'debit', 'account_id': account_receivable.id, 'debit': 100.0, 'credit': 0.0}), + ], + } + + self.assertEqual(account_payable.current_balance, 0) + + self.env['account.move'].create(payable_debit_move).action_post() + account_payable._compute_current_balance() + self.assertEqual(account_payable.current_balance, 100) + + self.env['account.move'].create(payable_credit_move).action_post() + account_payable._compute_current_balance() + self.assertEqual(account_payable.current_balance, 0) + + self.env['account.move'].create(payable_credit_move).action_post() + account_payable._compute_current_balance() + self.assertEqual(account_payable.current_balance, -100) + + self.env['account.move'].create(payable_credit_move).button_cancel() + account_payable._compute_current_balance() + self.assertEqual(account_payable.current_balance, -100, 'Canceled invoices/bills should not be used when computing the balance') + + # draft invoice + self.env['account.move'].create(payable_credit_move) + account_payable._compute_current_balance() + self.assertEqual(account_payable.current_balance, -100, 'Draft invoices/bills should not be used when computing the balance')