forked from OCA/maintenance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hooks.py
51 lines (41 loc) · 1.77 KB
/
hooks.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
# Copyright 2017 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import logging
from odoo import SUPERUSER_ID, _, api
from odoo.exceptions import UserError
def post_init_hook(cr, registry):
logging.getLogger("odoo.addons.maintenance_plan").info(
"Migrating existing preventive maintenance"
)
env = api.Environment(cr, SUPERUSER_ID, {})
equipments = env["maintenance.equipment"].search([("period", "!=", False)])
if equipments:
maintenance_kind = env["maintenance.kind"].create(
{"name": "Install", "active": True}
)
for equipment in equipments:
request = equipment.maintenance_ids.filtered(
lambda r: r.maintenance_type == "preventive"
and not r.stage_id.done
and r.request_date == equipment.next_action_date
)
if len(request) > 1:
raise UserError(
_(
"You have multiple preventive maintenance requests on "
"equipment %s next action date (%s). Please leave only "
"one preventive request on the date of equipment's next "
"action to install the module."
)
% (equipment.name, equipment.next_action_date)
)
elif len(request) == 1:
request.write({"maintenance_kind_id": maintenance_kind.id})
env["maintenance.plan"].create(
{
"equipment_id": equipment.id,
"maintenance_kind_id": maintenance_kind.id,
"duration": equipment.maintenance_duration,
"interval": equipment.period,
}
)