time_accountings_controller.rb 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. customers = {}
  20. organizations = {}
  21. agents = {}
  22. results = []
  23. time_unit.each do |ticket_id, local_time_unit|
  24. ticket = Ticket.lookup(id: ticket_id)
  25. next if !ticket
  26. if !customers[ticket.customer_id]
  27. customers[ticket.customer_id] = '-'
  28. if ticket.customer_id
  29. customer_user = User.lookup(id: ticket.customer_id)
  30. if customer_user
  31. customers[ticket.customer_id] = customer_user.fullname
  32. end
  33. end
  34. end
  35. if !organizations[ticket.organization_id]
  36. organizations[ticket.organization_id] = '-'
  37. if ticket.organization_id
  38. organization = Organization.lookup(id: ticket.organization_id)
  39. if organization
  40. organizations[ticket.organization_id] = organization.name
  41. end
  42. end
  43. end
  44. if !customers[local_time_unit[:agent_id]]
  45. agent_user = User.lookup(id: local_time_unit[:agent_id])
  46. agent = '-'
  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. if params[:download]
  61. header = [
  62. {
  63. name: 'Ticket#',
  64. width: 15,
  65. },
  66. {
  67. name: 'Title',
  68. width: 30,
  69. },
  70. {
  71. name: 'Customer',
  72. width: 20,
  73. },
  74. {
  75. name: 'Organization',
  76. width: 20,
  77. },
  78. {
  79. name: 'Agent',
  80. width: 20,
  81. },
  82. {
  83. name: 'Time Units',
  84. width: 10,
  85. },
  86. {
  87. name: 'Time Units Total',
  88. width: 10,
  89. },
  90. {
  91. name: 'Created at',
  92. width: 10,
  93. },
  94. {
  95. name: 'Closed at',
  96. width: 10,
  97. },
  98. {
  99. name: 'Close Escalation At',
  100. width: 10,
  101. },
  102. {
  103. name: 'Close In Min',
  104. width: 10,
  105. },
  106. {
  107. name: 'Close Diff In Min',
  108. width: 10,
  109. },
  110. {
  111. name: 'First Response At',
  112. width: 10,
  113. },
  114. {
  115. name: 'First Response Escalation At',
  116. width: 10,
  117. },
  118. {
  119. name: 'First Response In Min',
  120. width: 10,
  121. },
  122. {
  123. name: 'First Response Diff In Min',
  124. width: 10,
  125. },
  126. {
  127. name: 'Update Escalation At',
  128. width: 10,
  129. },
  130. {
  131. name: 'Update In Min',
  132. width: 10,
  133. },
  134. {
  135. name: 'Update Diff In Min',
  136. width: 10,
  137. },
  138. {
  139. name: 'Last Contact At',
  140. width: 10,
  141. },
  142. {
  143. name: 'Last Contact Agent At',
  144. width: 10,
  145. },
  146. {
  147. name: 'Last Contact Customer At',
  148. width: 10,
  149. },
  150. {
  151. name: 'Article Count',
  152. width: 10,
  153. },
  154. {
  155. name: 'Escalation At',
  156. width: 10,
  157. },
  158. ]
  159. objects = ObjectManager::Attribute.where(editable: true,
  160. active: true,
  161. object_lookup_id: ObjectLookup.lookup(name: 'Ticket').id)
  162. .pluck(:name, :display, :data_type, :data_option)
  163. .map { |name, display, data_type, data_option| { name: name, display: display, data_type: data_type, data_option: data_option } }
  164. objects.each do |object|
  165. header.push({ name: object[:display], width: 10 })
  166. end
  167. result = []
  168. results.each do |row|
  169. row[:ticket].each_key do |field|
  170. next if row[:ticket][field].blank?
  171. next if !row[:ticket][field].is_a?(ActiveSupport::TimeWithZone)
  172. row[:ticket][field] = row[:ticket][field].iso8601
  173. end
  174. result_row = [
  175. row[:ticket]['number'],
  176. row[:ticket]['title'],
  177. row[:customer],
  178. row[:organization],
  179. row[:agent],
  180. row[:time_unit],
  181. row[:ticket]['time_unit'],
  182. row[:ticket]['created_at'],
  183. row[:ticket]['close_at'],
  184. row[:ticket]['close_escalation_at'],
  185. row[:ticket]['close_in_min'],
  186. row[:ticket]['close_diff_in_min'],
  187. row[:ticket]['first_response_at'],
  188. row[:ticket]['first_response_escalation_at'],
  189. row[:ticket]['first_response_in_min'],
  190. row[:ticket]['first_response_diff_in_min'],
  191. row[:ticket]['update_escalation_at'],
  192. row[:ticket]['update_in_min'],
  193. row[:ticket]['update_diff_in_min'],
  194. row[:ticket]['last_contact_at'],
  195. row[:ticket]['last_contact_agent_at'],
  196. row[:ticket]['last_contact_customer_at'],
  197. row[:ticket]['article_count'],
  198. row[:ticket]['escalation_at'],
  199. ]
  200. # needed to get human values for boolean/select rather than true/false values
  201. ticket = Ticket.lookup(id: row[:ticket]['id'])
  202. # Object Manager attributes
  203. # We already queried ObjectManager::Attributes, so we just use objects
  204. objects.each do |object|
  205. key = object[:name]
  206. case object[:data_type]
  207. when 'boolean', 'select'
  208. value = object[:data_option]['options'][ticket.send(key.to_sym)]
  209. value.present? ? result_row.push(value) : result_row.push('')
  210. when 'datetime', 'date'
  211. row[:ticket][key].present? ? result_row.push(row[:ticket][key].to_time.iso8601) : result_row.push('')
  212. else
  213. # for text, integer and tree select
  214. row[:ticket][key].present? ? result_row.push(row[:ticket][key]) : result_row.push('')
  215. end
  216. end
  217. result.push result_row
  218. end
  219. content = sheet("By Ticket #{year}-#{month}", header, result)
  220. send_data(
  221. content,
  222. filename: "by_ticket-#{year}-#{month}.xls",
  223. type: 'application/vnd.ms-excel',
  224. disposition: 'attachment'
  225. )
  226. return
  227. end
  228. render json: results
  229. end
  230. def by_customer
  231. year = params[:year] || Time.zone.now.year
  232. month = params[:month] || Time.zone.now.month
  233. start_periode = Time.zone.parse("#{year}-#{month}-01")
  234. end_periode = start_periode.end_of_month
  235. time_unit = {}
  236. Ticket::TimeAccounting.where('created_at >= ? AND created_at <= ?', start_periode, end_periode).pluck(:ticket_id, :time_unit, :created_by_id).each do |record|
  237. if !time_unit[record[0]]
  238. time_unit[record[0]] = {
  239. time_unit: 0,
  240. agent_id: record[2],
  241. }
  242. end
  243. time_unit[record[0]][:time_unit] += record[1]
  244. end
  245. customers = {}
  246. time_unit.each do |ticket_id, local_time_unit|
  247. ticket = Ticket.lookup(id: ticket_id)
  248. next if !ticket
  249. if !customers[ticket.customer_id]
  250. organization = nil
  251. if ticket.organization_id
  252. organization = Organization.lookup(id: ticket.organization_id).attributes
  253. end
  254. customers[ticket.customer_id] = {
  255. customer: User.lookup(id: ticket.customer_id).attributes,
  256. organization: organization,
  257. time_unit: local_time_unit[:time_unit],
  258. }
  259. next
  260. end
  261. customers[ticket.customer_id][:time_unit] += local_time_unit[:time_unit]
  262. end
  263. results = []
  264. customers.each_value do |content|
  265. results.push content
  266. end
  267. if params[:download]
  268. header = [
  269. {
  270. name: 'Customer',
  271. width: 30,
  272. },
  273. {
  274. name: 'Organization',
  275. width: 30,
  276. },
  277. {
  278. name: 'Time Units',
  279. width: 10,
  280. }
  281. ]
  282. result = []
  283. results.each do |row|
  284. customer_name = User.find(row[:customer]['id']).fullname
  285. organization_name = ''
  286. if row[:organization].present?
  287. organization_name = row[:organization]['name']
  288. end
  289. result_row = [customer_name, organization_name, row[:time_unit]]
  290. result.push result_row
  291. end
  292. content = sheet("By Customer #{year}-#{month}", header, result)
  293. send_data(
  294. content,
  295. filename: "by_customer-#{year}-#{month}.xls",
  296. type: 'application/vnd.ms-excel',
  297. disposition: 'attachment'
  298. )
  299. return
  300. end
  301. render json: results
  302. end
  303. def by_organization
  304. year = params[:year] || Time.zone.now.year
  305. month = params[:month] || Time.zone.now.month
  306. start_periode = Time.zone.parse("#{year}-#{month}-01")
  307. end_periode = start_periode.end_of_month
  308. time_unit = {}
  309. Ticket::TimeAccounting.where('created_at >= ? AND created_at <= ?', start_periode, end_periode).pluck(:ticket_id, :time_unit, :created_by_id).each do |record|
  310. if !time_unit[record[0]]
  311. time_unit[record[0]] = {
  312. time_unit: 0,
  313. agent_id: record[2],
  314. }
  315. end
  316. time_unit[record[0]][:time_unit] += record[1]
  317. end
  318. organizations = {}
  319. time_unit.each do |ticket_id, local_time_unit|
  320. ticket = Ticket.lookup(id: ticket_id)
  321. next if !ticket
  322. next if !ticket.organization_id
  323. if !organizations[ticket.organization_id]
  324. organizations[ticket.organization_id] = {
  325. organization: Organization.lookup(id: ticket.organization_id).attributes,
  326. time_unit: local_time_unit[:time_unit],
  327. }
  328. next
  329. end
  330. organizations[ticket.organization_id][:time_unit] += local_time_unit[:time_unit]
  331. end
  332. results = []
  333. organizations.each_value do |content|
  334. results.push content
  335. end
  336. if params[:download]
  337. header = [
  338. {
  339. name: 'Organization',
  340. width: 40,
  341. },
  342. {
  343. name: 'Time Units',
  344. width: 20,
  345. }
  346. ]
  347. result = []
  348. results.each do |row|
  349. organization_name = ''
  350. if row[:organization].present?
  351. organization_name = row[:organization]['name']
  352. end
  353. result_row = [organization_name, row[:time_unit]]
  354. result.push result_row
  355. end
  356. content = sheet("By Organization #{year}-#{month}", header, result)
  357. send_data(
  358. content,
  359. filename: "by_organization-#{year}-#{month}.xls",
  360. type: 'application/vnd.ms-excel',
  361. disposition: 'attachment'
  362. )
  363. return
  364. end
  365. render json: results
  366. end
  367. private
  368. def sheet(title, header, result)
  369. # Create a new Excel workbook
  370. temp_file = Tempfile.new('time_tracking.xls')
  371. workbook = WriteExcel.new(temp_file)
  372. # Add a worksheet
  373. worksheet = workbook.add_worksheet
  374. # Add and define a format
  375. format = workbook.add_format # Add a format
  376. format.set_bold
  377. format.set_size(14)
  378. format.set_color('black')
  379. worksheet.set_row(0, 0, header.count)
  380. # Write a formatted and unformatted string, row and column notation.
  381. worksheet.write_string(0, 0, title, format)
  382. format_header = workbook.add_format # Add a format
  383. format_header.set_italic
  384. format_header.set_bg_color('gray')
  385. format_header.set_color('white')
  386. count = 0
  387. header.each do |item|
  388. if item[:width]
  389. worksheet.set_column(count, count, item[:width])
  390. end
  391. worksheet.write_string(2, count, item[:name], format_header)
  392. count += 1
  393. end
  394. row_count = 2
  395. result.each do |row|
  396. row_count += 1
  397. row_item_count = 0
  398. row.each do |item|
  399. if item.acts_like?(:date)
  400. worksheet.write_date_time(row_count, row_item_count, item.to_time.iso8601)
  401. else
  402. worksheet.write_string(row_count, row_item_count, item)
  403. end
  404. row_item_count += 1
  405. end
  406. end
  407. workbook.close
  408. # read file again
  409. file = File.new(temp_file, 'r')
  410. contents = file.read
  411. file.close
  412. contents
  413. end
  414. end