diff --git a/app/Controllers/ClientController.php b/app/Controllers/ClientController.php index 74daba0b..164bee10 100755 --- a/app/Controllers/ClientController.php +++ b/app/Controllers/ClientController.php @@ -1538,7 +1538,7 @@ class ClientController extends AdminController - + // Save Rack Rate function public function createClientPolicyPremium() { diff --git a/app/Controllers/PolicyTransactionController.php b/app/Controllers/PolicyTransactionController.php index 09274d01..8b2a80bb 100644 --- a/app/Controllers/PolicyTransactionController.php +++ b/app/Controllers/PolicyTransactionController.php @@ -1193,9 +1193,6 @@ class PolicyTransactionController extends BaseController // $data['is_cd_reduce_from_bds'] = 1; // } - $data['is_cd_reduce_from_bds'] = ($data['policy_type_id'] > 7 && $data['client_type'] == 1) ? 1 : 0; - - if (empty($data['data_received_date'])) { $data['data_received_date'] = null; } else { @@ -1255,6 +1252,8 @@ class PolicyTransactionController extends BaseController $client_branch_id = $issue_type['client_branch_id']; } + $data['is_cd_reduce_from_bds'] = (isset($issue_type['policy_type_id']) && $issue_type['policy_type_id'] > 7 && $data['client_type'] == 1) ? 1 : 0; + // if($data['bro_payable_by'] == ""){ // $data['bro_payable_by'] = $issue_type['bro_payable_by']; // } @@ -1324,13 +1323,16 @@ class PolicyTransactionController extends BaseController } private function updateEndorsementTransaction($id, $data) - { + { + $old_endorse_data = $this->policyTransactionModel->where('id', $id)->where('is_active', 1)->first(); $update = $this->policyTransactionModel->where('id', $id)->set($data)->update(); if ($update) { $this->insertTransactionStatus($id, $data, 1); - $this->handleCompletedStatus($data, $id); + if($old_endorse_data['status'] != "completed"){ + $this->handleCompletedStatus($data, $id); + } $this->insertOrUpdateCoShareDetails($data, $id); @@ -1344,11 +1346,10 @@ class PolicyTransactionController extends BaseController { if ($data['client_type'] == 1 && $data['status'] == 'completed' && $data['is_cd_reduce_from_bds'] == 1) { - $tolamt = $data['total'][0] ?? 0; + $tolamt = (int) $data['total'][0] ?? 0; $description = 'The following amount of Rs. ' . round($tolamt, 2) . '/- has been' . ($data['action_type'] == 'deletion' ? ' Credit ' : ' Debit ') . 'from the policy transaction (BDS)'; - $cd_tranction_data = [ 'amount' => $tolamt, 'sub_type_id' => $data['action_type'] == 'deletion' ? 3 : 4, diff --git a/app/Controllers/ThzController.php b/app/Controllers/ThzController.php index 0dea90c1..79924a5c 100644 --- a/app/Controllers/ThzController.php +++ b/app/Controllers/ThzController.php @@ -1,723 +1,744 @@ -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); - - 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; - $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.", - '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; - - $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 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(); - } - - $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); - } - } 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; - - $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() - { - - try { - $data = $this->request->getPost(); - - $data['notes_type'] = $data['notes_type'] ?? 'External'; - - $result = $this->thzMasterNotesModel->insert($data); - - if (empty($result)) { - // return $this->response->setJSON((['status' => 'error', 'message' => 'No tickets found']))->setStatusCode(404); - throw new \RuntimeException('No tickets found', 404); - } - - if ($data['notes_type'] == "External") { - $emailNotificationResult = $this->ticketConversationSaveNotification($data); - } else { - $emailNotificationResult = 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) { - $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 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)) { - 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) { - $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 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.*, 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; - - $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); - } - } 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.*, 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') { - $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' => '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) { - $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 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(); - - - - 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; - - $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); - } - } - - - /************************************************** 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.*, 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(); - } - - // 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(); - } - - - - 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, $message); - - 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'); - - $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) - { - - $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 = "", $message = "", $bcc = "") - { - - $mailTemplate = $this->ticketMailTemplateModel->where('template_name', $templateName)->findAll()[0] ?? []; - - if (empty($mailTemplate)) { - return; - } - - $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%', $message ?? '', $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]); - } - - - 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); - } -} +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); + + 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; + $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) { + $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 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(), [2, 3, 4])) { + $data['assign_to'] = $data['assign_to'] ?? get_session_userid(); + } + + $tickets = $this->fetchTicketsBasedOnrole($data); + + + if ($returnType === 'api') { + if (empty($tickets)) { + // return $this->response->setJSON([ 'status' => 'error','message' => 'No tickets found'])->setStatusCode(404); + $this->myLogger->logme('error', 'empty tickets in ticket list: ' . json_encode($tickets)); + throw new \RuntimeException('No tickets found', 400); + } + } else { + $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() + { + + 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); + } + + if ($data['notes_type'] == "External") { + $emailNotificationResult = $this->ticketConversationSaveNotification($data); + } else { + $emailNotificationResult = 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) { + $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 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) { + $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 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.*, 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 { + $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) { + $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); + } + } 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.*, 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') { + $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) { + $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 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(); + + + + 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) { + $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); + } + } + + + /************************************************** 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.*, 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(); + } + + // 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(); + } + + + + 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, $message); + + 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 = "", $message = "", $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%', $message ?? '', $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); + } +} diff --git a/app/Controllers/ThzController_old.php b/app/Controllers/ThzController_old.php new file mode 100644 index 00000000..0dea90c1 --- /dev/null +++ b/app/Controllers/ThzController_old.php @@ -0,0 +1,723 @@ +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); + + 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; + $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.", + '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; + + $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 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(); + } + + $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); + } + } 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; + + $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() + { + + try { + $data = $this->request->getPost(); + + $data['notes_type'] = $data['notes_type'] ?? 'External'; + + $result = $this->thzMasterNotesModel->insert($data); + + if (empty($result)) { + // return $this->response->setJSON((['status' => 'error', 'message' => 'No tickets found']))->setStatusCode(404); + throw new \RuntimeException('No tickets found', 404); + } + + if ($data['notes_type'] == "External") { + $emailNotificationResult = $this->ticketConversationSaveNotification($data); + } else { + $emailNotificationResult = 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) { + $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 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)) { + 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) { + $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 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.*, 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; + + $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); + } + } 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.*, 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') { + $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' => '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) { + $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 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(); + + + + 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; + + $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); + } + } + + + /************************************************** 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.*, 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(); + } + + // 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(); + } + + + + 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, $message); + + 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'); + + $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) + { + + $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 = "", $message = "", $bcc = "") + { + + $mailTemplate = $this->ticketMailTemplateModel->where('template_name', $templateName)->findAll()[0] ?? []; + + if (empty($mailTemplate)) { + return; + } + + $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%', $message ?? '', $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]); + } + + + 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); + } +} diff --git a/app/Views/leads_form_handler.php b/app/Views/leads_form_handler.php index 484a5f2f..2367957c 100644 --- a/app/Views/leads_form_handler.php +++ b/app/Views/leads_form_handler.php @@ -882,6 +882,7 @@ if (isset($selected_lead_type)) { if (data.salse_person_id) { selecSalsePerson(data.salse_person_id); } + } catch (error) { console.error('Error in dynamicLeadsDataForEdit function:', error); $('.loader').fadeOut(); @@ -1147,6 +1148,22 @@ if (isset($selected_lead_type)) { dateFormat: "d-m-Y" }); }); + + setTimeout(function () { + let selectedOption = $('#contact_person_summary option').filter(function () { + return $(this).text().trim() === (data.contact_person_name || '').trim(); + }); + + if (selectedOption.length) { + selectedOption.prop('selected', true); + console.log("Selected option:", selectedOption.text().trim(), "Value:", selectedOption.val()); + } else { + console.log("No match found for:", data.contact_person_name); + } + + $('#contact_person_summary').trigger('change'); + }, 2000); + }); }); } diff --git a/app/Views/policy_grid.php b/app/Views/policy_grid.php index 9f4e8436..a0dcfa7b 100755 --- a/app/Views/policy_grid.php +++ b/app/Views/policy_grid.php @@ -3464,6 +3464,11 @@ function appendNewTab(tabNameData = null, data = null) } } + $('.nav-link').click(function(){ + $('.nav-link').removeClass('active-tab'); + $(this).addClass('active-tab'); + }); + } function appendGridData(data, policy_grid_id_value = null, unique_id = null) diff --git a/app/Views/policy_transaction_endorsement_form.php b/app/Views/policy_transaction_endorsement_form.php index bb1b877d..b15d5cc0 100644 --- a/app/Views/policy_transaction_endorsement_form.php +++ b/app/Views/policy_transaction_endorsement_form.php @@ -363,11 +363,11 @@