table('ticket_master tm') ->select(' tm.id, tm.emp_mobile as mobileNo, tm.emp_mail as emailId, tm.doa as claimDateOfAdmission, tm.dod as claimDateOfDischarge, tm.hospital_name as hospName, tm.claim_amount as claimAmount, cp.policy_no as policyNo, e.id as empId, tm.tpa_no as memberId, tn.note as disease, tn.note as reasonForHospitalization, cf.url as fileName, cf.url as filePath ') ->join('employees e', 'e.id = tm.emp_id', 'left') ->join('client_policy cp', 'tm.client_policy_id = cp.id', 'left') ->join('ticket_notes tn', 'tn.ticket_id = tm.id', 'left') ->join('claim_files cf', "cf.ticket_id = tm.id AND cf.file_type = 2 and cf.mime_type = 'application/pdf'", 'left') ->where('tm.id', $claimId) ->get() ->getRowArray(); // single record // Map DB result to request body if ($data) { $filePath = $data['filePath'] ?? ''; $filename = basename($filePath); $fileDir = WRITEPATH . 'uploads/claim_files/'.$filename; if ($fileDir) { $downloadUrl = base_url('fileDownload?file_path=').$fileDir; } else { $downloadUrl = ''; } $body = [ "policyNo" => $data['policyNo'] ?? "", "memberId" => $data['memberId'] ?? "", "mobileNo" => $data['mobileNo'] ?? "", "emailId" => $data['emailId'] ?? "", "claimDateOfAdmission" => $data['claimDateOfAdmission'] ?? "", "claimDateOfDischarge" => $data['claimDateOfDischarge'] ?? "", "hospName" => $data['hospName'] ?? "", "hospAddress" => $data['hospName'] ?? "", // If hospAddress not available, reuse hospName "reasonForHospitalization" => $data['reasonForHospitalization'] ?? "", "disease" => $data['disease'] ?? "", "claimAmount" => (float)($data['claimAmount'] ?? 0), "claimType" => "HOSPITALIZATION", // static value "claimSubmissionAttachments" => [ "fileName" => $data['fileName'] ?? "", "filePath" => $downloadUrl ?? "" ] ]; } else { $body = []; } // dd($body); // $body = [ // "policyNo" => "570000/48/2025/286", // "memberId" => 4078919887, // "mobileNo" => "95000XXXXX", // "emailId" => "test@Medi.com", // "claimDateOfAdmission" => "2023-12-14", // "claimDateOfDischarge" => "2023-12-16", // "hospName" => "TestHosp", // "hospAddress" => "Testhosp", // "reasonForHospitalization" => "test", // "disease" => "testkediney", // "claimAmount" => 10, // "claimType" => "HOSPITALIZATION", // "claimSubmissionAttachments" => [ // "fileName" => "Test.pdf", // "filePath" => "https://apiintegration.mediassist.in/IntegrationEcard/DownloadEcard/4078613742/Senthil Kumar P/556/5386" // ] // ]; log_message('error', 'TPA CLAIM PUSH | claimId: '.$claimId.' | payload: '.json_encode($body)); $response = call_third_party_api($url, $method, $headers, $body); if($response['status'] != true){ log_message('error', 'TPA CLAIM PUSH FAILED | claimId: '.$claimId.' | response: '.json_encode($response)); return; } // return $this->response->setJSON($response); $claimRef = $response['data']['claimReferenceNo'] ?? null; if(!empty($claimRef)){ log_message('error', 'TPA CLAIM PUSH SUCCESS | claimId: '.$claimId.' | claimReferenceNo: '.$claimRef); $db->table('ticket_master') ->where('id',$claimId) ->update([ 'tpa_claim_push_reference_no' => $claimRef ]); return; } else { log_message('error', 'TPA CLAIM PUSH SUCCESS BUT claimReferenceNo EMPTY | claimId: '.$claimId.' | response: '.json_encode($response)); return; } } public function EcardRequest ($employeeId = null, $policyNo = null){ helper('api'); // $url = ''. getenv('MEDI_ASSIST_API_BASE_URL').'/EcardUrl'; $url = getenv('MEDI_ASSIST_API_BASE_URL_ECARDREQUEST'); $method = 'POST'; $headers = [ 'Content-Type: application/json', 'Username:'. getenv('MEDI_ASSIST_API_USERNAME').'', 'Password:'. getenv('MEDI_ASSIST_API_PASSWORD').'', ]; $body = [ "employeeId" => $employeeId, "policyNo" => $policyNo ]; // $body = [ // "employeeId" => "CITPL120006", // "policyNo" => "97000063250400000031" // ]; $response = call_third_party_api($url, $method, $headers, $body); if($response['status'] != true){ log_message('error', 'Ecard Request FAILED | employeeId: '.$employeeId.' | policyNo: '.$policyNo.' | response: '.json_encode($response)); return null; } $ecardUrl = $response['data']['ecardUrl'] ?? null; if(!empty($ecardUrl)){ log_message('error', 'Ecard Request PUSH SUCCESS | employeeId: '.$employeeId.' | policyNo: '.$policyNo.' | ecardUrl: '.$ecardUrl); return $ecardUrl; } else { log_message('error', 'Ecard Request SUCCESS BUT ecardUrl EMPTY | employeeId: '.$employeeId.' | policyNo: '.$policyNo.' | response: '.json_encode($response)); return null; } } public function GetBenefDetails($requestData) { helper('api'); $function_calling_type = $requestData['return_type'] ?? 'job'; try { // $url = getenv('MEDI_ASSIST_API_BASE_URL') . '/GetBenefDetails'; $url = getenv('MEDI_ASSIST_API_BASE_URL_FETCHTPA'); $method = 'POST'; $headers = [ 'Content-Type: application/json', 'Username:' . getenv('MEDI_ASSIST_API_USERNAME'), 'Password:' . getenv('MEDI_ASSIST_API_PASSWORD'), ]; $policyNo = $requestData['policy_no'] ?? null; $client_policy_id = $requestData['client_policy_id'] ?? null; if (empty($policyNo)) { log_message('error', 'GetBenefDetails: policy_no missing in request'); if($function_calling_type == "job"){ return ['status' => false, 'message' => 'policy_no required']; }else{ return $this->respond(['status' => false, 'message' => 'policy_no required']); } } if (empty($client_policy_id)) { log_message('error', 'GetBenefDetails: client_policy_id missing in request'); if($function_calling_type == "job"){ return ['status' => false, 'message' => 'client_policy_id required']; }else{ return $this->respond(['status' => false, 'message' => 'client_policy_id required']); } } log_message('error', "GetBenefDetails called for policy_no: {$policyNo}"); log_message('error', "GetBenefDetails called for client_policy_id: {$client_policy_id}"); $employeePolicyModel = new EmployeePolicyModel(); $employeePolicyData = $employeePolicyModel ->select(' employees.*, employee_polices.id as emp_policy_id, employee_polices.client_policy_id, ') ->join('employees', 'employees.id = employee_polices.employee_id') ->where('employee_polices.is_active', 1) ->where('employee_polices.status', 'active') ->where('employees.is_active', 1) ->where('employees.emp_status', 'active') ->where('employee_polices.tpa_id IS NULL') ->where('employee_polices.client_policy_id', $client_policy_id) ->findAll(); if (empty($employeePolicyData)) { log_message('error', 'GetBenefDetails: employeePolicyData is empty'); if($function_calling_type == "job"){ return ['status' => false, 'message' => 'employeePolicyData not found']; }else{ return $this->respond(['status' => false, 'message' => 'employeePolicyData not found']); } } $allBenef = []; $startIndex = 0; $range = 100; $totalCount = 0; do { $body = [ "policyNo" => $policyNo, "startDate" => "", "endDate" => "", "isDeActivedata" => false, "startIndex" => $startIndex, "range" => $range, "employeeId" => "" ]; log_message('error', "GetBenefDetails API Request (startIndex={$startIndex}): " . json_encode($body)); log_message('error', "GetBenefDetails API parems " . json_encode([$url, $method, $headers, $body])); $response = call_third_party_api($url, $method, $headers, $body); if ($response['status'] != true) { // update file table status after the tpa id failed to update if (isset($requestData['file_id']) && !empty($requestData['file_id'])) { $file_model = new BatchFileModel(); $file_model->where('id', $requestData['file_id'])->set('status', 'failed-8')->update(); log_message('error', "Files table status updated for the file id : {$requestData['file_id']}"); } else { log_message('error', "Failed to update file table status."); } log_message('error', 'GetBenefDetails API failed: ' . json_encode($response)); if($function_calling_type == "job"){ return ['status' => false, 'message' => 'API call failed', 'data' => $response]; }else{ return $this->respond(['status' => false, 'message' => 'API call failed', 'data' => $response]); } } $data = $response['data'] ?? []; if (!isset($data['benefDetails'])) { log_message('error', "GetBenefDetails: 'benefDetails' missing in API response: " . json_encode($data)); break; } $count = $data['count'] ?? 0; $totalCount = $count; $fetchedCount = count($data['benefDetails']); log_message('error', "Fetched {$fetchedCount} records (startIndex={$startIndex}) of total {$count}"); $allBenef = array_merge($allBenef, $data['benefDetails']); $startIndex += $range; } while ($startIndex < $totalCount); // now update DB $db = \Config\Database::connect(); $updated = 0; $employee_policy_ids = []; foreach ($employeePolicyData as $policy_data) { foreach ($allBenef as $row) { // log_message( // "error", // "POLICY MATCH CHECK: " . json_encode([ // 'policy_data' => [ // 'name' => $policy_data['name'] ?? null, // 'emp_code' => $policy_data['emp_code'] ?? null, // 'relationship' => $policy_data['relationship'] ?? null, // 'gender' => $policy_data['gender'] ?? null, // 'dob' => $policy_data['dob'] ?? null, // ], // 'row_data' => [ // 'benefName' => $row['benefName'] ?? null, // 'priBenefEmpCode' => $row['priBenefEmpCode'] ?? null, // 'relName' => $row['relName'] ?? null, // 'benefSex' => $row['benefSex'] ?? null, // 'benefDOB' => $row['benefDOB'] ?? null, // 'benefDOB_fmt' => change_date_format($row['benefDOB'],'d/m/Y H:i:s') ?? null, // ], // ]) // ); if ( strtolower(trim($policy_data['name'] ?? '')) == strtolower(trim($row['benefName'] ?? '')) && ($policy_data['emp_code'] ?? '') == ($row['priBenefEmpCode'] ?? '') && strtolower(trim($policy_data['relationship'] ?? '')) == strtolower(trim($row['relName'] ?? '')) && ($policy_data['gender'] ?? '') == ($row['benefSex'] ?? '') && ($policy_data['dob'] ?? '') == (change_date_format($row['benefDOB'], 'd/m/Y H:i:s') ?? '') ) { log_message('error', "✅ Match found: emp_code={$row['priBenefEmpCode']} policy={$row['polNo']}"); $sql = "UPDATE employee_polices SET tpa_id = ? WHERE id = ?"; $db->query($sql, [$row['benefMediAssistID'], $policy_data['emp_policy_id']]); // for e-card send if(strtolower(trim($policy_data['relationship'])) == 'self'){ $employee_policy_ids[] = $policy_data['emp_policy_id']; } if ($db->affectedRows() > 0) { $updated++; log_message('error', "✅ Updated tpa_id={$row['benefMediAssistID']} for emp_code={$row['priBenefEmpCode']} policy={$row['polNo']}"); } else { log_message('error', "⚠️ No update (already set or not matched) for emp_code={$row['priBenefEmpCode']} policy={$row['polNo']}"); } }else{ log_message('error', "❌ Not matched: emp_code={$row['priBenefEmpCode']} policy={$row['polNo']}"); } } } // send e-card if(!empty($employee_policy_ids)){ log_message('error', "sendMailForDownloadingECard JOB PUSHED."); Jobs::addJob(['job_name' => 'sendMailForDownloadingECard', 'payload' => $employee_policy_ids]); } // update file table status after the tpa id successfully updated if (isset($requestData['file_id']) && !empty($requestData['file_id'])) { $file_model = new BatchFileModel(); $file_model->where('id', $requestData['file_id'])->set('status', 'success')->update(); log_message('error', "Files table status updated for the file id : {$requestData['file_id']}"); } else { log_message('error', "Failed to update file table status."); } log_message('error', "GetBenefDetails completed. Total fetched={$totalCount}, updated={$updated}"); if($function_calling_type == "job"){ return [ 'status' => true, 'message' => 'Updated successfully', 'total_fetched' => $totalCount, 'total_updated' => $updated ]; }else{ return $this->respond([ 'status' => true, 'message' => 'Updated successfully', 'total_fetched' => $totalCount, 'total_updated' => $updated ]); } } catch (\Throwable $th) { // update file table status after the tpa id failed to update if (isset($requestData['file_id']) && !empty($requestData['file_id'])) { $file_model = new BatchFileModel(); $file_model->where('id', $requestData['file_id'])->set('status', 'failed-8')->update(); log_message('error', "Files table status updated for the file id : {$requestData['file_id']}"); } else { log_message('error', "Failed to update file table status."); } $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, ]; log_message('error', 'Exception thrown while calling GetBenefDetails API: ' . json_encode($errorData)); if($function_calling_type == "job"){ return ['status' => false, 'message' => 'API call failed', 'data' => $errorData]; }else{ return $this->respond(['status' => false, 'message' => 'API call failed', 'data' => $errorData]); } } } // public function GetBenefDetails (){ // $postData = $this->request->getJSON(true); // helper('api'); // $url = ''. getenv('MEDI_ASSIST_API_BASE_URL').'/GetBenefDetails'; // $method = 'POST'; // $headers = [ // 'Content-Type: application/json', // 'Username:'. getenv('MEDI_ASSIST_API_USERNAME').'', // 'Password:'. getenv('MEDI_ASSIST_API_PASSWORD').'', // ]; // $body = [ // "policyNo" => $$postData['policy_no'], // "startDate" => "", // "endDate" => "", // "isDeActivedata" => false, // "startIndex" => 0, // "range" => 100 , // "employeeId" => "" // ]; // // $body = [ // // "policyNo" => "97000063250400000031", // // "startDate" => "", // // "endDate" => "", // // "isDeActivedata" => false, // // "startIndex" => 0, // // "range" => 100 , // // "employeeId" => "" // // ]; // $response = call_third_party_api($url, $method, $headers, $body); // if($response['status'] != true){ // return $this->response->setJSON([ // 'status' => false, // 'message' => 'failed.', // 'data' => $response // ]); // } // return $this->response->setJSON($response); // } public function ClaimDetail (){ helper('api'); $url = ''. getenv('MEDI_ASSIST_API_BASE_URL').'/ClaimDetail'; $method = 'POST'; $headers = [ 'Content-Type: application/json', 'Username:'. getenv('MEDI_ASSIST_API_USERNAME').'', 'Password:'. getenv('MEDI_ASSIST_API_PASSWORD').'', ]; $body = [ "policyNo" => "97000063250400000031", "startDate" => "", "endDate" => "", "employeeCode" => "CITPL120193", "memberID" => "", "claimNo" => "", "claimRefNo" => "" ]; $response = call_third_party_api($url, $method, $headers, $body); if($response['status'] != true){ return $this->response->setJSON([ 'status' => false, 'message' => 'failed.', 'data' => $response ]); } return $this->response->setJSON($response); } public function HospitalNetwork (){ $postData = $this->request->getJSON(true); helper('api'); $url = ''. getenv('MEDI_ASSIST_API_BASE_URL').'/NetworkHospital'; $method = 'POST'; $headers = [ 'Content-Type: application/json', 'Username:'. getenv('MEDI_ASSIST_API_USERNAME').'', 'Password:'. getenv('MEDI_ASSIST_API_PASSWORD').'', ]; $body = [ "startIndex" => 0, "endIndex" => 10, "policyNumber" => "97000063250400000031" ]; $response = call_third_party_api($url, $method, $headers, $body); if($response['status'] != true){ return $this->response->setJSON([ 'status' => false, 'message' => 'failed.', 'data' => $response ]); } return $this->response->setJSON($response); } public function IntimateClaim (){ $postData = $this->request->getJSON(true); helper('api'); // $url = ''. getenv('MEDI_ASSIST_API_BASE_URL').'/ClaimAPIServiceUAT/Claim/IntimateClaim'; $url = "https://apiintegration.mediassist.in/ClaimAPIServiceUAT/Claim/IntimateClaim"; $method = 'POST'; $headers = [ 'Content-Type: application/json', 'Username:' .'NhanceUsr', 'Password:' .'NhU$p&Cc5wGQbr2', ]; $p = 'NhU$p&Cc5wGQbr2'; $body = [ "Username" => "NhanceUsr", "Password" => $p, "policyNo" => "570000/48/2025/286", "memberId" => 4078919887, "DateOfAdmisssion" => date('Y-m-d', strtotime('2023-12-14')), "HospitalName" => "test", "AilmentDescription" => "Kidney", "ContactNo" => "78745XXXXX" ]; $response = call_third_party_api($url, $method, $headers, $body); if($response['status'] != true){ return $this->response->setJSON([ 'status' => false, 'message' => 'failed.', 'data' => $response ]); } return $this->response->setJSON($response); } public function IRSubmission (){ $postData = $this->request->getJSON(true); helper('api'); // $url = ''. getenv('MEDI_ASSIST_API_BASE_URL').'/ClaimAPIServiceUAT/Claim/IRSubmission'; $url = "https://apiintegration.mediassist.in/ClaimAPIServiceUAT/Claim/IRSubmission"; $method = 'POST'; $headers = [ 'Content-Type: application/json', 'Username:' .'NhanceUsr', 'Password:' .'NhU$p&Cc5wGQbr2', ]; $body = [ "ClaimID" => "134431104", "Attachments" => [ "AttachmentName" => "Test.pdf", "AttachmentPath" => "https://apiintegration.mediassist.in/IntegrationEcard/DownloadEcard/4078613742/Senthil Kumar P/556/5386" ] ]; $response = call_third_party_api($url, $method, $headers, $body); if($response['status'] != true){ return $this->response->setJSON([ 'status' => false, 'message' => 'failed.', 'data' => $response ]); } return $this->response->setJSON($response); } public function fileDownload() { $filePath = $this->request->getGet('file_path'); $filename = basename($filePath); // Return file as download return $this->response->download($filePath, null)->setFileName($filename); } }