From c78bfc146386d714cb0eb80d834c790687825bf1 Mon Sep 17 00:00:00 2001 From: Yannick Tivisse Date: Thu, 23 Jun 2022 10:17:49 +0200 Subject: [PATCH] [FIX] hr_work_entry_contract: Fix work entry generation cron Purpose ======= The cron is breaking (expected singleton error) if treating contracts from the same company at the same time. Actually, this shouldn't be done that way to avoid mixing unrelated resource.calendar.leave. Handle the contracts over 2 cron execution instead. --- addons/hr_work_entry_contract/models/hr_contract.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/addons/hr_work_entry_contract/models/hr_contract.py b/addons/hr_work_entry_contract/models/hr_contract.py index 2f490a253b7..19cf58a8e4b 100644 --- a/addons/hr_work_entry_contract/models/hr_contract.py +++ b/addons/hr_work_entry_contract/models/hr_contract.py @@ -388,6 +388,12 @@ class HrContract(models.Model): (not c.last_generation_date or c.last_generation_date < today)) if not contracts_todo: return + countract_todo_count = len(contracts_todo) + # Filter contracts by company, work entries generation is not supposed to be called on + # contracts from differents companies, as we will retrieve the resource.calendar.leave + # and we don't want to mix everything up. The other contracts will be treated when the + # cron is re-triggered + contracts_todo = contracts_todo.filtered(lambda c: c.company_id == contracts_todo[0].company_id) # generate a batch of work entries BATCH_SIZE = 100 # Since attendance based are more volatile for their work entries generation @@ -398,5 +404,5 @@ class HrContract(models.Model): contracts_todo = contracts_todo.sorted(key=lambda c: 1 if c.has_static_work_entries() else 100) contracts_todo[:BATCH_SIZE]._generate_work_entries(start, stop, False) # if necessary, retrigger the cron to generate more work entries - if len(contracts_todo) > BATCH_SIZE: + if countract_todo_count > BATCH_SIZE: self.env.ref('hr_work_entry_contract.ir_cron_generate_missing_work_entries')._trigger()