diff --git a/sf_maintenance/__init__.py b/sf_maintenance/__init__.py index dc5e6b69..2ae6446f 100644 --- a/sf_maintenance/__init__.py +++ b/sf_maintenance/__init__.py @@ -2,3 +2,4 @@ # Part of Odoo. See LICENSE file for full copyright and licensing details. from . import models +from . import wizard diff --git a/sf_maintenance/__manifest__.py b/sf_maintenance/__manifest__.py index 88c53ff8..63f4604a 100644 --- a/sf_maintenance/__manifest__.py +++ b/sf_maintenance/__manifest__.py @@ -18,6 +18,7 @@ 'views/equipment_maintenance_standards_views.xml', 'views/maintenance_request_views.xml', 'views/maintenance_equipment_category_views.xml', + 'wizard/maintenance_request_wizard.xml', ], 'installable': True, 'application': False, diff --git a/sf_maintenance/models/sf_maintenance.py b/sf_maintenance/models/sf_maintenance.py index d5bfa6b4..3d68bb9a 100644 --- a/sf_maintenance/models/sf_maintenance.py +++ b/sf_maintenance/models/sf_maintenance.py @@ -1,11 +1,12 @@ # -*- coding: utf-8 -*- import json import base64 +import logging from datetime import timedelta import requests from odoo.addons.sf_base.commons.common import Common from odoo import api, fields, models, _ -from odoo.exceptions import UserError +from odoo.exceptions import UserError, ValidationError class SfMaintenanceEquipmentCategory(models.Model): @@ -122,6 +123,13 @@ class SfMaintenanceEquipment(models.Model): 'sf_maintenance_equipment_ids', string='设备维保标准') eq_maintenance_id = fields.Many2one('equipment.maintenance.standards', string='设备保养标准', domain="[('maintenance_type','=','保养')]") + + initial_action_date = fields.Date(string='重置保养日期') + initial_action_date_old = fields.Date(string='重置保养日期(旧)') + next_action_date = fields.Date(string='下次预防保养') + + initial_overhaul_date = fields.Date(string='重置维修日期') + initial_overhaul_date_old = fields.Date(string='重置维修日期(旧)') overhaul_date = fields.Date(string='下次预防检修') overhaul_period = fields.Integer(string='预防检修频次') overhaul_duration = fields.Float(string='检修时长') @@ -129,6 +137,61 @@ class SfMaintenanceEquipment(models.Model): overhaul_id = fields.Many2one('equipment.maintenance.standards', string='设备检修标准', domain="[('maintenance_type','=','检修')]") + def confirm_maintenance(self): + """ + 确认保养/检修 + """ + context = self.env.context + if context['type'] == '保养': + if not self.initial_action_date: + raise ValidationError('重置保养日期不能为空!!') + elif self.initial_action_date < fields.Date.today(): + raise ValidationError('重置保养日期不能小于当前日期!!') + elif context['type'] == '检修': + if not self.initial_overhaul_date: + raise ValidationError('重置检修日期不能为空!!') + elif self.initial_overhaul_date < fields.Date.today(): + raise ValidationError('重置检修日期不能小于当前日期!!') + + request_ids = self.env['maintenance.request'].search([('stage_id.done', '=', False), + ('equipment_id', '=', self.id), + ('maintenance_type', '=', 'preventive'), + ('sf_maintenance_type', '=', context['type'])]) + + if not request_ids: + return self.create_maintenance_request(context['type']) + else: + return { + "type": "ir.actions.act_window", + "res_model": "maintenance.request.wizard", + "views": [[False, "form"]], + "target": "new", + 'context': { + 'equipment_id': self.id + } + } + + def create_maintenance_request(self, maintenance_request_type): + """ + 根据条件创建维保计划 + """ + if maintenance_request_type == '保养': + self._create_new_request(self.initial_action_date + timedelta(days=self.period)) + self.initial_action_date_old = self.initial_action_date + elif maintenance_request_type == '检修': + self._create_new_request1(self.initial_overhaul_date + timedelta(days=self.overhaul_period)) + self.initial_overhaul_date_old = self.initial_overhaul_date + return { + 'type': 'ir.actions.client', + 'tag': 'display_notification', + 'params': { + 'title': f'创建{maintenance_request_type}计划', + 'message': f'新{maintenance_request_type}维保计划创建成功', + 'type': 'success', + 'next': {'type': 'ir.actions.act_window_close'}, + } + } + @api.onchange('eq_maintenance_id', 'overhaul_id') def _compute_equipment_maintenance_standards_ids(self): for record in self: @@ -591,11 +654,13 @@ class SfMaintenanceEquipment(models.Model): ('equipment_id', '=', equipment.id), ('sf_maintenance_type', '=', '保养'), ('stage_id.done', '!=', True), + ('active', '!=', False), ('close_date', '=', False)], order="request_date asc", limit=1) last_maintenance_done = self.env['maintenance.request'].search([ ('equipment_id', '=', equipment.id), ('sf_maintenance_type', '=', '保养'), ('stage_id.done', '=', True), + ('active', '!=', False), ('close_date', '!=', False)], order="close_date desc", limit=1) if next_maintenance_todo and last_maintenance_done: next_date = next_maintenance_todo.request_date @@ -624,7 +689,7 @@ class SfMaintenanceEquipment(models.Model): if next_date < date_now: next_date = date_now else: - next_date = equipment.effective_date + timedelta(days=equipment.period) + next_date = equipment.initial_action_date + timedelta(days=equipment.period) equipment.next_action_date = next_date else: self.next_action_date = False @@ -635,11 +700,13 @@ class SfMaintenanceEquipment(models.Model): ('equipment_id', '=', equipment.id), ('sf_maintenance_type', '=', '检修'), ('stage_id.done', '!=', True), + ('active', '!=', False), ('close_date', '=', False)], order="request_date asc", limit=1) last_maintenance_done = self.env['maintenance.request'].search([ ('equipment_id', '=', equipment.id), ('sf_maintenance_type', '=', '检修'), ('stage_id.done', '=', True), + ('active', '!=', False), ('close_date', '!=', False)], order="close_date desc", limit=1) if next_maintenance_todo and last_maintenance_done: next_date = next_maintenance_todo.request_date @@ -668,7 +735,7 @@ class SfMaintenanceEquipment(models.Model): if next_date < date_now: next_date = date_now else: - next_date = equipment.effective_date + timedelta(days=equipment.overhaul_period) + next_date = equipment.initial_overhaul_date + timedelta(days=equipment.overhaul_period) equipment.overhaul_date = next_date else: self.overhaul_date = False @@ -735,6 +802,7 @@ class SfMaintenanceEquipment(models.Model): next_requests = self.env['maintenance.request'].search([('stage_id.done', '=', False), ('equipment_id', '=', equipment.id), ('maintenance_type', '=', 'preventive'), + ('active', '=', True), ('request_date', '=', equipment.next_action_date), ('sf_maintenance_type', '=', '保养')]) if not next_requests: @@ -743,6 +811,7 @@ class SfMaintenanceEquipment(models.Model): next_requests = self.env['maintenance.request'].search([('stage_id.done', '=', False), ('equipment_id', '=', equipment.id), ('maintenance_type', '=', 'preventive'), + ('active', '=', True), ('request_date', '=', equipment.overhaul_date), ('sf_maintenance_type', '=', '检修')]) if not next_requests: diff --git a/sf_maintenance/models/sf_maintenance_requests.py b/sf_maintenance/models/sf_maintenance_requests.py index 8686926b..397c8c90 100644 --- a/sf_maintenance/models/sf_maintenance_requests.py +++ b/sf_maintenance/models/sf_maintenance_requests.py @@ -14,6 +14,8 @@ class SfMaintenanceEquipmentCategory(models.Model): equipment_maintenance_id = fields.Many2one('equipment.maintenance.standards', string='设备维保标准', domain="[('maintenance_type','=',sf_maintenance_type)]") + active = fields.Boolean('有效', default=True) + @api.onchange('sf_maintenance_type') def _compute_equipment_maintenance_request_id(self): for record in self: diff --git a/sf_maintenance/security/ir.model.access.csv b/sf_maintenance/security/ir.model.access.csv index abbd4878..9a20f67f 100644 --- a/sf_maintenance/security/ir.model.access.csv +++ b/sf_maintenance/security/ir.model.access.csv @@ -20,7 +20,8 @@ access_maintenance_equipment_agv_log,maintenance_equipment_agv_log,model_mainten -access_maintenance_system_user,equipment.request system user,maintenance.model_maintenance_request,base.group_user,1,0,0,0 +access_maintenance_system_user,equipment.request system user,maintenance.model_maintenance_request,base.group_user,1,1,1,0 +access_maintenance_wizard_system_user,maintenance.request.wizard system user,model_maintenance_request_wizard,base.group_user,1,1,1,0 access_maintenance_equipment_group_plan_dispatch,maintenance.equipment,maintenance.model_maintenance_equipment,sf_base.group_plan_dispatch,1,0,0,0 access_maintenance_equipment_oee_group_plan_dispatch,maintenance_equipment_oee,model_maintenance_equipment_oee,sf_base.group_plan_dispatch,1,0,0,0 diff --git a/sf_maintenance/views/maintenance_views.xml b/sf_maintenance/views/maintenance_views.xml index 8c6cb40d..4fe044d3 100644 --- a/sf_maintenance/views/maintenance_views.xml +++ b/sf_maintenance/views/maintenance_views.xml @@ -60,9 +60,9 @@ - + + domain="[('brand_id', '=', brand_id)]"/> @@ -73,7 +73,7 @@ + options="{'no_create': True}"/>