[FIX] account: autopost draft entries

The following commit https://github.com/odoo/odoo/commit/0015662d166f5e07bcbd6c5e9e14f8837ffa58a3 was incorrectly done because the records are already limited to a 100 in the search query. We therefore remove redundant loop.

We also moved up all possible UserError in the _post() method before the soft computation such that if any UserError is raised (with the autopost cron for example), the soft computation will not be executed.

task-3482939

closes odoo/odoo#133756

X-original-commit: d87dc624c33c8edd0eafc130b02a95fd91e58094
Signed-off-by: Hugo Poncelet (hupo) <hupo@odoo.com>
Signed-off-by: William André (wan) <wan@odoo.com>
This commit is contained in:
hupo-odoo
2023-08-31 14:15:04 +00:00
parent b5c595e0aa
commit 9ed78c75c3
+28 -29
View File
@@ -3666,23 +3666,12 @@ class AccountMove(models.Model):
elif invoice.is_purchase_document(include_receipts=True):
raise UserError(_("The Bill/Refund date is required to validate this document."))
if soft:
future_moves = self.filtered(lambda move: move.date > fields.Date.context_today(self))
for move in future_moves:
if move.auto_post == 'no':
move.auto_post = 'at_date'
msg = _('This move will be posted at the accounting date: %(date)s', date=format_date(self.env, move.date))
move.message_post(body=msg)
to_post = self - future_moves
else:
to_post = self
for move in to_post:
for move in self:
if move.state in ['posted', 'cancel']:
raise UserError(_('The entry %s (id %s) must be in draft.') % (move.name, move.id))
if not move.line_ids.filtered(lambda line: line.display_type not in ('line_section', 'line_note')):
raise UserError(_('You need to add a line before posting.'))
if move.auto_post != 'no' and move.date > fields.Date.context_today(self):
if not soft and move.auto_post != 'no' and move.date > fields.Date.context_today(self):
date_msg = move.date.strftime(get_lang(self.env).date_format)
raise UserError(_("This move is configured to be auto-posted on %s", date_msg))
if not move.journal_id.active:
@@ -3699,6 +3688,18 @@ class AccountMove(models.Model):
if move.line_ids.account_id.filtered(lambda account: account.deprecated):
raise UserError(_("A line of this move is using a deprecated account, you cannot post it."))
if soft:
future_moves = self.filtered(lambda move: move.date > fields.Date.context_today(self))
for move in future_moves:
if move.auto_post == 'no':
move.auto_post = 'at_date'
msg = _('This move will be posted at the accounting date: %(date)s', date=format_date(self.env, move.date))
move.message_post(body=msg)
to_post = self - future_moves
else:
to_post = self
for move in to_post:
affects_tax_report = move._affect_tax_report()
lock_dates = move._get_violated_lock_dates(move.date, affects_tax_report)
if lock_dates:
@@ -4071,29 +4072,27 @@ class AccountMove(models.Model):
It is used to post entries such as those created by the module
account_asset and recurring entries created in _post().
'''
records = self.search([
moves = self.search([
('state', '=', 'draft'),
('date', '<=', fields.Date.context_today(self)),
('auto_post', '!=', 'no'),
('to_check', '=', False),
], limit=100)
for ids in self._cr.split_for_in_conditions(records.ids, size=100):
moves = self.browse(ids)
try: # try posting in batch
with self.env.cr.savepoint():
moves._post()
except UserError: # if at least one move cannot be posted, handle moves one by one
for move in moves:
try:
with self.env.cr.savepoint():
move._post()
except UserError as e:
move.to_check = True
msg = _('The move could not be posted for the following reason: %(error_message)s', error_message=e)
move.message_post(body=msg, message_type='comment')
try: # try posting in batch
with self.env.cr.savepoint():
moves._post()
except UserError: # if at least one move cannot be posted, handle moves one by one
for move in moves:
try:
with self.env.cr.savepoint():
move._post()
except UserError as e:
move.to_check = True
msg = _('The move could not be posted for the following reason: %(error_message)s', error_message=e)
move.message_post(body=msg, message_type='comment')
if len(records) == 100: # assumes there are more whenever search hits limit
if len(moves) == 100: # assumes there are more whenever search hits limit
self.env.ref('account.ir_cron_auto_post_draft_entry')._trigger()
@api.model