From 2dda2db0f7166bdb0094f85b30830e991bd3d8ab 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 | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/fieldservice_recurring/models/fsm_recurring.py b/fieldservice_recurring/models/fsm_recurring.py index a5613c0e33..1d1d8d44fe 100644 --- a/fieldservice_recurring/models/fsm_recurring.py +++ b/fieldservice_recurring/models/fsm_recurring.py @@ -78,10 +78,29 @@ 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, + required=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(