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)
|
|
|
|
|
|
|
|
article_count_update(record)
|
|
|
|
|
|
|
|
first_response_update(record)
|
|
|
|
|
|
|
|
sender_type_update(record)
|
|
|
|
|
|
|
|
last_contact_update(record)
|
|
|
|
|
|
|
|
# save ticket
|
|
|
|
record.ticket.save
|
|
|
|
end
|
|
|
|
|
|
|
|
# get article count
|
|
|
|
def article_count_update(record)
|
|
|
|
record.ticket.article_count = record.ticket.articles.count
|
|
|
|
end
|
|
|
|
|
|
|
|
# set frist response
|
|
|
|
def first_response_update(record)
|
|
|
|
|
|
|
|
# return if we run import mode
|
|
|
|
return if Setting.get('import_mode')
|
|
|
|
|
|
|
|
# if article in internal
|
|
|
|
return true if record.internal
|
|
|
|
|
|
|
|
# if sender is not agent
|
2015-09-22 23:50:38 +00:00
|
|
|
sender = Ticket::Article::Sender.lookup(id: record.sender_id)
|
|
|
|
type = Ticket::Article::Type.lookup(id: record.type_id)
|
|
|
|
return true if sender.name != 'Agent'
|
2015-05-01 15:24:17 +00:00
|
|
|
|
|
|
|
# if article is a message to customer
|
|
|
|
return true if !type.communication
|
|
|
|
|
|
|
|
# check if first_response is already set
|
|
|
|
return true if record.ticket.first_response
|
|
|
|
|
|
|
|
# set first_response
|
|
|
|
record.ticket.first_response = record.created_at
|
|
|
|
|
|
|
|
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
|
2015-05-01 15:24:17 +00:00
|
|
|
return if count > 1
|
|
|
|
|
|
|
|
record.ticket.create_article_type_id = record.type_id
|
|
|
|
record.ticket.create_article_sender_id = record.sender_id
|
|
|
|
end
|
|
|
|
|
|
|
|
# set last contact
|
|
|
|
def last_contact_update(record)
|
|
|
|
|
|
|
|
# if article in internal
|
|
|
|
return true if record.internal
|
|
|
|
|
|
|
|
# if article is a message to customer
|
2015-09-22 23:50:38 +00:00
|
|
|
return true 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)
|
|
|
|
return true if ticket.last_contact_customer &&
|
|
|
|
ticket.last_contact &&
|
|
|
|
ticket.last_contact_customer == ticket.last_contact
|
|
|
|
|
2015-05-01 15:24:17 +00:00
|
|
|
# check if last communication is done by agent, else do not set last_contact_customer
|
2015-10-30 10:05:51 +00:00
|
|
|
if ticket.last_contact_customer.nil? ||
|
|
|
|
ticket.last_contact_agent.nil? ||
|
|
|
|
ticket.last_contact_agent.to_i > ticket.last_contact_customer.to_i
|
2015-05-01 15:24:17 +00:00
|
|
|
|
|
|
|
# set last_contact customer
|
|
|
|
record.ticket.last_contact_customer = record.created_at
|
|
|
|
|
|
|
|
# set last_contact
|
|
|
|
record.ticket.last_contact = record.created_at
|
|
|
|
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
# if sender is not agent
|
|
|
|
return if sender.name != 'Agent'
|
|
|
|
|
|
|
|
# set last_contact_agent
|
|
|
|
record.ticket.last_contact_agent = record.created_at
|
|
|
|
|
|
|
|
# set last_contact
|
|
|
|
record.ticket.last_contact = record.created_at
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|