[FIX] account: don't recompute ending balance of last statement
Previously a task was created to prevent holes between bank statement, meaning that starting balance should always be the same as previous ending balance. The goal was that during synchronization or imports, we could add statement lines in previous statement and not have to recompute manually the full chain of starting and ending balance of next statements entries. This works fine with online synchronization as we always write after creating the statements the balance of the account on the latest statements to detect missing stmts, however if we create stmts by hand, it is possible that we have the following case: Statement on 10 January with a starting and ending balance of 100 and 200 respectively. We are missing 2 statements with a line of 50 each before that date. So we create the first missing statement at date: 8 January with a starting balance 0 and ending balance 50, this will recompute the starting and ending balance of next statement and overwrite the values to 50 and 150 (starting and ending balance respectively). At that time the system will say that everything is normal but we are still missing a stmt and we are not seeing it. The way to fix this is by not touching the ending balance of the last stmt entry during the computation, that way we can still detect that there is an error and that 50€ are still missing. closes odoo/odoo#48000 X-original-commit: b97b4f4217989e8cae5c32be0d62c11d36eaf1d7 Signed-off-by: Cedric Snauwaert (csn) <csn@openerp.com>
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user