2014-02-03 19:23:00 +00:00
|
|
|
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
2015-04-27 23:19:26 +00:00
|
|
|
module Ticket::Escalation
|
2013-08-16 14:30:51 +00:00
|
|
|
|
2013-08-17 16:09:19 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
rebuild escalations for all open tickets
|
|
|
|
|
|
|
|
result = Ticket::Escalation.rebuild_all
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = true
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
def self.rebuild_all
|
2015-09-10 19:09:50 +00:00
|
|
|
state_list_open = Ticket::State.by_category('open')
|
2013-08-16 14:30:51 +00:00
|
|
|
|
2015-09-10 19:09:50 +00:00
|
|
|
tickets = Ticket.where(state_id: state_list_open)
|
2015-05-07 10:25:16 +00:00
|
|
|
tickets.each(&:escalation_calculation)
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
2013-08-16 14:30:51 +00:00
|
|
|
|
2013-08-17 16:09:19 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
rebuild escalation for ticket
|
|
|
|
|
|
|
|
ticket = Ticket.find(123)
|
|
|
|
result = ticket.escalation_calculation
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = true
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
def escalation_calculation
|
2014-05-30 13:36:07 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# set escalation off if ticket is already closed
|
2016-02-23 09:19:06 +00:00
|
|
|
state = Ticket::State.lookup(id: state_id)
|
2015-09-10 19:09:50 +00:00
|
|
|
escalation_disabled = false
|
2015-04-27 23:19:26 +00:00
|
|
|
if state.ignore_escalation?
|
2015-09-10 19:09:50 +00:00
|
|
|
escalation_disabled = true
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
2013-08-16 14:30:51 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# get sla for ticket
|
2015-09-10 19:09:50 +00:00
|
|
|
calendar = nil
|
|
|
|
sla = escalation_calculation_get_sla
|
|
|
|
if sla
|
|
|
|
calendar = sla.calendar
|
|
|
|
end
|
2014-05-30 13:36:07 +00:00
|
|
|
|
2015-09-10 19:09:50 +00:00
|
|
|
# if no escalation is enabled
|
2015-09-23 00:29:13 +00:00
|
|
|
if !sla || !calendar
|
2014-05-30 13:36:07 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# nothing to change
|
2015-05-07 12:10:38 +00:00
|
|
|
return true if !escalation_time
|
2013-08-16 14:30:51 +00:00
|
|
|
|
2015-09-10 19:09:50 +00:00
|
|
|
self.escalation_time = nil
|
2016-02-23 09:19:06 +00:00
|
|
|
self.updated_by_id = 1
|
2015-09-10 19:09:50 +00:00
|
|
|
self.callback_loop = true
|
2015-05-07 12:10:38 +00:00
|
|
|
save
|
2015-04-27 23:19:26 +00:00
|
|
|
return true
|
|
|
|
end
|
2013-08-16 14:30:51 +00:00
|
|
|
|
2015-09-10 19:09:50 +00:00
|
|
|
# reset escalation attributes
|
|
|
|
self.escalation_time = nil
|
|
|
|
self.first_response_escal_date = nil
|
|
|
|
self.update_time_escal_date = nil
|
|
|
|
self.close_time_escal_date = nil
|
2013-08-16 14:30:51 +00:00
|
|
|
|
2015-09-10 19:09:50 +00:00
|
|
|
biz = Biz::Schedule.new do |config|
|
2015-09-22 23:22:45 +00:00
|
|
|
|
|
|
|
# get business hours
|
2015-09-20 23:13:06 +00:00
|
|
|
hours = {}
|
2016-06-30 20:04:48 +00:00
|
|
|
calendar.business_hours.each { |day, meta|
|
2015-09-20 23:13:06 +00:00
|
|
|
next if !meta[:active]
|
|
|
|
next if !meta[:timeframes]
|
|
|
|
hours[day.to_sym] = {}
|
2016-06-30 20:04:48 +00:00
|
|
|
meta[:timeframes].each { |frame|
|
2015-09-20 23:13:06 +00:00
|
|
|
next if !frame[0]
|
|
|
|
next if !frame[1]
|
|
|
|
hours[day.to_sym][frame[0]] = frame[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
config.hours = hours
|
2015-10-30 09:14:34 +00:00
|
|
|
if !hours || hours.empty?
|
2016-03-01 14:26:46 +00:00
|
|
|
raise "No congifure hours found in calendar #{calendar.inspect}"
|
2015-10-30 09:14:34 +00:00
|
|
|
end
|
2015-09-22 23:22:45 +00:00
|
|
|
|
|
|
|
# get holidays
|
|
|
|
holidays = []
|
|
|
|
if calendar.public_holidays
|
2016-06-30 20:04:48 +00:00
|
|
|
calendar.public_holidays.each { |day, meta|
|
2015-09-22 23:22:45 +00:00
|
|
|
next if !meta
|
|
|
|
next if !meta['active']
|
|
|
|
next if meta['removed']
|
|
|
|
holidays.push Date.parse(day)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
config.holidays = holidays
|
|
|
|
|
|
|
|
# get timezone
|
2015-09-10 19:09:50 +00:00
|
|
|
config.time_zone = calendar.timezone
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
2013-08-16 14:30:51 +00:00
|
|
|
|
2015-09-10 19:09:50 +00:00
|
|
|
# fist response
|
|
|
|
# calculate first response escalation
|
|
|
|
if sla.first_response_time
|
|
|
|
self.first_response_escal_date = biz.time(sla.first_response_time, :minutes).after(created_at)
|
|
|
|
pending_time = pending_minutes(created_at, first_response_escal_date, biz)
|
|
|
|
if pending_time && pending_time > 0
|
|
|
|
self.first_response_escal_date = biz.time(pending_time, :minutes).after(first_response_escal_date)
|
|
|
|
end
|
|
|
|
end
|
2013-08-16 14:30:51 +00:00
|
|
|
|
2015-09-10 19:09:50 +00:00
|
|
|
# get response time in min
|
|
|
|
if first_response
|
|
|
|
self.first_response_in_min = pending_minutes(created_at, first_response, biz, 'business_minutes')
|
|
|
|
else
|
|
|
|
self.escalation_time = first_response_escal_date
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
2013-08-16 14:30:51 +00:00
|
|
|
|
2015-09-10 19:09:50 +00:00
|
|
|
# set time to show if sla is raised or not
|
|
|
|
if sla.first_response_time && first_response_in_min
|
|
|
|
self.first_response_diff_in_min = sla.first_response_time - first_response_in_min
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
2013-08-16 14:30:51 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# update time
|
2015-09-10 19:09:50 +00:00
|
|
|
# calculate escalation
|
2015-10-30 09:14:34 +00:00
|
|
|
if !last_contact_customer && !last_contact_agent
|
2015-05-07 12:10:38 +00:00
|
|
|
last_update = created_at
|
2015-10-30 09:14:34 +00:00
|
|
|
elsif !last_contact_customer && last_contact_agent
|
|
|
|
last_update = last_contact_agent
|
|
|
|
elsif last_contact_customer && !last_contact_agent
|
|
|
|
last_update = last_contact_customer
|
|
|
|
elsif last_contact_agent > last_contact_customer
|
|
|
|
last_update = last_contact_agent
|
|
|
|
elsif last_contact_agent < last_contact_customer
|
|
|
|
last_update = last_contact_customer
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
2015-10-30 09:14:34 +00:00
|
|
|
if sla.update_time && last_update
|
2015-09-10 19:09:50 +00:00
|
|
|
self.update_time_escal_date = biz.time(sla.update_time, :minutes).after(last_update)
|
|
|
|
pending_time = pending_minutes(last_update, update_time_escal_date, biz)
|
|
|
|
if pending_time && pending_time > 0
|
|
|
|
self.update_time_escal_date = biz.time(pending_time, :minutes).after(update_time_escal_date)
|
|
|
|
end
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
2015-09-13 18:01:18 +00:00
|
|
|
if update_time_escal_date && ((!escalation_time && update_time_escal_date) || update_time_escal_date < escalation_time)
|
2015-09-11 08:46:53 +00:00
|
|
|
self.escalation_time = update_time_escal_date
|
2015-09-10 19:09:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# get update time in min
|
2015-10-30 09:14:34 +00:00
|
|
|
if last_update && last_update != created_at
|
|
|
|
self.update_time_in_min = pending_minutes(created_at, last_update, biz, 'business_minutes')
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
2013-08-16 14:30:51 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# set sla time
|
2015-09-10 19:09:50 +00:00
|
|
|
if sla.update_time && update_time_in_min
|
|
|
|
self.update_time_diff_in_min = sla.update_time - update_time_in_min
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
2013-08-16 14:30:51 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# close time
|
2015-09-10 19:09:50 +00:00
|
|
|
# calculate close time escalation
|
2015-09-20 23:13:06 +00:00
|
|
|
if sla.solution_time
|
|
|
|
self.close_time_escal_date = biz.time(sla.solution_time, :minutes).after(created_at)
|
2015-09-10 19:09:50 +00:00
|
|
|
pending_time = pending_minutes(created_at, first_response_escal_date, biz)
|
|
|
|
if pending_time && pending_time > 0
|
|
|
|
self.close_time_escal_date = biz.time(pending_time, :minutes).after(close_time_escal_date)
|
|
|
|
end
|
|
|
|
end
|
2015-04-27 23:19:26 +00:00
|
|
|
|
2015-09-10 19:09:50 +00:00
|
|
|
# get close time in min
|
|
|
|
if close_time
|
|
|
|
self.close_time_in_min = pending_minutes(created_at, close_time, biz, 'business_minutes')
|
2016-01-15 17:22:57 +00:00
|
|
|
elsif close_time_escal_date && ((!escalation_time && close_time_escal_date) || close_time_escal_date < escalation_time)
|
|
|
|
self.escalation_time = close_time_escal_date
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
2015-09-10 19:09:50 +00:00
|
|
|
|
|
|
|
# set time to show if sla is raised or not
|
2015-09-20 23:13:06 +00:00
|
|
|
if sla.solution_time && close_time_in_min
|
|
|
|
self.close_time_diff_in_min = sla.solution_time - close_time_in_min
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
2015-09-10 19:09:50 +00:00
|
|
|
|
|
|
|
if escalation_disabled
|
|
|
|
self.escalation_time = nil
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
2015-04-30 15:25:04 +00:00
|
|
|
|
2016-01-15 17:22:57 +00:00
|
|
|
return if !changed?
|
2015-04-30 15:25:04 +00:00
|
|
|
|
|
|
|
self.callback_loop = true
|
2016-02-23 09:19:06 +00:00
|
|
|
self.updated_by_id = 1
|
2015-05-07 12:10:38 +00:00
|
|
|
save
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
2013-08-16 14:30:51 +00:00
|
|
|
|
2013-08-25 23:04:41 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
return sla for ticket
|
|
|
|
|
|
|
|
ticket = Ticket.find(123)
|
|
|
|
result = ticket.escalation_calculation_get_sla
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = selected_sla
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
def escalation_calculation_get_sla
|
|
|
|
sla_selected = nil
|
2015-12-11 16:00:04 +00:00
|
|
|
sla_list = Cache.get('SLA::List::Active')
|
2015-05-07 10:11:45 +00:00
|
|
|
if sla_list.nil?
|
2016-01-19 06:58:58 +00:00
|
|
|
sla_list = Sla.all.order(:name, :created_at)
|
2015-12-11 16:00:04 +00:00
|
|
|
Cache.write('SLA::List::Active', sla_list, { expires_in: 1.hour })
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
2016-06-30 20:04:48 +00:00
|
|
|
sla_list.each { |sla|
|
2015-04-27 23:19:26 +00:00
|
|
|
if !sla.condition || sla.condition.empty?
|
|
|
|
sla_selected = sla
|
|
|
|
elsif sla.condition
|
2015-09-23 14:25:06 +00:00
|
|
|
query_condition, bind_condition = Ticket.selector2sql(sla.condition)
|
2015-12-11 16:00:04 +00:00
|
|
|
ticket = Ticket.where(query_condition, *bind_condition).find_by(id: id)
|
2015-09-22 23:22:45 +00:00
|
|
|
next if !ticket
|
|
|
|
sla_selected = sla
|
|
|
|
break
|
2013-08-16 14:30:51 +00:00
|
|
|
end
|
2015-04-27 23:19:26 +00:00
|
|
|
}
|
|
|
|
sla_selected
|
|
|
|
end
|
2015-04-27 21:27:51 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
private
|
2013-08-16 14:30:51 +00:00
|
|
|
|
2015-09-10 19:09:50 +00:00
|
|
|
# get business minutes of pending time
|
|
|
|
# type = business_minutes (pending time in business minutes)
|
|
|
|
# type = non_business_minutes (pending time in non business minutes)
|
|
|
|
def pending_minutes(start_time, end_time, biz, type = 'non_business_minutes')
|
2015-04-27 21:27:51 +00:00
|
|
|
|
2015-09-10 19:09:50 +00:00
|
|
|
working_time_in_min = 0
|
|
|
|
total_time_in_min = 0
|
|
|
|
last_state = nil
|
|
|
|
last_state_change = nil
|
2015-04-27 23:19:26 +00:00
|
|
|
last_state_is_pending = false
|
2015-09-10 19:09:50 +00:00
|
|
|
pending_minutes = 0
|
|
|
|
history_get.each { |history_item|
|
2015-04-27 23:19:26 +00:00
|
|
|
|
|
|
|
# ignore if it isn't a state change
|
|
|
|
next if !history_item['attribute']
|
|
|
|
next if history_item['attribute'] != 'state'
|
|
|
|
|
|
|
|
# ignore all newer state before start_time
|
|
|
|
next if history_item['created_at'] < start_time
|
|
|
|
|
|
|
|
# ignore all older state changes after end_time
|
|
|
|
next if last_state_change && last_state_change > end_time
|
|
|
|
|
|
|
|
# if created_at is later then end_time, use end_time as last time
|
|
|
|
if history_item['created_at'] > end_time
|
|
|
|
history_item['created_at'] = end_time
|
|
|
|
end
|
2015-04-27 21:27:51 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# get initial state and time
|
|
|
|
if !last_state
|
|
|
|
last_state = history_item['value_from']
|
|
|
|
last_state_change = start_time
|
|
|
|
end
|
2015-04-27 21:27:51 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# check if time need to be counted
|
|
|
|
counted = true
|
|
|
|
if history_item['value_from'] == 'pending reminder'
|
|
|
|
counted = false
|
|
|
|
elsif history_item['value_from'] == 'close'
|
|
|
|
counted = false
|
2013-08-16 14:30:51 +00:00
|
|
|
end
|
|
|
|
|
2015-09-10 19:09:50 +00:00
|
|
|
diff = biz.within(last_state_change, history_item['created_at']).in_minutes
|
2015-04-27 23:19:26 +00:00
|
|
|
if counted
|
|
|
|
# puts "Diff count #{history_item['value_from']} -> #{history_item['value_to']} / #{last_state_change} -> #{history_item['created_at']}"
|
2015-09-10 19:09:50 +00:00
|
|
|
working_time_in_min = working_time_in_min + diff
|
2015-05-07 20:49:15 +00:00
|
|
|
# else
|
|
|
|
# puts "Diff not count #{history_item['value_from']} -> #{history_item['value_to']} / #{last_state_change} -> #{history_item['created_at']}"
|
2013-08-16 14:30:51 +00:00
|
|
|
end
|
2015-09-10 19:09:50 +00:00
|
|
|
total_time_in_min = total_time_in_min + diff
|
2013-08-16 14:30:51 +00:00
|
|
|
|
2016-01-15 17:22:57 +00:00
|
|
|
last_state_is_pending = if history_item['value_to'] == 'pending reminder'
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
2015-04-27 23:19:26 +00:00
|
|
|
|
|
|
|
# remember for next loop last state
|
|
|
|
last_state = history_item['value_to']
|
|
|
|
last_state_change = history_item['created_at']
|
|
|
|
}
|
|
|
|
|
|
|
|
# if last state isnt pending, count rest
|
|
|
|
if !last_state_is_pending && last_state_change && last_state_change < end_time
|
2015-09-10 19:09:50 +00:00
|
|
|
diff = biz.within(last_state_change, end_time).in_minutes
|
|
|
|
working_time_in_min = working_time_in_min + diff
|
|
|
|
total_time_in_min = total_time_in_min + diff
|
2013-08-16 14:30:51 +00:00
|
|
|
end
|
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# if we have not had any state change
|
|
|
|
if !last_state_change
|
2015-09-10 19:09:50 +00:00
|
|
|
diff = biz.within(start_time, end_time).in_minutes
|
|
|
|
working_time_in_min = working_time_in_min + diff
|
|
|
|
total_time_in_min = total_time_in_min + diff
|
2013-08-16 14:30:51 +00:00
|
|
|
end
|
2015-04-27 23:19:26 +00:00
|
|
|
|
2015-09-10 19:09:50 +00:00
|
|
|
#puts "#{type}:working_time_in_min:#{working_time_in_min}|free_time:#{total_time_in_min - working_time_in_min}"
|
|
|
|
if type == 'non_business_minutes'
|
|
|
|
return total_time_in_min - working_time_in_min
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
2015-09-10 19:09:50 +00:00
|
|
|
working_time_in_min
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
|
|
|
|
2014-02-03 19:23:00 +00:00
|
|
|
end
|