2014-02-03 19:23:00 +00:00
|
|
|
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
2015-04-27 23:19:26 +00:00
|
|
|
# rubocop:disable ClassAndModuleChildren
|
|
|
|
module Ticket::Overviews
|
2013-08-17 20:04:57 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
all overview by user
|
|
|
|
|
2013-08-19 10:21:31 +00:00
|
|
|
result = Ticket::Overviews.all(
|
2013-08-17 20:04:57 +00:00
|
|
|
:current_user => User.find(123),
|
|
|
|
)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = [overview1, overview2]
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
def self.all (data)
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# get customer overviews
|
|
|
|
if data[:current_user].is_role('Customer')
|
|
|
|
role = data[:current_user].is_role( 'Customer' )
|
|
|
|
if data[:current_user].organization_id && data[:current_user].organization.shared
|
|
|
|
overviews = Overview.where( role_id: role.id, active: true )
|
|
|
|
else
|
|
|
|
overviews = Overview.where( role_id: role.id, organization_shared: false, active: true )
|
2013-08-17 20:04:57 +00:00
|
|
|
end
|
2015-04-27 23:19:26 +00:00
|
|
|
return overviews
|
2015-04-27 21:27:51 +00:00
|
|
|
end
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# get agent overviews
|
|
|
|
role = data[:current_user].is_role( 'Agent' )
|
|
|
|
return if !role
|
|
|
|
Overview.where( role_id: role.id, active: true )
|
|
|
|
end
|
|
|
|
|
2013-08-17 20:04:57 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
selected overview by user
|
|
|
|
|
2013-08-19 10:21:31 +00:00
|
|
|
result = Ticket::Overviews.list(
|
2013-08-17 20:04:57 +00:00
|
|
|
:current_user => User.find(123),
|
|
|
|
:view => 'some_view_url',
|
|
|
|
)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = {
|
|
|
|
:tickets => tickets, # [ticket1, ticket2, ticket3]
|
|
|
|
:tickets_count => tickets_count, # count of tickets
|
|
|
|
:overview => overview_selected_raw, # overview attributes
|
|
|
|
}
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
def self.list (data)
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
overviews = self.all(data)
|
|
|
|
return if !overviews
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# build up attributes hash
|
|
|
|
overview_selected = nil
|
|
|
|
overview_selected_raw = nil
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
overviews.each { |overview|
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# remember selected view
|
|
|
|
if data[:view] && data[:view] == overview.link
|
|
|
|
overview_selected = overview
|
|
|
|
overview_selected_raw = Marshal.load( Marshal.dump(overview.attributes) )
|
|
|
|
end
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# replace e.g. 'current_user.id' with current_user.id
|
2015-04-30 18:13:25 +00:00
|
|
|
overview.condition.each { |item, value|
|
2015-05-07 09:04:40 +00:00
|
|
|
|
|
|
|
next if !value
|
|
|
|
next if value.class.to_s != 'String'
|
|
|
|
|
|
|
|
parts = value.split( '.', 2 )
|
|
|
|
|
|
|
|
next if !parts[0]
|
|
|
|
next if !parts[1]
|
|
|
|
next if parts[0] != 'current_user'
|
|
|
|
|
|
|
|
overview.condition[item] = data[:current_user][parts[1].to_sym]
|
2013-08-17 20:04:57 +00:00
|
|
|
}
|
2015-04-27 23:19:26 +00:00
|
|
|
}
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
if data[:view] && !overview_selected
|
2015-05-07 11:27:07 +00:00
|
|
|
fail "No such view '#{ data[:view] }'"
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# sortby
|
|
|
|
# prio
|
|
|
|
# state
|
|
|
|
# group
|
|
|
|
# customer
|
|
|
|
|
|
|
|
# order
|
|
|
|
# asc
|
|
|
|
# desc
|
|
|
|
|
|
|
|
# groupby
|
|
|
|
# prio
|
|
|
|
# state
|
|
|
|
# group
|
|
|
|
# customer
|
|
|
|
|
|
|
|
# all = attributes[:myopenassigned]
|
|
|
|
# all.merge( { :group_id => groups } )
|
|
|
|
|
|
|
|
# @tickets = Ticket.where(:group_id => groups, attributes[:myopenassigned] ).limit(params[:limit])
|
|
|
|
# get only tickets with permissions
|
|
|
|
if data[:current_user].is_role('Customer')
|
2015-04-30 17:51:31 +00:00
|
|
|
group_ids = Group.select( 'groups.id' )
|
2015-05-01 12:27:57 +00:00
|
|
|
.where( 'groups.active = ?', true )
|
|
|
|
.map( &:id )
|
2015-04-27 23:19:26 +00:00
|
|
|
else
|
2015-04-30 17:51:31 +00:00
|
|
|
group_ids = Group.select( 'groups.id' ).joins(:users)
|
2015-05-01 12:27:57 +00:00
|
|
|
.where( 'groups_users.user_id = ?', [ data[:current_user].id ] )
|
|
|
|
.where( 'groups.active = ?', true )
|
|
|
|
.map( &:id )
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
2015-04-27 21:27:51 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# overview meta for navbar
|
|
|
|
if !overview_selected
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# loop each overview
|
|
|
|
result = []
|
|
|
|
overviews.each { |overview|
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# get count
|
|
|
|
count = Ticket.where( group_id: group_ids ).where( _condition( overview.condition ) ).count()
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# get meta info
|
|
|
|
all = {
|
|
|
|
name: overview.name,
|
|
|
|
prio: overview.prio,
|
|
|
|
link: overview.link,
|
2015-04-27 21:27:51 +00:00
|
|
|
}
|
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# push to result data
|
|
|
|
result.push all.merge( { count: count } )
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
end
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# get result list
|
|
|
|
if data[:array]
|
|
|
|
order_by = overview_selected[:order][:by].to_s + ' ' + overview_selected[:order][:direction].to_s
|
|
|
|
if overview_selected.group_by && !overview_selected.group_by.empty?
|
|
|
|
order_by = overview_selected.group_by + '_id, ' + order_by
|
2013-08-17 20:04:57 +00:00
|
|
|
end
|
2015-04-30 17:51:31 +00:00
|
|
|
tickets = Ticket.select( 'id' )
|
2015-05-01 12:27:57 +00:00
|
|
|
.where( group_id: group_ids )
|
|
|
|
.where( _condition( overview_selected.condition ) )
|
|
|
|
.order( order_by )
|
|
|
|
.limit( 500 )
|
2015-04-27 23:19:26 +00:00
|
|
|
|
|
|
|
ticket_ids = []
|
|
|
|
tickets.each { |ticket|
|
|
|
|
ticket_ids.push ticket.id
|
|
|
|
}
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2015-04-30 17:51:31 +00:00
|
|
|
tickets_count = Ticket.where( group_id: group_ids )
|
2015-05-01 12:27:57 +00:00
|
|
|
.where( _condition( overview_selected.condition ) )
|
|
|
|
.count()
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
return {
|
|
|
|
ticket_ids: ticket_ids,
|
2015-04-27 13:42:53 +00:00
|
|
|
tickets_count: tickets_count,
|
|
|
|
overview: overview_selected_raw,
|
2013-08-17 20:04:57 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# get tickets for overview
|
|
|
|
data[:start_page] ||= 1
|
2015-04-30 17:51:31 +00:00
|
|
|
tickets = Ticket.where( group_id: group_ids )
|
2015-05-01 12:27:57 +00:00
|
|
|
.where( _condition( overview_selected.condition ) )
|
|
|
|
.order( overview_selected[:order][:by].to_s + ' ' + overview_selected[:order][:direction].to_s )
|
|
|
|
# .limit( overview_selected.view[ data[:view_mode].to_sym ][:per_page] )
|
|
|
|
# .offset( overview_selected.view[ data[:view_mode].to_sym ][:per_page].to_i * ( data[:start_page].to_i - 1 ) )
|
2015-04-27 23:19:26 +00:00
|
|
|
|
2015-04-30 17:51:31 +00:00
|
|
|
tickets_count = Ticket.where( group_id: group_ids )
|
2015-05-01 12:27:57 +00:00
|
|
|
.where( _condition( overview_selected.condition ) )
|
|
|
|
.count()
|
2015-04-27 23:19:26 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
tickets: tickets,
|
|
|
|
tickets_count: tickets_count,
|
|
|
|
overview: overview_selected_raw,
|
|
|
|
}
|
|
|
|
end
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def self._condition(condition)
|
|
|
|
sql = ''
|
|
|
|
bind = [nil]
|
|
|
|
condition.each {|key, value|
|
|
|
|
if sql != ''
|
|
|
|
sql += ' AND '
|
|
|
|
end
|
|
|
|
if value.class == Array
|
|
|
|
sql += " #{key} IN (?)"
|
|
|
|
bind.push value
|
|
|
|
elsif value.class == Hash || value.class == ActiveSupport::HashWithIndifferentAccess
|
|
|
|
time = Time.now
|
|
|
|
if value['area'] == 'minute'
|
|
|
|
if value['direction'] == 'last'
|
|
|
|
time -= value['count'].to_i * 60
|
|
|
|
else
|
|
|
|
time += value['count'].to_i * 60
|
|
|
|
end
|
|
|
|
elsif value['area'] == 'hour'
|
|
|
|
if value['direction'] == 'last'
|
|
|
|
time -= value['count'].to_i * 60 * 60
|
|
|
|
else
|
|
|
|
time += value['count'].to_i * 60 * 60
|
|
|
|
end
|
|
|
|
elsif value['area'] == 'day'
|
|
|
|
if value['direction'] == 'last'
|
|
|
|
time -= value['count'].to_i * 60 * 60 * 24
|
|
|
|
else
|
|
|
|
time += value['count'].to_i * 60 * 60 * 24
|
|
|
|
end
|
|
|
|
elsif value['area'] == 'month'
|
|
|
|
if value['direction'] == 'last'
|
|
|
|
time -= value['count'].to_i * 60 * 60 * 24 * 31
|
|
|
|
else
|
|
|
|
time += value['count'].to_i * 60 * 60 * 24 * 31
|
2013-08-17 20:04:57 +00:00
|
|
|
end
|
2015-04-27 23:19:26 +00:00
|
|
|
elsif value['area'] == 'year'
|
2014-02-03 19:23:00 +00:00
|
|
|
if value['direction'] == 'last'
|
2015-04-27 23:19:26 +00:00
|
|
|
time -= value['count'].to_i * 60 * 60 * 24 * 365
|
2014-02-03 19:23:00 +00:00
|
|
|
else
|
2015-04-27 23:19:26 +00:00
|
|
|
time += value['count'].to_i * 60 * 60 * 24 * 365
|
2014-02-03 19:23:00 +00:00
|
|
|
end
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
|
|
|
if value['direction'] == 'last'
|
|
|
|
sql += " #{key} > ?"
|
|
|
|
bind.push time
|
2013-08-17 20:04:57 +00:00
|
|
|
else
|
2015-04-27 23:19:26 +00:00
|
|
|
sql += " #{key} < ?"
|
|
|
|
bind.push time
|
2013-08-17 20:04:57 +00:00
|
|
|
end
|
2015-04-27 23:19:26 +00:00
|
|
|
else
|
|
|
|
sql += " #{key} = ?"
|
|
|
|
bind.push value
|
|
|
|
end
|
|
|
|
}
|
|
|
|
bind[0] = sql
|
|
|
|
bind
|
2014-02-03 19:23:00 +00:00
|
|
|
end
|
|
|
|
end
|