diff --git a/app/assets/javascripts/app/controllers/report.js.coffee b/app/assets/javascripts/app/controllers/report.js.coffee index 3fa632cfe..d60bcaec9 100644 --- a/app/assets/javascripts/app/controllers/report.js.coffee +++ b/app/assets/javascripts/app/controllers/report.js.coffee @@ -245,15 +245,19 @@ class Download extends App.Controller if _.isEmpty(tickets) @el.find('.js-dataDownloadTable').html('') else + profile_id = 0 + for key, value of @params.profileSelected + if value + profile_id = key + downloadUrl = "#{@apiPath}/reports/sets?sheet=true;metric=#{@params.metric};year=#{@params.year};month=#{@params.month};week=#{@params.week};day=#{@params.day};timeRange=#{@params.timeRange};profile_id=#{profile_id};downloadBackendSelected=#{@params.downloadBackendSelected}" html = App.view('report/download_list')( - tickets: tickets - count: count - url: url - download: @apiPath + '/reports/csvforset/' + name + tickets: tickets + count: count + url: url + download: downloadUrl ) @el.find('.js-dataDownloadTable').html(html) - @startLoading() @ajax( id: 'report_download' type: 'POST' @@ -271,7 +275,6 @@ class Download extends App.Controller ) processData: true success: (data) => - @stopLoading() App.Collection.loadAssets(data.assets) ticket_collection = [] if data.ticket_ids diff --git a/app/controllers/reports_controller.rb b/app/controllers/reports_controller.rb index cfb119e94..40d73a3bf 100644 --- a/app/controllers/reports_controller.rb +++ b/app/controllers/reports_controller.rb @@ -1,10 +1,12 @@ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ +require 'tempfile' + class ReportsController < ApplicationController before_action :authentication_check # GET /api/reports/config - def config + def reporting_config return if deny_if_not_role('Report') render json: { config: Report.config, @@ -89,22 +91,38 @@ class ReportsController < ApplicationController selector: backend[:condition], params: backend[:params], ) + + # generate sheet + next if !params[:sheet] + content = sheet(get_params[:profile], backend[:display], result) + send_data( + content, + filename: "tickets-#{get_params[:profile].name}-#{backend[:display]}.xls", + type: 'application/vnd.ms-excel', + disposition: 'attachment' + ) } + return if params[:sheet] + render json: result end def params_all profile = nil - if !params[:profiles] + if !params[:profiles] && !params[:profile_id] render json: { error: 'No such profiles param', }, status: :unprocessable_entity return end - params[:profiles].each {|profile_id, active| - next if !active - profile = Report::Profile.find(profile_id) - } + if params[:profile_id] + profile = Report::Profile.find(params[:profile_id]) + else + params[:profiles].each {|profile_id, active| + next if !active + profile = Report::Profile.find(profile_id) + } + end if !profile render json: { error: 'No such active profile', @@ -112,14 +130,14 @@ class ReportsController < ApplicationController return end - config = Report.config - if !config || !config[:metric] || !config[:metric][params[:metric].to_sym] + local_config = Report.config + if !local_config || !local_config[:metric] || !local_config[:metric][params[:metric].to_sym] render json: { error: "No such metric #{params[:metric]}" }, status: :unprocessable_entity return end - metric = config[:metric][params[:metric].to_sym] + metric = local_config[:metric][params[:metric].to_sym] #{"metric"=>"count", "year"=>2015, "month"=>10, "week"=>43, "day"=>20, "timeSlot"=>"year", "report"=>{"metric"=>"count", "year"=>2015, "month"=>10, "week"=>43, "day"=>20, "timeSlot"=>"year"}} if params[:timeRange] == 'realtime' @@ -147,11 +165,71 @@ class ReportsController < ApplicationController { profile: profile, metric: metric, - config: config, + config: local_config, start: start, stop: stop, range: range, } end + def sheet(profile, title, result) + + # Create a new Excel workbook + temp_file = Tempfile.new('time_tracking.xls') + workbook = WriteExcel.new(temp_file) + + # Add a worksheet + worksheet = workbook.add_worksheet + worksheet.set_column(0, 0, 10) + worksheet.set_column(1, 1, 34) + worksheet.set_column(2, 2, 10) + worksheet.set_column(3, 3, 10) + worksheet.set_column(4, 7, 20) + + # Add and define a format + format = workbook.add_format # Add a format + format.set_bold + format.set_size(14) + format.set_color('black') + worksheet.set_row(0, 0, 6) + + # Write a formatted and unformatted string, row and column notation. + worksheet.write(0, 0, "Tickets: #{profile.name} (#{title})", format) + + format_header = workbook.add_format # Add a format + format_header.set_italic + format_header.set_bg_color('gray') + format_header.set_color('white') + worksheet.write(2, 0, '#', format_header ) + worksheet.write(2, 1, 'Title', format_header ) + worksheet.write(2, 2, 'State', format_header ) + worksheet.write(2, 3, 'Priority', format_header ) + worksheet.write(2, 4, 'Customer', format_header ) + worksheet.write(2, 5, 'Created at', format_header ) + worksheet.write(2, 6, 'Updated at', format_header ) + worksheet.write(2, 7, 'Closed at', format_header ) + + row = 2 + result[:ticket_ids].each {|ticket_id| + ticket = Ticket.lookup(id: ticket_id) + row += 1 + worksheet.write(row, 0, ticket.number ) + worksheet.write(row, 1, ticket.title) + worksheet.write(row, 2, ticket.state.name) + worksheet.write(row, 3, ticket.priority.name) + worksheet.write(row, 4, ticket.customer.fullname) + worksheet.write(row, 5, ticket.created_at) + worksheet.write(row, 6, ticket.updated_at) + worksheet.write(row, 7, ticket.close_time) + } + + workbook.close + + # read file again + file = File.new(temp_file, 'r') + contents = file.read + file.close + contents + end + end diff --git a/config/routes/report.rb b/config/routes/report.rb index 0dfa453bf..8fe454b89 100644 --- a/config/routes/report.rb +++ b/config/routes/report.rb @@ -2,9 +2,9 @@ Zammad::Application.routes.draw do api_path = Rails.configuration.api_path # reports - match api_path + '/reports/config', to: 'reports#config', via: :get - match api_path + '/reports/generate', to: 'reports#generate', via: :post - match api_path + '/reports/sets', to: 'reports#sets', via: :post + match api_path + '/reports/config', to: 'reports#reporting_config', via: :get + match api_path + '/reports/generate', to: 'reports#generate', via: :post + match api_path + '/reports/sets', to: 'reports#sets', via: [:post, :get] # report_profiles match api_path + '/report_profiles', to: 'report_profiles#index', via: :get diff --git a/test/integration/report_test.rb b/test/integration/report_test.rb index 04794f2ea..16d14bd61 100644 --- a/test/integration/report_test.rb +++ b/test/integration/report_test.rb @@ -24,7 +24,7 @@ class ReportTest < ActiveSupport::TestCase # drop/create indexes #Rake::Task["searchindex:drop"].execute #Rake::Task["searchindex:create"].execute - #system('rake searchindex:rebuild') + system('rake searchindex:rebuild') Group.create_if_not_exists( name: 'Report Test',