2013-06-12 15:59:58 +00:00
|
|
|
# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2013-03-25 12:42:42 +00:00
|
|
|
require 'time_calculation'
|
2013-04-30 12:13:53 +00:00
|
|
|
require 'sla'
|
|
|
|
|
2012-07-29 20:25:31 +00:00
|
|
|
class Ticket < ApplicationModel
|
2013-08-15 22:16:38 +00:00
|
|
|
before_create :check_generate, :check_defaults
|
2012-11-13 10:34:45 +00:00
|
|
|
before_update :check_defaults
|
2012-07-20 08:08:31 +00:00
|
|
|
before_destroy :destroy_dependencies
|
2013-06-29 00:13:03 +00:00
|
|
|
after_create :notify_clients_after_create
|
|
|
|
after_update :notify_clients_after_update
|
|
|
|
after_destroy :notify_clients_after_destroy
|
2013-01-01 20:29:26 +00:00
|
|
|
|
2012-04-16 08:04:49 +00:00
|
|
|
belongs_to :group
|
2012-12-24 13:55:43 +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
|
2012-12-24 13:55:43 +00:00
|
|
|
belongs_to :ticket_state, :class_name => 'Ticket::State'
|
|
|
|
belongs_to :ticket_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 :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
|
|
|
|
2013-08-16 14:30:51 +00:00
|
|
|
include Ticket::Escalation
|
2013-08-17 20:04:57 +00:00
|
|
|
include Ticket::Subject
|
2013-08-17 21:10:36 +00:00
|
|
|
include Ticket::Permission
|
2013-08-17 21:10:11 +00:00
|
|
|
extend Ticket::Search
|
2013-08-16 14:30:51 +00:00
|
|
|
|
2013-03-28 23:13:15 +00:00
|
|
|
attr_accessor :callback_loop
|
|
|
|
|
2013-08-17 21:10:36 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
merge tickets
|
|
|
|
|
|
|
|
ticket = Ticket.find(123)
|
|
|
|
result = ticket.merge_to(
|
|
|
|
:ticket_id => 123,
|
|
|
|
)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = true|false
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2012-04-10 14:06:46 +00:00
|
|
|
def agent_of_group
|
2013-01-01 20:29:26 +00:00
|
|
|
Group.find( self.group_id ).users.where( :active => true ).joins(:roles).where( 'roles.name' => 'Agent', 'roles.active' => true ).uniq()
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
|
|
|
|
2013-08-16 14:30:51 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
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,
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
Ticket::Article.where( :ticket_id => self.id ).update_all( ['ticket_id = ?', data[:ticket_id] ] )
|
2012-11-07 23:47:05 +00:00
|
|
|
|
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(
|
2013-06-12 15:59:58 +00:00
|
|
|
:ticket_id => self.id,
|
2013-01-01 20:29:26 +00:00
|
|
|
:ticket_article_type_id => Ticket::Article::Type.lookup( :name => 'note' ).id,
|
|
|
|
:ticket_article_sender_id => Ticket::Article::Sender.lookup( :name => 'Agent' ).id,
|
2012-07-03 13:24:31 +00:00
|
|
|
:body => 'merged',
|
|
|
|
:internal => false
|
|
|
|
)
|
|
|
|
|
|
|
|
# add history to both
|
|
|
|
|
|
|
|
# link tickets
|
2012-08-21 10:28:41 +00:00
|
|
|
Link.add(
|
|
|
|
:link_type => 'parent',
|
|
|
|
:link_object_source => 'Ticket',
|
|
|
|
:link_object_source_value => data[:ticket_id],
|
|
|
|
:link_object_target => 'Ticket',
|
|
|
|
:link_object_target_value => self.id
|
|
|
|
)
|
2012-07-03 13:24:31 +00:00
|
|
|
|
|
|
|
# set state to 'merged'
|
2013-01-01 20:29:26 +00:00
|
|
|
self.ticket_state_id = Ticket::State.lookup( :name => 'merged' ).id
|
2012-07-03 13:24:31 +00:00
|
|
|
|
|
|
|
# rest owner
|
2012-08-21 10:28:41 +00:00
|
|
|
self.owner_id = User.where( :login => '-' ).first.id
|
2012-07-03 13:24:31 +00:00
|
|
|
|
|
|
|
# save ticket
|
|
|
|
self.save
|
|
|
|
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
|
2013-06-12 15:59:58 +00:00
|
|
|
return if self.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
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
def check_defaults
|
|
|
|
if !self.owner_id
|
|
|
|
self.owner_id = 1
|
|
|
|
end
|
|
|
|
if self.customer_id
|
|
|
|
customer = User.find( self.customer_id )
|
2013-08-17 21:10:11 +00:00
|
|
|
if self.organization_id != customer.organization_id
|
2013-06-12 15:59:58 +00:00
|
|
|
self.organization_id = customer.organization_id
|
2012-11-13 10:34:45 +00:00
|
|
|
end
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
2013-06-12 15:59:58 +00:00
|
|
|
end
|
2013-06-12 14:57:29 +00:00
|
|
|
|
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 history
|
|
|
|
History.remove( 'Ticket', self.id )
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2013-08-16 14:30:51 +00:00
|
|
|
# delete articles
|
|
|
|
self.articles.destroy_all
|
|
|
|
end
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2013-06-07 05:57:52 +00:00
|
|
|
end
|