2015-05-01 15:24:17 +00:00
|
|
|
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
|
|
|
class Observer::Ticket::ArticleChanges < ActiveRecord::Observer
|
|
|
|
observe 'ticket::_article'
|
|
|
|
|
|
|
|
def after_create(record)
|
|
|
|
|
2016-05-04 09:45:05 +00:00
|
|
|
changed = false
|
|
|
|
if article_count_update(record)
|
|
|
|
changed = true
|
|
|
|
end
|
2015-05-01 15:24:17 +00:00
|
|
|
|
2016-09-14 07:15:30 +00:00
|
|
|
if first_response_at_update(record)
|
2016-05-04 09:45:05 +00:00
|
|
|
changed = true
|
|
|
|
end
|
2015-05-01 15:24:17 +00:00
|
|
|
|
2016-05-04 09:45:05 +00:00
|
|
|
if sender_type_update(record)
|
|
|
|
changed = true
|
|
|
|
end
|
2015-05-01 15:24:17 +00:00
|
|
|
|
2016-09-14 07:15:30 +00:00
|
|
|
if last_contact_update_at(record)
|
2016-05-04 09:45:05 +00:00
|
|
|
changed = true
|
|
|
|
end
|
2015-05-01 15:24:17 +00:00
|
|
|
|
|
|
|
# save ticket
|
2016-05-04 16:12:53 +00:00
|
|
|
if !changed
|
|
|
|
record.ticket.touch
|
|
|
|
return
|
|
|
|
end
|
2015-05-01 15:24:17 +00:00
|
|
|
record.ticket.save
|
|
|
|
end
|
|
|
|
|
|
|
|
# get article count
|
|
|
|
def article_count_update(record)
|
2016-05-04 09:45:05 +00:00
|
|
|
current_count = record.ticket.article_count
|
|
|
|
sender = Ticket::Article::Sender.lookup(name: 'System')
|
|
|
|
count = Ticket::Article.where(ticket_id: record.ticket_id).where('sender_id NOT IN (?)', sender.id).count
|
|
|
|
return false if current_count == count
|
|
|
|
record.ticket.article_count = count
|
|
|
|
true
|
2015-05-01 15:24:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# set frist response
|
2016-09-14 07:15:30 +00:00
|
|
|
def first_response_at_update(record)
|
2015-05-01 15:24:17 +00:00
|
|
|
|
|
|
|
# return if we run import mode
|
2016-05-04 09:45:05 +00:00
|
|
|
return false if Setting.get('import_mode')
|
2015-05-01 15:24:17 +00:00
|
|
|
|
|
|
|
# if article in internal
|
2016-05-04 09:45:05 +00:00
|
|
|
return false if record.internal
|
2015-05-01 15:24:17 +00:00
|
|
|
|
|
|
|
# if sender is not agent
|
2015-09-22 23:50:38 +00:00
|
|
|
sender = Ticket::Article::Sender.lookup(id: record.sender_id)
|
2016-05-04 09:45:05 +00:00
|
|
|
return false if sender.name != 'Agent'
|
2015-05-01 15:24:17 +00:00
|
|
|
|
|
|
|
# if article is a message to customer
|
2016-05-04 09:45:05 +00:00
|
|
|
type = Ticket::Article::Type.lookup(id: record.type_id)
|
|
|
|
return false if !type.communication
|
2015-05-01 15:24:17 +00:00
|
|
|
|
2016-09-14 07:15:30 +00:00
|
|
|
# check if first_response_at is already set
|
|
|
|
return false if record.ticket.first_response_at
|
2015-05-01 15:24:17 +00:00
|
|
|
|
2016-09-14 07:15:30 +00:00
|
|
|
# set first_response_at
|
|
|
|
record.ticket.first_response_at = record.created_at
|
2015-05-01 15:24:17 +00:00
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
# set sender type
|
|
|
|
def sender_type_update(record)
|
|
|
|
|
|
|
|
# ignore if create channel is already set
|
2015-09-22 23:50:38 +00:00
|
|
|
count = Ticket::Article.where(ticket_id: record.ticket_id).count
|
2016-05-04 09:45:05 +00:00
|
|
|
return false if count > 1
|
2015-05-01 15:24:17 +00:00
|
|
|
|
|
|
|
record.ticket.create_article_type_id = record.type_id
|
|
|
|
record.ticket.create_article_sender_id = record.sender_id
|
2016-05-04 09:45:05 +00:00
|
|
|
true
|
2015-05-01 15:24:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# set last contact
|
2016-09-14 07:15:30 +00:00
|
|
|
def last_contact_update_at(record)
|
2015-05-01 15:24:17 +00:00
|
|
|
|
|
|
|
# if article in internal
|
2016-05-04 09:45:05 +00:00
|
|
|
return false if record.internal
|
|
|
|
|
|
|
|
# if sender is system
|
|
|
|
sender = Ticket::Article::Sender.lookup(id: record.sender_id)
|
|
|
|
return false if sender.name == 'System'
|
2015-05-01 15:24:17 +00:00
|
|
|
|
|
|
|
# if article is a message to customer
|
2016-05-04 09:45:05 +00:00
|
|
|
return false if !Ticket::Article::Type.lookup(id: record.type_id).communication
|
2015-05-01 15:24:17 +00:00
|
|
|
|
|
|
|
# if sender is customer
|
2015-09-22 23:50:38 +00:00
|
|
|
sender = Ticket::Article::Sender.lookup(id: record.sender_id)
|
2015-10-30 10:05:51 +00:00
|
|
|
ticket = record.ticket
|
2015-05-01 15:24:17 +00:00
|
|
|
if sender.name == 'Customer'
|
|
|
|
|
2015-10-30 10:05:51 +00:00
|
|
|
# if customer is sending agains, ignore update of last contact (usecase of update escalation)
|
2016-09-14 07:15:30 +00:00
|
|
|
return false if ticket.last_contact_customer_at &&
|
|
|
|
ticket.last_contact_at &&
|
|
|
|
ticket.last_contact_customer_at == ticket.last_contact_at
|
2015-10-30 10:05:51 +00:00
|
|
|
|
2016-09-14 07:15:30 +00:00
|
|
|
# check if last communication is done by agent, else do not set last_contact_customer_at
|
|
|
|
if ticket.last_contact_customer_at.nil? ||
|
|
|
|
ticket.last_contact_agent_at.nil? ||
|
|
|
|
ticket.last_contact_agent_at.to_i > ticket.last_contact_customer_at.to_i
|
2015-05-01 15:24:17 +00:00
|
|
|
|
2016-09-14 07:15:30 +00:00
|
|
|
# set last_contact_at customer
|
|
|
|
record.ticket.last_contact_customer_at = record.created_at
|
2015-05-01 15:24:17 +00:00
|
|
|
|
|
|
|
# set last_contact
|
2016-09-14 07:15:30 +00:00
|
|
|
record.ticket.last_contact_at = record.created_at
|
2015-05-01 15:24:17 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
# if sender is not agent
|
2016-05-04 09:45:05 +00:00
|
|
|
return false if sender.name != 'Agent'
|
2015-05-01 15:24:17 +00:00
|
|
|
|
2016-09-14 07:15:30 +00:00
|
|
|
# set last_contact_agent_at
|
|
|
|
record.ticket.last_contact_agent_at = record.created_at
|
2015-05-01 15:24:17 +00:00
|
|
|
|
|
|
|
# set last_contact
|
2016-09-14 07:15:30 +00:00
|
|
|
record.ticket.last_contact_at = record.created_at
|
2015-05-01 15:24:17 +00:00
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|