nhance/app/Controllers/ThzController.php

170 lines
4.4 KiB
PHP

<?php
namespace App\Controllers;
use App\Controllers\BaseController;
use CodeIgniter\HTTP\ResponseInterface;
use App\Models\ThzMasterModel;
use App\Models\ThzMasterNotesModel;
use App\ControllerCleaners\ThzControllerCleaner;
class ThzController extends BaseController
{
protected $thzMasterModel;
protected $thzMasterNotesModel;
protected $thzControllerCleaner;
public function __construct()
{
$this->thzMasterModel = new ThzMasterModel();
$this->thzMasterNotesModel = new ThzMasterNotesModel();
$this->thzControllerCleaner = new ThzControllerCleaner();
}
public function index()
{
//
}
public function ticketSave(){
$data = $this->request->getJSON(true);
if (!empty($data['thz_id'])) {
$data['updated_by'] = get_session_userid();
$ticket_id = $data['thz_id'];
$text = "update";
$result = $this->thzMasterModel->where('thz_id', $ticket_id)->set($data)->update();
} else {
$data['created_by'] = get_session_userid();
$text = "create";
$result = $this->thzMasterModel->insert($data);
}
return $this->response->setJSON(([ 'status' => $result ? 'success' : 'error' ,
'message' => $result ? "Ticket {$text}d successfully" : "Unable to {$text} ticket. Please try again." ]));
}
public function ticketList()
{
$returnType = strtolower($this->request->getGet('return_type') ?? 'api');
$data = $this->request->getJSON(true);
$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['assigner'] = $this->userModel->where('is_active', 1)->findAll();
return $this->loadLayout('ticket_list_web', $data);
}
return $this->response->setJSON([
'status' => 'success',
'data' => $tickets,
]);
}
public function ticketConversationSave(){
$data = $this->request->getJSON(true);
$result = $this->thzMasterNotesModel->insert($data);
if(empty($result)){
return $this->response->setJSON((['status' => 'error', 'message' => 'No tickets found']));
}
return $this->response->setJSON((['status' => 'success', 'data' => $result]));
}
public function ticketConversationList(){
$data = $this->request->getJSON(true);
$thz_id = $data['thz_id'] ?? null;
$result = $this->thzMasterNotesModel->ticketConversationList($thz_id);
if(empty($result)){
return $this->response->setJSON((['status' => 'error', 'message' => 'No tickets found']));
}
return $this->response->setJSON((['status' => 'success', 'data' => $result]));
}
/************************************************** PRIVATE FUNCTIONS ********************************************************/
private function checkGeneralUserOrEmployee($data){
if($data['empcode']){
return $data['empcode'];
} elseif ($data['mobile']) {
return $data['mobile'];
} elseif ($data['email']) {
return $data['email'];
} else {
return null;
}
}
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
->where('assign_to', $assign_to)
->findAll();
}
if (!empty($id) && !empty($mobile)) {
// Specific ticket by ID and mobile
return $this->thzMasterModel
->where('thz_id', $id)
->where('mobile', $mobile)
->findAll();
}
// All tickets (e.g., for managers)
return $this->thzMasterModel->findAll();
}
}