GWM : file upload

This commit is contained in:
Gowtham M 2025-09-02 18:18:53 +05:30
parent f8ed1acae4
commit bc149fe761
6 changed files with 230 additions and 30 deletions

View File

@ -41,6 +41,11 @@ $routes->group('api', ['filter' => 'appSignature'], function ($routes) {
$routes->post('agent/createAgent', 'AgentController::createAgent');
$routes->post('agent/updateAgent', 'AgentController::updateAgent');
$routes->post('agent/changeAgentStatus', 'AgentController::changeAgentStatus');
$routes->get('agent/downloadAgentCertificateFile', 'AgentController::downloadAgentCertificateFile');
$routes->get('agent/agentIncentiveFileList', 'AgentController::agentIncentiveFileList');
$routes->post('agent/uploadAgentIncentiveFile', 'AgentController::uploadAgentIncentiveFile');
$routes->get('agent/deleteAgentIncentiveFile', 'AgentController::deleteAgentIncentiveFile');
$routes->get('agent/downloadAgentIncentiveFile', 'AgentController::downloadAgentIncentiveFile');
//Staff

View File

@ -4,14 +4,17 @@ 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
@ -57,10 +60,8 @@ class AgentController extends ResourceController
// handle file uploads
$certificateFile = $this->request->getFile('certificate_file_name');
$incentiveFile = $this->request->getFile('incentive_file_name');
$certificateFileName = null;
$incentiveFileName = null;
// certificate upload
if ($certificateFile && $certificateFile->isValid()) {
@ -72,23 +73,12 @@ class AgentController extends ResourceController
$certificateFile->move($uploadPath, $certificateFileName);
}
// 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 = [
'name' => $data['name'],
'email' => $data['email'],
'mobile' => $data['mobile'],
'address' => $data['address'] ?? null,
'certificate_file_name' => $certificateFileName,
'incentive_file_name' => $incentiveFileName,
'created_by' => $data['created_by'] ?? null
];
@ -114,14 +104,13 @@ class AgentController extends ResourceController
$id = $data['id'];
// check if agent exists
$agent = $this->AgentModel->find($id);
if (!$agent) {
return $this->respond(['status' => 'failed', 'code' => 200, 'data' => 'Date Not Found'], 200);
}
$agent = $this->AgentModel->find($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');
$incentiveFile = $this->request->getFile('incentive_file_name');
$updateData = [
'name' => $data['name'] ?? null,
@ -141,16 +130,6 @@ class AgentController extends ResourceController
$updateData['certificate_file_name'] = $certificateFileName;
}
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);
$updateData['incentive_file_name'] = $incentiveFileName;
}
$this->AgentModel->update($id, $updateData);
return $this->respond(['status' => 'success', 'code' => 200, 'data' => []], 200);
@ -160,7 +139,7 @@ class AgentController extends ResourceController
}
}
// ACTIVATE / DEACTIVATE agent
// ACTIVATE / DEACTIVATE agent
public function changeAgentStatus()
{
try {
@ -193,6 +172,150 @@ class AgentController extends ResourceController
}
}
// 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($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();
// 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($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($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);
}
}

View File

@ -18,8 +18,11 @@ class EnquiryController extends ResourceController
public function enquiryList()
{
try {
$data = $this->enquiryModel->findAll();
return $this->respond(['status' => 'success', 'code' => 200, 'data' => $data], 200);
} catch (\Exception $e) {
return $this->respond(['status' => 'failed', 'code' => 500, 'error' => $e->getMessage()], 500);
}

View File

@ -0,0 +1,39 @@
<?php
namespace App\Models;
use CodeIgniter\Model;
class AgentIncentiveFileModel extends Model
{
protected $table = 'partner_agent_incentive_file';
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
// Fields allowed for insert/update
protected $allowedFields = [
'agent_id',
'incentive_month',
'incentive_file_name',
'is_active',
'created_by',
'created_on',
'updated_by',
'updated_on'
];
// Automatically manage created/updated timestamps
protected $useTimestamps = false; // we already handle manually in table
protected $createdField = 'created_on';
protected $updatedField = 'updated_on';
// Return result as array
protected $returnType = 'array';
// Validation rules (optional, add as per your need)
protected $validationRules = [
'agent_id' => 'required|integer',
'incentive_month' => 'required|valid_date',
'incentive_file_name'=> 'required|string|max_length[150]'
];
}

View File

@ -14,7 +14,6 @@ class AgentModel extends Model
'mobile',
'address',
'certificate_file_name',
'incentive_file_name',
'email_otp',
'firebase_device_token',
'is_active',

View File

@ -28,4 +28,35 @@ class EnquiryModel extends Model
protected $useTimestamps = true;
protected $createdField = 'created_on';
protected $updatedField = 'updated_on';
public function getEnquiry($getBy = null , $agentId = null , $enquiryId = null)
{
$data = $this->select( 'partner_enquiry.*,
VT.vehicle_type as vehicle_type,
A.name as agent_name'
)
->join('partner_vehicle_type_master VT', 'TV.id = partner_enquiry.vehicle_type_id ', 'left')
->join('partner_agent A', 'A.id = partner_enquiry.agent_id ', 'left')
->where('partner_enquiry.is_active', 1);
if($getBy == 'ALL' || $getBy == null)
{
return $data->findAll();
}
else if($getBy == 'AGENT')
{
return $data->where('agent_id',$agentId)->findAll();
}
else if($getBy == 'ENQUERY')
{
return $data->where('partner_enquiry.id', $enquiryId)->first();
}
else if($getBy == 'APPROVAL')
{
return $data->whereIn('partner_enquiry.id', $enquiryId)->findAll();
}
}
}