ticket.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. merge tickets
  66. ticket = Ticket.find(123)
  67. result = ticket.merge_to(
  68. :ticket_id => 123,
  69. )
  70. returns
  71. result = true|false
  72. =end
  73. def merge_to(data)
  74. # update articles
  75. Ticket::Article.where( :ticket_id => self.id ).update_all( ['ticket_id = ?', data[:ticket_id] ] )
  76. # update history
  77. # create new merge article
  78. Ticket::Article.create(
  79. :ticket_id => self.id,
  80. :type_id => Ticket::Article::Type.lookup( :name => 'note' ).id,
  81. :sender_id => Ticket::Article::Sender.lookup( :name => 'Agent' ).id,
  82. :body => 'merged',
  83. :internal => false
  84. )
  85. # add history to both
  86. # link tickets
  87. Link.add(
  88. :link_type => 'parent',
  89. :link_object_source => 'Ticket',
  90. :link_object_source_value => data[:ticket_id],
  91. :link_object_target => 'Ticket',
  92. :link_object_target_value => self.id
  93. )
  94. # set state to 'merged'
  95. self.state_id = Ticket::State.lookup( :name => 'merged' ).id
  96. # rest owner
  97. self.owner_id = User.where( :login => '-' ).first.id
  98. # save ticket
  99. self.save
  100. end
  101. private
  102. def check_generate
  103. return if self.number
  104. self.number = Ticket::Number.generate
  105. end
  106. def check_defaults
  107. if !self.owner_id
  108. self.owner_id = 1
  109. end
  110. if self.customer_id
  111. customer = User.find( self.customer_id )
  112. if self.organization_id != customer.organization_id
  113. self.organization_id = customer.organization_id
  114. end
  115. end
  116. end
  117. def destroy_dependencies
  118. # delete articles
  119. self.articles.destroy_all
  120. end
  121. end