ticket_moved.rb 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class Report::TicketMoved < Report::Base
  3. =begin
  4. result = Report::TicketMoved.aggs(
  5. range_start: Time.zone.parse('2015-01-01T00:00:00Z'),
  6. range_end: Time.zone.parse('2015-12-31T23:59:59Z'),
  7. interval: 'month', # quarter, month, week, day, hour, minute, second
  8. selector: selector, # ticket selector to get only a collection of tickets
  9. params: { type: 'in' }, # in|out
  10. timezone: 'Europe/Berlin',
  11. )
  12. returns
  13. [4,5,1,5,0,51,5,56,7,4]
  14. =end
  15. def self.aggs(params_origin)
  16. params = params_origin.dup
  17. selector = params[:selector]['ticket.group_id']
  18. if !selector
  19. return [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  20. end
  21. result = []
  22. case params[:interval]
  23. when 'month'
  24. stop_interval = 12
  25. when 'week'
  26. stop_interval = 7
  27. when 'day'
  28. stop_interval = 31
  29. when 'hour'
  30. stop_interval = 24
  31. when 'minute'
  32. stop_interval = 60
  33. end
  34. (1..stop_interval).each do |_counter|
  35. case params[:interval]
  36. when 'month'
  37. params[:range_end] = params[:range_start].next_month
  38. when 'week', 'day'
  39. params[:range_end] = params[:range_start].next_day
  40. when 'hour'
  41. params[:range_end] = params[:range_start] + 1.hour
  42. when 'minute'
  43. params[:range_end] = params[:range_start] + 1.minute
  44. end
  45. local_params = group_attributes(selector, params)
  46. local_selector = params[:selector].clone
  47. without_merged_tickets = {
  48. 'ticket_state.name' => {
  49. 'operator' => 'is not',
  50. 'value' => 'merged'
  51. }
  52. }
  53. local_selector.merge!(without_merged_tickets) # do not show merged tickets in reports
  54. if params[:params][:type] == 'out'
  55. local_selector.delete('ticket.group_id')
  56. end
  57. defaults = {
  58. object: 'Ticket',
  59. type: 'updated',
  60. attribute: 'group',
  61. start: params[:range_start],
  62. end: params[:range_end],
  63. selector: local_selector
  64. }
  65. local_params = defaults.merge(local_params)
  66. count = history_count(local_params)
  67. result.push count
  68. params[:range_start] = params[:range_end]
  69. end
  70. result
  71. end
  72. =begin
  73. result = Report::TicketMoved.items(
  74. range_start: Time.zone.parse('2015-01-01T00:00:00Z'),
  75. range_end: Time.zone.parse('2015-12-31T23:59:59Z'),
  76. selector: selector, # ticket selector to get only a collection of tickets
  77. params: { type: 'in' }, # in|out
  78. )
  79. returns
  80. {
  81. count: 123,
  82. ticket_ids: [4,5,1,5,0,51,5,56,7,4],
  83. assets: assets,
  84. }
  85. =end
  86. def self.items(params)
  87. selector = params[:selector]['ticket.group_id']
  88. if !selector
  89. return {
  90. count: 0,
  91. ticket_ids: [],
  92. }
  93. end
  94. local_params = group_attributes(selector, params)
  95. without_merged_tickets = {
  96. 'ticket_state.name' => {
  97. 'operator' => 'is not',
  98. 'value' => 'merged'
  99. }
  100. }
  101. local_selector = params[:selector].merge!(without_merged_tickets) # do not show merged tickets in reports
  102. if params[:params][:type] == 'out'
  103. local_selector.delete('ticket.group_id')
  104. end
  105. defaults = {
  106. object: 'Ticket',
  107. type: 'updated',
  108. attribute: 'group',
  109. start: params[:range_start],
  110. end: params[:range_end],
  111. selector: local_selector
  112. }
  113. local_params = defaults.merge(local_params)
  114. result = history(local_params)
  115. return result if params[:sheet].present?
  116. assets = {}
  117. result[:ticket_ids].each do |ticket_id|
  118. ticket_full = Ticket.find(ticket_id)
  119. assets = ticket_full.assets(assets)
  120. end
  121. result[:assets] = assets
  122. result
  123. end
  124. def self.group_attributes(selector, params)
  125. group_id = selector['value']
  126. case selector['operator']
  127. when 'is'
  128. if params[:params][:type] == 'in'
  129. return {
  130. id_not_from: group_id,
  131. id_to: group_id,
  132. }
  133. else
  134. return {
  135. id_from: group_id,
  136. id_not_to: group_id,
  137. }
  138. end
  139. when 'is not'
  140. if params[:params][:type] == 'in'
  141. return {
  142. id_from: group_id,
  143. id_not_to: group_id,
  144. }
  145. else
  146. return {
  147. id_not_from: group_id,
  148. id_to: group_id,
  149. }
  150. end
  151. end
  152. raise "Unknown selector params '#{selector.inspect}'"
  153. end
  154. end