diff --git a/app/Controllers/NotificationController.php b/app/Controllers/NotificationController.php index efdc2571..b497e958 100755 --- a/app/Controllers/NotificationController.php +++ b/app/Controllers/NotificationController.php @@ -87,6 +87,8 @@ class NotificationController extends AdminController 'client_hr_summary_mail_btn' ]; $data = []; + $emptyTemplate = []; + $updatedTemplate = []; foreach ($checkboxNames as $checkboxName) { @@ -111,9 +113,31 @@ class NotificationController extends AdminController $id = $find['id']; $changeData['enabled']= $value; $update = $notificationModel->update($id, $changeData); + $updatedTemplate [] = $key; + } + + if(!$find && $value == 1){ + $emptyTemplate [] = $key; } } - return json_encode("true"); + + if(count($emptyTemplate) > 0){ + return $this->response->setJSON([ + 'status' => false, + 'code' => 400, + 'message' => 'Notification settings updation Failed.', + 'updated_templates' => $updatedTemplate, + 'empty_templates' => $emptyTemplate + ]); + } + + return $this->response->setJSON([ + 'status' => true, + 'code' => 200, + 'message' => 'Notification settings updated successfully.', + 'updated_templates' => $updatedTemplate, + 'empty_templates' => $emptyTemplate + ]); } // This for Load the Template Data for in View Page diff --git a/app/Controllers/ThzController.php b/app/Controllers/ThzController.php index e5c4c6dd..0dea90c1 100644 --- a/app/Controllers/ThzController.php +++ b/app/Controllers/ThzController.php @@ -15,6 +15,7 @@ use App\Models\UserModel; use App\Models\ClientPolicyModel; use App\Models\ClientModel; use App\Models\TicketMailTemplateModel; +use App\Models\ClientRMModel; class ThzController extends BaseController @@ -31,6 +32,8 @@ class ThzController extends BaseController protected $clientModel; protected $ticketMailTemplateModel; + + protected $cientRmModel; protected $myLogger; public function __construct() @@ -43,6 +46,7 @@ class ThzController extends BaseController $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(); } @@ -52,178 +56,181 @@ class ThzController extends BaseController // } - 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); - - if ($old && isset($data['status'])) { - - 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); - $references = $old['status']." - ".$data['status']." - ".$hist_id; - }else{ - $references = $old['status']." - ".$data['status']; - } - }else{ - $references = "No Details"; - } - - $updateID = $data['thz_id']; - - } else { - - $text = "create"; - $insertID = $this->insertTicket($data); - $result = true; + 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); - $id = isset($insertID) && !empty($insertID) ? $insertID : $updateID ; + if ($old && isset($data['status'])) { - $emailNotificationResult = $this->ticketSaveNotification($data , $insertOrUpdate = $text , $id); + 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 + ]; - return $this->response->setJSON([ + if (!empty(get_session_userid())) { + $history['created_by'] = get_session_userid(); + } + + $hist_id = $this->thzHistoryModel->insert($history); + $references = $old['status'] . " - " . $data['status'] . " - " . $hist_id; + } else { + $references = $old['status'] . " - " . $data['status']; + } + } else { + $references = "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); + + return $this->response->setJSON([ 'status' => $result ? 'success' : 'error', - 'message' => $result ? "Ticket {$text}d successfully " : "Unable to {$text} ticket. Please try again." , + '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) { - $code = ($e->getCode() && $e->getCode() >= 100 && $e->getCode() < 600) ? $e->getCode() : 500; + ])->setStatusCode($result ? 200 : 400); + } catch (\Throwable $e) { + $code = ($e->getCode() && $e->getCode() >= 100 && $e->getCode() < 600) ? $e->getCode() : 500; - $isDbError = $e instanceof \CodeIgniter\Database\Exceptions\DatabaseException - || $e instanceof \mysqli_sql_exception - || $e instanceof \PDOException; + $isDbError = $e instanceof \CodeIgniter\Database\Exceptions\DatabaseException + || $e instanceof \mysqli_sql_exception + || $e instanceof \PDOException; - $context = ['title' => get_class($e),'type' => get_class($e),'code' => $code,'message' => $e->getMessage(),'file' => $e->getFile(),'line' => $e->getLine()]; + $context = ['title' => get_class($e), 'type' => get_class($e), 'code' => $code, 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]; - $this->myLogger->logme('error', "Exception: {message} in {file} on line {line}", $context, 0); - - return $this->response->setJSON([ 'status' => 'error', 'message' => $isDbError ? 'Data Access Error' : $e->getMessage(), - 'ref' => $isDbError ? 'database - ' : 'application - ' . $code . ' - ' . $e->getLine() - ])->setStatusCode($code); + $this->myLogger->logme('error', "Exception: {message} in {file} on line {line}", $context, 0); + + return $this->response->setJSON([ + 'status' => 'error', + 'message' => $isDbError ? 'Data Access Error' : $e->getMessage(), + 'ref' => $isDbError ? 'database - ' : 'application - ' . $code . ' - ' . $e->getLine() + ])->setStatusCode($code); } } - + public function ticketList() { - try{ - $returnType = strtolower($this->request->getGet('return_type') ?? 'api'); - - - $data = $this->request->getGet(); - if ($returnType === 'web' && in_array(get_role_id(), [2,3,4])) { - $data['assign_to'] = $data['assign_to'] ?? get_session_userid(); - } + try { + $returnType = strtolower($this->request->getGet('return_type') ?? 'api'); - $tickets = $this->fetchTicketsBasedOnrole($data); - - if ($returnType === 'api') { - if (empty($tickets)) { - // return $this->response->setJSON([ 'status' => 'error','message' => 'No tickets found'])->setStatusCode(404); - throw new \RuntimeException('No tickets found', 400); + $data = $this->request->getGet(); + if ($returnType === 'web' && in_array(get_role_id(), [2, 3, 4])) { + $data['assign_to'] = $data['assign_to'] ?? get_session_userid(); } - }else{ - - $data['page_name'] = "Tickets"; - $data['ticket_data'] = $tickets; - $data['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. - $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) { - $code = ($e->getCode() && $e->getCode() >= 100 && $e->getCode() < 600) ? $e->getCode() : 500; + $tickets = $this->fetchTicketsBasedOnrole($data); - $isDbError = $e instanceof \CodeIgniter\Database\Exceptions\DatabaseException - || $e instanceof \mysqli_sql_exception - || $e instanceof \PDOException; - $context = ['title' => get_class($e),'type' => get_class($e),'code' => $code,'message' => $e->getMessage(),'file' => $e->getFile(),'line' => $e->getLine()]; + if ($returnType === 'api') { + if (empty($tickets)) { + // return $this->response->setJSON([ 'status' => 'error','message' => 'No tickets found'])->setStatusCode(404); + throw new \RuntimeException('No tickets found', 400); + } + } else { - $this->myLogger->logme('error', "Exception: {message} in {file} on line {line}", $context, 0); - - return $this->response->setJSON([ 'status' => 'error', 'message' => $isDbError ? 'Data Access Error' : $e->getMessage(), - 'ref' => $isDbError ? 'database - ' : 'application - ' . $code . ' - ' . $e->getLine() - ])->setStatusCode($code); - } + $data['page_name'] = "Tickets"; + $data['ticket_data'] = $tickets; + $data['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. + $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) { + $code = ($e->getCode() && $e->getCode() >= 100 && $e->getCode() < 600) ? $e->getCode() : 500; + + $isDbError = $e instanceof \CodeIgniter\Database\Exceptions\DatabaseException + || $e instanceof \mysqli_sql_exception + || $e instanceof \PDOException; + + $context = ['title' => get_class($e), 'type' => get_class($e), 'code' => $code, 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]; + + $this->myLogger->logme('error', "Exception: {message} in {file} on line {line}", $context, 0); + + return $this->response->setJSON([ + 'status' => 'error', + 'message' => $isDbError ? 'Data Access Error' : $e->getMessage(), + 'ref' => $isDbError ? 'database - ' : 'application - ' . $code . ' - ' . $e->getLine() + ])->setStatusCode($code); + } } - public function ticketConversationSave(){ + public function ticketConversationSave() + { - try { + try { $data = $this->request->getPost(); - $data['notes_type'] = $data['notes_type'] ?? 'External' ; + $data['notes_type'] = $data['notes_type'] ?? 'External'; $result = $this->thzMasterNotesModel->insert($data); - if(empty($result)){ + if (empty($result)) { // return $this->response->setJSON((['status' => 'error', 'message' => 'No tickets found']))->setStatusCode(404); - throw new \RuntimeException('No tickets found', 404); + throw new \RuntimeException('No tickets found', 404); } - if($data['notes_type'] == "External"){ + if ($data['notes_type'] == "External") { $emailNotificationResult = $this->ticketConversationSaveNotification($data); - }else{ - $emailNotificationResult = false ; + } else { + $emailNotificationResult = false; } - - - return $this->response->setJSON([ + + + return $this->response->setJSON([ 'status' => $result ? 'success' : 'error', - 'message' => $result ? "Ticket Notes created successfully" : "Unable to create ticket notes. Please try again." , + '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) { - $code = ($e->getCode() && $e->getCode() >= 100 && $e->getCode() < 600) ? $e->getCode() : 500; + ])->setStatusCode($result ? 200 : 400); + } catch (\Throwable $e) { + $code = ($e->getCode() && $e->getCode() >= 100 && $e->getCode() < 600) ? $e->getCode() : 500; - $isDbError = $e instanceof \CodeIgniter\Database\Exceptions\DatabaseException - || $e instanceof \mysqli_sql_exception - || $e instanceof \PDOException; + $isDbError = $e instanceof \CodeIgniter\Database\Exceptions\DatabaseException + || $e instanceof \mysqli_sql_exception + || $e instanceof \PDOException; - $context = ['title' => get_class($e),'type' => get_class($e),'code' => $code,'message' => $e->getMessage(),'file' => $e->getFile(),'line' => $e->getLine()]; + $context = ['title' => get_class($e), 'type' => get_class($e), 'code' => $code, 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]; - $this->myLogger->logme('error', "Exception: {message} in {file} on line {line}", $context, 0); - - return $this->response->setJSON([ 'status' => 'error', 'message' => $isDbError ? 'Data Access Error' : $e->getMessage(), - 'ref' => $isDbError ? 'database - ' : 'application - ' . $code . ' - ' . $e->getLine() - ])->setStatusCode($code); + $this->myLogger->logme('error', "Exception: {message} in {file} on line {line}", $context, 0); + + return $this->response->setJSON([ + 'status' => 'error', + 'message' => $isDbError ? 'Data Access Error' : $e->getMessage(), + 'ref' => $isDbError ? 'database - ' : 'application - ' . $code . ' - ' . $e->getLine() + ])->setStatusCode($code); } } @@ -254,190 +261,193 @@ class ThzController extends BaseController } return $this->response->setJSON(['status' => 'success', 'data' => $result])->setStatusCode(200); + } catch (\Throwable $e) { + $code = ($e->getCode() && $e->getCode() >= 100 && $e->getCode() < 600) ? $e->getCode() : 500; - } - catch (\Throwable $e) { - $code = ($e->getCode() && $e->getCode() >= 100 && $e->getCode() < 600) ? $e->getCode() : 500; + $isDbError = $e instanceof \CodeIgniter\Database\Exceptions\DatabaseException + || $e instanceof \mysqli_sql_exception + || $e instanceof \PDOException; - $isDbError = $e instanceof \CodeIgniter\Database\Exceptions\DatabaseException - || $e instanceof \mysqli_sql_exception - || $e instanceof \PDOException; + $context = ['title' => get_class($e), 'type' => get_class($e), 'code' => $code, 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]; - $context = ['title' => get_class($e),'type' => get_class($e),'code' => $code,'message' => $e->getMessage(),'file' => $e->getFile(),'line' => $e->getLine()]; + $this->myLogger->logme('error', "Exception: {message} in {file} on line {line}", $context, 0); - $this->myLogger->logme('error', "Exception: {message} in {file} on line {line}", $context, 0); - - return $this->response->setJSON([ 'status' => 'error', 'message' => $isDbError ? 'Data Access Error' : $e->getMessage(), - 'ref' => $isDbError ? 'database - ' : 'application - ' . $code . ' - ' . $e->getLine() - ])->setStatusCode($code); + return $this->response->setJSON([ + 'status' => 'error', + 'message' => $isDbError ? 'Data Access Error' : $e->getMessage(), + 'ref' => $isDbError ? 'database - ' : 'application - ' . $code . ' - ' . $e->getLine() + ])->setStatusCode($code); } } public function ticketConversationList() { - $data = $this->request->getGet(); + $data = $this->request->getGet(); - $returnType = strtolower($this->request->getGet('return_type') ?? 'api'); + $returnType = strtolower($this->request->getGet('return_type') ?? 'api'); - $thz_id = $data['thz_id'] ?? null; - - $ticketType = $data['notes_type'] ?? 'External' ; + $thz_id = $data['thz_id'] ?? null; - if($thz_id){ - $result['master'] = $this->thzMasterModel - ->select('thz_master.*, CONCAT(user_profiles.first_name, " ", user_profiles.last_name) as assignee_name') - ->join('user_profiles', 'user_profiles.id = thz_master.assign_to', 'left') - ->where('thz_master.thz_id', $thz_id) - ->findAll(); + $ticketType = $data['notes_type'] ?? 'External'; - if(!empty($result['master'])){ - $notes = $this->thzMasterNotesModel->ticketConversationList($thz_id, $returnType ); - $result['notes'] = !empty($notes) ? $notes : []; - }else{ - // return $this->response->setJSON((['status' => 'error', 'message' => 'No tickets found']))->setStatusCode(400); - 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 ($thz_id) { + $result['master'] = $this->thzMasterModel + ->select('thz_master.*, CONCAT(user_profiles.first_name, " ", user_profiles.last_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 { + // return $this->response->setJSON((['status' => 'error', 'message' => 'No tickets found']))->setStatusCode(400); + 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) { - $code = ($e->getCode() && $e->getCode() >= 100 && $e->getCode() < 600) ? $e->getCode() : 500; + if ($returnType === 'api') { + try { + return $this->response->setJSON((['status' => 'success', 'data' => $result]))->setStatusCode(200); + } catch (\Throwable $e) { + $code = ($e->getCode() && $e->getCode() >= 100 && $e->getCode() < 600) ? $e->getCode() : 500; - $isDbError = $e instanceof \CodeIgniter\Database\Exceptions\DatabaseException - || $e instanceof \mysqli_sql_exception - || $e instanceof \PDOException; + $isDbError = $e instanceof \CodeIgniter\Database\Exceptions\DatabaseException + || $e instanceof \mysqli_sql_exception + || $e instanceof \PDOException; - $context = ['title' => get_class($e),'type' => get_class($e),'code' => $code,'message' => $e->getMessage(),'file' => $e->getFile(),'line' => $e->getLine()]; + $context = ['title' => get_class($e), 'type' => get_class($e), 'code' => $code, 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]; - $this->myLogger->logme('error', "Exception: {message} in {file} on line {line}", $context, 0); - - return $this->response->setJSON([ 'status' => 'error', 'message' => $isDbError ? 'Data Access Error' : $e->getMessage(), - 'ref' => $isDbError ? 'database - ' : 'application - ' . $code . ' - ' . $e->getLine() - ])->setStatusCode($code); - } - }else{ + $this->myLogger->logme('error', "Exception: {message} in {file} on line {line}", $context, 0); - $assign_to = $result['master'][0]['assign_to']; - $mobile = $result['master'][0]['mobile']; + return $this->response->setJSON([ + 'status' => 'error', + 'message' => $isDbError ? 'Data Access Error' : $e->getMessage(), + 'ref' => $isDbError ? 'database - ' : 'application - ' . $code . ' - ' . $e->getLine() + ])->setStatusCode($code); + } + } else { - $toGetPolicyId = (!empty($result['master'][0]['policy_id']) && strtolower($result['master'][0]['policy_id']) !== 'null') - ? $result['master'][0]['policy_id'] - : ''; + $assign_to = $result['master'][0]['assign_to']; + $mobile = $result['master'][0]['mobile']; - $result['related_tickets'] = $this->thzMasterModel - ->select('thz_master.*, CONCAT(user_profiles.first_name, " ", user_profiles.last_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) : ''; + $toGetPolicyId = (!empty($result['master'][0]['policy_id']) && strtolower($result['master'][0]['policy_id']) !== 'null') + ? $result['master'][0]['policy_id'] + : ''; - $result['ticket_type'] = $this->thzTypeModel->where('is_active', 1)->findAll(); - - return $this->loadLayout('thz_notes', $result); - - } + $result['related_tickets'] = $this->thzMasterModel + ->select('thz_master.*, CONCAT(user_profiles.first_name, " ", user_profiles.last_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') { + try { + if ($method === 'get') { $types = $this->thzTypeModel->select('id,name,is_active')->where('is_active', 1)->findAll(); if (empty($types)) { - return $this->response->setJSON(['status' => 'error','message' => 'No ticket types found'])->setStatusCode(404); + return $this->response->setJSON(['status' => 'error', 'message' => 'No ticket types found'])->setStatusCode(404); } - return $this->response->setJSON(['status' => 'success','data' => $types])->setStatusCode(200); - } - + return $this->response->setJSON(['status' => 'success', 'data' => $types])->setStatusCode(200); + } + if ($method === 'post') { - + $data = $this->request->getPost(); if (!empty($data['id'])) { - $text = "update"; + $text = "update"; $result = $this->thzTypeModel->update($data['id'], $data); $updateID = $data['id']; } else { - $text = "create"; + $text = "create"; $insertID = $this->thzTypeModel->insert($data); $result = true; } $id = isset($insertID) && !empty($insertID) ? $insertID : ($updateID ?? null); - return $this->response->setJSON([ + 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) { - $code = ($e->getCode() && $e->getCode() >= 100 && $e->getCode() < 600) ? $e->getCode() : 500; + return $this->response->setJSON(['status' => 'error', 'message' => 'Invalid request method'])->setStatusCode(405); + } catch (\Throwable $e) { + $code = ($e->getCode() && $e->getCode() >= 100 && $e->getCode() < 600) ? $e->getCode() : 500; - $isDbError = $e instanceof \CodeIgniter\Database\Exceptions\DatabaseException - || $e instanceof \mysqli_sql_exception - || $e instanceof \PDOException; + $isDbError = $e instanceof \CodeIgniter\Database\Exceptions\DatabaseException + || $e instanceof \mysqli_sql_exception + || $e instanceof \PDOException; - $context = ['title' => get_class($e),'type' => get_class($e),'code' => $code,'message' => $e->getMessage(),'file' => $e->getFile(),'line' => $e->getLine()]; + $context = ['title' => get_class($e), 'type' => get_class($e), 'code' => $code, 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]; - $this->myLogger->logme('error', "Exception: {message} in {file} on line {line}", $context, 0); - - return $this->response->setJSON([ 'status' => 'error', 'message' => $isDbError ? 'Data Access Error' : $e->getMessage(), - 'ref' => $isDbError ? 'database - ' : 'application - ' . $code . ' - ' . $e->getLine() - ])->setStatusCode($code); - } + $this->myLogger->logme('error', "Exception: {message} in {file} on line {line}", $context, 0); + + return $this->response->setJSON([ + 'status' => 'error', + 'message' => $isDbError ? 'Data Access Error' : $e->getMessage(), + 'ref' => $isDbError ? 'database - ' : 'application - ' . $code . ' - ' . $e->getLine() + ])->setStatusCode($code); + } } 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.*, CONCAT(user_profiles.first_name, " ", user_profiles.last_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(); + 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.*, CONCAT(user_profiles.first_name, " ", user_profiles.last_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)) { // return $this->response->setJSON([ 'status' => 'error','message' => 'No History found'])->setStatusCode(404); throw new \RuntimeException('No History found', 404); } - - return $this->response->setJSON(['status' => 'success','data' => $details])->setStatusCode(200); - } catch (\Throwable $e) { - $code = ($e->getCode() && $e->getCode() >= 100 && $e->getCode() < 600) ? $e->getCode() : 500; - $isDbError = $e instanceof \CodeIgniter\Database\Exceptions\DatabaseException - || $e instanceof \mysqli_sql_exception - || $e instanceof \PDOException; + return $this->response->setJSON(['status' => 'success', 'data' => $details])->setStatusCode(200); + } catch (\Throwable $e) { + $code = ($e->getCode() && $e->getCode() >= 100 && $e->getCode() < 600) ? $e->getCode() : 500; - $context = ['title' => get_class($e),'type' => get_class($e),'code' => $code,'message' => $e->getMessage(),'file' => $e->getFile(),'line' => $e->getLine()]; + $isDbError = $e instanceof \CodeIgniter\Database\Exceptions\DatabaseException + || $e instanceof \mysqli_sql_exception + || $e instanceof \PDOException; - $this->myLogger->logme('error', "Exception: {message} in {file} on line {line}", $context, 0); - - return $this->response->setJSON([ 'status' => 'error', 'message' => $isDbError ? 'Data Access Error' : $e->getMessage(), - 'ref' => $isDbError ? 'database - ' : 'application - ' . $code . ' - ' . $e->getLine() - ])->setStatusCode($code); + $context = ['title' => get_class($e), 'type' => get_class($e), 'code' => $code, 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]; + + $this->myLogger->logme('error', "Exception: {message} in {file} on line {line}", $context, 0); + + return $this->response->setJSON([ + 'status' => 'error', + 'message' => $isDbError ? 'Data Access Error' : $e->getMessage(), + 'ref' => $isDbError ? 'database - ' : 'application - ' . $code . ' - ' . $e->getLine() + ])->setStatusCode($code); } } @@ -454,70 +464,73 @@ class ThzController extends BaseController if (!empty($assign_to)) { // Tickets assigned to a staff return $this->thzMasterModel - ->select('thz_master.*, CONCAT(user_profiles.first_name, " ", user_profiles.last_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(); + ->select('thz_master.*, CONCAT(user_profiles.first_name, " ", user_profiles.last_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.*, CONCAT(user_profiles.first_name, " ", user_profiles.last_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(); + ->select('thz_master.*, CONCAT(user_profiles.first_name, " ", user_profiles.last_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.*, CONCAT(user_profiles.first_name, " ", user_profiles.last_name) as assignee_name') - ->join('user_profiles', 'user_profiles.id = thz_master.assign_to', 'left') - ->orderBy('thz_master.created_at','desc') - ->findAll(); + ->select('thz_master.*, CONCAT(user_profiles.first_name, " ", user_profiles.last_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){ - + 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 + 'assignee_email' => $assignee_email ]; $ticketId = $id; - $result = $this->ticketSaveEmailNotificationBasedOnRole($emailToNotified , $insertOrUpdate , $ticketId , $data); - - return $result; + $result = $this->ticketSaveEmailNotificationBasedOnRole($emailToNotified, $insertOrUpdate, $ticketId, $data, $client_id); + return $result; } - private function ticketConversationSaveNotification($data){ + private function ticketConversationSaveNotification($data) + { - $thz_id = $data['thz_id'] ; // thz is alias of ticket + $thz_id = $data['thz_id']; // thz is alias of ticket $data['subject'] = $this->findSubject($data['thz_id']); - $assignee_id = $this->findAssigneeId($data['thz_id']) ; + $assignee_id = $this->findAssigneeId($data['thz_id']); + + $user_email = $this->findUserEmail($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 ; + $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"; @@ -527,40 +540,43 @@ class ThzController extends BaseController $message = $data['notes'] ?? ''; - $this->sendEmail( $templateName, $data , $to_email , $ticketId , $message); + $this->sendEmail($templateName, $data, $to_email, $ticketId, $message); return true; - } - private function findAssigneeEmail($assignee_id){ + private function findAssigneeEmail($assignee_id) + { $row = $this->userModel->where('id', $assignee_id)->get()->getRow(); $assigneeEmail = $row ? $row->email : null; - return $assigneeEmail ? $assigneeEmail : ""; - + return $assigneeEmail ? $assigneeEmail : ""; } - private function findAssigneeId($ticketId){ + private function findAssigneeId($ticketId) + { $row = $this->thzMasterModel->where('thz_id', $ticketId)->get()->getRow(); $assigneeId = $row ? $row->assign_to : null; - return $assigneeId ? $assigneeId : 0 ; + return $assigneeId ? $assigneeId : 0; } - private function findUserEmail($ticket_id){ + private function findUserEmail($ticket_id) + { $row = $this->thzMasterModel->where('thz_id', $ticket_id)->get()->getRow(); $email = $row ? $row->email : null; - return $email ? $email : " " ; + return $email ? $email : " "; } - private function findSubject($ticketId){ + private function findSubject($ticketId) + { $row = $this->thzMasterModel->where('thz_id', $ticketId)->get()->getRow(); $subject = $row ? $row->subject : null; - return $subject ? $subject : " " ; + return $subject ? $subject : " "; } - private function ticketSaveEmailNotificationBasedOnRole($emailToNotified , $insertOrUpdate , $ticketId , $data){ + private function ticketSaveEmailNotificationBasedOnRole($emailToNotified, $insertOrUpdate, $ticketId, $data, $client_id) + { $user_email = $emailToNotified['user_email'] ?? null; $assignee_email = $emailToNotified['assignee_email'] ?? null; @@ -572,21 +588,20 @@ class ThzController extends BaseController case 'update': - $this->sendEmail( 'ticket_save_update_assignee', $data ,$assignee_email , $ticketId ) ; + $this->sendEmail('ticket_save_update_assignee', $data, $assignee_email, $ticketId); break; - + case 'create': - $this->sendEmail( 'ticket_save_create_assignee', $data ,$assignee_email , $ticketId ) ; - + $this->sendEmail('ticket_save_create_assignee', $data, $assignee_email, $ticketId); + break; - + default: # code... break; } - } if (!empty($user_email)) { @@ -595,47 +610,55 @@ class ThzController extends BaseController case 'update': - if(empty($assignee_email)){ - $data['assigneeAndStatus'] = "Not Yet Assigned"; - $this->sendEmail( 'ticket_save_update_user', $data ,$user_email , $ticketId ) ; - }else{ - $data['assigneeAndStatus'] = "Assigned With {$assignee_email}"; - $this->sendEmail( 'ticket_save_update_user', $data ,$user_email , $ticketId ) ; - } + $data['assigneeAndStatus'] = empty($assignee_email) + ? "Not Yet Assigned" + : "Assigned With {$assignee_email}"; + + $this->sendEmail('ticket_save_update_user', $data, $user_email, $ticketId); + + + break; - break; - case 'create': - if(empty($assignee_email)){ - $this->sendEmail( 'ticket_save_create_user_unassigned', $data ,$user_email , $ticketId ) ; - }else{ - $data['assignee_email'] = $assignee_email ; - $this->sendEmail( 'ticket_save_create_user_assigned', $data , $user_email , $ticketId ) ; + $account_manager_email = $this->cientRmModel->findAccountManagerEmail($client_id); + + $cc = getenv(' G_ticketing_system_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->sendEmail($template, $data, $account_manager_email, $ticketId, $cc); + } + break; - + default: # code... break; } - } return true; - - } - private function updateTicket($data){ + 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) @@ -647,12 +670,13 @@ class ThzController extends BaseController } - private function sendEmail( $templateName, $data , $to_email , $ticketId , $message = ""){ - - $mailTemplate = $this->ticketMailTemplateModel->where('template_name',$templateName )->findAll()[0] ?? []; + private function sendEmail($templateName, $data, $to_email, $ticketId , $cc = "", $message = "", $bcc = "") + { - if(empty($mailTemplate)){ - return ; + $mailTemplate = $this->ticketMailTemplateModel->where('template_name', $templateName)->findAll()[0] ?? []; + + if (empty($mailTemplate)) { + return; } $mailTemplate['subject'] = str_replace('%subject%', $data['subject'] ?? '', $mailTemplate['subject']); @@ -665,39 +689,35 @@ class ThzController extends BaseController $mailTemplate['mail_content'] = str_replace('%assignee_email%', $data['assignee_email'] ?? '', $mailTemplate['mail_content']); - $mailTemplate['mail_content'] = str_replace('%assigneeAndStatus%',$data['assigneeAndStatus'] ?? '' ,$mailTemplate['mail_content']); + $mailTemplate['mail_content'] = str_replace('%assigneeAndStatus%', $data['assigneeAndStatus'] ?? '', $mailTemplate['mail_content']); $from_mail = ""; $to_mail = $to_email; - $cc_string = ""; - $subject = $mailTemplate['subject'] ; - $message = $mailTemplate['mail_content'] ; + $cc_string = $cc; + $subject = $mailTemplate['subject']; + $message = $mailTemplate['mail_content']; $attachments = ""; $reply_to = ""; - $bcc_string = ""; - - $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]); + $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]); } 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); + $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); } - - } - diff --git a/app/Models/ClientRMModel.php b/app/Models/ClientRMModel.php index 799649e5..ee02782c 100755 --- a/app/Models/ClientRMModel.php +++ b/app/Models/ClientRMModel.php @@ -21,22 +21,38 @@ class ClientRMModel extends Model public function checkExistenceOfClientRelation($id, $user_id, $level) { $query = $this->where('client_id', $id) - ->where('user_id', $user_id) - ->where('level', $level) - ->countAllResults(); + ->where('user_id', $user_id) + ->where('level', $level) + ->countAllResults(); return ($query > 0) ? true : false; } - public function getAllClientRM(){ + public function getAllClientRM() + { return $this->db->table('client_rm') - ->select('client_rm.client_id, client_rm.level,') - ->select('user_profiles.first_name as account_manager') - ->join('user_profiles', 'user_profiles.id = client_rm.user_id') - ->where('level', 3) - ->get() - ->getResult(); - + ->select('client_rm.client_id, client_rm.level,') + ->select('user_profiles.first_name as account_manager') + ->join('user_profiles', 'user_profiles.id = client_rm.user_id') + ->where('level', 3) + ->get() + ->getResult(); } + public function findAccountManagerEmail($client_id) + { + $builder = $this->db->table('client_rm'); + $builder->select('user_profiles.email'); + $builder->join('user_profiles', 'user_profiles.id = client_rm.user_id'); + $builder->where('client_rm.client_id', $client_id); + $builder->where('client_rm.level', 3); // Assuming level 3 is for account managers + $result = $builder->get()->getResultArray(); + + if (count($result) > 0) { + // Extract just the email values into a simple string array + return array_column($result, 'email'); + } else { + return null; // or [] + } + } } diff --git a/app/Views/client_basic_info.php b/app/Views/client_basic_info.php index 0cf659a3..f43c65ee 100755 --- a/app/Views/client_basic_info.php +++ b/app/Views/client_basic_info.php @@ -64,33 +64,30 @@ input:checked + .slider:before {