diff --git a/addons/account/i18n/account.pot b/addons/account/i18n/account.pot index 0789bd2aea9e0107e5838d18c913637e48335002..01f17f1a01d05b2a1b47726b31220342c6a17927 100644 --- a/addons/account/i18n/account.pot +++ b/addons/account/i18n/account.pot @@ -11998,6 +11998,13 @@ msgid "" " e.g: .*N°48748 abc123.*" msgstr "" +#. module: account +#: code:addons/account/models/account_move.py:0 +#, python-format +msgid "" +"The move could not be posted for the following reason: %(error_message)s" +msgstr "" + #. module: account #: model:ir.model.fields,help:account.field_account_move_line__move_id msgid "The move of this entry line." diff --git a/addons/account/models/account_move.py b/addons/account/models/account_move.py index e7cb28f975bb66c8411f6e203df0008079b37633..3d96cce9f64efbe37c79bde27c6348c0d6aa226c 100644 --- a/addons/account/models/account_move.py +++ b/addons/account/models/account_move.py @@ -3102,8 +3102,22 @@ class AccountMove(models.Model): ('date', '<=', fields.Date.context_today(self)), ('auto_post', '=', True), ]) - for ids in self._cr.split_for_in_conditions(records.ids, size=1000): - self.browse(ids)._post() + + for ids in self._cr.split_for_in_conditions(records.ids, size=100): + moves = self.browse(ids) + try: # try posting in batch + moves._post() + except UserError: # if at least one move cannot be posted, handle moves one by one + for move in moves: + try: + 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', + author_id=self.env.ref('base.partner_root').id) + if not self.env.registry.in_test_mode(): self._cr.commit() diff --git a/addons/account/tests/test_account_move_out_invoice.py b/addons/account/tests/test_account_move_out_invoice.py index 3ee2b7676a2dd08e89c9c779860024a557f4f3e1..9f672f3e92b8a596d6c4a4f6b572f2d4cf4d4bed 100644 --- a/addons/account/tests/test_account_move_out_invoice.py +++ b/addons/account/tests/test_account_move_out_invoice.py @@ -3591,3 +3591,37 @@ class TestAccountMoveOutInvoiceOnchanges(AccountTestInvoicingCommon): self.assertRecordValues(caba_move, [{ 'date': fields.Date.from_string('2023-01-30'), }]) + + @freeze_time('2023-01-01') + def test_post_valid_invoices_when_auto_post(self): + valid_invoice = self.init_invoice(move_type='out_invoice', products=self.product_a, invoice_date='2023-01-01') + + # missing partner + invalid_invoice_1 = self.env['account.move'].create({ + 'move_type': 'out_invoice', + 'invoice_date': '2023-01-01', + 'date': '2023-01-01', + 'invoice_line_ids': [(0, 0, { + 'name': 'test line', + 'price_unit': 10, + 'quantity': 1, + 'account_id': self.company_data['default_account_revenue'].id, + })], + }) + + # missing invoice lines + invalid_invoice_2 = self.init_invoice(move_type='out_invoice', invoice_date='2023-01-01') + + (valid_invoice + invalid_invoice_1 + invalid_invoice_2).auto_post = True + + self.registry.enter_test_mode(self.env.cr) + self.env['account.move']._autopost_draft_entries() + self.registry.leave_test_mode() + self.assertEqual(valid_invoice.state, 'posted') + self.assertEqual(invalid_invoice_1.state, 'draft') + self.assertEqual(invalid_invoice_1.message_ids[0].body, + "<p>The move could not be posted for the following reason: " + "The field 'Customer' is required, please complete it to validate the Customer Invoice.</p>") + self.assertEqual(invalid_invoice_2.state, 'draft') + self.assertEqual(invalid_invoice_2.message_ids[0].body, + "<p>The move could not be posted for the following reason: You need to add a line before posting.</p>")