From c36f9c184d69bbb7cf392e96bce4e46a35ffde64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Wed, 28 Feb 2024 17:21:34 +0100 Subject: [PATCH] [FIX] event_sale: avoid multiple update of registration state Various registration side updates depends on its state: notably communication schedulers and lead rules management. However event_sale changes the 'state' field from a classic selection field to a computed one, introducing links with sale order and sale order lines as well as payment state computation. Currently code leads to a double update of state when creating a new registration, which causes communication scheduler to be called twice and create additional queries. This commit tries to avoid this by updating value of state only once, instead of setting everything to draft then updating to another value afterwards. This avoids notably schedulers to be triggered or called twice in the same transaction. Task-3764894: Event: Allow using cron triggers for communication Part of Task-3084943: Event: Improve communication scheduler scalability Part-of: odoo/odoo#155777 --- addons/event_sale/models/event_registration.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/addons/event_sale/models/event_registration.py b/addons/event_sale/models/event_registration.py index 9300d610790..a17821cdc1c 100644 --- a/addons/event_sale/models/event_registration.py +++ b/addons/event_sale/models/event_registration.py @@ -26,19 +26,18 @@ class EventRegistration(models.Model): @api.depends('sale_order_id.state', 'sale_order_id.currency_id', 'sale_order_line_id.price_total') def _compute_registration_status(self): - self.filtered(lambda reg: not reg.state).state = 'draft' for so_line, registrations in self.grouped('sale_order_line_id').items(): cancelled_so_registrations = registrations.filtered(lambda reg: reg.sale_order_id.state == 'cancel') cancelled_so_registrations.state = 'cancel' cancelled_registrations = cancelled_so_registrations | registrations.filtered(lambda reg: reg.state == 'cancel') if not so_line or float_is_zero(so_line.price_total, precision_digits=so_line.currency_id.rounding): registrations.sale_status = 'free' - registrations.filtered(lambda reg: reg.state == 'draft').write({"state": "open"}) + registrations.filtered(lambda reg: not reg.state or reg.state == 'draft').state = "open" else: sold_registrations = registrations.filtered(lambda reg: reg.sale_order_id.state == 'sale') - cancelled_registrations sold_registrations.sale_status = 'sold' (registrations - sold_registrations).sale_status = 'to_pay' - sold_registrations.filtered(lambda reg: reg.state in {'draft', 'cancel'}).write({"state": "open"}) + sold_registrations.filtered(lambda reg: not reg.state or reg.state in {'draft', 'cancel'}).state = "open" (registrations - sold_registrations - cancelled_registrations).state = 'draft' @api.depends('sale_order_id')