[FIX] account: ignore non-posted invoices when computing an account's balance

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) <agr@odoo.com>
Signed-off-by: Nshimiyimana Serge Séna (sesn) <sesn@odoo.com>
This commit is contained in:
Nshimiyimana Séna
2022-08-25 04:21:31 +02:00
parent f8c2689f3b
commit ff6f8b29e3
2 changed files with 43 additions and 1 deletions
+1 -1
View File
@@ -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'],
)
@@ -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')