diff --git a/addons/account/models/account_bank_statement.py b/addons/account/models/account_bank_statement.py index dc32b00894b..9e6bb38e292 100644 --- a/addons/account/models/account_bank_statement.py +++ b/addons/account/models/account_bank_statement.py @@ -127,6 +127,7 @@ class AccountBankStatement(models.Model): @api.depends('previous_statement_id', 'previous_statement_id.balance_end_real') def _compute_ending_balance(self): + latest_statement = self.env['account.bank.statement'].search([('journal_id', '=', self[0].journal_id.id)], limit=1) for statement in self: # recompute balance_end_real in case we are in a bank journal and if we change the # balance_end_real of previous statement as we don't want @@ -135,8 +136,13 @@ class AccountBankStatement(models.Model): # journal for verification and creating cash difference entries so we don't want # to recompute the value in that case if statement.journal_type == 'bank': - total_entry_encoding = sum([line.amount for line in statement.line_ids]) - statement.balance_end_real = statement.previous_statement_id.balance_end_real + total_entry_encoding + # If we are on last statement and that statement already has a balance_end_real, don't change the balance_end_real + # Otherwise, recompute balance_end_real to prevent holes between statement. + if latest_statement.id and statement.id == latest_statement.id and not float_is_zero(statement.balance_end_real, precision_digits=statement.currency_id.decimal_places): + statement.balance_end_real = statement.balance_end_real or 0.0 + else: + total_entry_encoding = sum([line.amount for line in statement.line_ids]) + statement.balance_end_real = statement.previous_statement_id.balance_end_real + total_entry_encoding else: # Need default value statement.balance_end_real = statement.balance_end_real or 0.0 diff --git a/addons/account/tests/test_account_bank_statement.py b/addons/account/tests/test_account_bank_statement.py index 3ac0030d2cc..6e017e3340e 100644 --- a/addons/account/tests/test_account_bank_statement.py +++ b/addons/account/tests/test_account_bank_statement.py @@ -106,10 +106,12 @@ class TestBankStatement(AccountTestCommon): # and since we are propagating an update, the balance_end_real should have been recomputed to # the correct value self.assertEqual(bnk3.balance_start, 250) - self.assertEqual(bnk3.balance_end_real, 275) + self.assertEqual(bnk3.balance_end_real, 200) # This should not have been recomputed as this is the last of the chain self.assertEqual(bnk3.balance_end, 275) + bnk3.balance_end_real = 275 # Correct ending balance # Change date of bank stmt4 to be the last + self.assertEqual(bnk4.balance_end_real, 200) #Check balance before moving it to the last bnk4.date = '2019-01-20' self.assertEqual(bnk1.previous_statement_id.id, False) self.assertEqual(bnk2.previous_statement_id.id, bnk1.id) @@ -122,7 +124,8 @@ class TestBankStatement(AccountTestCommon): self.assertEqual(bnk1.balance_end_real, 100) self.assertEqual(bnk2.balance_end_real, 150) self.assertEqual(bnk3.balance_end_real, 175) - self.assertEqual(bnk4.balance_end_real, 275) + self.assertEqual(bnk4.balance_end_real, 200) # This should not have change + bnk4.balance_end_real = 275 # Correct ending balance # Move bnk3 to first position bnk3.date = '2019-01-01' @@ -210,6 +213,10 @@ class TestBankStatement(AccountTestCommon): self.assertEqual(bnk3.previous_statement_id.id, bnk2.id) self.assertEqual(bnk4.previous_statement_id.id, bnk3.id) self.assertEqual(bnk5.previous_statement_id.id, bnk4.id) + self.assertEqual(bnk1.balance_start, 0) + self.assertEqual(bnk1.balance_end_real, 100) + self.assertEqual(bnk2.balance_start, 100) + self.assertEqual(bnk2.balance_end_real, 150) self.assertEqual(bnk3.balance_start, 150) self.assertEqual(bnk3.balance_end_real, 175) self.assertEqual(bnk4.balance_start, 175) @@ -228,10 +235,10 @@ class TestBankStatement(AccountTestCommon): self.assertEqual(bnk4.balance_start, 125) self.assertEqual(bnk4.balance_end_real, 225) self.assertEqual(bnk5.balance_start, 225) - self.assertEqual(bnk5.balance_end_real, 325) + self.assertEqual(bnk5.balance_end_real, 375) # Ending balance of last statement should not have changed # Delete bnk1 bnk3 and bnk4 at the same time and check that balance are correct (bnk1 + bnk3 + bnk4).unlink() self.assertEqual(bnk5.previous_statement_id.id, False) self.assertEqual(bnk5.balance_start, 0) - self.assertEqual(bnk5.balance_end_real, 100) + self.assertEqual(bnk5.balance_end_real, 375) # Ending balance of last statement should not have changed