["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', // Policy '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', // Payment 'cheque_neft_number' => 'utr_details', 'cheque_neft_date' => 'settled_date', // References 'cl_inward_no' => 'claim_number', 'uhid' => 'tpa_no', ]; /** * Extra dump → claim_report fields beyond ticketMasterMapping. protected $statusMapping = [ 'CL Paid with Settlement Letter' => 11, 'Settled' => 11, 'Paid' => 11, 'Rejected' => 8, 'Approved' => 9, 'Closed' => 12, 'CL Rejected' => 8, 'CL Approved' => 9, 'AL Closed' => 12, ]; 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; } $builder = $this->db->table('claims_dump_reliance'); 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(int $fileId): bool { $rows = $this->db->table('claims_dump_reliance cd') ->select('cd.id, tm.id AS ticket_id') ->join('ticket_master tm', 'tm.claim_dump_ref_id = cd.id AND tm.file_id = cd.file_id', 'inner') ->where('cd.is_active', 1) ->where('cd.file_id', $fileId) ->where('cd.ticket_id IS NULL') ->get() ->getResultArray(); if (empty($rows)) { return true; } $updateData = []; foreach ($rows as $row) { $updateData[] = [ 'id' => $row['id'], 'ticket_id' => $row['ticket_id'], ]; } return $this->db->table('claims_dump_reliance')->updateBatch($updateData, 'id') !== false; } public function updateTicketMasterRejectedReasonInTPATable($data): bool { if (empty($data)) { return false; } $builder = $this->db->table('claims_dump_reliance'); return $builder->updateBatch($data, 'id') !== false; } /** * 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'); } $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 $this->emptyClaimMasterMappingResponse('claims_dump_reliance', (int) $file_id); } $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 = []; $status_update_array = []; 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, 'client_id' => $file_data['client_id'] ?? null, 'client_policy_id' => $file_data['client_policy_id'] ?? null, ]; $newStatusId = $this->resolveClaimStatusId( $this->statusMapping, $row['final_status'] ?? '', (int) $row['id'], $rejecetd_reason ); if ($newStatusId === null) { continue; } $existingTicket = $this->getExistingTicketMasterClaim($params); if ($this->handleExistingTicketStatusUpdate( $existingTicket, $newStatusId, (int) $row['id'], $status_update_array, $rejecetd_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_code'] = $employee_data['emp_code'] ?? null; $item['emp_mail'] = $employee_data['emp_mail'] ?? null; $item['emp_mobile'] = $employee_data['emp_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'] = $newStatusId; $item['file_id'] = $file_id; $item['created_by'] = $file_data['created_by'] ?? null; $item['claim_dump_ref_id'] = $row['id']; $item['claim_dump_date'] = $file_data['claim_dump_date'] ?? null; $item['claim_description'] = $row['diagnosis'] ?? null; $item['tpa_ailments'] = $row['diagnosis'] ?? null; $item['claim_type'] = 1; $item['priority'] = 1; $item['mode_of_intimation'] = 5; $item['ticket_type_id'] = 1; $item['claim_created_by'] = 'DUMP_TPA'; $mapped[] = $item; } return [ 'status' => true, 'mapped_array' => $mapped, 'rejected_reason_array' => $rejecetd_reason, 'status_update_array' => $status_update_array, ]; } 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 } }