nhance/app/Controllers/TicketController.php
2025-01-30 10:29:44 +05:30

614 lines
24 KiB
PHP

<?php
namespace App\Controllers;
use App\Controllers\BaseController;
use CodeIgniter\HTTP\ResponseInterface;
use App\Models\TicketMasterModel;
use App\Models\TicketClaimStatusModel;
use App\Models\ClientModel;
use App\Models\TicketHistoryModel;
use App\Models\TicketMailTemplateModel;
use App\Models\TicketMessageModel;
use App\Models\TicketNoteModel;
use App\Models\UserModel;
use DOMDocument;
use Psr\Log\LoggerInterface;
use Kint\Kint;
use App\Helpers\MailHelper;
use CodeIgniter\API\ResponseTrait;
class TicketController extends BaseController
{
use ResponseTrait;
protected $myLogger;
protected $ticketType;
protected $priorityType;
protected $relationshipType;
protected $modeOFIntimate;
protected $claimType;
protected $claimStatus;
protected $placeHolders;
protected $clientModel;
protected $ticketMasterModel;
protected $ticketHistoryModel;
protected $ticketMailTemplateModel;
protected $ticketMessageModel;
protected $ticketNoteModel;
protected $triggerType;
protected $userModel;
public function __construct()
{
$this->myLogger = \Config\Services::mylogger();
$this->ticketMasterModel = new TicketMasterModel();
$this->ticketType = [1 => "Claim-GMC", 2 => "Claim-GPA", 3 => "EDLI", 4 => "GTLI"];
$this->priorityType = [
1 => "Low",
2 => "Medium",
3 => "High",
4 => "Urgent",
5 => "Emergency",
6 => "Critical"
];
$this->relationshipType = [
"self" => "Self",
"spouse" => "Spouse",
"son" => "Son",
"daughter" => "Daughter",
"father" => "Father",
"mother" => "Mother",
"father-in-law" => "Father-in-Law",
"mother-in-law" => "Mother-in-Law"
];
$this->modeOFIntimate = [
1 => "In Person",
2 => "Courier",
3 => "Online Mode - Email",
4 => "Online Mode - To TPA"
];
$this->claimType = [
1 => [
1 => "Main",
2 => "OPD",
3 => "Pre ReOpen",
4 => "Post ReOpen",
],
2 => [
1 => "TTD",
2 => "PTD",
3 => "PPD",
4 => "Death",
]
];
$this->triggerType = [
1 => "Trigger 1",
2 => "Trigger 2",
3 => "Trigger 3",
4 => "Trigger 4",
5 => "Trigger 5",
6 => "Trigger 6",
7 => "Trigger 7"
];
$this->placeHolders = [
'((ACM))' => 'acm',
'((ACM_CONTACT))' => 'acm_mobile',
'((EMPLOYEE_NAME)' => 'emp_name',
'((EMPLOYEE_ID))' => 'emp_code',
'((CORPORATE_NAME))' => 'client_name',
'((INSURED_NAME]' => 'insured_emp_name',
'((CLAIM_NO))' => 'claim_no',
];
$this->ticketMasterModel = new TicketMasterModel();
$this->claimStatus = new TicketClaimStatusModel();
$this->clientModel = new ClientModel();
$this->ticketHistoryModel = new TicketHistoryModel();
$this->ticketMailTemplateModel = new TicketMailTemplateModel();
$this->ticketNoteModel = new TicketNoteModel();
$this->ticketMessageModel = new TicketMessageModel();
$this->userModel = new UserModel();
}
public function ticketList()
{
if ($this->request->is('get')) {
$data['page_name'] = "Claims";
$data['ticket_type'] = $this->ticketType;
$data['claim_status'] = $this->claimStatus->select('id,ticket_type,claim_status')->where('is_active',1)->findAll();
$data['client_list'] = $this->clientModel->select('id,client_name')->where('is_active',1)->findAll();
$data['ticket_data'] = $this->ticketSearch(1);
return $this->loadLayout('ticket_search', $data);
} else{
$data['ticket_data'] = $this->ticketSearch();
$html = view('ticket_list', $data);
return $this->respond(['status' => true, 'html' => $html], 200);
}
}
public function ticketSearch($action = null)
{
//action 1 get last 100 rows, action 2 filter and get all rows related to that
if ($action == 1){
$data = $this->ticketMasterModel
->select('ticket_master.id, ticket_claim_status.claim_status as status,
ticket_master.claim_number as claim_no,ticket_master.tpa_id,ticket_master.emp_name,
insurers.name as insurer_name, clients.client_name,
DATE_FORMAT(ticket_master.created_at, "%d-%m-%Y") as tat')
->join('insurers', 'insurers.id = ticket_master.insurer_id and insurers.is_active = 1', 'left')
->join('clients', 'clients.id = ticket_master.client_id and clients.is_active = 1', 'left')
->join('ticket_claim_status', 'ticket_claim_status.id = ticket_master.claim_status_id and ticket_claim_status.is_active = 1', 'left')
->where('ticket_master.is_active', 1)
->orderBy('ticket_master.id', 'DESC')
->limit(100)
->findAll();
return $data;
}else{
$search_data = $this->request->getPost();
// print_r($search_data);
$where = [];
foreach ($search_data as $search_objects => $key) {
if ($key != null && $key != '' && $key != 0) {
$where[$search_objects] = $key;
}
}
// print_rr($where);
$data = $this->ticketMasterModel
->select('ticket_master.id, ticket_claim_status.claim_status as status,
ticket_master.claim_number as claim_no,ticket_master.tpa_id,ticket_master.emp_name,
insurers.name as insurer_name, clients.client_name,
DATE_FORMAT(ticket_master.created_at, "%d-%m-%Y") as tat')
->join('insurers', 'insurers.id = ticket_master.insurer_id and insurers.is_active = 1', 'left')
->join('clients', 'clients.id = ticket_master.client_id and clients.is_active = 1', 'left')
->join('ticket_claim_status', 'ticket_claim_status.id = ticket_master.claim_status_id and ticket_claim_status.is_active = 1', 'left')
->where('ticket_master.is_active', 1)
->where($where)
->findAll();
// print_rr($data);
// log_message('error',' Claims Master Data '.json_encode($data));die();
// print_rr($data);die();
return $data;
}
}
public function ticket_form($ticket_type)
{
$data = $this->ticket_form_data($ticket_type);
return $this->loadLayout('ticket_form_handler', $data);
}
public function ticket_form_data($ticket_type)
{
$data['ticket_form'] = 1;
$data['ticket_type'] = $this->ticketType;
$data['priorityType'] = $this->priorityType;
$data['relationshipType'] = $this->relationshipType;
$data['modeOFIntimate'] = $this->modeOFIntimate;
$data['acms'] = db_connect()->table('user_profiles')
->select('user_profiles.id, user_profiles.first_name')
->where('user_profiles.is_active', 1)
->where('user_profiles.role', 3)
->get()
->getResultArray();
$data['claim_status'] = $this->claimStatus
->select('ticket_claim_status.*')
->where('ticket_claim_status.is_active', 1)
->where('ticket_claim_status.ticket_type', $ticket_type)
->get()
->getResultArray();
if($ticket_type == 1){
$data['claim_type'] = $this->claimType[1];
}else{
$data['claim_type'] = $this->claimType[2];
if($ticket_type == 2){
$data['ticket_form'] = 'GPA';
}else if($ticket_type = 3){
$data['ticket_form'] == 'EDLI';
}else if($ticket_type = 4){
$data['ticket_form'] = 'GTLI';
}
}
return $data;
}
public function view_ticket($ticket_id)
{
$ticket_data = $this->ticketMasterModel->where('id', $ticket_id)->where('is_active', 1)->first();
$ticket_data = $this->formatDateForClaim($ticket_data, 'd/m/Y');
// dd($ticket_data);
$data = $this->ticket_form_data($ticket_data['ticket_type_id']);
$data['ticket_data'] = $ticket_data;
$data['reply_data'] = $this->ticketMasterModel
->join('ticket_claim_status','ticket_master.claim_status_id = ticket_claim_status.id and ticket_claim_status.is_active = 1')
->join('ticket_mail_template','ticket_claim_status.trigger_type = ticket_mail_template.trigger_type and ticket_claim_status.ticket_type = ticket_mail_template.ticket_type and ticket_mail_template.is_active = 1')
->where('ticket_master.id',$ticket_id)
->where('ticket_master.is_active',1)
->first();
$data['placeHolders'] = $this->placeHolders;
$data['message_data'] = $this->getTicketMessage($ticket_id);
$data['ticket_history'] = $this->ticketHistory($ticket_id);
// dd($data);
return $this->loadLayout('ticket_edit_onbording', $data);
}
public function formatDateForClaim($ticket_data, $out_put_format = 'Y-m-d')
{
if(empty($ticket_data['doa'])){
$ticket_data['doa'] = null;
}else{
$ticket_data['doa'] = change_date_format($ticket_data['doa'], null, $out_put_format);
}
if(empty($ticket_data['dod'])){
$ticket_data['dod'] = null;
}else{
$ticket_data['dod'] = change_date_format($ticket_data['dod'], null, $out_put_format);
}
if(empty($ticket_data['dob'])){
$ticket_data['dob'] = null;
}else{
$ticket_data['dob'] = change_date_format($ticket_data['dob'], null, $out_put_format);
}
if(empty($ticket_data['date_of_join'])){
$ticket_data['date_of_join'] = null;
}else{
$ticket_data['date_of_join'] = change_date_format($ticket_data['date_of_join'], null, $out_put_format);
}
if(empty($ticket_data['date_of_incep'])){
$ticket_data['date_of_incep'] = null;
}else{
$ticket_data['date_of_incep'] = change_date_format($ticket_data['date_of_incep'], null, $out_put_format);
}
if(empty($ticket_data['date_of_accident'])){
$ticket_data['date_of_accident'] = null;
}else{
$ticket_data['date_of_accident'] = change_date_format($ticket_data['date_of_accident'], null, $out_put_format);
}
if(empty($ticket_data['date_of_death'])){
$ticket_data['date_of_death'] = null;
}else{
$ticket_data['date_of_death'] = change_date_format($ticket_data['date_of_death'], null, $out_put_format);
}
if(empty($ticket_data['date_of_intimat'])){
$ticket_data['date_of_intimat'] = null;
}else{
$ticket_data['date_of_intimat'] = change_date_format($ticket_data['date_of_intimat'], null, $out_put_format);
}
return $ticket_data;
}
public function createTicket()
{
$ticket_data = $this->request->getPost();
$ticket_data = $this->formatDateForClaim($ticket_data);
// print_rr($ticket_data); die;
if($ticket_data){
$return_value = $this->ticketMasterModel->insert($ticket_data);
if($return_value){
return $this->respond(['status' => true, 'code' => 200, 'data' => $ticket_data, "message" => "Claim created successfully"], 200);
} else {
return $this->respond(['status' => false, 'code' => 404, 'message' => 'Failed to create claim'], 200);
}
}else{
return $this->respond(['status' => false, 'code' => 404, 'message' => 'claim data not found'], 200);
}
}
public function updateTicket()
{
$ticket_id = $this->request->getPost('ticket_master_id');
$ticket_data = $this->request->getPost();
$ticket_data = $this->formatDateForClaim($ticket_data);
// print_rr($ticket_data); die;
$old_ticket_data = $this->ticketMasterModel->where('id', $ticket_id)->where('is_active', 1)->first();
if($ticket_data){
$return_value = $this->ticketMasterModel->where('id', $ticket_id)->set($ticket_data)->update();
if($return_value){
return $this->respond(['status' => true, 'code' => 200, 'data' => $ticket_data, "message" => "Claim updated successfully"], 200);
} else {
return $this->respond(['status' => false, 'code' => 404, 'message' => 'Failed to update claim'], 200);
}
}else{
return $this->respond(['status' => false, 'code' => 404, 'message' => 'claim data not found'], 200);
}
}
public function mailTemplate()
{
$data = [];
$data['trigger_type'] = $this->triggerType;
$data['policy_type'] = $this->ticketType;
$data['placeHolders'] = $this->placeHolders;
$data['ticket_data'] = $this->ticketMailTemplateModel->where('is_active',1)->findAll();
$data['ticket_type'] = $this->ticketType;
$data['trigger_type'] = $this->triggerType;
return $this->loadLayout('ticket_mail_template', $data);
}
public function crudTemplate($action){
//action 1 is create. action 2 is edit and action 3 is delete
if ($action == 1){
$received_data = $this->request->getPost();
if (isset($received_data['id']) && $received_data['id'] != '' ){
$status = $this->ticketMailTemplateModel->save($received_data);
if ($status){
return $this->respond(['status'=> true],200);
}else{
return $this->respond(['status' => false],200);
}
}else{
$status = $this->ticketMailTemplateModel->save($received_data);
if ($status){
return $this->respond(['status'=> true],200);
}else{
return $this->respond(['status' => false],200);
}
}
}
elseif ($action == 2){
$table_id = (int)$this->request->getPost('id');
$data = $this->ticketMailTemplateModel->where('id',$table_id)->where('is_active',1)->first();
if ($data){
return $this->respond(['status' => true,'data'=> $data],200);
}else{
return $this->respond(['status' => false],404);
}
}
elseif ($action == 3){
$table_id = (int)$this->request->getPost('id');
$sql = "update ticket_mail_template set is_active = 0 where id = $table_id ";
$status = $this->ticketMailTemplateModel->query($sql);
if($status){
return $this->respond(['status'=> true],200);
}else{
return $this->respond(['status' => false],404);
}
}
}
public function crudNote($action){
// action = 1 is read, action 2 is insert/update
if ($action == 1){
$ticket_master_id = $this->request->getPost('id');
if (isset($ticket_master_id) && $ticket_master_id != ''){
$data = $this->ticketNoteModel->where('is_active',1)->where('ticket_id',$ticket_master_id)->first();
if ($data){
return $this->respond(['status' => true, 'data' => $data],200);
}else{
return $this->respond(['status' => false],404);
}
}
}elseif ($action == 2){
$data = $this->request->getPost();
// if (isset($data['id']) && $data['id'] != ''){
$status = $this->ticketNoteModel->save($data);
// }
if ($status){
return $this->respond(['status'=>true],200);
}else{
return $this->respond(['status'=>false],404);
}
}
}
public function saveReply(){
$received_data = $this->request->getPost();
$received_data['sender'] = 'staff';
$status = $this->ticketMessageModel->save($received_data);
if ($status){
return $this->respond(['status' => true,'code'=>200],200);
}else{
return $this->respond(['status' => false,'code' => 404],404);
}
}
public function getTicketMessage($ticket_master_id){
// log_message('error','Function called');die();
// $ticket_master_id = $this->request->getPost('id');
$user_id = get_session_userid();
$logged_user = $this->userModel->select('first_name,last_name,profile')->where('id',$user_id)->first();
$dataToSend = [];
$dataToSend['user_name'] = $logged_user['first_name'].' '.$logged_user['last_name'];
$dataToSend['messages'] = $this->ticketMessageModel->where('is_active',1)
->where('ticket_id',$ticket_master_id)->orderBy('created_at', 'DESC')
->findAll();
foreach ($dataToSend['messages'] as $message){
$mail_content_converted = $this->convertHtmlToText($message['mail_content']);
$message['mail_content'] = $mail_content_converted;
}
$dataToSend['emp_name'] = $this->ticketMasterModel->select('emp_name')
->where('id',$ticket_master_id)->where('is_active',1)
->first()['emp_name'];
// dd($dataToSend);
if($dataToSend){
return $dataToSend;
}else{
$data = [];
return $data;
}
}
public function convertHtmlToText($html) {
$dom = new DOMDocument();
@$dom->loadHTML($html);
return strip_tags($dom->saveHTML());
}
public function sendTringger($ticket_id = 13)
{
$ticket_data = $this->ticketMasterModel->where('id', $ticket_id)->where('is_active', 1)->first();
$template_data = $this->claimStatus
->select('ticket_mail_template.*')
->join('ticket_mail_template', 'ticket_mail_template.trigger_type = ticket_claim_status.trigger_type')
->where('ticket_mail_template.is_ative', 1)
->where('ticket_claim_status.is_ative', 1)
->where('id', $ticket_data['claim_status_id'])
->first();
if(!empty($template_data)){
$res = MailHelper::send_email(['mail' => $ticket_data['emp_email'], 'subject' => 'Mail Via Attachment Testing URL', 'message' => "",'attachments' => []]);
}
}
public function placeholderReplace($template_data, $ticket_data)
{
$placeholders = $this->placeHolders;
}
public function ticketHistory($ticket_id){
$sql = "SELECT
th.id,
th.field_name,
th.display_name,
th.old_value,
th.new_value,
th.created_at,
CONCAT(creator.first_name, ' ', creator.last_name) as modified_by,
-- Claim Status
old_status.claim_status as old_status_value,
new_status.claim_status as new_status_value,
-- Account Manager
CONCAT(old_account_manager.first_name, ' ', old_account_manager.last_name) as old_account_manager,
CONCAT(new_account_manager.first_name, ' ', new_account_manager.last_name) as new_account_manager,
-- Insured Employee ID Change
old_insured_emp.name as old_insured_id_name,
new_insured_emp.name as new_insured_id_name
FROM
ticket_history th
-- Join for the user who made the change
LEFT JOIN user_profiles creator
ON th.created_by = creator.id
-- Status value lookups
LEFT JOIN ticket_claim_status old_status
ON th.field_name = 'claim_status_id'
AND th.old_value = old_status.id
AND old_status.is_active = 1
LEFT JOIN ticket_claim_status new_status
ON th.field_name = 'claim_status_id'
AND th.new_value = new_status.id
LEFT JOIN user_profiles as old_account_manager
ON th.field_name = 'acm_id'
AND th.old_value = old_account_manager.id
LEFT JOIN user_profiles as new_account_manager
ON th.field_name = 'acm_id'
AND th.new_value = new_account_manager.id
LEFT JOIN employees as old_insured_emp
ON th.field_name = 'insured_emp_id'
AND th.old_value = old_insured_emp.id
LEFT JOIN employees as new_insured_emp
ON th.field_name = 'insured_emp_id'
AND th.new_value = new_insured_emp.id
WHERE
th.ticket_id = $ticket_id
AND th.is_active = 1
ORDER BY
th.created_at DESC";
$data = $this->ticketHistoryModel->query($sql)->getResultArray();
$priorityType = $this->priorityType;
$relationshipType = $this->relationshipType;
$modeOFIntimate = $this->modeOFIntimate;
$claim_type = $this->claimType;
foreach ($data as &$row){
if ($row['field_name'] == 'claim_status_id'){
$new_old_value = isset($row['old_status_value'])?$row['old_status_value']:'-';
$new_new_value = $row['new_status_value'];
$row['old_value'] = $new_old_value;
$row['new_value'] = $new_new_value;
}elseif ($row['field_name'] == 'acm_id'){
$new_old_value = isset($row['old_account_manager'])?$row['old_account_manager']:'-';
$new_new_value = $row['new_account_manager'];
$row['old_value'] = $new_old_value;
$row['new_value'] = $new_new_value;
}elseif ($row['field_name'] == 'insured_emp_id'){
$new_old_value = isset($row['old_insured_id_name'])?$row['old_insured_id_name']:'-';
$new_new_value = $row['new_insured_id_name'];
$row['old_value'] = $new_old_value;
$row['new_value'] = $new_new_value;
}elseif ($row['field_name'] == 'priority'){
$new_old_value = isset($priorityType[$row['old_value']])?$priorityType[$row['old_value']]:'-';
$new_new_value = $priorityType[$row['new_value']];
$row['old_value'] = $new_old_value;
$row['new_value'] = $new_new_value;
}elseif ($row['field_name'] == 'relationship'){
$new_old_value = isset($relationshipType[$row['old_value']])?$relationshipType[$row['old_value']]: '-';
$new_new_value = $relationshipType[$row['new_value']];
$row['old_value'] = $new_old_value;
$row['new_value'] = $new_new_value;
}elseif ($row['field_name'] == 'mode_of_intimation'){
$new_old_value = isset($modeOFIntimate[$row['old_value']])?$modeOFIntimate[$row['old_value']]:"-";
$new_new_value = $modeOFIntimate[$row['new_value']];
$row['old_value'] = $new_old_value;
$row['new_value'] = $new_new_value;
}
elseif ($row['field_name'] == 'claim_type'){
$new_old_value = isset($claim_type[$row['old_value']])?$claim_type[$row['old_value']]:'-';
$new_new_value = $claim_type[$row['new_value']];
$row['old_value'] = $new_old_value;
$row['new_value'] = $new_new_value;
}
elseif ($row['field_name'] == 'claim_type'){
$new_old_value = isset($claim_type[$row['old_value']])?$claim_type[$row['old_value']]:'-';
$new_new_value = $claim_type[$row['new_value']];
$row['old_value'] = $new_old_value;
$row['new_value'] = $new_new_value;
}else{
$new_old_value = isset($row['old_value'])?$row['old_value']:'-';
$row['old_value'] = $new_old_value;
}
}
return($data);
}
}