2016-10-19 03:11:36 +00:00
|
|
|
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
|
2015-04-27 23:19:26 +00:00
|
|
|
module Ticket::ScreenOptions
|
2013-08-17 21:10:11 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
list attributes
|
|
|
|
|
|
|
|
result = Ticket::ScreenOptions.attributes_to_change(
|
2015-06-30 22:26:24 +00:00
|
|
|
ticket_id: 123,
|
|
|
|
article_id: 123,
|
2013-08-17 21:10:11 +00:00
|
|
|
|
2015-06-30 22:26:24 +00:00
|
|
|
ticket: ticket_model,
|
2017-06-16 20:43:09 +00:00
|
|
|
current_user: User.find(123),
|
2013-08-17 21:10:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = {
|
2015-06-30 22:26:24 +00:00
|
|
|
type_id: type_ids,
|
|
|
|
state_id: state_ids,
|
|
|
|
priority_id: priority_ids,
|
|
|
|
owner_id: owner_ids,
|
|
|
|
group_id: group_ids,
|
|
|
|
group_id__owner_id: groups_users,
|
2013-08-17 21:10:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
def self.attributes_to_change(params)
|
2017-06-16 20:43:09 +00:00
|
|
|
raise 'current_user param needed' if !params[:current_user]
|
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
if params[:ticket_id]
|
2015-11-17 14:04:36 +00:00
|
|
|
params[:ticket] = Ticket.find(params[:ticket_id])
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
|
|
|
if params[:article_id]
|
2015-11-17 14:04:36 +00:00
|
|
|
params[:article] = Ticket::Article.find(params[:article_id])
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
2013-08-17 21:10:11 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
filter = {}
|
|
|
|
assets = {}
|
2014-09-09 23:42:20 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# get ticket states
|
|
|
|
state_ids = []
|
|
|
|
if params[:ticket]
|
|
|
|
state_type = params[:ticket].state.state_type
|
|
|
|
end
|
|
|
|
state_types = ['open', 'closed', 'pending action', 'pending reminder']
|
|
|
|
if state_type && !state_types.include?(state_type.name)
|
|
|
|
state_ids.push params[:ticket].state.id
|
|
|
|
end
|
2017-06-16 20:43:09 +00:00
|
|
|
state_types.each do |type|
|
2015-11-17 14:04:36 +00:00
|
|
|
state_type = Ticket::StateType.find_by(name: type)
|
2015-05-07 09:04:40 +00:00
|
|
|
next if !state_type
|
2017-06-16 20:43:09 +00:00
|
|
|
state_type.states.each do |state|
|
2015-05-07 09:04:40 +00:00
|
|
|
assets = state.assets(assets)
|
|
|
|
state_ids.push state.id
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
|
|
|
end
|
2015-04-27 23:19:26 +00:00
|
|
|
filter[:state_id] = state_ids
|
|
|
|
|
|
|
|
# get priorities
|
|
|
|
priority_ids = []
|
2017-06-16 20:43:09 +00:00
|
|
|
Ticket::Priority.where(active: true).each do |priority|
|
2015-04-27 23:19:26 +00:00
|
|
|
assets = priority.assets(assets)
|
|
|
|
priority_ids.push priority.id
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
2015-04-27 23:19:26 +00:00
|
|
|
filter[:priority_id] = priority_ids
|
|
|
|
|
|
|
|
type_ids = []
|
|
|
|
if params[:ticket]
|
2015-05-07 07:23:16 +00:00
|
|
|
types = %w(note phone)
|
2015-04-27 23:19:26 +00:00
|
|
|
if params[:ticket].group.email_address_id
|
|
|
|
types.push 'email'
|
2013-08-17 21:10:11 +00:00
|
|
|
end
|
2017-06-16 20:43:09 +00:00
|
|
|
types.each do |type_name|
|
2015-04-27 23:19:26 +00:00
|
|
|
type = Ticket::Article::Type.lookup( name: type_name )
|
2017-06-16 20:43:09 +00:00
|
|
|
next if type.blank?
|
|
|
|
type_ids.push type.id
|
|
|
|
end
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
|
|
|
filter[:type_id] = type_ids
|
|
|
|
|
|
|
|
# get group / user relations
|
|
|
|
agents = {}
|
2017-06-16 20:43:09 +00:00
|
|
|
User.with_permissions('ticket.agent').each do |user|
|
2015-04-27 23:19:26 +00:00
|
|
|
agents[ user.id ] = 1
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
2015-04-27 23:19:26 +00:00
|
|
|
|
|
|
|
dependencies = { group_id: { '' => { owner_id: [] } } }
|
2017-06-16 20:43:09 +00:00
|
|
|
|
|
|
|
filter[:group_id] = []
|
|
|
|
groups = if params[:current_user].permissions?('ticket.agent')
|
|
|
|
params[:current_user].groups_access('create')
|
|
|
|
else
|
|
|
|
Group.where(active: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
groups.each do |group|
|
|
|
|
filter[:group_id].push group.id
|
2015-04-27 23:19:26 +00:00
|
|
|
assets = group.assets(assets)
|
|
|
|
dependencies[:group_id][group.id] = { owner_id: [] }
|
2017-06-16 20:43:09 +00:00
|
|
|
|
|
|
|
User.group_access(group.id, 'full').each do |user|
|
2015-04-27 23:19:26 +00:00
|
|
|
next if !agents[ user.id ]
|
|
|
|
assets = user.assets(assets)
|
|
|
|
dependencies[:group_id][ group.id ][ :owner_id ].push user.id
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
|
|
|
end
|
2015-04-27 21:27:51 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
{
|
2017-06-16 20:43:09 +00:00
|
|
|
assets: assets,
|
2015-11-17 14:04:36 +00:00
|
|
|
form_meta: {
|
2017-06-16 20:43:09 +00:00
|
|
|
filter: filter,
|
2015-11-17 14:04:36 +00:00
|
|
|
dependencies: dependencies,
|
|
|
|
}
|
2015-04-27 23:19:26 +00:00
|
|
|
}
|
|
|
|
end
|
2013-08-17 21:10:11 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
list tickets by customer groupd in state categroie open and closed
|
|
|
|
|
|
|
|
result = Ticket::ScreenOptions.list_by_customer(
|
2015-06-30 22:26:24 +00:00
|
|
|
customer_id: 123,
|
|
|
|
limit: 15, # optional, default 15
|
2013-08-17 21:10:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = {
|
2015-06-30 22:26:24 +00:00
|
|
|
ticket_ids_open: tickets_open,
|
|
|
|
ticket_ids_closed: tickets_closed,
|
|
|
|
assets: { ...list of assets... },
|
2013-08-17 21:10:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
def self.list_by_customer(data)
|
|
|
|
|
|
|
|
# get closed/open states
|
2017-03-27 14:06:18 +00:00
|
|
|
state_list_open = Ticket::State.by_category(:open)
|
|
|
|
state_list_closed = Ticket::State.by_category(:closed)
|
2015-04-27 23:19:26 +00:00
|
|
|
|
|
|
|
# get tickets
|
|
|
|
tickets_open = Ticket.where(
|
|
|
|
customer_id: data[:customer_id],
|
|
|
|
state_id: state_list_open
|
|
|
|
).limit( data[:limit] || 15 ).order('created_at DESC')
|
|
|
|
assets = {}
|
|
|
|
ticket_ids_open = []
|
2016-06-30 20:04:48 +00:00
|
|
|
tickets_open.each { |ticket|
|
2015-04-27 23:19:26 +00:00
|
|
|
ticket_ids_open.push ticket.id
|
|
|
|
assets = ticket.assets(assets)
|
|
|
|
}
|
|
|
|
|
|
|
|
tickets_closed = Ticket.where(
|
|
|
|
customer_id: data[:customer_id],
|
|
|
|
state_id: state_list_closed
|
|
|
|
).limit( data[:limit] || 15 ).order('created_at DESC')
|
|
|
|
ticket_ids_closed = []
|
2016-06-30 20:04:48 +00:00
|
|
|
tickets_closed.each { |ticket|
|
2015-04-27 23:19:26 +00:00
|
|
|
ticket_ids_closed.push ticket.id
|
|
|
|
assets = ticket.assets(assets)
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
ticket_ids_open: ticket_ids_open,
|
|
|
|
ticket_ids_closed: ticket_ids_closed,
|
|
|
|
assets: assets,
|
|
|
|
}
|
2015-04-27 21:27:51 +00:00
|
|
|
end
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|