helpdesk_ticket.py 17.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
from odoo import models, fields, api, _, SUPERUSER_ID
import random
from datetime import datetime, timedelta
from odoo.tools import email_re, email_escape_char, email_split
from odoo.exceptions import UserError,ValidationError
from odoo.tools import email_re
import uuid
import pytz
import logging
_logger = logging.getLogger(__name__)



class HelpdeskTicket(models.Model):
    _inherit = 'helpdesk.ticket'

17
    erp_id = fields.Many2one('erp.modules','Módulo')
18 19 20 21
    incidence_type = fields.Selection([
        ('odoo', 'Falla Odoo Nacional'),
        ('mclick','Falla Morsa Click Nacional'),
        ('suc','Sucursal detenida'),
22 23
        ('internet_fail','Falla General de Internet'),
        ('slow_odoo','Lentitud en Odoo'),
24 25 26
    ],string="Incidencia")
    warehouse_id = fields.Many2one('warehouse.helpdesk','Sucursal')
    call_before_10 = fields.Boolean('Atención antes de 10 minutos')
Arturo Jasso Origel committed
27
    color  = fields.Integer('Color',compute="_get_color")
28
    erp_type = fields.Selection([
29 30 31
        ('soporte','Soporte'),
        ('mejora','Mejora'),
        ('reporte','Reporte')
32
    ],string="Tipo de asistencia")
Arturo Jasso Origel committed
33
    ticket_reason = fields.Many2one('ticket.reason','Motivo de Ticket',
34
        default=lambda self: self.env['ticket.reason'].search([('name','=','Ninguno'),],limit=1).id
35 36 37 38
    )
    problem_solved_on_time = fields.Boolean('¿Solucionamos complementame tu problema?')
    good_service_actitude = fields.Boolean('¿Ha sido buena nuestra actitud de Servicio?')
    begin_before_ten_min  = fields.Boolean('¿COmenzamos a atenderte antes de 10 minutos?')
39 40 41
    is_support = fields.Boolean('Es soporte',compute='_compute_ticket_type')
    is_erp = fields.Boolean('Es ERP',compute='_compute_ticket_type')
    is_emergency = fields.Boolean('Es 911',compute='_compute_ticket_type') 
42 43 44 45
    user_branch_id = fields.Many2one('warehouse.helpdesk', related="partner_id.user_ids.warehouse_id", string='Usuario Sucursal', store=True)
    user_region_id = fields.Many2one('region.helpdesk', related="user_branch_id.region_id", string="Región", store=True,)
    user_id = fields.Many2one('res.users',string="Usuario Asignado", tracking=True,domain="[('id','in',team_members)]")
    team_members = fields.Many2many('res.users',relation="team_members_user_rel",related="team_id.team_members")
46 47 48 49 50 51
    module_route = fields.Char("Ruta de la transacción")
    process_text = fields.Char("¿En que proceso utilizas la transacción?")
    required_validation = fields.Char("Cuales son las validaciones que se requieren")
    common_scenery = fields.Char("¿Qué escenarios son comunes a trabajar con esta transacción?")
    module_impact = fields.Char("¿Tu solicitud, impacta de algún modo a algún otro Módulo?")
    system_id = fields.Many2one('erp.system','Sistema')
52
    team_admin_id = fields.Many2one('res.users',string="Administrador del equipo",related='team_id.team_admin_id')
53 54
    comp_morsa = fields.Selection(related="partner_id.user_ids.comp_morsa",string='Empresa')
    
55
    
56 57 58 59
    def _get_color(self):
        in_progress_stage = self.env.ref('sh_all_in_one_helpdesk.in_progress_stage').id
        new_stage = self.env.ref('sh_all_in_one_helpdesk.new_stage').id
        ten_min = self.env.ref('helpdesk_morsa.response_10_minutes').id
Arturo Jasso Origel committed
60 61 62 63 64

        simple_time = float(self.env['ir.config_parameter'].sudo().get_param('helpdesk_morsa.time_simple_alert'))
        medium_time = float(self.env['ir.config_parameter'].sudo().get_param('helpdesk_morsa.time_medium_alert'))
        extreme_time = float(self.env['ir.config_parameter'].sudo().get_param('helpdesk_morsa.time_extreme_alert'))

65
        for rec in self:
