2017-01-16 13:34:44 +00:00
|
|
|
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
|
|
|
class TimeAccountingsController < ApplicationController
|
2017-02-15 12:29:25 +00:00
|
|
|
prepend_before_action { authentication_check(permission: 'admin.time_accounting') }
|
2017-01-16 13:34:44 +00:00
|
|
|
|
|
|
|
def by_ticket
|
|
|
|
|
|
|
|
year = params[:year] || Time.zone.now.year
|
|
|
|
month = params[:month] || Time.zone.now.month
|
|
|
|
|
2017-09-05 19:24:43 +00:00
|
|
|
start_periode = Time.zone.parse("#{year}-#{month}-01")
|
2017-01-16 13:34:44 +00:00
|
|
|
end_periode = start_periode.end_of_month
|
|
|
|
|
|
|
|
time_unit = {}
|
2017-10-01 12:25:52 +00:00
|
|
|
Ticket::TimeAccounting.where('created_at >= ? AND created_at <= ?', start_periode, end_periode).pluck(:ticket_id, :time_unit, :created_by_id).each do |record|
|
2017-01-16 13:34:44 +00:00
|
|
|
if !time_unit[record[0]]
|
|
|
|
time_unit[record[0]] = {
|
|
|
|
time_unit: 0,
|
2018-12-19 17:31:51 +00:00
|
|
|
agent_id: record[2],
|
2017-01-16 13:34:44 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
time_unit[record[0]][:time_unit] += record[1]
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-01-16 13:34:44 +00:00
|
|
|
customers = {}
|
|
|
|
organizations = {}
|
|
|
|
agents = {}
|
|
|
|
results = []
|
2017-10-01 12:25:52 +00:00
|
|
|
time_unit.each do |ticket_id, local_time_unit|
|
2017-01-16 13:34:44 +00:00
|
|
|
ticket = Ticket.lookup(id: ticket_id)
|
|
|
|
next if !ticket
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-01-16 13:34:44 +00:00
|
|
|
if !customers[ticket.customer_id]
|
|
|
|
customers[ticket.customer_id] = '-'
|
|
|
|
if ticket.customer_id
|
|
|
|
customer_user = User.lookup(id: ticket.customer_id)
|
|
|
|
if customer_user
|
|
|
|
customers[ticket.customer_id] = customer_user.fullname
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if !organizations[ticket.organization_id]
|
|
|
|
organizations[ticket.organization_id] = '-'
|
|
|
|
if ticket.organization_id
|
|
|
|
organization = Organization.lookup(id: ticket.organization_id)
|
|
|
|
if organization
|
|
|
|
organizations[ticket.organization_id] = organization.name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-09-28 06:49:06 +00:00
|
|
|
if !agents[local_time_unit[:agent_id]]
|
2017-01-16 13:34:44 +00:00
|
|
|
agent_user = User.lookup(id: local_time_unit[:agent_id])
|
|
|
|
if agent_user
|
|
|
|
agents[local_time_unit[:agent_id]] = agent_user.fullname
|
|
|
|
end
|
|
|
|
end
|
|
|
|
result = {
|
2018-12-19 17:31:51 +00:00
|
|
|
ticket: ticket.attributes,
|
|
|
|
time_unit: local_time_unit[:time_unit],
|
|
|
|
customer: customers[ticket.customer_id],
|
2017-01-16 13:34:44 +00:00
|
|
|
organization: organizations[ticket.organization_id],
|
2018-12-19 17:31:51 +00:00
|
|
|
agent: agents[local_time_unit[:agent_id]],
|
2017-01-16 13:34:44 +00:00
|
|
|
}
|
|
|
|
results.push result
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-04-21 06:47:28 +00:00
|
|
|
|
|
|
|
if params[:download]
|
|
|
|
header = [
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'Ticket#',
|
2017-04-21 06:47:28 +00:00
|
|
|
width: 15,
|
|
|
|
},
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'Title',
|
2017-04-21 06:47:28 +00:00
|
|
|
width: 30,
|
|
|
|
},
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'Customer',
|
2017-04-21 06:47:28 +00:00
|
|
|
width: 20,
|
|
|
|
},
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'Organization',
|
2017-04-21 06:47:28 +00:00
|
|
|
width: 20,
|
|
|
|
},
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'Agent',
|
2017-04-21 06:47:28 +00:00
|
|
|
width: 20,
|
|
|
|
},
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'Time Units',
|
2017-04-21 06:47:28 +00:00
|
|
|
width: 10,
|
|
|
|
},
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'Time Units Total',
|
2017-04-21 06:47:28 +00:00
|
|
|
width: 10,
|
|
|
|
},
|
2017-07-06 09:05:28 +00:00
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'Created at',
|
2019-06-24 07:28:34 +00:00
|
|
|
width: 18,
|
2017-07-06 09:05:28 +00:00
|
|
|
},
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'Closed at',
|
2019-06-24 07:28:34 +00:00
|
|
|
width: 18,
|
2017-07-06 09:05:28 +00:00
|
|
|
},
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'Close Escalation At',
|
2019-06-24 07:28:34 +00:00
|
|
|
width: 18,
|
2017-07-06 09:05:28 +00:00
|
|
|
},
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'Close In Min',
|
2017-07-06 09:05:28 +00:00
|
|
|
width: 10,
|
|
|
|
},
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'Close Diff In Min',
|
2017-07-06 09:05:28 +00:00
|
|
|
width: 10,
|
|
|
|
},
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'First Response At',
|
2019-06-24 07:28:34 +00:00
|
|
|
width: 18,
|
2017-07-06 09:05:28 +00:00
|
|
|
},
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'First Response Escalation At',
|
2019-06-24 07:28:34 +00:00
|
|
|
width: 18,
|
2017-07-06 09:05:28 +00:00
|
|
|
},
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'First Response In Min',
|
2017-07-06 09:05:28 +00:00
|
|
|
width: 10,
|
|
|
|
},
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'First Response Diff In Min',
|
2017-07-06 09:05:28 +00:00
|
|
|
width: 10,
|
|
|
|
},
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'Update Escalation At',
|
2019-06-24 07:28:34 +00:00
|
|
|
width: 18,
|
2017-07-06 09:05:28 +00:00
|
|
|
},
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'Update In Min',
|
2017-07-06 09:05:28 +00:00
|
|
|
width: 10,
|
|
|
|
},
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'Update Diff In Min',
|
2017-07-06 09:05:28 +00:00
|
|
|
width: 10,
|
|
|
|
},
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'Last Contact At',
|
2019-06-24 07:28:34 +00:00
|
|
|
width: 18,
|
2017-07-06 09:05:28 +00:00
|
|
|
},
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'Last Contact Agent At',
|
2019-06-24 07:28:34 +00:00
|
|
|
width: 18,
|
2017-07-06 09:05:28 +00:00
|
|
|
},
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'Last Contact Customer At',
|
2019-06-24 07:28:34 +00:00
|
|
|
width: 18,
|
2017-07-06 09:05:28 +00:00
|
|
|
},
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'Article Count',
|
2017-07-06 09:05:28 +00:00
|
|
|
width: 10,
|
|
|
|
},
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'Escalation At',
|
2019-06-24 07:28:34 +00:00
|
|
|
width: 18,
|
2017-07-06 09:05:28 +00:00
|
|
|
},
|
2017-04-21 06:47:28 +00:00
|
|
|
]
|
2018-12-19 17:31:51 +00:00
|
|
|
objects = ObjectManager::Attribute.where(editable: true,
|
|
|
|
active: true,
|
|
|
|
to_create: false,
|
2018-04-27 13:35:20 +00:00
|
|
|
object_lookup_id: ObjectLookup.lookup(name: 'Ticket').id)
|
|
|
|
.pluck(:name, :display, :data_type, :data_option)
|
|
|
|
.map { |name, display, data_type, data_option| { name: name, display: display, data_type: data_type, data_option: data_option } }
|
|
|
|
objects.each do |object|
|
2019-06-24 07:28:34 +00:00
|
|
|
header.push({ name: object[:display], width: 18 })
|
2018-04-27 13:35:20 +00:00
|
|
|
end
|
|
|
|
|
2017-04-21 06:47:28 +00:00
|
|
|
result = []
|
2017-10-01 12:25:52 +00:00
|
|
|
results.each do |row|
|
2017-04-21 06:47:28 +00:00
|
|
|
result_row = [
|
|
|
|
row[:ticket]['number'],
|
|
|
|
row[:ticket]['title'],
|
|
|
|
row[:customer],
|
|
|
|
row[:organization],
|
|
|
|
row[:agent],
|
|
|
|
row[:time_unit],
|
|
|
|
row[:ticket]['time_unit'],
|
2017-07-06 09:05:28 +00:00
|
|
|
row[:ticket]['created_at'],
|
|
|
|
row[:ticket]['close_at'],
|
|
|
|
row[:ticket]['close_escalation_at'],
|
|
|
|
row[:ticket]['close_in_min'],
|
|
|
|
row[:ticket]['close_diff_in_min'],
|
|
|
|
row[:ticket]['first_response_at'],
|
|
|
|
row[:ticket]['first_response_escalation_at'],
|
|
|
|
row[:ticket]['first_response_in_min'],
|
|
|
|
row[:ticket]['first_response_diff_in_min'],
|
|
|
|
row[:ticket]['update_escalation_at'],
|
|
|
|
row[:ticket]['update_in_min'],
|
|
|
|
row[:ticket]['update_diff_in_min'],
|
|
|
|
row[:ticket]['last_contact_at'],
|
|
|
|
row[:ticket]['last_contact_agent_at'],
|
|
|
|
row[:ticket]['last_contact_customer_at'],
|
|
|
|
row[:ticket]['article_count'],
|
|
|
|
row[:ticket]['escalation_at'],
|
2017-04-21 06:47:28 +00:00
|
|
|
]
|
2018-04-27 13:35:20 +00:00
|
|
|
|
|
|
|
# Object Manager attributes
|
|
|
|
# We already queried ObjectManager::Attributes, so we just use objects
|
|
|
|
objects.each do |object|
|
|
|
|
key = object[:name]
|
|
|
|
case object[:data_type]
|
|
|
|
when 'boolean', 'select'
|
2019-06-24 07:28:34 +00:00
|
|
|
value = row[:ticket][key]
|
|
|
|
if object[:data_option] && object[:data_option]['options'] && object[:data_option]['options'][row[:ticket][key]]
|
|
|
|
value = object[:data_option]['options'][row[:ticket][key]]
|
|
|
|
end
|
2018-04-27 13:35:20 +00:00
|
|
|
value.present? ? result_row.push(value) : result_row.push('')
|
|
|
|
else
|
|
|
|
# for text, integer and tree select
|
|
|
|
row[:ticket][key].present? ? result_row.push(row[:ticket][key]) : result_row.push('')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-21 06:47:28 +00:00
|
|
|
result.push result_row
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-04-21 06:47:28 +00:00
|
|
|
content = sheet("By Ticket #{year}-#{month}", header, result)
|
|
|
|
send_data(
|
|
|
|
content,
|
2018-12-19 17:31:51 +00:00
|
|
|
filename: "by_ticket-#{year}-#{month}.xls",
|
|
|
|
type: 'application/vnd.ms-excel',
|
2017-04-21 06:47:28 +00:00
|
|
|
disposition: 'attachment'
|
|
|
|
)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2017-01-16 13:34:44 +00:00
|
|
|
render json: results
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_customer
|
|
|
|
|
|
|
|
year = params[:year] || Time.zone.now.year
|
|
|
|
month = params[:month] || Time.zone.now.month
|
|
|
|
|
2017-09-05 19:24:43 +00:00
|
|
|
start_periode = Time.zone.parse("#{year}-#{month}-01")
|
2017-01-16 13:34:44 +00:00
|
|
|
end_periode = start_periode.end_of_month
|
|
|
|
|
|
|
|
time_unit = {}
|
2017-10-01 12:25:52 +00:00
|
|
|
Ticket::TimeAccounting.where('created_at >= ? AND created_at <= ?', start_periode, end_periode).pluck(:ticket_id, :time_unit, :created_by_id).each do |record|
|
2017-01-16 13:34:44 +00:00
|
|
|
if !time_unit[record[0]]
|
|
|
|
time_unit[record[0]] = {
|
|
|
|
time_unit: 0,
|
2018-12-19 17:31:51 +00:00
|
|
|
agent_id: record[2],
|
2017-01-16 13:34:44 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
time_unit[record[0]][:time_unit] += record[1]
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-01-16 13:34:44 +00:00
|
|
|
|
|
|
|
customers = {}
|
2017-10-01 12:25:52 +00:00
|
|
|
time_unit.each do |ticket_id, local_time_unit|
|
2017-01-16 13:34:44 +00:00
|
|
|
ticket = Ticket.lookup(id: ticket_id)
|
|
|
|
next if !ticket
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-01-16 13:34:44 +00:00
|
|
|
if !customers[ticket.customer_id]
|
|
|
|
organization = nil
|
|
|
|
if ticket.organization_id
|
|
|
|
organization = Organization.lookup(id: ticket.organization_id).attributes
|
|
|
|
end
|
|
|
|
customers[ticket.customer_id] = {
|
2018-12-19 17:31:51 +00:00
|
|
|
customer: User.lookup(id: ticket.customer_id).attributes,
|
2017-01-16 13:34:44 +00:00
|
|
|
organization: organization,
|
2018-12-19 17:31:51 +00:00
|
|
|
time_unit: local_time_unit[:time_unit],
|
2017-01-16 13:34:44 +00:00
|
|
|
}
|
|
|
|
next
|
|
|
|
end
|
|
|
|
customers[ticket.customer_id][:time_unit] += local_time_unit[:time_unit]
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-01-16 13:34:44 +00:00
|
|
|
results = []
|
2017-11-23 08:09:44 +00:00
|
|
|
customers.each_value do |content|
|
2017-01-16 13:34:44 +00:00
|
|
|
results.push content
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-04-21 06:47:28 +00:00
|
|
|
|
|
|
|
if params[:download]
|
|
|
|
header = [
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'Customer',
|
2017-04-21 06:47:28 +00:00
|
|
|
width: 30,
|
|
|
|
},
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'Organization',
|
2017-04-21 06:47:28 +00:00
|
|
|
width: 30,
|
|
|
|
},
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'Time Units',
|
2017-04-21 06:47:28 +00:00
|
|
|
width: 10,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
result = []
|
2017-10-01 12:25:52 +00:00
|
|
|
results.each do |row|
|
2017-04-21 06:47:28 +00:00
|
|
|
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
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-04-21 06:47:28 +00:00
|
|
|
content = sheet("By Customer #{year}-#{month}", header, result)
|
|
|
|
send_data(
|
|
|
|
content,
|
2018-12-19 17:31:51 +00:00
|
|
|
filename: "by_customer-#{year}-#{month}.xls",
|
|
|
|
type: 'application/vnd.ms-excel',
|
2017-04-21 06:47:28 +00:00
|
|
|
disposition: 'attachment'
|
|
|
|
)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2017-01-16 13:34:44 +00:00
|
|
|
render json: results
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_organization
|
|
|
|
|
|
|
|
year = params[:year] || Time.zone.now.year
|
|
|
|
month = params[:month] || Time.zone.now.month
|
|
|
|
|
2017-09-05 19:24:43 +00:00
|
|
|
start_periode = Time.zone.parse("#{year}-#{month}-01")
|
2017-01-16 13:34:44 +00:00
|
|
|
end_periode = start_periode.end_of_month
|
|
|
|
|
|
|
|
time_unit = {}
|
2017-10-01 12:25:52 +00:00
|
|
|
Ticket::TimeAccounting.where('created_at >= ? AND created_at <= ?', start_periode, end_periode).pluck(:ticket_id, :time_unit, :created_by_id).each do |record|
|
2017-01-16 13:34:44 +00:00
|
|
|
if !time_unit[record[0]]
|
|
|
|
time_unit[record[0]] = {
|
|
|
|
time_unit: 0,
|
2018-12-19 17:31:51 +00:00
|
|
|
agent_id: record[2],
|
2017-01-16 13:34:44 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
time_unit[record[0]][:time_unit] += record[1]
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-01-16 13:34:44 +00:00
|
|
|
|
|
|
|
organizations = {}
|
2017-10-01 12:25:52 +00:00
|
|
|
time_unit.each do |ticket_id, local_time_unit|
|
2017-01-16 13:34:44 +00:00
|
|
|
ticket = Ticket.lookup(id: ticket_id)
|
|
|
|
next if !ticket
|
|
|
|
next if !ticket.organization_id
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-01-16 13:34:44 +00:00
|
|
|
if !organizations[ticket.organization_id]
|
|
|
|
organizations[ticket.organization_id] = {
|
|
|
|
organization: Organization.lookup(id: ticket.organization_id).attributes,
|
2018-12-19 17:31:51 +00:00
|
|
|
time_unit: local_time_unit[:time_unit],
|
2017-01-16 13:34:44 +00:00
|
|
|
}
|
|
|
|
next
|
|
|
|
end
|
|
|
|
organizations[ticket.organization_id][:time_unit] += local_time_unit[:time_unit]
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-01-16 13:34:44 +00:00
|
|
|
results = []
|
2017-11-23 08:09:44 +00:00
|
|
|
organizations.each_value do |content|
|
2017-01-16 13:34:44 +00:00
|
|
|
results.push content
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-04-21 06:47:28 +00:00
|
|
|
|
|
|
|
if params[:download]
|
|
|
|
header = [
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'Organization',
|
2017-04-21 06:47:28 +00:00
|
|
|
width: 40,
|
|
|
|
},
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'Time Units',
|
2017-04-21 06:47:28 +00:00
|
|
|
width: 20,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
result = []
|
2017-10-01 12:25:52 +00:00
|
|
|
results.each do |row|
|
2017-04-21 06:47:28 +00:00
|
|
|
organization_name = ''
|
|
|
|
if row[:organization].present?
|
|
|
|
organization_name = row[:organization]['name']
|
|
|
|
end
|
|
|
|
result_row = [organization_name, row[:time_unit]]
|
|
|
|
result.push result_row
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-04-21 06:47:28 +00:00
|
|
|
content = sheet("By Organization #{year}-#{month}", header, result)
|
|
|
|
send_data(
|
|
|
|
content,
|
2018-12-19 17:31:51 +00:00
|
|
|
filename: "by_organization-#{year}-#{month}.xls",
|
|
|
|
type: 'application/vnd.ms-excel',
|
2017-04-21 06:47:28 +00:00
|
|
|
disposition: 'attachment'
|
|
|
|
)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2017-01-16 13:34:44 +00:00
|
|
|
render json: results
|
|
|
|
end
|
|
|
|
|
2017-04-21 06:47:28 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def sheet(title, header, result)
|
|
|
|
|
2019-06-24 07:28:34 +00:00
|
|
|
params[:timezone] ||= Setting.get('timezone_default')
|
|
|
|
|
2017-04-21 06:47:28 +00:00
|
|
|
# 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')
|
2019-06-24 07:28:34 +00:00
|
|
|
|
|
|
|
format_time = workbook.add_format(num_format: 'yyyy-mm-dd hh:mm:ss')
|
|
|
|
format_date = workbook.add_format(num_format: 'yyyy-mm-dd')
|
|
|
|
|
|
|
|
format_footer = workbook.add_format
|
|
|
|
format_footer.set_italic
|
|
|
|
format_footer.set_color('gray')
|
|
|
|
format_footer.set_size(8)
|
|
|
|
|
|
|
|
worksheet.set_row(0, 18, header.count)
|
2017-04-21 06:47:28 +00:00
|
|
|
|
|
|
|
# Write a formatted and unformatted string, row and column notation.
|
2018-01-22 14:37:42 +00:00
|
|
|
worksheet.write_string(0, 0, title, format)
|
2017-04-21 06:47:28 +00:00
|
|
|
|
|
|
|
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
|
2017-10-01 12:25:52 +00:00
|
|
|
header.each do |item|
|
2017-04-21 06:47:28 +00:00
|
|
|
if item[:width]
|
|
|
|
worksheet.set_column(count, count, item[:width])
|
|
|
|
end
|
2018-01-22 14:37:42 +00:00
|
|
|
worksheet.write_string(2, count, item[:name], format_header)
|
2017-04-21 06:47:28 +00:00
|
|
|
count += 1
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-04-21 06:47:28 +00:00
|
|
|
|
|
|
|
row_count = 2
|
2017-10-01 12:25:52 +00:00
|
|
|
result.each do |row|
|
2017-04-21 06:47:28 +00:00
|
|
|
row_count += 1
|
|
|
|
row_item_count = 0
|
2017-10-01 12:25:52 +00:00
|
|
|
row.each do |item|
|
2019-06-24 07:28:34 +00:00
|
|
|
if item.acts_like?(:time)
|
|
|
|
worksheet.write_date_time(row_count, row_item_count, time_in_localtime_for_excel(item, params[:timezone]), format_time) if item.present?
|
|
|
|
elsif item.acts_like?(:date)
|
|
|
|
worksheet.write_date_time(row_count, row_item_count, item.to_s, format_date) if item.present?
|
|
|
|
elsif item.is_a?(Integer) || item.is_a?(Float)
|
|
|
|
worksheet.write_number(row_count, row_item_count, item)
|
2018-01-22 14:37:42 +00:00
|
|
|
else
|
2019-06-24 07:28:34 +00:00
|
|
|
worksheet.write_string(row_count, row_item_count, item.to_s)
|
2018-01-22 14:37:42 +00:00
|
|
|
end
|
2017-04-21 06:47:28 +00:00
|
|
|
row_item_count += 1
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
|
|
|
end
|
2017-04-21 06:47:28 +00:00
|
|
|
|
2019-06-24 07:28:34 +00:00
|
|
|
row_count += 2
|
|
|
|
worksheet.write_string(row_count, 0, "#{Translation.translate(current_user.locale, 'Timezone')}: #{params[:timezone]}", format_footer)
|
|
|
|
|
2017-04-21 06:47:28 +00:00
|
|
|
workbook.close
|
|
|
|
|
|
|
|
# read file again
|
|
|
|
file = File.new(temp_file, 'r')
|
|
|
|
contents = file.read
|
|
|
|
file.close
|
|
|
|
contents
|
|
|
|
end
|
|
|
|
|
2019-06-24 07:28:34 +00:00
|
|
|
def time_in_localtime_for_excel(time, timezone)
|
|
|
|
return if time.blank?
|
|
|
|
|
|
|
|
if timezone.present?
|
|
|
|
offset = time.in_time_zone(timezone).utc_offset
|
|
|
|
time -= offset
|
|
|
|
end
|
|
|
|
time.utc.iso8601.to_s.sub(/Z$/, '')
|
|
|
|
end
|
2017-01-16 13:34:44 +00:00
|
|
|
end
|