diff --git a/app/Controllers/ClientController.php b/app/Controllers/ClientController.php index 4bbf2189..d974f31c 100755 --- a/app/Controllers/ClientController.php +++ b/app/Controllers/ClientController.php @@ -6179,7 +6179,12 @@ class ClientController extends AdminController // $response = $ticketServiceController->getClaimExcelErrorData(["file_id" => 41]); // $response = $ticketServiceController->claimDumpOnBoardProcess(["file_id" => 17]); // $response = $ticketServiceController->extractExcelData("claims_dump_form_client.xlsx"); - // $response = $ticketServiceController->tpaClaimDumpImporter(["file_id" => 48]); + // $response = $ticketServiceController->tpaClaimDumpImporter(["file_id" => 52]); //reliance + // $response = $ticketServiceController->tpaClaimDumpImporter(["file_id" => 50]); //fhpl + // $response = $ticketServiceController->tpaClaimDumpImporter(["file_id" => 51]); //abhi + // $response = $ticketServiceController->tpaClaimDumpImporter(["file_id" => 48]); //vidal + // $response = $ticketServiceController->tpaClaimDumpImporter(["file_id" => 53]); //icici + // $response = $ticketServiceController->tpaClaimDumpToTicketMasterImporters(["file_id" => 48]); // $response = $ticketServiceController->tpaClaimDumpToTicketMasterImporters(["file_id" => 48]); // dd($response); diff --git a/app/Controllers/JobWorker.php b/app/Controllers/JobWorker.php index 9cc001dd..160c6fe9 100755 --- a/app/Controllers/JobWorker.php +++ b/app/Controllers/JobWorker.php @@ -155,6 +155,14 @@ class JobWorker extends AdminController 'type' => 'CC', // Handler Category 'handler' => 'App\Controllers\TicketServiceController', ], + 'tpaClaimDumpImporter' => [ + 'type' => 'CC', // Handler Category + 'handler' => 'App\Controllers\TicketServiceController', + ], + 'tpaClaimDumpToTicketMasterImporters' => [ + 'type' => 'CC', // Handler Category + 'handler' => 'App\Controllers\TicketServiceController', + ], 'excelMultieventFileFormateValidation' => [ 'type' => 'CC', // Handler Category 'handler' => 'App\Controllers\EmployeeMultiEventServiceController', diff --git a/app/Controllers/PolicyTransactionController.php b/app/Controllers/PolicyTransactionController.php index f0d6fc56..81eede97 100644 --- a/app/Controllers/PolicyTransactionController.php +++ b/app/Controllers/PolicyTransactionController.php @@ -1094,6 +1094,7 @@ private function updateInceptionPolicy($id, $data) { // print_r($data); die; + $data['updated_by'] = get_session_userid(); $old_pt_data = $this->policyTransactionModel->where('is_active', 1)->where("id", $id)->first(); $old_pt_co_share_data = $this->PTCOShareDetailsModel->where('is_active', 1)->where("id", $id)->first(); if ($this->policyTransactionModel->update($id, $data)) { @@ -2007,6 +2008,7 @@ { if ($id) { $data['is_active'] = 0; + $data['updated_by'] = get_session_userid(); $policy_transaction_data = $this->policyTransactionModel->where('id', $id)->first(); if($policy_transaction_data['policy_type_id'] > 7 && $type == 0){ @@ -2418,6 +2420,7 @@ private function updateEndorsementTransaction($id, $data) { + $data['updated_by'] = get_session_userid(); $old_endorse_data = $this->policyTransactionModel->where('id', $id)->where('is_active', 1)->first(); $update = $this->policyTransactionModel->where('id', $id)->set($data)->update(); diff --git a/app/Controllers/TicketController.php b/app/Controllers/TicketController.php index 09021a99..05ceb883 100644 --- a/app/Controllers/TicketController.php +++ b/app/Controllers/TicketController.php @@ -2813,6 +2813,8 @@ class TicketController extends BaseController { $data['tab_name'] = "Claim Dupm Upload"; $data['page_name'] = "Claims"; + $data['tpa_list'] = $this->TPAModel->where('is_active', 1)->findAll(); + if($this->request->is('get')){ @@ -2877,17 +2879,25 @@ class TicketController extends BaseController $status = 'inprogress'; $insert_data = [ 'file_name' => $filename, - 'status' => $status + 'status' => $status, + 'client_id' => !empty($this->request->getPost('client_id')) ? $this->request->getPost('client_id') : null, + 'client_policy_id' => !empty($this->request->getPost('client_policy_id')) ? $this->request->getPost('client_policy_id') : null, + 'tpa_id' => !empty($this->request->getPost('tpa_id')) ? $this->request->getPost('tpa_id') : null, ]; $file_id = $this->claimDumpFileModel->insert($insert_data); $this->myLogger->logme("error", 'claim_dumb_file_id : {file_id}, uploaded success', ['file_id' => $file_id]); //after file upload success than call the file formate validation in service controller - $ticketServiceController = new TicketServiceController(); - // $response = $ticketServiceController->claimDumpExcelFileFormatValidation(["file_id" => $file_id]); - $r = Jobs::addJob(['job_name' => 'claimDumpExcelFileFormatValidation', 'payload' => ['file_id' => $file_id]]); + // $ticketServiceController = new TicketServiceController(); + if(!empty($insert_data['tpa_id'])){ + $r = Jobs::addJob(['job_name' => 'tpaClaimDumpImporter', 'payload' => ['file_id' => $file_id]]); + // $response = $ticketServiceController->claimDumpExcelFileFormatValidation(["file_id" => $file_id]); + }else{ + $r = Jobs::addJob(['job_name' => 'claimDumpExcelFileFormatValidation', 'payload' => ['file_id' => $file_id]]); + // $response = $ticketServiceController->claimDumpExcelFileFormatValidation(["file_id" => $file_id]); + } return $this->respond(['status' => true, 'code' => 200, 'message' => 'File uploaded successfully. File being validated'], 200); } @@ -2986,8 +2996,12 @@ class TicketController extends BaseController } + + // -------- CLAIM MIS UPLOAD ---------------------------------------------------------------------------------------------- + public function claimMisFileList() - { + { + $data['tab_name'] = "Claim MIS Upload"; $data['page_name'] = "Cliam MIS Files"; $data['claim_mis_file_list'] = $this->claimmisFileModel ->select('claims_mis_files.*, user_profiles.first_name as user_name') diff --git a/app/Controllers/TicketServiceController.php b/app/Controllers/TicketServiceController.php index 5cfa99f4..14c1b19a 100644 --- a/app/Controllers/TicketServiceController.php +++ b/app/Controllers/TicketServiceController.php @@ -7,6 +7,9 @@ use CodeIgniter\HTTP\ResponseInterface; use CodeIgniter\API\ResponseTrait; use PhpOffice\PhpSpreadsheet\Cell\Coordinate; use Kint\Kint; +use App\Libraries\TpaClaimsImportFactory; +use App\Libraries\TPAClaimsImportServices\FhplClaimImportService; +use App\Libraries\TPAClaimsImportServices\BaseTpaClaimImportService; use App\Models\ClaimDumpFileModel; use App\Models\EmployeeModel; @@ -15,10 +18,14 @@ use App\Models\TPAModel; use App\Models\ClientModel; use App\Models\TicketClaimStatusModel; use App\Models\ClientPolicyModel; +use App\Models\ClaimsDumpFhplModel; use App\Helpers\ExcelSanitizeHelper; use App\Models\TicketMasterModel; +use PhpOffice\PhpSpreadsheet\IOFactory; +use PhpOffice\PhpSpreadsheet\Spreadsheet; + class TicketServiceController extends BaseController { use ResponseTrait; @@ -35,6 +42,10 @@ class TicketServiceController extends BaseController protected $ticketClaimStatusModel; protected $clientPolicyModel; + protected $medi_assist_primary_key; + protected $vidal_primary_key; + protected $icici_primary_key; + public function __construct() { @@ -50,6 +61,11 @@ class TicketServiceController extends BaseController $this->ticketClaimStatusModel = new TicketClaimStatusModel(); $this->clientPolicyModel = new ClientPolicyModel(); + $this->medi_assist_primary_key = getenv('MEDI_ASSIST_PRIMARY_KEY_CONSTANT'); + $this->vidal_primary_key = getenv('VIDAL_PRIMARY_KEY_CONSTANT'); + $this->icici_primary_key = getenv('ICICI_PRIMARY_KEY_CONSTANT'); + + $this->claim_dump_excel_columns = [ // Mandatory Fields @@ -1819,5 +1835,243 @@ class TicketServiceController extends BaseController { } + + + // -------------------------------------------------------------------------------------------------------------------------------- + + + public function tpaClaimImporter($params) + { + $file_id = $params['file_id']; + $tpa_claims_files_data = $this->claimDumpFileModel->where('id', $file_id)->first(); + + + if($this->vidal_primary_key == $tpa_claims_files_data['tpa_id']){ + + }else if($this->icici_primary_key == $tpa_claims_files_data['tpa_id']){ + + }else{ + $this->myLogger->logme('error', 'No TPA found to import'); + return ['status' => false, 'message' => 'No TPA found to import']; + } + } + + public function tpaClaimDumpImporter($params) + { + try { + + $file_id = $params['file_id'] ?? null; + $file_path = WRITEPATH . 'uploads/claim_dump_excel/'; + + if (!$file_id) { + return ['status' => false, 'message' => 'File ID is missing']; + } + + $fileData = $this->claimDumpFileModel->where('id', $file_id)->first(); + + if (!$fileData) { + return ['status' => false, 'message' => 'Invalid file ID. No file data found']; + } + + $file_full_path = $file_path . $fileData['file_name']; + + if (!is_file($file_full_path)) { + return ['status' => false, 'message' => 'Claim dump file not found']; + } + + $handler = TpaClaimsImportFactory::make($fileData['tpa_id'] ?? 0); + $result = $handler->runTpaClaimDumpInsert($file_full_path, $file_id); + + // ---------- Prepare update data ---------- + $data = []; + + if (!empty($result['status']) && $result['status'] === true) { + $data['status'] = 'success'; + $data['reason'] = null; + } else { + $data['status'] = 'failed'; + + $errorMessage = $result['message'] ?? 'Unknown import error'; + + $reason = [ + 'error_summary' => array_count_values([5]), + 'error_data' => $errorMessage + ]; + + $data['reason'] = json_encode($reason, JSON_UNESCAPED_UNICODE); + } + + // dd($data); + // ---------- Update DB ---------- + $sql = "UPDATE claim_dump_files SET status = ?, reason = ? WHERE id = ?"; + + $updated = db_connect()->query( + $sql, + [ + $data['status'] ?? null, + $data['reason'] ?? null, + $file_id + ] + ); + + if (!$updated) { + $this->myLogger->logme('error', 'Claim dump file update failed for file_id: ' . $file_id); + } + + dd(db_connect()->getLastQuery()->getQuery()); + + if(!empty($result['status']) && $result['status'] === true){ + $r = Jobs::addJob(['job_name' => 'tpaClaimDumpToTicketMasterImporters', 'payload' => ['file_id' => $file_id]]); + } + + return $result; + + } catch (\Throwable $th) { + + $this->myLogger->logme( + "error", + 'TPA_CLAIM_IMPORTER_JOB : ' . + $th->getMessage() . ' | Line: ' . $th->getLine() + ); + + return [ + 'status' => false, + 'message' => 'TPA Claim dump import failed', + 'error_data' => [ + 'message' => $th->getMessage(), + 'file' => $th->getFile(), + 'line' => $th->getLine(), + 'trace' => $th->getTraceAsString() + ] + ]; + } + } + + public function tpaClaimDumpToTicketMasterImporters($params) + { + try { + + $file_id = $params['file_id'] ?? null; + $fileData = $this->claimDumpFileModel->where('id', $file_id)->first(); + + if (!$fileData) { + return ['status' => false, 'message' => 'Invalid file ID No file data found to import']; + } + + $handler = TpaClaimsImportFactory::make($fileData['tpa_id'] ?? 0); + $result = $handler->runTicketMasterInsert($params); + + return $result; + + } catch (\Throwable $th) { + + $this->myLogger->logme("error", 'TICKET_MASTER_CLAIM_IMPORTER_JOB :' .($th->getMessage() . ' --- ' . $th->getLine() . '----' . $th->getTraceAsString())); + + $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' => $th->getMessage(), + 'error_data' => json_encode($errorData, JSON_PRETTY_PRINT) + ]; + } + } + + public function tpaClaimDumpToTicketMasterImportBatchSeperater($params) + { + try { + + $file_id = $params['file_id'] ?? null; + $fileData = $this->claimDumpFileModel->where('id', $file_id)->first(); + + if (!$fileData) { + return ['status' => false, 'message' => 'Invalid file ID No file data found to import']; + } + + $handler = TpaClaimsImportFactory::make($fileData['tpa_id'] ?? 0); + $result = $handler->runTicketMasterInsert($params); + + return $result; + + } catch (\Throwable $th) { + + $this->myLogger->logme("error", 'TICKET_MASTER_CLAIM_IMPORTER_JOB :' .($th->getMessage() . ' --- ' . $th->getLine() . '----' . $th->getTraceAsString())); + + $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' => $th->getMessage(), + 'error_data' => json_encode($errorData, JSON_PRETTY_PRINT) + ]; + } + } + + public function readExcelBySheetName(string $filePath, string $sheetName): array + { + if (!file_exists($filePath)) { + return []; + } + + $spreadsheet = IOFactory::load($filePath); + + // Get sheet by name + $sheet = $spreadsheet->getSheetByName($sheetName); + + if ($sheet === null) { + return []; + } + + $rows = $sheet->toArray(null, true, true, true); + + // Need at least header + one row + if (count($rows) < 2) { + return []; + } + + // First row = headers + $headers = array_shift($rows); + $headers = array_map('trim', $headers); + + $data = []; + + foreach ($rows as $row) { + // Skip completely empty rows + if (!array_filter($row)) { + continue; + } + + $item = []; + + foreach ($headers as $key => $headerName) { + if ($headerName !== '') { + $item[$headerName] = $row[$key] ?? null; + } + } + + $data[] = $item; + } + + return $data; + } + } diff --git a/app/Helpers/utility_helper.php b/app/Helpers/utility_helper.php index 15d873eb..5295224b 100755 --- a/app/Helpers/utility_helper.php +++ b/app/Helpers/utility_helper.php @@ -649,7 +649,7 @@ if (!function_exists('change_date_format')) { $date = DateTime::createFromFormat($source_format, $date_str); if (!$date) { // throw new Exception("Invalid date string for source format: $source_format"); - log_message('error', "❌ Date format error : Invalid date string | Params => date_str: {$date_str}, source_format: {$source_format}, output_format: {$output_format}"); + // log_message('error', "❌ Date format error : Invalid date string | Params => date_str: {$date_str}, source_format: {$source_format}, output_format: {$output_format}"); return null; } return $date->format($output_format); @@ -660,7 +660,7 @@ if (!function_exists('change_date_format')) { $date = DateTime::createFromFormat($source_format, $date_str); if (!$date) { // throw new Exception("Invalid date string for source format: $source_format"); - log_message('error', "❌ Date format error : Invalid date string | Params => date_str: {$date_str}, source_format: {$source_format}, output_format: {$output_format}"); + // log_message('error', "❌ Date format error : Invalid date string | Params => date_str: {$date_str}, source_format: {$source_format}, output_format: {$output_format}"); return null; } return $date->format('Y-m-d'); // MySQL default format @@ -677,15 +677,15 @@ if (!function_exists('change_date_format')) { // If no format matches, throw an exception $allowed_placeholders = implode(', ', $allowed_formats); // throw new Exception("Invalid date string format. Allowed formats: $allowed_placeholders"); - log_message( - 'error', - "❌ Date format error: Invalid date string. Allowed formats: {$allowed_placeholders} | Params => date_str: {$date_str}, source_format: {$source_format}, output_format: {$output_format}" - ); + // log_message( + // 'error', + // "❌ Date format error: Invalid date string. Allowed formats: {$allowed_placeholders} | Params => date_str: {$date_str}, source_format: {$source_format}, output_format: {$output_format}" + // ); return null; } } catch (Exception $e) { // return "Error: " . $e->getMessage(); - log_message('error', "❌ Date format error: {$e->getMessage()} | Params => date_str: {$date_str}, source_format: {$source_format}, output_format: {$output_format}"); + // log_message('error', "❌ Date format error: {$e->getMessage()} | Params => date_str: {$date_str}, source_format: {$source_format}, output_format: {$output_format}"); return null; } diff --git a/app/Libraries/TPAClaimsImportServices/AbhiClaimImportService.php b/app/Libraries/TPAClaimsImportServices/AbhiClaimImportService.php new file mode 100644 index 00000000..1f62ea7f --- /dev/null +++ b/app/Libraries/TPAClaimsImportServices/AbhiClaimImportService.php @@ -0,0 +1,466 @@ + ["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', + 'relation' => 'relationship', + + // Policy / Claim identifiers + 'policy_number' => 'policy_no', + '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', + + 'priority' => 1, + 'mode_of_intimation' => 5, + 'ticket_type_id' => 1, + ]; + + + protected $statusMapping = [ + 'Settled' => 11, + 'Rejected' => 8, + 'Cancelled' => 13, + ]; + + + /** + * ABSTRACT FUNCTIONs + */ + public function bulkInsertTPATable(array $data): bool + { + if (empty($data)) { + return false; + } + + $builder = $this->db->table('claims_dump_abhi'); + + foreach ($data as $value) { + + if (!$builder->insert($value)) { + + // 🔍 Debug purpose + log_message('error', print_r($this->db->error(), true)); + log_message('error', $this->db->getLastQuery()); + + return false; + } + } + + return true; + } + + + 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_abhi'); + $builder->insertBatch($data); + + return true; + } + + + public function updateTicketMasterRejectedReasonInTPATable($data): bool + { + if (empty($data)) { + return false; + } + + $builder = $this->db->table('claims_dump_abhi'); + $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; + + if ($this->isDateValue($value)) { + $value = $this->normalizeDate($value); + } + + $item[$dbColumn] = trim($value); + } + + $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[$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['member_code'], $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['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; + + $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; + } + + $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 + } + +} diff --git a/app/Libraries/TPAClaimsImportServices/BaseTpaClaimImportService.php b/app/Libraries/TPAClaimsImportServices/BaseTpaClaimImportService.php new file mode 100644 index 00000000..33fbedc9 --- /dev/null +++ b/app/Libraries/TPAClaimsImportServices/BaseTpaClaimImportService.php @@ -0,0 +1,368 @@ +db = db_connect(); + $this->claimDumpFileModel = new ClaimDumpFileModel(); + } + + /** + * First JOB for insert TPA wise Bulk Upload + */ + public function runTpaClaimDumpInsert(string $filePath, int $fileId): array + { + $this->db->transStart(); + + $fileData = $this->claimDumpFileModel->where('id', $fileId)->first(); + + if (env('FHPL_PRIMARY_KEY_CONSTANT') == $fileData['tpa_id']) { + $rows = $this->readExcelBySheetName($filePath, 'Claims&Preauth'); + } else if (env('R_CARE_PRIMARY_KEY_CONSTANT') == $fileData['tpa_id']) { + $rows = $this->readExcelBySheetName($filePath, 'CL'); + // $AL = $this->readExcelBySheetName($filePath, 'AL'); + // $rows = array_merge($CL, $AL); + } else { + $rows = $this->readExcel($filePath); + } + // dd($rows); + + if (empty($rows)) { + return ['status' => false, 'message' => 'Excel file contains no data or wrong file upload']; + } + + $tpaInsertData = $this->mapTPAData($rows, $fileId); + dd($tpaInsertData); + + if (empty($tpaInsertData)) { + return ['status' => false, 'message' => 'These records already exist in the system.']; + } + + if (env('FHPL_PRIMARY_KEY_CONSTANT') == $fileData['tpa_id']) { + $ClaimsDumpFhplModel = $this->db->table('claims_dump_fhpl'); + $return_res = $ClaimsDumpFhplModel->insertBatch($tpaInsertData); + } else if (env('R_CARE_PRIMARY_KEY_CONSTANT') == $fileData['tpa_id']) { + $ClaimsDumpIciciModel = $this->db->table('claims_dump_reliance'); + $return_res = $ClaimsDumpIciciModel->insertBatch($tpaInsertData); + } else if (env('ICICI_PRIMARY_KEY_CONSTANT') == $fileData['tpa_id']) { + $ClaimsDumpIciciModel = $this->db->table('claims_dump_icici'); + $return_res = $ClaimsDumpIciciModel->insertBatch($tpaInsertData); + } else if (env('ABHI_PRIMARY_KEY_CONSTANT') == $fileData['tpa_id']) { + // $ClaimsDumpAbhiModel = $this->db->table('claims_dump_abhi'); + // $return_res = $ClaimsDumpAbhiModel->insertBatch($tpaInsertData); + $return_res = $this->bulkInsertTPATable($tpaInsertData); + } else if (env('VIDAL_PRIMARY_KEY_CONSTANT') == $fileData['tpa_id']) { + $ClaimsDumpVidalModel = $this->db->table('claims_dump_vidal'); + $return_res = $ClaimsDumpVidalModel->insertBatch($tpaInsertData); + } else if (env('MEDI_ASSIST_PRIMARY_KEY_CONSTANT') == $fileData['tpa_id']) { + $ClaimsDumpMediAssistModel = $this->db->table('claims_dump_medi_assist'); + $return_res = $ClaimsDumpMediAssistModel->insertBatch($tpaInsertData); + } else{ + + } + + // $return_res = $this->bulkInsertTPATable($tpaInsertData); + + if (!$return_res) { + return ['status' => false, 'message' => 'TPA Import bulk insert failed']; + } + + $this->db->transComplete(); + + if ($this->db->transStatus() === false) { + + $error = $this->db->error(); + + $error_data = [ + 'message' => $error['message'] ?: 'Unknown DB error', + 'code' => $error['code'] ?? null, + 'last_query' => (string) $this->db->getLastQuery() + ]; + + // dd($error_data); + // unset($error_data['last_query']); + return ['status' => false, 'message' => 'TPA Import transaction failed', 'error_data' => $error_data]; + } + + return ['status' => true, 'message' => 'File uploaded successfully', 'record_count' => count($tpaInsertData ?? [])]; + } + + /** + * Second JOB for insert Ticket Master table after insert the TPA bulk upload success + */ + public function runTicketMasterInsert(array $params): array + { + $this->db->transStart(); + + $file_id = $params['file_id']; + + $ticketMasterData = $this->mapClaimMasterData($file_id); + + if (!$ticketMasterData['status']) { + return $ticketMasterData; + } + + $return_res = $this->importClaimMaster($ticketMasterData['mapped_array']); + + if(!$return_res){ + return ['status' => false, 'message' => 'Ticket Master Claim bulk insert failed']; + } + + $this->updateTicketMasterRejectedReasonInTPATable($ticketMasterData['rejected_reason_array']); + + $this->db->transComplete(); + + if ($this->db->transStatus() === false) { + return ['status' => false, 'message' => 'Ticket Master Claim bulk insert failed']; + } + + + + return ['status' => true, 'message' => 'Ticket Master Claim inserted successfully']; + + } + + /** + * Read Excel and return associative rows (header based) + */ + protected function readExcel(string $filePath): array + { + helper('excel_util_helper'); + + if (!file_exists($filePath)) { + throw new RuntimeException("File not found: {$filePath}"); + } + + $spreadsheet = IOFactory::load($filePath); + $sheet = $spreadsheet->getActiveSheet(); + + $rows = $sheet->toArray(null, true, true, true); + + // dd($rows); + + if (count($rows) < 2) { + return []; + } + + // First row is header + $headers = array_shift($rows); + $headers = array_map('trim', $headers); + + $data = []; + + foreach ($rows as $row) { + + if(check_row_is_empty_or_null($row)){ + break; + } + + $item = []; + foreach ($headers as $key => $headerName) { + if ($headerName !== '') { + $item[$headerName] = $row[$key] ?? null; + } + } + $data[] = $item; + } + + return $data; + } + + /** + * Read Excel By Sheet name and return associative rows (header based) + */ + + protected function readExcelBySheetName(string $filePath, string $sheetName): array + { + if (!file_exists($filePath)) { + throw new RuntimeException("File not found: {$filePath}"); + } + + $spreadsheet = IOFactory::load($filePath); + + // Get sheet by name + $sheet = $spreadsheet->getSheetByName($sheetName); + + if ($sheet === null) { + throw new RuntimeException("Sheet '{$sheetName}' not found in Excel file"); + } + + $rows = $sheet->toArray(null, true, true, true); + + // Need at least header + one row + if (count($rows) < 2) { + return []; + } + + // First row = headers + $headers = array_shift($rows); + $headers = array_map('trim', $headers); + + $data = []; + + foreach ($rows as $row) { + // Skip completely empty rows + if (!array_filter($row)) { + continue; + } + + $item = []; + + foreach ($headers as $key => $headerName) { + if ($headerName !== '') { + $item[$headerName] = $row[$key] ?? null; + } + } + + $data[] = $item; + } + + return $data; + } + + + /** + * Dublicate check in the ticket_master table records + */ + protected function checkDublicateTicketMasterClaim(array $param): bool + { + $ticketMaster = new TicketMasterModel(); + $ticket_master_data = $ticketMaster + ->where('doa', $param['doa']) + ->where('tpa_no', $param['tpa_no']) + ->where('claim_amount', $param['claim_amount']) + ->where('emp_code', $param['emp_code']) + ->where('is_active', 1) + ->findAll(); + + if(count($ticket_master_data) > 0){ + return true; + } + + return false; + } + + /** + * Dublicate check in the TPA specific table records + */ + protected function checkDuplicateTpaClaim(string $table, array $params): bool + { + return $this->db->table($table) + ->where($params) + ->where('is_active', 1) + ->countAllResults() > 0; + } + + /** + * Dublicate check in the TPA specific table records + */ + protected function getTpaClaimDumpData(string $table, array $params): array + { + $tpaClaimDumpDataCount = $this->db + ->table($table) + ->where('is_active', 1) + ->where('file_id', $params['file_id']) + ->where('ticket_id IS NULL') + ->countAllResults(); + + $batch_size = 100; + $total_batch = (int) ceil($tpaClaimDumpDataCount / $batch_size); + $batch_no = isset($params['batch_no']) ? (int) $params['batch_no'] : null; + $last_emp_id = (int) ($params['last_emp_id'] ?? 0); + + + return $this->db + ->table($table) + ->where('is_active', 1) + ->where('file_id', $params['file_id']) + ->where('ticket_id IS NULL') + ->get() + ->getResultArray(); + + } + + /** + * Dublicate check in the TPA specific table records + */ + public function getEmployeeDetails(int $client_id, int $client_policy_id, string $emp_code, string $relation): array + { + $EmployeeModel = new EmployeeModel(); + $employeeData = $EmployeeModel + ->select([ + 'employees.id AS emp_id', + 'employees.email_corporate AS emp_mail', + 'employees.mobile AS emp_mobile', + 'employees.name AS emp_name', + + // insured employee + 'insured.id AS insured_emp_id', + 'insured.name AS insured_name' + ]) + ->join( + 'employee_polices', + 'employees.id = employee_polices.employee_id' + ) + ->join( + 'employees AS insured', + "insured.emp_code = employees.emp_code + AND insured.client_id = employees.client_id + AND LOWER(insured.relationship) = " . $EmployeeModel->db->escape(strtolower($relation)), + 'left' + ) + ->where('employees.is_active', 1) + ->where('employee_polices.is_active', 1) + ->where('employee_polices.client_policy_id', $client_policy_id) + ->where('employees.client_id', $client_id) + ->where('employees.emp_code', $emp_code) + ->where('LOWER(employees.relationship)', 'self') + ->first(); + + + return $employeeData ?? []; + } + + /** + * Map Excel rows to TPA table structure + */ + abstract protected function mapTPAData(array $rows, $fileId): array; + + /** + * Map DB rows to Ticket Master table structure + */ + abstract protected function mapClaimMasterData($fileId): array; + + /** + * Insert into TPA-specific table (bulk) + */ + abstract protected function bulkInsertTPATable(array $data): bool; + + /** + * Insert into Ticket-Master-specific table (bulk) + */ + abstract protected function importClaimMaster(array $data): bool; + + /** + * Update TPA table with ticket_master primary key + */ + abstract protected function updateTicketIdInTPATable(): bool; + + /** + * Update TPA table with ticket_master insert rejected reason + */ + abstract protected function updateTicketMasterRejectedReasonInTPATable(array $data): bool; +} diff --git a/app/Libraries/TPAClaimsImportServices/FhplClaimImportService.php b/app/Libraries/TPAClaimsImportServices/FhplClaimImportService.php new file mode 100644 index 00000000..ced6c2f3 --- /dev/null +++ b/app/Libraries/TPAClaimsImportServices/FhplClaimImportService.php @@ -0,0 +1,503 @@ +["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 = [ + + // Employee / Member details + 'member_code' => 'emp_code', + 'relation' => 'relationship', + + // Policy / Claim identifiers + 'policy_number' => 'policy_no', + 'certificate_no' => 'claim_number', + 'abhi_claim_no' => 'tpa_claim_id', + + // Dates + 'doa' => 'doa', + 'dod' => 'dod', + 'intimation_date' => 'date_of_intimat', + + // Claim info + 'claim_type' => 'claim_type', + 'claim_status' => 'tpa_claim_status', + 'claimed_amount' => 'claim_amount', + 'abhi_amount_less_coins_current_month' => 'approved_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', + + 'priority' => 1, + 'mode_of_intimation' => 5, + 'ticket_type_id' => 1, + ]; + + protected $statusMapping = [ + 'Settled' => 11, + 'Rejected' => 8, + ]; + + /** + * ABSTRACT FUNCTIONs + */ + public function bulkInsertTPATable(array $data): bool + { + if (empty($data)) { + return false; + } + + $ClaimsDumpFhplModel = new ClaimsDumpFhplModel(); + $result = $ClaimsDumpFhplModel->insertBatch($data); + + return true; + + } + + + 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_fhpl'); + $builder->insertBatch($data); + + return true; + } + + + public function updateTicketMasterRejectedReasonInTPATable($data): bool + { + if (empty($data)) { + return false; + } + + $builder = $this->db->table('claims_dump_fhpl'); + $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; + + if ($this->isDateValue($value)) { + $value = $this->normalizeDate($value); + } + + $item[$dbColumn] = trim($value); + } + + // $params = [ + // 'admission_date' => change_date_format($item['admission_date'] ?? '') ?? null, + // 'employee_number' => $item['employee_number'] ?? null, + // 'claim_amount' => $item['claim_amount'] ?? null, + // 'primary_policy_holder_card_id' => $item['primary_policy_holder_card_id'] ?? 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['date_of_admission'] ?? '') ?? null, + 'emp_code' => $row['employee_number'] ?? null, + 'claim_amount' => $row['claim_amount'] ?? null, + 'tpa_no' => $row['primary_policy_holder_card_id'] ?? 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, $row['gender'] ?? null); + + $employee_data = $this->getEmployeeDetails($file_data['client_id'], $file_data['client_policy_id'], $row['employee_number'], $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['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'] = isset($row['mainclaim_prepost_type']) && !empty($row['mainclaim_prepost_type']) && strtolower($row['mainclaim_prepost_type']) == 'normal' ? 1 : 3; + + $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 (e.g. 44927) + if (is_numeric($value) && $value > 30000) { + return true; + } + + // Common date formats + return preg_match( + '/^\d{1,2}[\/\-]\d{1,2}[\/\-]\d{2,4}$|^\d{4}[\/\-]\d{1,2}[\/\-]\d{1,2}$/', + (string) $value + ) === 1; + } + + + public function normalizeDate($value): ?string + { + try { + // Excel numeric date + if (is_numeric($value)) { + return date('Y-m-d', \PhpOffice\PhpSpreadsheet\Shared\Date::excelToTimestamp($value)); + } + + // String date + return date('Y-m-d', strtotime(str_replace('/', '-', $value))); + } catch (\Throwable $e) { + return null; + } + } + + + public function convertRelation(string $relation, string $gender): ?string + { + if (empty($relation)) { + return null; + } + + $relation = strtolower($relation); + $gender = strtolower($gender); + + if ($relation == 'self') { + return $relation; + } + + if ($relation == 'spouse') { + return $relation; + } + + if ($relation == 'child' && $gender == 'male') { + return 'son'; + } + + if ($relation == 'child' && $gender == 'female') { + return 'daughter'; + } + + if ($relation == 'parents' && $gender == 'male') { + return 'father'; + } + + if ($relation == 'parents' && $gender == 'female') { + return 'mother'; + } + + if ($relation == 'parents-in-law' && $gender == 'male') { + return 'father-in-law'; + } + + if ($relation == 'parents-in-law' && $gender == 'female') { + return 'mother-in-law'; + } + + return null; + } + + + +} diff --git a/app/Libraries/TPAClaimsImportServices/IciciClaimImportService.php b/app/Libraries/TPAClaimsImportServices/IciciClaimImportService.php new file mode 100644 index 00000000..9f1ddfe3 --- /dev/null +++ b/app/Libraries/TPAClaimsImportServices/IciciClaimImportService.php @@ -0,0 +1,445 @@ + ["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', + 'relation_group' => 'relationship', + + // 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', + 'tpa_no' => 'uhid', + 'rejected_query_desc' => 'claim_description', + ]; + + protected $statusMapping = [ + 'PAID' => 11, + 'REJECTED' => 8, + ]; + + + /** + * ABSTRACT FUNCTIONs + */ + public function bulkInsertTPATable(array $data): bool + { + if (empty($data)) { + return false; + } + + try { + + + $ClaimsDumpFhplModel = new ClaimsDumpFhplModel(); + $result = $ClaimsDumpFhplModel->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, + ]; + 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_icici'); + $builder->insertBatch($data); + + return true; + } + + + public function updateTicketMasterRejectedReasonInTPATable($data): bool + { + if (empty($data)) { + return false; + } + + $builder = $this->db->table('claims_dump_icici'); + $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; + + if ($this->isDateValue($value)) { + $value = $this->normalizeDate($value); + } + + $item[$dbColumn] = $value; + } + + $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[$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'] = strtolower($row['relation_group'] ?? ''); + + $employee_data = $this->getEmployeeDetails($file_data['client_id'], $file_data['client_policy_id'], $row['employee_number'], $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['updated_status']] ?? 61; + $item['file_id'] = $file_id; + $item['created_by'] = $file_data['created_by'] ?? null; + $item['claim_type'] = 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 (e.g. 44927) + if (is_numeric($value) && $value > 30000) { + return true; + } + + // Common date formats + return preg_match( + '/^\d{1,2}[\/\-]\d{1,2}[\/\-]\d{2,4}$|^\d{4}[\/\-]\d{1,2}[\/\-]\d{1,2}$/', + (string) $value + ) === 1; + } + + + public function normalizeDate($value): ?string + { + try { + // Excel numeric date + if (is_numeric($value)) { + return date('Y-m-d', \PhpOffice\PhpSpreadsheet\Shared\Date::excelToTimestamp($value)); + } + + // String date + return date('Y-m-d', strtotime(str_replace('/', '-', $value))); + } catch (\Throwable $e) { + return null; + } + } + + + public function convertRelation(string $relation, string $gender): ?string + { + if (empty($relation)) { + return null; + } + + $relation = strtolower($relation); + $gender = strtolower($gender); + + if ($relation == 'self') { + return $relation; + } + + if ($relation == 'spouse') { + return $relation; + } + + if ($relation == 'child' && $gender == 'male') { + return 'son'; + } + + if ($relation == 'child' && $gender == 'female') { + return 'daughter'; + } + + if ($relation == 'parents' && $gender == 'male') { + return 'father'; + } + + if ($relation == 'parents' && $gender == 'female') { + return 'mother'; + } + + if ($relation == 'parents-in-law' && $gender == 'male') { + return 'father-in-law'; + } + + if ($relation == 'parents-in-law' && $gender == 'female') { + return 'mother-in-law'; + } + + return null; + } + + + +} diff --git a/app/Libraries/TPAClaimsImportServices/MediAssistClaimImportService.php b/app/Libraries/TPAClaimsImportServices/MediAssistClaimImportService.php new file mode 100644 index 00000000..c8e39b21 --- /dev/null +++ b/app/Libraries/TPAClaimsImportServices/MediAssistClaimImportService.php @@ -0,0 +1,491 @@ + ["col_name" => "insurance_company", "col_index" => 0], "db_column" => "insurance_company"], + ["excel_column" => ["col_name" => "insurer_region_name", "col_index" => 1], "db_column" => "insurer_region_name"], + ["excel_column" => ["col_name" => "insurer_ro_code", "col_index" => 2], "db_column" => "insurer_ro_code"], + ["excel_column" => ["col_name" => "insurer_do_code", "col_index" => 3], "db_column" => "insurer_do_code"], + ["excel_column" => ["col_name" => "insurer_bo_code", "col_index" => 4], "db_column" => "insurer_bo_code"], + ["excel_column" => ["col_name" => "event_id", "col_index" => 5], "db_column" => "event_id"], + ["excel_column" => ["col_name" => "claim_id", "col_index" => 6], "db_column" => "claim_id"], + ["excel_column" => ["col_name" => "insurer_claim_ref_no", "col_index" => 7], "db_column" => "insurer_claim_ref_no"], + ["excel_column" => ["col_name" => "claim_pre_auths", "col_index" => 8], "db_column" => "claim_pre_auths"], + ["excel_column" => ["col_name" => "ma_policy_id", "col_index" => 9], "db_column" => "ma_policy_id"], + ["excel_column" => ["col_name" => "policy_no", "col_index" => 10], "db_column" => "policy_no"], + ["excel_column" => ["col_name" => "policy_holder_name", "col_index" => 11], "db_column" => "policy_holder_name"], + ["excel_column" => ["col_name" => "policy_type", "col_index" => 12], "db_column" => "policy_type"], + ["excel_column" => ["col_name" => "policy_subtype_desc", "col_index" => 13], "db_column" => "policy_subtype_desc"], + ["excel_column" => ["col_name" => "policy_start_date", "col_index" => 14], "db_column" => "policy_start_date"], + ["excel_column" => ["col_name" => "policy_end_date", "col_index" => 15], "db_column" => "policy_end_date"], + ["excel_column" => ["col_name" => "devlopment_officer", "col_index" => 16], "db_column" => "devlopment_officer"], + ["excel_column" => ["col_name" => "agent", "col_index" => 17], "db_column" => "agent"], + ["excel_column" => ["col_name" => "broker", "col_index" => 18], "db_column" => "broker"], + ["excel_column" => ["col_name" => "pribenef_employee_code", "col_index" => 19], "db_column" => "pribenef_employee_code"], + ["excel_column" => ["col_name" => "pribenef_name", "col_index" => 20], "db_column" => "pribenef_name"], + ["excel_column" => ["col_name" => "pribenef_floater_sum", "col_index" => 21], "db_column" => "pribenef_floater_sum"], + ["excel_column" => ["col_name" => "benef_maid", "col_index" => 22], "db_column" => "benef_maid"], + ["excel_column" => ["col_name" => "benef_insurer_id", "col_index" => 23], "db_column" => "benef_insurer_id"], + ["excel_column" => ["col_name" => "benef_name", "col_index" => 24], "db_column" => "benef_name"], + ["excel_column" => ["col_name" => "benef_gender", "col_index" => 25], "db_column" => "benef_gender"], + ["excel_column" => ["col_name" => "benef_relation", "col_index" => 26], "db_column" => "benef_relation"], + ["excel_column" => ["col_name" => "benef_age", "col_index" => 27], "db_column" => "benef_age"], + ["excel_column" => ["col_name" => "benef_sum_insured", "col_index" => 28], "db_column" => "benef_sum_insured"], + ["excel_column" => ["col_name" => "balance_sum_insured", "col_index" => 29], "db_column" => "balance_sum_insured"], + ["excel_column" => ["col_name" => "intimation_id", "col_index" => 30], "db_column" => "intimation_id"], + ["excel_column" => ["col_name" => "intimation_date", "col_index" => 31], "db_column" => "intimation_date"], + ["excel_column" => ["col_name" => "settled_date", "col_index" => 32], "db_column" => "settled_date"], + ["excel_column" => ["col_name" => "ClaimSource", "col_index" => 33], "db_column" => "claim_source"], + ["excel_column" => ["col_name" => "claim_mode_of_rcpt", "col_index" => 34], "db_column" => "claim_mode_of_rcpt"], + ["excel_column" => ["col_name" => "claim_type", "col_index" => 35], "db_column" => "claim_type"], + ["excel_column" => ["col_name" => "claim_sub_type", "col_index" => 36], "db_column" => "claim_sub_type"], + ["excel_column" => ["col_name" => "claim_stage", "col_index" => 37], "db_column" => "claim_stage"], + ["excel_column" => ["col_name" => "claim_status", "col_index" => 38], "db_column" => "claim_status"], + ["excel_column" => ["col_name" => "is_cashlessanywhere", "col_index" => 39], "db_column" => "is_cashlessanywhere"], + ["excel_column" => ["col_name" => "date_of_admission", "col_index" => 40], "db_column" => "date_of_admission"], + ["excel_column" => ["col_name" => "date_of_discharge", "col_index" => 41], "db_column" => "date_of_discharge"], + ["excel_column" => ["col_name" => "claim_amount", "col_index" => 42], "db_column" => "claim_amount"], + ["excel_column" => ["col_name" => "claim_approved_amount", "col_index" => 43], "db_column" => "claim_approved_amount"], + ["excel_column" => ["col_name" => "incurred_amount", "col_index" => 44], "db_column" => "incurred_amount"], + ["excel_column" => ["col_name" => "primary_icd_group", "col_index" => 45], "db_column" => "primary_icd_group"], + ["excel_column" => ["col_name" => "primary_ailment_name", "col_index" => 46], "db_column" => "primary_ailment_name"], + ["excel_column" => ["col_name" => "primary_ailment_code", "col_index" => 47], "db_column" => "primary_ailment_code"], + ["excel_column" => ["col_name" => "treatment_type", "col_index" => 48], "db_column" => "treatment_type"], + ["excel_column" => ["col_name" => "treatment_name", "col_index" => 49], "db_column" => "treatment_name"], + ["excel_column" => ["col_name" => "hospital_id", "col_index" => 50], "db_column" => "hospital_id"], + ["excel_column" => ["col_name" => "hospital_name", "col_index" => 51], "db_column" => "hospital_name"], + ["excel_column" => ["col_name" => "hospital_city", "col_index" => 52], "db_column" => "hospital_city"], + ["excel_column" => ["col_name" => "hospital_state", "col_index" => 53], "db_column" => "hospital_state"], + ["excel_column" => ["col_name" => "hospital_pincode", "col_index" => 54], "db_column" => "hospital_pincode"], + ["excel_column" => ["col_name" => "hospital_address", "col_index" => 55], "db_column" => "hospital_address"], + ["excel_column" => ["col_name" => "Clinic_DoctorName_Hospital", "col_index" => 56], "db_column" => "clinic_doctorname_hospital"], + ["excel_column" => ["col_name" => "OPD_pincode", "col_index" => 57], "db_column" => "opd_pincode"], + ["excel_column" => ["col_name" => "payable_amount_OPD_Consultation", "col_index" => 58], "db_column" => "payable_amount_opd_consultation"], + ["excel_column" => ["col_name" => "payable_amount_Dental", "col_index" => 59], "db_column" => "payable_amount_dental"], + ["excel_column" => ["col_name" => "payable_amount_Diagnostics", "col_index" => 60], "db_column" => "payable_amount_diagnostics"], + ["excel_column" => ["col_name" => "payable_amount_Other", "col_index" => 61], "db_column" => "payable_amount_other"], + ["excel_column" => ["col_name" => "payable_amount_Pharmacy", "col_index" => 62], "db_column" => "payable_amount_pharmacy"], + ["excel_column" => ["col_name" => "payable_amount_Vaccination", "col_index" => 63], "db_column" => "payable_amount_vaccination"], + ["excel_column" => ["col_name" => "payable_amount_Miscellaneous_Charges", "col_index" => 64], "db_column" => "payable_amount_miscellaneous_charges"], + ["excel_column" => ["col_name" => "payable_amount_Health_Checkup", "col_index" => 65], "db_column" => "payable_amount_health_checkup"], + ["excel_column" => ["col_name" => "deduction_amount_copay", "col_index" => 66], "db_column" => "deduction_amount_copay"], + ["excel_column" => ["col_name" => "deduction_amount_excess_ailment", "col_index" => 67], "db_column" => "deduction_amount_excess_ailment"], + ["excel_column" => ["col_name" => "deduction_amount_excess_policy", "col_index" => 68], "db_column" => "deduction_amount_excess_policy"], + ["excel_column" => ["col_name" => "deduction_amount_prorata", "col_index" => 69], "db_column" => "deduction_amount_prorata"], + ["excel_column" => ["col_name" => "deduction_amount_hospital_discount", "col_index" => 70], "db_column" => "deduction_amount_hospital_discount"], + ["excel_column" => ["col_name" => "deduction_amount_paid_by_patient", "col_index" => 71], "db_column" => "deduction_amount_paid_by_patient"], + ["excel_column" => ["col_name" => "deduction_amount_issurer_approved", "col_index" => 72], "db_column" => "deduction_amount_issurer_approved"], + ["excel_column" => ["col_name" => "deduction_amount_deductible", "col_index" => 73], "db_column" => "deduction_amount_deductible"], + ["excel_column" => ["col_name" => "deduction_amount_intimation_penalty", "col_index" => 74], "db_column" => "deduction_amount_intimation_penalty"], + ["excel_column" => ["col_name" => "claim_payable_to_name", "col_index" => 75], "db_column" => "claim_payable_to_name"], + ["excel_column" => ["col_name" => "utr_no", "col_index" => 76], "db_column" => "utr_no"], + ["excel_column" => ["col_name" => "utr_date", "col_index" => 77], "db_column" => "utr_date"], + ["excel_column" => ["col_name" => "denial_short_description", "col_index" => 78], "db_column" => "denial_short_description"], + ["excel_column" => ["col_name" => "claim_received_date", "col_index" => 79], "db_column" => "claim_received_date"], + ["excel_column" => ["col_name" => "last_necessary_doc_rec_date", "col_index" => 80], "db_column" => "last_necessary_doc_rec_date"], + ["excel_column" => ["col_name" => "processed_date", "col_index" => 81], "db_column" => "processed_date"], + ["excel_column" => ["col_name" => "first_document_attached_date", "col_index" => 82], "db_column" => "first_document_attached_date"], + ["excel_column" => ["col_name" => "claim_processing_tat_days", "col_index" => 83], "db_column" => "claim_processing_tat_days"], + ["excel_column" => ["col_name" => "ready_for_payment_date", "col_index" => 84], "db_column" => "ready_for_payment_date"], + ["excel_column" => ["col_name" => "payment_date", "col_index" => 85], "db_column" => "payment_date"], + ["excel_column" => ["col_name" => "claim_payment_tat_days", "col_index" => 86], "db_column" => "claim_payment_tat_days"], + ["excel_column" => ["col_name" => "denial_description", "col_index" => 87], "db_column" => "denial_description"], + ["excel_column" => ["col_name" => "error_group", "col_index" => 88], "db_column" => "error_group"], + ["excel_column" => ["col_name" => "tpa_name", "col_index" => 89], "db_column" => "tpa_name"], + ["excel_column" => ["col_name" => "new_short_error_group", "col_index" => 90], "db_column" => "new_short_error_group"], + ["excel_column" => ["col_name" => "death_claim", "col_index" => 91], "db_column" => "death_claim"], + ["excel_column" => ["col_name" => "vip_claim", "col_index" => 92], "db_column" => "vip_claim"], + ["excel_column" => ["col_name" => "balance_sum_insured_exhausted", "col_index" => 93], "db_column" => "balance_sum_insured_exhausted"], + ]; + + + protected $ticketMasterMapping = [ + + // Employee / Beneficiary details + 'pribenef_employee_code' => 'emp_code', + 'benef_relation' => 'relationship', + + // Policy / Claim identifiers + 'policy_no' => 'policy_no', + 'claim_id' => 'tpa_claim_id', + 'insurer_claim_ref_no' => 'tpa_no', + 'claim_pre_auths' => 'tpa_claim_push_reference_no', + + // Claim type & status + 'claim_type' => 'claim_type', + 'claim_status' => 'tpa_claim_status', + + // Dates + 'date_of_admission' => 'doa', + 'date_of_discharge' => 'dod', + 'intimation_date' => 'date_of_intimat', + 'settled_date' => 'settled_date', + 'claim_received_date' => 'registration_date', + 'processed_date' => 'approved_date', + + // Amounts + 'claim_amount' => 'claim_amount', + 'claim_approved_amount' => 'approved_amount', + + // Hospital details + 'hospital_name' => 'hospital_name', + 'hospital_address' => 'hospital_address', + 'hospital_city' => 'hospital_city', + 'hospital_state' => 'hospital_state', + 'hospital_pincode' => 'hospital_pin_code', + 'hospital_phone_no' => 'hospital_phone_no', + + // Denial / approval + 'denial_description' => 'denial_reason', + 'approved_description' => 'approved_description', + + // Payment + 'utr_no' => 'utr_details', + 'priority' => 1, + 'mode_of_intimation' => 5, + 'ticket_type_id' => 1, + ]; + + + protected $statusMapping = [ + 'Settled' => 11, + 'Rejected' => 8, + 'Denied' => 8, + 'Cancelled' => 13, + 'Processed' => 61, + 'Information Awaited' => 4, + 'Denied Letter Sent' => 66, + 'Cashless Document Awaited' => 3, + ]; + + + /** + * ABSTRACT FUNCTIONs + */ + public function bulkInsertTPATable(array $data): bool + { + if (empty($data)) { + return false; + } + + $builder = $this->db->table('claims_dump_medi_assist'); + $builder->insertBatch($data); + + return true; + } + + + 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_medi_assist'); + $builder->insertBatch($data); + + return true; + } + + + public function updateTicketMasterRejectedReasonInTPATable($data): bool + { + if (empty($data)) { + return false; + } + + $builder = $this->db->table('claims_dump_medi_assist'); + $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; + + if ($this->isDateValue($value)) { + $value = $this->normalizeDate($value); + } + + $item[$dbColumn] = $value; + } + + $params = [ + 'date_of_admission' => change_date_format($item['date_of_admission'] ?? '') ?? null, + 'employee_number' => $item['employee_number'] ?? null, + 'claim_amount' => $item['claim_amount'] ?? null, + 'primary_policy_holder_card_id' => $item['primary_policy_holder_card_id'] ?? null + ]; + + $is_duplicate = $this->checkDuplicateTpaClaim('claims_dump_medi_assist', $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_medi_assist', ['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['date_of_admission'] ?? '') ?? null, + 'emp_code' => $row['employee_number'] ?? null, + 'claim_amount' => $row['claim_amount'] ?? null, + 'tpa_no' => $row['primary_policy_holder_card_id'] ?? 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, $row['gender'] ?? null); + + $employee_data = $this->getEmployeeDetails($file_data['client_id'], $file_data['client_policy_id'], $row['employee_number'], $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_dump_ref_id'] = $row['id']; + $item['file_id'] = $file_id; + $item['claim_status_id'] = $statusMapping[$row['claim_status']] ?? 61; + $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; + + $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 (e.g. 44927) + if (is_numeric($value) && $value > 30000) { + return true; + } + + // Common date formats + return preg_match( + '/^\d{1,2}[\/\-]\d{1,2}[\/\-]\d{2,4}$|^\d{4}[\/\-]\d{1,2}[\/\-]\d{1,2}$/', + (string) $value + ) === 1; + } + + + public function normalizeDate($value): ?string + { + try { + // Excel numeric date + if (is_numeric($value)) { + return date('Y-m-d', \PhpOffice\PhpSpreadsheet\Shared\Date::excelToTimestamp($value)); + } + + // String date + return date('Y-m-d', strtotime(str_replace('/', '-', $value))); + } catch (\Throwable $e) { + return null; + } + } + + + public function convertRelation(string $relation, string $gender): ?string + { + if (empty($relation)) { + return null; + } + + $relation = strtolower($relation); + $gender = strtolower($gender); + + if ($relation == 'self') { + return $relation; + } + + if ($relation == 'spouse') { + return $relation; + } + + if ($relation == 'child' && $gender == 'male') { + return 'son'; + } + + if ($relation == 'child' && $gender == 'female') { + return 'daughter'; + } + + if ($relation == 'parents' && $gender == 'male') { + return 'father'; + } + + if ($relation == 'parents' && $gender == 'female') { + return 'mother'; + } + + if ($relation == 'parents-in-law' && $gender == 'male') { + return 'father-in-law'; + } + + if ($relation == 'parents-in-law' && $gender == 'female') { + return 'mother-in-law'; + } + + return null; + } + + + +} diff --git a/app/Libraries/TPAClaimsImportServices/RcareClaimImportService.php b/app/Libraries/TPAClaimsImportServices/RcareClaimImportService.php new file mode 100644 index 00000000..8fef8232 --- /dev/null +++ b/app/Libraries/TPAClaimsImportServices/RcareClaimImportService.php @@ -0,0 +1,447 @@ + ["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', + 'insured_name' => 'insured_name', + 'patient_name' => 'emp_name', + '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', + + 'priority' => 1, + 'mode_of_intimation' => 5, + 'ticket_type_id' => 1, + ]; + + protected $statusMapping = [ + 'Settled' => 11, + 'Rejected' => 8, + 'Denied' => 8, + 'Cancelled' => 13, + 'Processed' => 61, + 'Information Awaited' => 4, + 'Denied Letter Sent' => 66, + 'Cashless Document Awaited' => 3, + ]; + + /** + * 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; + + if ($this->isDateValue($value)) { + $value = $this->normalizeDate($value); + } + + $item[$dbColumn] = $value; + } + + $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, $row['gender'] ?? 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_type'] = 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 (e.g. 44927) + if (is_numeric($value) && $value > 30000) { + return true; + } + + // Common date formats + return preg_match( + '/^\d{1,2}[\/\-]\d{1,2}[\/\-]\d{2,4}$|^\d{4}[\/\-]\d{1,2}[\/\-]\d{1,2}$/', + (string) $value + ) === 1; + } + + + public function normalizeDate($value): ?string + { + try { + // Excel numeric date + if (is_numeric($value)) { + return date('Y-m-d', \PhpOffice\PhpSpreadsheet\Shared\Date::excelToTimestamp($value)); + } + + // String date + return date('Y-m-d', strtotime(str_replace('/', '-', $value))); + } catch (\Throwable $e) { + return null; + } + } + + + public function convertRelation(string $relation, string $gender): ?string + { + if (empty($relation)) { + return null; + } + + $relation = strtolower($relation); + $gender = strtolower($gender); + + if ($relation == 'self') { + return $relation; + } + + if ($relation == 'spouse') { + return $relation; + } + + if ($relation == 'child' && $gender == 'male') { + return 'son'; + } + + if ($relation == 'child' && $gender == 'female') { + return 'daughter'; + } + + if ($relation == 'parents' && $gender == 'male') { + return 'father'; + } + + if ($relation == 'parents' && $gender == 'female') { + return 'mother'; + } + + if ($relation == 'parents-in-law' && $gender == 'male') { + return 'father-in-law'; + } + + if ($relation == 'parents-in-law' && $gender == 'female') { + return 'mother-in-law'; + } + + return null; + } + + + +} diff --git a/app/Libraries/TPAClaimsImportServices/VidalClaimImportService.php b/app/Libraries/TPAClaimsImportServices/VidalClaimImportService.php new file mode 100644 index 00000000..33243d22 --- /dev/null +++ b/app/Libraries/TPAClaimsImportServices/VidalClaimImportService.php @@ -0,0 +1,811 @@ + "TPA Policy Number", "db_column" => "tpa_policy_number"], + ["excel_column" => "Insurer Policy Number", "db_column" => "insurer_policy_number"], + ["excel_column" => "Corporate Name", "db_column" => "corporate_name"], + ["excel_column" => "Proposer Name", "db_column" => "proposer_name"], + ["excel_column" => "Corporate Group ID", "db_column" => "corporate_group_id"], + + ["excel_column" => "Policy Start Date", "db_column" => "policy_start_date"], + ["excel_column" => "Policy End date", "db_column" => "policy_end_date"], + + ["excel_column" => "Insurance Company Name", "db_column" => "insurance_company_name"], + + ["excel_column" => "Employee Number", "db_column" => "employee_number"], + ["excel_column" => "Employee Location", "db_column" => "employee_location"], + + ["excel_column" => "Primary Policy Holder Name", "db_column" => "primary_policy_holder_name"], + ["excel_column" => "Primary Policy Holder Card ID", "db_column" => "primary_policy_holder_card_id"], + + ["excel_column" => "Employee Grade", "db_column" => "employee_grade"], + + ["excel_column" => "Patient Health Card ID", "db_column" => "patient_health_card_id"], + ["excel_column" => "Patient Name", "db_column" => "patient_name"], + ["excel_column" => "Date of Birth", "db_column" => "date_of_birth"], + ["excel_column" => "Age", "db_column" => "age"], + ["excel_column" => "Gender", "db_column" => "gender"], + ["excel_column" => "Relation", "db_column" => "relation"], + + ["excel_column" => "Sum Insured", "db_column" => "sum_insured"], + ["excel_column" => "Balance Sum Insured", "db_column" => "balance_sum_insured"], + ["excel_column" => "Top Up Sum Insured", "db_column" => "top_up_sum_insured"], + + ["excel_column" => "Enrollment status", "db_column" => "enrollment_status"], + + ["excel_column" => "Policy Inception date", "db_column" => "policy_inception_date"], + ["excel_column" => "Policy Exit Date", "db_column" => "policy_exit_date"], + + ["excel_column" => "Insurer Risk ID", "db_column" => "insurer_risk_id"], + + ["excel_column" => "Claim Preauth Identifier", "db_column" => "claim_preauth_identifier"], + ["excel_column" => "TPA Claim Number", "db_column" => "tpa_claim_number"], + ["excel_column" => "Preauth Number", "db_column" => "preauth_number"], + ["excel_column" => "Insurer Claim Number", "db_column" => "insurer_claim_number"], + + ["excel_column" => "Claim Received date", "db_column" => "claim_received_date"], + ["excel_column" => "Date of Admission", "db_column" => "date_of_admission"], + ["excel_column" => "Date of Discharge", "db_column" => "date_of_discharge"], + + ["excel_column" => "Hospital Name", "db_column" => "hospital_name"], + ["excel_column" => "Hospital ID", "db_column" => "hospital_id"], + ["excel_column" => "Hospital Address", "db_column" => "hospital_address"], + ["excel_column" => "Hospital City", "db_column" => "hospital_city"], + ["excel_column" => "Hospital State", "db_column" => "hospital_state"], + ["excel_column" => "Hospital Pincode", "db_column" => "hospital_pincode"], + ["excel_column" => "Hospital Network status", "db_column" => "hospital_network_status"], + ["excel_column" => "PPN Hospital Status", "db_column" => "ppn_hospital_status"], + + ["excel_column" => "Room Category", "db_column" => "room_category"], + ["excel_column" => "Room Days", "db_column" => "room_days"], + ["excel_column" => "ICU Days", "db_column" => "icu_days"], + + ["excel_column" => "Medical Surgical Identifier", "db_column" => "medical_surgical_identifier"], + ["excel_column" => "Treatment Type of AYUSH", "db_column" => "treatment_type_of_ayush"], + ["excel_column" => "Package", "db_column" => "package"], + + ["excel_column" => "Diagnosis", "db_column" => "diagnosis"], + ["excel_column" => "Illness Details", "db_column" => "illness_details"], + ["excel_column" => "Ailment Grouping", "db_column" => "ailment_grouping"], + + ["excel_column" => "Primary ICD Code", "db_column" => "primary_icd_code"], + ["excel_column" => "Secondary ICD Code", "db_column" => "secondary_icd_code"], + + ["excel_column" => "Procedure Code", "db_column" => "procedure_code"], + ["excel_column" => "Procedure Description", "db_column" => "procedure_description"], + ["excel_column" => "Procedure Grouping", "db_column" => "procedure_grouping"], + + ["excel_column" => "Claim Submission mode", "db_column" => "claim_submission_mode"], + ["excel_column" => "Admission Category", "db_column" => "admission_category"], + ["excel_column" => "Type of Claim", "db_column" => "type_of_claim"], + ["excel_column" => "MainClaim_Prepost type", "db_column" => "mainclaim_prepost_type"], + ["excel_column" => "Type of Hospitalization", "db_column" => "type_of_hospitalization"], + ["excel_column" => "Death Status", "db_column" => "death_status"], + ["excel_column" => "Treatement Given", "db_column" => "treatment_given"], + + ["excel_column" => "Claim Amount", "db_column" => "claim_amount"], + ["excel_column" => "Total Billed Amount", "db_column" => "total_billed_amount"], + + ["excel_column" => "Claim Consultation Charges", "db_column" => "claim_consultation_charges"], + ["excel_column" => "Claim Investigation Charges", "db_column" => "claim_investigation_charges"], + ["excel_column" => "Claim Medicines", "db_column" => "claim_medicines"], + ["excel_column" => "Claim Nursing", "db_column" => "claim_nursing"], + ["excel_column" => "Claim Room Charges", "db_column" => "claim_room_charges"], + ["excel_column" => "Claim Icu Charges", "db_column" => "claim_icu_charges"], + ["excel_column" => "Claim Stay Charges", "db_column" => "claim_stay_charges"], + ["excel_column" => "Claim Surgeon Charges", "db_column" => "claim_surgeon_charges"], + ["excel_column" => "Claim Surgery Charges", "db_column" => "claim_surgery_charges"], + ["excel_column" => "Claim Miscellaneous Charges", "db_column" => "claim_miscellaneous_charges"], + ["excel_column" => "Claim Other Charges", "db_column" => "claim_other_charges"], + + ["excel_column" => "Approved Consultation Charges", "db_column" => "approved_consultation_charges"], + ["excel_column" => "Approved Investigation Charges", "db_column" => "approved_investigation_charges"], + ["excel_column" => "Approved Medicines", "db_column" => "approved_medicines"], + ["excel_column" => "Approved Nursing", "db_column" => "approved_nursing"], + ["excel_column" => "Approved Room Charges", "db_column" => "approved_room_charges"], + ["excel_column" => "Approved Icu Charges", "db_column" => "approved_icu_charges"], + ["excel_column" => "Approved Stay Charges", "db_column" => "approved_stay_charges"], + ["excel_column" => "Approved Surgeon Charges", "db_column" => "approved_surgeon_charges"], + ["excel_column" => "Approved Surgery Charges", "db_column" => "approved_surgery_charges"], + ["excel_column" => "Approved Miscellaneous Charges", "db_column" => "approved_miscellaneous_charges"], + ["excel_column" => "Approved Others Charges", "db_column" => "approved_others_charges"], + + ["excel_column" => "Total Disallowed Amount", "db_column" => "total_disallowed_amount"], + ["excel_column" => "Copayment Amount", "db_column" => "copayment_amount"], + ["excel_column" => "Deposit Amount", "db_column" => "deposit_amount"], + + ["excel_column" => "Exceeds Policy Limit", "db_column" => "exceeds_policy_limit"], + ["excel_column" => "Copay Buffer", "db_column" => "copay_buffer"], + ["excel_column" => "Hospital Discount Amount", "db_column" => "hospital_discount_amount"], + ["excel_column" => "Deductible Amount", "db_column" => "deductible_amount"], + + ["excel_column" => "Approved Amount", "db_column" => "approved_amount"], + ["excel_column" => "Total Incurred Amount", "db_column" => "total_incurred_amount"], + ["excel_column" => "Total Buffer Approved", "db_column" => "total_buffer_approved"], + ["excel_column" => "Total Buffer Utlilized", "db_column" => "total_buffer_utilized"], + + ["excel_column" => "TDS Amount", "db_column" => "tds_amount"], + ["excel_column" => "Net Amount", "db_column" => "net_amount"], + + ["excel_column" => "Claim Decision date", "db_column" => "claim_decision_date"], + ["excel_column" => "Payment Reference Number", "db_column" => "payment_reference_number"], + ["excel_column" => "Payment Reference Date", "db_column" => "payment_reference_date"], + + ["excel_column" => "Claim Status", "db_column" => "claim_status"], + ["excel_column" => "Deduction Remarks", "db_column" => "deduction_remarks"], + ["excel_column" => "Rejection Reasons", "db_column" => "rejection_reasons"], + ["excel_column" => "Claim Query Reasons", "db_column" => "claim_query_reasons"], + + ["excel_column" => "Insurer Request Sent date", "db_column" => "insurer_request_sent_date"], + ["excel_column" => "Insurer Conf. Received date", "db_column" => "insurer_conf_received_date"], + ["excel_column" => "Claim Reopen Date", "db_column" => "claim_reopen_date"], + + ["excel_column" => "First Query Raised date", "db_column" => "first_query_raised_date"], + ["excel_column" => "First Query response date", "db_column" => "first_query_response_date"], + ["excel_column" => "Last Query Raised date", "db_column" => "last_query_raised_date"], + ["excel_column" => "Last Query response date", "db_column" => "last_query_response_date"], + ["excel_column" => "Last Document Received date", "db_column" => "last_document_received_date"], + ]; + + + protected $mapping = [ + + ["excel_column" => ["col_name" => "TPA Policy Number", "col_index" => 0], "db_column" => "tpa_policy_number"], + ["excel_column" => ["col_name" => "Insurer Policy Number", "col_index" => 1], "db_column" => "insurer_policy_number"], + ["excel_column" => ["col_name" => "Corporate Name", "col_index" => 2], "db_column" => "corporate_name"], + ["excel_column" => ["col_name" => "Proposer Name", "col_index" => 3], "db_column" => "proposer_name"], + ["excel_column" => ["col_name" => "Corporate Group ID", "col_index" => 4], "db_column" => "corporate_group_id"], + + ["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" => "Insurance Company Name", "col_index" => 7], "db_column" => "insurance_company_name"], + + ["excel_column" => ["col_name" => "Employee Number", "col_index" => 8], "db_column" => "employee_number"], + ["excel_column" => ["col_name" => "Employee Location", "col_index" => 9], "db_column" => "employee_location"], + + ["excel_column" => ["col_name" => "Primary Policy Holder Name", "col_index" => 10], "db_column" => "primary_policy_holder_name"], + ["excel_column" => ["col_name" => "Primary Policy Holder Card ID", "col_index" => 11], "db_column" => "primary_policy_holder_card_id"], + + ["excel_column" => ["col_name" => "Employee Grade", "col_index" => 12], "db_column" => "employee_grade"], + + ["excel_column" => ["col_name" => "Patient Health Card ID", "col_index" => 13], "db_column" => "patient_health_card_id"], + ["excel_column" => ["col_name" => "Patient Name", "col_index" => 14], "db_column" => "patient_name"], + ["excel_column" => ["col_name" => "Date of Birth", "col_index" => 15], "db_column" => "date_of_birth"], + ["excel_column" => ["col_name" => "Age", "col_index" => 16], "db_column" => "age"], + ["excel_column" => ["col_name" => "Gender", "col_index" => 17], "db_column" => "gender"], + ["excel_column" => ["col_name" => "Relation", "col_index" => 18], "db_column" => "relation"], + + ["excel_column" => ["col_name" => "Sum Insured", "col_index" => 19], "db_column" => "sum_insured"], + ["excel_column" => ["col_name" => "Balance Sum Insured", "col_index" => 20], "db_column" => "balance_sum_insured"], + ["excel_column" => ["col_name" => "Top Up Sum Insured", "col_index" => 21], "db_column" => "top_up_sum_insured"], + + ["excel_column" => ["col_name" => "Enrollment status", "col_index" => 22], "db_column" => "enrollment_status"], + + ["excel_column" => ["col_name" => "Policy Inception date", "col_index" => 23], "db_column" => "policy_inception_date"], + ["excel_column" => ["col_name" => "Policy Exit Date", "col_index" => 24], "db_column" => "policy_exit_date"], + + ["excel_column" => ["col_name" => "Insurer Risk ID", "col_index" => 25], "db_column" => "insurer_risk_id"], + + ["excel_column" => ["col_name" => "Claim Preauth Identifier", "col_index" => 26], "db_column" => "claim_preauth_identifier"], + ["excel_column" => ["col_name" => "TPA Claim Number", "col_index" => 27], "db_column" => "tpa_claim_number"], + ["excel_column" => ["col_name" => "Preauth Number", "col_index" => 28], "db_column" => "preauth_number"], + ["excel_column" => ["col_name" => "Insurer Claim Number", "col_index" => 29], "db_column" => "insurer_claim_number"], + + ["excel_column" => ["col_name" => "Claim Received date", "col_index" => 30], "db_column" => "claim_received_date"], + ["excel_column" => ["col_name" => "Date of Admission", "col_index" => 31], "db_column" => "date_of_admission"], + ["excel_column" => ["col_name" => "Date of Discharge", "col_index" => 32], "db_column" => "date_of_discharge"], + + ["excel_column" => ["col_name" => "Hospital Name", "col_index" => 33], "db_column" => "hospital_name"], + ["excel_column" => ["col_name" => "Hospital ID", "col_index" => 34], "db_column" => "hospital_id"], + ["excel_column" => ["col_name" => "Hospital Address", "col_index" => 35], "db_column" => "hospital_address"], + ["excel_column" => ["col_name" => "Hospital City", "col_index" => 36], "db_column" => "hospital_city"], + ["excel_column" => ["col_name" => "Hospital State", "col_index" => 37], "db_column" => "hospital_state"], + ["excel_column" => ["col_name" => "Hospital Pincode", "col_index" => 38], "db_column" => "hospital_pincode"], + ["excel_column" => ["col_name" => "Hospital Network status", "col_index" => 39], "db_column" => "hospital_network_status"], + ["excel_column" => ["col_name" => "PPN Hospital Status", "col_index" => 40], "db_column" => "ppn_hospital_status"], + + ["excel_column" => ["col_name" => "Room Category", "col_index" => 41], "db_column" => "room_category"], + ["excel_column" => ["col_name" => "Room Days", "col_index" => 42], "db_column" => "room_days"], + ["excel_column" => ["col_name" => "ICU Days", "col_index" => 43], "db_column" => "icu_days"], + + ["excel_column" => ["col_name" => "Medical Surgical Identifier", "col_index" => 44], "db_column" => "medical_surgical_identifier"], + ["excel_column" => ["col_name" => "Treatment Type of AYUSH", "col_index" => 45], "db_column" => "treatment_type_of_ayush"], + ["excel_column" => ["col_name" => "Package", "col_index" => 46], "db_column" => "package"], + + ["excel_column" => ["col_name" => "Diagnosis", "col_index" => 47], "db_column" => "diagnosis"], + ["excel_column" => ["col_name" => "Illness Details", "col_index" => 48], "db_column" => "illness_details"], + ["excel_column" => ["col_name" => "Ailment Grouping", "col_index" => 49], "db_column" => "ailment_grouping"], + + ["excel_column" => ["col_name" => "Primary ICD Code", "col_index" => 50], "db_column" => "primary_icd_code"], + ["excel_column" => ["col_name" => "Secondary ICD Code", "col_index" => 51], "db_column" => "secondary_icd_code"], + + ["excel_column" => ["col_name" => "Procedure Code", "col_index" => 52], "db_column" => "procedure_code"], + ["excel_column" => ["col_name" => "Procedure Description", "col_index" => 53], "db_column" => "procedure_description"], + ["excel_column" => ["col_name" => "Procedure Grouping", "col_index" => 54], "db_column" => "procedure_grouping"], + + ["excel_column" => ["col_name" => "Claim Submission mode", "col_index" => 55], "db_column" => "claim_submission_mode"], + ["excel_column" => ["col_name" => "Admission Category", "col_index" => 56], "db_column" => "admission_category"], + ["excel_column" => ["col_name" => "Type of Claim", "col_index" => 57], "db_column" => "type_of_claim"], + ["excel_column" => ["col_name" => "MainClaim_Prepost type", "col_index" => 58], "db_column" => "mainclaim_prepost_type"], + ["excel_column" => ["col_name" => "Type of Hospitalization", "col_index" => 59], "db_column" => "type_of_hospitalization"], + ["excel_column" => ["col_name" => "Death Status", "col_index" => 60], "db_column" => "death_status"], + ["excel_column" => ["col_name" => "Treatement Given", "col_index" => 61], "db_column" => "treatment_given"], + + ["excel_column" => ["col_name" => "Claim Amount", "col_index" => 62], "db_column" => "claim_amount"], + ["excel_column" => ["col_name" => "Total Billed Amount", "col_index" => 63], "db_column" => "total_billed_amount"], + + ["excel_column" => ["col_name" => "Claim Consultation Charges", "col_index" => 64], "db_column" => "claim_consultation_charges"], + ["excel_column" => ["col_name" => "Claim Investigation Charges", "col_index" => 65], "db_column" => "claim_investigation_charges"], + ["excel_column" => ["col_name" => "Claim Medicines", "col_index" => 66], "db_column" => "claim_medicines"], + ["excel_column" => ["col_name" => "Claim Nursing", "col_index" => 67], "db_column" => "claim_nursing"], + ["excel_column" => ["col_name" => "Claim Room Charges", "col_index" => 68], "db_column" => "claim_room_charges"], + ["excel_column" => ["col_name" => "Claim Icu Charges", "col_index" => 69], "db_column" => "claim_icu_charges"], + ["excel_column" => ["col_name" => "Claim Stay Charges", "col_index" => 70], "db_column" => "claim_stay_charges"], + ["excel_column" => ["col_name" => "Claim Surgeon Charges", "col_index" => 71], "db_column" => "claim_surgeon_charges"], + ["excel_column" => ["col_name" => "Claim Surgery Charges", "col_index" => 72], "db_column" => "claim_surgery_charges"], + ["excel_column" => ["col_name" => "Claim Miscellaneous Charges", "col_index" => 73], "db_column" => "claim_miscellaneous_charges"], + ["excel_column" => ["col_name" => "Claim Other Charges", "col_index" => 74], "db_column" => "claim_other_charges"], + + ["excel_column" => ["col_name" => "Approved Consultation Charges", "col_index" => 75], "db_column" => "approved_consultation_charges"], + ["excel_column" => ["col_name" => "Approved Investigation Charges", "col_index" => 76], "db_column" => "approved_investigation_charges"], + ["excel_column" => ["col_name" => "Approved Medicines", "col_index" => 77], "db_column" => "approved_medicines"], + ["excel_column" => ["col_name" => "Approved Nursing", "col_index" => 78], "db_column" => "approved_nursing"], + ["excel_column" => ["col_name" => "Approved Room Charges", "col_index" => 79], "db_column" => "approved_room_charges"], + ["excel_column" => ["col_name" => "Approved Icu Charges", "col_index" => 80], "db_column" => "approved_icu_charges"], + ["excel_column" => ["col_name" => "Approved Stay Charges", "col_index" => 81], "db_column" => "approved_stay_charges"], + ["excel_column" => ["col_name" => "Approved Surgeon Charges", "col_index" => 82], "db_column" => "approved_surgeon_charges"], + ["excel_column" => ["col_name" => "Approved Surgery Charges", "col_index" => 83], "db_column" => "approved_surgery_charges"], + ["excel_column" => ["col_name" => "Approved Miscellaneous Charges", "col_index" => 84], "db_column" => "approved_miscellaneous_charges"], + ["excel_column" => ["col_name" => "Approved Others Charges", "col_index" => 85], "db_column" => "approved_others_charges"], + + ["excel_column" => ["col_name" => "Total Disallowed Amount", "col_index" => 86], "db_column" => "total_disallowed_amount"], + ["excel_column" => ["col_name" => "Copayment Amount", "col_index" => 87], "db_column" => "copayment_amount"], + ["excel_column" => ["col_name" => "Deposit Amount", "col_index" => 88], "db_column" => "deposit_amount"], + + ["excel_column" => ["col_name" => "Exceeds Policy Limit", "col_index" => 89], "db_column" => "exceeds_policy_limit"], + ["excel_column" => ["col_name" => "Copay Buffer", "col_index" => 90], "db_column" => "copay_buffer"], + ["excel_column" => ["col_name" => "Hospital Discount Amount", "col_index" => 91], "db_column" => "hospital_discount_amount"], + ["excel_column" => ["col_name" => "Deductible Amount", "col_index" => 92], "db_column" => "deductible_amount"], + + ["excel_column" => ["col_name" => "Approved Amount", "col_index" => 93], "db_column" => "approved_amount"], + ["excel_column" => ["col_name" => "Total Incurred Amount", "col_index" => 94], "db_column" => "total_incurred_amount"], + ["excel_column" => ["col_name" => "Total Buffer Approved", "col_index" => 95], "db_column" => "total_buffer_approved"], + ["excel_column" => ["col_name" => "Total Buffer Utlilized", "col_index" => 96], "db_column" => "total_buffer_utilized"], + + ["excel_column" => ["col_name" => "TDS Amount", "col_index" => 97], "db_column" => "tds_amount"], + ["excel_column" => ["col_name" => "Net Amount", "col_index" => 98], "db_column" => "net_amount"], + + ["excel_column" => ["col_name" => "Claim Decision date", "col_index" => 99], "db_column" => "claim_decision_date"], + ["excel_column" => ["col_name" => "Payment Reference Number", "col_index" => 100], "db_column" => "payment_reference_number"], + ["excel_column" => ["col_name" => "Payment Reference Date", "col_index" => 101], "db_column" => "payment_reference_date"], + + ["excel_column" => ["col_name" => "Claim Status", "col_index" => 102], "db_column" => "claim_status"], + ["excel_column" => ["col_name" => "Deduction Remarks", "col_index" => 103], "db_column" => "deduction_remarks"], + ["excel_column" => ["col_name" => "Rejection Reasons", "col_index" => 104], "db_column" => "rejection_reasons"], + ["excel_column" => ["col_name" => "Claim Query Reasons", "col_index" => 105], "db_column" => "claim_query_reasons"], + + ["excel_column" => ["col_name" => "Insurer Request Sent date", "col_index" => 106], "db_column" => "insurer_request_sent_date"], + ["excel_column" => ["col_name" => "Insurer Conf. Received date", "col_index" => 107], "db_column" => "insurer_conf_received_date"], + ["excel_column" => ["col_name" => "Claim Reopen Date", "col_index" => 108], "db_column" => "claim_reopen_date"], + + ["excel_column" => ["col_name" => "First Query Raised date", "col_index" => 109], "db_column" => "first_query_raised_date"], + ["excel_column" => ["col_name" => "First Query response date", "col_index" => 110], "db_column" => "first_query_response_date"], + ["excel_column" => ["col_name" => "Last Query Raised date", "col_index" => 111], "db_column" => "last_query_raised_date"], + ["excel_column" => ["col_name" => "Last Query response date", "col_index" => 112], "db_column" => "last_query_response_date"], + ["excel_column" => ["col_name" => "Last Document Received date", "col_index" => 113], "db_column" => "last_document_received_date"], + ]; + + + protected $ticketMasterMapping = [ + + // Policy / Claim identifiers + 'insurer_policy_number' => 'policy_no', + 'insurer_claim_number' => 'claim_number', + 'tpa_claim_number' => 'tpa_claim_id', + + // Employee / Insured details + 'employee_number' => 'emp_code', + 'primary_policy_holder_name' => 'emp_name', + 'patient_name' => 'insured_name', + + // Dates + 'date_of_admission' => 'doa', + 'date_of_discharge' => 'dod', + 'date_of_birth' => 'dob', + 'claim_received_date' => 'registration_date', + + // Claim details + 'claim_amount' => 'claim_amount', + 'approved_amount' => 'approved_amount', + 'sum_insured' => 'si_amt', + 'claim_status' => 'tpa_claim_status', + + // Hospital details + 'hospital_name' => 'hospital_name', + 'hospital_address' => 'hospital_address', + 'hospital_city' => 'hospital_city', + 'hospital_state' => 'hospital_state', + 'hospital_pincode' => 'hospital_pin_code', + + // Meta + 'file_id' => 'file_id', + 'priority' => 1, + 'mode_of_intimation' => 5, + 'ticket_type_id' => 1, + ]; + + + protected $statusMapping = [ + 'CL Paid with Settlement Letter' => 11, + 'CL Rejected' => 8, + 'CL Approved' => 9, + 'AL Closed' => 12, + ]; + + + /** + * ABSTRACT FUNCTIONs + */ + public function bulkInsertTPATable(array $data): bool + { + if (empty($data)) { + return false; + } + + $builder = $this->db->table('claims_dump_vidal'); + $builder->insertBatch($data); + + return true; + } + + + 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_vidal'); + $builder->insertBatch($data); + + return true; + } + + + public function updateTicketMasterRejectedReasonInTPATable($data): bool + { + if (empty($data)) { + return false; + } + + $builder = $this->db->table('claims_dump_vidal'); + $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; + + if ($this->isDateValue($value)) { + $value = $this->normalizeDate($value); + } + + $item[$dbColumn] = $value; + } + + $params = [ + 'date_of_admission' => change_date_format($item['date_of_admission'] ?? '') ?? null, + 'employee_number' => $item['employee_number'] ?? null, + 'claim_amount' => $item['claim_amount'] ?? null, + 'primary_policy_holder_card_id' => $item['primary_policy_holder_card_id'] ?? null + ]; + + $is_duplicate = $this->checkDuplicateTpaClaim('claims_dump_vidal', $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_vidal', ['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['date_of_admission'] ?? '') ?? null, + 'emp_code' => $row['employee_number'] ?? null, + 'claim_amount' => $row['claim_amount'] ?? null, + 'tpa_no' => $row['primary_policy_holder_card_id'] ?? 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, $row['gender'] ?? null); + + $employee_data = $this->getEmployeeDetails($file_data['client_id'], $file_data['client_policy_id'], $row['employee_number'], $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['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; + + $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]; + } + } + + public function mapClaimMasterDataOld($file_id): array + { + + $tpaClaimDumpData = $this->db + ->table('claims_dump_vidal') + ->where('is_active', 1) + ->where('file_id', $file_id) + ->where('ticket_id IS NULL') + ->get() + ->getResultArray(); + + if (empty($tpaClaimDumpData)) { + return []; + } + + $ClientPolicyModel = new ClientPolicyModel(); + $client_policy_data = $ClientPolicyModel->where('is_active', 1)->findAll(); + + $mapped = []; + $rejecetd_reason = []; + + foreach ($tpaClaimDumpData as $row) { + + $item = []; + + if(isset($row['insurer_policy_number']) && !empty($row['insurer_policy_number'])){ + + $basic_claim_data = $this->getClientPolicyDataBypolicyNo($client_policy_data, $row['insurer_policy_number']); + if(empty($basic_claim_data)){ + $reason = " This policy no ( " . $row['insurer_policy_number'] ." ) does not exist in our system"; + $rejecetd_reason[$row['id']] = ["ticket_master_insert_reject_reason" => $reason]; + continue; + } + + $item['client_id'] = $basic_claim_data['client_id'] ?? null; + $item['client_policy_id'] = $basic_claim_data['id'] ?? null; + $item['insurer_id'] = $basic_claim_data['insurer_id'] ?? null; + $item['tpa_id'] = $basic_claim_data['tpa_id'] ?? null; + $item['acm_id'] = $basic_claim_data['acm_id'] ?? null; + $item['policy_no'] = $basic_claim_data['policy_no'] ?? null; + $item['relationship'] = $this->convertRelation($row['relation'] ?? null, $row['gender'] ?? null); + + $employee_data = $this->getEmployeeDetails($item['client_id'], $item['client_policy_id'], $row['employee_number'], $item['relationship']); + if(!empty($employee_data)){ + $item['emp_id'] = $employee_data['client_id'] ?? null; + $item['emp_name'] = $employee_data['id'] ?? null; + $item['emp_mail'] = $employee_data['insurer_id'] ?? null; + $item['emp_mobile'] = $employee_data['tpa_id'] ?? null; + $item['insured_emp_id'] = $employee_data['insured_emp_id'] ?? null; + $item['insured_name'] = $employee_data['insured_name'] ?? null; + }else{ + $reason = " This policy no ( " . $row['insurer_policy_number'] ." ) does not exist in our system"; + $rejecetd_reason[$row['id']] = ["ticket_master_insert_reject_reason" => $reason]; + continue; + } + + }else{ + $reason = " Policy Number is empty"; + $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; + } + + $isduplicate = checkDuplicateClaim([ + 'doa' => change_date_format($item['doa'] ?? '') ?? null, + 'emp_code' => $item['emp_code'] ?? null, + 'claim_amount' => $item['claim_amount'] ?? null, + 'policy_no' => $item['policy_no'] ?? null + ]); + + if($isduplicate){ + $reason = " This claim ( " . $row['insurer_policy_number'] ." ) does not exist in our system"; + $rejecetd_reason[$row['id']] = ["ticket_master_insert_reject_reason" => $reason]; + continue; + } + + // Meta fields + $item['file_id'] = $file_id; + $item['created_by'] = get_session_userid() ?? null; + $item['claim_type'] = isset($row['mainclaim_prepost_type']) && !empty($row['mainclaim_prepost_type']) && strtolower($row['mainclaim_prepost_type']) == 'normal' ? 1 : 3; + + $mapped[] = $item; + + } + + return ['mapped_array' => $mapped, 'rejected_reason_array' => $rejecetd_reason]; + } + + + /** + * HELPER FUNCTIONs + */ + + public function isDateValue($value): bool + { + if (empty($value)) { + return false; + } + + // Excel numeric date (e.g. 44927) + if (is_numeric($value) && $value > 30000) { + return true; + } + + // Common date formats + return preg_match( + '/^\d{1,2}[\/\-]\d{1,2}[\/\-]\d{2,4}$|^\d{4}[\/\-]\d{1,2}[\/\-]\d{1,2}$/', + (string) $value + ) === 1; + } + + + public function normalizeDate($value): ?string + { + try { + // Excel numeric date + if (is_numeric($value)) { + return date('Y-m-d', \PhpOffice\PhpSpreadsheet\Shared\Date::excelToTimestamp($value)); + } + + // String date + return date('Y-m-d', strtotime(str_replace('/', '-', $value))); + } catch (\Throwable $e) { + return null; + } + } + + + public function looksLikeDate($value): bool + { + if (empty($value)) { + return false; + } + + // Excel numeric date + if (is_numeric($value) && $value > 30000) { + return true; + } + + return preg_match( + '/\d{1,2}[\/\-]\d{1,2}[\/\-]\d{2,4}| + \d{4}[\/\-]\d{1,2}[\/\-]\d{1,2}| + \d{1,2}\s?[A-Za-z]{3,}\s?\d{2,4}| + \d{8}/x', + (string) $value + ) === 1; + } + + + public function convertRelation(string $relation, string $gender): ?string + { + if (empty($relation)) { + return null; + } + + $relation = strtolower($relation); + $gender = strtolower($gender); + + if ($relation == 'self') { + return $relation; + } + + if ($relation == 'spouse') { + return $relation; + } + + if ($relation == 'child' && $gender == 'male') { + return 'son'; + } + + if ($relation == 'child' && $gender == 'female') { + return 'daughter'; + } + + if ($relation == 'parents' && $gender == 'male') { + return 'father'; + } + + if ($relation == 'parents' && $gender == 'female') { + return 'mother'; + } + + if ($relation == 'parents-in-law' && $gender == 'male') { + return 'father-in-law'; + } + + if ($relation == 'parents-in-law' && $gender == 'female') { + return 'mother-in-law'; + } + + return null; + } + + + public function getClientPolicyDataBypolicyNo(array $client_policy_data, string $policy_number): array + { + $matched_data = []; + foreach ($client_policy_data as $key => $value) { + if(trim($value) == trim($policy_number)){ + $matched_data = $value; + } + } + + if(!empty($matched_data)){ + $ClientRMModel = new ClientRMModel(); + $acm_data = $ClientRMModel->where('is_active', 1)->where('level', 3)->where('client_id', $matched_data['client_id'])->orderBy('id', 'asc')->first(); + if(!empty($acm_data)){ + $matched_data['acm_id'] = $acm_data['id'] ?? null; + } + } + + return $matched_data; + } + +} diff --git a/app/Libraries/TpaClaimsImportFactory.php b/app/Libraries/TpaClaimsImportFactory.php new file mode 100644 index 00000000..baeef074 --- /dev/null +++ b/app/Libraries/TpaClaimsImportFactory.php @@ -0,0 +1,37 @@ + new VidalClaimImportService(), + (int) env('ABHI_PRIMARY_KEY_CONSTANT') => new AbhiClaimImportService(), + (int) env('MEDI_ASSIST_PRIMARY_KEY_CONSTANT') => new MediAssistClaimImportService(), + (int) env('FHPL_PRIMARY_KEY_CONSTANT') => new FhplClaimImportService(), + (int) env('R_CARE_PRIMARY_KEY_CONSTANT') => new RcareClaimImportService(), + (int) env('ICICI_PRIMARY_KEY_CONSTANT') => new IciciClaimImportService(), + default => throw new InvalidArgumentException( + "Unsupported TPA ID: {$tpaId}" + ), + }; + } +} diff --git a/app/Models/ClaimDumpFileModel.php b/app/Models/ClaimDumpFileModel.php index 224adafc..e34d6116 100644 --- a/app/Models/ClaimDumpFileModel.php +++ b/app/Models/ClaimDumpFileModel.php @@ -10,6 +10,9 @@ class ClaimDumpFileModel extends Model protected $primaryKey = 'id'; protected $allowedFields = [ "id", + "client_id", + "client_policy_id", + "tpa_id", "file_name", "status", "reason", diff --git a/app/Models/ClaimsDumpFhplModel.php b/app/Models/ClaimsDumpFhplModel.php new file mode 100644 index 00000000..d8cd6632 --- /dev/null +++ b/app/Models/ClaimsDumpFhplModel.php @@ -0,0 +1,141 @@ +getResultArray(); // $countofalldata = count($result); // dd($result); - // dd($this->db->getLastQuery()->getQuery()); + dd($this->db->getLastQuery()->getQuery()); $keys = []; $filtered = []; diff --git a/app/Views/claim_dump_file_list.php b/app/Views/claim_dump_file_list.php index 696d97ce..0cf55ed9 100644 --- a/app/Views/claim_dump_file_list.php +++ b/app/Views/claim_dump_file_list.php @@ -22,6 +22,8 @@ } .dataTables_length label {height: 21px !important;} + + .readonly-select { background-color: #f3f3f3 !important; cursor: not-allowed; pointer-events: none; } + [ Sample Excel ]