From bc149fe761bd20695df406557ea9716949619084 Mon Sep 17 00:00:00 2001 From: Gowtham M Date: Tue, 2 Sep 2025 18:18:53 +0530 Subject: [PATCH] GWM : file upload --- app/Config/Routes.php | 5 + app/Controllers/AgentController.php | 181 +++++++++++++++++++++---- app/Controllers/EnquiryController.php | 3 + app/Models/AgentIncentiveFileModel.php | 39 ++++++ app/Models/AgentModel.php | 1 - app/Models/EnquiryModel.php | 31 +++++ 6 files changed, 230 insertions(+), 30 deletions(-) create mode 100644 app/Models/AgentIncentiveFileModel.php diff --git a/app/Config/Routes.php b/app/Config/Routes.php index e6c9ebb..b2d3ec9 100755 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -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 diff --git a/app/Controllers/AgentController.php b/app/Controllers/AgentController.php index 1979fc6..e1d67ce 100644 --- a/app/Controllers/AgentController.php +++ b/app/Controllers/AgentController.php @@ -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); + } + } + + + + diff --git a/app/Controllers/EnquiryController.php b/app/Controllers/EnquiryController.php index 34d6a56..1e54761 100644 --- a/app/Controllers/EnquiryController.php +++ b/app/Controllers/EnquiryController.php @@ -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); } diff --git a/app/Models/AgentIncentiveFileModel.php b/app/Models/AgentIncentiveFileModel.php new file mode 100644 index 0000000..735cf33 --- /dev/null +++ b/app/Models/AgentIncentiveFileModel.php @@ -0,0 +1,39 @@ + 'required|integer', + 'incentive_month' => 'required|valid_date', + 'incentive_file_name'=> 'required|string|max_length[150]' + ]; +} diff --git a/app/Models/AgentModel.php b/app/Models/AgentModel.php index 00ba4db..afbfb64 100644 --- a/app/Models/AgentModel.php +++ b/app/Models/AgentModel.php @@ -14,7 +14,6 @@ class AgentModel extends Model 'mobile', 'address', 'certificate_file_name', - 'incentive_file_name', 'email_otp', 'firebase_device_token', 'is_active', diff --git a/app/Models/EnquiryModel.php b/app/Models/EnquiryModel.php index 2a8b188..a03169f 100644 --- a/app/Models/EnquiryModel.php +++ b/app/Models/EnquiryModel.php @@ -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(); + } + + } }