tickets.rb 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class CalendarSubscriptions::Tickets
  3. def initialize(user, preferences)
  4. @user = user
  5. @preferences = preferences
  6. @tzid = 'UTC'
  7. end
  8. def all
  9. events_data = []
  10. return events_data if @preferences.blank?
  11. events_data += new_open
  12. events_data += pending
  13. events_data += escalation
  14. events_data
  15. end
  16. def alarm?
  17. alarm = false
  18. return alarm if @preferences.blank?
  19. return alarm if !@preferences[:alarm]
  20. @preferences[:alarm]
  21. end
  22. def owner_ids(method)
  23. owner_ids = []
  24. return owner_ids if @preferences.blank?
  25. return owner_ids if !@preferences[method]
  26. return owner_ids if @preferences[method].blank?
  27. preferences = @preferences[method]
  28. if preferences[:own]
  29. owner_ids = [ @user.id ]
  30. end
  31. if preferences[:not_assigned]
  32. owner_ids.push( 1 )
  33. end
  34. owner_ids
  35. end
  36. def new_open
  37. events_data = []
  38. owner_ids = owner_ids(:new_open)
  39. return events_data if owner_ids.blank?
  40. condition = {
  41. 'ticket.owner_id' => {
  42. operator: 'is',
  43. value: owner_ids,
  44. },
  45. 'ticket.state_id' => {
  46. operator: 'is',
  47. value: Ticket::State.where(
  48. state_type_id: Ticket::StateType.where(
  49. name: %w[new open],
  50. ),
  51. ).map(&:id),
  52. },
  53. }
  54. tickets = Ticket.search(
  55. current_user: @user,
  56. condition: condition,
  57. )
  58. user_locale = @user.locale
  59. translated_ticket = Translation.translate(user_locale, 'ticket')
  60. events_data = []
  61. tickets.each do |ticket|
  62. event_data = {}
  63. translated_state = Translation.translate(user_locale, ticket.state.name)
  64. event_data[:dtstart] = Icalendar::Values::Date.new(Time.zone.today, 'tzid' => @tzid)
  65. event_data[:dtend] = Icalendar::Values::Date.new(Time.zone.today, 'tzid' => @tzid)
  66. event_data[:summary] = "#{translated_state} #{translated_ticket}: '#{ticket.title}'"
  67. event_data[:description] = "T##{ticket.number}"
  68. events_data.push event_data
  69. end
  70. events_data
  71. end
  72. def pending
  73. events_data = []
  74. owner_ids = owner_ids(:pending)
  75. return events_data if owner_ids.blank?
  76. condition = {
  77. 'ticket.owner_id' => {
  78. operator: 'is',
  79. value: owner_ids,
  80. },
  81. 'ticket.state_id' => {
  82. operator: 'is',
  83. value: Ticket::State.where(
  84. state_type_id: Ticket::StateType.where(
  85. name: [
  86. 'pending reminder',
  87. 'pending action',
  88. ],
  89. ),
  90. ).map(&:id),
  91. },
  92. }
  93. tickets = Ticket.search(
  94. current_user: @user,
  95. condition: condition,
  96. )
  97. user_locale = @user.locale
  98. translated_ticket = Translation.translate(user_locale, 'ticket')
  99. customer = Translation.translate(user_locale, 'customer')
  100. events_data = []
  101. tickets.each do |ticket|
  102. next if !ticket.pending_time
  103. event_data = {}
  104. pending_time = ticket.pending_time
  105. if pending_time < Time.zone.today
  106. pending_time = Time.zone.today
  107. end
  108. translated_state = Translation.translate(user_locale, ticket.state.name)
  109. event_data[:dtstart] = Icalendar::Values::DateTime.new(pending_time, 'tzid' => @tzid)
  110. event_data[:dtend] = Icalendar::Values::DateTime.new(pending_time, 'tzid' => @tzid)
  111. event_data[:summary] = "#{translated_state} #{translated_ticket}: '#{ticket.title}' #{customer}: #{ticket.customer.longname}"
  112. event_data[:description] = "T##{ticket.number}"
  113. if alarm?
  114. event_data[:alarm] = {
  115. summary: event_data[:summary],
  116. trigger: '-PT1M',
  117. }
  118. end
  119. events_data.push event_data
  120. end
  121. events_data
  122. end
  123. def escalation
  124. events_data = []
  125. owner_ids = owner_ids(:escalation)
  126. return events_data if owner_ids.blank?
  127. condition = {
  128. 'ticket.owner_id' => {
  129. operator: 'is',
  130. value: owner_ids,
  131. },
  132. 'ticket.escalation_at' => {
  133. operator: 'is not',
  134. value: nil,
  135. }
  136. }
  137. tickets = Ticket.search(
  138. current_user: @user,
  139. condition: condition,
  140. )
  141. user_locale = @user.locale
  142. translated_ticket_escalation = Translation.translate(user_locale, 'ticket escalation')
  143. customer = Translation.translate(user_locale, 'customer')
  144. tickets.each do |ticket|
  145. next if !ticket.escalation_at
  146. event_data = {}
  147. escalation_at = ticket.escalation_at
  148. if escalation_at < Time.zone.today
  149. escalation_at = Time.zone.today
  150. end
  151. event_data[:dtstart] = Icalendar::Values::DateTime.new(escalation_at, 'tzid' => @tzid)
  152. event_data[:dtend] = Icalendar::Values::DateTime.new(escalation_at, 'tzid' => @tzid)
  153. event_data[:summary] = "#{translated_ticket_escalation}: '#{ticket.title}' #{customer}: #{ticket.customer.longname}"
  154. event_data[:description] = "T##{ticket.number}"
  155. if alarm?
  156. event_data[:alarm] = {
  157. summary: event_data[:summary],
  158. trigger: '-PT1M',
  159. }
  160. end
  161. events_data.push event_data
  162. end
  163. events_data
  164. end
  165. end