nhance/app/Libraries/TPAClaimsImportServices/RcareClaimImportService.php
2026-01-28 09:50:53 +05:30

421 lines
16 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 RcareClaimImportService extends BaseTpaClaimImportService
{
/**
* MAPPING ARRAYs
*/
protected $mapping = [
["excel_column" => ["col_name" => "CLInwardNo", "col_index" => 0], "db_column" => "cl_inward_no"],
["excel_column" => ["col_name" => "Inward Date", "col_index" => 1], "db_column" => "inward_date"],
["excel_column" => ["col_name" => "Claim Classification", "col_index" => 2], "db_column" => "claim_classification"],
["excel_column" => ["col_name" => "Policy Name", "col_index" => 3], "db_column" => "policy_name"],
["excel_column" => ["col_name" => "Policy Number", "col_index" => 4], "db_column" => "policy_number"],
["excel_column" => ["col_name" => "Policy Start Date", "col_index" => 5], "db_column" => "policy_start_date"],
["excel_column" => ["col_name" => "Policy End Date", "col_index" => 6], "db_column" => "policy_end_date"],
["excel_column" => ["col_name" => "UHID", "col_index" => 7], "db_column" => "uhid"],
["excel_column" => ["col_name" => "Insured Name", "col_index" => 8], "db_column" => "insured_name"],
["excel_column" => ["col_name" => "Patient Name", "col_index" => 9], "db_column" => "patient_name"],
["excel_column" => ["col_name" => "Employee/Member Id", "col_index" => 10], "db_column" => "employee_member_id"],
["excel_column" => ["col_name" => "Grade", "col_index" => 11], "db_column" => "grade"],
["excel_column" => ["col_name" => "Relation", "col_index" => 12], "db_column" => "relation"],
["excel_column" => ["col_name" => "Age", "col_index" => 13], "db_column" => "age"],
["excel_column" => ["col_name" => "Gender", "col_index" => 14], "db_column" => "gender"],
["excel_column" => ["col_name" => "DF/DNF idenitifcation", "col_index" => 15], "db_column" => "df_dnf_identification"],
["excel_column" => ["col_name" => "Sum Insured", "col_index" => 16], "db_column" => "sum_insured"],
["excel_column" => ["col_name" => "DOA/OPD Treatment From", "col_index" => 17], "db_column" => "doa_opd_treatment_from"],
["excel_column" => ["col_name" => "DOD/OPD Treatment To", "col_index" => 18], "db_column" => "dod_opd_treatment_to"],
["excel_column" => ["col_name" => "Hospital Name", "col_index" => 19], "db_column" => "hospital_name"],
["excel_column" => ["col_name" => "Hospital District", "col_index" => 20], "db_column" => "hospital_district"],
["excel_column" => ["col_name" => "Hospital State", "col_index" => 21], "db_column" => "hospital_state"],
["excel_column" => ["col_name" => "ICDCode", "col_index" => 22], "db_column" => "icd_code"],
["excel_column" => ["col_name" => "Disease Category", "col_index" => 23], "db_column" => "disease_category"],
["excel_column" => ["col_name" => "Final Status", "col_index" => 24], "db_column" => "final_status"],
["excel_column" => ["col_name" => "Approved Date", "col_index" => 25], "db_column" => "approved_date"],
["excel_column" => ["col_name" => "Claimed Amount", "col_index" => 26], "db_column" => "claimed_amount"],
["excel_column" => ["col_name" => "Disallowed Amount", "col_index" => 27], "db_column" => "disallowed_amount"],
["excel_column" => ["col_name" => "Net Sanct Amt", "col_index" => 28], "db_column" => "net_sanction_amount"],
["excel_column" => ["col_name" => "Reason For Disallowance", "col_index" => 29], "db_column" => "reason_for_disallowance"],
["excel_column" => ["col_name" => "External Query Remarks", "col_index" => 30], "db_column" => "external_query_remarks"],
["excel_column" => ["col_name" => "Rejection Remarks", "col_index" => 31], "db_column" => "rejection_remarks"],
["excel_column" => ["col_name" => "Cheque/NEFT Number", "col_index" => 32], "db_column" => "cheque_neft_number"],
["excel_column" => ["col_name" => "Cheque/NEFT Date", "col_index" => 33], "db_column" => "cheque_neft_date"],
["excel_column" => ["col_name" => "Diagnosis", "col_index" => 34], "db_column" => "diagnosis"],
["excel_column" => ["col_name" => "Treatment Detail", "col_index" => 35], "db_column" => "treatment_detail"],
["excel_column" => ["col_name" => "Member Reimbursement CL Type", "col_index" => 36], "db_column" => "member_reimbursement_cl_type"],
];
protected $ticketMasterMapping = [
// Employee / Member
'employee_member_id' => 'emp_code',
'relation' => 'relationship',
// Policy
'policy_number' => 'policy_no',
'policy_start_date' => 'date_of_incep',
// Claim Dates
'doa_opd_treatment_from' => 'doa',
'dod_opd_treatment_to' => 'dod',
'approved_date' => 'approved_date',
// Claim Amounts
'claimed_amount' => 'claim_amount',
'net_sanction_amount' => 'approved_amount',
// Status / Remarks
'final_status' => 'tpa_claim_status',
'reason_for_disallowance' => 'denial_reason',
'rejection_remarks' => 'return_remark',
// Hospital
'hospital_name' => 'hospital_name',
'hospital_state' => 'hospital_state',
'hospital_district' => 'hospital_city',
'diagnosis' => 'claim_description',
// Payment
'cheque_neft_number' => 'utr_details',
'cheque_neft_date' => 'settled_date',
// References
'cl_inward_no' => 'claim_number',
];
protected $statusMapping = [
'Settled' => 11,
'Rejected' => 8,
'Denied' => 8,
'Cancelled' => 13,
'Processed' => 61,
'Information Awaited' => 4,
'Denied Letter Sent' => 66,
'Cashless Document Awaited' => 3,
];
protected $dateColumns = [
'inward_date',
'policy_start_date',
'policy_end_date',
'doa_opd_treatment_from',
'dod_opd_treatment_to',
'approved_date',
'cheque_neft_date'
];
/**
* ABSTRACT FUNCTIONs
*/
public function bulkInsertTPATable(array $data): bool
{
if (empty($data)) {
return false;
}
try {
$builder = $this->db->table('claims_dump_reliance');
$builder->insertBatch($data);
return true;
} 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,
];
dd($errorData);
return false;
}
}
public function importClaimMaster(array $data): bool
{
if (empty($data)) {
return false;
}
$ticketMasterModel = new TicketMasterModel();
$ticketMasterModel->insertBatch($data);
return true;
}
public function updateTicketIdInTPATable(): bool
{
if (empty($data)) {
return false;
}
$builder = $this->db->table('claims_dump_reliance');
$builder->insertBatch($data);
return true;
}
public function updateTicketMasterRejectedReasonInTPATable($data): bool
{
if (empty($data)) {
return false;
}
$builder = $this->db->table('claims_dump_reliance');
$builder->insertBatch($data);
return true;
}
/**
* 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, 'd-M-y', 'Y-m-d');
}
$params = [
'doa_opd_treatment_from' => $item['doa_opd_treatment_from'] ?? 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_reliance', $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_reliance', ['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_opd_treatment_from'] ?? '') ?? null,
'emp_code' => $row['employee_member_id'] ?? null,
'claim_amount' => $row['claimed_amount'] ?? null,
'tpa_no' => $row['uhid'] ?? null
];
$isduplicate = $this->checkDublicateTicketMasterClaim($params);
if ($isduplicate) {
$reason = "This claim already exists in our system.";
$rejecetd_reason[$row['id']] = ["ticket_master_insert_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['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_mail'] = $employee_data['email_corporate'] ?? null;
$item['emp_mobile'] = $employee_data['mobile'] ?? null;
$item['insured_emp_id'] = $employee_data['insured_emp_id'] ?? null;
$item['insured_name'] = $employee_data['insured_name'] ?? null;
} else {
$reason = sprintf(
'The policy number "%s" does not exist in our system. ' .
'Details - Client ID: %s, Client Policy ID: %s, Employee Number: %s, Relationship: %s',
$row['insurer_policy_number'],
$file_data['client_id'],
$file_data['client_policy_id'],
$row['employee_number'],
$item['relationship']
);
$rejecetd_reason[$row['id']] = ["ticket_master_insert_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['final_status']] ?? 61;
$item['file_id'] = $file_id;
$item['created_by'] = $file_data['created_by'] ?? null;
$item['claim_dump_ref_id'] = $row['id'];
$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
}
}