178 lines
6.6 KiB
PHP
178 lines
6.6 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
use CodeIgniter\RESTful\ResourceController;
|
|
use App\Models\PolicyModel;
|
|
|
|
class PolicyController extends ResourceController
|
|
{
|
|
protected $PolicyModel;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->PolicyModel = new PolicyModel();
|
|
}
|
|
|
|
// List policies
|
|
public function policyList()
|
|
{
|
|
try {
|
|
$data = $this->PolicyModel->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);
|
|
}
|
|
}
|
|
|
|
// Find single policy
|
|
public function findPolicy()
|
|
{
|
|
try {
|
|
$id = $this->request->getGet('id');
|
|
$record = $this->PolicyModel->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, 'data' => $e->getMessage()], 500);
|
|
}
|
|
}
|
|
|
|
// Create policy
|
|
public function createPolicy()
|
|
{
|
|
try {
|
|
$data = $this->request->getPost();
|
|
$uploadPath = WRITEPATH . 'uploads/policy/';
|
|
|
|
$policyPdf = $this->request->getFile('policy_pdf_file_name');
|
|
$policyReceipt = $this->request->getFile('policy_payment_receipt_file_name');
|
|
|
|
$pdfFileName = null;
|
|
$receiptFileName = null;
|
|
|
|
// PDF Upload
|
|
if ($policyPdf && $policyPdf->isValid()) {
|
|
$pdfPath = $uploadPath . 'policy_pdf/';
|
|
if (!is_dir($pdfPath)) {
|
|
mkdir($pdfPath, 0777, true);
|
|
}
|
|
$pdfFileName = time() . '_' . $policyPdf->getRandomName();
|
|
$policyPdf->move($pdfPath, $pdfFileName);
|
|
}
|
|
|
|
// Receipt Upload
|
|
if ($policyReceipt && $policyReceipt->isValid()) {
|
|
$receiptPath = $uploadPath . 'policy_payment_receipt/';
|
|
if (!is_dir($receiptPath)) {
|
|
mkdir($receiptPath, 0777, true);
|
|
}
|
|
$receiptFileName = time() . '_' . $policyReceipt->getRandomName();
|
|
$policyReceipt->move($receiptPath, $receiptFileName);
|
|
}
|
|
|
|
$insertData = [
|
|
'quotation_id' => $data['quotation_id'],
|
|
'insurer_name' => $data['insurer_name'],
|
|
'premium_amount' => $data['premium_amount'],
|
|
'policy_number' => $data['policy_number'],
|
|
'policy_pdf_file_name' => $pdfFileName,
|
|
'policy_payment_receipt_file_name' => $receiptFileName,
|
|
'created_by' => $data['created_by'] ?? null
|
|
];
|
|
|
|
$this->PolicyModel->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);
|
|
}
|
|
}
|
|
|
|
// Update policy
|
|
public function updatePolicy()
|
|
{
|
|
try {
|
|
$data = $this->request->getPost();
|
|
if (!isset($data['id'])) {
|
|
return $this->respond(['status' => 'failed', 'code' => 200, 'data' => 'ID Required'], 200);
|
|
}
|
|
|
|
$id = $data['id'];
|
|
$policy = $this->PolicyModel->find($id);
|
|
if (!$policy) {
|
|
return $this->respond(['status' => 'failed', 'code' => 200, 'data' => 'Data Not Found'], 200);
|
|
}
|
|
|
|
$updateData = [
|
|
'quotation_id' => $data['quotation_id'] ?? $policy['quotation_id'],
|
|
'insurer_name' => $data['insurer_name'] ?? $policy['insurer_name'],
|
|
'premium_amount' => $data['premium_amount'] ?? $policy['premium_amount'],
|
|
'policy_number' => $data['policy_number'] ?? $policy['policy_number'],
|
|
'updated_by' => $data['updated_by'] ?? null
|
|
];
|
|
|
|
$uploadPath = WRITEPATH . 'uploads/policy/';
|
|
|
|
// PDF Update
|
|
$policyPdf = $this->request->getFile('policy_pdf_file_name');
|
|
if ($policyPdf && $policyPdf->isValid()) {
|
|
$pdfPath = $uploadPath . 'policy_pdf/';
|
|
if (!is_dir($pdfPath)) {
|
|
mkdir($pdfPath, 0777, true);
|
|
}
|
|
$pdfFileName = time() . '_' . $policyPdf->getRandomName();
|
|
$policyPdf->move($pdfPath, $pdfFileName);
|
|
$updateData['policy_pdf_file_name'] = $pdfFileName;
|
|
}
|
|
|
|
// Receipt Update
|
|
$policyReceipt = $this->request->getFile('policy_payment_receipt_file_name');
|
|
if ($policyReceipt && $policyReceipt->isValid()) {
|
|
$receiptPath = $uploadPath . 'policy_payment_receipt/';
|
|
if (!is_dir($receiptPath)) {
|
|
mkdir($receiptPath, 0777, true);
|
|
}
|
|
$receiptFileName = time() . '_' . $policyReceipt->getRandomName();
|
|
$policyReceipt->move($receiptPath, $receiptFileName);
|
|
$updateData['policy_payment_receipt_file_name'] = $receiptFileName;
|
|
}
|
|
|
|
$this->PolicyModel->update($id, $updateData);
|
|
|
|
return $this->respond(['status' => 'success', 'code' => 200, 'data' => []], 200);
|
|
|
|
} catch (\Exception $e) {
|
|
return $this->respond(['status' => 'failed', 'code' => 500, 'data' => $e->getMessage()], 500);
|
|
}
|
|
}
|
|
|
|
// Active / Deactive policy
|
|
public function toggleActiveStatus()
|
|
{
|
|
try {
|
|
$id = $this->request->getPost('id');
|
|
if (!$id) {
|
|
return $this->respond(['status' => 'failed', 'code' => 200, 'data' => 'ID Required'], 200);
|
|
}
|
|
|
|
$policy = $this->PolicyModel->find($id);
|
|
if (!$policy) {
|
|
return $this->respond(['status' => 'failed', 'code' => 200, 'data' => 'Data Not Found'], 200);
|
|
}
|
|
|
|
$newStatus = ($policy['is_active'] == 1) ? 0 : 1;
|
|
$this->PolicyModel->update($id, ['is_active' => $newStatus]);
|
|
|
|
return $this->respond(['status' => 'success', 'code' => 200, 'data' => ['is_active' => $newStatus]], 200);
|
|
|
|
} catch (\Exception $e) {
|
|
return $this->respond(['status' => 'failed', 'code' => 500, 'data' => $e->getMessage()], 500);
|
|
}
|
|
}
|
|
}
|