thzMasterModel = new ThzMasterModel(); $this->thzMasterNotesModel = new ThzMasterNotesModel(); $this->thzControllerCleaner = new ThzControllerCleaner(); $this->userModel = new UserModel(); $this->clientPolicyModel = new ClientPolicyModel(); $this->ticketMailTemplateModel = new TicketMailTemplateModel(); } public function index() { // } public function ticketSave(){ $data = $this->request->getPost(); if (!empty($data['thz_id'])) { $text = "update"; $result = $this->updateTicket($data); $updateID = $data['thz_id']; } else { $text = "create"; $insertID = $this->insertTicket($data); $result = true; } $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..!!' ])->setStatusCode($result ? 200 : 400); } public function ticketList() { $returnType = strtolower($this->request->getGet('return_type') ?? 'api'); $data = $this->request->getGet(); $tickets = $this->fetchTicketsBasedOnrole($data); if ($returnType === 'api') { if (empty($tickets)) { return $this->response->setJSON([ 'status' => 'error','message' => 'No tickets found'])->setStatusCode(404); } }else{ $data['page_name'] = "Ticket List"; $data['ticket_data'] = $tickets; $data['assignee'] = $this->userModel->where('is_active', 1)->whereIn('role', ['1', '5'])->findAll(); // enga 5-"head" and 1-"admin" role person thaan assignee.. $data['client_list'] = $this->clientModel->getCreatedByUserName(); return $this->loadLayout('thz_list', $data); } return $this->response->setJSON([ 'status' => 'success', 'data' => $tickets, ])->setStatusCode(200); } public function ticketConversationSave(){ $data = $this->request->getPost(); $result = $this->thzMasterNotesModel->insert($data); if(empty($result)){ return $this->response->setJSON((['status' => 'error', 'message' => 'No tickets found']))->setStatusCode(404); } return $this->response->setJSON([ 'status' => $result ? 'success' : 'error', 'message' => $result ? "Ticket Notes created successfully" : "Unable to create ticket notes. Please try again." ])->setStatusCode($result ? 200 : 400); } public function ticketAutoFetchDetails(){ $data = $this->request->getPost(); $client_id = $data['client_id']; $mobile = $data['mobile']; if($client_id && $mobile){ $details = $this->thzMasterModel->ticketAutoFetchDetails($client_id,$mobile); if(empty($details)){ return $this->response->setJSON((['status' => 'error', 'message' => 'No Data found']))->setStatusCode(400); }else{ $policyIds = array_column($details, 'policy_id'); $policyIds = array_filter($policyIds); $clientPolicy = $this->clientPolicyModel->whereIn('policy_id', $policyIds)->where('is_active', 1)->get()->getResultArray(); $result['client'] =[]; } }else{ return $this->response->setJSON((['status' => 'error', 'message' => 'Client and Mobile Not found']))->setStatusCode(400); } return $this->response->setJSON((['status' => 'success', 'data' => $result]))->setStatusCode(200); } public function ticketConversationList(){ $data = $this->request->getGet(); $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(); if(!empty($result['master'])){ $notes = $this->thzMasterNotesModel->ticketConversationList($thz_id); $result['notes'] = !empty($notes) ? $notes : []; }else{ return $this->response->setJSON((['status' => 'error', 'message' => 'No tickets found']))->setStatusCode(400); } }else{ return $this->response->setJSON((['status' => 'error', 'message' => 'No tickets found']))->setStatusCode(404); } return $this->response->setJSON((['status' => 'success', 'data' => $result]))->setStatusCode(200); } public function ticketConversationSaveNotification($data){ } public function ticketAssigning(){ $returnType = strtolower($this->request->getGet('return_type') ?? 'api'); $data = $this->request->getVar(); $assignee_id = $data['assignee_id']; $thz_id = $data['thz_id']; $result = $this->thzMasterModel->set('assign_to',$assignee_id)->where('thz_id',$thz_id)->update(); if($returnType === 'api'){ if(empty($result)){ return $this->response->setJSON((['status' => 'error', 'message' => 'No tickets found'])); } return $this->response->setJSON((['status' => 'success', 'data' => $result])); }else{ } } /************************************************** 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) ->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) ->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') ->findAll(); } private function ticketSaveNotification( $data , $insertOrUpdate , $id){ $user_email = $data['email'] ?? null; $assignee_id = $data['assign_to'] ?? null; $assignee_email = $this->findAssigneeEmail($assignee_id); $emailToNotified = [ 'user_email' => $user_email, 'assignee_email' => $assignee_email ]; $ticketId = $id; $result = $this->ticketSaveEmailNotificationBasedOnRole($emailToNotified , $insertOrUpdate , $ticketId , $data); return $result; } private function findAssigneeEmail($assignee_id){ $result = $this->userModel->where('id',$assignee_id)->find()->getResultArray(); $assigneeEmail = $result[0]['email']; return $assigneeEmail; } private function ticketSaveEmailNotificationBasedOnRole($emailToNotified , $insertOrUpdate , $ticketId , $data){ $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': $this->sendEmail( 'ticket_save_update_user', $data ,$user_email , $ticketId ) ; break; case 'create': if(empty($assignee_email)){ $this->sendEmail( 'ticket_save_create_user_unassigned', $data ,$user_email , $ticketId ) ; }else{ $this->sendEmail( 'ticket_save_create_user_assigned', $data , $user_email , $ticketId ) ; } 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){ $mail = $this->ticketMailTemplateModel->where('template_name',$templateName )->findAll(); $mail['subject'] = str_replace('%subject%', $data['subject'] ?? '', $mail['subject']); $mail['mail_content'] = str_replace('%subject%', $data['subject'] ?? '', $mail['mail_content']); $mail['mail_content'] = str_replace('%ticketId%', $ticketId ?? '', $mail['mail_content']); $from_mail = ""; $to_mail = $to_email; $cc_string = ""; $subject = $mail['subject'] ; $message = $mail['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]); } }