| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Services\Tickets; |
| 4 |
|
| 5 |
use FluentSupport\App\Models\Attachment; |
| 6 |
use FluentSupport\App\Models\Conversation; |
| 7 |
use FluentSupport\Framework\Support\Arr; |
| 8 |
|
| 9 |
class ResponseService |
| 10 |
{ |
| 11 |
public function createResponse($data, $person, $ticket) |
| 12 |
{ |
| 13 |
if(empty($data['content'])) { |
| 14 |
return false; |
| 15 |
} |
| 16 |
|
| 17 |
$convoType = Arr::get($data, 'conversation_type', 'response'); |
| 18 |
|
| 19 |
$response = [ |
| 20 |
'person_id' => $person->id, |
| 21 |
'ticket_id' => $ticket->id, |
| 22 |
'conversation_type' => $convoType, |
| 23 |
'content' => wp_unslash(wp_kses_post($data['content'])), |
| 24 |
'source' => Arr::get($data, 'source', 'web') |
| 25 |
]; |
| 26 |
|
| 27 |
$response = apply_filters('fluent_support/new_'.$person->person_type.'_' . $convoType, $response, $ticket, $person); |
| 28 |
|
| 29 |
$createdResponse = \FluentSupport\App\Models\Conversation::create($response); |
| 30 |
$createdResponse->load('person'); |
| 31 |
|
| 32 |
if ($attachments = Arr::get($data, 'attachments', [])) { |
| 33 |
Attachment::where('ticket_id', $ticket->id) |
| 34 |
->whereIn('file_hash', $attachments) |
| 35 |
->update([ |
| 36 |
'conversation_id' => $createdResponse->id, |
| 37 |
'status' => 'active' |
| 38 |
]); |
| 39 |
$createdResponse->load('attachments'); |
| 40 |
} |
| 41 |
|
| 42 |
if ($person->person_type == 'agent' && $ticket->status == 'new' && $convoType == 'response') { |
| 43 |
$ticket->status = 'active'; |
| 44 |
if($ticket->created_at) { |
| 45 |
$ticket->first_response_time = strtotime(current_time('mysql')) - strtotime($ticket->created_at); |
| 46 |
} else { |
| 47 |
$ticket->first_response_time = 300; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
$agentAdded = false; |
| 52 |
$updateData = []; |
| 53 |
|
| 54 |
if($person->person_type == 'agent') { |
| 55 |
if (!$ticket->agent_id && $convoType == 'response') { |
| 56 |
$ticket->agent_id = $person->id; |
| 57 |
$agentAdded = true; |
| 58 |
$updateData = [ |
| 59 |
'agent_id' => $person->id |
| 60 |
]; |
| 61 |
} |
| 62 |
|
| 63 |
if($convoType == 'response') { |
| 64 |
$ticket->last_agent_response = current_time('mysql'); |
| 65 |
$ticket->waiting_since = current_time('mysql'); |
| 66 |
} |
| 67 |
} else { |
| 68 |
|
| 69 |
if($ticket->last_agent_response && strtotime($ticket->last_agent_response) > strtotime($ticket->last_customer_response)) { |
| 70 |
$ticket->waiting_since = current_time('mysql'); |
| 71 |
} |
| 72 |
|
| 73 |
$ticket->last_customer_response = current_time('mysql'); |
| 74 |
} |
| 75 |
|
| 76 |
$ticket->response_count += 1; |
| 77 |
|
| 78 |
$closed = false; |
| 79 |
if (Arr::get($data, 'close_ticket') == 'yes' && $ticket->status != 'closed') { |
| 80 |
$ticket->status = 'closed'; |
| 81 |
$ticket->resolved_at = current_time('mysql'); |
| 82 |
$ticket->closed_by = $person->id; |
| 83 |
$ticket->total_close_time = current_time('timestamp') - strtotime($ticket->created_at); |
| 84 |
$closed = true; |
| 85 |
Conversation::create([ |
| 86 |
'ticket_id' => $ticket->id, |
| 87 |
'person_id' => $person->id, |
| 88 |
'conversation_type' => 'internal_info', |
| 89 |
'content' => __('Ticket has been closed', 'fluent-support') |
| 90 |
]); |
| 91 |
} |
| 92 |
|
| 93 |
$ticket->save(); |
| 94 |
|
| 95 |
if ($agentAdded) { |
| 96 |
$ticket->load('agent'); |
| 97 |
do_action('fluent_support/agent_assigned_to_ticket', $person, $ticket); |
| 98 |
$updateData['agent'] = $ticket->agent; |
| 99 |
} |
| 100 |
|
| 101 |
do_action('fluent_support/' . $convoType . '_added_by_'.$person->person_type, $createdResponse, $ticket, $person); |
| 102 |
|
| 103 |
|
| 104 |
if ($closed) { |
| 105 |
do_action('fluent_support/ticket_closed', $ticket, $person); |
| 106 |
do_action('fluent_support/ticket_closed_by_' . $person->person_type, $ticket, $person); |
| 107 |
} |
| 108 |
|
| 109 |
return [ |
| 110 |
'response' => $createdResponse, |
| 111 |
'ticket' => $ticket, |
| 112 |
'update_data' => $updateData |
| 113 |
]; |
| 114 |
} |
| 115 |
} |
| 116 |
|