From c1c0887b2d8f8d0d9f4ec5158237763eadd1321b Mon Sep 17 00:00:00 2001 From: Gowtham M Date: Wed, 24 Dec 2025 18:45:25 +0530 Subject: [PATCH] sync tpa claims to nhance database: GWM --- app/Config/Routes.php | 3 +- app/Controllers/MediAssistApiController.php | 355 +++++++++++++++++++- 2 files changed, 342 insertions(+), 16 deletions(-) diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 13aff9ac..60be1178 100755 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -767,11 +767,12 @@ $routes->get('claimStatus','VidalApiController::claimStatus'); $routes->get('EcardRequest','MediAssistApiController::EcardRequest'); $routes->get('HospitalNetwork','MediAssistApiController::HospitalNetwork'); $routes->get('GetBenefDetails','MediAssistApiController::GetBenefDetails'); -$routes->get('ClaimDetail','VidalApiController::ClaimDetail'); +$routes->get('ClaimDetail','MediAssistApiController::ClaimDetail'); $routes->get('SubmitClaim','MediAssistApiController::SubmitClaim'); $routes->get('IntimateClaim','MediAssistApiController::IntimateClaim'); $routes->get('IRSubmission','MediAssistApiController::IRSubmission'); $routes->get('ClaimStatusUpdate','MediAssistApiController::ClaimStatusUpdate'); +$routes->get('syncTpaClaimToNhance','MediAssistApiController::syncTpaClaimToNhance'); // API service diff --git a/app/Controllers/MediAssistApiController.php b/app/Controllers/MediAssistApiController.php index 245157e4..692eee3a 100644 --- a/app/Controllers/MediAssistApiController.php +++ b/app/Controllers/MediAssistApiController.php @@ -6,6 +6,7 @@ use App\Controllers\BaseController; use App\Models\BatchFileModel; use App\Models\EmployeePolicyModel; use App\Models\TpaApiDataModel; +use App\Models\ClientPolicyModel; use CodeIgniter\HTTP\ResponseInterface; use CodeIgniter\HTTP\IncomingRequest; use CodeIgniter\HTTP\RequestInterface; @@ -25,6 +26,7 @@ class MediAssistApiController extends BaseController } + public function SubmitClaim ($claimId = null) { @@ -482,7 +484,7 @@ class MediAssistApiController extends BaseController } } - public function ClaimDetail($claimId = null) // 585 this id for test + public function ClaimDetail($claimId = 585) // 585 this id for test { helper('api'); @@ -545,19 +547,21 @@ class MediAssistApiController extends BaseController // dd($body); - // $body = [ - // "policyNo" => "97000063250400000031", - // "startDate" => "31/08/2025", - // "endDate" => "01/09/2025", - // "employeeCode" => "CITPL120193", - // "memberID" => "", - // "claimNo" => "", - // "claimRefNo" => "HOSP4078102577_16092025111030" - // ]; + $body = [ + "policyNo" => "97000063250400000031", + "startDate" => "01/11/2025", + "endDate" => "05/11/2025", + "employeeCode" => "", + "memberID" => "", + "claimNo" => "", + "claimRefNo" => "" + ]; // CALL API $response = call_third_party_api($url, $method, $headers, $body); + dd($response); + if ($response['status'] != true || empty($response['data']['claimsData'][0])) { log_message('error', 'CLAIM STATUS FAILED | for ticket ID: ' . $claimId.' | response: '.json_encode($response)); @@ -616,9 +620,9 @@ class MediAssistApiController extends BaseController // Maping tpa claim status with local claim Status if (isset($validStatuses[$currentStatus])) { - $updateArray = ['claim_status_id' => $validStatuses[$currentStatus] , 'tpa_claim_status' => $currentStatus , 'tpa_claim_id' => $tpa_claim_no , 'updated_at' => date('Y-m-d H:i:s')]; + $updateArray = ['claim_status_id' => $validStatuses[$currentStatus] , 'tpa_claim_status' => $currentStatus , 'tpa_claim_id' => $tpa_claim_no, 'claim_number' => $tpa_claim_no, 'updated_at' => date('Y-m-d H:i:s')]; }else{ - $updateArray = ['tpa_claim_status' => $currentStatus , 'tpa_claim_id' => $tpa_claim_no , 'updated_at' => date('Y-m-d H:i:s')]; + $updateArray = ['tpa_claim_status' => $currentStatus , 'tpa_claim_id' => $tpa_claim_no, 'claim_number' => $tpa_claim_no , 'updated_at' => date('Y-m-d H:i:s')]; } // UPDATE ticket_master @@ -797,7 +801,6 @@ class MediAssistApiController extends BaseController // unlink($file_array['json_file_path']); // delete temp json file } - public function ClaimStatusUpdate() { helper('api'); @@ -928,9 +931,9 @@ class MediAssistApiController extends BaseController // Maping tpa claim status with local claim Status if (isset($validStatuses[$currentStatus])) { - $updateArray = ['claim_status_id' => $validStatuses[$currentStatus] , 'tpa_claim_status' => $currentStatus , 'tpa_claim_id' => $tpa_claim_no , 'updated_at' => date('Y-m-d H:i:s')]; + $updateArray = ['claim_status_id' => $validStatuses[$currentStatus] , 'tpa_claim_status' => $currentStatus , 'tpa_claim_id' => $tpa_claim_no , 'claim_number' => $tpa_claim_no , 'updated_at' => date('Y-m-d H:i:s')]; }else{ - $updateArray = ['tpa_claim_status' => $currentStatus , 'tpa_claim_id' => $tpa_claim_no , 'updated_at' => date('Y-m-d H:i:s')]; + $updateArray = ['tpa_claim_status' => $currentStatus , 'tpa_claim_id' => $tpa_claim_no , 'claim_number' => $tpa_claim_no, 'updated_at' => date('Y-m-d H:i:s')]; } // UPDATE ticket_master @@ -952,6 +955,328 @@ class MediAssistApiController extends BaseController ]); } + public function syncTpaClaimToNhance() + { + helper(['api', 'date']); + + $url = getenv('MEDI_ASSIST_API_BASE_URL_CLAIMSTATUS'); + $method = 'POST'; + + $headers = [ + 'Content-Type: application/json', + 'Username:' . getenv('MEDI_ASSIST_API_USERNAME'), + 'Password:' . getenv('MEDI_ASSIST_API_PASSWORD'), + ]; + + // REQUEST DATES + $reqStartDate = $this->request->getGet('start_date'); + $reqEndDate = $this->request->getGet('end_date'); + + // If start_date OR end_date is missing → last 7 days + if (empty($reqStartDate) || empty($reqEndDate)) { + + $endDateObj = new \DateTime(); // today + $startDateObj = (clone $endDateObj)->modify('-6 days'); + + } else { + + $startDateObj = new \DateTime($reqStartDate); + $endDateObj = new \DateTime($reqEndDate); + } + + // Final usable variables + $reqStartDate = $startDateObj->format('Y-m-d'); + $reqEndDate = $endDateObj->format('Y-m-d'); + + // Final usable variables + $reqStartDate = new \DateTime($reqStartDate); + $reqEndDate = new \DateTime($reqEndDate); + + + + // FETCH POLICY DATA + $policies = $this->db->table('client_policy cp') + ->select(" + cp.policy_no AS policyNo, + cp.policy_start_date AS policyStartDate, + cp.policy_end_date AS policyEndDate + ") + ->where('cp.is_active', 1) + ->where('cp.policy_type_id', 2) + ->where('cp.policy_status', 1) + // ->where('cp.policy_no', '97000063250400000031') + ->where('cp.tpa_id', $this->mediAssistTpaId) + ->get() + ->getResultArray(); + + if (empty($policies)) { + log_message('error', 'No policies found for claim sync'); + return $this->response->setJSON([ + 'status' => false, + 'message' => 'Policies not found' + ]); + } + + + + $finalResult = []; + $errorData = []; + + + // LOOP POLICIES + + foreach ($policies as $policy) { + + $policyStart = new \DateTime($policy['policyStartDate']); + $policyEnd = new \DateTime($policy['policyEndDate']); + + + // ADJUST DATE RANGE (inside policy period) + + $startDate = max($reqStartDate, $policyStart); + $endDate = min($reqEndDate, $policyEnd); + + + if ($startDate > $endDate) { + continue; // No valid range for this policy + } + + + // SPLIT INTO 7-DAY CHUNKS + + $chunkStart = clone $startDate; + + while ($chunkStart <= $endDate) { + + $chunkEnd = clone $chunkStart; + $chunkEnd->modify('+6 days'); + + if ($chunkEnd > $endDate) { + $chunkEnd = clone $endDate; + } + + + // PREPARE BODY + + $body = [ + "policyNo" => $policy['policyNo'], + "startDate" => $chunkStart->format('d/m/Y'), + "endDate" => $chunkEnd->format('d/m/Y'), + "employeeCode" => "", + "memberID" => "", + "claimNo" => "", + "claimRefNo" => "", + ]; + + + // CALL TPA API + + $response = call_third_party_api($url, $method, $headers, $body); + + if (empty($response['status']) || empty($response['data']['claimsData'])) { + + log_message('error','CLAIM STATUS FAILED | ' .'policyNo: ' . $policy['policyNo'] .' | ' . $chunkStart->format('Y-m-d') .' to ' . $chunkEnd->format('Y-m-d') .' | response: ' . json_encode($response) ); + + $errorData[] = [ + 'policy_no' => $policy['policyNo'], + 'start' => $chunkStart->format('Y-m-d'), + 'end' => $chunkEnd->format('Y-m-d'), + ]; + + } else { + + $finalResult = array_merge($finalResult,$response['data']['claimsData']); + } + + // Move to next chunk + $chunkStart->modify('+7 days'); + } + } + + + // 5️⃣ FINAL RESPONSE + foreach ($finalResult as $key => $value) { + + $relationship = strtolower(trim($value['relation'] ?? '')); + + if ($relationship === 'employee') { + $relationship = 'self'; + } + + $ticket = $this->db->table('ticket_master tm')->select("tm.id") + ->where('tm.policy_no', $value['policY_NUMBER']) + ->where('tm.emp_code', $value['employeE_NO']) + ->where('tm.claim_number', $value['tpA_CLAIM_NO']) + ->get() + ->getRowArray(); + + if(!$ticket) + { + + // VALID STATUS LIST + $validStatuses = [ + + "Claim Received" => 1, + "In Progress" => 5, + "Processed" => 11, + "Claim Paid" => 11, + "Denied" => 13, + "Cancelled" => 13, + + "Information Awaited" => 4, + "Confirmation Awaited" => 4, + "Information Awaited Reminder" => 4, + "Information Awaited Final Reminder" => 4, + "Insurer Concurrence Awaited" => 6, + "Closed" => 12, + + "Physical Documents Awaited" => 9, + "Processed - Payment Initiated" => 10, + "Processed - Transaction Failed" => 10, + "Processed - Account Details Updated" => 10, + "Processed - Debit Note Raised With Insurer for Payment"=> 10, + "Processed - Payment Initiated by Insurer" => 10, + "Payment - Refunded to Insurer" => 14, + "Processed - Processing Payment" => 10, + "Processed - Physical Documents Awaited" => 9, + + // Extra Mappings (based on your DB list) + "NON ID" => 1, + "ID NOT GENERATED" => 2, + "CDA" => 3, + "REJECTED" => 8, + "APPROVED" => 9, + "PAYMENT INITIATED" => 10, + "SETTLED" => 11, + "RETURNED" => 14, + "UNDER PROCESS - TPA" => 61, + "DENIAL REVIEW AWAITED" => 66, + ]; + + $currentStatus = trim($value['claim_Current_Status'] ?? ''); + + $claimStatusId = $validStatuses[$currentStatus] ?? null; + + if (!$claimStatusId) { + log_message('error', 'Unknown claim status: '.$currentStatus); + continue; + } + + $clientpolicy = $this->db->table('client_policy cp') + ->select(" + cp.id as client_policy_id, + cp.client_id , + cp.insurer_id , + cp.tpa_id , + client_rm.id as acm_id + ") + ->join('client_rm', 'client_rm.client_id = cp.client_id AND client_rm.level = 3', 'left') + ->where('cp.policy_no', $value['policY_NUMBER']) + ->get() + ->getRowArray(); + + $employee = $this->db->table('employees e') + ->select(" + e.id as emp_id, + e.emp_code , + e2.id as insured_emp_id, + ") + ->join( + 'employees e2', + "e2.emp_code = e.emp_code AND e2.relationship = ".$this->db->escape($relationship), + 'left' + ) + ->where('e.emp_code', $value['employeE_NO']) + ->where('e.relationship', 'self') + ->get() + ->getRowArray(); + + + + $claimData = [ + // Core + 'ticket_type_id' => 1, + 'claim_status_id' => $claimStatusId, + 'policy_no' => $value['policY_NUMBER'] ?? null, + 'claim_number' => $value['tpA_CLAIM_NO'] ?? null, + 'tpa_claim_id' => $value['tpA_CLAIM_NO'] ?? null, + + // local promary id + 'tpa_id' => $clientpolicy['tpa_id'] ?? null, + 'insurer_id' => $clientpolicy['insurer_id'] ?? null, + 'client_policy_id' => $clientpolicy['client_policy_id'] ?? null, + 'client_id' => $clientpolicy['client_id'] ?? null, + 'acm_id' => $clientpolicy['acm_id'] ?? null, + + // Employee / Insured + 'emp_id' => $employee['emp_id'] ?? null, + 'insured_emp_id' => $employee['insured_emp_id'] ?? null, + 'emp_code' => $value['employeE_NO'] ?? null, + 'emp_name' => $value['employeE_NAME'] ?? null, + 'insured_name' => $value['beneficiarY_NAME'] ?? null, + 'relationship' => $relationship, + 'emp_mobile' => $value['mobile'] ?? null, + 'emp_mail' => $value['email'] ?? null, + + // Claim info + 'claim_type' => 1, + 'mode_of_intimation' => 5, + 'claim_amount' => $value['estimateD_CLAIM_AMOUNT'] + ?? $value['finaL_BILL_AMOUNT'] + ?? null, + 'approved_amount' => $value['claiM_APPROVED_AMOUNT'] ?? null, + + // Dates + 'dob' => $this->mediDate($value['datE_OF_BIRTH'] ?? null), + 'doa' => $this->mediDate($value['datE_OF_ADMISSION'] ?? null), + 'dod' => $this->mediDate($value['datE_OF_DISCHARGE'] ?? null), + 'raised_date' => $this->mediDate($value['claiM_REGISTERED_DATE'] ?? null), + 'approved_date' => $this->mediDate($value['approved_Date'] ?? null), + 'settled_date' => $this->mediDate($value['paiD_DATE'] ?? null), + + // Hospital + 'hospital_name' => $value['hospitaL_NAME'] ?? null, + 'hospital_address' => $value['hospitaL_ADDRESS'] ?? null, + 'hospital_state' => $value['hospitaL_STATE'] ?? null, + 'hospital_city' => $value['hospitaL_CITY'] ?? null, + 'hospital_pin_code'=> $value['hosP_PINCODE'] ?? null, + + // Payment + 'utr_details' => $value['banK_CHEQUE_NO'] ?? null, + 'settle_letter' => $value['settlement_LetterLink'] ?? null, + ]; + + $this->db->table('ticket_master')->insert($claimData); + + log_message( + 'info', + 'New claim created | Policy: '.$claimData['policy_no']. + ' | Claim: '.$claimData['claim_number'] + ); + + } + + } + + + return $this->response->setJSON([ + 'status' => true, + 'message' => 'TPA claim status sync completed', + 'total_records' => count($finalResult), + 'result' => $finalResult, + 'errors' => $errorData + ]); + } + + + public function mediDate($date) + { + if (empty($date)) return null; + $dt = \DateTime::createFromFormat('d/m/Y H:i:s', $date); + return $dt ? $dt->format('Y-m-d') : null; + } + +