Skip to content

Commit 85b43dc

Browse files
[IMP] maintenance_location: Add inverse relation and equipment count to locations
1 parent 6467c6b commit 85b43dc

File tree

3 files changed

+66
-7
lines changed

3 files changed

+66
-7
lines changed

maintenance_location/models/maintenance_location.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,41 @@ class MaintenanceLocation(models.Model):
3535
sequence = fields.Integer(default=10)
3636
active = fields.Boolean(default=True)
3737

38+
equipment_ids = fields.One2many(
39+
"maintenance.equipment", "location_id", string="Equipments"
40+
)
41+
42+
child_equipment_ids = fields.Many2many(
43+
comodel_name="maintenance.equipment",
44+
compute="_compute_child_equipment_ids",
45+
string="All Equipments (incl. sublocations)",
46+
)
47+
48+
equipment_count = fields.Integer(compute="_compute_equipment_count")
49+
50+
def _compute_child_equipment_ids(self):
51+
for location in self:
52+
location.child_equipment_ids = self.env["maintenance.equipment"].search([
53+
("location_id", "child_of", location.id),
54+
("location_id", "!=", location.id)
55+
])
56+
57+
def _compute_equipment_count(self):
58+
all_locations = self.env["maintenance.location"].search([
59+
("id", "child_of", self.ids)
60+
])
61+
equip_data = self.env["maintenance.equipment"].read_group(
62+
domain=[("location_id", "in", all_locations.ids)],
63+
fields=["location_id"],
64+
groupby=["location_id"],
65+
)
66+
count_dict = {x["location_id"][0]: x["location_id_count"] for x in equip_data}
67+
for location in self:
68+
descendant_ids = all_locations.filtered(
69+
lambda l: l.parent_path and l.parent_path.startswith(location.parent_path)
70+
).ids
71+
location.equipment_count = sum(count_dict.get(d_id, 0) for d_id in descendant_ids)
72+
3873
@api.depends("name", "parent_id.complete_name")
3974
def _compute_complete_name(self):
4075
for location in self:

maintenance_location/tests/test_maintenance_location.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ def setUpClass(cls):
2626
{"name": "Laptop", "location_id": self.location_1.id}
2727
)
2828

29+
self.equipment = self.env["maintenance.equipment"].create(
30+
{"name": "Printer", "location_id": self.location_1.id}
31+
)
32+
2933
self.plan = self.env["maintenance.plan"].create(
3034
{
3135
"equipment_id": self.equipment.id,
@@ -51,3 +55,7 @@ def test_request_creation(self):
5155
self.assertTrue(request)
5256
for r in request:
5357
self.assertEqual(r.location_id.id, self.location_1.id)
58+
59+
def test_count_equipment(self):
60+
self.assertEqual(self.location_1.equipment_count, 3)
61+
self.assertEqual(self.location_2.equipment_count, 0)

maintenance_location/views/maintenance_location.xml

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,28 @@
1515
</h1>
1616
</div>
1717
<group name="first" col="2">
18-
<field name="description" />
19-
<field name="parent_id" class="oe_inline" />
20-
<label for="latitude" string="Location" />
21-
<span class="oe_inline">
22-
Latitude: <field name="latitude" nolabel="1" /><br />
23-
Longitude: <field name="longitude" nolabel="1" />
24-
</span>
18+
<group>
19+
<field name="description" />
20+
<field name="parent_id" class="oe_inline" />
21+
<label for="latitude" string="Location" />
22+
<span class="oe_inline">
23+
Latitude: <field name="latitude" nolabel="1" /><br />
24+
Longitude: <field name="longitude" nolabel="1" />
25+
</span>
26+
</group>
27+
<group>
28+
<field
29+
name="equipment_ids"
30+
widget="many2many_tags"
31+
string="Equipments"
32+
/>
33+
<field
34+
name="child_equipment_ids"
35+
widget="many2many_tags"
36+
string="Child Equipments"
37+
/>
38+
</group>
39+
2540
</group>
2641
</sheet>
2742
</form>
@@ -42,6 +57,7 @@
4257
<field name="arch" type="xml">
4358
<tree>
4459
<field name="complete_name" />
60+
<field name="equipment_count" />
4561
</tree>
4662
</field>
4763
</record>

0 commit comments

Comments
 (0)