ticket.rb 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. class ExcelSheet::Ticket < ExcelSheet
  2. =begin
  3. excel = ExcelSheet::Ticket.new(
  4. title: "#{year}-#{month}",
  5. ticket_ids: ticket_ids,
  6. additional_attributes: additional_attributes,
  7. additional_attributes_header: additional_attributes_header,
  8. timezone: params[:timezone],
  9. locale: current_user.locale,
  10. )
  11. excel.content
  12. =end
  13. def initialize(params)
  14. @ticket_ids = params[:ticket_ids] || []
  15. @additional_attributes = params[:additional_attributes] || []
  16. @additional_attributes_header = params[:additional_attributes_header] || []
  17. super(
  18. title: params[:title],
  19. header: ticket_header,
  20. records: [],
  21. timezone: params[:timezone],
  22. locale: params[:locale]
  23. )
  24. end
  25. def ticket_header
  26. header = [
  27. { display: '#', name: 'number', width: 18, data_type: 'string' },
  28. { display: 'Title', name: 'title', width: 34, data_type: 'string' },
  29. { display: 'State', name: 'state_id', width: 14, data_type: 'string' },
  30. { display: 'Priority', name: 'priority_id', width: 14, data_type: 'string' },
  31. { display: 'Group', name: 'group_id', width: 20, data_type: 'string' },
  32. { display: 'Owner', name: 'owner_id', width: 20, data_type: 'string' },
  33. { display: 'Customer', name: 'customer_id', width: 20, data_type: 'string' },
  34. { display: 'Organization', name: 'organization_id', width: 20, data_type: 'string' },
  35. { display: 'Create Channel', name: 'create_article_type_id', width: 10, data_type: 'string' },
  36. { display: 'Sender', name: 'create_article_sender_id', width: 14, data_type: 'string' },
  37. { display: 'Tags', name: 'tag_list', width: 20, data_type: 'string' },
  38. { display: 'Time Units Total', name: 'time_unit', width: 10, data_type: 'float' },
  39. ]
  40. header = header.concat(@additional_attributes_header) if @additional_attributes_header
  41. # ObjectManager attributes
  42. objects = ObjectManager::Attribute.where(active: true,
  43. to_create: false,
  44. object_lookup_id: ObjectLookup.lookup(name: 'Ticket').id)
  45. .pluck(:name, :display, :data_type, :data_option)
  46. .map { |name, display, data_type, data_option| { name: name, display: display, data_type: data_type, data_option: data_option, width: 20 } }
  47. objects.each do |object|
  48. already_exists = false
  49. header.each do |local_header|
  50. next if local_header[:name] != object[:name]
  51. already_exists = true
  52. break
  53. end
  54. next if already_exists
  55. header.push object
  56. end
  57. header.concat([
  58. { display: 'Created At', name: 'created_at', width: 18, data_type: 'datetime' },
  59. { display: 'Updated At', name: 'updated_at', width: 18, data_type: 'datetime' },
  60. { display: 'Closed At', name: 'close_at', width: 18, data_type: 'datetime' },
  61. { display: 'Close Escalation At', name: 'close_escalation_at', width: 18, data_type: 'datetime' },
  62. { display: 'Close In Min', name: 'close_in_min', width: 10, data_type: 'integer' },
  63. { display: 'Close Diff In Min', name: 'close_diff_in_min', width: 10, data_type: 'integer' },
  64. { display: 'First Response At', name: 'first_response_at', width: 18, data_type: 'datetime' },
  65. { display: 'First Response Escalation At', name: 'first_response_escalation_at', width: 18, data_type: 'datetime' },
  66. { display: 'First Response In Min', name: 'first_response_in_min', width: 10, data_type: 'integer' },
  67. { display: 'First Response Diff In Min', name: 'first_response_diff_in_min', width: 10, data_type: 'integer' },
  68. { display: 'Update Escalation At', name: 'update_escalation_at', width: 18, data_type: 'datetime' },
  69. { display: 'Update In Min', name: 'update_in_min', width: 10, data_type: 'integer' },
  70. { display: 'Update Diff In Min', name: 'update_diff_in_min', width: 10, data_type: 'integer' },
  71. { display: 'Last Contact At', name: 'last_contact_at', width: 18, data_type: 'datetime' },
  72. { display: 'Last Contact Agent At', name: 'last_contact_agent_at', width: 18, data_type: 'datetime' },
  73. { display: 'Last Contact Customer At', name: 'last_contact_customer_at', width: 18, data_type: 'datetime' },
  74. { display: 'Article Count', name: 'article_count', width: 10, data_type: 'integer' },
  75. { display: 'Escalation At', name: 'escalation_at', width: 18, data_type: 'datetime' },
  76. ])
  77. end
  78. def gen_rows
  79. @ticket_ids.each_with_index do |ticket_id, index|
  80. ticket = ::Ticket.lookup(id: ticket_id)
  81. raise "Can't find Ticket with ID #{ticket_id} for '#{@title}' #{self.class.name} generation" if !ticket
  82. gen_row_by_header(ticket, @additional_attributes[index])
  83. rescue => e
  84. Rails.logger.error e
  85. end
  86. end
  87. end