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, e.emp_code as memberId, tn.note as disease, tn.note as reasonForHospitalization, cf.doc_name 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['empId'] ?? "", "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" // ] // ]; $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_LIVE').'/EcardUrl'; $method = 'POST'; $headers = [ 'Content-Type: application/json', 'Username:'. getenv('MEDI_ASSIST_API_USERNAME_LIVE').'', 'Password:'. getenv('MEDI_ASSIST_API_PASSWORD_LIVE').'', ]; $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_LIVE') . '/GetBenefDetails'; $method = 'POST'; $headers = [ 'Content-Type: application/json', 'Username:' . getenv('MEDI_ASSIST_API_USERNAME_LIVE'), 'Password:' . getenv('MEDI_ASSIST_API_PASSWORD_LIVE'), ]; $policyNo = $requestData['policy_no'] ?? 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']); } } log_message('error', "GetBenefDetails called for policy_no: {$policyNo}"); $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)); $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 FileModel(); $file_model->where('id', $requestData['file_id'])->set('status', 'failed')->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; foreach ($allBenef as $row) { // $update = $db->table('employee_polices') // ->join('client_policy', 'employee_polices.client_policy_id = client_policy.id AND client_policy.policy_no = ' . $db->escape($row['polNo']), 'left') // ->join( // 'employees', // 'employee_polices.employee_id = employees.id // AND employees.emp_code = ' . $db->escape($row['priBenefEmpCode']) . ' // AND employees.relationship = ' . $db->escape($row['relName']), // 'left' // ) // ->where('employee_polices.tpa_id IS NULL') // ->update(['employee_polices.tpa_id' => $row['benefMediAssistID']]); $update = $db->table('employee_polices') ->join('client_policy', 'employee_polices.client_policy_id = client_policy.id', 'left') ->join('employees', 'employee_polices.employee_id = employees.id', 'left') ->where('client_policy.policy_no', $row['polNo']) ->where('employees.emp_code', $row['priBenefEmpCode']) ->where('employees.relationship', $row['relName']) ->where('employee_polices.tpa_id IS NULL', null, false) ->update(['employee_polices.tpa_id' => $row['benefMediAssistID']]); if ($db->affectedRows() > 0) { $updated++; log_message('error', "Updated tpa_id for emp_code={$row['priBenefEmpCode']}, policy={$row['polNo']}"); } } // update file table status after the tpa id successfully updated if (isset($requestData['file_id']) && !empty($requestData['file_id'])) { $file_model = new FileModel(); $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 FileModel(); $file_model->where('id', $requestData['file_id'])->set('status', 'failed')->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_LIVE').'/GetBenefDetails'; // $method = 'POST'; // $headers = [ // 'Content-Type: application/json', // 'Username:'. getenv('MEDI_ASSIST_API_USERNAME_LIVE').'', // 'Password:'. getenv('MEDI_ASSIST_API_PASSWORD_LIVE').'', // ]; // $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_LIVE').'/ClaimDetail'; $method = 'POST'; $headers = [ 'Content-Type: application/json', 'Username:'. getenv('MEDI_ASSIST_API_USERNAME_LIVE').'', 'Password:'. getenv('MEDI_ASSIST_API_PASSWORD_LIVE').'', ]; $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_LIVE').'/NetworkHospital'; $method = 'POST'; $headers = [ 'Content-Type: application/json', 'Username:'. getenv('MEDI_ASSIST_API_USERNAME_LIVE').'', 'Password:'. getenv('MEDI_ASSIST_API_PASSWORD_LIVE').'', ]; $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); } }