nhance/app/Libraries/TPAClaimsImportServices/FhplClaimImportService.php

483 lines
24 KiB
PHP

<?php
namespace App\Libraries\TPAClaimsImportServices;
use App\Models\TicketMasterModel;
use App\Models\ClientPolicyModel;
use App\Models\ClaimDumpFileModel;
use App\Models\ClaimsDumpFhplModel;
class FhplClaimImportService extends BaseTpaClaimImportService
{
/**
* MAPPING ARRAYs
*/
protected $mapping = [
["excel_column"=>["col_name"=>"Requesttype","col_index"=>0],"db_column"=>"request_type"],
["excel_column"=>["col_name"=>"Intimation ID","col_index"=>1],"db_column"=>"intimation_id"],
["excel_column"=>["col_name"=>"Intimation Date","col_index"=>2],"db_column"=>"intimation_date"],
["excel_column"=>["col_name"=>"claimid","col_index"=>3],"db_column"=>"claim_id"],
["excel_column"=>["col_name"=>"slno","col_index"=>4],"db_column"=>"sl_no"],
["excel_column"=>["col_name"=>"UHIDNO","col_index"=>5],"db_column"=>"uhid_no"],
["excel_column"=>["col_name"=>"Membername","col_index"=>6],"db_column"=>"member_name"],
["excel_column"=>["col_name"=>"Main Memuhidno","col_index"=>7],"db_column"=>"main_member_uhid_no"],
["excel_column"=>["col_name"=>"Main Memname","col_index"=>8],"db_column"=>"main_member_name"],
["excel_column"=>["col_name"=>"Gender","col_index"=>9],"db_column"=>"gender"],
["excel_column"=>["col_name"=>"DOB","col_index"=>10],"db_column"=>"dob"],
["excel_column"=>["col_name"=>"Yrs","col_index"=>11],"db_column"=>"years"],
["excel_column"=>["col_name"=>"relationship","col_index"=>12],"db_column"=>"relationship"],
["excel_column"=>["col_name"=>"employeeid","col_index"=>13],"db_column"=>"employee_id"],
["excel_column"=>["col_name"=>"Mobile","col_index"=>14],"db_column"=>"mobile"],
["excel_column"=>["col_name"=>"Email","col_index"=>15],"db_column"=>"email"],
["excel_column"=>["col_name"=>"Policy No","col_index"=>16],"db_column"=>"policy_no"],
["excel_column"=>["col_name"=>"Policy Start Date","col_index"=>17],"db_column"=>"policy_start_date"],
["excel_column"=>["col_name"=>"Policy Commencing Date","col_index"=>18],"db_column"=>"policy_commencing_date"],
["excel_column"=>["col_name"=>"Policy Expiry Date","col_index"=>19],"db_column"=>"policy_expiry_date"],
["excel_column"=>["col_name"=>"Organisationname","col_index"=>20],"db_column"=>"organisation_name"],
["excel_column"=>["col_name"=>"Claimreceiveddate","col_index"=>21],"db_column"=>"claim_received_date"],
["excel_column"=>["col_name"=>"Admdate","col_index"=>22],"db_column"=>"admission_date"],
["excel_column"=>["col_name"=>"Dis Date","col_index"=>23],"db_column"=>"discharge_date"],
["excel_column"=>["col_name"=>"Diagnosis","col_index"=>24],"db_column"=>"diagnosis"],
["excel_column"=>["col_name"=>"Service Type","col_index"=>25],"db_column"=>"service_type"],
["excel_column"=>["col_name"=>"Service Sub Type","col_index"=>26],"db_column"=>"service_sub_type"],
["excel_column"=>["col_name"=>"icdcode First Level","col_index"=>27],"db_column"=>"icd_code_first_level"],
["excel_column"=>["col_name"=>"icdcode Second Level","col_index"=>28],"db_column"=>"icd_code_second_level"],
["excel_column"=>["col_name"=>"icdcode Third Level","col_index"=>29],"db_column"=>"icd_code_third_level"],
["excel_column"=>["col_name"=>"Claim Type","col_index"=>30],"db_column"=>"claim_type"],
["excel_column"=>["col_name"=>"Providername","col_index"=>31],"db_column"=>"provider_name"],
["excel_column"=>["col_name"=>"provideraddress","col_index"=>32],"db_column"=>"provider_address"],
["excel_column"=>["col_name"=>"providerplace","col_index"=>33],"db_column"=>"provider_place"],
["excel_column"=>["col_name"=>"providerstate","col_index"=>34],"db_column"=>"provider_state"],
["excel_column"=>["col_name"=>"Provider Pincode","col_index"=>35],"db_column"=>"provider_pincode"],
["excel_column"=>["col_name"=>"PROVIDERTYPE","col_index"=>36],"db_column"=>"provider_type"],
["excel_column"=>["col_name"=>"Provider Identification","col_index"=>37],"db_column"=>"provider_identification"],
["excel_column"=>["col_name"=>"coverageamount","col_index"=>38],"db_column"=>"coverage_amount"],
["excel_column"=>["col_name"=>"claimamount","col_index"=>39],"db_column"=>"claim_amount"],
["excel_column"=>["col_name"=>"billedamount","col_index"=>40],"db_column"=>"billed_amount"],
["excel_column"=>["col_name"=>"Disallowed Amount","col_index"=>41],"db_column"=>"disallowed_amount"],
["excel_column"=>["col_name"=>"Dis Allowence Reason1","col_index"=>42],"db_column"=>"dis_allowance_reason_1"],
["excel_column"=>["col_name"=>"Dis Allowence Reason2","col_index"=>43],"db_column"=>"dis_allowance_reason_2"],
["excel_column"=>["col_name"=>"settledamt","col_index"=>44],"db_column"=>"settled_amount"],
["excel_column"=>["col_name"=>"Incurred Amount","col_index"=>45],"db_column"=>"incurred_amount"],
["excel_column"=>["col_name"=>"Discountamount","col_index"=>46],"db_column"=>"discount_amount"],
["excel_column"=>["col_name"=>"TDSAmount","col_index"=>47],"db_column"=>"tds_amount"],
["excel_column"=>["col_name"=>"Net Amount Paid","col_index"=>48],"db_column"=>"net_amount_paid"],
["excel_column"=>["col_name"=>"Co Payment","col_index"=>49],"db_column"=>"co_payment"],
["excel_column"=>["col_name"=>"Current Claim Status","col_index"=>50],"db_column"=>"current_claim_status"],
["excel_column"=>["col_name"=>"Balance Suminsured","col_index"=>51],"db_column"=>"balance_sum_insured"],
["excel_column"=>["col_name"=>"Chequeno","col_index"=>52],"db_column"=>"cheque_no"],
["excel_column"=>["col_name"=>"chequedate","col_index"=>53],"db_column"=>"cheque_date"],
["excel_column"=>["col_name"=>"Claim Passed Date","col_index"=>54],"db_column"=>"claim_passed_date"],
["excel_column"=>["col_name"=>"Settled Date","col_index"=>55],"db_column"=>"settled_date"],
["excel_column"=>["col_name"=>"Pending Remarks","col_index"=>56],"db_column"=>"pending_remarks"],
["excel_column"=>["col_name"=>"Ir Investigation","col_index"=>57],"db_column"=>"ir_investigation"],
["excel_column"=>["col_name"=>"Date of IR","col_index"=>58],"db_column"=>"ir_date"],
["excel_column"=>["col_name"=>"Date of IRretrieval Date","col_index"=>59],"db_column"=>"ir_retrieval_date"],
["excel_column"=>["col_name"=>"first Reminder","col_index"=>60],"db_column"=>"first_reminder"],
["excel_column"=>["col_name"=>"Second Reminder","col_index"=>61],"db_column"=>"second_reminder"],
["excel_column"=>["col_name"=>"Rejection Remarks","col_index"=>62],"db_column"=>"rejection_remarks"],
["excel_column"=>["col_name"=>"Payee name","col_index"=>63],"db_column"=>"payee_name"],
["excel_column"=>["col_name"=>"Treatment Type","col_index"=>64],"db_column"=>"treatment_type"],
["excel_column"=>["col_name"=>"roomdays","col_index"=>65],"db_column"=>"room_days"],
["excel_column"=>["col_name"=>"icudays","col_index"=>66],"db_column"=>"icu_days"],
["excel_column"=>["col_name"=>"totalstay","col_index"=>67],"db_column"=>"total_stay"],
["excel_column"=>["col_name"=>"Room Rent Claimed","col_index"=>68],"db_column"=>"room_rent_claimed"],
["excel_column"=>["col_name"=>"ICU Claimed","col_index"=>69],"db_column"=>"icu_claimed"],
["excel_column"=>["col_name"=>"ICU Related","col_index"=>70],"db_column"=>"icu_related"],
["excel_column"=>["col_name"=>"Nursing Claimed","col_index"=>71],"db_column"=>"nursing_claimed"],
["excel_column"=>["col_name"=>"Nursing Charges","col_index"=>72],"db_column"=>"nursing_charges"],
["excel_column"=>["col_name"=>"Room Rent Related","col_index"=>73],"db_column"=>"room_rent_related"],
["excel_column"=>["col_name"=>"Professional Charges","col_index"=>74],"db_column"=>"professional_charges"],
["excel_column"=>["col_name"=>"Drugs Medication Consumables Investigationsetc","col_index"=>75],"db_column"=>"drugs_medication_consumables"],
["excel_column"=>["col_name"=>"Investigations Procedures IP","col_index"=>76],"db_column"=>"investigations_procedures_ip"],
["excel_column"=>["col_name"=>"Domicillary Hospitalization","col_index"=>77],"db_column"=>"domicillary_hospitalization"],
["excel_column"=>["col_name"=>"Maternity","col_index"=>78],"db_column"=>"maternity"],
["excel_column"=>["col_name"=>"Day Care","col_index"=>79],"db_column"=>"day_care"],
["excel_column"=>["col_name"=>"Operation Theatre","col_index"=>80],"db_column"=>"operation_theatre"],
["excel_column"=>["col_name"=>"Organ Donar","col_index"=>81],"db_column"=>"organ_donor"],
["excel_column"=>["col_name"=>"Ancilliary Services","col_index"=>82],"db_column"=>"ancillary_services"],
["excel_column"=>["col_name"=>"Dental","col_index"=>83],"db_column"=>"dental"],
["excel_column"=>["col_name"=>"Out Patient Coverage","col_index"=>84],"db_column"=>"out_patient_coverage"],
["excel_column"=>["col_name"=>"Personal Accident","col_index"=>85],"db_column"=>"personal_accident"],
["excel_column"=>["col_name"=>"Critical Illness","col_index"=>86],"db_column"=>"critical_illness"],
["excel_column"=>["col_name"=>"Health Check Up","col_index"=>87],"db_column"=>"health_check_up"],
["excel_column"=>["col_name"=>"Spectacles Contact Lenses Hearing Aid","col_index"=>88],"db_column"=>"spectacles_contact_lenses_hearing_aid"],
["excel_column"=>["col_name"=>"Notes","col_index"=>89],"db_column"=>"notes"],
["excel_column"=>["col_name"=>"Buffer Amount","col_index"=>90],"db_column"=>"buffer_amount"],
["excel_column"=>["col_name"=>"tertiaryamount","col_index"=>91],"db_column"=>"tertiary_amount"],
["excel_column"=>["col_name"=>"insurancename","col_index"=>92],"db_column"=>"insurance_name"],
["excel_column"=>["col_name"=>"Roname","col_index"=>93],"db_column"=>"ro_name"],
["excel_column"=>["col_name"=>"Class Of Accommodation","col_index"=>94],"db_column"=>"class_of_accommodation"],
["excel_column"=>["col_name"=>"claimcreateddatetime","col_index"=>95],"db_column"=>"claim_created_datetime"],
["excel_column"=>["col_name"=>"Insurer Claim ID","col_index"=>96],"db_column"=>"insurer_claim_id"],
["excel_column"=>["col_name"=>"Gipsa","col_index"=>97],"db_column"=>"gipsa"],
["excel_column"=>["col_name"=>"Date Of Joining","col_index"=>98],"db_column"=>"date_of_joining"],
["excel_column"=>["col_name"=>"GIPSAHospital","col_index"=>99],"db_column"=>"gipsa_hospital"],
["excel_column"=>["col_name"=>"Package","col_index"=>100],"db_column"=>"package"],
["excel_column"=>["col_name"=>"Is NIDB","col_index"=>101],"db_column"=>"is_nidb"],
["excel_column"=>["col_name"=>"NIDB Removed Date","col_index"=>102],"db_column"=>"nidb_removed_date"],
["excel_column"=>["col_name"=>"Investigationdate","col_index"=>103],"db_column"=>"investigation_date"],
["excel_column"=>["col_name"=>"Investigation Retrieval Date","col_index"=>104],"db_column"=>"investigation_retrieval_date"],
["excel_column"=>["col_name"=>"Reopeneddate","col_index"=>105],"db_column"=>"reopened_date"],
["excel_column"=>["col_name"=>"Referto Insurer Date","col_index"=>106],"db_column"=>"refer_to_insurer_date"],
["excel_column"=>["col_name"=>"Receiveddatefrom Insurer","col_index"=>107],"db_column"=>"received_date_from_insurer"],
["excel_column"=>["col_name"=>"Rejection Category","col_index"=>108],"db_column"=>"rejection_category"],
["excel_column"=>["col_name"=>"Referto Insurer Reasons","col_index"=>109],"db_column"=>"refer_to_insurer_reasons"],
["excel_column"=>["col_name"=>"Is VIP","col_index"=>110],"db_column"=>"is_vip"],
["excel_column"=>["col_name"=>"Main Claim Status","col_index"=>111],"db_column"=>"main_claim_status"],
["excel_column"=>["col_name"=>"Main Claimtype","col_index"=>112],"db_column"=>"main_claim_type"],
["excel_column"=>["col_name"=>"icd Third Level Code","col_index"=>113],"db_column"=>"icd_third_level_code"],
["excel_column"=>["col_name"=>"Benefit Plan Name","col_index"=>114],"db_column"=>"benefit_plan_name"],
["excel_column"=>["col_name"=>"zone","col_index"=>115],"db_column"=>"zone"],
["excel_column"=>["col_name"=>"Grade","col_index"=>116],"db_column"=>"grade"],
["excel_column"=>["col_name"=>"Last Modified Date","col_index"=>117],"db_column"=>"last_modified_date"],
["excel_column"=>["col_name"=>"Temp MOU","col_index"=>118],"db_column"=>"temp_mou"],
];
protected $ticketMasterMapping = [
// Claim / Reference
'claim_id' => 'claim_number',
// Employee / Member
'employee_id' => 'emp_code',
// Claim Dates
'admission_date' => 'doa',
'discharge_date' => 'dod',
'claim_received_date' => 'date_of_intimat',
'claim_passed_date' => 'approved_date',
'settled_date' => 'settled_date',
'current_claim_status' => 'tpa_claim_status',
// Amounts
'claim_amount' => 'claim_amount',
'settled_amount' => 'settled_amount',
'disallowed_amount' => 'denial_reason',
'coverage_amount' => 'si_amt',
// Hospital
'provider_name' => 'hospital_name',
'provider_address' => 'hospital_address',
'provider_state' => 'hospital_state',
'provider_place' => 'hospital_city',
'provider_pincode' => 'hospital_pin_code',
// Remarks / Description
'diagnosis' => 'claim_description',
'rejection_remarks' => 'return_remark',
// Payment
'cheque_no' => 'utr_details',
'uhid_no' => 'tpa_no',
'claim_type' => 'tpa_claim_type',
];
protected $statusMapping = [
'Settled' => 11,
'Rejected' => 8,
];
protected $dateColumns = [
'intimation_date',
'policy_start_date',
'policy_commencing_date',
'policy_expiry_date',
'claim_received_date',
'admission_date',
'discharge_date',
'cheque_date',
'claim_passed_date',
'settled_date',
'ir_date',
'ir_retrieval_date',
'first_reminder',
'second_reminder',
'date_of_joining',
'nidb_removed_date',
'investigation_date',
'investigation_retrieval_date',
'reopened_date',
'refer_to_insurer_date',
'received_date_from_insurer',
'claim_created_datetime',
'last_modified_date'
];
/**
* ABSTRACT FUNCTIONs
*/
public function bulkInsertTPATable(array $data): bool
{
if (empty($data)) {
return false;
}
$builder = $this->db->table('claims_dump_fhpl');
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_fhpl');
return $builder->updateBatch($data, 'id');
}
public function updateTicketMasterRejectedReasonInTPATable($data): bool
{
if (empty($data)) {
return false;
}
$builder = $this->db->table('claims_dump_fhpl');
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] = trim($value);
}
foreach ($this->dateColumns as $key => $value) {
$item[$value] = change_date_format($item[$value] ?? null, 'd-M-y', 'Y-m-d');
}
$params = [
'admission_date' => $item['admission_date'] ?? null,
'employee_id' => $item['employee_id'] ?? null,
'claim_amount' => $item['claim_amount'] ?? null,
'uhid_no' => $item['uhid_no'] ?? null
];
$is_duplicate = $this->checkDuplicateTpaClaim('claims_dump_fhpl', $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_fhpl', ['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['admission_date'] ?? '') ?? null,
'emp_code' => $row['employee_id'] ?? null,
'claim_amount' => $row['claim_amount'] ?? null,
'tpa_no' => $row['uhid_no'] ?? 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['relationship'] ?? null);
$employee_data = $this->getEmployeeDetails($file_data['client_id'], $file_data['client_policy_id'], $row['employee_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['current_claim_status']] ?? 61;
$item['file_id'] = $file_id;
$item['claim_dump_ref_id'] = $row['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
}
}