286 lines
11 KiB
PHP
286 lines
11 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use CodeIgniter\RESTful\ResourceController;
|
|
|
|
use App\Models\AgentModel;
|
|
use App\Models\EnquiryModel;
|
|
use App\Models\QuotationModel;
|
|
use App\Models\PolicyModel;
|
|
|
|
class EnquiryController extends ResourceController
|
|
{
|
|
protected $enquiryModel;
|
|
protected $AgentModel;
|
|
protected $QuotationModel;
|
|
protected $PolicyModel;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->enquiryModel = new EnquiryModel();
|
|
$this->AgentModel = new AgentModel();
|
|
$this->enquiryModel = new QuotationModel();
|
|
$this->AgentModel = new PolicyModel();
|
|
}
|
|
|
|
// List all enquiries
|
|
public function enquiryList()
|
|
{
|
|
try {
|
|
|
|
$agent_id = $this->request->getGet('agent_id');
|
|
$staff_id = $this->request->getGet('staff_id');
|
|
$enquiry_id = $this->request->getGet('enquiry_id');
|
|
$manager_id = $this->request->getGet('manager_id');
|
|
|
|
if (!empty($agent_id)) {
|
|
$data = $this->enquiryModel->getEnquiry('AGENT', $agent_id);
|
|
} elseif (!empty($staff_id)) {
|
|
$data = $this->enquiryModel->getEnquiry('STAFF', $staff_id);
|
|
} elseif (!empty($enquiry_id)) {
|
|
$data = $this->enquiryModel->getEnquiry('ENQUIRY',$enquiry_id);
|
|
} elseif (!empty($manager_id)) {
|
|
$data = $this->enquiryModel->getEnquiry('MANAGER',$manager_id);
|
|
} else {
|
|
$data = $this->enquiryModel->getEnquiry('ALL');
|
|
}
|
|
|
|
return $this->respond(['status' => 'success', 'code' => 200, 'data' => $data], 200);
|
|
|
|
} catch (\Exception $e) {
|
|
return $this->respond(['status' => 'failed', 'code' => 500, 'error' => $e->getMessage()], 500);
|
|
}
|
|
}
|
|
|
|
// List all enquiries
|
|
public function enquiryQuotePolicyView()
|
|
{
|
|
try {
|
|
|
|
$enquiry_id = $this->request->getGet('enquiry_id');
|
|
|
|
|
|
|
|
return $this->respond(['status' => 'success', 'code' => 200, 'data' => []], 200);
|
|
|
|
} catch (\Exception $e) {
|
|
return $this->respond(['status' => 'failed', 'code' => 500, 'error' => $e->getMessage()], 500);
|
|
}
|
|
}
|
|
|
|
// CREATE enquiry
|
|
public function createEnquiry()
|
|
{
|
|
try {
|
|
$data = $this->request->getPost();
|
|
|
|
// handle file uploads
|
|
$idProofFile = $this->request->getFile('id_proof_file_name');
|
|
$rcFile = $this->request->getFile('rc_file_name');
|
|
$previousPolicy = $this->request->getFile('previous_policy_file_name');
|
|
|
|
$idProofFileName = null;
|
|
$rcFileName = null;
|
|
$previousPolicyFileName = null;
|
|
|
|
// ID proof upload
|
|
if ($idProofFile && $idProofFile->isValid()) {
|
|
$uploadPath = WRITEPATH . 'uploads/enquiry/id_proof/';
|
|
if (!is_dir($uploadPath)) mkdir($uploadPath, 0777, true);
|
|
$idProofFileName = time() . '_' . $idProofFile->getRandomName();
|
|
$idProofFile->move($uploadPath, $idProofFileName);
|
|
}
|
|
|
|
// RC upload
|
|
if ($rcFile && $rcFile->isValid()) {
|
|
$uploadPath = WRITEPATH . 'uploads/enquiry/rc/';
|
|
if (!is_dir($uploadPath)) mkdir($uploadPath, 0777, true);
|
|
$rcFileName = time() . '_' . $rcFile->getRandomName();
|
|
$rcFile->move($uploadPath, $rcFileName);
|
|
}
|
|
|
|
// Previous policy upload
|
|
if ($previousPolicy && $previousPolicy->isValid()) {
|
|
$uploadPath = WRITEPATH . 'uploads/enquiry/previous_policy/';
|
|
if (!is_dir($uploadPath)) mkdir($uploadPath, 0777, true);
|
|
$previousPolicyFileName = time() . '_' . $previousPolicy->getRandomName();
|
|
$previousPolicy->move($uploadPath, $previousPolicyFileName);
|
|
}
|
|
|
|
$insertData = [
|
|
'agent_id' => $data['agent_id'],
|
|
'reg_no' => $data['reg_no'],
|
|
'vehicle_type_id' => $data['vehicle_type_id'],
|
|
'insurer_id' => $data['insurer_id'],
|
|
'rc_file_name' => $rcFileName,
|
|
'id_proof_file_name' => $idProofFileName,
|
|
'previous_policy_file_name' => $previousPolicyFileName,
|
|
'remarks' => $data['remarks'],
|
|
'manager_id' => $data['manager_id'] ,
|
|
'created_by' => $data['created_by']
|
|
];
|
|
|
|
$this->enquiryModel->insert($insertData);
|
|
|
|
return $this->respond(['status' => 'success', 'code' => 200, 'message' => 'Enquiry created successfully'], 200);
|
|
|
|
} catch (\Exception $e) {
|
|
return $this->respond(['status' => 'failed', 'code' => 500, 'error' => $e->getMessage()], 500);
|
|
}
|
|
}
|
|
|
|
// UPDATE enquiry
|
|
public function updateEnquiry()
|
|
{
|
|
try {
|
|
$data = $this->request->getPost();
|
|
|
|
if (!isset($data['id'])) {
|
|
return $this->respond(['status' => 'failed', 'code' => 200, 'error' => 'ID Required'], 200);
|
|
}
|
|
|
|
$id = $data['id'];
|
|
$enquiry = $this->enquiryModel->find($id);
|
|
|
|
if (!$enquiry) {
|
|
return $this->respond(['status' => 'failed', 'code' => 200, 'error' => 'Data Not Found'], 200);
|
|
}
|
|
|
|
$updateData = [
|
|
'agent_id' => $data['agent_id'] ?? $enquiry['agent_id'],
|
|
'reg_no' => $data['reg_no'] ?? $enquiry['reg_no'],
|
|
'vehicle_type_id' => $data['vehicle_type_id'] ?? $enquiry['vehicle_type_id'],
|
|
'insurer_id' => $data['insurer_id'] ?? $enquiry['insurer_id'],
|
|
'remarks' => $data['remarks'] ?? $enquiry['remarks'],
|
|
'updated_by' => $data['updated_by'] ?? null,
|
|
];
|
|
|
|
// File updates
|
|
$idProofFile = $this->request->getFile('id_proof_file_name');
|
|
$rcFile = $this->request->getFile('rc_file_name');
|
|
$previousPolicy = $this->request->getFile('previous_policy_file_name');
|
|
|
|
if ($idProofFile && $idProofFile->isValid()) {
|
|
$uploadPath = WRITEPATH . 'uploads/enquiry/id_proof/';
|
|
if (!is_dir($uploadPath)) mkdir($uploadPath, 0777, true);
|
|
$idProofFileName = time() . '_' . $idProofFile->getRandomName();
|
|
$idProofFile->move($uploadPath, $idProofFileName);
|
|
$updateData['id_proof_file_name'] = $idProofFileName;
|
|
}
|
|
|
|
if ($rcFile && $rcFile->isValid()) {
|
|
$uploadPath = WRITEPATH . 'uploads/enquiry/rc/';
|
|
if (!is_dir($uploadPath)) mkdir($uploadPath, 0777, true);
|
|
$rcFileName = time() . '_' . $rcFile->getRandomName();
|
|
$rcFile->move($uploadPath, $rcFileName);
|
|
$updateData['rc_file_name'] = $rcFileName;
|
|
}
|
|
|
|
if ($previousPolicy && $previousPolicy->isValid()) {
|
|
$uploadPath = WRITEPATH . 'uploads/enquiry/previous_policy/';
|
|
if (!is_dir($uploadPath)) mkdir($uploadPath, 0777, true);
|
|
$previousPolicyFileName = time() . '_' . $previousPolicy->getRandomName();
|
|
$previousPolicy->move($uploadPath, $previousPolicyFileName);
|
|
$updateData['previous_policy_file_name'] = $previousPolicyFileName;
|
|
}
|
|
|
|
$this->enquiryModel->update($id, $updateData);
|
|
|
|
return $this->respond(['status' => 'success', 'code' => 200, 'message' => 'Enquiry updated successfully'], 200);
|
|
|
|
} catch (\Exception $e) {
|
|
return $this->respond(['status' => 'failed', 'code' => 500, 'error' => $e->getMessage()], 500);
|
|
}
|
|
}
|
|
|
|
//Assign to staff
|
|
public function enquiryAssignUpdate()
|
|
{
|
|
try {
|
|
|
|
$data = $this->request->getJSON(true);
|
|
|
|
if (!isset($data['id'])) {
|
|
return $this->respond(['status' => 'failed', 'code' => 200, 'error' => 'ID Required'], 200);
|
|
}
|
|
|
|
$id = $data['id'];
|
|
$enquiry = $this->enquiryModel->find($id);
|
|
|
|
if (!$enquiry) {
|
|
return $this->respond(['status' => 'failed', 'code' => 200, 'error' => 'Data Not Found'], 200);
|
|
}
|
|
|
|
$updateData = [
|
|
'assigned_to' => $data['assigned_to']
|
|
];
|
|
|
|
$this->enquiryModel->update($id, $updateData);
|
|
|
|
return $this->respond(['status' => 'success', 'code' => 200, 'message' => 'Enquiry updated successfully'], 200);
|
|
|
|
|
|
return $this->respond(['status' => 'success', 'code' => 200, 'data' => $data], 200);
|
|
|
|
} catch (\Exception $e) {
|
|
return $this->respond(['status' => 'failed', 'code' => 500, 'error' => $e->getMessage()], 500);
|
|
}
|
|
}
|
|
|
|
|
|
// Download enquiry file
|
|
public function downloadEnquiryFile()
|
|
{
|
|
try {
|
|
$enquiryId = $this->request->getGet('enquiry_id');
|
|
$fileType = $this->request->getGet('file_type'); // rc | id_proof | previous_policy
|
|
|
|
if (!$enquiryId || !$fileType) {
|
|
return $this->respond(['status' => 'failed','code' => 400,'data' => 'enquiry_id and file_type are required'], 200);
|
|
}
|
|
|
|
// Map file_type to DB column and folder
|
|
$fileMap = [
|
|
'rc' => ['column' => 'rc_file_name', 'folder' => 'rc'],
|
|
'id_proof' => ['column' => 'id_proof_file_name', 'folder' => 'id_proof'],
|
|
'previous_policy' => ['column' => 'previous_policy_file_name', 'folder' => 'previous_policy'],
|
|
];
|
|
|
|
if (!array_key_exists($fileType, $fileMap)) {
|
|
return $this->respond(['status' => 'failed','code' => 400,'data' => 'Invalid file_type'], 200);
|
|
}
|
|
|
|
$fileColumn = $fileMap[$fileType]['column'];
|
|
$folder = $fileMap[$fileType]['folder'];
|
|
|
|
// Fetch record from DB
|
|
$fileRecord = $this->enquiryModel->where('is_active', 1)->find($enquiryId);
|
|
|
|
if (!$fileRecord || empty($fileRecord[$fileColumn])) {
|
|
return $this->respond([ 'status' => 'failed','code' => 404,'data' => 'File not found in database'], 200);
|
|
}
|
|
|
|
$filePath = WRITEPATH . "uploads/enquiry/{$folder}/" . $fileRecord[$fileColumn];
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|