screen_options.rb 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. # rubocop:disable ClassAndModuleChildren
  3. module Ticket::ScreenOptions
  4. =begin
  5. list of active agents
  6. result = Ticket::ScreenOptions.agents()
  7. returns
  8. result = [user1, user2]
  9. =end
  10. def self.agents
  11. User.where( active: true ).joins(:roles).where( 'roles.name' => 'Agent', 'roles.active' => true ).uniq()
  12. end
  13. =begin
  14. list attributes
  15. result = Ticket::ScreenOptions.attributes_to_change(
  16. :ticket_id => 123,
  17. :article_id => 123,
  18. :ticket => ticket_model,
  19. )
  20. returns
  21. result = {
  22. :type_id => type_ids,
  23. :state_id => state_ids,
  24. :priority_id => priority_ids,
  25. :owner_id => owner_ids,
  26. :group_id => group_ids,
  27. :group_id__owner_id => groups_users,
  28. }
  29. =end
  30. def self.attributes_to_change(params)
  31. if params[:ticket_id]
  32. params[:ticket] = Ticket.find( params[:ticket_id] )
  33. end
  34. if params[:article_id]
  35. params[:article] = Ticket::Article.find( params[:article_id] )
  36. end
  37. filter = {}
  38. assets = {}
  39. # get ticket states
  40. state_ids = []
  41. if params[:ticket]
  42. state_type = params[:ticket].state.state_type
  43. end
  44. state_types = ['open', 'closed', 'pending action', 'pending reminder']
  45. if state_type && !state_types.include?(state_type.name)
  46. state_ids.push params[:ticket].state.id
  47. end
  48. state_types.each {|type|
  49. state_type = Ticket::StateType.find_by( name: type )
  50. next if !state_type
  51. state_type.states.each {|state|
  52. assets = state.assets(assets)
  53. state_ids.push state.id
  54. }
  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 = %w(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: { '' => { owner_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. {
  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. :ticket_ids_open => tickets_open,
  108. :ticket_ids_closed => tickets_closed,
  109. :assets => { ...list of assets... },
  110. }
  111. =end
  112. def self.list_by_customer(data)
  113. # get closed/open states
  114. state_list_open = Ticket::State.by_category( 'open' )
  115. state_list_closed = Ticket::State.by_category( 'closed' )
  116. # get tickets
  117. tickets_open = Ticket.where(
  118. customer_id: data[:customer_id],
  119. state_id: state_list_open
  120. ).limit( data[:limit] || 15 ).order('created_at DESC')
  121. assets = {}
  122. ticket_ids_open = []
  123. tickets_open.each {|ticket|
  124. ticket_ids_open.push ticket.id
  125. assets = ticket.assets(assets)
  126. }
  127. tickets_closed = Ticket.where(
  128. customer_id: data[:customer_id],
  129. state_id: state_list_closed
  130. ).limit( data[:limit] || 15 ).order('created_at DESC')
  131. ticket_ids_closed = []
  132. tickets_closed.each {|ticket|
  133. ticket_ids_closed.push ticket.id
  134. assets = ticket.assets(assets)
  135. }
  136. {
  137. ticket_ids_open: ticket_ids_open,
  138. ticket_ids_closed: ticket_ids_closed,
  139. assets: assets,
  140. }
  141. end
  142. end