From a36898d6d573ac8045266046d0fc5d6f1003784e Mon Sep 17 00:00:00 2001 From: scampbell Date: Thu, 17 Oct 2019 19:11:26 -0700 Subject: [PATCH] [IMP] Connector Equipment Service Update --- .../models/maintenance_equipment.py | 2 +- .../models/agreement_serviceprofile.py | 253 ++++++++++++++---- .../models/maintenance_equipment.py | 15 +- 3 files changed, 213 insertions(+), 57 deletions(-) diff --git a/connector_equipment/models/maintenance_equipment.py b/connector_equipment/models/maintenance_equipment.py index 3409e26..9e0857d 100644 --- a/connector_equipment/models/maintenance_equipment.py +++ b/connector_equipment/models/maintenance_equipment.py @@ -39,7 +39,7 @@ def _connect(self, function, serviceprofiles=None): for sp_id in serviceprofiles: self.remove_service(sp_id) - @api.multi + @api.model def create(self, vals): res = super().create(vals) if self.managed: diff --git a/connector_equipment_service/models/agreement_serviceprofile.py b/connector_equipment_service/models/agreement_serviceprofile.py index e5ea8f8..186beed 100644 --- a/connector_equipment_service/models/agreement_serviceprofile.py +++ b/connector_equipment_service/models/agreement_serviceprofile.py @@ -7,70 +7,221 @@ class AgreementServiceProfile(models.Model): _inherit = ['agreement.serviceprofile'] + + # DO NOT REMOVE UNTIL [WIP] IS READY TO BE TAKEN OFF + # + # @api.multi + # def write(self, vals): + # # If equipment was empty and now set + # if vals.get('equipment_id', False) and not self.equipment_id: + # new = self.env['maintenance.equipment'].\ + # browse(vals.get('equipment_id')) + # # If new equipment is managed + # if new.managed: + # new._connect('add_service', serviceprofiles=self) + # self.message_post(body=_('Added Service')) - @api.multi - def write(self, vals): - # If equipment was empty and now set to managed equipment - if vals.get('equipment_id', False) and not self.equipment_id: + # # If SP is changed but not the managed equipment + # if 'equipment_id' not in vals and self.equipment_id.managed: + # self.equipment_id._connect('update_service', + # serviceprofiles=self) + # self.message_post(body=_('Updated Service')) + + # # If SP state -> In Progress and equipment is managed and not changed + # if (vals.get('stage_id', False) == self.env. + # ref('agreement_serviceprofile.servpro_stage_progress').id and + # self.equipment_id.managed): + # self.equipment_id._connect('activate_service', + # serviceprofiles=self) + # self.message_post(body=_('Activated Service')) + + # # If SP state is changed to Suspend and equipment is managed + # if (vals.get('stage_id', False) == self.env. + # ref('agreement_serviceprofile.servpro_stage_suspend').id and + # self.equipment_id.managed): + # self.equipment_id._connect('suspend_service', + # serviceprofiles=self) + # self.message_post(body=_('Suspended Service')) + + # # If SP state is changed to Closed or Cancelled and equipment is managed + # if (vals.get('stage_id', False) in [self.env. + # ref('agreement_serviceprofile.servpro_stage_close').id, self.env. + # ref('agreement_serviceprofile.servpro_stage_cancel').id] and + # self.equipment_id.managed): + # self.equipment_id._connect('suspend_service', + # serviceprofiles=self) + # self.equipment_id._connect('remove_service', + # serviceprofiles=self) + # self.message_post(body=_('Suspended Service')) + + # # If equipment was a managed equipment and is changed to False + # if not vals.get('stage_id', False) and self.equipment_id.managed: + # self.equipment_id._connect('remove_service', + # serviceprofiles=self) + # self.message_post(body=_('Removed Service')) + + # # If equipment is changed to another equipment + # if vals.get('equipment_id', False) and self.equipment_id: + # # If previous equipment is managed + # if self.equipment_id.managed: + # # SP is Active (or going to be) + # if ((self.stage_id.id == self.env. + # ref('agreement_serviceprofile.servpro_stage_progress').id and + # not vals.get('stage_id', False)) + # or vals.get('stage_id', False) == self.env. + # ref('agreement_serviceprofile.servpro_stage_progress').id): + # self.equipment_id._connect('suspend_service', + # serviceprofiles=self) + # self.message_post(body=_('Previous Service Suspended')) + # else: + # self.equipment_id._connect('remove_service', + # serviceprofiles=self) + # self.message_post(body=_('Previous Service Removed')) + # # If new equipment is managed and SP is Active (or going to be) + # new = self.env['maintenance.equipment'].\ + # browse(vals.get('equipment_id')) + # if new.managed: + # # SP is Active (or going to be) + # if ((self.stage_id.id == self.env. + # ref('agreement_serviceprofile.servpro_stage_progress').id and + # not vals.get('stage_id', False)) + # or vals.get('stage_id', False) == self.env. + # ref('agreement_serviceprofile.servpro_stage_progress').id): + # new._connect('activate_service', serviceprofiles=self) + # self.message_post(body=_('New Service Activated')) + # else: + # new._connect('add_service', serviceprofiles=self) + # self.message_post(body=_('New Service Added')) + # return super().write(vals) + + @api.model + def create(self, vals): + # If SP is created with a managed equipment + if vals.get('equipment_id', False): new = self.env['maintenance.equipment'].\ browse(vals.get('equipment_id')) new._connect('add_service', serviceprofiles=self) + return super().create(vals) + + @api.multi + def write(self, vals): + equip_id = self.get_equip(vals) + # Add Service + # If equipment was empty and now set to managed or stage in draft + if equip_id and (not self.equipment_id or + self.get_stage(vals) == 'draft'): + equip_id._connect('add_service', serviceprofiles=self) self.message_post(body=_('Added Service')) + + # Update Service # If SP is changed but not the managed equipment - if 'equipment_id' not in vals and self.equipment_id.managed: + if 'equipment_id' not in vals and equip_id: self.equipment_id._connect('update_service', serviceprofiles=self) self.message_post(body=_('Updated Service')) - # If SP state is changed to In Progress and equipment is managed - if (vals.get('stage_id', False) == self.env. - ref('contract.servpro_stage_progress').id and - self.equipment_id.managed): - self.equipment_id._connect('activate_service', + + # Activate Service (Provision?) + # If SP state -> In Progress and equipment is managed + if self.get_stage(vals) == 'in_progress' and equip_id: + equip_id._connect('activate_service', serviceprofiles=self) self.message_post(body=_('Activated Service')) - # If SP state is changed to Suspend and equipment is managed - if (vals.get('stage_id', False) == self.env. - ref('contract.servpro_stage_suspend').id and - self.equipment_id.managed): - self.equipment_id._connect('suspend_service', + + # Suspend Service + # If SP state -> Suspend and equipment is managed + if self.get_stage(vals) == 'suspend' and equip_id: + equip_id._connect('suspend_service', serviceprofiles=self) self.message_post(body=_('Suspended Service')) - # If equipment was a managed equipment and is changed to False - if 'equipment_id' in vals and self.equipment_id.managed: - if not vals['equipment_id']: - self.equipment_id._connect('remove_service', - serviceprofiles=self) - self.message_post(body=_('Removed Service')) - # If equipment is changed to another equipment - if vals.get('equipment_id', False) and self.equipment_id: - # If previous equipment is managed - if self.equipment_id.managed: - # SP is Active (or going to be) - if ((self.stage_id.id == self.env. - ref('contract.servpro_stage_progress').id and - not vals.get('stage_id', False)) - or vals.get('stage_id', False) == self.env. - ref('contract.servpro_stage_progress').id): - self.equipment_id._connect('suspend_service', + + # Suspend/Remove Service + # If SP state -> Closed or Cancelled and equipment is managed + if self.get_stage(vals) in ['closed', 'cancelled'] and equip_id: + equip_id._connect('suspend_service', + serviceprofiles=self) + equip_id._connect('remove_service', + serviceprofiles=self) + self.message_post(body=_('Suspended Service')) + self.message_post(body=_('Removed Service')) + + # If equipment was changed, handle old equipment accordingly + if vals.get('equipment_id', False): + self.equip_changed(vals) + + return super().write(vals) + + #This method handles the old equipment if it is changed + def equip_changed(self, vals): + # Was the old Equipment Managed? + if self.equipment_id.managed: + # Is the SP In Progress (or going to be) + if self.get_stage(vals) in ['in_progress', 'to_renew']: + # Suspend + self.equipment_id._connect('suspend_service', serviceprofiles=self) - self.message_post(body=_('Previous Service Suspended')) - else: - self.equipment_id._connect('remove_service', + self.message_post(body=_('Previous Service Suspended')) + # SP is not In Progress (or going to be) + else: + # Remove + self.equipment_id._connect('remove_service', serviceprofiles=self) - self.message_post(body=_('Previous Service Removed')) - # If new equipment is managed and SP is Active (or going to be) - new = self.env['maintenance.equipment'].\ + self.message_post(body=_('Previous Service Removed')) + + # This method returns the final equipment on the form + # If there is a managed equipment in vals, use it + # If there is not, check self for managed equipment + # If neither, return False + def get_equip(self, vals): + equip = vals.get('equipment_id', False) + if equip: + equip = self.env['maintenance.equipment'].\ browse(vals.get('equipment_id')) - if new.managed: - # SP is Active (or going to be) - if ((self.stage_id.id == self.env. - ref('contract.servpro_stage_progress').id and - not vals.get('stage_id', False)) - or vals.get('stage_id', False) == self.env. - ref('contract.servpro_stage_progress').id): - new._connect('activate_service', serviceprofiles=self) - self.message_post(body=_('New Service Activated')) - else: - new._connect('add_service', serviceprofiles=self) - self.message_post(body=_('New Service Added')) - return super().write(vals) + if equip.managed: + return equip + else: + if self.equipment_id.managed: + return self.equipment_id + return False + + # This method returns the appriopriate stage_id + # If there is a stage in vals, use it + # If there is no stage in vals, use the current stage + def get_stage(self, vals): + x = '' + if ((vals.get('stage_id', False) == self.env. + ref('agreement_serviceprofile.servpro_stage_draft').id) or + (not vals.get('stage_id', False) and + self.stage_id.id == self.env. + ref('agreement_serviceprofile.servpro_stage_draft').id)): + x = 'draft' + if ((vals.get('stage_id', False) == self.env. + ref('agreement_serviceprofile.servpro_stage_progress').id) or + (not vals.get('stage_id', False) and + self.stage_id.id == self.env. + ref('agreement_serviceprofile.servpro_stage_progress').id)): + x = 'in_progress' + if ((vals.get('stage_id', False) == self.env. + ref('agreement_serviceprofile.servpro_stage_suspend').id) or + (not vals.get('stage_id', False) and + self.stage_id.id == self.env. + ref('agreement_serviceprofile.servpro_stage_suspend').id)): + x = 'suspend' + if ((vals.get('stage_id', False) == self.env. + ref('agreement_serviceprofile.servpro_stage_renew').id) or + (not vals.get('stage_id', False) and + self.stage_id.id == self.env. + ref('agreement_serviceprofile.servpro_stage_renew').id)): + x = 'to_renew' + if ((vals.get('stage_id', False) == self.env. + ref('agreement_serviceprofile.servpro_stage_close').id) or + (not vals.get('stage_id', False) and + self.stage_id.id == self.env. + ref('agreement_serviceprofile.servpro_stage_close').id)): + x = 'closed' + if ((vals.get('stage_id', False) == self.env. + ref('agreement_serviceprofile.servpro_stage_cancel').id) or + (not vals.get('stage_id', False) and + self.stage_id.id == self.env. + ref('agreement_serviceprofile.servpro_stage_cancel').id)): + x = 'cancel' + return x diff --git a/connector_equipment_service/models/maintenance_equipment.py b/connector_equipment_service/models/maintenance_equipment.py index 89569f4..9a64eb8 100644 --- a/connector_equipment_service/models/maintenance_equipment.py +++ b/connector_equipment_service/models/maintenance_equipment.py @@ -10,20 +10,25 @@ class MaintenanceEquipment(models.Model): @api.multi def add_service(self, service_profile): - raise NotImplementedError() + return True + # raise NotImplementedError() @api.multi def update_service(self, service_profile): - raise NotImplementedError() + return True + # raise NotImplementedError() @api.multi def activate_service(self, service_profile): - raise NotImplementedError() + return True + # raise NotImplementedError() @api.multi def suspend_service(self, service_profile): - raise NotImplementedError() + return True + # raise NotImplementedError() @api.multi def remove_service(self, service_profile): - raise NotImplementedError() + return True + # raise NotImplementedError()