2016-10-19 03:11:36 +00:00
|
|
|
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
|
2015-04-27 23:19:26 +00:00
|
|
|
class Ticket::State < ApplicationModel
|
2017-05-02 15:21:13 +00:00
|
|
|
include ChecksLatestChangeObserved
|
2017-01-31 17:13:45 +00:00
|
|
|
|
2018-04-12 14:57:37 +00:00
|
|
|
belongs_to :state_type, class_name: 'Ticket::StateType', inverse_of: :states
|
|
|
|
belongs_to :next_state, class_name: 'Ticket::State'
|
|
|
|
|
2017-02-12 17:21:03 +00:00
|
|
|
after_create :ensure_defaults
|
|
|
|
after_update :ensure_defaults
|
|
|
|
after_destroy :ensure_defaults
|
|
|
|
|
2018-04-12 14:57:37 +00:00
|
|
|
validates :name, presence: true
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2017-02-12 17:21:03 +00:00
|
|
|
attr_accessor :callback_loop
|
|
|
|
|
2013-08-06 09:23:25 +00:00
|
|
|
=begin
|
|
|
|
|
2017-03-27 14:06:18 +00:00
|
|
|
looks up states for a given category
|
2013-08-06 09:23:25 +00:00
|
|
|
|
2017-03-31 11:56:42 +00:00
|
|
|
states = Ticket::State.by_category(:open) # :open|:closed|:work_on|:work_on_all|:viewable|:viewable_agent_new|:viewable_agent_edit|:viewable_customer_new|:viewable_customer_edit|:pending_reminder|:pending_action|:pending|:merged
|
2013-08-06 09:23:25 +00:00
|
|
|
|
2013-08-06 09:49:56 +00:00
|
|
|
returns:
|
|
|
|
|
2017-03-27 14:06:18 +00:00
|
|
|
state object list
|
2013-08-06 09:49:56 +00:00
|
|
|
|
2013-08-06 09:23:25 +00:00
|
|
|
=end
|
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
def self.by_category(category)
|
2017-03-27 14:06:18 +00:00
|
|
|
|
|
|
|
case category.to_sym
|
|
|
|
when :open
|
|
|
|
state_types = ['new', 'open', 'pending reminder', 'pending action']
|
|
|
|
when :pending_reminder
|
|
|
|
state_types = ['pending reminder']
|
|
|
|
when :pending_action
|
|
|
|
state_types = ['pending action']
|
2017-03-31 11:56:42 +00:00
|
|
|
when :pending
|
|
|
|
state_types = ['pending reminder', 'pending action']
|
2017-03-27 14:06:18 +00:00
|
|
|
when :work_on
|
2017-11-23 08:09:44 +00:00
|
|
|
state_types = %w[new open]
|
2017-03-27 14:06:18 +00:00
|
|
|
when :work_on_all
|
|
|
|
state_types = ['new', 'open', 'pending reminder']
|
|
|
|
when :viewable
|
|
|
|
state_types = ['new', 'open', 'pending reminder', 'pending action', 'closed', 'removed']
|
2017-03-31 11:56:42 +00:00
|
|
|
when :viewable_agent_new
|
|
|
|
state_types = ['new', 'open', 'pending reminder', 'pending action', 'closed']
|
|
|
|
when :viewable_agent_edit
|
|
|
|
state_types = ['open', 'pending reminder', 'pending action', 'closed']
|
|
|
|
when :viewable_customer_new
|
2017-11-23 08:09:44 +00:00
|
|
|
state_types = %w[new closed]
|
2017-03-31 11:56:42 +00:00
|
|
|
when :viewable_customer_edit
|
2017-11-23 08:09:44 +00:00
|
|
|
state_types = %w[open closed]
|
2017-03-27 14:06:18 +00:00
|
|
|
when :closed
|
2017-11-23 08:09:44 +00:00
|
|
|
state_types = %w[closed]
|
2017-03-27 14:06:18 +00:00
|
|
|
when :merged
|
2017-11-23 08:09:44 +00:00
|
|
|
state_types = %w[merged]
|
2014-02-03 19:23:00 +00:00
|
|
|
end
|
2017-03-27 14:06:18 +00:00
|
|
|
|
|
|
|
raise "Unknown category '#{category}'" if state_types.blank?
|
|
|
|
|
|
|
|
Ticket::State.where(
|
|
|
|
state_type_id: Ticket::StateType.where(name: state_types)
|
|
|
|
)
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
2013-08-06 09:49:56 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
check if state is ignored for escalation
|
|
|
|
|
2015-09-10 19:09:50 +00:00
|
|
|
state = Ticket::State.lookup(name: 'state name')
|
2013-08-06 09:49:56 +00:00
|
|
|
|
2014-06-08 22:01:20 +00:00
|
|
|
result = state.ignore_escalation?
|
2013-08-06 09:49:56 +00:00
|
|
|
|
|
|
|
returns:
|
|
|
|
|
|
|
|
true/false
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
def ignore_escalation?
|
2015-09-10 19:09:50 +00:00
|
|
|
return true if ignore_escalation
|
2015-04-27 23:19:26 +00:00
|
|
|
false
|
2013-08-06 09:49:56 +00:00
|
|
|
end
|
2017-02-12 17:21:03 +00:00
|
|
|
|
|
|
|
def ensure_defaults
|
|
|
|
return if callback_loop
|
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
%w[default_create default_follow_up].each do |default_field|
|
2017-02-12 17:21:03 +00:00
|
|
|
states_with_default = Ticket::State.where(default_field => true)
|
|
|
|
next if states_with_default.count == 1
|
|
|
|
|
|
|
|
if states_with_default.count.zero?
|
|
|
|
state = Ticket::State.where(active: true).order(id: :asc).first
|
|
|
|
state[default_field] = true
|
|
|
|
state.callback_loop = true
|
|
|
|
state.save!
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2017-10-01 12:25:52 +00:00
|
|
|
Ticket::State.all.each do |local_state|
|
2017-02-12 17:21:03 +00:00
|
|
|
next if local_state.id == id
|
|
|
|
next if local_state[default_field] == false
|
|
|
|
local_state[default_field] = false
|
|
|
|
local_state.callback_loop = true
|
|
|
|
local_state.save!
|
|
|
|
next
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-02-12 17:21:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
end
|