Files
Odoo-18.0-20251222/maintenance_plan/hooks.py
tocmo0nlord adbe430761
Some checks failed
pre-commit / pre-commit (push) Has been cancelled
tests / Detect unreleased dependencies (push) Has been cancelled
tests / test with OCB (push) Has been cancelled
tests / test with Odoo (push) Has been cancelled
Initial commit: Odoo 18.0-20251222 extra-addons
2026-03-13 20:43:25 +00:00

49 lines
1.8 KiB
Python

# Copyright 2017 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import logging
from odoo import _
from odoo.exceptions import UserError
def post_init_hook(env):
logging.getLogger("odoo.addons.maintenance_plan").info(
"Migrating existing preventive maintenance"
)
equipments = env["maintenance.equipment"].search([("expected_mtbf", "!=", False)])
if equipments:
maintenance_kind = env["maintenance.kind"].create(
{"name": "Install", "active": True}
)
for equipment in equipments:
request = equipment.maintenance_ids.filtered(
lambda r, equipment=equipment: 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 %(name)s next action date (%(date)s). "
"Please leave only one preventive request on the "
"date of equipment's next action to install the module.",
name=equipment.name,
date=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.mtbf,
"interval": equipment.expected_mtbf,
}
)