1261 lines
57 KiB
PHP
1261 lines
57 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;
|
|
|
|
protected $tier1VehicleTypeMap = [
|
|
|
|
/* ---------------- Two Wheeler ---------------- */
|
|
'two wheeler' => 'Two Wheeler',
|
|
'2 wheeler' => 'Two Wheeler',
|
|
'2w' => 'Two Wheeler',
|
|
'motorcycle' => 'Two Wheeler',
|
|
'bike' => 'Two Wheeler',
|
|
'scooter' => 'Two Wheeler',
|
|
'moped' => 'Two Wheeler',
|
|
|
|
/* ---------------- Four Wheeler ---------------- */
|
|
'four wheeler' => 'Four Wheeler',
|
|
'4 wheeler' => 'Four Wheeler',
|
|
'4w' => 'Four Wheeler',
|
|
'private car' => 'Four Wheeler',
|
|
'car' => 'Four Wheeler',
|
|
'jeep' => 'Four Wheeler',
|
|
|
|
/* ---------------- Goods Vehicle ---------------- */
|
|
'goods vehicle' => 'Goods Vehicle',
|
|
'good vehicle' => 'Goods Vehicle',
|
|
'goods carrier' => 'Goods Vehicle',
|
|
'goods carrying vehicle' => 'Goods Vehicle',
|
|
'cargo vehicle' => 'Goods Vehicle',
|
|
'lorry' => 'Goods Vehicle',
|
|
'truck' => 'Goods Vehicle',
|
|
'transport vehicle goods'=> 'Goods Vehicle',
|
|
|
|
/* ---------------- Commercial Vehicle ---------------- */
|
|
'commercial vehicle' => 'Commercial Vehicle',
|
|
'commercial car' => 'Commercial Vehicle',
|
|
'taxi' => 'Commercial Vehicle',
|
|
'cab' => 'Commercial Vehicle',
|
|
'hire vehicle' => 'Commercial Vehicle',
|
|
'passenger commercial' => 'Commercial Vehicle',
|
|
|
|
/* ---------------- Bus ---------------- */
|
|
'bus' => 'Bus',
|
|
'passenger bus' => 'Bus',
|
|
'school bus' => 'Bus',
|
|
'private bus' => 'Bus',
|
|
'stage carriage' => 'Bus',
|
|
|
|
/* ---------------- Tractor ---------------- */
|
|
'tractor' => 'Tractor',
|
|
'agricultural tractor' => 'Tractor',
|
|
'farm tractor' => 'Tractor',
|
|
];
|
|
|
|
protected $fuelType = [
|
|
'Petrol' => 'Petrol','Diesel' => 'Diesel','CNG' => 'CNG','LPG' => 'LPG',
|
|
];
|
|
|
|
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->select('partner_policy.* , ipti.insurance_plan_type,pq.insurer_id,,A.retention_rate as agent_retention_rate,S.retention_rate as manager_retention_rate')
|
|
->join('partner_quotation pq', 'pq.id = partner_policy.quotation_id', 'left')
|
|
->join('partner_insurance_plan_type_master ipti', 'ipti.id = pq.insurance_plan_type_id', 'left')
|
|
->join('partner_agent A', 'A.id = partner_policy.agent_id', 'left')
|
|
->join('partner_staff S', 'S.id = partner_policy.manager_id', 'left')
|
|
->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,
|
|
|
|
// newly added fields
|
|
'tp' => $data['tp'] ?? null,
|
|
'od' => $data['od'] ?? null,
|
|
'pa' => $data['pa'] ?? null,
|
|
'cgst' => $data['cgst'] ?? null,
|
|
'sgst' => $data['sgst'] ?? null,
|
|
'igst' => $data['igst'] ?? null,
|
|
'rto_state_code' => $data['rto_state_code'] ?? null,
|
|
'rto_city_code' => $data['rto_city_code'] ?? null,
|
|
'weight' => $data['weight'] ?? null,
|
|
'fuel_type' => $data['fuel_type'] ?? null,
|
|
'date_of_registration' => !empty($data['date_of_registration']) ? format_date_for_database($data['date_of_registration']) : null,
|
|
'year_of_manufacture' => $data['year_of_manufacture'] ?? null,
|
|
'engine_no' => $data['engine_no'] ?? null,
|
|
'chassis_no' => $data['chassis_no'] ?? null,
|
|
'make' => $data['make'] ?? null,
|
|
'model' => $data['model'] ?? null,
|
|
'cubic_capacity' => $data['cubic_capacity'] ?? null,
|
|
'vehicle_type' => $data['vehicle_type'] ?? null,
|
|
'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)){
|
|
$enquiryArray = ['vehicle_type_id' => $data['vehicle_type_id'] ?? 0 , 'broker_id' => $data['broker_id'] ?? 0 , 'status'=> 'Policy Created' , 'enquiry_status' => 'Completed' ];
|
|
$this->EnquiryModel->update($quotationData['enquiry_id'], $enquiryArray );
|
|
}
|
|
|
|
// Call helper to create BDS record after creating client,clientPolicy,vehicle records
|
|
$policyData = $this->PolicyModel->select('partner_policy.*,Q.insurer_id,Q.insurer_branch_id,partner_policy.insured_name as client_name,E.mobile as client_mobile,E.email as client_email,E.reg_no,E.vehicle_type_id,A.id as agent_id,A.agent_code')
|
|
->join('partner_quotation Q', 'Q.id = partner_policy.quotation_id AND Q.status = "Accepted"', 'left')
|
|
->join('partner_enquiry E', 'E.id = partner_policy.enquiry_id', 'left')
|
|
->join('partner_agent A', 'A.id = partner_policy.agent_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->getJSON(true);
|
|
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 = [
|
|
'rc_no' => $data['rc_no'] ?? $policy['rc_no'],
|
|
'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'],
|
|
'tp' => $data['tp'] ?? null,
|
|
'od' => $data['od'] ?? null,
|
|
'pa' => $data['pa'] ?? null,
|
|
'cgst' => $data['cgst'] ?? null,
|
|
'sgst' => $data['sgst'] ?? null,
|
|
'igst' => $data['igst'] ?? null,
|
|
'rto_state_code' => $data['rto_state_code'] ?? null,
|
|
'rto_city_code' => $data['rto_city_code'] ?? null,
|
|
'weight' => $data['weight'] ?? null,
|
|
'fuel_type' => $data['fuel_type'] ?? null,
|
|
'date_of_registration' => !empty($data['date_of_registration']) ? format_date_for_database($data['date_of_registration']) : null,
|
|
'year_of_manufacture' => $data['year_of_manufacture'] ?? null,
|
|
'engine_no' => $data['engine_no'] ?? null,
|
|
'chassis_no' => $data['chassis_no'] ?? null,
|
|
'make' => $data['make'] ?? null,
|
|
'model' => $data['model'] ?? null,
|
|
'cubic_capacity' => $data['cubic_capacity'] ?? null,
|
|
'vehicle_type' => $data['vehicle_type'] ?? null,
|
|
'broker_name' => $data['broker_name'] ?? null,
|
|
'commission_amount' => $data['commission_amount'] ?? null,
|
|
'updated_by' => $data['updated_by'] ?? null,
|
|
'is_data_accuracy_checked'=> 1
|
|
];
|
|
|
|
$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);
|
|
|
|
|
|
// Call helper to create BDS record after creating client,clientPolicy,vehicle records
|
|
$policyData = $this->PolicyModel->select('partner_policy.*,Q.insurer_id,Q.insurer_branch_id,partner_policy.insured_name as client_name,E.mobile as client_mobile,E.email as client_email,E.reg_no,E.vehicle_type_id,A.id as agent_id,A.agent_code,E.broker_id')
|
|
->join('partner_quotation Q', 'Q.id = partner_policy.quotation_id AND Q.status = "Accepted"', 'left')
|
|
->join('partner_enquiry E', 'E.id = partner_policy.enquiry_id', 'left')
|
|
->join('partner_agent A', 'A.id = partner_policy.agent_id', 'left')
|
|
->where('partner_policy.id',$id)
|
|
->first();
|
|
if($policyData['broker_id'] == 1) //only for nhance
|
|
{
|
|
$bdsLogs = createBDS($policyData, $id);
|
|
if (!empty($bdsLogs)) {
|
|
foreach ($bdsLogs as $msg) {
|
|
log_message('info', '[BDS Entry] ' . $msg);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
return $this->respond(['status' => 'success', 'code' => 200, 'data' => []], 200);
|
|
|
|
} catch (\Exception $e) {
|
|
return $this->respond(['status' => 'failed', 'code' => 500, 'data' => $e->getMessage()], 500);
|
|
}
|
|
}
|
|
|
|
public function uploadPolicyFile()
|
|
{
|
|
try {
|
|
|
|
$data = $this->request->getPost();
|
|
$uploadPath = WRITEPATH . 'uploads/policy/';
|
|
|
|
$policyPdf = $this->request->getFile('policy_pdf_file_name');
|
|
|
|
$pdfFileName = 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);
|
|
}
|
|
|
|
//fetch enquiry_id & agent_id
|
|
$quotData = $this->QuotationModel->select('partner_quotation.*,E.agent_id,E.name , VT.vehicle_type')
|
|
->join('partner_enquiry E', 'E.id = partner_quotation.enquiry_id', 'left')
|
|
->join('partner_agent A', 'A.id = E.agent_id', 'left')
|
|
->join('vehicle_type VT', 'VT.id = E.vehicle_type_id', 'left')
|
|
->where('partner_quotation.id',$data['quotation_id'])
|
|
->first();
|
|
|
|
if (!$quotData) {
|
|
return $this->respond(['status' => 'failed', 'code' => 404, 'message' => 'Quotation not found'], 404);
|
|
}
|
|
|
|
// ===== CHECK EXISTING POLICY =====
|
|
$policy = $this->PolicyModel
|
|
->where('enquiry_id', $quotData['enquiry_id'])
|
|
->first();
|
|
|
|
if ($policy)
|
|
{
|
|
|
|
$updateData = [
|
|
'policy_pdf_file_name' => $pdfFileName,
|
|
'updated_by' => $data['updated_by']
|
|
];
|
|
|
|
$this->PolicyModel->update($data['id'], $updateData);
|
|
$policyId = $data['id'];
|
|
|
|
Jobs::addJob(['job_name' => 'readFileAndCalculateCommission','payload' => ['policy_id' => $policyId]]);
|
|
log_message("info",'readFileAndCalculateCommission job pushed');
|
|
|
|
return $this->respond(['status' => 'success', 'code' => 200, 'data' => $policyId ], 200);
|
|
|
|
}
|
|
|
|
if (!isset($data['id'])) {
|
|
|
|
$insertData = [
|
|
'enquiry_id' => $quotData['enquiry_id'],
|
|
'quotation_id' => $data['quotation_id'],
|
|
'insured_name' => $quotData['name'],
|
|
'manager_id' => $data['manager_id'],
|
|
'agent_id' => $quotData['agent_id'],
|
|
'vehicle_type' => $quotData['vehicle_type'],
|
|
'policy_pdf_file_name' => $pdfFileName,
|
|
'created_by' => $data['created_by']
|
|
];
|
|
|
|
$policyId = $this->PolicyModel->insert($insertData);
|
|
|
|
//update enquiry status
|
|
$quotationData = $this->QuotationModel->where('id',$data['quotation_id'])->first();
|
|
if (!empty($quotationData)){
|
|
$enquiryArray = ['status'=> 'Policy Created' , 'enquiry_status' => 'Completed' ];
|
|
$this->EnquiryModel->update($quotationData['enquiry_id'], $enquiryArray );
|
|
}
|
|
|
|
Jobs::addJob(['job_name' => 'readFileAndCalculateCommission','payload' => ['policy_id' => $policyId]]);
|
|
log_message("info",'readFileAndCalculateCommission job pushed');
|
|
|
|
}else{
|
|
|
|
$updateData = [
|
|
'policy_pdf_file_name' => $pdfFileName,
|
|
'updated_by' => $data['updated_by']
|
|
];
|
|
|
|
$this->PolicyModel->update($data['id'], $updateData);
|
|
$policyId = $data['id'];
|
|
|
|
Jobs::addJob(['job_name' => 'readFileAndCalculateCommission','payload' => ['policy_id' => $policyId]]);
|
|
log_message("info",'readFileAndCalculateCommission job pushed');
|
|
|
|
}
|
|
|
|
|
|
return $this->respond(['status' => 'success', 'code' => 200, 'data' => $policyId ], 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);
|
|
}
|
|
}
|
|
|
|
public function PolicyFilePath()
|
|
{
|
|
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];
|
|
$publicUrl = base_url("uploads/policy/{$folder}/" . $fileRecord[$fileColumn]);
|
|
|
|
if (!file_exists($filePath)) {
|
|
return $this->respond(['status' => 'failed','code' => 404,'data' => 'File missing on server'], 200);
|
|
}
|
|
|
|
|
|
// return $this->respond(['status' => 'success', 'code' => 200, 'data' => $publicUrl ], 200);
|
|
|
|
// Detect the file mime type
|
|
$mime = mime_content_type($filePath);
|
|
|
|
// Stream file to browser / Flutter
|
|
return $this->response
|
|
->setHeader('Content-Type', $mime)
|
|
->setHeader('Content-Disposition', 'inline; filename="' . $fileRecord[$fileColumn] . '"')
|
|
->setBody(file_get_contents($filePath));
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
return $this->respond(['status' => 'failed','code' => 500,'message' => $e->getMessage() ], 500);
|
|
}
|
|
}
|
|
|
|
public function readFileAndCalculateCommission(array $data)
|
|
{
|
|
$policyId = $data['policy_id'];
|
|
|
|
log_message('debug', 'checkPolicyDoc() started for policy_id: '.$policyId);
|
|
|
|
$readDoc = $this->checkPolicyDoc($policyId);
|
|
|
|
if($readDoc['status'] == 'success')
|
|
{
|
|
|
|
$data = $readDoc['data'];
|
|
|
|
$vehicleType = $data['vehicle']['vehicle_type'] ?? null;
|
|
$fuelType = $data['vehicle']['fuel_type'] ?? null;
|
|
if($vehicleType !== null)
|
|
{
|
|
$vehicleType = $this->postProcessGeminiData($vehicleType,$this->tier1VehicleTypeMap);
|
|
}
|
|
else
|
|
{
|
|
$vehicleType = 'UN_IDEN_DOC';
|
|
}
|
|
if($fuelType !== null)
|
|
{
|
|
$fuelType = $this->postProcessGeminiData($fuelType,$this->fuelType);
|
|
}
|
|
else
|
|
{
|
|
$fuelType = 'UN_IDEN_DOC';
|
|
}
|
|
|
|
//update data in to policy table
|
|
$policyData['policy_number'] = $data['policy']['policy_number'] ?? null;
|
|
$policyData['issued_date'] = $data['policy']['issue_date'] ?? null;
|
|
$policyData['start_date'] = $data['policy']['period']['start'] ?? null;
|
|
$policyData['end_date'] = $data['policy']['period']['end'] ?? null;
|
|
$policyData['broker_name'] = $data['policy']['intermediary_name'] ?? null;
|
|
$policyData['tp'] = $data['premium']['tp'] ?? null;
|
|
$policyData['od'] = $data['premium']['od'] ?? null;
|
|
$policyData['pa'] = $data['premium']['pa'] ?? null;
|
|
$policyData['cgst'] = $data['premium']['taxes']['cgst'] ?? 0;
|
|
$policyData['sgst'] = $data['premium']['taxes']['sgst'] ?? 0;
|
|
$policyData['igst'] = $data['premium']['taxes']['igst'] ?? 0;
|
|
$policyData['premium_amount'] = $data['premium']['total'] ?? null;
|
|
$policyData['rto_state_code'] = $data['vehicle']['rto_state_code'] ?? null;
|
|
$policyData['rto_city_code'] = $data['vehicle']['rto_city_code'] ?? null;
|
|
$policyData['weight'] = $data['vehicle']['weight'] ?? null;
|
|
$policyData['fuel_type'] = $fuelType;
|
|
$policyData['date_of_registration'] = $data['vehicle']['date_of_registration'] ?? null;
|
|
$policyData['year_of_manufacture'] = $data['vehicle']['year_of_manufacture'] ?? null;
|
|
$policyData['engine_no'] = $data['vehicle']['engine_no'] ?? null;
|
|
$policyData['chassis_no'] = $data['vehicle']['chassis_no'] ?? null;
|
|
$policyData['make'] = $data['vehicle']['make'] ?? null;
|
|
$policyData['model'] = $data['vehicle']['model'] ?? null;
|
|
$policyData['cubic_capacity'] = $data['vehicle']['cubic_capacity'] ?? null;
|
|
// $policyData['vehicle_type'] = $vehicleType;
|
|
$policyData['rc_no'] = $data['vehicle']['reg_no'] ?? null;
|
|
$policyData['insured_name'] = $data['insured']['name'] ?? null;
|
|
|
|
|
|
|
|
log_message('debug', 'Policy Update Payload: ' . json_encode($policyData));
|
|
|
|
$updateStatus = $this->PolicyModel->update($policyId,$policyData);
|
|
|
|
if ($updateStatus)
|
|
{
|
|
log_message('debug', 'Policy updated successfully for ID: '.$policyId);
|
|
|
|
$calculateCommission = $this->calculateCommission($policyId);
|
|
|
|
// if($calculateCommission['status'] == 'success')
|
|
// {
|
|
// log_message('debug', 'BDS record creating started for ID: '.$policyId);
|
|
|
|
// // Call helper to create BDS record after creating client,clientPolicy,vehicle records
|
|
// $policyData = $this->PolicyModel->select('partner_policy.*,Q.insurer_id,Q.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,A.id as agent_id,A.agent_code')
|
|
// ->join('partner_quotation Q', 'Q.id = partner_policy.quotation_id AND Q.status = "Accepted"', 'left')
|
|
// ->join('partner_enquiry E', 'E.id = partner_policy.enquiry_id', 'left')
|
|
// ->join('partner_agent A', 'A.id = partner_policy.agent_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);
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
} else {
|
|
log_message('error', 'Failed to update policy for ID: '.$policyId);
|
|
}
|
|
} else {
|
|
log_message('error', 'checkPolicyDoc returned failure: ' . json_encode($readDoc));
|
|
}
|
|
|
|
|
|
return ['checkPolicyDoc' => $readDoc['status'] , 'calculateCommission' => $calculateCommission['status'] ?? 'failed' ];
|
|
}
|
|
|
|
public function checkPolicyDoc($policyId = null , $return = null)
|
|
{
|
|
|
|
try
|
|
{
|
|
|
|
|
|
// Replace with your actual Gemini API key
|
|
$apiKey = getenv('GEMINI_API_KEY');
|
|
// The model to use and the API endpoint
|
|
$model = "gemini-pro";
|
|
$model = getenv('GEMINI_MODEL');
|
|
// $model = "gemini-1.5-pro";
|
|
$url = "https://generativelanguage.googleapis.com/v1beta/models/{$model}:generateContent?key={$apiKey}";
|
|
|
|
$record = $this->PolicyModel->find($policyId);
|
|
$uploadedPath = WRITEPATH . 'uploads/policy/policy_pdf/';
|
|
$filePath = $uploadedPath.$record['policy_pdf_file_name'];
|
|
// dd($filePath);
|
|
// Check if the file exists
|
|
if (!file_exists($filePath)) {
|
|
log_message('info',"Error: File not found at {$filePath}");
|
|
if($return == true)
|
|
{
|
|
return $this->respond(['status'=>"failed", 'message'=> "File not found at {$filePath}"], 200);
|
|
}else{
|
|
return ['status'=>"failed", 'message'=> "File not found at {$filePath}"];
|
|
}
|
|
}
|
|
|
|
// Get the file's MIME type using the finfo extension
|
|
$finfo = finfo_open(FILEINFO_MIME_TYPE);
|
|
$mimeType = finfo_file($finfo, $filePath);
|
|
finfo_close($finfo);
|
|
|
|
// Define supported inline MIME types
|
|
$supportedInlineMimeTypes = ['application/pdf','text/csv'];
|
|
|
|
// Read the file content and encode it to Base64
|
|
$fileContent = file_get_contents($filePath);
|
|
$base64Content = base64_encode($fileContent);
|
|
// The prompt you want to send to the model
|
|
// $prompt = "give me a json with emp data like name,age,dob,mobile and email. only json not any explanations";
|
|
$prompt = "Read the following motor policy pdf document and convert into JSON format as sample specified. Give me only JSON,not any explanations.while pick up premium break up give own damage, third party ,personal accident and taxes as mentioned in JSON format.Notes:vehicle_type should be like Two Wheeler,Four Feeler,Good Vehicle,Bus, Tractor,Commercial Vehicle.rto_state_code is first two char of reg_no and rto_city_code 3rd & 4th char of reg_no and it should be two digit numeric code. Any date should be in mysql date format";
|
|
$prompt .= '"{\"policy\":{\"intermediary_name\":\"\",\"policy_number\":\"\",\"issue_date\":\"\",\"period\":{\"start\":\"\",\"end\":\"\"},\"insurer\":\"\",\"previous_policy_number\":\"\"},\"insured\":{\"name\":\"\",\"father_name\":\"\",\"address\":[\"\"],\"mobile\":\"\",\"id_proofs\":{\"aadhaar\":\"\",\"pan\":\"\"}},\"vehicle\":{\"reg_no\":\"\",\"rto_state_code\":\"\",\"rto_city_code\":\"\",\"weight\":\"\",\"fuel_type\":\"\",\"date_of_registration\":\"\",\"year_of_manufacture\":\"\",\"engine_no\":\"\",\"chassis_no\":\"\",\"make\":\"\",\"model\":\"\",\"year\":\"\",\"cubic_capacity\":\"\",\"vehicle_type\":\"\"},\"rto\":\"\",\"premium\":{\"tp\":0,\"od\":0,\"pa\":0,\"taxes\":{\"cgst\":\"\",\"sgst\":\"\",\"igst\":\"\"},\"total\":0,\"in_words\":\"\"},\"endorsements\":[{\"code\":\"\",\"desc\":\"\"}]}"';
|
|
|
|
$payloadPart = null;
|
|
|
|
// The text part of the prompt
|
|
$text_part = [
|
|
"text" => $prompt
|
|
];
|
|
// Conditionally handle the file upload based on MIME type
|
|
if (in_array($mimeType, $supportedInlineMimeTypes)) {
|
|
// Handle PDF as inline data
|
|
$fileContent = file_get_contents($filePath);
|
|
$base64Content = base64_encode($fileContent);
|
|
$payloadPart = [
|
|
"inlineData" => [
|
|
"mimeType" => $mimeType,
|
|
"data" => $base64Content
|
|
]
|
|
];
|
|
|
|
$content_parts = [
|
|
"parts" => [
|
|
$text_part,
|
|
$payloadPart
|
|
]
|
|
];
|
|
}
|
|
// else {
|
|
// // Handle Excel/CSV using the Files API
|
|
// // echo "Detected unsupported inline MIME type ({$mimeType}). Uploading via Files API...\n";
|
|
// $fileInfo = $this->uploadFileToGemini($filePath, $apiKey); // Pass apiKey
|
|
|
|
// // print_r($fileInfo);
|
|
// // The file part, using the URI from the Files API upload
|
|
// $file_part = [
|
|
// "fileData" => [
|
|
// "fileUri" => $fileInfo['uri'],
|
|
// "mimeType" => $fileInfo['mimeType']
|
|
// ]
|
|
// ];
|
|
|
|
// // The full content array, containing both parts
|
|
// $content_parts = [
|
|
// "parts" => [
|
|
// $text_part,
|
|
// $file_part
|
|
// ]
|
|
// ];
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// The final JSON payload
|
|
$data = [
|
|
"contents" => [
|
|
$content_parts
|
|
]
|
|
];
|
|
|
|
// Encode the data to a JSON string
|
|
$json_data = json_encode($data);
|
|
|
|
// Initialize cURL
|
|
$ch = curl_init($url);
|
|
|
|
// Set cURL options
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); // Set the Content-Type header
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data); // Set the JSON payload
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the response as a string
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Optional: to bypass SSL verification if needed (not recommended for production)
|
|
|
|
// Execute the cURL request and get the response
|
|
$response = curl_exec($ch);
|
|
|
|
// Check for cURL errors
|
|
if (curl_errno($ch)) {
|
|
$curl_error = curl_error($ch);
|
|
log_message('info',"cURL Error: " . $curl_error);
|
|
if($return == true)
|
|
{
|
|
return $this->respond(['status'=>"failed", 'message'=> "cURL Error: " . $curl_error], 200);
|
|
}else{
|
|
return ['status'=>"failed", 'message'=> "cURL Error: " . $curl_error];
|
|
}
|
|
}
|
|
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
|
// Close the cURL handle
|
|
curl_close($ch);
|
|
$ch = null; // Mark handle as closed
|
|
// Check for non-successful HTTP status codes
|
|
if ($http_code < 200 || $http_code >= 300) {
|
|
log_message('info',"API returned non-successful HTTP status code: $http_code. Response: " . substr($response, 0, 200));
|
|
if($return == true)
|
|
{
|
|
return $this->respond(['status'=>"failed", 'message'=> "API returned non-successful HTTP status code: $http_code. Response: " . substr($response, 0, 200)], 200);
|
|
}else{
|
|
return ['status'=>"failed", 'message'=> "API returned non-successful HTTP status code: $http_code. Response: " . substr($response, 0, 200)];
|
|
}
|
|
}
|
|
|
|
// Decode the JSON response
|
|
$responseData = json_decode($response, true, 512, JSON_THROW_ON_ERROR);
|
|
$text_path = $responseData['candidates'][0]['content']['parts'][0]['text'] ?? null;
|
|
|
|
if ($text_path) {
|
|
$generatedText = $text_path;
|
|
log_message('info', "Successfully received generated text.");
|
|
|
|
// 9. Attempt to extract and decode the exact JSON from the generated text
|
|
$json_string = '';
|
|
|
|
// Preferred: Regex to find content inside a JSON markdown block: ```json\n(.*?)\n```
|
|
$extracted_json_match = [];
|
|
if (preg_match('/```json\s*(.*?)\s*```/s', $generatedText, $extracted_json_match)) {
|
|
$json_string = trim($extracted_json_match[1]);
|
|
} else {
|
|
// Fallback: Find the first '{' and the last '}'
|
|
$start = strpos($generatedText, '{');
|
|
$end = strrpos($generatedText, '}');
|
|
if ($start !== false && $end !== false && $end > $start) {
|
|
$json_string = substr($generatedText, $start, $end - $start + 1);
|
|
}
|
|
}
|
|
|
|
if (empty($json_string)) {
|
|
log_message('info',"Could not extract a valid JSON string from the generated text.");
|
|
if($return == true)
|
|
{
|
|
return $this->respond(['status'=>"failed", 'message'=> "Could not extract a valid JSON string from the generated text."], 200);
|
|
}else{
|
|
return ['status'=>"failed", 'message'=> "Could not extract a valid JSON string from the generated text."];
|
|
}
|
|
}
|
|
|
|
// Decode the extracted JSON string
|
|
$final_data = json_decode($json_string, true, 512, JSON_THROW_ON_ERROR);
|
|
|
|
log_message('info', "Extracted and decoded final JSON data successfully.");
|
|
|
|
if($return == true)
|
|
{
|
|
return $this->respond(['status'=>"success", 'message'=> "Doc read sucess" , 'data'=>$final_data], 200);
|
|
}else{
|
|
return ['status'=>"success", 'message'=> "Doc read sucess" , 'data'=>$final_data];
|
|
}
|
|
|
|
|
|
} else {
|
|
log_message('info',"Response structure invalid or no generated text candidate found.");
|
|
if($return == true)
|
|
{
|
|
return $this->respond(['status'=>"failed", 'message'=> "Response structure invalid or no generated text candidate found"], 200);
|
|
}else{
|
|
return ['status'=>"failed", 'message'=> "Response structure invalid or no generated text candidate found"];
|
|
}
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
// if ($ch !== null) {
|
|
// curl_close($ch);
|
|
// }
|
|
log_message('error', "Fatal Error: " . $e->getMessage());
|
|
if($return == true)
|
|
{
|
|
return $this->respond(['status'=>"failed", 'message'=> "Error: " . $e->getMessage()], 200);
|
|
}else{
|
|
return ['status'=>"failed", 'message'=> "Error: " . $e->getMessage()];
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public function calculateCommission($policyId = null , $return = null)
|
|
{
|
|
log_message('debug', 'calculateCommission() started with policyId: ' . $policyId);
|
|
|
|
$record = $this->PolicyModel->select('partner_policy.* , rto_state.rto_state_name as rto_state_code ,pe.insurer_id , VT.vehicle_type as vehicle_type_master , ipti.insurance_plan_type,A.retention_rate as agent_retention_rate,S.retention_rate as manager_retention_rate')
|
|
->join('partner_enquiry pe', 'pe.id = partner_policy.enquiry_id', 'left')
|
|
->join('vehicle_type VT', 'VT.id = pe.vehicle_type_id', 'left')
|
|
->join('partner_quotation pq', 'pq.id = partner_policy.quotation_id', 'left')
|
|
->join('partner_insurance_plan_type_master ipti', 'ipti.id = pq.insurance_plan_type_id', 'left')
|
|
->join('rto_master rto_state', 'rto_state.rto_state = partner_policy.rto_state_code', 'left')
|
|
// ->join('rto_master rto_city', 'rto_city.rto_code = partner_policy.rto_city_code', 'left')
|
|
->join('partner_agent A', 'A.id = partner_policy.agent_id', 'left')
|
|
->join('partner_staff S', 'S.id = partner_policy.manager_id', 'left')
|
|
->where('partner_policy.id', $policyId)
|
|
->first();
|
|
|
|
$apiUrl = getenv('COMMISSION_CALCULATION_URL');
|
|
|
|
$token = getenv('COMMISSION_CALCULATION_TOKEN');
|
|
|
|
$manufactureYear = (int) $record['year_of_manufacture'];
|
|
$currentYear = (int) date("Y");
|
|
$vehicleAge = $currentYear - $manufactureYear;
|
|
|
|
$postData = [
|
|
"department" => "Motor",
|
|
"policy_business_type" => "retail",
|
|
"product" => "",
|
|
"renewal_type" => "fresh",
|
|
"renewal_sub_type" => "fresh",
|
|
"vehicle_type" => $record['vehicle_type'],
|
|
"policy_type" => $record['insurance_plan_type'],
|
|
"premium" => $record['premium_amount'],
|
|
"od_premium" => $record['od'],
|
|
"tp_premium" => $record['tp'],
|
|
"cubic_capacity" => $record['cubic_capacity'],
|
|
"weight" => $record['weight'],
|
|
"make" => $record['make'],
|
|
"model" => $record['model'],
|
|
"manufacture_year" => $record['year_of_manufacture'],
|
|
"vehicle_age" => $vehicleAge,
|
|
"date_of_registration" => $record['date_of_registration'],
|
|
"fuel_type" => $record['fuel_type'],
|
|
"geo_rto_state" => $record['rto_state_code'],
|
|
"geo_rto_city" => $record['rto_city_code'],
|
|
"policy_issue_date" => $record['issued_date'],
|
|
"insurer_id" => $record['insurer_id']
|
|
];
|
|
|
|
log_message('debug', 'API Request Payload: ' . json_encode($postData));
|
|
|
|
$ch = curl_init($apiUrl);
|
|
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
// Headers
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
"Content-Type: application/json",
|
|
"Authorization: Bearer " . $token
|
|
]);
|
|
|
|
// POST Data
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
|
|
|
|
$response = curl_exec($ch);
|
|
$error = curl_error($ch);
|
|
|
|
curl_close($ch);
|
|
|
|
if ($error)
|
|
{
|
|
log_message('error', 'cURL Error: ' . $error);
|
|
if($return == true)
|
|
{
|
|
return $this->respond( ['status'=>"failed", 'message'=> 'cURL Error: ' . $error], 200);
|
|
}else{
|
|
return ['status'=>"failed", 'message'=> 'cURL Error: ' . $error];
|
|
}
|
|
|
|
} else {
|
|
|
|
log_message('debug', 'API Response: ' . $response);
|
|
|
|
$result = json_decode($response, true);
|
|
|
|
// Optional: Debug API result
|
|
// print_r($result);
|
|
|
|
if($return == true)
|
|
{
|
|
return $this->respond(['status' => 'success', 'code' => 200, 'data' => $result , 'payload' => $postData ], 200);
|
|
}
|
|
|
|
if (!empty($result) && isset($result['success']) && $result['success'] === true) {
|
|
|
|
|
|
|
|
$actualCommission = $result['data']['payout'];
|
|
$agentRetention = $record['agent_retention_rate'];
|
|
$managerRetention = $record['manager_retention_rate'];
|
|
$commissionDeduction = $actualCommission * ($agentRetention+$managerRetention) / 100;
|
|
$commission = $actualCommission - $commissionDeduction;
|
|
|
|
|
|
// Safely extract values
|
|
$policyData = [
|
|
'commission_from_insurer' => $result['data']['payout'] ?? null,
|
|
'agent_retention_rate' => $agentRetention,
|
|
'manager_retention_rate' => $managerRetention,
|
|
'commission_amount' => $commission,
|
|
'commission_applied_rule' => $result['data']['rule']['id'] ?? null,
|
|
];
|
|
|
|
|
|
log_message('debug', 'Updating Policy (ID=75) With: ' . json_encode($policyData));
|
|
|
|
if($return == true)
|
|
{
|
|
return $this->respond(['status' => 'success', 'code' => 200, 'data' => $policyData ], 200);
|
|
}else{
|
|
// Update record
|
|
$this->PolicyModel->update($policyId, $policyData);
|
|
log_message('debug', 'Policy update successful!');
|
|
return ['status'=>"success", 'message'=> "Commission Updated!"];
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
log_message('debug', 'calculateCommission() - Invalid API Response');
|
|
|
|
if($return == true)
|
|
{
|
|
return $this->respond( ['status'=>"failed", 'message'=> "Invalid API Response!"], 200);
|
|
}else{
|
|
return ['status'=>"failed", 'message'=> "Invalid API Response!"];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public function policyCommissionCalculationAndGeminiPolicyRead()
|
|
{
|
|
|
|
$type = $this->request->getGet('type');
|
|
$policyId = $this->request->getGet('policy_id');
|
|
|
|
if($type == 'read')
|
|
{
|
|
$readDoc = $this->checkPolicyDoc($policyId , false);
|
|
if($readDoc['status'] == 'success')
|
|
{
|
|
|
|
$data = $readDoc['data'];
|
|
|
|
$vehicleType = $data['vehicle']['vehicle_type'] ?? null;
|
|
$fuelType = $data['vehicle']['fuel_type'] ?? null;
|
|
if($vehicleType !== null)
|
|
{
|
|
$vehicleType = $this->postProcessGeminiData($vehicleType,$this->tier1VehicleTypeMap);
|
|
}
|
|
else
|
|
{
|
|
$vehicleType = 'UN_IDEN_DOC';
|
|
}
|
|
if($fuelType !== null)
|
|
{
|
|
$fuelType = $this->postProcessGeminiData($fuelType,$this->fuelType);
|
|
}
|
|
else
|
|
{
|
|
$fuelType = 'UN_IDEN_DOC';
|
|
}
|
|
|
|
//update data in to policy table
|
|
$policyData['policy_number'] = $data['policy']['policy_number'] ?? null;
|
|
$policyData['issued_date'] = $data['policy']['issue_date'] ?? null;
|
|
$policyData['start_date'] = $data['policy']['period']['start'] ?? null;
|
|
$policyData['end_date'] = $data['policy']['period']['end'] ?? null;
|
|
$policyData['broker_name'] = $data['policy']['intermediary_name'] ?? null;
|
|
$policyData['tp'] = $data['premium']['tp'] ?? null;
|
|
$policyData['od'] = $data['premium']['od'] ?? null;
|
|
$policyData['pa'] = $data['premium']['pa'] ?? null;
|
|
$policyData['cgst'] = $data['premium']['taxes']['cgst'] ?? 0;
|
|
$policyData['sgst'] = $data['premium']['taxes']['sgst'] ?? 0;
|
|
$policyData['igst'] = $data['premium']['taxes']['igst'] ?? 0;
|
|
$policyData['premium_amount'] = $data['premium']['total'] ?? null;
|
|
$policyData['rto_state_code'] = $data['vehicle']['rto_state_code'] ?? null;
|
|
$policyData['rto_city_code'] = $data['vehicle']['rto_city_code'] ?? null;
|
|
$policyData['weight'] = $data['vehicle']['weight'] ?? null;
|
|
$policyData['fuel_type'] = $fuelType;
|
|
$policyData['date_of_registration'] = $data['vehicle']['date_of_registration'] ?? null;
|
|
$policyData['year_of_manufacture'] = $data['vehicle']['year_of_manufacture'] ?? null;
|
|
$policyData['engine_no'] = $data['vehicle']['engine_no'] ?? null;
|
|
$policyData['chassis_no'] = $data['vehicle']['chassis_no'] ?? null;
|
|
$policyData['make'] = $data['vehicle']['make'] ?? null;
|
|
$policyData['model'] = $data['vehicle']['model'] ?? null;
|
|
$policyData['cubic_capacity'] = $data['vehicle']['cubic_capacity'] ?? null;
|
|
// $policyData['vehicle_type'] = $vehicleType;
|
|
$policyData['rc_no'] = $data['vehicle']['reg_no'] ?? null;
|
|
$policyData['insured_name'] = $data['insured']['name'] ?? null;
|
|
|
|
log_message('debug', 'Policy Update Payload: ' . json_encode($policyData));
|
|
|
|
$updateStatus = $this->PolicyModel->update($policyId,$policyData);
|
|
|
|
} else {
|
|
log_message('error', 'checkPolicyDoc returned failure: ' . json_encode($readDoc));
|
|
}
|
|
|
|
return $this->respond(['status'=>$readDoc['status'], 'data'=> $readDoc ], 200);
|
|
|
|
|
|
}else if($type == 'commission')
|
|
{
|
|
return $this->calculateCommission($policyId , true);
|
|
}
|
|
|
|
}
|
|
|
|
public function searchThePolicies()
|
|
{
|
|
|
|
try{
|
|
|
|
$policy_number = $this->request->getGet('policy_number');
|
|
$vehicle_no = $this->request->getGet('vehicle_number');
|
|
if ($policy_number == '' && $vehicle_no == '') {
|
|
return $this->respond([
|
|
'status' => 'failed',
|
|
'code' => 200,
|
|
'data' => 'Value Required'
|
|
], 200);
|
|
}
|
|
|
|
$builder = $this->PolicyModel->select('partner_policy.*');
|
|
$builder->join('partner_enquiry pe', 'partner_policy.enquiry_id = pe.id', 'left');
|
|
|
|
if ($policy_number) {
|
|
$builder->like('partner_policy.policy_number', $policy_number);
|
|
} elseif ($vehicle_no) {
|
|
$builder->like('pe.reg_no', $vehicle_no);
|
|
} else {
|
|
$builder->groupStart()
|
|
->like('partner_policy.policy_number', $policy_number)
|
|
->orLike('pe.reg_no', $vehicle_no)
|
|
->groupEnd();
|
|
}
|
|
$result = $builder->get()->getResultArray();
|
|
if (!$result) {
|
|
return $this->respond(['status' => 'failed', 'code' => 200, 'data' => 'No Data Found'], 200);
|
|
}
|
|
|
|
return $this->respond(['status' => 'success', 'code' => 200, 'data' => $result], 200);
|
|
|
|
} catch (\Exception $e) {
|
|
return $this->respond(['status' => 'failed', 'code' => 500, 'data' => $e->getMessage()], 500);
|
|
}
|
|
}
|
|
|
|
public function postProcessGeminiData(string $incoming, array $masterArray): string
|
|
{
|
|
|
|
$value = strtolower(trim($incoming));
|
|
$value = preg_replace('/\s+/', ' ', $value); // normalize spaces
|
|
|
|
return $masterArray[$value] ?? 'UN_IDEN_TYPE';
|
|
}
|
|
|
|
public function calculateCommissionRequest()
|
|
{
|
|
$postRequest = $this->request->getJSON(true);
|
|
|
|
$apiUrl = getenv('COMMISSION_CALCULATION_URL');
|
|
|
|
$token = getenv('COMMISSION_CALCULATION_TOKEN');
|
|
|
|
$manufactureYear = (int) $postRequest['year_of_manufacture'];
|
|
$currentYear = (int) date("Y");
|
|
$vehicleAge = $currentYear - $manufactureYear;
|
|
|
|
$postData = [
|
|
"department" => "Motor",
|
|
"policy_business_type" => "retail",
|
|
"product" => "",
|
|
"renewal_type" => "fresh",
|
|
"renewal_sub_type" => "fresh",
|
|
"vehicle_type" => $postRequest['vehicle_type'],
|
|
"policy_type" => $postRequest['insurance_plan_type'],
|
|
"premium" => $postRequest['premium_amount'],
|
|
"od_premium" => $postRequest['od'],
|
|
"tp_premium" => $postRequest['tp'],
|
|
"cubic_capacity" => $postRequest['cubic_capacity'],
|
|
"weight" => $postRequest['weight'],
|
|
"make" => $postRequest['make'],
|
|
"model" => $postRequest['model'],
|
|
"manufacture_year" => $postRequest['year_of_manufacture'],
|
|
"vehicle_age" => $vehicleAge,
|
|
"date_of_registration" => $postRequest['date_of_registration'],
|
|
"fuel_type" => $postRequest['fuel_type'],
|
|
"geo_rto_state" => $postRequest['rto_state_code'],
|
|
"geo_rto_city" => $postRequest['rto_city_code'],
|
|
"policy_issue_date" => $postRequest['issued_date'],
|
|
"insurer_id" => $postRequest['insurer_id']
|
|
];
|
|
|
|
log_message('debug', 'API Request Payload: ' . json_encode($postData));
|
|
|
|
$ch = curl_init($apiUrl);
|
|
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
// Headers
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
"Content-Type: application/json",
|
|
"Authorization: Bearer " . $token
|
|
]);
|
|
|
|
// POST Data
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
|
|
|
|
$response = curl_exec($ch);
|
|
$error = curl_error($ch);
|
|
|
|
curl_close($ch);
|
|
|
|
if ($error)
|
|
{
|
|
log_message('error', 'cURL Error: ' . $error);
|
|
|
|
return $this->respond( ['status'=>"failed", 'message'=> 'cURL Error: ' . $error], 200);
|
|
|
|
} else {
|
|
|
|
log_message('debug', 'API Response: ' . $response);
|
|
|
|
$result = json_decode($response, true);
|
|
|
|
|
|
if (!empty($result) && isset($result['success']) && $result['success'] === true) {
|
|
|
|
$actualCommission = $result['data']['payout'];
|
|
$agentRetention = $postRequest['agent_retention_rate'];
|
|
$managerRetention = $postRequest['manager_retention_rate'];
|
|
$commissionDeduction = $actualCommission * ($agentRetention+$managerRetention) / 100;
|
|
$commission = $actualCommission - $commissionDeduction;
|
|
|
|
|
|
// Safely extract values
|
|
$policyData = [
|
|
'commission_from_insurer' => $result['data']['payout'] ?? null,
|
|
'agent_retention_rate' => $agentRetention,
|
|
'manager_retention_rate' => $managerRetention,
|
|
'commission_amount' => $commission,
|
|
'commission_applied_rule' => $result['data']['rule']['id'] ?? null,
|
|
];
|
|
|
|
|
|
log_message('debug', 'Updating Policy (ID=75) With: ' . json_encode($policyData));
|
|
|
|
return $this->respond(['status' => 'success', 'code' => 200, 'data' => $policyData ], 200);
|
|
|
|
} else {
|
|
log_message('debug', 'calculateCommission() - Invalid API Response');
|
|
|
|
return $this->respond( ['status'=>"failed", 'message'=> "Invalid API Response!"], 200);
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
public function vehicleTypeMaster()
|
|
{
|
|
// Get only values & make unique
|
|
$vehicleTypes = array_values(array_unique($this->tier1VehicleTypeMap));
|
|
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'data' => $vehicleTypes
|
|
], 200);
|
|
}
|
|
|
|
public function fuelTypeMaster()
|
|
{
|
|
$fuelTypes = array_values(array_unique($this->fuelType));
|
|
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'data' => $fuelTypes
|
|
], 200);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|