2013-02-01 19:58:53 +00:00
|
|
|
module Import
|
|
|
|
end
|
2013-01-07 08:46:38 +00:00
|
|
|
module Import::OTRS
|
|
|
|
def self.request(part)
|
2013-01-07 09:30:07 +00:00
|
|
|
url = Setting.get('import_otrs_endpoint') + '/' + part + ';Key=' + Setting.get('import_otrs_endpoint_key')
|
2013-01-25 10:58:56 +00:00
|
|
|
puts 'GET: ' + url
|
2013-01-07 08:46:38 +00:00
|
|
|
# response = Net::HTTP.get_response( URI.parse(url), { :use_ssl => true, :verify_mode => OpenSSL::SSL::VERIFY_NONE } )
|
|
|
|
uri = URI.parse(url)
|
|
|
|
http = Net::HTTP.new(uri.host, uri.port)
|
2013-04-15 21:33:11 +00:00
|
|
|
|
2013-01-07 08:46:38 +00:00
|
|
|
if url =~ /https/i
|
|
|
|
http.use_ssl = true
|
|
|
|
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
|
|
|
end
|
|
|
|
request = Net::HTTP::Get.new(uri.request_uri)
|
2013-04-18 12:42:28 +00:00
|
|
|
|
|
|
|
# http basic auth (if needed)
|
|
|
|
user = Setting.get('import_otrs_user');
|
|
|
|
password = Setting.get('import_otrs_password');
|
|
|
|
if user && user != '' && password && password != ''
|
|
|
|
request.basic_auth user, password
|
|
|
|
end
|
|
|
|
|
2013-01-25 22:17:50 +00:00
|
|
|
begin
|
|
|
|
response = http.request(request)
|
2013-01-07 08:46:38 +00:00
|
|
|
# puts 'R:' + response.body.to_s
|
2013-01-25 22:17:50 +00:00
|
|
|
rescue Exception => e
|
|
|
|
puts "can't get #{url}"
|
|
|
|
puts e.inspect
|
|
|
|
return
|
|
|
|
end
|
2013-04-15 21:33:11 +00:00
|
|
|
if !response
|
|
|
|
raise "Can't connect to #{url}, got no response!"
|
|
|
|
end
|
|
|
|
if response.code.to_s != '200'
|
|
|
|
raise "Connection to #{url} failed, '#{response.code.to_s}'!"
|
|
|
|
end
|
|
|
|
return response
|
2013-01-07 08:46:38 +00:00
|
|
|
end
|
2013-01-23 23:53:39 +00:00
|
|
|
def self.post(base, data)
|
|
|
|
url = Setting.get('import_otrs_endpoint') + '/' + base
|
|
|
|
data['Key'] = Setting.get('import_otrs_endpoint_key')
|
2013-01-25 10:58:56 +00:00
|
|
|
puts 'POST: ' + url
|
2013-01-23 23:53:39 +00:00
|
|
|
uri = URI.parse(url)
|
|
|
|
http = Net::HTTP.new(uri.host, uri.port)
|
2013-04-15 21:33:11 +00:00
|
|
|
|
2013-01-23 23:53:39 +00:00
|
|
|
if url =~ /https/i
|
|
|
|
http.use_ssl = true
|
|
|
|
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
|
|
|
end
|
|
|
|
request = Net::HTTP::Post.new(uri.request_uri)
|
2013-04-15 21:33:11 +00:00
|
|
|
|
2013-04-18 12:42:28 +00:00
|
|
|
# http basic auth (if needed)
|
|
|
|
user = Setting.get('import_otrs_user');
|
|
|
|
password = Setting.get('import_otrs_password');
|
|
|
|
if user && user != '' && password && password != ''
|
2013-04-28 21:21:05 +00:00
|
|
|
request.basic_auth user, password
|
2013-04-18 12:42:28 +00:00
|
|
|
end
|
|
|
|
|
2013-04-28 21:21:05 +00:00
|
|
|
request.set_form_data(data)
|
|
|
|
response = http.request(request)
|
|
|
|
|
2013-04-15 21:33:11 +00:00
|
|
|
if !response
|
|
|
|
raise "Can't connect to #{url}, got no response!"
|
|
|
|
end
|
|
|
|
if response.code.to_s != '200'
|
|
|
|
raise "Connection to #{url} failed, '#{response.code.to_s}'!"
|
|
|
|
end
|
2013-01-23 23:53:39 +00:00
|
|
|
return response
|
|
|
|
end
|
2013-01-07 08:46:38 +00:00
|
|
|
|
|
|
|
def self.json(response)
|
|
|
|
data = Encode.conv( 'utf8', response.body.to_s )
|
|
|
|
JSON.parse( data )
|
|
|
|
end
|
|
|
|
|
2013-01-23 23:53:39 +00:00
|
|
|
def self.auth(username, password)
|
|
|
|
response = post( "public.pl", { :Action => 'Export', :Type => 'Auth', :User => username, :Pw => password } )
|
2013-01-25 22:17:50 +00:00
|
|
|
return if !response
|
2013-01-23 23:53:39 +00:00
|
|
|
return if response.code.to_s != '200'
|
|
|
|
|
|
|
|
result = json(response)
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2013-02-17 18:28:32 +00:00
|
|
|
def self.session(session_id)
|
|
|
|
response = post( "public.pl", { :Action => 'Export', :Type => 'SessionCheck', :SessionID => session_id } )
|
|
|
|
return if !response
|
|
|
|
return if response.code.to_s != '200'
|
|
|
|
|
|
|
|
result = json(response)
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2013-01-07 08:46:38 +00:00
|
|
|
def self.start
|
|
|
|
puts 'Start import...'
|
|
|
|
|
2013-01-07 09:30:07 +00:00
|
|
|
# # set system in import mode
|
|
|
|
# Setting.set('import_mode', true)
|
2013-01-07 08:46:38 +00:00
|
|
|
|
|
|
|
# check if system is in import mode
|
|
|
|
if !Setting.get('import_mode')
|
|
|
|
raise "System is not in import mode!"
|
|
|
|
end
|
|
|
|
|
2013-01-07 09:30:07 +00:00
|
|
|
response = request("public.pl?Action=Export")
|
2013-01-25 22:17:50 +00:00
|
|
|
return if !response
|
2013-01-07 08:46:38 +00:00
|
|
|
return if response.code.to_s != '200'
|
|
|
|
|
|
|
|
#self.ticket('156115')
|
|
|
|
#return
|
|
|
|
# create states
|
|
|
|
ticket_state
|
|
|
|
|
|
|
|
# create priorities
|
|
|
|
ticket_priority
|
|
|
|
|
|
|
|
# create groups
|
|
|
|
ticket_group
|
|
|
|
|
|
|
|
# create agents
|
|
|
|
user
|
|
|
|
|
|
|
|
# create customers
|
|
|
|
# customer
|
|
|
|
|
|
|
|
result = JSON.parse( response.body )
|
2013-01-25 23:06:21 +00:00
|
|
|
result = result.reverse
|
|
|
|
|
2013-02-01 19:58:53 +00:00
|
|
|
Thread.abort_on_exception = true
|
2013-02-17 20:59:57 +00:00
|
|
|
thread_count = 4
|
2013-01-25 23:06:21 +00:00
|
|
|
threads = {}
|
|
|
|
(1..thread_count).each {|thread|
|
|
|
|
threads[thread] = Thread.new {
|
|
|
|
sleep thread * 3
|
2013-02-17 20:59:57 +00:00
|
|
|
puts "Started import thread# #{thread} ..."
|
|
|
|
run = true
|
|
|
|
while run
|
2013-01-25 23:06:21 +00:00
|
|
|
ticket_ids = result.pop(20)
|
2013-02-17 20:59:57 +00:00
|
|
|
if !ticket_ids.empty?
|
|
|
|
self.ticket(ticket_ids)
|
|
|
|
else
|
|
|
|
puts "... thread# #{thread}, no more work."
|
|
|
|
run = false
|
|
|
|
end
|
2013-01-25 23:06:21 +00:00
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(1..thread_count).each {|thread|
|
|
|
|
threads[thread].join
|
|
|
|
}
|
|
|
|
|
2013-01-07 08:46:38 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2013-05-07 20:45:00 +00:00
|
|
|
def self.diff_worker
|
2013-03-05 06:45:14 +00:00
|
|
|
return if !Setting.get('import_mode')
|
2013-03-10 16:45:48 +00:00
|
|
|
return if Setting.get('import_otrs_endpoint') == 'http://otrs_host/otrs'
|
2013-05-07 20:45:00 +00:00
|
|
|
self.diff
|
2013-02-19 19:04:35 +00:00
|
|
|
end
|
|
|
|
|
2013-02-17 20:59:57 +00:00
|
|
|
def self.diff
|
|
|
|
puts 'Start diff...'
|
|
|
|
|
|
|
|
# check if system is in import mode
|
|
|
|
if !Setting.get('import_mode')
|
|
|
|
raise "System is not in import mode!"
|
|
|
|
end
|
|
|
|
|
|
|
|
# create states
|
|
|
|
ticket_state
|
|
|
|
|
|
|
|
# create priorities
|
|
|
|
ticket_priority
|
|
|
|
|
|
|
|
# create groups
|
|
|
|
ticket_group
|
|
|
|
|
|
|
|
# create agents
|
|
|
|
user
|
|
|
|
|
|
|
|
self.ticket_diff()
|
|
|
|
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def self.ticket_diff()
|
|
|
|
url = "public.pl?Action=Export;Type=TicketDiff;Limit=30"
|
|
|
|
response = request( url )
|
|
|
|
return if !response
|
|
|
|
return if response.code.to_s != '200'
|
|
|
|
result = json(response)
|
|
|
|
self._ticket_result(result)
|
|
|
|
end
|
|
|
|
|
2013-01-07 08:46:38 +00:00
|
|
|
def self.ticket(ticket_ids)
|
2013-01-07 09:30:07 +00:00
|
|
|
url = "public.pl?Action=Export;Type=Ticket;"
|
2013-01-07 08:46:38 +00:00
|
|
|
ticket_ids.each {|ticket_id|
|
|
|
|
url = url + "TicketID=#{CGI::escape ticket_id};"
|
|
|
|
}
|
|
|
|
response = request( url )
|
2013-01-25 22:17:50 +00:00
|
|
|
return if !response
|
2013-01-07 08:46:38 +00:00
|
|
|
return if response.code.to_s != '200'
|
|
|
|
|
|
|
|
result = json(response)
|
2013-02-17 20:59:57 +00:00
|
|
|
self._ticket_result(result)
|
|
|
|
end
|
2013-06-04 12:52:56 +00:00
|
|
|
|
2013-02-17 20:59:57 +00:00
|
|
|
def self._ticket_result(result)
|
2013-01-07 08:46:38 +00:00
|
|
|
# puts result.inspect
|
|
|
|
map = {
|
|
|
|
:Ticket => {
|
2013-02-19 07:26:04 +00:00
|
|
|
:Changed => :updated_at,
|
|
|
|
:Created => :created_at,
|
|
|
|
:CreateBy => :created_by_id,
|
|
|
|
:TicketNumber => :number,
|
|
|
|
:QueueID => :group_id,
|
|
|
|
:StateID => :ticket_state_id,
|
|
|
|
:PriorityID => :ticket_priority_id,
|
|
|
|
:Owner => :owner,
|
|
|
|
:CustomerUserID => :customer,
|
|
|
|
:Title => :title,
|
|
|
|
:TicketID => :id,
|
|
|
|
:FirstResponse => :first_response,
|
2013-04-02 13:03:21 +00:00
|
|
|
# :FirstResponseTimeDestinationDate => :first_response_escal_date,
|
|
|
|
# :FirstResponseInMin => :first_response_in_min,
|
|
|
|
# :FirstResponseDiffInMin => :first_response_diff_in_min,
|
2013-02-19 07:26:04 +00:00
|
|
|
:Closed => :close_time,
|
2013-04-02 13:03:21 +00:00
|
|
|
# :SoltutionTimeDestinationDate => :close_time_escal_date,
|
|
|
|
# :CloseTimeInMin => :close_time_in_min,
|
|
|
|
# :CloseTimeDiffInMin => :close_time_diff_in_min,
|
2013-01-07 08:46:38 +00:00
|
|
|
},
|
|
|
|
:Article => {
|
|
|
|
:SenderType => :ticket_article_sender,
|
|
|
|
:ArticleType => :ticket_article_type,
|
|
|
|
:TicketID => :ticket_id,
|
|
|
|
:ArticleID => :id,
|
|
|
|
:Body => :body,
|
|
|
|
:From => :from,
|
|
|
|
:To => :to,
|
|
|
|
:Cc => :cc,
|
|
|
|
:Subject => :subject,
|
|
|
|
:InReplyTo => :in_reply_to,
|
|
|
|
:MessageID => :message_id,
|
|
|
|
# :ReplyTo => :reply_to,
|
|
|
|
:References => :references,
|
|
|
|
:Changed => :updated_at,
|
|
|
|
:Created => :created_at,
|
|
|
|
:ChangedBy => :updated_by_id,
|
|
|
|
:CreatedBy => :created_by_id,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
result.each {|record|
|
2013-02-01 19:58:53 +00:00
|
|
|
|
|
|
|
# use transaction
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
|
2013-01-07 08:46:38 +00:00
|
|
|
ticket_new = {
|
2013-01-25 10:58:56 +00:00
|
|
|
:title => '',
|
2013-01-07 08:46:38 +00:00
|
|
|
:created_by_id => 1,
|
|
|
|
:updated_by_id => 1,
|
|
|
|
}
|
|
|
|
map[:Ticket].each { |key,value|
|
2013-02-19 07:26:04 +00:00
|
|
|
if record['Ticket'][key.to_s] && record['Ticket'][key.to_s].class == String
|
2013-01-07 08:46:38 +00:00
|
|
|
ticket_new[value] = Encode.conv( 'utf8', record['Ticket'][key.to_s] )
|
2013-02-19 07:26:04 +00:00
|
|
|
else
|
|
|
|
ticket_new[value] = record['Ticket'][key.to_s]
|
2013-01-07 08:46:38 +00:00
|
|
|
end
|
|
|
|
}
|
|
|
|
# puts key.to_s
|
|
|
|
# puts value.to_s
|
|
|
|
#puts 'new ticket data ' + ticket_new.inspect
|
|
|
|
# check if state already exists
|
|
|
|
ticket_old = Ticket.where( :id => ticket_new[:id] ).first
|
|
|
|
#puts 'TICKET OLD ' + ticket_old.inspect
|
|
|
|
# find user
|
|
|
|
if ticket_new[:owner]
|
2013-02-01 19:58:53 +00:00
|
|
|
user = User.lookup( :login => ticket_new[:owner] )
|
2013-01-07 08:46:38 +00:00
|
|
|
if user
|
|
|
|
ticket_new[:owner_id] = user.id
|
|
|
|
else
|
|
|
|
ticket_new[:owner_id] = 1
|
|
|
|
end
|
|
|
|
ticket_new.delete(:owner)
|
|
|
|
end
|
2013-02-01 19:58:53 +00:00
|
|
|
if ticket_new[:customer]
|
|
|
|
user = User.lookup( :login => ticket_new[:customer] )
|
|
|
|
if user
|
|
|
|
ticket_new[:customer_id] = user.id
|
|
|
|
else
|
|
|
|
ticket_new[:customer_id] = 1
|
2013-01-07 08:46:38 +00:00
|
|
|
end
|
2013-02-01 19:58:53 +00:00
|
|
|
ticket_new.delete(:customer)
|
|
|
|
else
|
|
|
|
ticket_new[:customer_id] = 1
|
2013-01-07 08:46:38 +00:00
|
|
|
end
|
2013-02-01 19:58:53 +00:00
|
|
|
# puts 'ttt' + ticket_new.inspect
|
|
|
|
# set state types
|
|
|
|
if ticket_old
|
|
|
|
puts "update Ticket.find(#{ticket_new[:id]})"
|
|
|
|
ticket_old.update_attributes(ticket_new)
|
|
|
|
else
|
|
|
|
puts "add Ticket.find(#{ticket_new[:id]})"
|
|
|
|
ticket = Ticket.new(ticket_new)
|
|
|
|
ticket.id = ticket_new[:id]
|
|
|
|
ticket.save
|
2013-01-07 08:46:38 +00:00
|
|
|
end
|
|
|
|
|
2013-02-01 19:58:53 +00:00
|
|
|
record['Articles'].each { |article|
|
2013-06-04 12:52:56 +00:00
|
|
|
|
2013-02-01 19:58:53 +00:00
|
|
|
# get article values
|
|
|
|
article_new = {
|
|
|
|
:created_by_id => 1,
|
|
|
|
:updated_by_id => 1,
|
|
|
|
}
|
|
|
|
map[:Article].each { |key,value|
|
|
|
|
if article[key.to_s]
|
|
|
|
article_new[value] = Encode.conv( 'utf8', article[key.to_s] )
|
|
|
|
end
|
|
|
|
}
|
|
|
|
# create customer/sender if needed
|
|
|
|
if article_new[:ticket_article_sender] == 'customer' && article_new[:created_by_id].to_i == 1 && !article_new[:from].empty?
|
|
|
|
# set extra headers
|
|
|
|
begin
|
|
|
|
email = Mail::Address.new( article_new[:from] ).address
|
|
|
|
rescue
|
|
|
|
email = article_new[:from]
|
|
|
|
if article_new[:from] =~ /<(.+?)>/
|
|
|
|
email = $1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
user = User.where( :email => email ).first
|
2013-04-18 12:42:28 +00:00
|
|
|
if !user
|
|
|
|
user = User.where( :login => email ).first
|
|
|
|
end
|
2013-02-01 19:58:53 +00:00
|
|
|
if !user
|
|
|
|
begin
|
|
|
|
display_name = Mail::Address.new( article_new[:from] ).display_name ||
|
|
|
|
( Mail::Address.new( article_new[:from] ).comments && Mail::Address.new( article_new[:from] ).comments[0] )
|
|
|
|
rescue
|
|
|
|
display_name = article_new[:from]
|
|
|
|
end
|
2013-06-04 12:52:56 +00:00
|
|
|
|
2013-02-01 19:58:53 +00:00
|
|
|
# do extra decoding because we needed to use field.value
|
|
|
|
display_name = Mail::Field.new( 'X-From', display_name ).to_s
|
2013-06-04 12:52:56 +00:00
|
|
|
|
2013-02-01 19:58:53 +00:00
|
|
|
roles = Role.lookup( :name => 'Customer' )
|
|
|
|
user = User.create(
|
|
|
|
:login => email,
|
|
|
|
:firstname => display_name,
|
|
|
|
:lastname => '',
|
|
|
|
:email => email,
|
|
|
|
:password => '',
|
|
|
|
:active => true,
|
2013-02-17 18:28:32 +00:00
|
|
|
:role_ids => [roles.id],
|
2013-02-01 19:58:53 +00:00
|
|
|
:updated_by_id => 1,
|
|
|
|
:created_by_id => 1,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
article_new[:created_by_id] = user.id
|
2013-01-07 22:27:32 +00:00
|
|
|
end
|
2013-06-04 12:52:56 +00:00
|
|
|
|
2013-02-01 19:58:53 +00:00
|
|
|
if article_new[:ticket_article_sender] == 'customer'
|
|
|
|
article_new[:ticket_article_sender_id] = Ticket::Article::Sender.lookup( :name => 'Customer' ).id
|
|
|
|
article_new.delete( :ticket_article_sender )
|
2013-01-07 22:27:32 +00:00
|
|
|
end
|
2013-02-01 19:58:53 +00:00
|
|
|
if article_new[:ticket_article_sender] == 'agent'
|
|
|
|
article_new[:ticket_article_sender_id] = Ticket::Article::Sender.lookup( :name => 'Agent' ).id
|
|
|
|
article_new.delete( :ticket_article_sender )
|
|
|
|
end
|
|
|
|
if article_new[:ticket_article_sender] == 'system'
|
|
|
|
article_new[:ticket_article_sender_id] = Ticket::Article::Sender.lookup( :name => 'System' ).id
|
|
|
|
article_new.delete( :ticket_article_sender )
|
|
|
|
end
|
2013-06-04 12:52:56 +00:00
|
|
|
|
2013-02-01 19:58:53 +00:00
|
|
|
if article_new[:ticket_article_type] == 'email-external'
|
|
|
|
article_new[:ticket_article_type_id] = Ticket::Article::Type.lookup( :name => 'email' ).id
|
|
|
|
article_new[:internal] = false
|
|
|
|
elsif article_new[:ticket_article_type] == 'email-internal'
|
|
|
|
article_new[:ticket_article_type_id] = Ticket::Article::Type.lookup( :name => 'email' ).id
|
|
|
|
article_new[:internal] = true
|
|
|
|
elsif article_new[:ticket_article_type] == 'note-external'
|
|
|
|
article_new[:ticket_article_type_id] = Ticket::Article::Type.lookup( :name => 'note' ).id
|
|
|
|
article_new[:internal] = false
|
|
|
|
elsif article_new[:ticket_article_type] == 'note-internal'
|
|
|
|
article_new[:ticket_article_type_id] = Ticket::Article::Type.lookup( :name => 'note' ).id
|
|
|
|
article_new[:internal] = true
|
|
|
|
elsif article_new[:ticket_article_type] == 'phone'
|
|
|
|
article_new[:ticket_article_type_id] = Ticket::Article::Type.lookup( :name => 'phone' ).id
|
|
|
|
article_new[:internal] = false
|
|
|
|
elsif article_new[:ticket_article_type] == 'webrequest'
|
|
|
|
article_new[:ticket_article_type_id] = Ticket::Article::Type.lookup( :name => 'web' ).id
|
|
|
|
article_new[:internal] = false
|
|
|
|
else
|
|
|
|
article_new[:ticket_article_type_id] = 9
|
|
|
|
end
|
|
|
|
article_new.delete( :ticket_article_type )
|
|
|
|
article_old = Ticket::Article.where( :id => article_new[:id] ).first
|
|
|
|
#puts 'ARTICLE OLD ' + article_old.inspect
|
|
|
|
# set state types
|
|
|
|
if article_old
|
|
|
|
puts "update Ticket::Article.find(#{article_new[:id]})"
|
|
|
|
# puts article_new.inspect
|
|
|
|
article_old.update_attributes(article_new)
|
|
|
|
else
|
|
|
|
puts "add Ticket::Article.find(#{article_new[:id]})"
|
|
|
|
article = Ticket::Article.new(article_new)
|
|
|
|
article.id = article_new[:id]
|
|
|
|
article.save
|
|
|
|
end
|
2013-06-04 12:52:56 +00:00
|
|
|
|
2013-02-01 19:58:53 +00:00
|
|
|
}
|
2013-06-04 12:52:56 +00:00
|
|
|
|
2013-02-01 19:58:53 +00:00
|
|
|
record['History'].each { |history|
|
|
|
|
# puts '-------'
|
|
|
|
# puts history.inspect
|
|
|
|
if history['HistoryType'] == 'NewTicket'
|
2013-06-04 12:52:56 +00:00
|
|
|
History.add(
|
2013-02-01 19:58:53 +00:00
|
|
|
:id => history['HistoryID'],
|
|
|
|
:o_id => history['TicketID'],
|
|
|
|
:history_type => 'created',
|
|
|
|
:history_object => 'Ticket',
|
|
|
|
:created_at => history['CreateTime'],
|
|
|
|
:created_by_id => history['CreateBy']
|
|
|
|
)
|
|
|
|
end
|
|
|
|
if history['HistoryType'] == 'StateUpdate'
|
|
|
|
data = history['Name']
|
|
|
|
# "%%new%%open%%"
|
|
|
|
from = nil
|
|
|
|
to = nil
|
|
|
|
if data =~ /%%(.+?)%%(.+?)%%/
|
|
|
|
from = $1
|
|
|
|
to = $2
|
|
|
|
state_from = Ticket::State.lookup( :name => from )
|
|
|
|
state_to = Ticket::State.lookup( :name => to )
|
|
|
|
if state_from
|
|
|
|
from_id = state_from.id
|
|
|
|
end
|
|
|
|
if state_to
|
|
|
|
to_id = state_to.id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
# puts "STATE UPDATE (#{history['HistoryID']}): -> #{from}->#{to}"
|
2013-06-04 12:52:56 +00:00
|
|
|
History.add(
|
2013-02-01 19:58:53 +00:00
|
|
|
:id => history['HistoryID'],
|
|
|
|
:o_id => history['TicketID'],
|
|
|
|
:history_type => 'updated',
|
|
|
|
:history_object => 'Ticket',
|
|
|
|
:history_attribute => 'ticket_state',
|
|
|
|
:value_from => from,
|
|
|
|
:id_from => from_id,
|
|
|
|
:value_to => to,
|
|
|
|
:id_to => to_id,
|
|
|
|
:created_at => history['CreateTime'],
|
|
|
|
:created_by_id => history['CreateBy']
|
|
|
|
)
|
|
|
|
end
|
|
|
|
if history['HistoryType'] == 'Move'
|
|
|
|
data = history['Name']
|
|
|
|
# "%%Queue1%%5%%Postmaster%%1"
|
|
|
|
from = nil
|
|
|
|
to = nil
|
|
|
|
if data =~ /%%(.+?)%%(.+?)%%(.+?)%%(.+?)$/
|
|
|
|
from = $1
|
|
|
|
from_id = $2
|
|
|
|
to = $3
|
|
|
|
to_id = $4
|
|
|
|
end
|
2013-06-04 12:52:56 +00:00
|
|
|
History.add(
|
2013-02-01 19:58:53 +00:00
|
|
|
:id => history['HistoryID'],
|
|
|
|
:o_id => history['TicketID'],
|
|
|
|
:history_type => 'updated',
|
|
|
|
:history_object => 'Ticket',
|
|
|
|
:history_attribute => 'group',
|
|
|
|
:value_from => from,
|
|
|
|
:value_to => to,
|
|
|
|
:id_from => from_id,
|
|
|
|
:id_to => to_id,
|
|
|
|
:created_at => history['CreateTime'],
|
|
|
|
:created_by_id => history['CreateBy']
|
|
|
|
)
|
|
|
|
end
|
|
|
|
if history['HistoryType'] == 'PriorityUpdate'
|
|
|
|
data = history['Name']
|
|
|
|
# "%%3 normal%%3%%5 very high%%5"
|
|
|
|
from = nil
|
|
|
|
to = nil
|
|
|
|
if data =~ /%%(.+?)%%(.+?)%%(.+?)%%(.+?)$/
|
|
|
|
from = $1
|
|
|
|
from_id = $2
|
|
|
|
to = $3
|
|
|
|
to_id = $4
|
|
|
|
end
|
2013-06-04 12:52:56 +00:00
|
|
|
History.add(
|
2013-02-01 19:58:53 +00:00
|
|
|
:id => history['HistoryID'],
|
|
|
|
:o_id => history['TicketID'],
|
|
|
|
:history_type => 'updated',
|
|
|
|
:history_object => 'Ticket',
|
|
|
|
:history_attribute => 'ticket_priority',
|
|
|
|
:value_from => from,
|
|
|
|
:value_to => to,
|
|
|
|
:id_from => from_id,
|
|
|
|
:id_to => to_id,
|
|
|
|
:created_at => history['CreateTime'],
|
|
|
|
:created_by_id => history['CreateBy']
|
|
|
|
)
|
|
|
|
end
|
|
|
|
if history['ArticleID'] && history['ArticleID'] != 0
|
2013-06-04 12:52:56 +00:00
|
|
|
History.add(
|
2013-02-01 19:58:53 +00:00
|
|
|
:id => history['HistoryID'],
|
|
|
|
:o_id => history['ArticleID'],
|
|
|
|
:history_type => 'created',
|
|
|
|
:history_object => 'Ticket::Article',
|
|
|
|
:related_o_id => history['TicketID'],
|
|
|
|
:related_history_object => 'Ticket',
|
|
|
|
:created_at => history['CreateTime'],
|
|
|
|
:created_by_id => history['CreateBy']
|
|
|
|
)
|
|
|
|
end
|
|
|
|
}
|
2013-01-07 08:46:38 +00:00
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.ticket_state
|
2013-01-07 09:30:07 +00:00
|
|
|
response = request( "public.pl?Action=Export;Type=State" )
|
2013-01-25 22:17:50 +00:00
|
|
|
return if !response
|
2013-01-07 08:46:38 +00:00
|
|
|
return if response.code.to_s != '200'
|
|
|
|
|
|
|
|
result = json(response)
|
|
|
|
# puts result.inspect
|
|
|
|
map = {
|
|
|
|
:ChangeTime => :updated_at,
|
|
|
|
:CreateTime => :created_at,
|
|
|
|
:CreateBy => :created_by_id,
|
|
|
|
:ChangeBy => :updated_by_id,
|
|
|
|
:Name => :name,
|
|
|
|
:ID => :id,
|
|
|
|
:ValidID => :active,
|
|
|
|
:Comment => :note,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ticket::State.all.each {|state|
|
|
|
|
state.name = state.name + '_tmp'
|
|
|
|
state.save
|
|
|
|
}
|
|
|
|
|
|
|
|
result.each { |state|
|
|
|
|
_set_valid(state)
|
|
|
|
|
|
|
|
# get new attributes
|
|
|
|
state_new = {
|
|
|
|
:created_by_id => 1,
|
|
|
|
:updated_by_id => 1,
|
|
|
|
}
|
|
|
|
map.each { |key,value|
|
|
|
|
if state[key.to_s]
|
|
|
|
state_new[value] = state[key.to_s]
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
# check if state already exists
|
|
|
|
state_old = Ticket::State.where( :id => state_new[:id] ).first
|
|
|
|
# puts 'st: ' + state['TypeName']
|
|
|
|
|
|
|
|
# set state types
|
|
|
|
if state['TypeName'] == 'pending auto'
|
|
|
|
state['TypeName'] = 'pending action'
|
|
|
|
end
|
|
|
|
ticket_state_type = Ticket::StateType.where( :name => state['TypeName'] ).first
|
|
|
|
state_new[:state_type_id] = ticket_state_type.id
|
|
|
|
if state_old
|
2013-01-07 22:27:32 +00:00
|
|
|
# puts 'TS: ' + state_new.inspect
|
2013-01-07 08:46:38 +00:00
|
|
|
state_old.update_attributes(state_new)
|
|
|
|
else
|
|
|
|
state = Ticket::State.new(state_new)
|
|
|
|
state.id = state_new[:id]
|
|
|
|
state.save
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
def self.ticket_priority
|
2013-01-07 09:30:07 +00:00
|
|
|
response = request( "public.pl?Action=Export;Type=Priority" )
|
2013-01-25 22:17:50 +00:00
|
|
|
return if !response
|
2013-01-07 08:46:38 +00:00
|
|
|
return if response.code.to_s != '200'
|
|
|
|
|
|
|
|
result = json(response)
|
|
|
|
map = {
|
|
|
|
:ChangeTime => :updated_at,
|
|
|
|
:CreateTime => :created_at,
|
|
|
|
:CreateBy => :created_by_id,
|
|
|
|
:ChangeBy => :updated_by_id,
|
|
|
|
:Name => :name,
|
|
|
|
:ID => :id,
|
|
|
|
:ValidID => :active,
|
|
|
|
:Comment => :note,
|
|
|
|
};
|
|
|
|
|
|
|
|
result.each { |priority|
|
|
|
|
_set_valid(priority)
|
|
|
|
|
|
|
|
# get new attributes
|
|
|
|
priority_new = {
|
|
|
|
:created_by_id => 1,
|
|
|
|
:updated_by_id => 1,
|
|
|
|
}
|
|
|
|
map.each { |key,value|
|
|
|
|
if priority[key.to_s]
|
|
|
|
priority_new[value] = priority[key.to_s]
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
# check if state already exists
|
|
|
|
priority_old = Ticket::Priority.where( :id => priority_new[:id] ).first
|
|
|
|
|
|
|
|
# set state types
|
|
|
|
if priority_old
|
|
|
|
priority_old.update_attributes(priority_new)
|
|
|
|
else
|
|
|
|
priority = Ticket::Priority.new(priority_new)
|
|
|
|
priority.id = priority_new[:id]
|
|
|
|
priority.save
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
def self.ticket_group
|
2013-01-07 09:30:07 +00:00
|
|
|
response = request( "public.pl?Action=Export;Type=Queue" )
|
2013-01-25 22:17:50 +00:00
|
|
|
return if !response
|
2013-01-07 08:46:38 +00:00
|
|
|
return if response.code.to_s != '200'
|
|
|
|
|
|
|
|
result = json(response)
|
|
|
|
map = {
|
|
|
|
:ChangeTime => :updated_at,
|
|
|
|
:CreateTime => :created_at,
|
|
|
|
:CreateBy => :created_by_id,
|
|
|
|
:ChangeBy => :updated_by_id,
|
|
|
|
:Name => :name,
|
|
|
|
:QueueID => :id,
|
|
|
|
:ValidID => :active,
|
|
|
|
:Comment => :note,
|
|
|
|
};
|
|
|
|
|
|
|
|
result.each { |group|
|
|
|
|
_set_valid(group)
|
|
|
|
|
|
|
|
# get new attributes
|
|
|
|
group_new = {
|
|
|
|
:created_by_id => 1,
|
|
|
|
:updated_by_id => 1,
|
|
|
|
}
|
|
|
|
map.each { |key,value|
|
|
|
|
if group[key.to_s]
|
|
|
|
group_new[value] = group[key.to_s]
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
# check if state already exists
|
|
|
|
group_old = Group.where( :id => group_new[:id] ).first
|
|
|
|
|
|
|
|
# set state types
|
|
|
|
if group_old
|
|
|
|
group_old.update_attributes(group_new)
|
|
|
|
else
|
|
|
|
group = Group.new(group_new)
|
|
|
|
group.id = group_new[:id]
|
|
|
|
group.save
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
def self.user
|
2013-01-07 09:30:07 +00:00
|
|
|
response = request( "public.pl?Action=Export;Type=User" )
|
2013-01-25 22:17:50 +00:00
|
|
|
return if !response
|
2013-01-07 08:46:38 +00:00
|
|
|
return if response.code.to_s != '200'
|
|
|
|
result = json(response)
|
|
|
|
map = {
|
|
|
|
:ChangeTime => :updated_at,
|
|
|
|
:CreateTime => :created_at,
|
|
|
|
:CreateBy => :created_by_id,
|
|
|
|
:ChangeBy => :updated_by_id,
|
|
|
|
:UserID => :id,
|
|
|
|
:ValidID => :active,
|
|
|
|
:Comment => :note,
|
|
|
|
:UserEmail => :email,
|
|
|
|
:UserFirstname => :firstname,
|
|
|
|
:UserLastname => :lastname,
|
|
|
|
# :UserTitle =>
|
|
|
|
:UserLogin => :login,
|
2013-06-04 12:52:56 +00:00
|
|
|
:UserPw => :password,
|
2013-01-07 08:46:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
result.each { |user|
|
|
|
|
# puts 'USER: ' + user.inspect
|
|
|
|
_set_valid(user)
|
|
|
|
|
2013-01-25 22:17:50 +00:00
|
|
|
role = Role.lookup( :name => 'Agent' )
|
2013-01-07 08:46:38 +00:00
|
|
|
# get new attributes
|
|
|
|
user_new = {
|
|
|
|
:created_by_id => 1,
|
|
|
|
:updated_by_id => 1,
|
|
|
|
:source => 'OTRS Import',
|
2013-01-25 22:17:50 +00:00
|
|
|
:role_ids => [ role.id ],
|
2013-01-07 08:46:38 +00:00
|
|
|
}
|
|
|
|
map.each { |key,value|
|
|
|
|
if user[key.to_s]
|
|
|
|
user_new[value] = user[key.to_s]
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
# check if state already exists
|
|
|
|
# user_old = User.where( :login => user_new[:login] ).first
|
|
|
|
user_old = User.where( :id => user_new[:id] ).first
|
|
|
|
|
|
|
|
# set state types
|
|
|
|
if user_old
|
|
|
|
puts "update User.find(#{user_new[:id]})"
|
|
|
|
# puts 'Update User' + user_new.inspect
|
2013-02-17 20:59:57 +00:00
|
|
|
user_new.delete( :role_ids )
|
2013-01-07 08:46:38 +00:00
|
|
|
user_old.update_attributes(user_new)
|
|
|
|
else
|
|
|
|
puts "add User.find(#{user_new[:id]})"
|
|
|
|
# puts 'Add User' + user_new.inspect
|
|
|
|
user = User.new(user_new)
|
|
|
|
user.id = user_new[:id]
|
|
|
|
user.save
|
|
|
|
end
|
|
|
|
|
|
|
|
# end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
def self.customer
|
|
|
|
done = false
|
|
|
|
count = 0
|
|
|
|
while done == false
|
|
|
|
sleep 2
|
|
|
|
puts "Count=#{count};Offset=#{count}"
|
2013-01-07 09:30:07 +00:00
|
|
|
response = request( "public.pl?Action=Export;Type=Customer;Count=100;Offset=#{count}" )
|
2013-01-25 22:17:50 +00:00
|
|
|
return if !response
|
2013-01-07 08:46:38 +00:00
|
|
|
count = count + 3000
|
|
|
|
return if response.code.to_s != '200'
|
|
|
|
result = json(response)
|
|
|
|
map = {
|
|
|
|
:ChangeTime => :updated_at,
|
|
|
|
:CreateTime => :created_at,
|
|
|
|
:CreateBy => :created_by_id,
|
|
|
|
:ChangeBy => :updated_by_id,
|
|
|
|
:ValidID => :active,
|
|
|
|
:UserComment => :note,
|
|
|
|
:UserEmail => :email,
|
|
|
|
:UserFirstname => :firstname,
|
|
|
|
:UserLastname => :lastname,
|
|
|
|
# :UserTitle =>
|
|
|
|
:UserLogin => :login,
|
|
|
|
:UserPassword => :password,
|
|
|
|
:UserPhone => :phone,
|
|
|
|
:UserFax => :fax,
|
|
|
|
:UserMobile => :mobile,
|
|
|
|
:UserStreet => :street,
|
|
|
|
:UserZip => :zip,
|
|
|
|
:UserCity => :city,
|
|
|
|
:UserCountry => :country,
|
|
|
|
};
|
|
|
|
|
|
|
|
done = true
|
|
|
|
result.each { |user|
|
|
|
|
done = false
|
|
|
|
_set_valid(user)
|
|
|
|
|
2013-01-25 23:06:21 +00:00
|
|
|
role = Role.lookup( :name => 'Customer' )
|
|
|
|
|
2013-01-07 08:46:38 +00:00
|
|
|
# get new attributes
|
|
|
|
user_new = {
|
|
|
|
:created_by_id => 1,
|
|
|
|
:updated_by_id => 1,
|
|
|
|
:source => 'OTRS Import',
|
2013-01-25 23:06:21 +00:00
|
|
|
:role_ids => [role.id],
|
2013-01-07 08:46:38 +00:00
|
|
|
}
|
|
|
|
map.each { |key,value|
|
|
|
|
if user[key.to_s]
|
|
|
|
user_new[value] = user[key.to_s]
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
# check if state already exists
|
|
|
|
# user_old = User.where( :login => user_new[:login] ).first
|
|
|
|
user_old = User.where( :login => user_new[:login] ).first
|
|
|
|
|
|
|
|
# set state types
|
|
|
|
if user_old
|
|
|
|
puts "update User.find(#{user_new[:id]})"
|
|
|
|
# puts 'Update User' + user_new.inspect
|
|
|
|
user_old.update_attributes(user_new)
|
|
|
|
else
|
|
|
|
# puts 'Add User' + user_new.inspect
|
|
|
|
puts "add User.find(#{user_new[:id]})"
|
|
|
|
user = User.new(user_new)
|
|
|
|
user.save
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
def self._set_valid(record)
|
|
|
|
# map
|
|
|
|
if record['ValidID'] == '3'
|
|
|
|
record['ValidID'] = '2'
|
|
|
|
end
|
|
|
|
if record['ValidID'] == '2'
|
|
|
|
record['ValidID'] = false
|
|
|
|
end
|
|
|
|
if record['ValidID'] == '1'
|
|
|
|
record['ValidID'] = true
|
|
|
|
end
|
|
|
|
if record['ValidID'] == '0'
|
|
|
|
record['ValidID'] = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|