nhance_partner_be/app/Controllers/StaffController.php
2025-10-11 10:20:59 +05:30

268 lines
9.1 KiB
PHP

<?php
namespace App\Controllers;
use CodeIgniter\RESTful\ResourceController;
use App\Controllers\BaseController;
use App\Models\StaffModel;
use App\Models\ManagerIncentiveFileModel;
class StaffController extends ResourceController
{
protected $StaffModel;
protected $ManagerIncentiveFileModel;
public function __construct()
{
$this->StaffModel = new StaffModel();
$this->ManagerIncentiveFileModel = new ManagerIncentiveFileModel();
}
// List of all Staffs
public function staffList()
{
try{
$manager_id = $this->request->getGet('manager_id');
$handler_id = $this->request->getGet('handler_id');
if(!empty($manager_id))
{
$data = $this->StaffModel->select('partner_staff.* , R.role')
->join('partner_role_master R', 'R.id = partner_staff.role_id', 'left')
->where('partner_staff.manager_id' , $manager_id )
->findAll();
}else{
$data = $this->StaffModel->select('partner_staff.* , R.role')
->join('partner_role_master R', 'R.id = partner_staff.role_id', 'left')
->where('partner_staff.handler_id' , $handler_id )
->findAll();
}
return $this->respond(['status' => 'success', 'code' => 200, 'data' => $data], 200);
} catch (\Exception $e) {
return $this->respond(['status' => 'failed', 'code' => 500, 'data' => $e->getMessage()], 500);
}
}
public function staffListForEnquiryAssignDropdown()
{
try{
$id = $this->request->getGet('handler_id');
$data = $this->StaffModel->select('partner_staff.id, partner_staff.name , R.role')
->join('partner_role_master R', 'R.id = partner_staff.role_id', 'left')
->where('partner_staff.handler_id' , $id )
->where('partner_staff.is_active' , 1 )
->findAll();
return $this->respond(['status' => 'success', 'code' => 200, 'data' => $data], 200);
} catch (\Exception $e) {
return $this->respond(['status' => 'failed', 'code' => 500, 'data' => $e->getMessage()], 500);
}
}
public function handlerListForStaffCreationDropdown()
{
try{
$manager_id = $this->request->getGet('manager_id');
$data = $this->StaffModel->select('partner_staff.id, partner_staff.name , R.role')
->join('partner_role_master R', 'R.id = partner_staff.role_id', 'left')
->where('partner_staff.handler_id' , $manager_id )
->where('partner_staff.role_id' , 2 )
->where('partner_staff.is_active' , 1 )
->findAll();
return $this->respond(['status' => 'success', 'code' => 200, 'data' => $data], 200);
} catch (\Exception $e) {
return $this->respond(['status' => 'failed', 'code' => 500, 'data' => $e->getMessage()], 500);
}
}
// Find a Staff
public function findStaff()
{
try{
$id = $this->request->getGet('id');
$record = $this->StaffModel->find($id);
if (!$record) {
return $this->respond(['status' => 'failed', 'code' => 404, 'data' => []], 200);
}
return $this->respond(['status' => 'success', 'code' => 200, 'data' => $record], 200);
} catch (\Exception $e) {
return $this->respond(['status' => 'failed', 'code' => 500, 'data' => $e->getMessage()], 500);
}
}
// CREATE Staff
public function createStaff()
{
try{
$data = $this->request->getJSON(true);
$insertData = [
'name' => $data['name'],
'email' => $data['email'],
'mobile' => $data['mobile'],
'role_id' => $data['role_id'],
'manager_id' => $data['manager_id'],
'handler_id' => $data['handler_id'] ?? null,
'emp_id' => $data['emp_id'],
'created_by' => $data['created_by']
];
$this->StaffModel->insert($insertData);
return $this->respond(['status' => 'success', 'code' => 200, 'data' => []], 200);
} catch (\Exception $e) {
return $this->respond(['status' => 'failed', 'code' => 500, 'data' => $e->getMessage()], 500);
}
}
// UPDATE Staff
public function updateStaff()
{
try{
$data = $this->request->getJSON(true);
print_r($data);
if (!isset($data['id'])) {
return $this->respond(['status' => 'failed', 'code' => 200, 'data' => 'ID Required'], 200);
}
$id = $data['id'];
// check if Staff exists
$Staff = $this->StaffModel->find($id);
if (!$Staff) {
return $this->respond(['status' => 'failed', 'code' => 200, 'data' => 'Date Not Found'], 200);
}
$updateData = [
'name' => $data['name'],
'email' => $data['email'],
'mobile' => $data['mobile'],
'role_id' => $data['role_id'] ?? null,
'emp_id' => $data['emp_id'] ?? null,
'handler_id' => $data['handler_id'] ?? null,
'updated_by' => $data['updated_by'] ?? null,
];
$this->StaffModel->update($id, $updateData);
return $this->respond(['status' => 'success', 'code' => 200, 'data' => []], 200);
} catch (\Exception $e) {
return $this->respond(['status' => 'failed', 'code' => 500, 'data' => $e->getMessage()], 500);
}
}
// ACTIVATE / DEACTIVATE Staff
public function changeStaffStatus()
{
try {
$data = $this->request->getJSON(true);
// validate inputs
if (!isset($data['id']) || !isset($data['is_active'])) {
return $this->respond(['status' => 'failed', 'code' => 200,'data' => 'ID and Status are required'], 200);
}
$id = $data['id'];
$is_active = (int) $data['is_active']; // 0 or 1
$status = ($is_active == 1) ? 0 : 1;
// check if Staff exists
$Staff = $this->StaffModel->find($id);
if (!$Staff) {
return $this->respond(['status' => 'failed','code' => 200, 'data' => 'Staff not found'], 200);
}
// update status
$this->StaffModel->update($id, ['is_active' => $status]);
return $this->respond([
'status' => 'success', 'code' => 200,'data' => "Staff status updated"], 200);
} catch (\Exception $e) {
return $this->respond(['status' => 'failed','code' => 500,'data' => $e->getMessage()], 500);
}
}
// List of all manager incentive files
public function managerIncentiveFileList()
{
try{
$manager_id = $this->request->getGet('manager_id');
$data = $this->ManagerIncentiveFileModel->where('manager_id',$manager_id)->where('is_active',1)->findAll();
return $this->respond(['status' => 'success', 'code' => 200, 'data' => $data], 200);
} catch (\Exception $e) {
return $this->respond(['status' => 'failed', 'code' => 500, 'data' => $e->getMessage()], 500);
}
}
// Download manager incentive file (proxy to external app)
public function downloadManagerIncentiveFile()
{
try {
$id = $this->request->getGet('id');
// External API URL
$url = "https://venbait.in/nhance/dev/user/download-incentive-file/" . urlencode($id);
// Use CURL request to fetch the file
$client = \Config\Services::curlrequest();
$response = $client->get($url);
if ($response->getStatusCode() === 200) {
// Pass the file contents back as download
return $this->response
->setHeader('Content-Type', $response->getHeaderLine('Content-Type'))
->setHeader('Content-Disposition', 'attachment; id="' . basename($id) . '"')
->setBody($response->getBody());
} else {
return $this->respond([
'status' => 'failed',
'code' => $response->getStatusCode(),
'message' => 'Unable to fetch file from source'
], $response->getStatusCode());
}
} catch (\Exception $e) {
return $this->respond(['status' => 'failed', 'code' => 500,'data' => $e->getMessage()], 500);
}
}
}