2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
2017-01-16 13:34:44 +00:00
|
|
|
|
|
|
|
class TimeAccountingsController < ApplicationController
|
2020-03-19 09:39:51 +00:00
|
|
|
prepend_before_action { authentication_check && authorize! }
|
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
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2019-09-16 20:36:03 +00:00
|
|
|
if !params[:download]
|
|
|
|
customers = {}
|
|
|
|
organizations = {}
|
|
|
|
agents = {}
|
|
|
|
results = []
|
|
|
|
time_unit.each do |ticket_id, local_time_unit|
|
|
|
|
ticket = Ticket.lookup(id: ticket_id)
|
|
|
|
next if !ticket
|
|
|
|
|
|
|
|
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
|
2017-01-16 13:34:44 +00:00
|
|
|
end
|
|
|
|
end
|
2019-09-16 20:36:03 +00:00
|
|
|
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
|
2017-01-16 13:34:44 +00:00
|
|
|
end
|
|
|
|
end
|
2019-09-16 20:36:03 +00:00
|
|
|
if !agents[local_time_unit[:agent_id]]
|
|
|
|
agent_user = User.lookup(id: local_time_unit[:agent_id])
|
|
|
|
if agent_user
|
|
|
|
agents[local_time_unit[:agent_id]] = agent_user.fullname
|
2018-04-27 13:35:20 +00:00
|
|
|
end
|
|
|
|
end
|
2019-09-16 20:36:03 +00:00
|
|
|
result = {
|
|
|
|
ticket: ticket.attributes,
|
|
|
|
time_unit: local_time_unit[:time_unit],
|
|
|
|
customer: customers[ticket.customer_id],
|
|
|
|
organization: organizations[ticket.organization_id],
|
|
|
|
agent: agents[local_time_unit[:agent_id]],
|
|
|
|
}
|
|
|
|
results.push result
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2019-09-16 20:36:03 +00:00
|
|
|
render json: results
|
2017-04-21 06:47:28 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2019-09-16 20:36:03 +00:00
|
|
|
ticket_ids = []
|
|
|
|
additional_attributes = []
|
|
|
|
additional_attributes_header = [{ display: 'Time Units', name: 'time_unit_for_range', width: 10, data_type: 'float' }]
|
|
|
|
time_unit.each do |ticket_id, local_time_unit|
|
|
|
|
ticket_ids.push ticket_id
|
|
|
|
additional_attribute = {
|
|
|
|
time_unit_for_range: local_time_unit[:time_unit],
|
|
|
|
}
|
|
|
|
additional_attributes.push additional_attribute
|
|
|
|
end
|
|
|
|
|
|
|
|
excel = ExcelSheet::Ticket.new(
|
|
|
|
title: "Tickets: #{year}-#{month}",
|
|
|
|
ticket_ids: ticket_ids,
|
|
|
|
additional_attributes: additional_attributes,
|
|
|
|
additional_attributes_header: additional_attributes_header,
|
|
|
|
timezone: params[:timezone],
|
|
|
|
locale: current_user.locale,
|
|
|
|
)
|
|
|
|
|
|
|
|
send_data(
|
|
|
|
excel.content,
|
|
|
|
filename: "by_ticket-#{year}-#{month}.xls",
|
|
|
|
type: 'application/vnd.ms-excel',
|
|
|
|
disposition: 'attachment'
|
|
|
|
)
|
2017-01-16 13:34:44 +00:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
{
|
2019-09-16 20:36:03 +00:00
|
|
|
name: 'Time Units',
|
|
|
|
width: 10,
|
|
|
|
data_type: 'float'
|
2017-04-21 06:47:28 +00:00
|
|
|
}
|
|
|
|
]
|
2019-09-16 20:36:03 +00:00
|
|
|
records = []
|
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]]
|
2019-09-16 20:36:03 +00:00
|
|
|
records.push result_row
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2019-09-16 20:36:03 +00:00
|
|
|
|
|
|
|
excel = ExcelSheet.new(
|
|
|
|
title: "By Customer #{year}-#{month}",
|
|
|
|
header: header,
|
|
|
|
records: records,
|
|
|
|
timezone: params[:timezone],
|
|
|
|
locale: current_user.locale,
|
|
|
|
)
|
2017-04-21 06:47:28 +00:00
|
|
|
send_data(
|
2019-09-16 20:36:03 +00:00
|
|
|
excel.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,
|
|
|
|
},
|
|
|
|
{
|
2019-09-16 20:36:03 +00:00
|
|
|
name: 'Time Units',
|
|
|
|
width: 20,
|
|
|
|
data_type: 'float',
|
2017-04-21 06:47:28 +00:00
|
|
|
}
|
|
|
|
]
|
2019-09-16 20:36:03 +00:00
|
|
|
records = []
|
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]]
|
2019-09-16 20:36:03 +00:00
|
|
|
records.push result_row
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2019-09-16 20:36:03 +00:00
|
|
|
|
|
|
|
excel = ExcelSheet.new(
|
|
|
|
title: "By Organization #{year}-#{month}",
|
|
|
|
header: header,
|
|
|
|
records: records,
|
|
|
|
timezone: params[:timezone],
|
|
|
|
locale: current_user.locale,
|
|
|
|
)
|
2017-04-21 06:47:28 +00:00
|
|
|
send_data(
|
2019-09-16 20:36:03 +00:00
|
|
|
excel.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
|
|
|
|
end
|