[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
This commit is contained in:
Thibault Delavallée
2024-03-18 10:23:55 +00:00
parent 793a42ff13
commit c36f9c184d
@@ -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')