66 67
            if rec.team_head:
                rec.color = 10
68
                if rec.is_support == True or rec.is_emergency == True:
69 70 71 72 73 74
                    if fields.Datetime.now() >= rec.create_date+timedelta(hours=simple_time) and fields.Datetime.now() < rec.create_date+timedelta(hours=medium_time) and rec.stage_id.id in [new_stage]:
                        rec.color = self.env['ir.config_parameter'].sudo().get_param('helpdesk_morsa.color_simple_alert')
                    elif fields.Datetime.now() >= rec.create_date+timedelta(hours=medium_time) and fields.Datetime.now() < rec.create_date+timedelta(hours=extreme_time) and rec.stage_id.id in [new_stage,ten_min]:
                        rec.color = self.env['ir.config_parameter'].sudo().get_param('helpdesk_morsa.color_medium_alert')
                    elif fields.Datetime.now() >= rec.create_date+timedelta(hours=extreme_time) and rec.stage_id.id in [new_stage,ten_min,in_progress_stage]:
                        rec.color = self.env['ir.config_parameter'].sudo().get_param('helpdesk_morsa.color_extreme_alert')
75
            else:
76 77
                rec.color = 10
                    
78

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    @api.depends('team_id')
    def _compute_ticket_type(self):
        for ticket in self:
            if ticket.team_id.id == self.env.ref('helpdesk_morsa.techinnical_support').id:
                ticket.is_support = True
            else:
                ticket.is_support = False
                
            if ticket.team_id.id == self.env.ref('helpdesk_morsa.erp_asistense').id:
                ticket.is_erp = True
            else:
                ticket.is_erp = False

            if ticket.team_id.id == self.env.ref('helpdesk_morsa.ti_emergency').id:
                ticket.is_emergency = True
            else:
                ticket.is_emergency = False
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
           
    def assign_me(self):
        self.user_id = self.env.user.id


    @api.onchange('team_id')
    def onchange_ticket_type(self):
        if self.team_id and not self.ticket_type:
            self.ticket_type = self.team_id.helpdesk_ticket_type_id.id
        elif self.team_id and self.ticket_type:
            self.ticket_type = self.team_id.helpdesk_ticket_type_id.id

    @api.onchange('ticket_type')
    def onchange_ticket_team(self):
        if self.ticket_type and not self.team_id:
            self.team_id = self.env['helpdesk.team'].search([('helpdesk_ticket_type_id','=',self.ticket_type.id)]).id
        elif self.team_id and self.ticket_type:
            self.team_id = self.env['helpdesk.team'].search([('helpdesk_ticket_type_id','=',self.ticket_type.id)]).id
114
          
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172

    @api.onchange('partner_id')
    def onchange_partner_simple_info(self):
        if self.partner_id:
            if self.partner_id.phone:
                self.mobile_no = self.partner_id.phone
            elif self.partner_id.mobile:
                self.mobile_no = self.partner_id.mobile

    
    def _compute_access_url(self):
        super(HelpdeskTicket, self)._compute_access_url()
        for ticket in self:
            if ticket.team_id.id == self.env.ref('helpdesk_morsa.techinnical_support').id:
                ticket.access_url = '/my/soporte/%s' % (ticket.id)
            elif ticket.team_id.id == self.env.ref('helpdesk_morsa.erp_asistense').id:
                ticket.access_url = '/my/erp/%s' % (ticket.id)
            elif ticket.team_id.id == self.env.ref('helpdesk_morsa.ti_emergency').id:
                ticket.access_url = '/my/911/%s' % (ticket.id)

    @api.model
    def create(self, vals):
        if vals.get('partner_id') == False and vals.get('email', False):
            emails = email_re.findall(vals.get('email') or '')
            email = emails and emails[0] or ''
            name = str(vals.get('email')).split('"')
            partner_id = self.env['res.partner'].create({
                'name':
                name[1],
                'email':
                email,
                'company_type':
                'person',
            })
            vals.update({
                'partner_id': partner_id.id,
                'email': email,
                'person_name': partner_id.name,
            })
        if self.env.company.sh_default_team_id and not vals.get(
                'team_id') and not vals.get('user_id'):
            vals.update({
                'team_id':
                self.env.company.sh_default_team_id.id,
                'team_head':
                self.env.company.sh_default_team_id.team_head.id,
                'user_id':
                self.env.company.sh_default_user_id.id,
            })
        
        company_id = self.env.company
        if 'company_id' in vals:
            self = self.with_company(vals['company_id'])

        if 'team_id' in vals:
            team = self.env['helpdesk.team'].sudo().search([('id','=',vals['team_id'])])
            if team.helpdesk_ticket_type_id:
                vals['ticket_type'] = team.helpdesk_ticket_type_id.id
