diff --git a/app/assets/javascripts/app/controllers/time_accounting.coffee b/app/assets/javascripts/app/controllers/time_accounting.coffee index c7b34172c..fbd5058a2 100644 --- a/app/assets/javascripts/app/controllers/time_accounting.coffee +++ b/app/assets/javascripts/app/controllers/time_accounting.coffee @@ -99,6 +99,7 @@ class Index extends App.ControllerSubContent timeRangeMonth: timeRangeMonth year: @year month: @month + apiPath: @apiPath ) configure_attributes = [ diff --git a/app/assets/javascripts/app/views/time_accounting/index.jst.eco b/app/assets/javascripts/app/views/time_accounting/index.jst.eco index c4f988651..da28bb51d 100644 --- a/app/assets/javascripts/app/views/time_accounting/index.jst.eco +++ b/app/assets/javascripts/app/views/time_accounting/index.jst.eco @@ -35,13 +35,13 @@ <% end %> -

<%- @T('Ticket') %>

+

<%- @T('Ticket') %> <%- @Icon('download') %>


-

<%- @T('Customer') %>

+

<%- @T('Customer') %> <%- @Icon('download') %>


-

<%- @T('Organization') %>

+

<%- @T('Organization') %> <%- @Icon('download') %>

diff --git a/app/controllers/time_accountings_controller.rb b/app/controllers/time_accountings_controller.rb index 40ce3c1e8..f2acb1a09 100644 --- a/app/controllers/time_accountings_controller.rb +++ b/app/controllers/time_accountings_controller.rb @@ -62,6 +62,61 @@ class TimeAccountingsController < ApplicationController } results.push result } + + if params[:download] + header = [ + { + name: 'Ticket#', + width: 15, + }, + { + name: 'Title', + width: 30, + }, + { + name: 'Customer', + width: 20, + }, + { + name: 'Organization', + width: 20, + }, + { + name: 'Agent', + width: 20, + }, + { + name: 'Time Units', + width: 10, + }, + { + name: 'Time Units Total', + width: 10, + }, + ] + result = [] + results.each { |row| + result_row = [ + row[:ticket]['number'], + row[:ticket]['title'], + row[:customer], + row[:organization], + row[:agent], + row[:time_unit], + row[:ticket]['time_unit'], + ] + result.push result_row + } + content = sheet("By Ticket #{year}-#{month}", header, result) + send_data( + content, + filename: "by_ticket-#{year}-#{month}.xls", + type: 'application/vnd.ms-excel', + disposition: 'attachment' + ) + return + end + render json: results end @@ -106,6 +161,42 @@ class TimeAccountingsController < ApplicationController customers.each { |_customer_id, content| results.push content } + + if params[:download] + header = [ + { + name: 'Customer', + width: 30, + }, + { + name: 'Organization', + width: 30, + }, + { + name: 'Time Units', + width: 10, + } + ] + result = [] + results.each { |row| + customer_name = User.find(row[:customer]['id']).fullname + organization_name = '' + if row[:organization].present? + organization_name = row[:organization]['name'] + end + result_row = [customer_name, organization_name, row[:time_unit]] + result.push result_row + } + content = sheet("By Customer #{year}-#{month}", header, result) + send_data( + content, + filename: "by_customer-#{year}-#{month}.xls", + type: 'application/vnd.ms-excel', + disposition: 'attachment' + ) + return + end + render json: results end @@ -146,7 +237,91 @@ class TimeAccountingsController < ApplicationController organizations.each { |_customer_id, content| results.push content } + + if params[:download] + header = [ + { + name: 'Organization', + width: 40, + }, + { + name: 'Time Units', + width: 20, + } + ] + result = [] + results.each { |row| + organization_name = '' + if row[:organization].present? + organization_name = row[:organization]['name'] + end + result_row = [organization_name, row[:time_unit]] + result.push result_row + } + content = sheet("By Organization #{year}-#{month}", header, result) + send_data( + content, + filename: "by_organization-#{year}-#{month}.xls", + type: 'application/vnd.ms-excel', + disposition: 'attachment' + ) + return + end + render json: results end + private + + def sheet(title, header, 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 + + # 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, header.count) + + # Write a formatted and unformatted string, row and column notation. + worksheet.write(0, 0, 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') + count = 0 + header.each { |item| + if item[:width] + worksheet.set_column(count, count, item[:width]) + end + worksheet.write(2, count, item[:name], format_header) + count += 1 + } + + row_count = 2 + result.each { |row| + row_count += 1 + row_item_count = 0 + row.each { |item| + worksheet.write(row_count, row_item_count, item) + row_item_count += 1 + } + } + + workbook.close + + # read file again + file = File.new(temp_file, 'r') + contents = file.read + file.close + contents + end + end