screen_options.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. module Ticket::ScreenOptions
  3. =begin
  4. list of active agents
  5. result = Ticket::ScreenOptions.agents()
  6. returns
  7. result = [user1, user2]
  8. =end
  9. def self.agents
  10. User.where( :active => true ).joins(:roles).where( 'roles.name' => 'Agent', 'roles.active' => true ).uniq()
  11. end
  12. =begin
  13. list attributes
  14. result = Ticket::ScreenOptions.attributes_to_change(
  15. :ticket_id => 123,
  16. :article_id => 123,
  17. :ticket => ticket_model,
  18. )
  19. returns
  20. result = {
  21. :type_id => type_ids,
  22. :state_id => state_ids,
  23. :priority_id => priority_ids,
  24. :owner_id => owner_ids,
  25. :group_id => group_ids,
  26. :group_id__owner_id => groups_users,
  27. }
  28. =end
  29. def self.attributes_to_change(params)
  30. if params[:ticket_id]
  31. params[:ticket] = self.find( params[:ticket_id] )
  32. end
  33. if params[:article_id]
  34. params[:article] = self.find( params[:article_id] )
  35. end
  36. filter = {}
  37. assets = {}
  38. # get ticket states
  39. state_ids = []
  40. if params[:ticket]
  41. state_type = params[:ticket].state.state_type
  42. end
  43. state_types = ['open', 'closed', 'pending action', 'pending reminder']
  44. if state_type && !state_types.include?(state_type.name)
  45. state_ids.push params[:ticket].state.id
  46. end
  47. state_types.each {|type|
  48. state_type = Ticket::StateType.where( :name => type ).first
  49. if state_type
  50. state_type.states.each {|state|
  51. assets = state.assets(assets)
  52. state_ids.push state.id
  53. }
  54. end
  55. }
  56. filter[:state_id] = state_ids
  57. # get priorities
  58. priority_ids = []
  59. Ticket::Priority.where( :active => true ).each { |priority|
  60. assets = priority.assets(assets)
  61. priority_ids.push priority.id
  62. }
  63. filter[:priority_id] = priority_ids
  64. type_ids = []
  65. if params[:ticket]
  66. types = ['note', 'phone']
  67. if params[:ticket].group.email_address_id
  68. types.push 'email'
  69. end
  70. types.each {|type_name|
  71. type = Ticket::Article::Type.lookup( :name => type_name )
  72. if type
  73. type_ids.push type.id
  74. end
  75. }
  76. end
  77. filter[:type_id] = type_ids
  78. # get group / user relations
  79. agents = {}
  80. Ticket::ScreenOptions.agents.each { |user|
  81. agents[ user.id ] = 1
  82. }
  83. dependencies = { :group_id => { '' => [] } }
  84. Group.where( :active => true ).each { |group|
  85. assets = group.assets(assets)
  86. dependencies[:group_id][group.id] = { :owner_id => [] }
  87. group.users.each {|user|
  88. next if !agents[ user.id ]
  89. assets = user.assets(assets)
  90. dependencies[:group_id][ group.id ][ :owner_id ].push user.id
  91. }
  92. }
  93. return {
  94. :assets => assets,
  95. :filter => filter,
  96. :dependencies => dependencies,
  97. }
  98. end
  99. =begin
  100. list tickets by customer groupd in state categroie open and closed
  101. result = Ticket::ScreenOptions.list_by_customer(
  102. :customer_id => 123,
  103. :limit => 15, # optional, default 15
  104. )
  105. returns
  106. result = {
  107. :open => tickets_open,
  108. :closed => tickets_closed,
  109. }
  110. =end
  111. def self.list_by_customer(data)
  112. # get closed/open states
  113. state_list_open = Ticket::State.by_category( 'open' )
  114. state_list_closed = Ticket::State.by_category( 'closed' )
  115. # get tickets
  116. tickets_open = Ticket.where(
  117. :customer_id => data[:customer_id],
  118. :state_id => state_list_open
  119. ).limit( data[:limit] || 15 ).order('created_at DESC')
  120. tickets_closed = Ticket.where(
  121. :customer_id => data[:customer_id],
  122. :state_id => state_list_closed
  123. ).limit( data[:limit] || 15 ).order('created_at DESC')
  124. return {
  125. :open => tickets_open,
  126. :closed => tickets_closed,
  127. }
  128. end
  129. end