Added possibility to download time accounting as excel format.

This commit is contained in:
Martin Edenhofer 2017-04-21 08:47:28 +02:00
parent b81378219d
commit 5239515096
3 changed files with 179 additions and 3 deletions

View file

@ -99,6 +99,7 @@ class Index extends App.ControllerSubContent
timeRangeMonth: timeRangeMonth
year: @year
month: @month
apiPath: @apiPath
)
configure_attributes = [

View file

@ -35,13 +35,13 @@
<% end %>
</div>
</div>
<h3><%- @T('Ticket') %></h3>
<h3><%- @T('Ticket') %> <a href="<%- @apiPath %>/time_accounting/log/by_ticket/<%= @year %>/<%= @month %>?download=true" class="js-downloadByTicket" data-type="attachment"><%- @Icon('download') %></a></h3>
<div class="js-tableTicket"></div>
<br>
<h3><%- @T('Customer') %></h3>
<h3><%- @T('Customer') %> <a href="<%- @apiPath %>/time_accounting/log/by_customer/<%= @year %>/<%= @month %>?download=true" class="js-downloadByCustomer" data-type="attachment"><%- @Icon('download') %></a></h3>
<div class="js-tableCustomer"></div>
<br>
<h3><%- @T('Organization') %></h3>
<h3><%- @T('Organization') %> <a href="<%- @apiPath %>/time_accounting/log/by_organization/<%= @year %>/<%= @month %>?download=true" class="js-downloadByOrganization" data-type="attachment"><%- @Icon('download') %></a></h3>
<div class="js-tableOrganization"></div>
</div>

View file

@ -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