diff --git a/app/Controllers/ClientController.php b/app/Controllers/ClientController.php index bff1d34f..a61d0917 100755 --- a/app/Controllers/ClientController.php +++ b/app/Controllers/ClientController.php @@ -5079,44 +5079,30 @@ class ClientController extends AdminController // $client_policy_data = $this->clientPolicyModel->getPolicyDetailsForRemainder($client_id=159, $client_branch_id=126); // $dashBoardController->sendRemainderMail($client_policy_data); - // $EmpDataServiceController = new EmpDataServiceController(); // $emp_data = $this->employeePolicyModel->getEmployeePolicyForEcard(12); // $ids = array_column($emp_data, 'id'); - // $EmpDataServiceController->sendMailForDownloadingECard($ids); $EmpDataServiceController = new EmpDataServiceController(); - // $return = $EmpDataServiceController->generateExcelForDeletion($batch_data); dd($return); die; - // $EmpDataServiceController->generateExcelForSIEnhancement($batch_data); die; - // $EmpDataServiceController->importInceptionFileValidation(['file_id' => 1932]); die; - // $EmpDataServiceController->importDeletionValidation(['file_id' => 304]); //for live - // $EmpDataServiceController->importSIEnhancementUpdateEndorsementID(['file_id' => 1199]); //for live - // $result = $EmpDataServiceController->importSIEnhancementValidation(['file_id' => 2496]); //for live - // $EmpDataServiceController->importInceptionUpdateTPAandUHID(['file_id' => 214]); //for live - - // $result = $this->employeePolicyModel->getDeletionEmployeeDataForExportExcel($batch_data, 1); dd($result); - // $result = $this->employeePolicyModel->getSIEnhancementEmployeesDataForExportExcel($batch_data, 1); + // $res = $EmpDataServiceController->generateExcelForDeletion($batch_data); dd($return); die; + // $res = $EmpDataServiceController->generateExcelForSIEnhancement($batch_data); die; + // $res = $EmpDataServiceController->importInceptionFileValidation(['file_id' => 1932]); die; + // $res = $EmpDataServiceController->importDeletionValidation(['file_id' => 304]); //for live + // $res = $EmpDataServiceController->importSIEnhancementUpdateEndorsementID(['file_id' => 1199]); //for live + // $res = $EmpDataServiceController->importSIEnhancementValidation(['file_id' => 2496]); //for live + // $res = $EmpDataServiceController->importInceptionUpdateTPAandUHID(['file_id' => 214]); //for live + // $res = $EmpDataServiceController->importInceptionUpdateTPAandUHID(['file_id' => 310]); dd($res);//for live + // $res = $EmpDataServiceController->getInceptionBasePremium(["13332","13333","13334","13335","13336"]); dd($res);//for live + // $res = $EmpDataServiceController->getDeletionBasePremium(["13248","13250","13249","13247"], json_decode('{"employeeIds":["13248","13250","13249","13247"],"client_id":"3927","client_policy_id":"6183","client_branch_id":"1874","cd_ac_no":"CD9751909505","endorsement_no":"END1238","count":4,"event_name":"deletion","policy_name":"GMC","user_id":"48"}', true)); dd($res);//for live + // $res = $EmpDataServiceController->makeEntryForBDSPolicyTransaction(json_decode('{"client_policy_id":"6187","endorsement_no":null,"emp_count":5,"action_type":"inception","no_of_insured":3,"no_of_dependent":0}', true)); dd($res);//for live + // $res = $EmpDataServiceController->sendMailForDownloadingECard($ids); + // $res = $this->employeePolicyModel->getDeletionEmployeeDataForExportExcel($batch_data, 1); dd($result); + // $res = $this->employeePolicyModel->getSIEnhancementEmployeesDataForExportExcel($batch_data, 1); // return $this->downloadInsurerExcelExport($batch_data); - // dd($result); die; - - // $empServiceController = new EmployeeServiceController(); - // $res = $empServiceController->employeesOnboardPreprocess(['file_id' => '741']); + // dd($res); die; // $PolicyTransactionController = new PolicyTransactionController(); // $res = $PolicyTransactionController->validateInsurerStatement(['file_id' => '36']); - // $empServiceController = new EmployeeServiceController(); - // $res = $empServiceController->excelFileDataValidation(['file_id' => '1101']); - // dd($res); - - // $EmpDataServiceController = new EmpDataServiceController(); - // $EmpDataServiceController->importInceptionFileValidation(['file_id' => 160]); - - // $EmpDataServiceController = new EmpDataServiceController(); - // $EmpDataServiceController->importInceptionUpdateTPAandUHID(['file_id' => 162]); - // $EmpDataServiceController->importInceptionFileValidation(['file_id' => 162]); - // $EmpDataServiceController->importDeletionValidation(['file_id' => 171]); - // $EmpDataServiceController->importDeletionUpdateEndorsementID(['file_id' => 171]); - // $EmpDataServiceController->importCorrectionUpdateEndorsementID(['file_id' => 188]); // $params = [ // 'client_policy_id' => 6090, diff --git a/app/Controllers/EmpDataServiceController.php b/app/Controllers/EmpDataServiceController.php index 6da138fd..38a7e190 100755 --- a/app/Controllers/EmpDataServiceController.php +++ b/app/Controllers/EmpDataServiceController.php @@ -43,6 +43,7 @@ use PhpOffice\PhpSpreadsheet\Reader\Xlsx; use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\Reader\Exception as SpreadsheetReaderException; use PhpParser\Node\Expr\Cast\Double; +use Kint\Kint; use function PHPUnit\Framework\returnSelf; @@ -1576,6 +1577,7 @@ class EmpDataServiceController extends BaseController if ($insurer_or_tpa == 'tpa' && in_array($client_policy_data['policy_type_id'], [1,2,3,4,5])) { + //verify the TPA ID is not null if ($excel_data[$key][13] === null) { $missing_id[$key][] = [ 'row' => $key, @@ -1585,6 +1587,8 @@ class EmpDataServiceController extends BaseController ]; } } else if ($insurer_or_tpa == 'insurer') { + + //verify the UHID or Risk ID is not null if ($excel_data[$key][14] === null) { $missing_id[$key][] = [ 'row' => $key, @@ -1716,7 +1720,7 @@ class EmpDataServiceController extends BaseController ]; } - if ($insurer_or_tpa == 'tpa' && in_array($client_policy_data['policy_type_id'], [1,2,3,4,5])) { + if ($insurer_or_tpa == 'tpa') { if ($emp_value['uhid'] != $excel_data[$key][14]) { $errors[$key][] = [ @@ -1895,6 +1899,9 @@ class EmpDataServiceController extends BaseController $emp_endorsement_table_data = []; $enrollment_file_id = null; $endorsement_id = null; + $no_of_insured = []; + $no_of_dependent = []; + $emp_count = count($excel_data); $db = \Config\Database::connect(); @@ -1914,7 +1921,7 @@ class EmpDataServiceController extends BaseController $totals = $totals + floatval($amount); $query = $db->table('employee_polices'); - $query->select('employee_polices.id'); + $query->select('employee_polices.id, employees.id as emp_id, employees.emp_code, employees.name as emp_name, employees.relationship'); $query->join('employees', 'employees.id = employee_polices.employee_id'); $query->where('employee_polices.client_policy_id', $client_policy_id); $query->where('employees.client_id', $client_id); @@ -1935,11 +1942,20 @@ class EmpDataServiceController extends BaseController $query->limit(1); $result = $query->get()->getRowArray(); + // Kint::dump($result); + if (isset($result['id']) && $result['id'] !== null) { $emp_policy_ids[] = $result['id']; //for cash deposite $emp_details[] = array('id' => $result['id'], 'tpa_id' => $value[13], 'uhid' => $value[14]); + if (strtolower($result['relationship']) == 'self') { + $no_of_insured[] = ['emp_id' => $result['emp_id'], 'emp_code' => $result['emp_code'], 'emp_name' => $result['emp_name'] ]; + }else{ + $no_of_dependent[] = ['emp_id' => $result['emp_id'], 'emp_code' => $result['emp_code'], 'emp_name' => $result['emp_name'] ]; + } } + + if(in_array($file['event_type'], ['missed_inception']) && isset($value[18])){ $endorsement_id = $value[18]; @@ -2001,7 +2017,7 @@ class EmpDataServiceController extends BaseController } - // dd($emp_endorsement_table_data, $enrollment_file_id); + // dd($emp_endorsement_table_data, $enrollment_file_id, $no_of_insured, $no_of_dependent); //update the employee policy data (TPAID or UHID) $return = $this->employeePolicyModel->bulkUpdate($emp_details); @@ -2021,6 +2037,10 @@ class EmpDataServiceController extends BaseController $this->myLogger->logme('error', 'Inception Update TPA and UHID -- batch file status : {data}', ['data' => $status_val]); $this->myLogger->logme('error', 'Inception Update TPA and UHID -- total amount for cash deposite : {data}', ['data' => $totals]); + //get the BASE PREMIUM and GST for BDS Entry + $base_bremium_and_gst = $this->getInceptionBasePremium($emp_policy_ids); + $this->myLogger->logme('error', 'Inception Base Premium and GST :'. json_encode($base_bremium_and_gst ?? [])); + //update CD transaction entry if only insurer if ($file['insurer_or_tpa'] == 'insurer') { @@ -2063,6 +2083,10 @@ class EmpDataServiceController extends BaseController 'endorsement_no' => $endorsement_id ?? null, 'emp_count' => $emp_count ?? null, 'action_type' => $file['event_type'] ?? null, + 'no_of_insured' => count($no_of_insured ?? []) ?? null, + 'no_of_dependent' => count($no_of_dependent ?? []) ?? null, + 'base_premium' => $base_bremium_and_gst['base_premium'] ?? null, + 'gst' => $base_bremium_and_gst['gst'] ?? null, ]]); // $this->cashDepositCalculationForInception($depositeData); @@ -3005,6 +3029,8 @@ class EmpDataServiceController extends BaseController $endorsement_id = ''; $enrollment_file_id = ''; $endorsement_details = []; + $no_of_insured = []; + $no_of_dependent = []; $totals = 0; foreach ($excel_data as $key => $value) { @@ -3017,7 +3043,14 @@ class EmpDataServiceController extends BaseController $result = $this->empEndorsementModel - ->select('emp_endorsement.*, employees.id as emp_id, employee_polices.id as emp_policy_id') + ->select(' + emp_endorsement.*, + employees.id as emp_id, + employees.name as emp_name, + employees.emp_code, + employees.relationship, + employee_polices.id as emp_policy_id + ') ->join('employee_polices', 'employee_polices.id = emp_endorsement.pk') ->join('employees', 'employees.emp_code = emp_endorsement.emp_code') ->where('employees.emp_code', $emp_code) @@ -3043,6 +3076,11 @@ class EmpDataServiceController extends BaseController $emp_policy_ids[] = array('id' => $result['emp_policy_id'], 'is_active' => 0); $employeeIds[] = $result['emp_policy_id']; $endorsement_details[] = array('id' => $result['id'], 'group_key' => $result['group_key'], 'endorsement_id' => $value[19], 'status' => 'complete'); + if (strtolower($result['relationship']) == 'self') { + $no_of_insured[] = ['emp_id' => $result['emp_id'], 'emp_code' => $result['emp_code'], 'emp_name' => $result['emp_name'] ]; + }else{ + $no_of_dependent[] = ['emp_id' => $result['emp_id'], 'emp_code' => $result['emp_code'], 'emp_name' => $result['emp_name'] ]; + } } $empData = $this->employeePolicyModel @@ -3110,6 +3148,11 @@ class EmpDataServiceController extends BaseController $this->myLogger->logme('error', 'SI Enhancement Update Endorsement ID -- Employee count : {data}', ['data' => $emp_count]); $this->myLogger->logme('error', 'SI Enhancement Update Endorsement ID -- Batch File status : {data}', ['data' => $status_val]); + //get the BASE PREMIUM and GST for BDS Entry + $base_bremium_and_gst = $this->getInceptionBasePremium($employeeIds); + $this->myLogger->logme('error', 'SI Enhancement Base Premium and GST :'. json_encode($base_bremium_and_gst ?? [])); + + //call the cash deposite function if ($file['insurer_or_tpa'] == 'insurer') { @@ -3133,6 +3176,10 @@ class EmpDataServiceController extends BaseController 'endorsement_no' => $endorsement_id ?? null, 'emp_count' => $emp_count ?? null, 'action_type' => $file['event_type'] ?? null, + 'no_of_insured' => count($no_of_insured ?? []) ?? null, + 'no_of_dependent' => count($no_of_dependent ?? []) ?? null, + 'base_premium' => $base_bremium_and_gst['base_premium'] ?? null, + 'gst' => $base_bremium_and_gst['gst'] ?? null, ]]); } @@ -3521,6 +3568,8 @@ class EmpDataServiceController extends BaseController $employee_policy_table_primaryKey = []; $endorsement_id = ''; $enrollment_file_id = null; //files table primary key + $no_of_insured = []; + $no_of_dependent = []; $totals = 0; @@ -3554,6 +3603,12 @@ class EmpDataServiceController extends BaseController $employee_policy_table_data[] = array('id' => $result['emp_policy_primarykey'], 'date_of_exit' => change_date_format($result['date_of_exit']), 'reason_for_exit' => $result['reason_for_exit'], 'claim_status' => $result['claim_status'], 'status' => $result['status']); // $employees_table_data[] = array('id' => $result['employees_id'], 'emp_status' => $result['status']); $emp_endorsement_table_data[] = array('id' => $result['emp_endorsement_primarykey'], 'group_key' => $result['group_key'], 'endorsement_id' => $value[15], 'status' => 'complete'); + + if (strtolower($result['relationship']) == 'self') { + $no_of_insured[] = ['emp_id' => $result['employees_primarykey'], 'emp_code' => $result['emp_code'], 'emp_name' => $result['emp_name'] ]; + }else{ + $no_of_dependent[] = ['emp_id' => $result['employees_primarykey'], 'emp_code' => $result['emp_code'], 'emp_name' => $result['emp_name'] ]; + } } } @@ -3603,6 +3658,9 @@ class EmpDataServiceController extends BaseController $this->myLogger->logme('error', 'Deletion Update Endorsement ID -- Employee count : {data}', ['data' => $emp_count]); $this->myLogger->logme('error', 'Deletion Update Endorsement ID -- Batch File status : {data}', ['data' => $status_val]); + $base_bremium_and_gst = $this->getDeletionBasePremium($employee_policy_table_primaryKey, ['client_policy_id' => $client_policy_id]); + $this->myLogger->logme('error', 'Deletion Base Premium and GST :'. json_encode($base_bremium_and_gst ?? [])); + //call the cash deposite function if ($file['insurer_or_tpa'] == 'insurer') { @@ -3626,6 +3684,10 @@ class EmpDataServiceController extends BaseController 'endorsement_no' => $endorsement_id ?? null, 'emp_count' => $emp_count ?? null, 'action_type' => $file['event_type'] ?? null, + 'no_of_insured' => count($no_of_insured ?? []) ?? null, + 'no_of_dependent' => count($no_of_dependent ?? []) ?? null, + 'base_premium' => $base_bremium_and_gst['base_premium'] ?? null, + 'gst' => $base_bremium_and_gst['gst'] ?? null, ]]); } @@ -4290,8 +4352,6 @@ class EmpDataServiceController extends BaseController } - - /** * Below function retrieves the policy name associated with a given client policy ID. * @@ -4308,8 +4368,6 @@ class EmpDataServiceController extends BaseController } - - /** * Below function converts row data into column data format for SI enhancement. * @@ -4866,8 +4924,8 @@ class EmpDataServiceController extends BaseController $this->myLogger->logme("error", "Policy transaction inserted successfully: " . json_encode(['insert_id' => $insert_id])); //do not remove this commented item - // $coShareDetails = $this->ConstructPTShareData($policy_data, $insert_id); - // $pt_co_share_id = $this->PTCOShareDetailsModel->insert($coShareDetails); + $coShareDetails = $this->ConstructPTShareData($policy_data, $insert_id, $params); + $pt_co_share_id = $this->PTCOShareDetailsModel->insert($coShareDetails); // $this->myLogger->logme("error", "PT Co share data inserted successfully: " . json_encode(['pt_co_share_id' => $pt_co_share_id])); @@ -4903,11 +4961,12 @@ class EmpDataServiceController extends BaseController private function constructBDSData($policy_data, $lead_data, $params) { - + + $this->myLogger->logme("error", "constructBDSData Params: " . json_encode(['policy_data' => $policy_data, 'lead_data' => $lead_data, 'params' => $params])); $action_type_string = $params['action_type']; - if(in_array($params['action_type'], ['dependent_addition', 'missed_inception'])){ + if(in_array($params['action_type'], ['dependent_addition', 'missed_inception'])){ $action_type_string = "addition"; - } + } $policyTransactionData = [ @@ -4928,9 +4987,9 @@ class EmpDataServiceController extends BaseController 'policy_end_date' => $policy_data['policy_end_date'] ?? null, 'data_received_date' => $policy_data['data_received_date'] ?? null, 'closure_date' => $policy_data['closure_date'] ?? null, - 'emp_count' => $policy_data['emp_count'] ?? null, - 'dependent_count' => $policy_data['dependent_count'] ?? null, - 'revenue_type' => ($lead_data['lead_type'] == 1 ? "NA" : ($lead_data['lead_type'] == 2 ? "EA" : "EANR") ) ?? null, + 'emp_count' => $params['no_of_insured'] ?? null, + 'dependent_count' => $params['no_of_dependent'] ?? null, + 'revenue_type' => isset($lead_data['lead_type']) ?($lead_data['lead_type'] == 1 ? "NA" : ($lead_data['lead_type'] == 2 ? "EA" : "EANR") ) : null, 'co_share' => $policy_data['co_share'] ?? 0, 'pre_payable_by' => $policy_data['pre_payable_by'] ?? 1, 'renewal_date' => $policy_data['policy_end_date'] ?? null, @@ -4939,7 +4998,8 @@ class EmpDataServiceController extends BaseController 'is_cd_reduce_from_bds' => 0, 'client_type_id' => 0, - 'status' => "co_insurer_pending", + // 'status' => "co_insurer_pending", + 'status' => "completed", 'action_type' => $action_type_string ?? null, 'endorsement_no' => $params['endorsement_no'] ?? null, @@ -4954,18 +5014,23 @@ class EmpDataServiceController extends BaseController return $policyTransactionData; } - private function ConstructPTShareData($policy_data, $pt_id) + private function ConstructPTShareData($policy_data, $pt_id, $params) { $coShareData = [ 'pt_id' => $pt_id, 'insurer_id' => $policy_data['insurer_id'], 'insurer_branch_id' => $policy_data['insurer_branch_id'], + 'bp_amt' => $params['base_premium'], + 'cop_amt' => $params['base_premium'], + 'bp_gst_amt' => $params['gst'], + 'bp_sgst' => 9, + 'bp_cgst' => 9, 'co_share_type' => 1, + 'co_share_per' => 100, ]; $this->myLogger->logme("error", "Constructed PT co-share data: " . json_encode($coShareData)); - return $coShareData; } @@ -5113,8 +5178,117 @@ class EmpDataServiceController extends BaseController } + // -------------------------------------------------------------------------------------------------------------------------------- + + public function getInceptionBasePremium($ids) + { + $this->myLogger->logme('error', 'getInceptionBasePremium called with the params : ' . json_encode($ids ?? "")); + + // Validate input + if (empty($ids) || !is_array($ids)) { + return ['base_premium' => 0, 'gst' => 0]; + } + // Sanitize IDs to ensure they are integers + $sanitizedIds = array_filter(array_map('intval', $ids), function ($id) { + return $id > 0; + }); + if (empty($sanitizedIds)) { + return ['base_premium' => 0, 'gst' => 0]; + } + // Use parameter binding for security + $placeholders = str_repeat('?,', count($sanitizedIds) - 1) . '?'; + $amount = $this->employeePolicyModel->query(" + SELECT SUM(employee_polices.rata_premimum) AS base_premium, + SUM(employee_polices.gst) AS gst + FROM employee_polices + JOIN employees ON employees.id = employee_polices.employee_id + WHERE employee_polices.id IN ($placeholders) + ", $sanitizedIds)->getRowArray(); + + $this->myLogger->logme('error', 'Get Last Executed Query : '.db_connect()->getLastQuery()); + + // Ensure we return proper numeric values + return [ + 'base_premium' => round($amount['base_premium'] ?? 0, 2), + 'gst' => round($amount['gst'] ?? 0, 2) + ]; + } + + public function getDeletionBasePremium($ids, $params) + { + $this->myLogger->logme('error', 'getDeletionBasePremium called with the params : ' . json_encode(['emp_policy_id' => $ids ?? [], 'params' => $params ?? []] ?? "")); + + // Validate input + if (empty($ids) || !is_array($ids)) { + return ['base_premium' => 0, 'gst' => 0]; + } + + // Sanitize IDs to ensure they are integers + $sanitizedIds = array_filter(array_map('intval', $ids), function ($id) { + return $id > 0; + }); + + if (empty($sanitizedIds)) { + return ['base_premium' => 0, 'gst' => 0]; + } + + // Validate client_policy_id parameter + if (empty($params['client_policy_id'])) { + return ['base_premium' => 0, 'gst' => 0]; + } + + $get_insurer_id_from_client_policy = $this->clientPolicyModel + ->select('insurers.deletion_add_day') + ->join('insurers', 'client_policy.insurer_id = insurers.id') + ->where('client_policy.id', intval($params['client_policy_id'])) + ->first(); + + $add_one_day = 0; + if (isset($get_insurer_id_from_client_policy['deletion_add_day']) && $get_insurer_id_from_client_policy['deletion_add_day'] == 1) { + $add_one_day = 1; + } + + // Use parameter binding for security + $placeholders = str_repeat('?,', count($sanitizedIds) - 1) . '?'; + + // Prepare parameters array (ids repeated twice for both IN clauses) + $queryParams = array_merge($sanitizedIds, $sanitizedIds); + + $amount = $this->employeePolicyModel->query(" + SELECT + SUM(ROUND( + ((employee_polices.rata_premimum * (DATEDIFF(employee_polices.policy_end_date, emp_endorsement.new_value) + ?)) / 365), + 2 + )) AS base_premium, + SUM(ROUND( + (((employee_polices.rata_premimum * (DATEDIFF(employee_polices.policy_end_date, emp_endorsement.new_value) + ?)) / 365) * 0.18), + 2 + )) AS gst + FROM + employee_polices + JOIN + employees ON employees.id = employee_polices.employee_id + JOIN + emp_endorsement ON emp_endorsement.pk = employee_polices.id + WHERE + employee_polices.id IN ($placeholders) + AND emp_endorsement.pk IN ($placeholders) + AND employee_polices.claim_status = 0 + AND emp_endorsement.field_name = 'date_of_exit' + AND emp_endorsement.is_active = 1 + AND emp_endorsement.actions = 'd' + AND emp_endorsement.status != 'truncated' + ", array_merge([$add_one_day, $add_one_day], $queryParams))->getRowArray(); + + $this->myLogger->logme('error', 'Get Last Executed Query : '.db_connect()->getLastQuery()); + // Ensure we return proper numeric values with rounding + return [ + 'base_premium' => round($amount['base_premium'] ?? 0, 2), + 'gst' => round($amount['gst'] ?? 0, 2) + ]; + } } diff --git a/app/Models/EmployeePolicyModel.php b/app/Models/EmployeePolicyModel.php index e6b02543..82a8cb3b 100755 --- a/app/Models/EmployeePolicyModel.php +++ b/app/Models/EmployeePolicyModel.php @@ -1251,6 +1251,9 @@ class EmployeePolicyModel extends Model ee.id as emp_endorsement_primarykey, ep.id as emp_policy_primarykey, e.id as employees_primarykey, + e.relationship, + e.emp_code, + e.name as emp_name, ee.file_id, ee.group_key, ( @@ -2098,4 +2101,6 @@ class EmployeePolicyModel extends Model // var_dump($this->db->getLastQuery()); return $data; } + + } \ No newline at end of file