helpdesk_workflow_template_apply_wizard.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # -*- coding: utf-8 -*-
  2. from odoo import api, fields, models, _
  3. class HelpdeskWorkflowTemplateApplyWizard(models.TransientModel):
  4. _name = 'helpdesk.workflow.template.apply.wizard'
  5. _description = 'Apply Workflow Template Wizard'
  6. team_id = fields.Many2one(
  7. 'helpdesk.team',
  8. string='Team',
  9. required=True,
  10. readonly=True
  11. )
  12. workflow_template_id = fields.Many2one(
  13. 'helpdesk.workflow.template',
  14. string='Workflow Template',
  15. required=True,
  16. domain="[('active', '=', True)]"
  17. )
  18. replace_existing = fields.Boolean(
  19. string='Replace Existing Stages and SLAs',
  20. default=False,
  21. help='If checked, existing stages and SLAs will be removed before applying the template'
  22. )
  23. stage_count = fields.Integer(
  24. string='Stages to Create',
  25. compute='_compute_counts',
  26. store=False
  27. )
  28. sla_count = fields.Integer(
  29. string='SLA Policies to Create',
  30. compute='_compute_counts',
  31. store=False
  32. )
  33. existing_stage_count = fields.Integer(
  34. string='Existing Stages',
  35. compute='_compute_counts',
  36. store=False
  37. )
  38. existing_sla_count = fields.Integer(
  39. string='Existing SLA Policies',
  40. compute='_compute_counts',
  41. store=False
  42. )
  43. @api.depends('workflow_template_id', 'team_id')
  44. def _compute_counts(self):
  45. for wizard in self:
  46. if wizard.workflow_template_id:
  47. wizard.stage_count = len(wizard.workflow_template_id.stage_template_ids)
  48. wizard.sla_count = len(wizard.workflow_template_id.sla_template_ids)
  49. else:
  50. wizard.stage_count = 0
  51. wizard.sla_count = 0
  52. if wizard.team_id:
  53. wizard.existing_stage_count = len(wizard.team_id.stage_ids)
  54. wizard.existing_sla_count = len(
  55. self.env['helpdesk.sla'].search([('team_id', '=', wizard.team_id.id)])
  56. )
  57. else:
  58. wizard.existing_stage_count = 0
  59. wizard.existing_sla_count = 0
  60. @api.model
  61. def default_get(self, fields_list):
  62. """Set default team from context"""
  63. defaults = super().default_get(fields_list)
  64. if 'team_id' in fields_list and 'active_id' in self.env.context:
  65. defaults['team_id'] = self.env.context['active_id']
  66. if 'workflow_template_id' in fields_list and defaults.get('team_id'):
  67. team = self.env['helpdesk.team'].browse(defaults['team_id'])
  68. if team.workflow_template_id:
  69. defaults['workflow_template_id'] = team.workflow_template_id.id
  70. return defaults
  71. def action_apply_template(self):
  72. """Apply the workflow template to the team"""
  73. self.ensure_one()
  74. if not self.workflow_template_id:
  75. raise ValueError(_("Please select a workflow template"))
  76. if not self.team_id:
  77. raise ValueError(_("Team is required"))
  78. team = self.team_id
  79. # Replace existing if requested
  80. if self.replace_existing:
  81. # Remove existing SLAs
  82. existing_slas = self.env['helpdesk.sla'].search([('team_id', '=', team.id)])
  83. existing_slas.unlink()
  84. # Remove existing stages (only if they belong only to this team)
  85. for stage in team.stage_ids:
  86. if len(stage.team_ids) == 1 and stage.team_ids.id == team.id:
  87. stage.unlink()
  88. else:
  89. # Just remove from this team
  90. team.stage_ids = [(3, stage.id)]
  91. # Set template on team
  92. team.workflow_template_id = self.workflow_template_id
  93. # Apply template
  94. result = team.apply_workflow_template()
  95. return result