430 lines
14 KiB
PHP
430 lines
14 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Controllers\BaseController;
|
|
use App\Helpers\MailHelper;
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
|
|
use App\Models\ThzMasterModel;
|
|
use App\Models\ThzMasterNotesModel;
|
|
use App\ControllerCleaners\ThzControllerCleaner;
|
|
use App\Models\UserModel;
|
|
use App\Models\ClientPolicyModel;
|
|
use App\Models\ClientModel;
|
|
use App\Models\TicketMailTemplateModel;
|
|
|
|
|
|
class ThzController extends BaseController
|
|
{
|
|
|
|
protected $thzMasterModel;
|
|
protected $thzMasterNotesModel;
|
|
|
|
protected $thzControllerCleaner;
|
|
|
|
protected $userModel;
|
|
protected $clientPolicyModel;
|
|
protected $clientModel;
|
|
|
|
protected $ticketMailTemplateModel;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->thzMasterModel = new ThzMasterModel();
|
|
$this->thzMasterNotesModel = new ThzMasterNotesModel();
|
|
$this->thzControllerCleaner = new ThzControllerCleaner();
|
|
$this->userModel = new UserModel();
|
|
$this->clientPolicyModel = new ClientPolicyModel();
|
|
$this->clientModel = new ClientModel();
|
|
$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);
|
|
}
|
|
|
|
$emailNotificationResult = $this->ticketConversationSaveNotification($data);
|
|
|
|
|
|
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..!!'
|
|
])->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_details'] = $details;
|
|
$result['policy_details'] = empty($clientPolicy) ? [] : $clientPolicy ;
|
|
|
|
}
|
|
}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 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 ticketConversationSaveNotification($data){
|
|
|
|
$thz_id = $data['thz_id'] ; // thz is alias of ticket
|
|
|
|
$assignee_id = $this->findAssigneeId($data['thz_id']) ;
|
|
|
|
$user_email = $data['email'] ?? '';
|
|
|
|
$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->userModel->where('thz_id', $ticketId)->get()->getRow();
|
|
$assigneeId = $row ? $row->assign_to : null;
|
|
return $assigneeId ? $assigneeId : 0 ;
|
|
}
|
|
|
|
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 , $message = ""){
|
|
|
|
$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']);
|
|
|
|
$from_mail = "";
|
|
$to_mail = $to_email;
|
|
$cc_string = "";
|
|
$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]);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|