nhance/app/Controllers/PolicyTransactionController.php

1017 lines
43 KiB
PHP

<?php
namespace App\Controllers;
use App\Controllers\BaseController;
use App\Helpers\DepositHelper;
use CodeIgniter\API\ResponseTrait;
use App\Models\PolicyTransactionModel;
use App\Models\PolicyTransactionStatusModel;
use App\Models\ClientModel;
use App\Models\ClientBranchModel;
use App\Models\ClientDepositModel;
use App\Models\ClientPolicyModel;
use App\Models\PolicyTypeModel;
use App\Models\InsurerBranchModel;
use App\Models\InsurerModel;
use App\Models\KYCEntityTypeModel;
use App\Models\ClientKYCDocsModel;
use App\Models\TPABranchModel;
use App\Models\TPAModel;
use App\Models\PTFileModel;
use App\Models\PTCOShareDetailsModel;
use App\Models\EmployeeModel;
use App\Models\UserTeamsModel;
use App\Models\UserModel;
use App\Models\EmployeePolicyModel;
class PolicyTransactionController extends BaseController
{
use ResponseTrait;
protected $myLogger;
protected $policyTransactionModel;
protected $policyTransactionStatusModel;
protected $clientModel;
protected $clientPolicyModel;
protected $clientBranchModel;
protected $clientDepositModel;
protected $policyTypeModel;
protected $insurerBranchModel;
protected $insurerModel;
protected $kycEntityTypeModel;
protected $clientKYCDocsModel;
protected $tpaBranchModel;
protected $tpaModel;
protected $PTFileModel;
protected $PTCOShareDetailsModel;
protected $employeeModel;
protected $userTeamsModel;
protected $userModel;
protected $employeePolicyModel;
public function __construct()
{
set_session_context('Policy Tranction');
$this->myLogger = \Config\Services::mylogger();
$this->clientModel = new ClientModel();
$this->clientBranchModel = new ClientBranchModel();
$this->policyTypeModel = new PolicyTypeModel();
$this->clientDepositModel = new ClientDepositModel();
$this->clientPolicyModel = new ClientPolicyModel();
$this->insurerBranchModel = new InsurerBranchModel();
$this->insurerModel = new InsurerModel();
$this->policyTransactionModel = new PolicyTransactionModel();
$this->policyTransactionStatusModel = new PolicyTransactionStatusModel();
$this->kycEntityTypeModel = new KYCEntityTypeModel();
$this->clientKYCDocsModel = new ClientKYCDocsModel();
$this->tpaBranchModel = new TPABranchModel();
$this->tpaModel = new TPAModel();
$this->PTFileModel = new PTFileModel();
$this->PTCOShareDetailsModel = new PTCOShareDetailsModel();
$this->employeeModel = new EmployeeModel();
$this->userTeamsModel = new UserTeamsModel();
$this->userModel = new UserModel();
$this->employeePolicyModel = new EmployeePolicyModel();
}
// Policy Transaction Inception
public function viewInception()
{
$data['issuer'] = [1 => 'JIBS', 2 => 'Nhance'];
$data['client_type'] = [1 => 'Group', 2 => 'Individual'];
$data['issuing_type'] = [1 => 'Fresh', 2 => 'Renewal', 3 => 'Roll Over'];
// $data['policy_status'] = [
// 'pending' => 'Pending',
// 'exported_to_insurer' => 'Exported to Insurer',
// 'imported_from_insurer' => 'Imported from Insurer',
// 'exported_to_tpa' => 'Exported to TPA',
// 'imported_from_tpa' => 'Imported from TPA',
// 'completed' => 'Completed'
// ];
$data['policy_status'] = [
'under_process' => 'Under Process',
'client_pending' => 'Client Pending',
'insurer_pending' => 'Insurer Pending',
'co_insurer_pending' => 'Co-Insurer Pending',
'tpa_pending' => 'TPA Pending',
'validated' => 'Validated',
'cancelled' => 'Cancelled',
'instalment_pending' => 'Instalment Pending',
'completed' => 'Completed',
];
$data['invoice_status_array'] = [
'yet_to_generate' => 'Pending',
'generated' => 'Generated',
'send' => 'Sent',
'recived' => 'Payment Received',
];
$data['vehicle_type'] = [
'two_wheeler' => 'Two Wheeler',
'four_wheeler' => 'Four Wheeler',
'truck' => 'Truck',
'bus' => 'Bus',
'van' => 'Van',
'suv' => 'SUV',
'motorcycle' => 'Motorcycle',
'bicycle' => 'Bicycle'
];
$data['vehicle_des'] = [
'commercial' => 'Commercial',
'private' => 'Private',
];
$data['inception_data_list'] = $this->policyTransactionModel
->select('
policy_transaction.*,
clients.short_name AS client_short_name,
clients.client_code,
clients.client_type,
clients.client_name,
client_branch.branch_name AS client_branch_name,
insurers.name AS insurer_name,
insurers.short_name AS insurer_short_name,
policy_type.policy_type,
pt_co_share_details.agreed_amt AS amount,
pt_co_share_details.bp_amt AS bp,
pt_co_share_details.tp_amt AS tp
')
->join('pt_co_share_details', 'policy_transaction.id = pt_co_share_details.pt_id', 'left')
->join('clients', 'clients.id = policy_transaction.client_id', 'left')
->join('client_branch', 'policy_transaction.client_branch_id = client_branch.id', 'left')
->join('insurers', 'pt_co_share_details.insurer_id = insurers.id', 'left')
->join('client_policy', 'policy_transaction.client_policy_id = client_policy.id', 'left')
->join('policy_type', 'client_policy.policy_type_id = policy_type.id', 'left')
->where('policy_transaction.is_active', 1)
->where('policy_transaction.action_type', 'inception')
->orderBy('policy_transaction.id', 'desc')
->findAll();
$data['client'] = $this->clientModel->where('is_active', 1)->findAll();
$data['policy_type'] = $this->policyTypeModel->where('is_active', 1)->findAll();
$data['insurer'] = $this->insurerModel->where('is_active', 1)->findAll();
$data['entity'] = $this->kycEntityTypeModel->where('is_active', 1)->findAll();
$data['policy_types'] = $this->policyTypeModel->where('is_active', 1)->findAll();
$data['insurer_branch'] = $this->insurerBranchModel ->getInsurerBranchesWithInsurerNames();
$data['tpa'] = $this->tpaBranchModel ->getTpaBranchesWithTpaNames();
$data['salse_team'] = $this->userModel
->select('user_profiles.*')
->join('user_teams', 'user_profiles.id = user_teams.user_id')
->where('user_teams.team_id', 5)
->where('user_teams.is_active', 1)
->where('user_profiles.is_active', 1)
->findAll();
$data['ACM'] = $this->userModel
->where('user_profiles.role', 3)
->where('user_profiles.is_active', 1)
->findAll();
$this->loadLayout('policy_transaction_inception_list', $data);
}
// policy Transaction Create function start
public function createInceptionPolicy()
{
// print_r($this->request->getPost()); die;
$id = $this->request->getPost('id');
$data = $this->preparePolicyData();
// print_r($this->request->getPost()); die;
if (!$id) {
return $this->insertInceptionPolicy($data);
} else {
return $this->updateInceptionPolicy($id, $data);
}
}
private function preparePolicyData()
{
$data = $this->request->getPost();
// print_r($data); die;
// Format dates
$data['policy_issue_date'] = date('Y-m-d', strtotime($data['policy_issue_date'])) ?? null;
$data['policy_start_date'] = date('Y-m-d', strtotime($data['policy_start_date'])) ?? null;
$data['policy_end_date'] = date('Y-m-d', strtotime($data['policy_end_date'])) ?? null;
$data['renewal_date'] = date('Y-m-d', strtotime($data['renewal_date'])) ?? null;
$data['rollover_date'] = date('Y-m-d', strtotime($data['rollover_date'])) ?? null;
// Separate Insurer and TPA Branch IDs and IDs
if(isset($data['insurer_id']) && !empty($data['insurer_id'])){
list($data['insurer_branch_id'], $data['insurer_id']) = explode('-', $data['insurer_id']);
if ($data['tpa']) {
list($data['tpa_branch_id'], $data['tpa_id']) = explode('-', $data['tpa']);
}
}
$data['tsi'] = generate_tsi_code($data['issue_type']);
$data['action_type'] = 'inception';
if (!isset($data['co_share'])) {
$data['co_share'] = 0;
} elseif ($data['co_share']) {
$data['co_share'] = 1;
}
if (!isset($data['bro_payable_by'])) {
$data['bro_payable_by'] = 0;
} elseif ($data['bro_payable_by']) {
$data['bro_payable_by'] = 1;
}
if (!isset($data['same_as_proposer'])) {
$data['same_as_proposer'] = 0;
} elseif ($data['same_as_proposer']) {
$data['same_as_proposer'] = 1;
}
// Determine $is_addon value
$data['is_addon'] = in_array($data['policy_type_id'], [1, 2, 6, 7]) ? 1 :
(in_array($data['policy_type_id'], [4, 5]) ? 2 : ($data['policy_type_id'] == 3 && $data['base_policy'] ? 3 : 1));
return $data;
}
private function insertInceptionPolicy($data)
{
// print_r($data); die;
$insert = $this->policyTransactionModel->insert($data);
if ($insert) {
$this->insertTransactionStatus($insert, $data['status'], 1);
$emp_data = $this->processInsertIndividualMemberInEmpTable($data);
if(isset($data['follow_insurer_id']) && !empty($data['follow_insurer_id'][0])){
$pt_co_share_details = $this->insertOrUpdateCoShareDetails($data, $insert);
$client_policy_insert_data = $this->prepareClientPolicyInsertData($data);
$client_policy_id = $this->clientPolicyModel->insert($client_policy_insert_data);
$this->policyTransactionModel->update($insert, ['client_policy_id' => $client_policy_id]);
$emp_policy_insert = $this->InsertIndividualEmpPolicyTable($emp_data, $client_policy_id);
if ($data['status'] == 'completed') {
$this->processCompletedStatus($data, $client_policy_id, $data['insurer_id']);
}
$data['pt_co_share_details'] = $this->PTCOShareDetailsModel->where('pt_id', $insert)->where('is_active', 1)->findAll();
}
return $this->respondSuccess($insert, "Policy transaction created successfully", $data);
}
return $this->respondError("Failed to create policy transaction");
}
private function updateInceptionPolicy($id, $data)
{
if ($this->policyTransactionModel->update($id, $data)) {
$this->insertTransactionStatus($id, $data['status'], 1);
$emp_data = $this->processInsertIndividualMemberInEmpTable($data);
if (isset($data['follow_insurer_id']) && !empty($data['follow_insurer_id'][0])) {
$this->insertOrUpdateCoShareDetails($data, $id);
if (empty($data['client_policy_id']) || $data['client_policy_id'] == 0) {
$client_policy_insert_data = $this->prepareClientPolicyInsertData($data);
// print_r($client_policy_insert_data); die;
$client_policy_id = $this->clientPolicyModel->insert($client_policy_insert_data);
$this->policyTransactionModel->update($id, ['client_policy_id' => $client_policy_id]);
$this->InsertIndividualEmpPolicyTable($emp_data, $client_policy_id);
} else {
if (isset($data['emp_policy_id']) && !empty($data['emp_policy_id'][0])) {
$this->InsertIndividualEmpPolicyTable($emp_data, $data['client_policy_id']);
}
}
if ($data['status'] == 'completed') {
$this->processCompletedStatus($data, $data['client_policy_id'], $data['insurer_id']);
}
$data['pt_co_share_details'] = $this->PTCOShareDetailsModel->where('pt_id', $id)->where('is_active', 1)->findAll();
}
return $this->respondSuccess($id, "Policy transaction updated successfully", $data);
}
return $this->respondError("Failed to update policy transaction");
}
private function insertOrUpdateCoShareDetails($data, $pt_id)
{
// Prepare data for insertion and updating
$coShareDetails = [];
// print_r($data);
// die;
foreach ($data['follow_insurer_id'] as $index => $insurer) {
// Separate the insurer and insurer branch
list($insurer_branch_id, $insurer_id) = explode('-', $insurer);
// Prepare each co-share detail entry
$coShareDetails[] = [
'pt_id' => $pt_id,
'insurer_id' => $insurer_id ?? 0,
'insurer_branch_id' => $insurer_branch_id ?? 0,
'co_share_type' => isset($data['co_share_type'][$index]) ? 2 : 1,
'co_share_per' => $data['co_share_per'][$index] ?? 0,
'bp_amt' => $data['base_premium'][$index] ?? 0,
'bp_gst_amt' => $data['gst_amount'][$index] ?? 0,
'bp_igst' => $data['igst'][$index] ?? 0,
'bp_sgst' => $data['sgst'][$index] ?? 0,
'bp_cgst' => $data['cgst'][$index] ?? 0,
'tp_amt' => $data['tp_ter_premium'][$index] ?? 0,
'tep_amt' => $data['tp_ter_premium'][$index] ?? 0,
'agreed_amt' => $data['agreed_amount'][$index] ?? 0,
'agreed_bp_per' => $data['agreed_bp'][$index] ?? 0,
'agreed_tp_per' => $data['agreed_tp_ter'][$index] ?? 0,
'agreed_tep_per' => $data['agreed_tp_ter'][$index] ?? 0,
'standerd_bp_per' => $data['standard_bp'][$index] ?? 0,
'standerd_tp_per' => $data['standard_tp'][$index] ?? 0,
'standerd_tep_per' => $data['standard_tep'][$index] ?? 0,
'actual_bp_amt' => $data['actual_bp_amt'][$index] ?? 0,
'actual_tp_amt' => $data['actual_tp_amt'][$index] ?? 0,
'actual_tep_amt' => $data['actual_tep_amt'][$index] ?? 0,
'actual_bp_per' => $data['actual_bp_per'][$index] ?? 0,
'actual_tp_per' => $data['actual_tp_per'][$index] ?? 0,
'actual_tep_per' => $data['actual_tep_per'][$index] ?? 0,
'exp_amt' => $data['exp_amt'][$index] ?? 0,
'amount' => $data['total'][$index] ?? 0,
'stamp_duty' => $data['stamp_duty'][$index] ?? 0,
'cop_amt' => $data['co_premium'][$index] ?? 0,
'variance' => $data['variance'][$index] ?? 0,
'remark' => $data['remark'][$index] ?? null,
'created_by' => get_session_userid() ?? null,
'updated_by' => get_session_userid() ?? null,
'id' => $data['co_share_id'][$index] ?? null, // Assuming this is the ID to identify existing records
];
}
// print_r($coShareDetails); die;
// Separate data into insert and update batches
$insertData = array_filter($coShareDetails, function($detail) {
return empty($detail['id']); // Only insert new records
});
$updateData = array_filter($coShareDetails, function($detail) {
return !empty($detail['id']); // Only update existing records
});
// Insert new records
if (!empty($insertData)) {
$this->PTCOShareDetailsModel->insertBatch($insertData);
}
// Update existing records
if (!empty($updateData)) {
$this->PTCOShareDetailsModel->updateBatch($updateData, 'id'); // Assuming 'id' is the unique identifier
}
// print_r($this->PTCOShareDetailsModel->getLastQuery()); die;
return true;
}
private function prepareClientPolicyInsertData($data)
{
return [
'client_id' => $data['client_id'] ?? null,
'insurer_id' => $data['insurer_id'] ?? null,
'insurer_branch_id' => $data['insurer_branch_id'] ?? null,
'tpa_id' => $data['tpa_id'] ?? null,
'tpa_branch_id' => $data['tpa_branch_id'] ?? null,
'policy_type_id' => $data['policy_type_id'] ?? null,
'policy_start_date' => $data['policy_start_date'] ?? null,
'policy_end_date' => $data['policy_end_date'] ?? null ,
'policy_status' => 1,
'is_addon' => $data['is_addon'] ?? null,
'base_policy' => $data['base_policy'] ?? 0,
'created_by' => get_session_userid(),
'policy_no' => $data['policy_no'] ?? null,
'client_branch_id' => $data['client_branch_id'] ?? 0,
'cd_ac_no' => $data['cd_ac_no'] ?? null,
'gst' => 18,
];
}
private function insertTransactionStatus($policyTranId, $status, $statusType)
{
$statusData = [
'policy_tran_id' => $policyTranId,
'status' => $status,
'status_type' => $statusType,
'created_by' => get_session_userid(),
];
$this->policyTransactionStatusModel->insert($statusData);
}
private function processCompletedStatus($data, $client_policy_id, $insurer_id)
{
$totalAmount = (int)$data['total'][0] ?? 0;
$description = 'The following amount of Rs. ' . $totalAmount . '/- has been debited for the ' . $data['emp_count'] . ' employees at Inception.';
$cdTransactionData = [
'amount' => $totalAmount,
'sub_type_id' => 4,
'client_id' => $data['client_id'],
'client_policy_id' => $client_policy_id ?? 0,
'endorsement_no' => null,
'cd_ac_no' => $data['cd_ac_no'] ?? null,
'insurer_id' => $insurer_id,
'description' => $description,
'transaction_type' => 'Debit',
'updated_by' => get_session_userid(),
'event_name' => 'inception',
'is_active' => 1,
];
DepositHelper::saveDeposit($cdTransactionData, get_session_userid());
}
private function processInsertIndividualMemberInEmpTable($data)
{
// print_r($data);
if(isset($data['family_name']) && !empty($data['family_name'])){
$emp_data = [];
$emp_ids = [];
$random_code = generateRandomCode(); // Generate random code once
foreach ($data['family_name'] as $index => $val) {
// Determine the gender based on the relationship
$relationship = $data['relationship'][$index] ?? '';
$gender = 'M'; // Default to Male
// Set gender based on relationship
if (in_array($relationship, ['Mother', 'Daughter', 'Mother in law', 'Spouse'])) {
$gender = 'F';
}
$emp_data[] = [
'client_id' => $data['client_id'] ?? 0,
'client_branch_id' => $data['client_branch_id'] ?? 0,
'name' => $val,
'relationship' => $relationship,
'gender' => $gender,
'emp_status' => 'active',
'created_by' => get_session_userid(),
'emp_code' => $random_code, // Use the same random code for all
'id' => $data['emp_id'][$index] ?? null
];
$emp_ids[] = $data['emp_id'][$index] ?? 0;
}
// print_r($emp_data); die;
// Separate data into insert and update batches
$insertData = array_filter($emp_data, function($detail) {
return empty($detail['id']); // Only insert new records
});
$updateData = array_filter($emp_data, function($detail) {
return !empty($detail['id']); // Only update existing records
});
// Insert new records
if (!empty($insertData)) {
$this->employeeModel->insertBatch($insertData);
}
// Update existing records
if (!empty($updateData)) {
$this->employeeModel->updateBatch($updateData, 'id'); // Assuming 'id' is the unique identifier
}
return $emp_ids;
}
}
private function InsertIndividualEmpPolicyTable($emp_ids, $client_policy_id)
{
if(!empty($emp_ids)){
$emp_policy_data = [];
foreach ($emp_ids as $index => $emp_id) {
$emp_policy_data[] = [
'employee_id' => $emp_id ?? 0,
'client_policy_id' => $client_policy_id ?? 0,
'created_by' => get_session_userid(),
'status' => 'active',
];
}
print_r($emp_policy_data); die;
// Insert new records
if (!empty($emp_policy_data)) {
$this->employeePolicyModel->insertBatch($emp_policy_data);
}
return true;
}
}
private function respondSuccess($id, $message, $data)
{
return $this->respond(['status' => true, 'pt_id' => $id, 'message' => $message, 'data' =>$data], 200);
}
private function respondError($message)
{
return $this->respond(['status' => false, 'message' => $message], 200);
}
// policy Transaction Create function End
// Get the Single Inception Tranction data for edit
public function getInceptionDataForEdit($id)
{
$data = $this->policyTransactionModel
->select('
policy_transaction.*,
clients.short_name as client_short_name,
clients.client_type,
policy_type.policy_type,
client_policy.tpa_id as master_tpa_id,
client_policy.tpa_branch_id as master_tpa_branch_id,
client_policy.policy_type_id as master_policy_type_id,
client_policy.is_addon,
client_policy.base_policy,
')
->join('clients', 'clients.id = policy_transaction.client_id')
->join('client_policy', 'policy_transaction.client_policy_id = client_policy.id', 'left')
->join('policy_type', 'client_policy.policy_type_id = policy_type.id', 'left')
->where('policy_transaction.id', $id)
->first();
$data['policy_issue_date'] = !empty($data['policy_issue_date']) ? date('d-m-Y', strtotime($data['policy_issue_date'])) : null;
$data['policy_start_date'] = !empty($data['policy_start_date']) ? date('d-m-Y', strtotime($data['policy_start_date'])) : null;
$data['policy_end_date'] = !empty($data['policy_end_date']) ? date('d-m-Y', strtotime($data['policy_end_date'])) : null;
$data['renewal_date'] = !empty($data['renewal_date']) ? date('d-m-Y', strtotime($data['renewal_date'])) : null;
$data['rollover_date'] = !empty($data['rollover_date']) ? date('d-m-Y', strtotime($data['rollover_date'])) : null;
$data['created_at'] = !empty($data['created_at']) ? date('d-m-Y h:i:s A', strtotime($data['created_at'])) : null;
$data['updated_at'] = !empty($data['updated_at']) ? date('d-m-Y h:i:s A', strtotime($data['updated_at'])) : null;
// print_r($data); die;
$data['renewal_policy'] = $this->clientPolicyModel
->select('client_policy.*, policy_type.policy_type')
->join('policy_type', 'policy_type.id = client_policy.policy_type_id')
->where('client_policy.client_id', $data['client_id'])
->where('client_policy.client_branch_id', $data['client_branch_id'])
->where('client_policy.is_active', 1)
->findAll();
$data['base_policy_data'] = $this->clientPolicyModel
->select('client_policy.*, policy_type.policy_type')
->join('policy_type', 'policy_type.id = client_policy.policy_type_id')
->where('client_policy.client_id', $data['client_id'])
->where('client_policy.client_branch_id', $data['client_branch_id'])
->where('client_policy.policy_type_id', 2)
->where('client_policy.is_active', 1)
->findAll();
$data['pt_files'] = $this->PTFileModel->where('pt_id', $id)->where('is_active', 1)->findAll();
$data['pt_co_share_details'] = $this->PTCOShareDetailsModel->where('pt_id', $id)->where('is_active', 1)->findAll();
$data['emp_data'] = $this->employeeModel
->select('employees.*, employee_polices.id as emp_policy_id')
->join('employee_polices', 'employees.id = employee_polices.employee_id', 'left')
->where('employees.client_id', $data['client_id'])
->where('employees.is_active', 1)->findAll();
// print_r( $data['pt_co_share_details']); die;
if($data){
return $this->respond(['status' => true, 'data' => $data], 200);
}else{
return $this->respond(['status' => false], 200);
}
}
//------------------------------------------------------------------------------------------------
// Policy Transaction Endorsement
public function viewEndorsement()
{
$data['issuer'] = [1 => 'JIBS', 2 => 'Nhance'];
$data['client_type'] = [1 => 'Group', 2 => 'Individual'];
$data['policy_status'] = [
'under_process' => 'Under Process',
'client_pending' => 'Client Pending',
'insurer_pending' => 'Insurer Pending',
'co_insurer_pending' => 'Co-Insurer Pending',
'tpa_pending' => 'TPA Pending',
'validated' => 'Validated',
'cancelled' => 'Cancelled',
'instalment_pending' => 'Instalment Pending',
'completed' => 'Completed',
];
$data['invoice_status'] = [
'yet_to_generate' => 'Pending',
'generated' => 'Generated',
'send' => 'Sent',
'recived' => 'Payment Received',
];
$data['action_type'] = [
'addition' => 'Addition',
'deletion' => 'Deletion',
'addition_deletion' => 'Addition & Deletion',
'si_enhancement' => 'SI Enhancement',
'combo_a_d_si' => 'Combo A, D & SI',
'correction' => 'Correction',
'baby_addition' => 'Baby Addition',
'policy_instalment' => 'Policy Instalment',
'addition_inception' => 'Addition-Inception',
'bds_correction' => 'BDS Correction',
'policy_correction' => 'Policy Correction',
'policy_cancellation' => 'Policy Cancellation',
];
$data['endorsement_data_list'] = $this->policyTransactionModel
->select('
policy_transaction.*,
clients.short_name as client_short_name,
clients.client_code,
client_branch.branch_name as client_branch_name,
insurers.name AS insurer_name,
insurers.short_name AS insurer_short_name,
policy_type.policy_type
')
->join('pt_co_share_details', 'policy_transaction.id = pt_co_share_details.pt_id', 'left')
->join('clients', 'policy_transaction.client_id = clients.id', 'left')
->join('client_branch', 'policy_transaction.client_branch_id = client_branch.id', 'left')
->join('insurers', 'pt_co_share_details.insurer_id = insurers.id','left')
->join('client_policy', 'policy_transaction.client_policy_id = client_policy.id', 'left')
->join('policy_type', 'client_policy.policy_type_id = policy_type.id', 'left')
->where('policy_transaction.is_active', 1)
->where('policy_transaction.action_type !=', 'inception')
->orderBy('policy_transaction.id', 'desc')
->findAll();
$data['client'] = $this->clientModel->where('is_active', 1)->findAll();
$data['policy_type'] = $this->policyTypeModel->where('is_active', 1)->findAll();
// $data['insurer'] = $this->insurerModel->where('is_active', 1)->findAll();
// $data['tpa'] = $this->tpaModel->where('is_active', 1)->findAll();
$data['insurer_branch'] = $this->insurerBranchModel ->getInsurerBranchesWithInsurerNames();
$data['tpa'] = $this->tpaBranchModel ->getTpaBranchesWithTpaNames();
$this->loadLayout('policy_transaction_endorsement_list', $data);
}
public function createEndorsementPolicy()
{
$id = $this->request->getPost('id');
$data = $this->preparePolicyTransactionData();
if (!$id) {
return $this->insertEndorsementTransaction($data);
} else {
return $this->updateEndorsementTransaction($id, $data);
}
}
private function preparePolicyTransactionData()
{
$data = $this->request->getPost();
// print_r($data); die;
$data_received_date = (string)$this->request->getPost('data_received_date');
$closure_date = (string)$this->request->getPost('closure_date');
$data['data_received_date'] = date('Y-m-d', strtotime($data_received_date));
$data['closure_date'] = date('Y-m-d', strtotime($closure_date));
// Separate Insurer and TPA Branch IDs and IDs
if(isset($data['insurer_id']) && !empty($data['insurer_id'])){
list($data['insurer_branch_id'], $data['insurer_id']) = explode('-', $data['insurer_id']);
if ($data['tpa']) {
list($data['tpa_branch_id'], $data['tpa_id']) = explode('-', $data['tpa']);
}
}
$issue_type = $this->policyTransactionModel
->where('action_type', 'inception')
->where('client_id', $this->request->getPost('client_id'))
->where('client_policy_id', $this->request->getPost('client_policy_id'))
->first();
$data['tsi'] = generate_tsi_code($issue_type['issue_type'] ?? 1);
$client_branch_id = $data['client_branch_id'];
if($data['client_branch_id'] == ""){
$client_branch_id = $issue_type['client_branch_id'];
}
if(!$data['id']){
$data += [
'issuer' => $issue_type['issuer'] ?? null,
'client_type' => $data['client_type'] ?? $issue_type['client_type'] ?? null,
'policy_type_id' => $issue_type['policy_type_id'] ?? null,
'policy_type_id' => $issue_type['policy_type_id'] ?? null,
'issue_type' => $issue_type['issue_type'] ?? null,
'source_client_policy_id' => $issue_type['source_client_policy_id'] ?? null,
'cd_ac_no' => $issue_type['issue_type'] ?? null,
'policy_issue_date' => $issue_type['policy_issue_date'] ?? null,
'policy_start_date' => $issue_type['policy_start_date'] ?? null,
'policy_end_date' => $issue_type['policy_end_date'] ?? null,
'revenue_type' => $issue_type['revenue_type'] ?? null,
'co_share' => $issue_type['co_share'] ?? 0,
'bro_payable_by' => $issue_type['bro_payable_by'] ?? 0,
'installment' => $issue_type['installment'] ?? null,
'installment_data' => $issue_type['installment_data'] ?? null,
'location' => $issue_type['location'] ?? null,
'links' => $issue_type['links'] ??null,
'stage' => $issue_type['stage'] ?? null,
'etat' => $issue_type['etat'] ?? null,
'etat_band' => $issue_type['etat_band'] ?? null,
'edate' => $issue_type['edate'] ?? null,
'renewal_date' => $issue_type['renewal_date'] ?? null,
'rollover_date' => $issue_type['rollover_date'] ?? null,
'policy_holder_name' => $issue_type['policy_holder_name'] ?? null,
'same_as_proposer' => $issue_type['same_as_proposer'] ?? 1,
'ref' => $issue_type['ref'] ?? null,
'spl' => $issue_type['spl'] ?? null,
'fund_received' => $issue_type['fund_received'] ?? null,
'listed_insurers' => $issue_type['listed_insurers'] ?? null,
'ppteam' => $issue_type['ppteam'] ?? null,
'sales_generated_by' => $issue_type['sales_generated_by'] ?? null,
'serviced_by' => $issue_type['serviced_by'] ?? null
];
$data['client_branch_id'] = $client_branch_id;
}
// print_r($issue_type);
// print_r($data);
// die;
return $data;
}
private function insertEndorsementTransaction($data)
{
$insert = $this->policyTransactionModel->insert($data);
if ($insert) {
$this->insertTransactionStatus($insert, $data['status'], 1);
$this->handleCompletedStatus($data, $insert);
$this->insertOrUpdateCoShareDetails($data, $insert);
return $this->respond(['status' => true, 'message' => 'Endorsement transaction created successfully'], 200);
}
return $this->respond(['status' => false, 'message' => 'Endorsement transaction creation failed'], 400);
}
private function updateEndorsementTransaction($id, $data)
{
$update = $this->policyTransactionModel->where('id', $id)->set($data)->update();
if ($update) {
$this->insertTransactionStatus($id, $data['status'], 1);
$this->handleCompletedStatus($data, $id);
$this->insertOrUpdateCoShareDetails($data, $id);
return $this->respond(['status' => true, 'message' => 'Endorsement transaction updated successfully'], 200);
}
return $this->respond(['status' => false, 'message' => 'Endorsement transaction update failed'], 400);
}
private function handleCompletedStatus($data, $policy_tran_id)
{
if ($data['status'] == 'completed') {
$tolamt = $data['total'][0];
$description = 'The following amount of Rs. ' . round($tolamt, 2) . '/- has been' .
($data['action_type'] == 'deletion' ? ' Credit ' : ' Debit ') . 'from the policy transaction';
$cd_tranction_data = [
'amount' => $tolamt,
'sub_type_id' => $data['action_type'] == 'deletion' ? 3 : 4,
'client_id' => $data['client_id'],
'client_policy_id' => $data['client_policy_id'],
'endorsement_no' => null,
'cd_ac_no' => $data['cd_ac_no'] ?? null,
'insurer_id' => $data['insurer_id'],
'description' => $description,
'transaction_type' => $data['action_type'] == 'deletion' ? 'Credit' : 'Debit',
'updated_by' => get_session_userid(),
'event_name' => $data['action_type'],
'is_active' => 1,
];
DepositHelper::saveDeposit($cd_tranction_data, get_session_userid());
}
}
public function getEndorsementDataForEdit($id)
{
$data = $this->policyTransactionModel
->select('
policy_transaction.*,
clients.short_name as client_short_name,
clients.client_type,
client_policy.policy_start_date as s_date,
client_policy.policy_end_date as e_date,
')
->join('clients', 'clients.id = policy_transaction.client_id')
->join('client_policy', 'policy_transaction.client_policy_id = client_policy.id', 'left')
->where('policy_transaction.id', $id)
->first();
$data['policy_start_date'] = date('d-m-Y', strtotime($data['s_date']));
$data['policy_end_date'] = date('d-m-Y', strtotime($data['e_date']));
$data['data_received_date'] = date('d-m-Y', strtotime($data['data_received_date']));
$data['closure_date'] = date('d-m-Y', strtotime($data['closure_date']));
$data['pt_co_share_details'] = $this->PTCOShareDetailsModel->where('pt_id', $id)->where('is_active', 1)->findAll();
if($data){
return $this->respond(['status' => true, 'data' => $data], 200);
}else{
return $this->respond(['status' => false], 200);
}
}
//------------------------------------------------------------------------------------------------
//file upload function
public function uploadFile()
{
$files = $this->request->getFiles();
$doc_name = $this->request->getPost('doc_name[]');
$pt_id = $this->request->getPost('pt_id');
$uploadData = uploadFilesToGoogleDrive($files['file'], $doc_name, $pt_id);
$insert = false;
if(!isset($uploadData['status'])){
$this->PTFileModel->insertBatch($uploadData);
$insert = true;
}
if ($insert) {
$data['pt_files'] = $this->PTFileModel->where('pt_id', $pt_id)->where('is_active', 1)->findAll();
return $this->respond(['status' => true, 'message' => 'File uploaded successfully in G-Drive', 'data' => $data]);
} else {
return $this->respond(['status' => false, 'message' => 'Failed to upload file in G-Drive']);
}
}
// update Invoice Status
public function updateInvoiceStatus()
{
$ids = $this->request->getPost('ids');
$ids = json_decode($ids);
// var_dump($ids); die;
$data['invoice_status'] = $this->request->getPost('invoice_status');
if($this->request->getPost('invoice_status') == 'generated'){
$data['invoice_no'] = $this->request->getPost('invoice_no');
}
$update = $this->policyTransactionModel
->whereIn('id', $ids)
->set($data)
->update();
if ($update) {
return $this->respond(['status' => true, 'data' => $ids, 'message' => 'Invoice status updated successfully'], 200);
} else {
return $this->respond(['status' => true, 'data' => $ids, 'message' => 'Failed to update invoice status'], 200);
}
}
public function removeCoShareData($id)
{
$this->myLogger->logme('error', 'PT Co Share Detailes Remove function called');
$data = [
'updated_by' => get_session_userid(),
'is_active' => 0
];
$update = $this->PTCOShareDetailsModel->where('id', $id)->set($data)->update();
if ($update) {
return $this->respond(['status' => true, 'code' => 200, 'message' => 'Data removed successfully'], 200);
} else {
return $this->respond(['status' => false, 'code' => 404, 'message' => 'Failed to remove data'], 200);
}
}
public function getPTCOShareCount($client_id, $client_policy_id)
{
$totalCount = $this->PTCOShareDetailsModel
->select('pt_co_share_details.*')
->join('policy_transaction', 'policy_transaction.id = pt_co_share_details.pt_id')
->where('policy_transaction.client_id', $client_id)
->where('policy_transaction.client_policy_id', $client_policy_id)
->where('policy_transaction.action_type', 'inception')
->findAll();
if ($totalCount) {
return $this->respond(['status' => true, 'code' => 200, 'data' => $totalCount], 200);
} else {
return $this->respond(['status' => false, 'code' => 404, 'message' => 'Failed to get data'], 200);
}
}
//---------------------------------------------------------------------------------------------------
//get BDS Reports data
public function reportBDS()
{
$data['issuer'] = [1 => 'JIBS', 2 => 'Nhance'];
$data['client_type'] = [1 => 'Group', 2 => 'Individual'];
$data['issuing_type'] = [1 => 'Fresh', 2 => 'Renewal', 3 => 'Roll Over'];
$data['policy_status'] = [
'pending' => 'Pending',
'exported_to_insurer' => 'Exported to Insurer',
'imported_from_insurer' => 'Imported from Insurer',
'exported_to_tpa' => 'Exported to TPA',
'imported_from_tpa' => 'Imported from TPA',
'completed' => 'Completed'
];
$data['invoice_status_array'] = [
'yet_to_generate' => 'Yet to Generate',
'generated' => 'Generated',
'send' => 'Send',
'recived' => 'Recived',
];
$data['date_type'] = [
'policy_issue_date' => 'Policy Issue Date',
'policy_start_date' => 'Policy Start Date',
'policy_end_date' => 'Policy End Date',
'data_received_date' => 'Data Received Date',
'closure_date' => 'Closure Date',
];
$data['insurer'] = $this->insurerModel->where('is_active', 1)->findAll();
$data['policy_types'] = $this->policyTypeModel->where('is_active', 1)->findAll();
$data['clients'] = $this->clientModel->where('is_active', 1)->findAll();
//filter datas
$start_date = $this->request->getGet('start_date');
$end_date = $this->request->getGet('end_date');
$client_id = $this->request->getGet('client_id');
$insurer_id = $this->request->getGet('insurer_id');
$policy_type_id = $this->request->getGet('policy_type_id');
$date_type = $this->request->getGet('date_type');
$issuer = $this->request->getGet('issuer');
$start_date = (!isset($start_date) || $start_date === '' || $start_date === null) ? 0 : $start_date;
$end_date = (!isset($end_date) || $end_date === '' || $end_date === null) ? 0 : $end_date;
$client_id = (!isset($client_id) || $client_id === '' || $client_id === null) ? 0 : $client_id;
$insurer_id = (!isset($insurer_id) || $insurer_id === '' || $insurer_id === null) ? 0 : $insurer_id;
$policy_type_id = (!isset($policy_type_id) || $policy_type_id === '' || $policy_type_id === null) ? 0 : $policy_type_id;
$date_type = (!isset($date_type) || $date_type === '' || $date_type === null) ? 0 : $date_type;
$issuer = (!isset($issuer) || $issuer === '' || $issuer === null) ? 0 : $issuer;
//Actual data for the list
$data['report_list'] = $this->policyTransactionModel->getBDSReportList($start_date, $end_date, $client_id, $insurer_id, $policy_type_id, $date_type, $issuer);
// dd($data);
$this->loadLayout('report_bds_filter', $data);
}
}