256 lines
11 KiB
PHP
256 lines
11 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
use CodeIgniter\RESTful\ResourceController;
|
|
use App\Models\PolicyModel;
|
|
use App\Models\EnquiryModel;
|
|
use App\Models\QuotationModel;
|
|
|
|
class PolicyController extends ResourceController
|
|
{
|
|
protected $PolicyModel;
|
|
protected $QuotationModel;
|
|
protected $EnquiryModel;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->PolicyModel = new PolicyModel();
|
|
$this->QuotationModel = new QuotationModel();
|
|
$this->EnquiryModel = new EnquiryModel();
|
|
}
|
|
|
|
// List policies
|
|
public function policyList()
|
|
{
|
|
try {
|
|
$manager_id = $this->request->getGet('manager_id');
|
|
|
|
$data = $this->PolicyModel ->where('manager_id',$manager_id)->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);
|
|
|
|
$record['issued_date'] = format_date_for_client($record['issued_date']);
|
|
$record['start_date'] = format_date_for_client($record['start_date']);
|
|
$record['end_date'] = format_date_for_client($record['end_date']);
|
|
|
|
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);
|
|
}
|
|
|
|
//fetch enquiry_id & agent_id
|
|
$quotData = $this->QuotationModel->select('partner_quotation.*,E.agent_id')
|
|
->join('partner_enquiry E', 'E.id = partner_quotation.enquiry_id', 'left')
|
|
->where('partner_quotation.id',$data['quotation_id'])
|
|
->first();
|
|
|
|
$insertData = [
|
|
'enquiry_id' => $quotData['enquiry_id'],
|
|
'quotation_id' => $data['quotation_id'],
|
|
'insured_name' => $data['insured_name'],
|
|
'issued_date' => format_date_for_database($data['issued_date']),
|
|
'start_date' => format_date_for_database($data['start_date']),
|
|
'end_date' => format_date_for_database($data['end_date']),
|
|
'premium_amount' => $data['premium_amount'],
|
|
'policy_number' => $data['policy_number'],
|
|
'payment_mode' => $data['payment_mode'],
|
|
'manager_id' => $data['manager_id'],
|
|
'agent_id' => $quotData['agent_id'],
|
|
'policy_pdf_file_name' => $pdfFileName,
|
|
'policy_payment_receipt_file_name' => $receiptFileName,
|
|
'created_by' => $data['created_by']
|
|
];
|
|
|
|
|
|
$this->PolicyModel->insert($insertData);
|
|
$policyId = $this->PolicyModel->getInsertID();
|
|
|
|
//update enquiry status
|
|
$quotationData = $this->QuotationModel->where('id',$data['quotation_id'])->first();
|
|
if (!empty($quotationData)){
|
|
$this->EnquiryModel->update($quotationData['enquiry_id'], ['status'=> 'Policy Created' ]);
|
|
}
|
|
|
|
// Call helper to create BDS record after creating client,clientPolicy,vehicle records
|
|
$policyData = $this->PolicyModel->select('partner_policy.*,E.insurer_id,E.insurer_branch_id,E.name as client_name,E.mobile as client_mobile,E.email as client_email,E.reg_no,E.vehicle_type_id')
|
|
->join('partner_quotation Q', 'Q.id = partner_policy.quotation_id', 'left')
|
|
->join('partner_enquiry E', 'E.id = partner_policy.enquiry_id', 'left')
|
|
->where('partner_policy.id',$policyId)
|
|
->first();
|
|
$bdsLogs = createBDS($policyData, $policyId);
|
|
if (!empty($bdsLogs)) {
|
|
foreach ($bdsLogs as $msg) {
|
|
log_message('info', '[BDS Entry] ' . $msg);
|
|
}
|
|
}
|
|
|
|
trigger_email('policy_created', ['policy_id' => $policyId]);
|
|
|
|
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'],
|
|
'insured_name' => $data['insured_name'] ?? $policy['insured_name'],
|
|
'issued_date' => format_date_for_database($data['issued_date']),
|
|
'start_date' => format_date_for_database($data['start_date']),
|
|
'end_date' => format_date_for_database($data['end_date']),
|
|
'premium_amount' => $data['premium_amount'] ?? $policy['premium_amount'],
|
|
'policy_number' => $data['policy_number'] ?? $policy['policy_number'],
|
|
'payment_mode' => $data['payment_mode'] ?? $policy['payment_mode'],
|
|
'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);
|
|
}
|
|
}
|
|
|
|
// Download policy file
|
|
public function downloadPolicyFile()
|
|
{
|
|
try {
|
|
$policyId = $this->request->getGet('policy_id');
|
|
$fileType = $this->request->getGet('file_type'); // policy_pdf , policy_payment_receipt
|
|
|
|
if (!$policyId || !$fileType) {
|
|
return $this->respond(['status' => 'failed','code' => 400,'data' => 'policy_id and file_type are required'], 200);
|
|
}
|
|
|
|
// Map file_type to DB column and folder
|
|
$fileMap = [
|
|
'policy_pdf' => ['column' => 'policy_pdf_file_name', 'folder' => 'policy_pdf'],
|
|
'policy_payment_receipt'=> ['column' => 'policy_payment_receipt_file_name', 'folder' => 'policy_payment_receipt'],
|
|
];
|
|
|
|
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->PolicyModel->where('is_active', 1)->find($policyId);
|
|
|
|
if (!$fileRecord || empty($fileRecord[$fileColumn])) {
|
|
return $this->respond([ 'status' => 'failed','code' => 404,'data' => 'File not found in database'], 200);
|
|
}
|
|
|
|
$filePath = WRITEPATH . "uploads/policy/{$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);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|