trabajo-afectivo/app/models/ticket.rb

189 lines
4.8 KiB
Ruby
Raw Normal View History

2014-02-03 19:23:00 +00:00
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
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
load 'ticket/assets.rb'
2013-09-29 21:37:49 +00:00
include Ticket::Assets
load 'ticket/history_log.rb'
2013-09-29 21:37:49 +00:00
include Ticket::HistoryLog
load 'ticket/activity_stream_log.rb'
2013-09-29 21:37:49 +00:00
include Ticket::ActivityStreamLog
load 'ticket/search_index.rb'
include Ticket::SearchIndex
2013-09-29 21:37:49 +00:00
extend Ticket::Search
before_create :check_generate, :check_defaults
before_update :check_defaults
before_destroy :destroy_dependencies
after_create :notify_clients_after_create
after_update :notify_clients_after_update
after_destroy :notify_clients_after_destroy
2013-09-29 16:40:42 +00:00
2013-10-05 12:56:03 +00:00
activity_stream_support :ignore_attributes => {
:create_article_type_id => true,
:create_article_sender_id => true,
:article_count => true,
}
2013-09-29 21:37:49 +00:00
history_support :ignore_attributes => {
2013-10-05 12:56:03 +00:00
:create_article_type_id => true,
:create_article_sender_id => true,
:article_count => true,
2013-09-29 21:37:49 +00:00
}
2014-02-02 18:58:31 +00:00
search_index_support(
:ignore_attributes => {
:create_article_type_id => true,
:create_article_sender_id => true,
:article_count => true,
},
:keep_attributes => {
:customer_id => true,
:organization_id => true,
},
)
2012-04-16 08:04:49 +00:00
belongs_to :group
has_many :articles, :class_name => 'Ticket::Article', :after_add => :cache_update, :after_remove => :cache_update
belongs_to :organization
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
attr_accessor :callback_loop
=begin
2013-08-17 21:13:34 +00:00
list of agents in group of ticket
ticket = Ticket.find(123)
2013-08-17 21:13:34 +00:00
result = ticket.agent_of_group
returns
2013-08-17 21:13:34 +00:00
result = [user1, user2, ...]
=end
2012-04-10 14:06:46 +00:00
def agent_of_group
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
=begin
2014-11-10 07:34:20 +00:00
get user access conditions
connditions = Ticket.access_condition( User.find(1) )
returns
result = [user1, user2, ...]
=end
def self.access_condition(user)
access_condition = []
if user.is_role('Agent')
group_ids = Group.select( 'groups.id' ).joins(:users).
where( 'groups_users.user_id = ?', user.id ).
where( 'groups.active = ?', true ).
map( &:id )
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
merge tickets
ticket = Ticket.find(123)
result = ticket.merge_to(
: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(
:ticket_id => self.id,
:type_id => Ticket::Article::Type.lookup( :name => 'note' ).id,
:sender_id => Ticket::Article::Sender.lookup( :name => 'Agent' ).id,
:body => 'merged',
:internal => false
2012-07-03 13:24:31 +00:00
)
# 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'
self.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
def check_generate
return if self.number
self.number = Ticket::Number.generate
end
def check_defaults
if !self.owner_id
self.owner_id = 1
end
if self.customer_id
customer = User.find( self.customer_id )
if self.organization_id != customer.organization_id
self.organization_id = customer.organization_id
end
2012-04-10 14:06:46 +00:00
end
end
2013-06-12 14:57:29 +00:00
def destroy_dependencies
# delete articles
self.articles.destroy_all
end
2012-04-10 14:06:46 +00:00
end