From 4467947154bb0ded8f2070872cd11608f559869f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Todorovich?= Date: Tue, 3 Dec 2024 12:27:39 -0300 Subject: [PATCH] [IMP] fieldservice_recurring: populate fsm.recurring.person_id from the res.territory --- .../models/fsm_recurring.py | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/fieldservice_recurring/models/fsm_recurring.py b/fieldservice_recurring/models/fsm_recurring.py index a5613c0e33..f49dbc71f4 100644 --- a/fieldservice_recurring/models/fsm_recurring.py +++ b/fieldservice_recurring/models/fsm_recurring.py @@ -78,10 +78,28 @@ def _default_team_id(self): tracking=True, ) person_id = fields.Many2one( - "fsm.person", string="Assigned To", index=True, tracking=True + "fsm.person", + string="Assigned To", + compute="_compute_person_id", + precompute=True, + store=True, + readonly=False, + index=True, + tracking=True, ) equipment_ids = fields.Many2many("fsm.equipment") + @api.depends("location_id") + def _compute_person_id(self): + """Compute the person from the location's territory""" + for rec in self: + # If the person is one of the territory's workers, keep it. + if rec.person_id in rec.location_id.territory_id.person_ids: + continue + # If the territory has a primary assignment, use it. + if rec.location_id.territory_id.person_id: + rec.person_id = rec.location_id.territory_id.person_id + @api.depends("fsm_order_ids") def _compute_order_count(self): data = self.env["fsm.order"].read_group(