nhance/app/Controllers/ThzController.php
venba-Inspriron-3558 d3b5384db4 CHANGE_API
2025-08-19 11:33:15 +05:30

241 lines
7.3 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;
use App\Models\UserModel;
class ThzController extends BaseController
{
protected $thzMasterModel;
protected $thzMasterNotesModel;
protected $thzControllerCleaner;
protected $userModel;
public function __construct()
{
$this->thzMasterModel = new ThzMasterModel();
$this->thzMasterNotesModel = new ThzMasterNotesModel();
$this->thzControllerCleaner = new ThzControllerCleaner();
$this->userModel = new UserModel();
}
public function index()
{
//
}
public function ticketSave(){
$data = $this->request->getPost();
// $data = $this->request->getJSON(true);
// $return_type = isset($data['return_type']) ? $data['return_type'] : 'api';
if (!empty($data['thz_id'])) {
$text = "update";
$result = $this->updateTicket($data);
} else {
$text = "create";
$result = $this->insertTicket($data);
}
return $this->response->setJSON([
'status' => $result ? 'success' : 'error',
'message' => $result ? "Ticket {$text}d successfully" : "Unable to {$text} ticket. Please try again."
])->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..
return $this->loadLayout('thz_list', $data);
}
return $this->response->setJSON([
'status' => 'success',
'data' => $tickets,
])->setStatusCode(200);
}
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']))->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 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();
$notes = $this->thzMasterNotesModel->ticketConversationList($thz_id);
$result['notes'] = !empty($notes) ? $notes : [];
}else{
return $this->response->setJSON((['status' => 'error', 'message' => 'No tickets found']))->setStatusCode(404);
}
return $this->response->setJSON((['status' => 'success', 'data' => $result]))->setStatusCode(400);
}
public function ticketSaveNotification($data){
}
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 notificationBasedOnRole($data){
$id = $data['thz_id'] ?? null;
$assign_to = $data['assign_to'] ?? null;
$mobile = $data['mobile'] ?? null;
if (!empty($assign_to)) {
}
if (!empty($id) && !empty($mobile)) {
}
}
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();
$result = $this->thzMasterModel->insert($data);
return $result;
}
}