trabajo-afectivo/app/models/ticket.rb

449 lines
12 KiB
Ruby
Raw Normal View History

2012-07-29 20:25:31 +00:00
class Ticket < ApplicationModel
before_create :number_generate, :check_defaults
before_update :check_defaults
before_destroy :destroy_dependencies
2012-04-16 08:04:49 +00:00
belongs_to :group
2012-11-14 01:48:04 +00:00
has_many :articles, :class_name => 'Ticket::Article', :after_add => :cache_update, :after_remove => :cache_update
belongs_to :organization
2012-04-16 08:04:49 +00:00
belongs_to :ticket_state, :class_name => 'Ticket::State'
belongs_to :ticket_priority, :class_name => 'Ticket::Priority'
belongs_to :owner, :class_name => 'User'
belongs_to :customer, :class_name => 'User'
belongs_to :created_by, :class_name => 'User'
2012-04-10 14:06:46 +00:00
2012-07-29 20:25:31 +00:00
after_create :cache_delete
after_update :cache_delete
after_destroy :cache_delete
2012-04-10 14:06:46 +00:00
def self.number_check (string)
2012-11-28 10:03:17 +00:00
self.number_adapter.number_check_item(string)
2012-04-10 14:06:46 +00:00
end
def agent_of_group
Group.find(self.group_id).users.where( :active => true ).joins(:roles).where( 'roles.name' => 'Agent', 'roles.active' => true ).uniq()
end
def self.agents
User.where( :active => true ).joins(:roles).where( 'roles.name' => 'Agent', 'roles.active' => true ).uniq()
end
2012-07-03 13:24:31 +00:00
def merge_to(data)
2012-11-07 23:47:05 +00:00
2012-07-03 13:24:31 +00:00
# update articles
Ticket::Article.where( :ticket_id => self.id ).update_all( ['ticket_id = ?', data[:ticket_id] ] )
2012-11-07 23:47:05 +00:00
2012-07-03 13:24:31 +00:00
# update history
2012-11-07 23:47:05 +00:00
2012-07-03 13:24:31 +00:00
# create new merge article
Ticket::Article.create(
:created_by_id => data[:created_by_id],
:ticket_id => self.id,
:ticket_article_type_id => Ticket::Article::Type.where( :name => 'note' ).first.id,
:ticket_article_sender_id => Ticket::Article::Sender.where( :name => 'Agent' ).first.id,
:body => 'merged',
:internal => false
)
# add history to both
# link tickets
2012-08-21 10:28:41 +00:00
Link.add(
:link_type => 'parent',
:link_object_source => 'Ticket',
:link_object_source_value => data[:ticket_id],
:link_object_target => 'Ticket',
:link_object_target_value => self.id
)
2012-07-03 13:24:31 +00:00
# set state to 'merged'
self.ticket_state_id = Ticket::State.where( :name => 'merged' ).first.id
# rest owner
2012-08-21 10:28:41 +00:00
self.owner_id = User.where( :login => '-' ).first.id
2012-07-03 13:24:31 +00:00
# save ticket
self.save
end
2012-04-10 14:06:46 +00:00
# def self.agent
# Role.where( :name => ['Agent'], :active => true ).first.users.where( :active => true ).uniq()
# end
def subject_build (subject)
# clena subject
subject = self.subject_clean(subject)
ticket_hook = Setting.get('ticket_hook')
ticket_hook_divider = Setting.get('ticket_hook_divider')
# none position
if Setting.get('ticket_hook_position') == 'none'
return subject
end
# right position
if Setting.get('ticket_hook_position') == 'right'
return subject + " [#{ticket_hook}#{ticket_hook_divider}#{self.number}] "
end
# left position
return "[#{ticket_hook}#{ticket_hook_divider}#{self.number}] " + subject
end
def subject_clean (subject)
ticket_hook = Setting.get('ticket_hook')
ticket_hook_divider = Setting.get('ticket_hook_divider')
ticket_subject_size = Setting.get('ticket_subject_size')
# remove all possible ticket hook formats with []
subject = subject.gsub /\[#{ticket_hook}: #{self.number}\](\s+?|)/, ''
subject = subject.gsub /\[#{ticket_hook}:#{self.number}\](\s+?|)/, ''
subject = subject.gsub /\[#{ticket_hook}#{ticket_hook_divider}#{self.number}\](\s+?|)/, ''
# remove all possible ticket hook formats without []
subject = subject.gsub /#{ticket_hook}: #{self.number}(\s+?|)/, ''
subject = subject.gsub /#{ticket_hook}:#{self.number}(\s+?|)/, ''
subject = subject.gsub /#{ticket_hook}#{ticket_hook_divider}#{self.number}(\s+?|)/, ''
# remove leading "..:\s" and "..[\d+]:\s" e. g. "Re: " or "Re[5]: "
subject = subject.gsub /^(..(\[\d+\])?:\s)+/, ''
# resize subject based on config
if subject.length > ticket_subject_size.to_i
subject = subject[ 0, ticket_subject_size.to_i ] + '[...]'
end
return subject
end
2012-09-04 21:28:49 +00:00
# ticket.permission(
# :current_user => 123
# )
def permission (data)
# check customer
if data[:current_user].is_role('Customer')
# access ok if its own ticket
2012-09-04 21:28:49 +00:00
return true if self.customer_id == data[:current_user].id
# access ok if its organization ticket
if data[:current_user].organization_id && self.organization_id
return true if self.organization_id == data[:current_user].organization_id
end
# no access
2012-09-04 21:28:49 +00:00
return false
end
# check agent
return true if self.owner_id == data[:current_user].id
data[:current_user].groups.each {|group|
return true if self.group.id == group.id
}
return false
end
# Ticket.overview_list(
2012-09-04 21:28:49 +00:00
# :current_user => 123,
# )
def self.overview_list (data)
# get customer overviews
2012-09-04 21:28:49 +00:00
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 )
else
overviews = Overview.where( :role_id => role.id, :organization_shared => false )
end
return overviews
2012-09-04 21:28:49 +00:00
end
# get agent overviews
role = data[:current_user].is_role( 'Agent' )
overviews = Overview.where( :role_id => role.id )
return overviews
end
# Ticket.overview(
2012-09-04 21:28:49 +00:00
# :view => 'some_view_url',
# :current_user => OBJECT,
# )
def self.overview (data)
overviews = self.overview_list(data)
2012-09-04 21:28:49 +00:00
# build up attributes hash
overview_selected = nil
overview_selected_raw = nil
overviews.each { |overview|
2012-07-23 22:22:23 +00:00
# remember selected view
if data[:view] && data[:view] == overview.meta[:url]
overview_selected = overview
overview_selected_raw = Marshal.load( Marshal.dump(overview.attributes) )
end
# replace e.g. 'current_user.id' with current_user.id
overview.condition.each { |item, value |
if value && value.class.to_s == 'String'
parts = value.split( '.', 2 )
if parts[0] && parts[1] && parts[0] == 'current_user'
overview.condition[item] = data[:current_user][parts[1].to_sym]
end
end
}
}
2012-11-26 23:22:52 +00:00
if data[:view] && !overview_selected
return
end
# 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
2012-09-04 21:28:49 +00:00
if data[:current_user].is_role('Customer')
group_ids = Group.select( 'groups.id' ).
where( 'groups.active = ?', true ).
map( &:id )
else
group_ids = Group.select( 'groups.id' ).joins(:users).
where( 'groups_users.user_id = ?', [ data[:current_user].id ] ).
where( 'groups.active = ?', true ).
map( &:id )
end
# overview meta for navbar
if !overview_selected
# loop each overview
result = []
overviews.each { |overview|
# get count
count = Ticket.where( :group_id => group_ids ).where( overview.condition ).count()
2012-07-23 22:22:23 +00:00
# get meta info
all = overview.meta
2012-07-23 22:22:23 +00:00
# push to result data
result.push all.merge( { :count => count } )
}
return result
end
# get result list
if data[:array]
2012-11-03 16:45:57 +00:00
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
end
tickets = Ticket.select( 'id' ).
where( :group_id => group_ids ).
where( overview_selected.condition ).
2012-11-03 16:45:57 +00:00
order( order_by ).
2012-07-29 20:25:31 +00:00
limit( 500 )
2012-08-03 22:46:05 +00:00
ticket_ids = []
tickets.each { |ticket|
ticket_ids.push ticket.id
}
tickets_count = Ticket.where( :group_id => group_ids ).
where( overview_selected.condition ).
2012-09-04 21:28:49 +00:00
count()
return {
2012-11-26 23:22:52 +00:00
:ticket_list => ticket_ids,
:tickets_count => tickets_count,
:overview => overview_selected_raw,
}
end
# get tickets for overview
data[:start_page] ||= 1
tickets = Ticket.where( :group_id => group_ids ).
where( 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 ) )
tickets_count = Ticket.where( :group_id => group_ids ).
where( overview_selected.condition ).
count()
return {
:tickets => tickets,
:tickets_count => tickets_count,
:overview => overview_selected_raw,
}
end
2012-07-29 20:25:31 +00:00
# data = Ticket.full_data(123)
def self.full_data(ticket_id)
cache = self.cache_get(ticket_id)
return cache if cache
ticket = Ticket.find(ticket_id).attributes
self.cache_set( ticket_id, ticket )
return ticket
end
2012-07-29 15:27:01 +00:00
# Ticket.create_attributes(
# :current_user_id => 123,
# )
def self.create_attributes (data)
# get groups
2012-09-13 00:44:16 +00:00
group_ids = []
2012-07-29 15:27:01 +00:00
Group.where( :active => true ).each { |group|
2012-09-13 00:44:16 +00:00
group_ids.push group.id
2012-07-29 15:27:01 +00:00
}
# get related users
2012-09-13 00:44:16 +00:00
# users = {}
user_ids = []
agents = {}
2012-07-29 15:27:01 +00:00
Ticket.agents.each { |user|
2012-09-13 00:44:16 +00:00
agents[ user.id ] = 1
user_ids.push user.id
}
groups_users = {}
group_ids.each {|group_id|
groups_users[ group_id ] = []
Group.find(group_id).users.each {|user|
next if !agents[ user.id ]
groups_users[ group_id ].push user.id
# if !users[user.id]
# users[user.id] = User.user_data_full(user.id)
# end
}
2012-07-29 15:27:01 +00:00
}
# get states
ticket_state_ids = []
Ticket::State.where( :active => true ).each { |state|
ticket_state_ids.push state.id
}
# get priorities
ticket_priority_ids = []
Ticket::Priority.where( :active => true ).each { |priority|
ticket_priority_ids.push priority.id
}
2012-09-13 00:44:16 +00:00
return {
# :users => users,
:owner_id => user_ids,
:group_id__owner_id => groups_users,
:group_id => group_ids,
:ticket_state_id => ticket_state_ids,
:ticket_priority_id => ticket_priority_ids,
}
2012-07-29 15:27:01 +00:00
end
2012-11-28 10:03:17 +00:00
def self.number_adapter
# load backend based on config
adapter_name = Setting.get('ticket_number')
adapter = nil
case adapter_name
when Symbol, String
require "ticket/number/#{adapter_name.to_s.downcase}"
adapter = Ticket::Number.const_get("#{adapter_name.to_s.capitalize}")
else
raise "Missing number_adapter '#{adapter_name}'"
end
2012-11-28 10:03:17 +00:00
return adapter
end
private
2012-04-10 14:06:46 +00:00
def number_generate
# generate number
(1..25_000).each do |i|
2012-11-28 10:03:17 +00:00
number = Ticket.number_adapter.number_generate_item()
2012-04-10 14:06:46 +00:00
ticket = Ticket.where( :number => number ).first
if ticket != nil
number = number_adapter.number_generate_item()
2012-04-10 14:06:46 +00:00
else
self.number = number
return number
end
end
end
def check_defaults
if !self.owner_id
2012-04-10 14:06:46 +00:00
self.owner_id = 1
end
# if self.customer_id && ( !self.organization_id || self.organization_id.empty? )
if self.customer_id
customer = User.find( self.customer_id )
if self.organization_id != customer.organization_id
self.organization_id = customer.organization_id
end
end
2012-04-10 14:06:46 +00:00
end
def destroy_dependencies
2012-07-23 22:22:23 +00:00
# delete history
History.history_destroy( 'Ticket', self.id )
# delete articles
self.articles.destroy_all
end
2012-04-10 14:06:46 +00:00
2012-05-30 21:06:26 +00:00
class Number
end
2012-07-29 20:25:31 +00:00
class Flag < ApplicationModel
2012-04-10 14:06:46 +00:00
end
2012-07-29 20:25:31 +00:00
class Priority < ApplicationModel
2012-04-10 14:06:46 +00:00
self.table_name = 'ticket_priorities'
2012-07-29 20:25:31 +00:00
after_create :cache_delete
after_update :cache_delete
after_destroy :cache_delete
validates :name, :presence => true
2012-04-10 14:06:46 +00:00
end
2012-07-29 20:25:31 +00:00
class StateType < ApplicationModel
after_create :cache_delete
after_update :cache_delete
after_destroy :cache_delete
validates :name, :presence => true
2012-04-10 14:06:46 +00:00
end
2012-07-29 20:25:31 +00:00
class State < ApplicationModel
2012-04-10 14:06:46 +00:00
belongs_to :ticket_state_type, :class_name => 'Ticket::StateType'
2012-07-29 20:25:31 +00:00
after_create :cache_delete
after_update :cache_delete
after_destroy :cache_delete
validates :name, :presence => true
2012-04-10 14:06:46 +00:00
end
2012-07-23 22:22:23 +00:00
end