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-01-31 17:13:45 +00:00
|
|
|
include LatestChangeObserved
|
|
|
|
|
2017-02-12 17:21:03 +00:00
|
|
|
after_create :ensure_defaults
|
|
|
|
after_update :ensure_defaults
|
|
|
|
after_destroy :ensure_defaults
|
|
|
|
|
2015-07-03 17:47:23 +00:00
|
|
|
belongs_to :state_type, class_name: 'Ticket::StateType'
|
2016-06-06 15:26:37 +00:00
|
|
|
belongs_to :next_state, class_name: 'Ticket::State'
|
2015-04-27 23:19:26 +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
|
|
|
|
|
|
|
|
list tickets by customer
|
|
|
|
|
2017-01-09 14:00:03 +00:00
|
|
|
states = Ticket::State.by_category('open') # open|closed|work_on|work_on_all|pending_reminder|pending_action|merged
|
2013-08-06 09:23:25 +00:00
|
|
|
|
2013-08-06 09:49:56 +00:00
|
|
|
returns:
|
|
|
|
|
|
|
|
state objects
|
|
|
|
|
2013-08-06 09:23:25 +00:00
|
|
|
=end
|
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
def self.by_category(category)
|
|
|
|
if category == 'open'
|
|
|
|
return Ticket::State.where(
|
2016-06-06 15:26:37 +00:00
|
|
|
state_type_id: Ticket::StateType.where(name: ['new', 'open', 'pending reminder', 'pending action'])
|
2015-04-27 23:19:26 +00:00
|
|
|
)
|
2015-09-06 23:54:47 +00:00
|
|
|
elsif category == 'pending_reminder'
|
|
|
|
return Ticket::State.where(
|
2016-06-06 15:26:37 +00:00
|
|
|
state_type_id: Ticket::StateType.where(name: ['pending reminder'])
|
2015-09-06 23:54:47 +00:00
|
|
|
)
|
2015-09-08 07:49:15 +00:00
|
|
|
elsif category == 'pending_action'
|
|
|
|
return Ticket::State.where(
|
2016-06-06 15:26:37 +00:00
|
|
|
state_type_id: Ticket::StateType.where(name: ['pending action'])
|
2015-09-08 07:49:15 +00:00
|
|
|
)
|
2015-09-06 23:54:47 +00:00
|
|
|
elsif category == 'work_on'
|
|
|
|
return Ticket::State.where(
|
2016-06-06 15:26:37 +00:00
|
|
|
state_type_id: Ticket::StateType.where(name: %w(new open))
|
2015-09-06 23:54:47 +00:00
|
|
|
)
|
2015-09-08 06:55:09 +00:00
|
|
|
elsif category == 'work_on_all'
|
|
|
|
return Ticket::State.where(
|
2016-06-06 15:26:37 +00:00
|
|
|
state_type_id: Ticket::StateType.where(name: ['new', 'open', 'pending reminder'])
|
2015-09-08 06:55:09 +00:00
|
|
|
)
|
2015-04-27 23:19:26 +00:00
|
|
|
elsif category == 'closed'
|
|
|
|
return Ticket::State.where(
|
2016-06-06 15:26:37 +00:00
|
|
|
state_type_id: Ticket::StateType.where(name: %w(closed))
|
2015-04-27 23:19:26 +00:00
|
|
|
)
|
2017-01-09 14:00:03 +00:00
|
|
|
elsif category == 'merged'
|
|
|
|
return Ticket::State.where(
|
|
|
|
state_type_id: Ticket::StateType.where(name: %w(merged))
|
|
|
|
)
|
2014-02-03 19:23:00 +00:00
|
|
|
end
|
2016-03-01 14:26:46 +00:00
|
|
|
raise "Unknown category '#{category}'"
|
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
|
|
|
|
|
|
|
|
%w(default_create default_follow_up).each do |default_field|
|
|
|
|
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
|
|
|
|
|
|
|
|
Ticket::State.all.each { |local_state|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
end
|