From d11d4081bcb4adae804902ca38371c1ca7928335 Mon Sep 17 00:00:00 2001 From: Ryan Cen Date: Wed, 13 Mar 2024 17:03:27 -0700 Subject: [PATCH] [FIX] account: correctly use job count on cron The cron "send invoices automatically" was not properly using the job_count parameter because the to_process invoices were now pulled from a read_group. The limit parameter on read_group only limits the number of groups and it was being grouped by company. This meant if there was less than job_count number of companies this was not doing anything. And if one company had a large amount of invoices this would time out and repeat the same invoices the next time it was attempted because no progress is saved until the batch is over. Changing the read_group to a search. opw-3765192 Part-of: odoo/odoo#158035 --- addons/account/models/account_move.py | 7 +++---- addons/account/tests/test_account_move_send.py | 10 ++++++---- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/addons/account/models/account_move.py b/addons/account/models/account_move.py index 1f5e8c1b711..c7558c78b5b 100644 --- a/addons/account/models/account_move.py +++ b/addons/account/models/account_move.py @@ -4254,17 +4254,16 @@ class AccountMove(models.Model): ] limit = job_count + 1 - to_process = self.env['account.move']._read_group( + to_process = self.env['account.move'].search( [('send_and_print_values', '!=', False)], - groupby=['company_id'], - aggregates=['id:recordset'], limit=limit, ) need_retrigger = len(to_process) > job_count if not to_process: return - for _company, moves in to_process[:job_count]: + all_moves = to_process[:job_count] + for _company, moves in all_moves.grouped('company_id').items(): try: # Lock moves with self.env.cr.savepoint(flush=False): diff --git a/addons/account/tests/test_account_move_send.py b/addons/account/tests/test_account_move_send.py index 7593d95030b..d8bc94c04f3 100644 --- a/addons/account/tests/test_account_move_send.py +++ b/addons/account/tests/test_account_move_send.py @@ -1039,16 +1039,18 @@ class TestAccountMoveSend(TestAccountMoveSendCommon): order='id desc', limit=1, ) - self.assertEqual(json.loads(bus_1.message)['payload']['type'], 'success') - self.assertEqual(json.loads(bus_1.message)['payload']['action_button']['res_ids'], invoices_success.ids) + payload_1 = json.loads(bus_1.message)['payload'] + self.assertEqual(payload_1['type'], 'success') + self.assertEqual(sorted(payload_1['action_button']['res_ids']), invoices_success.ids) bus_2 = self.env['bus.bus'].sudo().search( [('channel', 'like', f'"res.partner",{sp_partner_2.id}')], order='id desc', limit=1, ) - self.assertEqual(json.loads(bus_2.message)['payload']['type'], 'warning') - self.assertEqual(json.loads(bus_2.message)['payload']['action_button']['res_ids'], invoices_error.ids) + payload_2 = json.loads(bus_2.message)['payload'] + self.assertEqual(payload_2['type'], 'warning') + self.assertEqual(sorted(payload_2['action_button']['res_ids']), invoices_error.ids) def test_send_and_print_only(self): invoice = self.init_invoice("out_invoice", amounts=[1000], post=True)