ticket.rb 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. class Ticket < ApplicationModel
  3. include Ticket::Escalation
  4. include Ticket::Subject
  5. include Ticket::Permission
  6. load 'ticket/assets.rb'
  7. include Ticket::Assets
  8. load 'ticket/history_log.rb'
  9. include Ticket::HistoryLog
  10. load 'ticket/activity_stream_log.rb'
  11. include Ticket::ActivityStreamLog
  12. load 'ticket/search_index.rb'
  13. include Ticket::SearchIndex
  14. extend Ticket::Search
  15. before_create :check_generate, :check_defaults
  16. before_update :check_defaults
  17. before_destroy :destroy_dependencies
  18. after_create :notify_clients_after_create
  19. after_update :notify_clients_after_update
  20. after_destroy :notify_clients_after_destroy
  21. activity_stream_support :ignore_attributes => {
  22. :create_article_type_id => true,
  23. :create_article_sender_id => true,
  24. :article_count => true,
  25. }
  26. history_support :ignore_attributes => {
  27. :create_article_type_id => true,
  28. :create_article_sender_id => true,
  29. :article_count => true,
  30. }
  31. search_index_support(
  32. :ignore_attributes => {
  33. :create_article_type_id => true,
  34. :create_article_sender_id => true,
  35. :article_count => true,
  36. },
  37. :keep_attributes => {
  38. :customer_id => true,
  39. :organization_id => true,
  40. },
  41. )
  42. belongs_to :group
  43. has_many :articles, :class_name => 'Ticket::Article', :after_add => :cache_update, :after_remove => :cache_update
  44. belongs_to :organization
  45. belongs_to :state, :class_name => 'Ticket::State'
  46. belongs_to :priority, :class_name => 'Ticket::Priority'
  47. belongs_to :owner, :class_name => 'User'
  48. belongs_to :customer, :class_name => 'User'
  49. belongs_to :created_by, :class_name => 'User'
  50. belongs_to :create_article_type, :class_name => 'Ticket::Article::Type'
  51. belongs_to :create_article_sender, :class_name => 'Ticket::Article::Sender'
  52. self.inheritance_column = nil
  53. attr_accessor :callback_loop
  54. =begin
  55. list of agents in group of ticket
  56. ticket = Ticket.find(123)
  57. result = ticket.agent_of_group
  58. returns
  59. result = [user1, user2, ...]
  60. =end
  61. def agent_of_group
  62. Group.find( self.group_id ).users.where( :active => true ).joins(:roles).where( 'roles.name' => 'Agent', 'roles.active' => true ).uniq()
  63. end
  64. =begin
  65. get user access conditions
  66. connditions = Ticket.access_condition( User.find(1) )
  67. returns
  68. result = [user1, user2, ...]
  69. =end
  70. def self.access_condition(user)
  71. access_condition = []
  72. if user.is_role('Agent')
  73. group_ids = Group.select( 'groups.id' ).joins(:users).
  74. where( 'groups_users.user_id = ?', user.id ).
  75. where( 'groups.active = ?', true ).
  76. map( &:id )
  77. access_condition = [ 'group_id IN (?)', group_ids ]
  78. else
  79. if !user.organization || ( !user.organization.shared || user.organization.shared == false )
  80. access_condition = [ 'customer_id = ?', user.id ]
  81. else
  82. access_condition = [ '( customer_id = ? OR organization_id = ? )', user.id, user.organization.id ]
  83. end
  84. end
  85. access_condition
  86. end
  87. =begin
  88. merge tickets
  89. ticket = Ticket.find(123)
  90. result = ticket.merge_to(
  91. :ticket_id => 123,
  92. )
  93. returns
  94. result = true|false
  95. =end
  96. def merge_to(data)
  97. # update articles
  98. Ticket::Article.where( :ticket_id => self.id ).update_all( ['ticket_id = ?', data[:ticket_id] ] )
  99. # update history
  100. # create new merge article
  101. Ticket::Article.create(
  102. :ticket_id => self.id,
  103. :type_id => Ticket::Article::Type.lookup( :name => 'note' ).id,
  104. :sender_id => Ticket::Article::Sender.lookup( :name => 'Agent' ).id,
  105. :body => 'merged',
  106. :internal => false
  107. )
  108. # add history to both
  109. # link tickets
  110. Link.add(
  111. :link_type => 'parent',
  112. :link_object_source => 'Ticket',
  113. :link_object_source_value => data[:ticket_id],
  114. :link_object_target => 'Ticket',
  115. :link_object_target_value => self.id
  116. )
  117. # set state to 'merged'
  118. self.state_id = Ticket::State.lookup( :name => 'merged' ).id
  119. # rest owner
  120. self.owner_id = User.where( :login => '-' ).first.id
  121. # save ticket
  122. self.save
  123. end
  124. private
  125. def check_generate
  126. return if self.number
  127. self.number = Ticket::Number.generate
  128. end
  129. def check_defaults
  130. if !self.owner_id
  131. self.owner_id = 1
  132. end
  133. if self.customer_id
  134. customer = User.find( self.customer_id )
  135. if self.organization_id != customer.organization_id
  136. self.organization_id = customer.organization_id
  137. end
  138. end
  139. end
  140. def destroy_dependencies
  141. # delete articles
  142. self.articles.destroy_all
  143. end
  144. end