diff --git a/addons/account/models/account_move.py b/addons/account/models/account_move.py index 4c994b6b6f0..958a1209830 100644 --- a/addons/account/models/account_move.py +++ b/addons/account/models/account_move.py @@ -639,7 +639,7 @@ class AccountMove(models.Model): JOIN account_move_account_move_send_rel move_send_rel ON move_send_rel.account_move_id in %(move_ids)s AND send.id = move_send_rel.account_move_send_id - WHERE send.mode != 'done' + WHERE send.mode = 'invoice_multi' """, params={'move_ids': tuple(self.ids)} ) @@ -4116,11 +4116,17 @@ class AccountMove(models.Model): :param with_commit: Flag indicating a commit should be made between each job. ''' # Clean already processed wizards. - self.env['account.move.send'].search([('mode', '=', 'done')]).unlink() + self.env['account.move.send'].search([ + '|', + ('mode', '=', 'done'), + '&', + ('mode', '=', 'invoice_single'), + ('create_date', '<=', fields.Date.context_today(self) - relativedelta(days=1)), + ]).unlink() # Process. limit = job_count + 1 - to_process = self.env['account.move.send'].search([('mode', '!=', 'done')], limit=limit) + to_process = self.env['account.move.send'].search([('mode', '=', 'invoice_multi')], limit=limit) need_retrigger = len(to_process) > job_count diff --git a/addons/account/tests/test_account_move_send.py b/addons/account/tests/test_account_move_send.py index 230ee1abf34..23e8976563e 100644 --- a/addons/account/tests/test_account_move_send.py +++ b/addons/account/tests/test_account_move_send.py @@ -2,9 +2,11 @@ # Part of Odoo. See LICENSE file for full copyright and licensing details. from datetime import date +from dateutil.relativedelta import relativedelta +from freezegun import freeze_time from unittest.mock import patch -from odoo import Command +from odoo import Command, fields from odoo.addons.account.tests.common import AccountTestInvoicingCommon from odoo.addons.mail.tests.common import MailCommon from odoo.exceptions import UserError @@ -829,6 +831,33 @@ class TestAccountMoveSend(TestAccountMoveSendCommon): self.env.ref('account.ir_cron_account_move_send').method_direct_trigger() self.assertFalse(wizard.exists()) + def test_unlink_wizard_invoice_single(self): + invoice = self.init_invoice("out_invoice", amounts=[1000], post=True) + wizard = self.create_send_and_print(invoice) + + def _hook_if_errors(self, moves_data, from_cron=False, allow_fallback_pdf=False): + raise UserError('Test Error') + + def _hook_invoice_document_before_pdf_report_render(self, invoice, invoice_data): + invoice_data['error'] = 'test_error' + + # Process with an error so that the wizard doesn't reach 'done' state + with ( + patch.object(type(wizard), '_hook_if_errors', _hook_if_errors), + patch.object(type(wizard), '_hook_invoice_document_before_pdf_report_render', _hook_invoice_document_before_pdf_report_render) + ): + with self.assertRaises(UserError), self.cr.savepoint(): + wizard.action_send_and_print(allow_fallback_pdf=False) + + # If cron is triggered within the same day wizard is still exists. We don't want to delete an opened wizard. + self.env.ref('account.ir_cron_account_move_send').method_direct_trigger() + self.assertTrue(wizard.exists()) + + # If cron is triggered one day or later than the creation it should be garbage collected. + with freeze_time(fields.Date.today() + relativedelta(days=1)): + self.env.ref('account.ir_cron_account_move_send').method_direct_trigger() + self.assertFalse(wizard.exists()) + def test_with_empty_mail_template(self): """ Test you can use the send & print wizard without any mail template. """ self.partner_a.email = "turlututu@tsointsoin"