From e64bbd7fa2879fbc75c00dee1c1b834e5f4738d5 Mon Sep 17 00:00:00 2001 From: Gowtham M Date: Wed, 26 Nov 2025 16:56:00 +0530 Subject: [PATCH] GWM : multiple file upload --- app/Config/Routes.php | 4 + app/Controllers/EnquiryController.php | 118 +++++++++++++++++++++++++- app/Models/FilesModel.php | 36 ++++++++ 3 files changed, 156 insertions(+), 2 deletions(-) create mode 100644 app/Models/FilesModel.php diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 4676aaf..fef6322 100755 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -87,6 +87,10 @@ $routes->group('api', ['filter' => 'appSignature'], function ($routes) { $routes->post('enquiry/createEnquiry', 'EnquiryController::createEnquiry'); $routes->post('enquiry/updateEnquiry', 'EnquiryController::updateEnquiry'); $routes->get('enquiry/downloadEnquiryFile', 'EnquiryController::downloadEnquiryFile'); + $routes->post('enquiry/uploadEnquiryFiles', 'EnquiryController::uploadEnquiryFiles'); + $routes->get('enquiry/downloadEnquiryFiles', 'EnquiryController::downloadEnquiryFiles'); + $routes->get('enquiry/enquiryFiles', 'EnquiryController::enquiryFiles'); + //Proposal $routes->get('quotation/quotationList', 'QuotationController::quotationList'); diff --git a/app/Controllers/EnquiryController.php b/app/Controllers/EnquiryController.php index b045ad7..ee8f363 100644 --- a/app/Controllers/EnquiryController.php +++ b/app/Controllers/EnquiryController.php @@ -8,6 +8,7 @@ use App\Models\AgentModel; use App\Models\EnquiryModel; use App\Models\QuotationModel; use App\Models\PolicyModel; +use App\Models\FilesModel; use DateTime; class EnquiryController extends ResourceController { @@ -16,6 +17,7 @@ class EnquiryController extends ResourceController protected $AgentModel; protected $QuotationModel; protected $PolicyModel; + protected $FilesModel; public function __construct() { @@ -25,6 +27,7 @@ class EnquiryController extends ResourceController $this->AgentModel = new AgentModel(); $this->QuotationModel = new QuotationModel(); $this->PolicyModel = new PolicyModel(); + $this->FilesModel = new FilesModel(); } // List all enquiries @@ -240,12 +243,28 @@ class EnquiryController extends ResourceController ]; - - $enquiryId = $this->enquiryModel->insert($insertData , true); // trigger_email('enquiry_created', ['enquiry_id' => $enquiryId]); + //create Quotation ana mark as accepted + $quotationData = [ + 'enquiry_id' => $enquiryId, + 'insured_declared_value' => $data['insured_declared_value'] ?? 0, + 'premium_amount' => $data['premium_amount'] ?? 0, + 'insurance_plan_type_id' => $data['insurance_plan_type_id'] ?? 0, + 'insurer_id' => $data['insurer_id'] ?? 0, + 'insurer_branch_id' => $insurerBranchresult['id'] ?? 0, + 'manager_id' => $data['manager_id'], + 'payment_mode_id' => $data['payment_mode_id'] ?? 0, + 'created_by' => $data['created_by'], + 'status' => 'Accepted' + ]; + $QuotationId = $this->QuotationModel->insert($quotationData, true); + $this->enquiryModel->update($data['enquiry_id'], [ 'status' => 'Proposal Accepted' ]); + + + return $this->respond(['status' => 'success', 'code' => 200, 'message' => 'Enquiry created successfully'], 200); } catch (\Exception $e) { @@ -422,6 +441,101 @@ class EnquiryController extends ResourceController } } + public function uploadEnquiryFiles() + { + try { + $enquiryId = $this->request->getPost('enquiry_id'); + + if (!$enquiryId) { + return $this->respond(['status' => 'failed', 'message' => 'enquiry_id required'], 404); + } + + $files = $this->request->getFiles(); + + if (!$files) { + return $this->respond(['status' => 'failed', 'message' => 'No files uploaded'], 404); + } + + + $savedFiles = []; + + foreach ($files['files'] as $file) { + if ($file->isValid()) { + $uploadPath = WRITEPATH . "uploads/enquiry/"; + if (!is_dir($uploadPath)) { + mkdir($uploadPath, 0777, true); + } + + // Generate file name + $newName = time() . '_' . $file->getRandomName(); + $file->move($uploadPath, $newName); + + // Save DB record + $this->FilesModel->insert([ + 'enquiry_id' => $enquiryId, + 'file_name' => $newName, + 'is_active' => 1, + 'created_by' => $this->request->getPost('created_by') ?? 0, + ]); + + $savedFiles[] = $newName; + } + } + + return $this->respond(['status' => 'success','uploaded_files' => $savedFiles ], 200); + + } catch (\Exception $e) { + return $this->respond(['status' => 'failed', 'message' => $e->getMessage()], 500); + } + } + + public function downloadEnquiryFiles() + { + + + $fileId = $this->request->getGet('file_id'); + + $file = $this->FilesModel->find($fileId); + + if (!$file) { + return $this->respond(['status' => 'failed','message' => 'Invalid file id'], 404); + } + + $filePath = WRITEPATH . "uploads/enquiry/{$file['file_name']}"; + + if (!file_exists($filePath)) { + return $this->respond([ + 'status' => 'failed', + 'message' => 'File missing on server' + ], 404); + } + + // Force file download + return $this->response->download($filePath, null); + } + + public function enquiryFiles() + { + $enquiryId = $this->request->getGet('enquiry_id'); + + if (!$enquiryId) { + return $this->respond(['status' => 'failed', 'message' => 'enquiry_id required'], 400); + } + + + $files = $this->FilesModel + ->where('enquiry_id', $enquiryId) + ->where('is_active', 1) + ->findAll(); + + return $this->respond([ + 'status' => 'success', + 'data' => $files + ], 200); + } + + + diff --git a/app/Models/FilesModel.php b/app/Models/FilesModel.php new file mode 100644 index 0000000..31fd02e --- /dev/null +++ b/app/Models/FilesModel.php @@ -0,0 +1,36 @@ + 'required|integer', + 'file_name' => 'required|min_length[1]|max_length[255]', + 'is_active' => 'permit_empty|in_list[0,1]' + ]; +}