Skip to content

Commit 00b04db

Browse files
committed
[FIX] fieldservice: Fix location partner type
Service locations `fsm.location` create a partner `res.partner` through inheritance. Before this change, partners could be explicitely typed "fsm_location" when adding them within children of an existing partner, which in turn would automatically create service locations on top of them. But creating a service location by hand would not set that type on the partner created through inheritance. After this change, all partners built from service locations are typed the same. Fixes #1128.
1 parent 77cd48b commit 00b04db

File tree

5 files changed

+39
-29
lines changed

5 files changed

+39
-29
lines changed

fieldservice/models/fsm_location.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,12 @@ class FSMLocation(models.Model):
7070
)
7171

7272
@api.model_create_multi
73-
def create(self, vals):
74-
res = super().create(vals)
75-
res.write({"fsm_location": True})
76-
return res
73+
def create(self, vals_list):
74+
for vals in vals_list:
75+
vals.update({"fsm_location": True, "type": "fsm_location"})
76+
return super(FSMLocation, self.with_context(creating_fsm_location=True)).create(
77+
vals_list
78+
)
7779

7880
@api.depends("partner_id.name", "fsm_parent_id.complete_name", "ref")
7981
def _compute_complete_name(self):

fieldservice/models/res_partner.py

+17-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright (C) 2018 - TODAY, Open Source Integrators
22
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
33

4-
from odoo import fields, models
4+
from odoo import api, fields, models
55

66

77
class ResPartner(models.Model):
@@ -52,21 +52,22 @@ def action_open_owned_locations(self):
5252
return action
5353

5454
def _convert_fsm_location(self):
55-
wiz = self.env["fsm.wizard"]
56-
partners_with_loc_ids = (
57-
self.env["fsm.location"]
58-
.sudo()
59-
.search([("active", "in", [False, True]), ("partner_id", "in", self.ids)])
60-
.mapped("partner_id")
61-
).ids
55+
"""Build service location when adding child partner with type=fsm_location."""
56+
if self.env.context.get("creating_fsm_location"):
57+
return # partner created by inheritance from fsm.location
58+
for partner in self:
59+
if partner.type == "fsm_location" and not partner.fsm_location_id:
60+
self.env["fsm.wizard"].action_convert_location(partner)
6261

63-
partners_to_convert = self.filtered(
64-
lambda p: p.type == "fsm_location" and p.id not in partners_with_loc_ids
65-
)
66-
for partner_to_convert in partners_to_convert:
67-
wiz.action_convert_location(partner_to_convert)
62+
@api.model_create_multi
63+
def create(self, vals_list):
64+
partners = super().create(vals_list)
65+
if any(vals.get("type") == "fsm_location" for vals in vals_list):
66+
partners._convert_fsm_location()
67+
return partners
6868

69-
def write(self, value):
70-
res = super().write(value)
71-
self._convert_fsm_location()
69+
def write(self, vals):
70+
res = super().write(vals)
71+
if vals.get("type") == "fsm_location":
72+
self._convert_fsm_location()
7273
return res

fieldservice/tests/test_fsm_location.py

+6
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ def test_fsm_location(self):
5050
]:
5151
self.assertEqual(location[x], self.test_location[x])
5252

53+
# Check partner defaults.
54+
self.assertTrue(location.fsm_location)
55+
self.assertFalse(location.fsm_person)
56+
self.assertFalse(location.is_company)
57+
self.assertEqual(location.type, "fsm_location")
58+
5359
# Test initial stage
5460
self.assertEqual(
5561
location.stage_id, self.env.ref("fieldservice.location_stage_1")

fieldservice/tests/test_fsm_wizard.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,15 @@ def test_convert_location(self):
6060
# convert test_partner to FSM Location
6161
self.Wizard.action_convert_location(self.test_partner)
6262

63-
# check if there is a new FSM Location with name 'Test Partner'
64-
self.wiz_location = self.env["fsm.location"].search(
65-
[("name", "=", "Test Partner")]
66-
)
67-
68-
# check if 'Test Partner' creation successful and fields copied over
69-
self.assertEqual(self.test_location.phone, self.wiz_location.phone)
70-
self.assertEqual(self.test_location.email, self.wiz_location.email)
63+
# Check new service location.
64+
new_location = self.env["fsm.location"].search([("name", "=", "Test Partner")])
65+
self.assertNotEqual(new_location, self.test_location)
66+
self.assertTrue(new_location.fsm_location)
67+
self.assertFalse(new_location.fsm_person)
68+
self.assertFalse(new_location.is_company)
69+
self.assertEqual(new_location.type, "fsm_location")
70+
self.assertEqual(self.test_location.phone, new_location.phone)
71+
self.assertEqual(self.test_location.email, new_location.email)
7172

7273
def test_convert_person(self):
7374
# convert test_partner to FSM Person

fieldservice/wizard/fsm_wizard.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def action_convert_location(self, partner):
3434
fl_model = self.env["fsm.location"]
3535
if fl_model.search_count([("partner_id", "=", partner.id)]) == 0:
3636
fl_model.create(self._prepare_fsm_location(partner))
37-
partner.write({"fsm_location": True})
37+
partner.write({"fsm_location": True, "type": "fsm_location"})
3838
self.action_other_address(partner)
3939
else:
4040
raise UserError(

0 commit comments

Comments
 (0)