2014-02-03 19:23:00 +00:00
|
|
|
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2012-07-29 20:25:31 +00:00
|
|
|
class Ticket < ApplicationModel
|
2013-09-29 21:37:49 +00:00
|
|
|
include Ticket::Escalation
|
|
|
|
include Ticket::Subject
|
|
|
|
include Ticket::Permission
|
2014-10-17 12:15:54 +00:00
|
|
|
load 'ticket/assets.rb'
|
2013-09-29 21:37:49 +00:00
|
|
|
include Ticket::Assets
|
2014-10-17 12:15:54 +00:00
|
|
|
load 'ticket/history_log.rb'
|
2013-09-29 21:37:49 +00:00
|
|
|
include Ticket::HistoryLog
|
2014-10-17 12:15:54 +00:00
|
|
|
load 'ticket/activity_stream_log.rb'
|
2013-09-29 21:37:49 +00:00
|
|
|
include Ticket::ActivityStreamLog
|
2014-10-17 12:15:54 +00:00
|
|
|
load 'ticket/search_index.rb'
|
2014-01-27 22:59:41 +00:00
|
|
|
include Ticket::SearchIndex
|
2013-09-29 21:37:49 +00:00
|
|
|
extend Ticket::Search
|
|
|
|
|
2015-01-07 21:28:15 +00:00
|
|
|
before_create :check_generate, :check_defaults, :check_title
|
2015-02-11 22:05:31 +00:00
|
|
|
before_update :check_defaults, :check_title, :reset_pending_time
|
2012-07-20 08:08:31 +00:00
|
|
|
before_destroy :destroy_dependencies
|
2015-02-01 12:08:11 +00:00
|
|
|
|
|
|
|
notify_clients_support
|
2013-09-29 16:40:42 +00:00
|
|
|
|
2015-02-25 20:52:14 +00:00
|
|
|
latest_change_support
|
|
|
|
|
2015-04-27 13:42:53 +00:00
|
|
|
activity_stream_support ignore_attributes: {
|
|
|
|
create_article_type_id: true,
|
|
|
|
create_article_sender_id: true,
|
|
|
|
article_count: true,
|
2013-10-05 12:56:03 +00:00
|
|
|
}
|
2013-09-29 21:37:49 +00:00
|
|
|
|
2015-04-27 13:42:53 +00:00
|
|
|
history_support ignore_attributes: {
|
|
|
|
create_article_type_id: true,
|
|
|
|
create_article_sender_id: true,
|
|
|
|
article_count: true,
|
2013-09-29 21:37:49 +00:00
|
|
|
}
|
2013-01-01 20:29:26 +00:00
|
|
|
|
2014-02-02 18:58:31 +00:00
|
|
|
search_index_support(
|
2015-04-27 13:42:53 +00:00
|
|
|
ignore_attributes: {
|
|
|
|
create_article_type_id: true,
|
|
|
|
create_article_sender_id: true,
|
|
|
|
article_count: true,
|
2014-02-02 18:58:31 +00:00
|
|
|
},
|
2015-04-27 13:42:53 +00:00
|
|
|
keep_attributes: {
|
|
|
|
customer_id: true,
|
|
|
|
organization_id: true,
|
2014-02-02 18:58:31 +00:00
|
|
|
},
|
|
|
|
)
|
2014-01-27 22:59:41 +00:00
|
|
|
|
2012-04-16 08:04:49 +00:00
|
|
|
belongs_to :group
|
2015-04-27 13:42:53 +00:00
|
|
|
has_many :articles, class_name: 'Ticket::Article', after_add: :cache_update, after_remove: :cache_update
|
2012-11-13 10:34:45 +00:00
|
|
|
belongs_to :organization
|
2015-04-27 13:42:53 +00:00
|
|
|
belongs_to :state, class_name: 'Ticket::State'
|
|
|
|
belongs_to :priority, class_name: 'Ticket::Priority'
|
|
|
|
belongs_to :owner, class_name: 'User'
|
|
|
|
belongs_to :customer, class_name: 'User'
|
|
|
|
belongs_to :created_by, class_name: 'User'
|
|
|
|
belongs_to :updated_by, class_name: 'User'
|
|
|
|
belongs_to :create_article_type, class_name: 'Ticket::Article::Type'
|
|
|
|
belongs_to :create_article_sender, class_name: 'Ticket::Article::Sender'
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2014-09-09 23:42:20 +00:00
|
|
|
self.inheritance_column = nil
|
|
|
|
|
2013-03-28 23:13:15 +00:00
|
|
|
attr_accessor :callback_loop
|
|
|
|
|
2013-08-17 21:10:36 +00:00
|
|
|
=begin
|
|
|
|
|
2013-08-17 21:13:34 +00:00
|
|
|
list of agents in group of ticket
|
2013-08-17 21:10:36 +00:00
|
|
|
|
|
|
|
ticket = Ticket.find(123)
|
2013-08-17 21:13:34 +00:00
|
|
|
result = ticket.agent_of_group
|
2013-08-17 21:10:36 +00:00
|
|
|
|
|
|
|
returns
|
|
|
|
|
2013-08-17 21:13:34 +00:00
|
|
|
result = [user1, user2, ...]
|
2013-08-17 21:10:36 +00:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
2012-04-10 14:06:46 +00:00
|
|
|
def agent_of_group
|
2015-05-07 12:10:38 +00:00
|
|
|
Group.find( group_id ).users.where( active: true ).joins(:roles).where( 'roles.name' => Z_ROLENAME_AGENT, 'roles.active' => true ).uniq()
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
|
|
|
|
2013-08-16 14:30:51 +00:00
|
|
|
=begin
|
|
|
|
|
2014-11-10 07:34:20 +00:00
|
|
|
get user access conditions
|
|
|
|
|
2015-02-15 09:23:55 +00:00
|
|
|
conditions = Ticket.access_condition( User.find(1) )
|
2014-11-10 07:34:20 +00:00
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = [user1, user2, ...]
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.access_condition(user)
|
|
|
|
access_condition = []
|
2015-05-08 08:15:45 +00:00
|
|
|
if user.role?(Z_ROLENAME_AGENT)
|
2015-04-30 17:51:31 +00:00
|
|
|
group_ids = Group.select( 'groups.id' ).joins(:users)
|
2015-05-01 12:27:57 +00:00
|
|
|
.where( 'groups_users.user_id = ?', user.id )
|
|
|
|
.where( 'groups.active = ?', true )
|
|
|
|
.map( &:id )
|
2014-11-10 07:34:20 +00:00
|
|
|
access_condition = [ 'group_id IN (?)', group_ids ]
|
|
|
|
else
|
|
|
|
if !user.organization || ( !user.organization.shared || user.organization.shared == false )
|
|
|
|
access_condition = [ 'customer_id = ?', user.id ]
|
|
|
|
else
|
|
|
|
access_condition = [ '( customer_id = ? OR organization_id = ? )', user.id, user.organization.id ]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
access_condition
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2015-05-21 14:40:04 +00:00
|
|
|
processes tickets which have reached their pending time and sets next state_id
|
|
|
|
|
|
|
|
processed_tickets = Ticket.process_pending()
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
processed_tickets = [<Ticket>, ...]
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.process_pending
|
|
|
|
|
|
|
|
ticket_states = Ticket::State.where(
|
|
|
|
state_type_id: Ticket::StateType.find_by( name: 'pending action' ),
|
|
|
|
)
|
|
|
|
.where.not(next_state_id: nil) # rubocop:disable Style/MultilineOperationIndentation
|
|
|
|
|
|
|
|
return [] if !ticket_states
|
|
|
|
|
|
|
|
next_state_map = {}
|
|
|
|
ticket_states.each { |state|
|
|
|
|
next_state_map[state.id] = state.next_state_id
|
|
|
|
}
|
|
|
|
|
|
|
|
tickets = where(
|
|
|
|
state_id: next_state_map.keys,
|
|
|
|
)
|
|
|
|
.where( 'pending_time <= ?', Time.zone.now ) # rubocop:disable Style/MultilineOperationIndentation
|
|
|
|
|
|
|
|
return [] if !tickets
|
|
|
|
|
|
|
|
result = []
|
|
|
|
tickets.each { |ticket|
|
|
|
|
ticket.state_id = next_state_map[ticket.state_id]
|
|
|
|
ticket.updated_at = Time.zone.now
|
|
|
|
ticket.updated_by_id = 1
|
|
|
|
ticket.save!
|
|
|
|
|
|
|
|
result.push ticket
|
|
|
|
}
|
|
|
|
|
2015-05-22 06:58:32 +00:00
|
|
|
# we do not have an destructor at this point, so we need to
|
|
|
|
# execute ticket events manually
|
|
|
|
Observer::Ticket::Notification.transaction
|
|
|
|
|
2015-05-21 14:40:04 +00:00
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2013-08-16 14:30:51 +00:00
|
|
|
merge tickets
|
|
|
|
|
2013-08-17 20:04:57 +00:00
|
|
|
ticket = Ticket.find(123)
|
|
|
|
result = ticket.merge_to(
|
2013-08-16 14:30:51 +00:00
|
|
|
:ticket_id => 123,
|
2015-04-01 11:47:10 +00:00
|
|
|
:user_id => 123,
|
2013-08-16 14:30:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = true|false
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2012-07-03 13:24:31 +00:00
|
|
|
def merge_to(data)
|
2012-11-07 23:47:05 +00:00
|
|
|
|
2012-07-03 13:24:31 +00:00
|
|
|
# update articles
|
2015-05-07 12:10:38 +00:00
|
|
|
Ticket::Article.where( ticket_id: id ).each(&:touch)
|
2015-03-09 01:31:09 +00:00
|
|
|
|
|
|
|
# quiet update of reassign of articles
|
2015-05-07 12:10:38 +00:00
|
|
|
Ticket::Article.where( ticket_id: id ).update_all( ['ticket_id = ?', data[:ticket_id] ] )
|
2012-11-07 23:47:05 +00:00
|
|
|
|
2015-01-31 13:33:05 +00:00
|
|
|
# touch new ticket (to broadcast change)
|
|
|
|
Ticket.find( data[:ticket_id] ).touch
|
|
|
|
|
2012-07-03 13:24:31 +00:00
|
|
|
# update history
|
2012-11-07 23:47:05 +00:00
|
|
|
|
2012-07-03 13:24:31 +00:00
|
|
|
# create new merge article
|
|
|
|
Ticket::Article.create(
|
2015-05-07 12:10:38 +00:00
|
|
|
ticket_id: id,
|
2015-04-27 13:42:53 +00:00
|
|
|
type_id: Ticket::Article::Type.lookup( name: 'note' ).id,
|
|
|
|
sender_id: Ticket::Article::Sender.lookup( name: Z_ROLENAME_AGENT ).id,
|
|
|
|
body: 'merged',
|
|
|
|
internal: false,
|
|
|
|
created_by_id: data[:user_id],
|
|
|
|
updated_by_id: data[:user_id],
|
2012-07-03 13:24:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# add history to both
|
|
|
|
|
|
|
|
# link tickets
|
2012-08-21 10:28:41 +00:00
|
|
|
Link.add(
|
2015-04-27 13:42:53 +00:00
|
|
|
link_type: 'parent',
|
|
|
|
link_object_source: 'Ticket',
|
|
|
|
link_object_source_value: data[:ticket_id],
|
|
|
|
link_object_target: 'Ticket',
|
2015-05-07 12:10:38 +00:00
|
|
|
link_object_target_value: id
|
2012-08-21 10:28:41 +00:00
|
|
|
)
|
2012-07-03 13:24:31 +00:00
|
|
|
|
|
|
|
# set state to 'merged'
|
2015-04-27 13:42:53 +00:00
|
|
|
self.state_id = Ticket::State.lookup( name: 'merged' ).id
|
2012-07-03 13:24:31 +00:00
|
|
|
|
|
|
|
# rest owner
|
2015-05-07 10:15:40 +00:00
|
|
|
self.owner_id = User.find_by( login: '-' ).id
|
2012-07-03 13:24:31 +00:00
|
|
|
|
|
|
|
# save ticket
|
2015-05-07 12:10:38 +00:00
|
|
|
save
|
2015-04-01 14:33:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2015-07-26 21:21:16 +00:00
|
|
|
know if online notifcation should be shown as already seen with current state
|
2015-04-01 14:33:24 +00:00
|
|
|
|
|
|
|
ticket = Ticket.find(1)
|
|
|
|
seen = ticket.online_notification_seen_state
|
2015-04-01 11:47:10 +00:00
|
|
|
|
2015-04-01 14:33:24 +00:00
|
|
|
returns
|
|
|
|
|
2015-07-26 21:21:16 +00:00
|
|
|
result = true # or false
|
2015-04-01 14:33:24 +00:00
|
|
|
|
|
|
|
=end
|
2015-04-01 11:47:10 +00:00
|
|
|
|
2015-04-01 14:33:24 +00:00
|
|
|
def online_notification_seen_state
|
2015-05-07 12:10:38 +00:00
|
|
|
state = Ticket::State.lookup( id: state_id )
|
2015-04-27 13:42:53 +00:00
|
|
|
state_type = Ticket::StateType.lookup( id: state.state_type_id )
|
2015-07-26 21:21:16 +00:00
|
|
|
|
|
|
|
# set all to seen if pending action state is a closed or merged state
|
|
|
|
if state_type.name == 'pending action' && state.next_state_id
|
2015-07-26 21:56:50 +00:00
|
|
|
state = Ticket::State.lookup( id: state.next_state_id )
|
2015-07-26 21:21:16 +00:00
|
|
|
state_type = Ticket::StateType.lookup( id: state.state_type_id )
|
|
|
|
end
|
|
|
|
|
2015-08-31 09:13:40 +00:00
|
|
|
# set all to seen if new state is pending reminder state
|
|
|
|
return true if state_type.name == 'pending reminder'
|
|
|
|
|
2015-07-26 21:21:16 +00:00
|
|
|
# set all to seen if new state is a closed or merged state
|
2015-04-01 14:33:24 +00:00
|
|
|
return true if state_type.name == 'closed'
|
|
|
|
return true if state_type.name == 'merged'
|
|
|
|
false
|
2012-07-03 13:24:31 +00:00
|
|
|
end
|
|
|
|
|
2012-11-28 10:03:17 +00:00
|
|
|
private
|
2012-11-28 09:46:26 +00:00
|
|
|
|
2013-08-15 22:16:38 +00:00
|
|
|
def check_generate
|
2015-05-07 12:10:38 +00:00
|
|
|
return if number
|
2013-08-15 22:16:38 +00:00
|
|
|
self.number = Ticket::Number.generate
|
2013-06-12 15:59:58 +00:00
|
|
|
end
|
2013-08-15 22:16:38 +00:00
|
|
|
|
2015-01-07 21:28:15 +00:00
|
|
|
def check_title
|
2015-04-30 15:25:04 +00:00
|
|
|
|
2015-05-07 12:10:38 +00:00
|
|
|
return if !title
|
2015-04-30 15:25:04 +00:00
|
|
|
|
2015-05-07 12:10:38 +00:00
|
|
|
title.gsub!(/\s|\t|\r/, ' ')
|
2015-01-07 21:28:15 +00:00
|
|
|
end
|
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
def check_defaults
|
2015-05-07 12:10:38 +00:00
|
|
|
if !owner_id
|
2013-06-12 15:59:58 +00:00
|
|
|
self.owner_id = 1
|
|
|
|
end
|
2015-04-30 15:25:04 +00:00
|
|
|
|
2015-05-07 12:10:38 +00:00
|
|
|
return if !customer_id
|
2015-04-30 15:25:04 +00:00
|
|
|
|
2015-05-07 12:10:38 +00:00
|
|
|
customer = User.find( customer_id )
|
|
|
|
return if organization_id == customer.organization_id
|
2015-04-30 15:25:04 +00:00
|
|
|
|
|
|
|
self.organization_id = customer.organization_id
|
2013-06-12 15:59:58 +00:00
|
|
|
end
|
2013-06-12 14:57:29 +00:00
|
|
|
|
2015-02-11 22:05:31 +00:00
|
|
|
def reset_pending_time
|
|
|
|
|
|
|
|
# ignore if no state has changed
|
2015-05-07 12:10:38 +00:00
|
|
|
return if !changes['state_id']
|
2015-02-11 22:05:31 +00:00
|
|
|
|
|
|
|
# check if new state isn't pending*
|
2015-05-07 12:10:38 +00:00
|
|
|
current_state = Ticket::State.lookup( id: state_id )
|
2015-04-27 13:42:53 +00:00
|
|
|
current_state_type = Ticket::StateType.lookup( id: current_state.state_type_id )
|
2015-02-11 22:05:31 +00:00
|
|
|
|
|
|
|
# in case, set pending_time to nil
|
2015-04-30 15:25:04 +00:00
|
|
|
return if current_state_type.name =~ /^pending/i
|
|
|
|
|
|
|
|
self.pending_time = nil
|
2015-02-11 22:05:31 +00:00
|
|
|
end
|
|
|
|
|
2013-08-16 14:30:51 +00:00
|
|
|
def destroy_dependencies
|
2013-06-13 15:03:08 +00:00
|
|
|
|
2013-08-16 14:30:51 +00:00
|
|
|
# delete articles
|
2015-05-07 12:10:38 +00:00
|
|
|
articles.destroy_all
|
2015-04-01 11:14:46 +00:00
|
|
|
|
|
|
|
# destroy online notifications
|
2015-05-07 12:10:38 +00:00
|
|
|
OnlineNotification.remove( self.class.to_s, id )
|
2013-08-16 14:30:51 +00:00
|
|
|
end
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|