forked from OCA/maintenance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccount_move.py
133 lines (118 loc) · 5.13 KB
/
account_move.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# Copyright 2022 Tecnativa - Víctor Martínez
# Copyright 2024 Tecnativa - Carolina Fernandez
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo import api, fields, models
class AccountMove(models.Model):
_inherit = "account.move"
equipment_count = fields.Integer(compute="_compute_equipment_count")
def _compute_equipment_count(self):
for item in self:
item.equipment_count = sum(item.mapped("line_ids.equipment_count"))
def unlink(self):
items = self.env["maintenance.equipment"].search([("move_id", "in", self.ids)])
items.write({"move_line_id": False, "move_id": False})
return super().unlink()
def action_post(self):
res = super().action_post()
# Prevent error if user does not have permission to create equipments
equipment_model = self.env["maintenance.equipment"].sudo()
for move in self.filtered(lambda r: r.is_purchase_document()):
for line in move.line_ids.filtered(
lambda x: (
not x.equipment_ids
and x.product_id
and x.product_id.product_tmpl_id.maintenance_ok
)
):
if not line.equipment_category_id:
line._set_equipment_category()
# Create equipments
limit = int(line.quantity) + 1
vals = line._prepare_equipment_vals()
equipment_ids = []
for _i in range(1, limit):
equipment = equipment_model.with_company(
move.company_id,
).create(vals.copy())
equipment_ids.append((4, equipment.id))
line.equipment_ids = equipment_ids
return res
def action_view_equipments(self):
items = self.env["maintenance.equipment"].search([("move_id", "=", self.id)])
action_dict = self.env["ir.actions.act_window"]._for_xml_id(
"maintenance.hr_equipment_action"
)
if len(items) == 1:
res = self.env.ref("maintenance.hr_equipment_view_form", False)
action_dict["views"] = [(res and res.id or False, "form")]
action_dict["res_id"] = items.id
elif items:
action_dict["domain"] = [("id", "in", items.ids)]
else:
action_dict = {"type": "ir.actions.act_window_close"}
return action_dict
class AccountMoveLine(models.Model):
_inherit = "account.move.line"
equipment_category_id = fields.Many2one(
comodel_name="maintenance.equipment.category",
string="Equipment Category",
compute="_compute_equipment_category_id",
store=True,
readonly=False,
)
equipment_ids = fields.Many2many(
comodel_name="maintenance.equipment",
string="Equipments",
)
equipment_count = fields.Integer(compute="_compute_equipment_count")
@api.depends("product_id")
def _compute_equipment_category_id(self):
for item in self:
if (
item.product_id.maintenance_ok
and item.product_id.product_tmpl_id.categ_id.equipment_category_ids
):
item.equipment_category_id = fields.first(
item.product_id.product_tmpl_id.categ_id.equipment_category_ids
)
else:
item.equipment_category_id = item.equipment_category_id
def _compute_equipment_count(self):
data = self.env["maintenance.equipment"].read_group(
[("move_line_id", "in", self.ids)], ["move_line_id"], ["move_line_id"]
)
mapping = {x["move_line_id"][0]: x["move_line_id_count"] for x in data}
for item in self:
item.equipment_count = mapping.get(item.id, 0)
def _prepare_equipment_category_vals(self):
categ = self.product_id.product_tmpl_id.categ_id
return {"name": categ.name, "product_category_id": categ.id}
def _set_equipment_category(self):
if not self.equipment_category_id:
category_model = self.env["maintenance.equipment.category"].sudo()
category = fields.first(
self.product_id.product_tmpl_id.categ_id.equipment_category_ids
)
if not category:
category = category_model.create(
self._prepare_equipment_category_vals()
)
self.equipment_category_id = category.id
def _prepare_equipment_vals(self):
equipment_name = self.name
description = False
if "\n" in self.name:
lf_index = self.name.index("\n")
equipment_name = self.name[:lf_index]
description = self.name[lf_index + 1 :]
return {
"move_line_id": self.id,
"name": equipment_name,
"product_id": self.product_id.id,
"category_id": self.equipment_category_id.id,
"assign_date": self.move_id.date,
"effective_date": self.move_id.date,
"partner_id": self.move_id.partner_id.id,
"partner_ref": self.move_id.ref,
"note": description,
}