Arturo Jasso Origel committed
173
                vals['team_head'] = team.team_head.id
174 175 176 177 178 179 180
            else:
                raise ValidationError(('No se pueden generar ticket, no cuenta con la configuración necesaria'))
            vals['name'] = self.env['ir.sequence'].sudo().next_by_code('helpdesk.ticket.'+str(team.id) or _('New'))
        if company_id.new_stage_id:
            vals['stage_id'] = company_id.new_stage_id.id

        vals['color'] = 10
181
        
182 183 184 185 186 187 188 189 190 191 192 193
        res = super(HelpdeskTicket, self).create(vals)
        
        if res.sh_sla_status_ids:
            for line in res.sh_sla_status_ids:
                line.sh_status = res.sh_status
        if res.ticket_from_website and res.company_id.new_stage_id.mail_template_ids and res.partner_id:
            for template in res.company_id.new_stage_id.mail_template_ids:
                template.sudo().send_mail(res.id, force_send=True)
        else:
            if not res.ticket_from_website and res.company_id.new_stage_id.mail_template_ids and res.partner_id:
                for template in res.company_id.new_stage_id.mail_template_ids:
                    template.sudo().send_mail(res.id, force_send=True)
194
        if res.team_id and res.team_head and not res.user_id:
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
            allocation_template = res.company_id.allocation_mail_template_id
            email_formatted = []
            if res.team_head.partner_id.email_formatted not in email_formatted:
                email_formatted.append(
                    res.team_head.partner_id.email_formatted)
            email_formatted_str = ','.join(email_formatted)
            email_values = {
                'email_from': str(res.team_head.partner_id.email_formatted),
                'email_to': email_formatted_str
            }
            if allocation_template:
                allocation_template.sudo().send_mail(res.id,
                                                     force_send=True,
                                                     email_values=email_values)
                res.ticket_allocated = True
210
        elif res.team_id and res.team_head and res.user_id:
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
            allocation_template = res.company_id.allocation_mail_template_id
            email_formatted = []
            if res.team_head.partner_id.email_formatted not in email_formatted:
                email_formatted.append(
                    res.team_head.partner_id.email_formatted)
            if res.user_id.partner_id.email_formatted not in email_formatted:
                email_formatted.append(res.user_id.partner_id.email_formatted)
            email_formatted_str = ','.join(email_formatted)
            email_values = {
                'email_from': str(res.team_head.partner_id.email_formatted),
                'email_to': email_formatted_str
            }
            if allocation_template:
                allocation_template.sudo().send_mail(res.id,
                                                     force_send=True,
                                                     email_values=email_values)
                res.ticket_allocated = True
        elif res.team_id and res.team_head and not res.user_id and res.sh_user_ids:
            allocation_template = res.company_id.allocation_mail_template_id
            email_formatted = []
            for user in res.sh_user_ids:
                if user.partner_id.email_formatted not in email_formatted:
                    email_formatted.append(user.partner_id.email_formatted)
            email_formatted_str = ','.join(email_formatted)
            email_values = {
                'email_from': str(res.team_head.partner_id.email_formatted),
                'email_to': email_formatted_str
            }
            if allocation_template:
                allocation_template.sudo().send_mail(res.id,
                                                     force_send=True,
                                                     email_values=email_values)
                res.ticket_allocated = True
        elif not res.team_id and not res.team_head and res.user_id and res.sh_user_ids:
            allocation_template = res.company_id.allocation_mail_template_id
            email_formatted = []
            if res.user_id.partner_id.email_formatted not in email_formatted:
                email_formatted.append(res.user_id.partner_id.email_formatted)
            for user in res.sh_user_ids:
                if user.id != res.user_id.id:
                    if user.partner_id.email_formatted not in email_formatted:
                        email_formatted.append(user.partner_id.email_formatted)
            email_formatted_str = ','.join(email_formatted)
            email_values = {
                'email_from': str(res.company_id.partner_id.email_formatted),
                'email_to': email_formatted_str
            }
            if allocation_template:
                allocation_template.sudo().send_mail(res.id,
                                                     force_send=True,
                                                     email_values=email_values)
                res.ticket_allocated = True
        elif not res.team_id and not res.team_head and res.user_id and not res.sh_user_ids:
            allocation_template = res.company_id.allocation_mail_template_id
            allocation_template.sudo().write({
                'email_from':
                str(res.company_id.partner_id.email_formatted),
                'email_to':
                str(res.user_id.partner_id.email_formatted),
                'partner_to':
                str(res.user_id.partner_id.id)
            })
            email_values = {
                'email_from': str(res.company_id.partner_id.email_formatted),
                'email_to': str(res.user_id.partner_id.email_formatted)
            }
            if allocation_template:
                allocation_template.sudo().send_mail(res.id,
                                                     force_send=True,
                                                     email_values=email_values)
                res.ticket_allocated = True
        elif not res.team_id and not res.team_head and not res.user_id and res.sh_user_ids:
            allocation_template = res.company_id.allocation_mail_template_id
            email_formatted = []
            for user in res.sh_user_ids:
                if user.partner_id.email_formatted not in email_formatted:
                    email_formatted.append(user.partner_id.email_formatted)
            email_formatted_str = ','.join(email_formatted)
            email_values = {
                'email_from': str(res.company_id.partner_id.email_formatted),
                'email_to': email_formatted_str
            }
            if allocation_template:
                allocation_template.sudo().send_mail(res.id,
                                                     force_send=True,
                                                     email_values=email_values)
                res.ticket_allocated = True
        if self.env.company.sh_auto_add_customer_as_follower:
            res.message_subscribe(partner_ids=res.partner_id.ids)
