ticket.rb 4.8 KB

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