nhance_partner_be/app/Controllers/AgentController.php

386 lines
13 KiB
PHP

<?php
namespace App\Controllers;
use CodeIgniter\RESTful\ResourceController;
use App\Controllers\BaseController;
use App\Models\AgentModel;
use App\Models\AgentIncentiveFileModel;
class AgentController extends ResourceController
{
protected $AgentModel;
protected $AgentIncentiveFileModel;
public function __construct()
{
$this->AgentModel = new AgentModel();
$this->AgentIncentiveFileModel = new AgentIncentiveFileModel();
}
// List of all agents
public function agentList()
{
try{
$id = $this->request->getGet('manager_id');
$data = $this->AgentModel->where('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);
}
}
public function agentListForEnquiryCreationDropdown()
{
try{
$id = $this->request->getGet('manager_id');
$data = $this->AgentModel->where('manager_id' , $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);
}
}
// Find a agent
public function findAgent()
{
try{
$id = $this->request->getGet('id');
$record = $this->AgentModel->find((int)$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 agent
public function createAgent()
{
try{
$data = $this->request->getPost();
// handle file uploads
$certificateFile = $this->request->getFile('certificate_file_name');
$certificateFileName = null;
// certificate upload
if ($certificateFile && $certificateFile->isValid()) {
$uploadPath = WRITEPATH . 'uploads/agent/certificate_file/';
if (!is_dir($uploadPath)) {
mkdir($uploadPath, 0777, true);
}
$certificateFileName = time() . '_' . $certificateFile->getRandomName();
$certificateFile->move($uploadPath, $certificateFileName);
}
$insertData = [
'name' => $data['name'],
'email' => $data['email'],
'mobile' => $data['mobile'],
'agent_code' => $data['agent_code'],
'address' => $data['address'],
'manager_id' => $data['manager_id'],
'sales_executive_id' => $data['sales_executive_id'],
'certificate_file_name' => $certificateFileName,
'created_by' => $data['created_by'],
'retention_rate' => $data['retention_rate'] ?? 0
];
$this->AgentModel->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 agent
public function updateAgent()
{
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 agent exists
$agent = $this->AgentModel->find((int)$id);
if (!$agent) {
return $this->respond(['status' => 'failed', 'code' => 200, 'data' => 'Date Not Found'], 200);
}
// handle file uploads
$certificateFile = $this->request->getFile('certificate_file_name');
$updateData = [
'name' => $data['name'] ?? null,
'email' => $data['email'] ?? null,
'mobile' => $data['mobile'] ?? null,
'address' => $data['address'] ?? null,
'agent_code' => $data['agent_code'] ?? null,
'sales_executive_id' => $data['sales_executive_id'] ?? null,
'updated_by' => $data['updated_by'] ?? null,
'retention_rate' => $data['retention_rate'] ?? $agent['retention_rate']
];
if ($certificateFile && $certificateFile->isValid()) {
$uploadPath = WRITEPATH . 'uploads/agent/certificate_file/';
if (!is_dir($uploadPath)) {
mkdir($uploadPath, 0777, true);
}
$certificateFileName = time() . '_' . $certificateFile->getRandomName();
$certificateFile->move($uploadPath, $certificateFileName);
$updateData['certificate_file_name'] = $certificateFileName;
}
$this->AgentModel->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);
}
}
//update device token
public function updateDeviceToken()
{
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 agent exists
$agent = $this->AgentModel->find((int)$id);
if (!$agent) {
return $this->respond(['status' => 'failed', 'code' => 200, 'data' => 'Date Not Found'], 200);
}
$updateData = [
'firebase_device_token' => $data['firebase_device_token'] ?? null,
];
$this->AgentModel->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 agent
public function changeAgentStatus()
{
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 agent exists
$agent = $this->AgentModel->find((int)$id);
if (!$agent) {
return $this->respond(['status' => 'failed','code' => 200, 'data' => 'Agent not found'], 200);
}
// update status
$this->AgentModel->update($id, ['is_active' => $status]);
return $this->respond([
'status' => 'success', 'code' => 200,'data' => "Agent status updated"], 200);
} catch (\Exception $e) {
return $this->respond(['status' => 'failed','code' => 500,'data' => $e->getMessage()], 500);
}
}
// Download agent certificate file
public function downloadAgentCertificateFile()
{
try {
$id = $this->request->getGet('agent_id');
if (!$id) {
return $this->respond(['status' => 'failed', 'code' => 400, 'data' => 'ID is required'], 200);
}
// Fetch record from DB
$fileRecord = $this->AgentModel->where('is_active',1)->find((int)$id);
if (!$fileRecord) {
return $this->respond(['status' => 'failed', 'code' => 404, 'data' => 'File not found'], 200);
}
$filePath = WRITEPATH . 'uploads/agent/certificate_file/' . $fileRecord['certificate_file_name'];
if (!file_exists($filePath)) {
return $this->respond(['status' => 'failed', 'code' => 404, 'data' => 'File missing on server'], 200);
}
// Force file download
return $this->response->download($filePath, null);
} catch (\Exception $e) {
return $this->respond(['status' => 'failed', 'code' => 500, 'message' => $e->getMessage()], 500);
}
}
// List of all agents incentive files
public function agentIncentiveFileList()
{
try{
$agent_id = $this->request->getGet('agent_id');
$data = $this->AgentIncentiveFileModel->where('agent_id',$agent_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);
}
}
// Upload agent incentive file
public function uploadAgentIncentiveFile()
{
try{
$data = $this->request->getPost();
//duplicate check
$duplicateData = $this->AgentIncentiveFileModel->where('agent_id',$data['agent_id'])->where('incentive_month',$data['incentive_month'])->first();
if(!empty($duplicateData)){
return $this->respond(['status' => 'failed', 'code' => 200, 'data' => 'Duplicate Entry.'], 200);
}
// handle file uploads
$incentiveFile = $this->request->getFile('incentive_file_name');
$incentiveFileName = null;
// incentive upload
if ($incentiveFile && $incentiveFile->isValid()) {
$uploadPath = WRITEPATH . 'uploads/agent/incentive_file/';
if (!is_dir($uploadPath)) {
mkdir($uploadPath, 0777, true);
}
$incentiveFileName = time() . '_' . $incentiveFile->getRandomName();
$incentiveFile->move($uploadPath, $incentiveFileName);
}
$insertData = [
'agent_id' => $data['agent_id'],
'incentive_month' => $data['incentive_month'],
'incentive_file_name' => $incentiveFileName,
'created_by' => $data['created_by'] ?? null
];
$this->AgentIncentiveFileModel->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);
}
}
// Delete agent incentive file
public function deleteAgentIncentiveFile()
{
try {
$id = $this->request->getGet('id');
// check if agent exists
$file = $this->AgentIncentiveFileModel->find((int)$id);
if (!$file) {
return $this->respond(['status' => 'failed','code' => 200, 'data' => 'File not found'], 200);
}
// update status
$this->AgentIncentiveFileModel->update($id, ['is_active' => 0]);
return $this->respond([
'status' => 'success', 'code' => 200,'data' => "File status updated"], 200);
} catch (\Exception $e) {
return $this->respond(['status' => 'failed','code' => 500,'data' => $e->getMessage()], 500);
}
}
// Download agent incentive file
public function downloadAgentIncentiveFile()
{
try {
$id = $this->request->getGet('id');
if (!$id) {
return $this->respond(['status' => 'failed', 'code' => 400, 'data' => 'ID is required'], 200);
}
// Fetch record from DB
$fileRecord = $this->AgentIncentiveFileModel->where('is_active',1)->find((int)$id);
if (!$fileRecord) {
return $this->respond(['status' => 'failed', 'code' => 404, 'data' => 'File not found'], 200);
}
$filePath = WRITEPATH . 'uploads/agent/incentive_file/' . $fileRecord['incentive_file_name'];
if (!file_exists($filePath)) {
return $this->respond(['status' => 'failed', 'code' => 404, 'data' => 'File missing on server'], 200);
}
// Force file download
return $this->response->download($filePath, null);
} catch (\Exception $e) {
return $this->respond(['status' => 'failed', 'code' => 500, 'message' => $e->getMessage()], 500);
}
}
}