nhance/app/Libraries/TPAClaimsImportServices/AbhiClaimImportService.php

448 lines
19 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;
class AbhiClaimImportService extends BaseTpaClaimImportService
{
/**
* MAPPING ARRAYs
*/
protected $mapping = [
["excel_column" => ["col_name" => "ABHI Claim No", "col_index" => 0], "db_column" => "abhi_claim_no"],
["excel_column" => ["col_name" => "New ABHI Claim No With Extension", "col_index" => 1], "db_column" => "new_abhi_claim_no_with_extension"],
["excel_column" => ["col_name" => "Unique Count", "col_index" => 2], "db_column" => "unique_count"],
["excel_column" => ["col_name" => "Proposer Name", "col_index" => 3], "db_column" => "proposer_name"],
["excel_column" => ["col_name" => "Patient Name", "col_index" => 4], "db_column" => "patient_name"],
["excel_column" => ["col_name" => "Family ID", "col_index" => 5], "db_column" => "family_id"],
["excel_column" => ["col_name" => "Member Code", "col_index" => 6], "db_column" => "member_code"],
["excel_column" => ["col_name" => "Patient Age", "col_index" => 7], "db_column" => "patient_age"],
["excel_column" => ["col_name" => "Relation", "col_index" => 8], "db_column" => "relation"],
["excel_column" => ["col_name" => "Gender", "col_index" => 9], "db_column" => "gender"],
["excel_column" => ["col_name" => "Certificate No", "col_index" => 10], "db_column" => "certificate_no"],
["excel_column" => ["col_name" => "Master Policy No", "col_index" => 11], "db_column" => "master_policy_no"],
["excel_column" => ["col_name" => "Policy Number", "col_index" => 12], "db_column" => "policy_number"],
["excel_column" => ["col_name" => "Policy From", "col_index" => 13], "db_column" => "policy_from"],
["excel_column" => ["col_name" => "Policy Upto", "col_index" => 14], "db_column" => "policy_upto"],
["excel_column" => ["col_name" => "Product Name", "col_index" => 15], "db_column" => "product_name"],
["excel_column" => ["col_name" => "Product Name 1", "col_index" => 16], "db_column" => "product_name_1"],
["excel_column" => ["col_name" => "Sub - Product", "col_index" => 17], "db_column" => "sub_product"],
["excel_column" => ["col_name" => "Policy Category", "col_index" => 18], "db_column" => "policy_category"],
["excel_column" => ["col_name" => "Policy Category (Carry Forward till Q3 18-19)+(New Q4 18-19 Onward)", "col_index" => 19], "db_column" => "policy_category_cf_q3_q4"],
["excel_column" => ["col_name" => "Sum Insured", "col_index" => 20], "db_column" => "sum_insured"],
["excel_column" => ["col_name" => "Bonus", "col_index" => 21], "db_column" => "bonus"],
["excel_column" => ["col_name" => "Intimation Date", "col_index" => 22], "db_column" => "intimation_date"],
["excel_column" => ["col_name" => "Date Of Doc Rec", "col_index" => 23], "db_column" => "date_of_doc_rec"],
["excel_column" => ["col_name" => "Intimation Final Month", "col_index" => 24], "db_column" => "intimation_final_month"],
["excel_column" => ["col_name" => "Reported Year", "col_index" => 25], "db_column" => "reported_year"],
["excel_column" => ["col_name" => "Reported Qurter", "col_index" => 26], "db_column" => "reported_quarter"],
["excel_column" => ["col_name" => "Hospital Name", "col_index" => 27], "db_column" => "hospital_name"],
["excel_column" => ["col_name" => "Hospital City", "col_index" => 28], "db_column" => "hospital_city"],
["excel_column" => ["col_name" => "Hospital State", "col_index" => 29], "db_column" => "hospital_state"],
["excel_column" => ["col_name" => "Diagnosis", "col_index" => 30], "db_column" => "diagnosis"],
["excel_column" => ["col_name" => "ICD Chapter", "col_index" => 31], "db_column" => "icd_chapter"],
["excel_column" => ["col_name" => "ICD Block", "col_index" => 32], "db_column" => "icd_block"],
["excel_column" => ["col_name" => "ICD Level1", "col_index" => 33], "db_column" => "icd_level1"],
["excel_column" => ["col_name" => "ICD Level2", "col_index" => 34], "db_column" => "icd_level2"],
["excel_column" => ["col_name" => "Procedure Description", "col_index" => 35], "db_column" => "procedure_description"],
["excel_column" => ["col_name" => "PCS Description", "col_index" => 36], "db_column" => "pcs_description"],
["excel_column" => ["col_name" => "DOA", "col_index" => 37], "db_column" => "doa"],
["excel_column" => ["col_name" => "DOD", "col_index" => 38], "db_column" => "dod"],
["excel_column" => ["col_name" => "Claim Type", "col_index" => 39], "db_column" => "claim_type"],
["excel_column" => ["col_name" => "Claim Category", "col_index" => 40], "db_column" => "claim_category"],
["excel_column" => ["col_name" => "Disc Datails", "col_index" => 41], "db_column" => "disc_details"],
["excel_column" => ["col_name" => "Disc Date", "col_index" => 42], "db_column" => "disc_date"],
["excel_column" => ["col_name" => "Hospital Code", "col_index" => 43], "db_column" => "hospital_code"],
["excel_column" => ["col_name" => "Claim Status", "col_index" => 44], "db_column" => "claim_status"],
["excel_column" => ["col_name" => "Final ABHI Status-Current Month", "col_index" => 45], "db_column" => "final_abhi_status_current_month"],
["excel_column" => ["col_name" => "Claimed Amount", "col_index" => 46], "db_column" => "claimed_amount"],
["excel_column" => ["col_name" => "ABHI Amount Less Coins - Current Month", "col_index" => 47], "db_column" => "abhi_amount_less_coins_current_month"],
["excel_column" => ["col_name" => "Repudiation Date", "col_index" => 48], "db_column" => "repudiation_date"],
["excel_column" => ["col_name" => "Settled Date", "col_index" => 49], "db_column" => "settled_date"],
["excel_column" => ["col_name" => "Settled Month", "col_index" => 50], "db_column" => "settled_month"],
["excel_column" => ["col_name" => "Settled Year", "col_index" => 51], "db_column" => "settled_year"],
["excel_column" => ["col_name" => "Settled Quarter", "col_index" => 52], "db_column" => "settled_quarter"],
["excel_column" => ["col_name" => "Rejection Category", "col_index" => 53], "db_column" => "rejection_category"],
["excel_column" => ["col_name" => "Rejection Category - Level 1", "col_index" => 54], "db_column" => "rejection_category_level_1"],
["excel_column" => ["col_name" => "Rejection Category - Level 2", "col_index" => 55], "db_column" => "rejection_category_level_2"],
["excel_column" => ["col_name" => "COVID tagging - Current Month", "col_index" => 56], "db_column" => "covid_tagging_current_month"],
["excel_column" => ["col_name" => "EXPECTED DOA", "col_index" => 57], "db_column" => "expected_doa"],
["excel_column" => ["col_name" => "EXPECTED DOD", "col_index" => 58], "db_column" => "expected_dod"],
["excel_column" => ["col_name" => "AGENT/BROKER CODE", "col_index" => 59], "db_column" => "agent_broker_code"],
["excel_column" => ["col_name" => "AGENT/BROKER NAME", "col_index" => 60], "db_column" => "agent_broker_name"],
["excel_column" => ["col_name" => "HEALTHCARD_ID", "col_index" => 61], "db_column" => "healthcard_id"],
["excel_column" => ["col_name" => "ABHI_NETWORK_NON_NETWORK", "col_index" => 62], "db_column" => "abhi_network_non_network"],
["excel_column" => ["col_name" => "CORPORATE_EMPLOYEE_CODE", "col_index" => 63], "db_column" => "corporate_employee_code"],
];
protected $ticketMasterMapping = [
// Employee / Member details
'member_code' => 'emp_code',
'abhi_claim_no' => 'claim_number',
// Dates
'doa' => 'doa',
'dod' => 'dod',
'intimation_date' => 'date_of_intimat',
// Claim info
'claim_status' => 'tpa_claim_status',
'claimed_amount' => 'claim_amount',
// Hospital details
'hospital_name' => 'hospital_name',
'hospital_city' => 'hospital_city',
'hospital_state' => 'hospital_state',
// Settlement / decision
'repudiation_date' => 'denial_date',
'settled_date' => 'settled_date',
'rejection_category' => 'denial_reason',
// Misc
'diagnosis' => 'claim_description',
'healthcard_id' => 'tpa_no',
'claim_type' => 'tpa_claim_type',
];
protected $statusMapping = [
'Settled' => 11,
'Rejected' => 8,
'Cancelled' => 13,
];
protected $dateColumns = [
'policy_from',
'policy_upto',
'intimation_date',
'date_of_doc_rec',
'doa',
'dod',
'repudiation_date',
'settled_date',
'expected_doa',
'expected_dod',
];
/**
* ABSTRACT FUNCTIONs
*/
public function bulkInsertTPATable(array $data): bool
{
if (empty($data)) {
return false;
}
$builder = $this->db->table('claims_dump_abhi');
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_abhi');
return $builder->updateBatch($data, 'id');
}
public function updateTicketMasterRejectedReasonInTPATable($data): bool
{
if (empty($data)) {
return false;
}
$builder = $this->db->table('claims_dump_abhi');
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, 'm/d/Y h:i:s A', 'Y-m-d');
}
$params = [
'doa' => $item['doa'] ?? null,
'member_code' => $item['member_code'] ?? null,
'claimed_amount' => $item['claimed_amount'] ?? null,
'healthcard_id' => $item['healthcard_id'] ?? null
];
$is_duplicate = $this->checkDuplicateTpaClaim('claims_dump_abhi', $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_abhi', ['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['member_code'] ?? null,
'claim_amount' => $row['claimed_amount'] ?? null,
'tpa_no' => $row['healthcard_id'] ?? 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'] ?? null);
$employee_data = $this->getEmployeeDetails($file_data['client_id'], $file_data['client_policy_id'], $row['member_code'], $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['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'] = isset($row['mainclaim_prepost_type']) && !empty($row['mainclaim_prepost_type']) && strtolower($row['mainclaim_prepost_type']) == 'normal' ? 1 : 3;
$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 isDateValue($value): bool
{
if (empty($value)) {
return false;
}
// Excel numeric date
if (is_numeric($value) && $value > 30000) {
return true;
}
return strtotime(str_replace('/', '-', $value)) !== false;
}
public function normalizeDate($value): ?string
{
try {
if (empty($value)) {
return null;
}
// Excel numeric date
if (is_numeric($value)) {
return date(
'Y-m-d',
\PhpOffice\PhpSpreadsheet\Shared\Date::excelToTimestamp($value)
);
}
$value = trim((string) $value);
// Replace / with - for strtotime compatibility
$value = str_replace('/', '-', $value);
$timestamp = strtotime($value);
if ($timestamp === false) {
return null;
}
return date('Y-m-d', $timestamp);
} catch (\Throwable $e) {
return null;
}
}
public function convertRelation(?string $relation): ?string
{
if (empty($relation)) {
return null;
}
// trim removes whitespace, strtolower handles case sensitivity
$relation = strtolower(trim($relation));
// Mapping specific keywords to standardized outputs
// ORDER MATTERS: Specific phrases (in-law) must come before general ones (father)
$map = [
'father-in-law' => ['father in law', 'father-in-law', 'fil'],
'mother-in-law' => ['mother in law', 'mother-in-law', 'mil'],
'father' => ['father', 'papa', 'dad'],
'mother' => ['mother', 'mom', 'mummy'],
'spouse' => ['spouse', 'wife', 'husband', 'hubby'],
'daughter' => ['daughter', 'daug'],
'son' => ['son'],
'self' => ['self', 'employee', 'main'],
];
foreach ($map as $standard => $keywords) {
foreach ($keywords as $keyword) {
if (str_contains($relation, $keyword)) {
return $standard;
}
}
}
return null;
}
}