# -*- coding: utf-8 -*- from odoo import api, fields, models, _ class HelpdeskWorkflowTemplateApplyWizard(models.TransientModel): _name = 'helpdesk.workflow.template.apply.wizard' _description = 'Apply Workflow Template Wizard' team_id = fields.Many2one( 'helpdesk.team', string='Team', required=True, readonly=True ) workflow_template_id = fields.Many2one( 'helpdesk.workflow.template', string='Workflow Template', required=True, domain="[('active', '=', True)]" ) replace_existing = fields.Boolean( string='Replace Existing Stages and SLAs', default=False, help='If checked, existing stages and SLAs will be removed before applying the template' ) stage_count = fields.Integer( string='Stages to Create', compute='_compute_counts', store=False ) sla_count = fields.Integer( string='SLA Policies to Create', compute='_compute_counts', store=False ) existing_stage_count = fields.Integer( string='Existing Stages', compute='_compute_counts', store=False ) existing_sla_count = fields.Integer( string='Existing SLA Policies', compute='_compute_counts', store=False ) @api.depends('workflow_template_id', 'team_id') def _compute_counts(self): for wizard in self: if wizard.workflow_template_id: wizard.stage_count = len(wizard.workflow_template_id.stage_template_ids) wizard.sla_count = len(wizard.workflow_template_id.sla_template_ids) else: wizard.stage_count = 0 wizard.sla_count = 0 if wizard.team_id: wizard.existing_stage_count = len(wizard.team_id.stage_ids) wizard.existing_sla_count = len( self.env['helpdesk.sla'].search([('team_id', '=', wizard.team_id.id)]) ) else: wizard.existing_stage_count = 0 wizard.existing_sla_count = 0 @api.model def default_get(self, fields_list): """Set default team from context""" defaults = super().default_get(fields_list) if 'team_id' in fields_list and 'active_id' in self.env.context: defaults['team_id'] = self.env.context['active_id'] if 'workflow_template_id' in fields_list and defaults.get('team_id'): team = self.env['helpdesk.team'].browse(defaults['team_id']) if team.workflow_template_id: defaults['workflow_template_id'] = team.workflow_template_id.id return defaults def action_apply_template(self): """Apply the workflow template to the team""" self.ensure_one() if not self.workflow_template_id: raise ValueError(_("Please select a workflow template")) if not self.team_id: raise ValueError(_("Team is required")) team = self.team_id # Replace existing if requested if self.replace_existing: # Remove existing SLAs existing_slas = self.env['helpdesk.sla'].search([('team_id', '=', team.id)]) existing_slas.unlink() # Remove existing stages (only if they belong only to this team) for stage in team.stage_ids: if len(stage.team_ids) == 1 and stage.team_ids.id == team.id: stage.unlink() else: # Just remove from this team team.stage_ids = [(3, stage.id)] # Set template on team team.workflow_template_id = self.workflow_template_id # Apply template result = team.apply_workflow_template() return result