193 lines
7.6 KiB
PHP
193 lines
7.6 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use CodeIgniter\RESTful\ResourceController;
|
|
use App\Models\EnquiryModel;
|
|
|
|
class EnquiryController extends ResourceController
|
|
{
|
|
protected $enquiryModel;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->enquiryModel = new EnquiryModel();
|
|
}
|
|
|
|
// List all enquiries
|
|
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);
|
|
}
|
|
}
|
|
|
|
// Find a single enquiry
|
|
public function findEnquiry()
|
|
{
|
|
try {
|
|
$id = $this->request->getGet('id');
|
|
$record = $this->enquiryModel->find($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, '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'] ?? null,
|
|
'insurer_id' => $data['insurer_id'] ?? null,
|
|
'rc_file_name' => $rcFileName,
|
|
'id_proof_file_name' => $idProofFileName,
|
|
'previous_policy_file_name' => $previousPolicyFileName,
|
|
'remarks' => $data['remarks'] ?? null,
|
|
'status' => $data['status'] ?? 'Pending',
|
|
'created_by' => $data['created_by'] ?? null
|
|
];
|
|
|
|
$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'],
|
|
'status' => $data['status'] ?? $enquiry['status'],
|
|
'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);
|
|
}
|
|
}
|
|
|
|
// Activate/Deactivate enquiry
|
|
public function toggleEnquiryStatus()
|
|
{
|
|
try {
|
|
$id = $this->request->getPost('id');
|
|
$isActive = $this->request->getPost('is_active');
|
|
|
|
if (!$id) {
|
|
return $this->respond(['status' => 'failed', 'code' => 200, 'error' => 'ID Required'], 200);
|
|
}
|
|
|
|
$this->enquiryModel->update($id, ['is_active' => $isActive]);
|
|
|
|
return $this->respond(['status' => 'success', 'code' => 200, 'message' => 'Status updated'], 200);
|
|
} catch (\Exception $e) {
|
|
return $this->respond(['status' => 'failed', 'code' => 500, 'error' => $e->getMessage()], 500);
|
|
}
|
|
}
|
|
}
|