nhance_partner_be/app/Controllers/StaffController.php

157 lines
4.9 KiB
PHP

<?php
namespace App\Controllers;
use CodeIgniter\RESTful\ResourceController;
use App\Controllers\BaseController;
use App\Models\StaffModel;
class StaffController extends ResourceController
{
protected $StaffModel;
public function __construct()
{
$this->StaffModel = new StaffModel();
}
// List of all Staffs
public function staffList()
{
try{
$id = $this->request->getGet('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' , $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);
}
}
// 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->getPost();
$insertData = [
'name' => $data['name'],
'email' => $data['email'],
'mobile' => $data['mobile'],
'role_id' => $data['role_id'],
'manager_id' => $data['manager_id'],
'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->getPost();
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,
'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->getPost();
// 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);
}
}
}