nhance/app/Libraries/TPAClaimsImportServices/IciciClaimImportService.php

402 lines
17 KiB
PHP

<?php
namespace App\Libraries\TPAClaimsImportServices;
use App\Models\TicketMasterModel;
use App\Models\ClientPolicyModel;
use App\Models\ClientRMModel;
use App\Models\EmployeeModel;
use App\Models\ClaimDumpFileModel;
use App\Models\ClaimsDumpFhplModel;
class IciciClaimImportService extends BaseTpaClaimImportService
{
/**
* MAPPING ARRAYs
*/
protected $mapping = [
["excel_column" => ["col_name" => "POLICY_NAME", "col_index" => 0], "db_column" => "policy_name"],
["excel_column" => ["col_name" => "POLICY_NO", "col_index" => 1], "db_column" => "policy_no"],
["excel_column" => ["col_name" => "UHID", "col_index" => 2], "db_column" => "uhid"],
["excel_column" => ["col_name" => "INSURED_NAME", "col_index" => 3], "db_column" => "insured_name"],
["excel_column" => ["col_name" => "MAIN_MEMBER_NAME", "col_index" => 4], "db_column" => "main_member_name"],
["excel_column" => ["col_name" => "EMPLOYEE_MEMBER_ID", "col_index" => 5], "db_column" => "employee_member_id"],
["excel_column" => ["col_name" => "GRADE", "col_index" => 6], "db_column" => "grade"],
["excel_column" => ["col_name" => "RELATION", "col_index" => 7], "db_column" => "relation"],
["excel_column" => ["col_name" => "AGE", "col_index" => 8], "db_column" => "age"],
["excel_column" => ["col_name" => "GENDER", "col_index" => 9], "db_column" => "gender"],
["excel_column" => ["col_name" => "DIAGNOSIS", "col_index" => 10], "db_column" => "diagnosis"],
["excel_column" => ["col_name" => "CLAIMED_AMOUNT", "col_index" => 11], "db_column" => "claimed_amount"],
["excel_column" => ["col_name" => "NET_SANCT_AMT", "col_index" => 12], "db_column" => "net_sanct_amt"],
["excel_column" => ["col_name" => "COPAYMENT_AMT", "col_index" => 13], "db_column" => "copayment_amt"],
["excel_column" => ["col_name" => "DISALLOWED_AMOUNT", "col_index" => 14], "db_column" => "disallowed_amount"],
["excel_column" => ["col_name" => "REASON_FOR_DISALLOWANCE", "col_index" => 15], "db_column" => "reason_for_disallowance"],
["excel_column" => ["col_name" => "PAYMENT_AMOUNT", "col_index" => 16], "db_column" => "payment_amount"],
["excel_column" => ["col_name" => "SUM_INSURED", "col_index" => 17], "db_column" => "sum_insured"],
["excel_column" => ["col_name" => "BAL_SUM_INSURED", "col_index" => 18], "db_column" => "bal_sum_insured"],
["excel_column" => ["col_name" => "TYPE_OF_CLAIM", "col_index" => 19], "db_column" => "type_of_claim"],
["excel_column" => ["col_name" => "Claim_r_Os_Amt", "col_index" => 20], "db_column" => "claim_r_os_amt"],
["excel_column" => ["col_name" => "Updated_status", "col_index" => 21], "db_column" => "updated_status"],
["excel_column" => ["col_name" => "Disease_Category", "col_index" => 22], "db_column" => "disease_category"],
["excel_column" => ["col_name" => "POLICY_START_DATE", "col_index" => 23], "db_column" => "policy_start_date"],
["excel_column" => ["col_name" => "POLICY_END_DATE", "col_index" => 24], "db_column" => "policy_end_date"],
["excel_column" => ["col_name" => "CLAIM_NUMBER", "col_index" => 25], "db_column" => "claim_number"],
["excel_column" => ["col_name" => "AL_NO", "col_index" => 26], "db_column" => "al_no"],
["excel_column" => ["col_name" => "HOSPITAL_CODE", "col_index" => 27], "db_column" => "hospital_code"],
["excel_column" => ["col_name" => "HOSPITAL_ID", "col_index" => 28], "db_column" => "hospital_id"],
["excel_column" => ["col_name" => "HOSPITAL_NAME", "col_index" => 29], "db_column" => "hospital_name"],
["excel_column" => ["col_name" => "TREATMENT_TAKEN", "col_index" => 30], "db_column" => "treatment_taken"],
["excel_column" => ["col_name" => "CF_Utilised_Amounnt", "col_index" => 31], "db_column" => "cf_utilised_amount"],
["excel_column" => ["col_name" => "DT_OF_DEFICIENCIES_SENT", "col_index" => 32], "db_column" => "dt_of_deficiencies_sent"],
["excel_column" => ["col_name" => "DT_OF_DEFICIENCIES_RECIEVED", "col_index" => 33], "db_column" => "dt_of_deficiencies_received"],
["excel_column" => ["col_name" => "PAYMENT_DATE", "col_index" => 34], "db_column" => "payment_date"],
["excel_column" => ["col_name" => "PAYEE_NAME", "col_index" => 35], "db_column" => "payee_name"],
["excel_column" => ["col_name" => "PAYMENT_MODE", "col_index" => 36], "db_column" => "payment_mode"],
["excel_column" => ["col_name" => "CHEQUE_NUMBER", "col_index" => 37], "db_column" => "cheque_number"],
["excel_column" => ["col_name" => "DOA", "col_index" => 38], "db_column" => "doa"],
["excel_column" => ["col_name" => "DOD", "col_index" => 39], "db_column" => "dod"],
["excel_column" => ["col_name" => "HOSPITAL_CITY", "col_index" => 40], "db_column" => "hospital_city"],
["excel_column" => ["col_name" => "HOSPITAL_STATE", "col_index" => 41], "db_column" => "hospital_state"],
["excel_column" => ["col_name" => "REJECTED_QUERY_REASON", "col_index" => 42], "db_column" => "rejected_query_reason"],
["excel_column" => ["col_name" => "REJECTED_QUERY_DESC", "col_index" => 43], "db_column" => "rejected_query_desc"],
["excel_column" => ["col_name" => "REJECTED_QUERY_CLOSED_DATE", "col_index" => 44], "db_column" => "rejected_query_closed_date"],
["excel_column" => ["col_name" => "REJREOPEN_CLOSURE_DATE", "col_index" => 45], "db_column" => "rejreopen_closure_date"],
["excel_column" => ["col_name" => "CLAIM_CLASSIFICATION", "col_index" => 46], "db_column" => "claim_classification"],
["excel_column" => ["col_name" => "TAGGED_INWARD_NO", "col_index" => 47], "db_column" => "tagged_inward_no"],
["excel_column" => ["col_name" => "INWARD_DATE", "col_index" => 48], "db_column" => "inward_date"],
["excel_column" => ["col_name" => "ICD_ID_L3", "col_index" => 49], "db_column" => "icd_id_l3"],
["excel_column" => ["col_name" => "Incidence_Count", "col_index" => 50], "db_column" => "incidence_count"],
["excel_column" => ["col_name" => "FLEXI_OPTION", "col_index" => 51], "db_column" => "flexi_option"],
["excel_column" => ["col_name" => "Base_TopUp_Option", "col_index" => 52], "db_column" => "base_topup_option"],
["excel_column" => ["col_name" => "POLICY_GROUP_NAME", "col_index" => 53], "db_column" => "policy_group_name"],
["excel_column" => ["col_name" => "Relation_Group", "col_index" => 54], "db_column" => "relation_group"],
["excel_column" => ["col_name" => "Age_Band", "col_index" => 55], "db_column" => "age_band"],
["excel_column" => ["col_name" => "Work_Location", "col_index" => 56], "db_column" => "work_location"],
["excel_column" => ["col_name" => "ILTC_TAG", "col_index" => 57], "db_column" => "iltc_tag"],
];
protected $ticketMasterMapping = [
// Employee / Member
'employee_member_id' => 'emp_code',
// Claim
'claim_number' => 'claim_number',
'claimed_amount' => 'claim_amount',
'claim_status' => 'tpa_claim_status',
// Dates
'doa' => 'doa',
'dod' => 'dod',
'payment_date' => 'settled_date',
// Hospital
'hospital_name' => 'hospital_name',
'hospital_city' => 'hospital_city',
'hospital_state' => 'hospital_state',
'cheque_number' => 'utr_details',
'uhid' => 'tpa_no',
'rejected_query_desc' => 'claim_description',
'type_of_claim' => 'tpa_claim_type',
];
protected $statusMapping = [
'PAID' => 11,
'REJECTED' => 8,
];
protected $dateColumns = [
'policy_start_date',
'policy_end_date',
'dt_of_deficiencies_sent',
'dt_of_deficiencies_received',
'payment_date',
'doa',
'dod',
'rejected_query_closed_date',
'rejreopen_closure_date',
];
/**
* ABSTRACT FUNCTIONs
*/
public function bulkInsertTPATable(array $data): bool
{
if (empty($data)) {
return false;
}
$builder = $this->db->table('claims_dump_icici');
return $builder->insertBatch($data);
}
public function importClaimMaster(array $data): bool
{
if (empty($data)) {
return false;
}
$ticketMasterModel = new TicketMasterModel();
return $ticketMasterModel->insertBatch($data);
}
public function updateTicketIdInTPATable(): bool
{
if (empty($data)) {
return false;
}
$builder = $this->db->table('claims_dump_icici');
return $builder->upsertBatch($data, 'id');
}
public function updateTicketMasterRejectedReasonInTPATable($data): bool
{
if (empty($data)) {
return false;
}
$builder = $this->db->table('claims_dump_icici');
return $builder->updateBatch($data, 'id');
}
/**
* MAPPING FUNCTIONs
*/
public function mapTPAData(array $rows, $file_id): array
{
$ClientPolicyModel = new ClaimDumpFileModel();
$file_data = $ClientPolicyModel->where('id', $file_id)->first();
$mapped = [];
foreach ($rows as $row) {
$item = [];
foreach ($this->mapping as $map) {
$excelColumn = $map['excel_column']['col_name'];
$dbColumn = $map['db_column'];
$value = $row[$excelColumn] ?? null;
$item[$dbColumn] = $value;
}
foreach ($this->dateColumns as $key => $value) {
$item[$value] = change_date_format($item[$value] ?? null);
}
$params = [
'doa' => change_date_format($item['doa'] ?? '') ?? null,
'employee_member_id' => $item['employee_member_id'] ?? null,
'claimed_amount' => $item['claimed_amount'] ?? null,
'uhid' => $item['uhid'] ?? null
];
$is_duplicate = $this->checkDuplicateTpaClaim('claims_dump_icici', $params);
if ($is_duplicate) {
$item = [];
continue;
}
$item['file_id'] = $file_id ?? null;
$item['client_id'] = $file_data['client_id'] ?? null;
$item['client_policy_id'] = $file_data['client_policy_id'] ?? null;
$item['created_by'] = $file_data['created_by'] ?? null;
$mapped[] = $item;
}
return $mapped;
}
public function mapClaimMasterData($file_id): array
{
$ClientPolicyModel = new ClaimDumpFileModel();
$file_data = $ClientPolicyModel->where('id', $file_id)->first();
$tpaClaimDumpData = $this->getTpaClaimDumpData('claims_dump_icici', ['file_id' => $file_id]);
if (empty($tpaClaimDumpData)) {
return ['status' => false, 'message' => "No data to insert in TICKET MASTER"];
}
$ClientPolicyModel = new ClientPolicyModel();
$client_policy_data = $ClientPolicyModel
->select("
client_policy.*,
(
SELECT id
FROM client_rm
WHERE is_active = 1
AND level = 3
AND client_id = client_policy.client_id
ORDER BY id ASC
LIMIT 1
) AS acm_id
")
->where('client_policy.id', $file_data['client_policy_id'])
->where('client_policy.is_active', 1)
->first();
try {
$mapped = [];
$rejecetd_reason = [];
foreach ($tpaClaimDumpData as $row) {
$params = [
'doa' => change_date_format($row['doa'] ?? '') ?? null,
'emp_code' => $row['employee_member_id'] ?? null,
'claim_amount' => $row['claim_amount'] ?? null,
'tpa_no' => $row['uhid'] ?? null
];
$isduplicate = $this->checkDublicateTicketMasterClaim($params);
if ($isduplicate) {
$reason = "This claim already exists in our system.";
$rejecetd_reason[] = ["id" => $row['id'], "master_reject_reason" => $reason];
continue;
}
$item = [];
$item['client_id'] = $client_policy_data['client_id'] ?? null;
$item['client_policy_id'] = $client_policy_data['id'] ?? null;
$item['insurer_id'] = $client_policy_data['insurer_id'] ?? null;
$item['tpa_id'] = $client_policy_data['tpa_id'] ?? null;
$item['acm_id'] = $client_policy_data['acm_id'] ?? null;
$item['policy_no'] = $client_policy_data['policy_no'] ?? null;
$item['relationship'] = $this->convertRelation($row['relation_group'] ?? '');
$employee_data = $this->getEmployeeDetails($file_data['client_id'], $file_data['client_policy_id'], $row['employee_member_id'], $item['relationship']);
if (!empty($employee_data)) {
$item['emp_id'] = $employee_data['id'] ?? null;
$item['emp_name'] = $employee_data['name'] ?? null;
$item['emp_code'] = $employee_data['emp_code'] ?? null;
$item['emp_mail'] = $employee_data['email_corporate'] ?? null;
$item['emp_mobile'] = $employee_data['mobile'] ?? null;
if (!empty($employee_data['insured_emp_id'])) {
$item['insured_emp_id'] = $employee_data['insured_emp_id'] ?? null;
$item['insured_name'] = $employee_data['insured_name'] ?? null;
}else{
$reason = 'This Dependent employee not exist in our system.';
$rejecetd_reason[] = ["id" => $row['id'], "master_reject_reason" => $reason];
continue;
}
} else {
$reason = 'This employee not exist in our system.';
$rejecetd_reason[] = ["id" => $row['id'], "master_reject_reason" => $reason];
continue;
}
foreach ($this->ticketMasterMapping as $tpaKey => $ticketMasterKey) {
$item[$ticketMasterKey] = array_key_exists($tpaKey, $row) ? $row[$tpaKey] : null;
}
// Meta fields
$item['claim_status_id'] = $statusMapping[$row['updated_status']] ?? 61;
$item['claim_dump_ref_id'] = $row['id'];
$item['file_id'] = $file_id;
$item['created_by'] = $file_data['created_by'] ?? null;
$item['claim_type'] = 1;
$item['priority'] = 1;
$item['mode_of_intimation'] = 5;
$item['ticket_type_id'] = 1;
$mapped[] = $item;
}
return ['status' => true, 'mapped_array' => $mapped, 'rejected_reason_array' => $rejecetd_reason];
} catch (\Throwable $th) {
$errorData = [
'message' => $th->getMessage(),
'file' => $th->getFile(),
'line' => $th->getLine(),
'code' => $th->getCode(),
'trace' => $th->getTraceAsString(),
'trace_array' => $th->getTrace(), // full array version (optional)
'function' => $th->getTrace()[0]['function'] ?? null,
'class' => $th->getTrace()[0]['class'] ?? null,
];
return ['status' => false, "message" => $errorData['message'], 'error_data' => $errorData];
}
}
/**
* HELPER FUNCTIONs
*/
public function convertRelation(?string $relation): ?string
{
if (empty($relation)) {
return null;
}
$relation = strtolower($relation);
if (str_contains($relation, 'self')) {
return 'self';
}
if (str_contains($relation, 'spouse') || str_contains($relation, 'wife') || str_contains($relation, 'husband')) {
return 'spouse';
}
if (str_contains($relation, 'daughter')) {
return 'daughter';
}
if (str_contains($relation, 'son')) {
return 'son';
}
if (str_contains($relation, 'father in law') || str_contains($relation, 'father-in-law')) {
return 'father-in-law';
}
if (str_contains($relation, 'mother in law') || str_contains($relation, 'mother-in-law')) {
return 'mother-in-law';
}
if (str_contains($relation, 'father')) {
return 'father';
}
if (str_contains($relation, 'mother')) {
return 'mother';
}
return null; // unmatched case
}
}