Arturo Jasso Origel committed
300
        res.create_initial_history_changes()
301
        res.append_sla_policies()
302 303 304
        return res


Arturo Jasso Origel committed
305 306 307 308 309 310 311 312 313 314
    def create_initial_history_changes(self):
        stage_history={
            'stage_task_id': self.id,
            'stage_name': self.stage_id.name,
            'date_in': datetime.now(),
            'date_in_by': self.env.user.id,
        }
        self.env['sh.helpdesk.ticket.stage.info'].sudo().create(stage_history)


315 316 317
    def append_sla_policies(self):
        sla = self.env['sh.helpdesk.sla'].search([
            ('sh_team_id','=',self.team_id.id),
318 319
            ('sh_ticket_type_id','=',self.ticket_type.id),
            ('sh_sla_target_type','=','reaching_stage'),
320 321 322 323 324 325 326
            ('sh_stage_id','!=',self.stage_id.id)
        ]).ids
        if sla:
            self.update({
                'sh_sla_policy_ids':[(6,0,sla)]
            })

327
    def write(self,vals):
328 329 330 331 332 333 334
        if self.is_support == True or self.is_erp == True or self.is_emergency == True:
            if "stage_id" in vals:
                if self.description == '<p><br></p>' or self.description==False:
                    raise ValidationError (_("Para cambiar de etapa, es necesario agregar una descripción"))
            if 'description' in  vals and vals["description"] != "" and self.description != False and vals['description'] != '<p><br></p>':
                min_description = int(self.env['ir.config_parameter'].sudo().get_param('helpdesk_morsa.description_min_lenght'))
                description = self.get_description_len(vals["description"])
335
                
336 337 338 339 340
                if description-11 < min_description:
                    raise ValidationError (_("Favor de agregar al menos "+str(min_description)+" caracteres a la descripción"))
            return super(HelpdeskTicket, self).write(vals)
        else:
            return super(HelpdeskTicket, self).write(vals)
Arturo Jasso Origel committed
341
        
342 343 344 345 346
    def action_approve(self):
        if self.description == '<p><br></p>' or self.description==False:
            raise ValidationError (_("Para cambiar de etapa, es necesario agregar una descripción"))
        else:
            return super(HelpdeskTicket, self).action_approve()
347 348 349 350 351 352
        
    def get_description_len(self,description):
        try:
            return len(description)
        except Exception as e:
            return 0
353
    
354 355 356