time_accountings_controller.rb 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class TimeAccountingsController < ApplicationController
  3. prepend_before_action { authentication_check(permission: 'admin.time_accounting') }
  4. def by_ticket
  5. year = params[:year] || Time.zone.now.year
  6. month = params[:month] || Time.zone.now.month
  7. start_periode = Time.zone.parse("#{year}-#{month}-01")
  8. end_periode = start_periode.end_of_month
  9. time_unit = {}
  10. Ticket::TimeAccounting.where('created_at >= ? AND created_at <= ?', start_periode, end_periode).pluck(:ticket_id, :time_unit, :created_by_id).each do |record|
  11. if !time_unit[record[0]]
  12. time_unit[record[0]] = {
  13. time_unit: 0,
  14. agent_id: record[2],
  15. }
  16. end
  17. time_unit[record[0]][:time_unit] += record[1]
  18. end
  19. if !params[:download]
  20. customers = {}
  21. organizations = {}
  22. agents = {}
  23. results = []
  24. time_unit.each do |ticket_id, local_time_unit|
  25. ticket = Ticket.lookup(id: ticket_id)
  26. next if !ticket
  27. if !customers[ticket.customer_id]
  28. customers[ticket.customer_id] = '-'
  29. if ticket.customer_id
  30. customer_user = User.lookup(id: ticket.customer_id)
  31. if customer_user
  32. customers[ticket.customer_id] = customer_user.fullname
  33. end
  34. end
  35. end
  36. if !organizations[ticket.organization_id]
  37. organizations[ticket.organization_id] = '-'
  38. if ticket.organization_id
  39. organization = Organization.lookup(id: ticket.organization_id)
  40. if organization
  41. organizations[ticket.organization_id] = organization.name
  42. end
  43. end
  44. end
  45. if !agents[local_time_unit[:agent_id]]
  46. agent_user = User.lookup(id: local_time_unit[:agent_id])
  47. if agent_user
  48. agents[local_time_unit[:agent_id]] = agent_user.fullname
  49. end
  50. end
  51. result = {
  52. ticket: ticket.attributes,
  53. time_unit: local_time_unit[:time_unit],
  54. customer: customers[ticket.customer_id],
  55. organization: organizations[ticket.organization_id],
  56. agent: agents[local_time_unit[:agent_id]],
  57. }
  58. results.push result
  59. end
  60. render json: results
  61. return
  62. end
  63. ticket_ids = []
  64. additional_attributes = []
  65. additional_attributes_header = [{ display: 'Time Units', name: 'time_unit_for_range', width: 10, data_type: 'float' }]
  66. time_unit.each do |ticket_id, local_time_unit|
  67. ticket_ids.push ticket_id
  68. additional_attribute = {
  69. time_unit_for_range: local_time_unit[:time_unit],
  70. }
  71. additional_attributes.push additional_attribute
  72. end
  73. excel = ExcelSheet::Ticket.new(
  74. title: "Tickets: #{year}-#{month}",
  75. ticket_ids: ticket_ids,
  76. additional_attributes: additional_attributes,
  77. additional_attributes_header: additional_attributes_header,
  78. timezone: params[:timezone],
  79. locale: current_user.locale,
  80. )
  81. send_data(
  82. excel.content,
  83. filename: "by_ticket-#{year}-#{month}.xls",
  84. type: 'application/vnd.ms-excel',
  85. disposition: 'attachment'
  86. )
  87. end
  88. def by_customer
  89. year = params[:year] || Time.zone.now.year
  90. month = params[:month] || Time.zone.now.month
  91. start_periode = Time.zone.parse("#{year}-#{month}-01")
  92. end_periode = start_periode.end_of_month
  93. time_unit = {}
  94. Ticket::TimeAccounting.where('created_at >= ? AND created_at <= ?', start_periode, end_periode).pluck(:ticket_id, :time_unit, :created_by_id).each do |record|
  95. if !time_unit[record[0]]
  96. time_unit[record[0]] = {
  97. time_unit: 0,
  98. agent_id: record[2],
  99. }
  100. end
  101. time_unit[record[0]][:time_unit] += record[1]
  102. end
  103. customers = {}
  104. time_unit.each do |ticket_id, local_time_unit|
  105. ticket = Ticket.lookup(id: ticket_id)
  106. next if !ticket
  107. if !customers[ticket.customer_id]
  108. organization = nil
  109. if ticket.organization_id
  110. organization = Organization.lookup(id: ticket.organization_id).attributes
  111. end
  112. customers[ticket.customer_id] = {
  113. customer: User.lookup(id: ticket.customer_id).attributes,
  114. organization: organization,
  115. time_unit: local_time_unit[:time_unit],
  116. }
  117. next
  118. end
  119. customers[ticket.customer_id][:time_unit] += local_time_unit[:time_unit]
  120. end
  121. results = []
  122. customers.each_value do |content|
  123. results.push content
  124. end
  125. if params[:download]
  126. header = [
  127. {
  128. name: 'Customer',
  129. width: 30,
  130. },
  131. {
  132. name: 'Organization',
  133. width: 30,
  134. },
  135. {
  136. name: 'Time Units',
  137. width: 10,
  138. data_type: 'float'
  139. }
  140. ]
  141. records = []
  142. results.each do |row|
  143. customer_name = User.find(row[:customer]['id']).fullname
  144. organization_name = ''
  145. if row[:organization].present?
  146. organization_name = row[:organization]['name']
  147. end
  148. result_row = [customer_name, organization_name, row[:time_unit]]
  149. records.push result_row
  150. end
  151. excel = ExcelSheet.new(
  152. title: "By Customer #{year}-#{month}",
  153. header: header,
  154. records: records,
  155. timezone: params[:timezone],
  156. locale: current_user.locale,
  157. )
  158. send_data(
  159. excel.content,
  160. filename: "by_customer-#{year}-#{month}.xls",
  161. type: 'application/vnd.ms-excel',
  162. disposition: 'attachment'
  163. )
  164. return
  165. end
  166. render json: results
  167. end
  168. def by_organization
  169. year = params[:year] || Time.zone.now.year
  170. month = params[:month] || Time.zone.now.month
  171. start_periode = Time.zone.parse("#{year}-#{month}-01")
  172. end_periode = start_periode.end_of_month
  173. time_unit = {}
  174. Ticket::TimeAccounting.where('created_at >= ? AND created_at <= ?', start_periode, end_periode).pluck(:ticket_id, :time_unit, :created_by_id).each do |record|
  175. if !time_unit[record[0]]
  176. time_unit[record[0]] = {
  177. time_unit: 0,
  178. agent_id: record[2],
  179. }
  180. end
  181. time_unit[record[0]][:time_unit] += record[1]
  182. end
  183. organizations = {}
  184. time_unit.each do |ticket_id, local_time_unit|
  185. ticket = Ticket.lookup(id: ticket_id)
  186. next if !ticket
  187. next if !ticket.organization_id
  188. if !organizations[ticket.organization_id]
  189. organizations[ticket.organization_id] = {
  190. organization: Organization.lookup(id: ticket.organization_id).attributes,
  191. time_unit: local_time_unit[:time_unit],
  192. }
  193. next
  194. end
  195. organizations[ticket.organization_id][:time_unit] += local_time_unit[:time_unit]
  196. end
  197. results = []
  198. organizations.each_value do |content|
  199. results.push content
  200. end
  201. if params[:download]
  202. header = [
  203. {
  204. name: 'Organization',
  205. width: 40,
  206. },
  207. {
  208. name: 'Time Units',
  209. width: 20,
  210. data_type: 'float',
  211. }
  212. ]
  213. records = []
  214. results.each do |row|
  215. organization_name = ''
  216. if row[:organization].present?
  217. organization_name = row[:organization]['name']
  218. end
  219. result_row = [organization_name, row[:time_unit]]
  220. records.push result_row
  221. end
  222. excel = ExcelSheet.new(
  223. title: "By Organization #{year}-#{month}",
  224. header: header,
  225. records: records,
  226. timezone: params[:timezone],
  227. locale: current_user.locale,
  228. )
  229. send_data(
  230. excel.content,
  231. filename: "by_organization-#{year}-#{month}.xls",
  232. type: 'application/vnd.ms-excel',
  233. disposition: 'attachment'
  234. )
  235. return
  236. end
  237. render json: results
  238. end
  239. end