[FIX] account: tax lock date: avoid posting account moves that would impact the tax report before the tax lock date

The previous version of this verification was buggy and inadequate. It was for example impossible to post an account.move before the tax lock date, even if it did not impact the tax report in any way.

closes odoo/odoo#36289

Signed-off-by: Laurent Smet <smetl@users.noreply.github.com>
This commit is contained in:
Olivier Colson
2019-09-06 11:00:17 +00:00
committed by oco-odoo
parent d4fab012f6
commit e270efff2e
2 changed files with 47 additions and 72 deletions
+7 -17
View File
@@ -361,6 +361,7 @@ class AccountMove(models.Model):
@api.multi
def button_cancel(self):
self._check_tax_lock_date()
AccountMoveLine = self.env['account.move.line']
excluded_move_ids = []
@@ -398,8 +399,14 @@ class AccountMove(models.Model):
if not all([x.company_id.id == move.company_id.id for x in move.line_ids]):
raise UserError(_("Cannot create moves for different companies."))
self.assert_balanced()
self._check_tax_lock_date()
return self._check_lock_date()
def _check_tax_lock_date(self):
for move in self:
if move.company_id.tax_lock_date and move.date <= move.company_id.tax_lock_date and any(line.tax_ids or line.tax_line_id or line.tag_ids.filtered(lambda x: x.applicability == 'taxes') for line in move.line_ids):
raise UserError(_("The operation is refused as it would impact an already issued tax statement. Please change the journal entry date or the tax lock date set in the settings ({}) to proceed").format(move.company_id.tax_lock_date or date.min))
@api.multi
def _check_lock_date(self):
for move in self:
@@ -739,22 +746,6 @@ class AccountMoveLine(models.Model):
return rec
@api.multi
@api.constrains('tax_ids', 'tax_line_id')
def _check_tax_lock_date1(self):
for line in self:
if line.date <= (line.company_id.tax_lock_date or date.min):
raise ValidationError(_("The operation is refused as it would impact an already issued tax statement. " +
"Please change the journal entry date or the tax lock date set in the settings ({}) to proceed").format(line.company_id.tax_lock_date or date.min))
@api.multi
@api.constrains('credit', 'debit', 'date')
def _check_tax_lock_date2(self):
for line in self:
if (line.tax_ids or line.tax_line_id) and line.date <= (line.company_id.tax_lock_date or date.min):
raise ValidationError(_("The operation is refused as it would impact an already issued tax statement. " +
"Please change the journal entry date or the tax lock date set in the settings ({}) to proceed").format(line.company_id.tax_lock_date or date.min))
@api.multi
@api.constrains('currency_id', 'account_id')
def _check_currency(self):
@@ -1291,7 +1282,6 @@ class AccountMoveLine(models.Model):
@api.multi
def unlink(self):
self._update_check()
self._check_tax_lock_date2()
move_ids = set()
for line in self:
if line.move_id.id not in move_ids:
@@ -50,67 +50,52 @@ class TestTaxBlockDate(AccountingTestCase):
})]
}
def test_create_before_block_date(self):
"""
Checks that you cannot create an account.move with a date before the tax
lock date
self.move_no_tax = {
'name': '/',
'journal_id': self.sale_journal_id.id,
'date': self.middle_day_month_str,
'line_ids': [(0, 0, {
'name': 'foo',
'debit': 10,
'account_id': self.account_id.id,
}), (0, 0, {
'name': 'bar',
'credit': 10,
'account_id': self.account_id.id,
})]
}
def test_tax_lock_date_post(self):
""" Checks that it is impossible to post an entry
before the tax lock date.
"""
# Posting after the lock date is always allowed
self.user_id.company_id.tax_lock_date = self.first_day_month_str
move_after_lock = self.env['account.move'].create(self.move.copy()) # copy() because mail.thread modifies the dictionary given to create
move_after_lock.post()
# Posting before the tax lock date is allowed only if the move doesn't contain any tax
self.user_id.company_id.tax_lock_date = self.last_day_month_str
with self.assertRaises(ValidationError):
move = self.env['account.move'].create(self.move)
move.post()
move_before_lock_no_tax = self.env['account.move'].create(self.move_no_tax)
move_before_lock_no_tax.post()
def test_change_after_block_date(self):
"""
Checks that you can change an account.move with a date after the tax
lock date
"""
self.user_id.company_id.tax_lock_date = self.first_day_month_str
move = self.env['account.move'].create(self.move)
move.line_ids[0].write({'account_id': self.other_account_id.id})
move.line_ids[1].write({'account_id': self.other_account_id.id})
move.line_ids[1].write({'debit': 11})
move.line_ids[0].write({'credit': 11})
move.post()
move.line_ids[1].write({'tax_ids': [(5, False, False)]})
def test_change_before_block_date(self):
"""
Checks that you cannot change an account.move with a date before the tax
lock date
"""
self.user_id.company_id.tax_lock_date = self.first_day_month_str
move = self.env['account.move'].create(self.move)
# Posting before the tax lock date is not allowed for moves impacting the tax report
self.user_id.company_id.tax_lock_date = self.last_day_month_str
move.line_ids[0].write({'account_id': self.other_account_id.id})
move.line_ids[1].write({'account_id': self.other_account_id.id})
with self.assertRaises(ValidationError):
with self.cr.savepoint():
move.line_ids[1].write({'debit': 11})
with self.assertRaises(ValidationError):
with self.cr.savepoint():
move.line_ids[1].write({'date': self.last_day_month_str, 'tax_ids': [(5, False, False)]})
move.line_ids[0].write({'credit': 10})
move.post()
move_before_lock = self.env['account.move'].create(self.move.copy())
with self.assertRaises(UserError):
move_before_lock.post()
def test_unlink_before_block_date(self):
"""
Checks that you cannot unlink an account.move with a date before the tax
lock date
"""
self.user_id.company_id.tax_lock_date = self.first_day_month_str
def test_tax_lock_date_cancel(self):
""" Checks that it is not possible to cancel an entry posted before the
tax lock date if it impacts the tax report. """
self.sale_journal_id.update_posted = True
move = self.env['account.move'].create(self.move)
move.post()
move_no_tax = self.env['account.move'].create(self.move_no_tax)
move_no_tax.post()
self.user_id.company_id.tax_lock_date = self.last_day_month_str
with self.assertRaises(ValidationError):
move.unlink()
def test_unlink_after_block_date(self):
"""
Checks that you can unlink an account.move with a date after the lock
date
"""
self.user_id.company_id.tax_lock_date = self.first_day_month_str
move = self.env['account.move'].create(self.move)
move.post()
move.unlink()
move_no_tax.button_cancel()
with self.assertRaises(UserError):
move.button_cancel()