nhance/app/Controllers/ThzController.php

655 lines
24 KiB
PHP

<?php
namespace App\Controllers;
use App\Controllers\BaseController;
use App\Helpers\MailHelper;
use CodeIgniter\HTTP\ResponseInterface;
use App\Models\ThzMasterModel;
use App\Models\ThzMasterNotesModel;
use App\Models\ThzTypeModel;
use App\Models\ThzHistoryModel;
use App\ControllerCleaners\ThzControllerCleaner;
use App\Models\UserModel;
use App\Models\ClientPolicyModel;
use App\Models\ClientModel;
use App\Models\TicketMailTemplateModel;
use App\Models\ClientRMModel;
class ThzController extends BaseController
{
protected $thzMasterModel;
protected $thzMasterNotesModel;
protected $thzTypeModel;
protected $thzHistoryModel;
protected $thzControllerCleaner;
protected $userModel;
protected $clientPolicyModel;
protected $clientModel;
protected $ticketMailTemplateModel;
protected $cientRmModel;
protected $myLogger;
public function __construct()
{
set_session_context('THZ-CONTROLLER');
$this->thzMasterModel = new ThzMasterModel();
$this->thzMasterNotesModel = new ThzMasterNotesModel();
$this->thzTypeModel = new thzTypeModel();
$this->thzHistoryModel = new ThzHistoryModel();
$this->thzControllerCleaner = new ThzControllerCleaner();
$this->userModel = new UserModel();
$this->clientPolicyModel = new ClientPolicyModel();
$this->clientModel = new ClientModel();
$this->cientRmModel = new ClientRMModel();
$this->ticketMailTemplateModel = new TicketMailTemplateModel();
$this->myLogger = \Config\Services::mylogger();
}
public function index()
{
//
}
public function ticketSave()
{
try {
$data = $this->request->getPost();
$references = "";
if (!empty($data['thz_id'])) {
$text = "update";
// here insert history Status.
$old = $this->thzMasterModel->where('thz_id', $data['thz_id'])->get()->getRowArray();
$result = $this->updateTicket($data);
$references = ($old && isset($data['status'])) ? $this->handleTicketHistory($old, $data) : "No Details" ;
$updateID = $data['thz_id'];
} else {
$text = "create";
$insertID = $this->insertTicket($data);
$result = true;
$references = "";
}
$id = isset($insertID) && !empty($insertID) ? $insertID : $updateID;
$emailNotificationResult = $this->ticketSaveNotification($data, $insertOrUpdate = $text, $id);
$this->myLogger->logme('error', 'emailNotificationResult: ' . json_encode($emailNotificationResult));
return $this->response->setJSON([
'status' => $result ? 'success' : 'error',
'message' => $result ? "Ticket {$text}d successfully " : "Unable to {$text} ticket. Please try again.",
'notification' => $emailNotificationResult ? 'Email Notification Success..!!' : 'Email Notification Failed..!!',
'ref' => $references
])->setStatusCode($result ? 200 : 400);
} catch (\Throwable $e) {
return handle_exception($e, $this->myLogger, $this->response);
}
}
public function ticketList()
{
$data['tab_name'] = "Tickets";
$data['page_name'] = "Tickets";
try {
$returnType = strtolower($this->request->getGet('return_type') ?? 'api');
$data = $this->request->getGet();
if ($returnType === 'web' && in_array(get_role_id(), [3, 4])) {
$data['assign_to'] = $data['assign_to'] ?? get_session_userid();
}
$tickets = $this->fetchTicketsBasedOnrole($data);
if ($returnType === 'api') {
if (empty($tickets)) {
$this->myLogger->logme('error', 'empty tickets in ticket list: ' . json_encode($tickets));
return $this->response->setJSON([ 'status' => 'error','message' => 'No tickets found'])->setStatusCode(404);
}
} else {
$data['ticket_data'] = $tickets;
$data['assignee'] = $this->userModel->where('is_active', 1)->whereIn('role', ['3', '4'])->findAll();
// enga 5-"head" and 1-"admin" assign pannvaga so dropdown la varakudhathu , 2-"manager l2 " - ivangalum assign pannalam
// 3,4 remain person varannum.
$data['client_list'] = $this->clientModel->getCreatedByUserName();
$data['ticket_type'] = $this->thzTypeModel->where('is_active', 1)->findAll();
return $this->loadLayout('thz_list', $data);
}
return $this->response->setJSON([
'status' => 'success',
'data' => $tickets,
])->setStatusCode(200);
} catch (\Throwable $e) {
return handle_exception($e, $this->myLogger, $this->response);
}
}
public function ticketConversationSave()
{
try {
$data = $this->request->getPost();
$data['notes_type'] = $data['notes_type'] ?? 'External';
$result = $this->thzMasterNotesModel->insert($data);
if (empty($result)) {
$this->myLogger->logme('error', 'Ticket conversation insert failed..!!: ' . json_encode($result));
throw new \RuntimeException('No tickets found', 404);
}
$emailNotificationResult = ($data['notes_type'] === "External")
? $this->ticketConversationSaveNotification($data)
: false;
return $this->response->setJSON([
'status' => $result ? 'success' : 'error',
'message' => $result ? "Ticket Notes created successfully" : "Unable to create ticket notes. Please try again.",
'notification' => $emailNotificationResult ? 'Email Notification Success..!!' : 'Email Notification Failed Or No Need..!!'
])->setStatusCode($result ? 200 : 400);
} catch (\Throwable $e) {
return handle_exception($e, $this->myLogger, $this->response);
}
}
public function ticketAutoFetchDetails()
{
try {
$data = $this->request->getPost();
$client_id = $data['client_id'] ?? null;
$mobile = $data['mobile'] ?? null;
if ($client_id && $mobile) {
$details = $this->thzMasterModel->ticketAutoFetchDetails($client_id, $mobile);
if (empty($details)) {
$this->myLogger->logme('error', 'empty AutoFetchDetails for client_id : ' . json_encode($client_id));
throw new \RuntimeException('No Data found', 400);
}
$policyIds = array_filter(array_column($details, 'policy_id'));
$clientPolicy = !empty($policyIds)
? $this->clientPolicyModel->whereIn('id', $policyIds)->where('is_active', 1)->get()->getResultArray()
: [];
$result['client_details'] = $details;
$result['policy_details'] = $clientPolicy ?? [];
} else {
throw new \RuntimeException('Client and Mobile Not found', 400);
}
return $this->response->setJSON(['status' => 'success', 'data' => $result])->setStatusCode(200);
} catch (\Throwable $e) {
return handle_exception($e, $this->myLogger, $this->response);
}
}
public function ticketConversationList()
{
$data = $this->request->getGet();
$returnType = strtolower($this->request->getGet('return_type') ?? 'api');
$thz_id = $data['thz_id'] ?? null;
$ticketType = $data['notes_type'] ?? 'External';
if ($thz_id) {
$result['master'] = $this->thzMasterModel
->select('thz_master.*, user_profiles.first_name as assignee_name')
->join('user_profiles', 'user_profiles.id = thz_master.assign_to', 'left')
->where('thz_master.thz_id', $thz_id)
->findAll();
if (!empty($result['master'])) {
$notes = $this->thzMasterNotesModel->ticketConversationList($thz_id, $returnType);
$result['notes'] = !empty($notes) ? $notes : [];
} else {
$this->myLogger->logme('error', 'No tickets found for ticket id ' . json_encode($thz_id));
throw new \RuntimeException('No tickets found', 400);
}
} else {
// return $this->response->setJSON((['status' => 'error', 'message' => 'No tickets found']))->setStatusCode(404);
throw new \RuntimeException('No tickets found', 404);
}
if ($returnType === 'api') {
try {
return $this->response->setJSON((['status' => 'success', 'data' => $result]))->setStatusCode(200);
} catch (\Throwable $e) {
return handle_exception($e, $this->myLogger, $this->response);
}
} else {
$assign_to = $result['master'][0]['assign_to'];
$mobile = $result['master'][0]['mobile'];
$toGetPolicyId = (!empty($result['master'][0]['policy_id']) && strtolower($result['master'][0]['policy_id']) !== 'null')
? $result['master'][0]['policy_id']
: '';
$result['related_tickets'] = $this->thzMasterModel
->select('thz_master.*, user_profiles.first_name as assignee_name')
->join('user_profiles', 'user_profiles.id = thz_master.assign_to', 'left')
->where('thz_master.assign_to', $assign_to)
->where('thz_master.mobile', $mobile)
->where('thz_master.thz_id !=', $thz_id)
->where('thz_master.status !=', 'Closed')
->where('thz_master.status !=', 'Resolved')
->findAll();
$result['assignee'] = $this->userModel->where('is_active', 1)->whereIn('role', ['2', '3', '4'])->findAll(); // enga 5-"head" and 1-"admin" assign pannvaga so dropdown la varakudhathu
// 2,3,4 remain person varannum.
$result['policy_terms'] = $toGetPolicyId ? $this->getPolicyTerms($toGetPolicyId) : '';
$result['ticket_type'] = $this->thzTypeModel->where('is_active', 1)->findAll();
return $this->loadLayout('thz_notes', $result);
}
}
public function ticketType()
{
$method = $this->request->getMethod(); // get, post
try {
if ($method === 'get') {
$types = $this->thzTypeModel->select('id,name,is_active')->where('is_active', 1)->findAll();
if (empty($types)) {
$this->myLogger->logme('error', 'No ticket types found ');
return $this->response->setJSON(['status' => 'error', 'message' => 'No ticket types found'])->setStatusCode(404);
}
return $this->response->setJSON(['status' => 'success', 'data' => $types])->setStatusCode(200);
}
if ($method === 'post') {
$data = $this->request->getPost();
if (!empty($data['id'])) {
$text = "update";
$result = $this->thzTypeModel->update($data['id'], $data);
$updateID = $data['id'];
} else {
$text = "create";
$insertID = $this->thzTypeModel->insert($data);
$result = true;
}
$id = isset($insertID) && !empty($insertID) ? $insertID : ($updateID ?? null);
return $this->response->setJSON([
'status' => $id ? 'success' : 'error',
'message' => $id ? "Ticket Type {$text}d successfully" : "Unable to {$text} ticket type. Please try again.",
'id' => $id
])->setStatusCode($id ? 200 : 400);
}
return $this->response->setJSON(['status' => 'error', 'message' => 'Invalid request method'])->setStatusCode(405);
} catch (\Throwable $e) {
return handle_exception($e, $this->myLogger, $this->response);
}
}
public function ticketHistoryList()
{
try {
$data = $this->request->getGet();
$thz_id = $data['thz_id'];
// $details = $this->thzHistoryModal->whereIn('thz_id', $thz_id)->where('is_active', 1)->get()->getResultArray();
$details = $this->thzHistoryModel->select('thz_history.*, user_profiles.first_name as created_name')
->join('user_profiles', 'user_profiles.id = thz_history.created_by', 'left')
->where('thz_history.thz_id', $thz_id)->where('thz_history.is_active', 1)
->orderBy('thz_history.created_at', 'desc')
->get()->getResultArray();
if (empty($details)) {
$this->myLogger->logme('error', 'No history details found for ticket id ' . json_encode($thz_id));
throw new \RuntimeException('No History found', 404);
}
return $this->response->setJSON(['status' => 'success', 'data' => $details])->setStatusCode(200);
} catch (\Throwable $e) {
return handle_exception($e, $this->myLogger, $this->response);
}
}
/************************************************** PRIVATE FUNCTIONS ********************************************************/
private function fetchTicketsBasedOnrole(array $data): array
{
// $id = $data['thz_id'] ?? null;
$assign_to = $data['assign_to'] ?? null;
$mobile = $data['mobile'] ?? null;
if (!empty($assign_to)) {
// Tickets assigned to a staff
return $this->thzMasterModel
->select('thz_master.*, user_profiles.first_name as assignee_name')
->join('user_profiles', 'user_profiles.id = thz_master.assign_to', 'left')
->where('thz_master.assign_to', $assign_to)
->orderBy('thz_master.created_at', 'desc')
->findAll();
}
// if (!empty($id) && !empty($mobile)) {
if (!empty($mobile)) {
// Specific ticket by ID and mobile
return $this->thzMasterModel
->select('thz_master.*, user_profiles.first_name as assignee_name')
->join('user_profiles', 'user_profiles.id = thz_master.assign_to', 'left')
// ->where('thz_id', $id)
->where('thz_master.mobile', $mobile)
->orderBy('thz_master.created_at', 'desc')
->findAll();
}
// All tickets (e.g., for managers)
return $this->thzMasterModel
->select('thz_master.*, user_profiles.first_name as assignee_name')
->join('user_profiles', 'user_profiles.id = thz_master.assign_to', 'left')
->orderBy('thz_master.created_at', 'desc')
->findAll();
}
private function ticketSaveNotification($data, $insertOrUpdate, $id)
{
$user_email = $data['email'] ?? null;
$assignee_id = $data['assign_to'] ?? null;
$assignee_email = $this->findAssigneeEmail($assignee_id);
$client_id = $data['client_id'] ?? null;
$emailToNotified = [
'user_email' => $user_email,
'assignee_email' => $assignee_email
];
$ticketId = $id;
$result = $this->ticketSaveEmailNotificationBasedOnRole($emailToNotified, $insertOrUpdate, $ticketId, $data, $client_id);
return $result;
}
private function ticketConversationSaveNotification($data)
{
$thz_id = $data['thz_id']; // thz is alias of ticket
$data['subject'] = $this->findSubject($data['thz_id']);
$assignee_id = $this->findAssigneeId($data['thz_id']);
$user_email = $this->findUserEmail($data['thz_id']);
$assignee_email = $this->findAssigneeEmail($assignee_id) ?? '';
$sender_mail = $data['notes_by'] == "User" ? $user_email : $assignee_email;
$receiver_mail = $data['notes_by'] == "User" ? $assignee_email : $user_email;
$templateName = "ticket_conversation_save_notification";
$to_email = $receiver_mail;
$ticketId = $thz_id;
// $message = $data['notes'] ?? '';
$this->sendEmail($templateName, $data, $to_email, $ticketId);
return true;
}
private function findAssigneeEmail($assignee_id)
{
$row = $this->userModel->where('id', $assignee_id)->get()->getRow();
$assigneeEmail = $row ? $row->email : null;
return $assigneeEmail ? $assigneeEmail : "";
}
private function findAssigneeId($ticketId)
{
$row = $this->thzMasterModel->where('thz_id', $ticketId)->get()->getRow();
$assigneeId = $row ? $row->assign_to : null;
return $assigneeId ? $assigneeId : 0;
}
private function findUserEmail($ticket_id)
{
$row = $this->thzMasterModel->where('thz_id', $ticket_id)->get()->getRow();
$email = $row ? $row->email : null;
return $email ? $email : " ";
}
private function findSubject($ticketId)
{
$row = $this->thzMasterModel->where('thz_id', $ticketId)->get()->getRow();
$subject = $row ? $row->subject : null;
return $subject ? $subject : " ";
}
private function ticketSaveEmailNotificationBasedOnRole($emailToNotified, $insertOrUpdate, $ticketId, $data, $client_id)
{
$user_email = $emailToNotified['user_email'] ?? null;
$assignee_email = $emailToNotified['assignee_email'] ?? null;
if (!empty($assignee_email)) {
switch ($insertOrUpdate) {
case 'update':
$this->sendEmail('ticket_save_update_assignee', $data, $assignee_email, $ticketId);
break;
case 'create':
$this->sendEmail('ticket_save_create_assignee', $data, $assignee_email, $ticketId);
break;
default:
# code...
break;
}
}
if (!empty($user_email)) {
switch ($insertOrUpdate) {
case 'update':
$data['assigneeAndStatus'] = empty($assignee_email)
? "Not Yet Assigned"
: "Assigned With {$assignee_email}";
$this->sendEmail('ticket_save_update_user', $data, $user_email, $ticketId);
break;
case 'create':
$account_manager_email = $this->cientRmModel->findAccountManagerEmail($client_id);
$cc = getenv('G_ticketing_system_cc');
$this->myLogger->logme('error', "env for cc G_ticketing_system_cc " . $cc);
$template = empty($assignee_email)
? 'ticket_save_create_user_unassigned'
: 'ticket_save_create_user_assigned';
if (!empty($assignee_email)) {
$data['assignee_email'] = $assignee_email;
}
$this->sendEmail($template, $data, $user_email, $ticketId);
// send to account manager and if it is more than one email also no problem we send to all account managers
if (!empty($account_manager_email)) {
$this->myLogger->logme('error', "mail sent for account manager " . json_encode($account_manager_email));
$this->sendEmail($template, $data, $account_manager_email, $ticketId, $cc);
} else {
$this->myLogger->logme('error', "mail not sent for account manager ");
}
break;
default:
# code...
break;
}
}
return true;
}
private function updateTicket($data)
{
$data['updated_by'] = get_session_userid();
$ticket_id = $data['thz_id'];
$result = $this->thzMasterModel->where('thz_id', $ticket_id)->set($data)->update();
return $result;
}
private function insertTicket($data)
{
$data['created_by'] = get_session_userid();
$this->thzMasterModel->insert($data);
return $this->thzMasterModel->insertID(); // returns the inserted row's ID
}
private function sendEmail($templateName, $data, $to_email, $ticketId, $cc = "", $bcc = "")
{
$mailTemplate = $this->ticketMailTemplateModel->where('template_name', $templateName)->findAll()[0] ?? [];
if (empty($mailTemplate)) {
$this->myLogger->logme('error', 'No Mail Template Found To send email in ticketing system');
return;
}
if (empty($cc)) {
$this->myLogger->logme('error', 'empty cc attached for template name : ' . json_encode($templateName));
} else {
$this->myLogger->logme('error', "cc attached for template name : " . json_encode($templateName));
}
$mailTemplate['subject'] = str_replace('%subject%', $data['subject'] ?? '', $mailTemplate['subject']);
$mailTemplate['mail_content'] = str_replace('%subject%', $data['subject'] ?? '', $mailTemplate['mail_content']);
$mailTemplate['mail_content'] = str_replace('%ticketId%', $ticketId ?? '', $mailTemplate['mail_content']);
$mailTemplate['mail_content'] = str_replace('%message%', $data['notes'] ?? '', $mailTemplate['mail_content']);
$mailTemplate['mail_content'] = str_replace('%assignee_email%', $data['assignee_email'] ?? '', $mailTemplate['mail_content']);
$mailTemplate['mail_content'] = str_replace('%assigneeAndStatus%', $data['assigneeAndStatus'] ?? '', $mailTemplate['mail_content']);
$from_mail = "";
$to_mail = $to_email;
$cc_string = $cc;
$subject = $mailTemplate['subject'];
$message = $mailTemplate['mail_content'];
$attachments = "";
$reply_to = "";
$bcc_string = $bcc;
$res = MailHelper::send_email(['from_mail' => $from_mail, 'mail' => $to_mail, 'cc' => $cc_string, 'subject' => $subject, 'message' => $message, 'attachments' => $attachments, 'reply_to' => $reply_to, 'bcc' => $bcc_string]);
$this->myLogger->logme('error', 'Mail Response : ' . json_encode($res));
}
public function getPolicyTerms($client_policy_id)
{
$policy_data = $this->clientPolicyModel->where('is_active', 1)->where('id', $client_policy_id)->first();
$policy_terms = [];
if (isset($policy_data['policy_terms']) && !empty($policy_data['policy_terms'])) {
$raw_terms = json_decode($policy_data['policy_terms'] ?? [], true) ?? [];
$ticketController = new TicketController();
$policy_terms = $ticketController->convertTermsToDisplay($raw_terms);
}
$data['policy_terms'] = $policy_terms;
return view('view_policy_terms', $data);
}
private function handleTicketHistory(array $old, array $data): string
{
if ($old['status'] != $data['status']) {
$history = [
'thz_id' => $data['thz_id'],
'field_name' => 'status',
'display_name' => 'Status',
'old_value' => $old['status'],
'new_value' => $data['status'],
'is_active' => 1
];
if (!empty(get_session_userid())) {
$history['created_by'] = get_session_userid();
}
$hist_id = $this->thzHistoryModel->insert($history);
return $old['status'] . " - " . $data['status'] . " - " . $hist_id;
}
return $old['status'] . " - " . $data['status'];
}
}