nhance/app/Controllers/FhplApiController.php

1047 lines
41 KiB
PHP

<?php
namespace App\Controllers;
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;
use CodeIgniter\API\ResponseTrait;
use App\Controllers\Jobs;
class FhplApiController extends BaseController
{
use ResponseTrait;
protected $db;
protected $fhplTpaId;
public function __construct()
{
$this->db = \Config\Database::connect();
$this->fhplTpaId = getenv('FHPL_PRIMARY_KEY_CONSTANT');
}
public function generateAuthToken()
{
$url = env('FHPL_TOKEN_URL');
// x-www-form-urlencoded body
$postData = http_build_query([
'UserName' => env('FHPL_USER_NAME'),
'Password' => env('FHPL_PASSWORD'),
'grant_type' => env('FHPL_GRANT_TYPE'),
]);
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'GET', // SAME AS POSTMAN
CURLOPT_POSTFIELDS => $postData,
CURLOPT_HTTPHEADER => [
'Content-Type: application/x-www-form-urlencoded',
'Accept: application/json',
],
CURLOPT_TIMEOUT => 30,
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (curl_errno($ch)) {
return $this->response->setJSON([
'status' => false,
'error' => curl_error($ch),
]);
}
curl_close($ch);
return $this->response->setJSON([
'status' => $httpCode === 200,
'http_code' => $httpCode,
'data' => json_decode($response, true),
]);
}
public function SubmitClaim($claimId = null) // 515
{
helper('api');
$data = $this->db->table('ticket_master tm')
->select('
tm.id,
tm.emp_mobile as mobileNo,
tm.emp_mail as emailId,
tm.doa as admissionDate,
tm.dod as dischargeDate,
tm.hospital_name as hospitalName,
tm.claim_amount as requestedAmount,
tm.tpa_no as dependentUniqueId,
cp.policy_no as policyNo,
e.emp_code as memberId,
tn.note as disease,
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();
if (count($data) && $data['filePath'] == null) {
log_message('error', "FHPL - Claim Push FAILED | claimId: '.$claimId.' - Claim or File Missing");
return ['status' => false, 'message' => 'Claim Push FAILED | Claim or File Missing'];
}
// Build absolute file path
$filename = basename($data['filePath']);
$pdfPath = WRITEPATH . 'uploads/claim_files/' . $filename;
if (!file_exists($pdfPath)) {
log_message('error', "FHPL - Claim Push FAILED | claimId: '.$claimId.' - PDF not found on server");
return ['status' => false, 'message' => 'Claim Push FAILED | PDF not found on server'];
}
// Convert PDF to Base64
$fileContent = base64_encode(file_get_contents($pdfPath));
// Generate FHPL Token
$tokenResponse = json_decode($this->generateAuthToken()->getBody(), true);
if (empty($tokenResponse['data']['access_token'])) {
log_message('error', "FHPL - Claim Push FAILED | claimId: '.$claimId.' - FHPL Token generation failed");
return ['status' => false, 'message' => 'Claim Push FAILED | FHPL Token generation failed'];
}
$token = $tokenResponse['data']['access_token'];
// Build FHPL Request
$body = [
"IssueID" => $data['dependentUniqueId'], // this key added after seeing the error in responce have to click with fhpl team
"Userid" => getenv('FHPL_USER_NAME'),
"PolicyNo" => $data['policyNo'], // "111700-TATAMTORS",
"UhidNo" => $data['dependentUniqueId'], // "OIC40830846",
"ClaimID" => (string) $data['id'],
"DOA" => date('Y-m-d', strtotime($data['admissionDate'])), //"2025-10-11",
"DateofDischarge"=> $data['dischargeDate'] ? date('Y-m-d', strtotime($data['dischargeDate'])) : null,
"ClaimedAmount" => (float) $data['requestedAmount'],
"DocumentType" => 20, // Fresh Claim
"PayeeName" => $data['memberId'],
"HospitalName" => $data['hospitalName'],
"MobileNo" => $data['mobileNo'],
"Documents" => [
[
"documentName" => $filename,
"documentCategory" => "IRR",
"filecontent" => $fileContent
]
]
];
$url = getenv('FHPL_BASE_URL') . "/api/ClaimSubmission";
$headers = [
"Authorization: Bearer " . $token,
"Content-Type: application/json"
];
log_message('error', 'FHPL - Claim Push | claimId: '.$claimId.' | payload: '.json_encode($body));
$response = call_third_party_api($url, 'POST', $headers, $body);
log_message('error', 'FHPL - Claim Push RESPONSE | ' . json_encode($response));
if($response['status'] != true){
log_message('error', 'FHPL - Claim Push FAILED | claimId: '.$claimId.' | response: '.json_encode($response));
$this->db->table('ticket_master')
->where('id',$claimId)
->update([ 'tpa_push_response' => json_encode($response) ]);
return ['status' => false, 'message' => 'Claim Push FAILED | API call failed'];
}
if ($response['status'] === true && !empty($response['data'][0]['ClaimsInfo']))
{
$claimsInfo = json_decode($response['data'][0]['ClaimsInfo'], true);
if (!empty($claimsInfo[0]['ClaimID'])) {
$fhplClaimNo = $claimsInfo[0]['ClaimID'];
$this->db->table('ticket_master')
->where('id', $claimId)
->update([
'claim_number' => $fhplClaimNo,
'tpa_claim_id' => $fhplClaimNo,
'tpa_claim_push_reference_no' => $fhplClaimNo,
'updated_at' => date('Y-m-d H:i:s')
]);
log_message('error', 'FHPL - Claim Push SUCCESS | claimId: '.$claimId.' | claimNO: '.$fhplClaimNo);
return ['status' => true, 'message' => 'Claim Push SUCCESS', 'response' => $response];
}else {
log_message('error', 'FHPL - Claim Push API SUCCESS BUT claimsInfo EMPTY | response: '.json_encode($response));
return ['status' => false, 'message' => 'Claim Push API SUCCESS BUT claimsInfo EMPTY'];
}
}
return ['status' => false, 'message' => 'Claim Push API SUCCESS BUT claimsInfo EMPTY'];
// return $this->response->setJSON($response);
}
public function ClaimDetail($claimId = null) //515
{
helper('api');
$ticket = $this->db->table('ticket_master tm')
->select("tm.id, tm.tpa_claim_id as claimNo, cp.policy_no , cp.policy_start_date , cp.policy_end_date")
->join('client_policy cp','tm.client_policy_id=cp.id')
->where('tm.id',$claimId)
->get()->getRowArray();
if(!$ticket) return ['status'=>false,'message'=>'Invalid claim'];
// Generate FHPL Token
$tokenResponse = json_decode($this->generateAuthToken()->getBody(), true);
if (empty($tokenResponse['data']['access_token'])) {
return ['status' => false,'message' => 'FHPL Token generation failed'];
}
$token = $tokenResponse['data']['access_token'];
$url = getenv('FHPL_BASE_URL')."/api/GetTPA_ClaimsDetails";
$body = [
"UserName" => getenv('FHPL_USER_NAME'),
"Password" => getenv('FHPL_PASSWORD'),
"PolicyNumber" => $ticket['policy_no'], //"GHI-81-25-00087313-000",
"Fromdate" => $ticket['policy_start_date'],//"2025-04-26",
"Todate" => $ticket['policy_end_date'],//"2025-04-27",
];
$headers = ["Authorization: Bearer ".$token,"Content-Type: application/json"];
$response = call_third_party_api($url,'POST',$headers,$body);
// dd($response);
if (empty($response['data'][0])) {
log_message('error', 'FHPL - Claim status FAILED | for ticket ID: ' . $claimId.' | response: '.json_encode($response));
return ['status' => false,'message' => 'API call failed.','data' => $response ];
}
// Extract claim status
$claimData = $response['data'][0];
$tpa_claim_no = $claimData['CLAIM_ID'] ?? '';
$currentStatus = $claimData['CLAIM_STATUS'] ?? '';
$tpa_claim_type = $claimData['CLAIM_TYPE'] ?? '';
$tpa_ailments = $claimData['AILMENT'] ?? '';
$validStatuses = [
"In-Progress" => 5,
"Under Process" => 5,
"Query" => 4,
"Paid" => 11,
"Rejected" => 8,
"Approved" => 8,
"Required Information" => 4,
];
$updateArray = [
'tpa_claim_status' => $currentStatus,
'updated_at' => date('Y-m-d H:i:s'),
];
if (isset($validStatuses[$currentStatus])) {
$updateArray['claim_status_id'] = $validStatuses[$currentStatus];
}
if (!empty($tpa_claim_type)) {
$updateArray['tpa_claim_type'] = $tpa_claim_type;
}
if (!empty($tpa_ailments)) {
$updateArray['tpa_ailments'] = $tpa_ailments;
}
$this->db->table('ticket_master')->where('id',$claimId)->update($updateArray);
log_message('error', "FHPL - Claim status SUCCESS | Updated ticket ID $claimId with claim status: $currentStatus");
return ['status' => true,'message' => 'Claim status updated.','updated_status' => $currentStatus,'api_response' => $response];
}
public function ClaimStatusUpdate()
{
helper('api');
$tickets = $this->db->table('ticket_master tm')
->select("tm.id,tm.tpa_claim_id,cp.policy_no")
->join('client_policy cp','tm.client_policy_id=cp.id')
->where('tm.tpa_claim_push_reference_no IS NOT NULL')
->whereNotIn('tm.claim_status_id', [8, 11, 12, 13, 66, 22, 24, 47, 49, 32, 34, 53, 55, 42, 44, 58, 60])
->where('tm.is_active', 1)
->where('cp.tpa_id', $this->fhplTpaId)
->get()->getResultArray();
$count=0;
foreach($tickets as $t){
$this->ClaimDetail($t['id']);
$count++;
}
return $this->response->setJSON(['status'=>true,'updated'=>$count]);
}
public function EcardRequest($employeeId = null,$policyNo = null,$uhid = null)
{
helper('api');
// Generate FHPL Token
$tokenResponse = json_decode($this->generateAuthToken()->getBody(), true);
if (empty($tokenResponse['data']['access_token'])) {
log_message('error', 'FHPL - Ecard Request FAILED | employeeId: '.$employeeId.' | policyNo: '.$policyNo.' | Message: FHPL Token generation failed');
return null;
}
$token = $tokenResponse['data']['access_token'];
$url = getenv('FHPL_BASE_URL')."/api/GetEcard";
$body = [
"UserName" => getenv('FHPL_USER_NAME'),
"Password" => getenv('FHPL_PASSWORD'),
"PolicyNumber" => $policyNo, //'10/12/2025/16/17',
"EmployeeID" => $employeeId, //'101225',
];
$headers = ["Authorization: Bearer ".$token,"Content-Type: application/json"];
$response = call_third_party_api($url,'POST',$headers,$body);
// dd($response);
if (($response['status'] ?? false) !== true) {
log_message('error', 'FHPL - Ecard Request FAILED | response: ' . json_encode($response));
return null;
}
if (empty($response['data'][0])) {
log_message('error', 'FHPL - Ecard Request FAILED | Empty data | response: ' . json_encode($response));
return null;
}
$apiData = $response['data'][0];
if (($apiData['STATUS'] ?? '') === 'SUCCESS') {
$ecardUrl = $apiData['E_Card'] ?? '';
if (!empty($ecardUrl)) {
log_message(
'error',
'FHPL - Ecard Request PUSH SUCCESS | employeeId: ' . $employeeId .
' | policyNo: ' . $policyNo .
' | ecardUrl: ' . $ecardUrl
);
return $ecardUrl;
}
}
log_message('error', 'FHPL - Ecard Request FAILED | response: ' . json_encode($response));
return null;
}
public function FhplGetBenefDetails($requestData = null)
{
$function_calling_type = $requestData['return_type'] ?? 'job';
try {
helper('api');
$policyNo = $requestData['policy_no'] ?? null;
$client_policy_id = $requestData['client_policy_id'] ?? null;
if (empty($policyNo)) {
log_message('error', 'FHPL - TPA ID Pull | 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', 'FHPL - TPA ID Pull | 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', "FHPL - TPA ID Pull | called for policy_no: {$policyNo} , client_policy_id: {$client_policy_id}");
// Fetch file download dates
$batchFiles = $this->db->table('batch_files f')
->select("f.created_at")
->where('f.client_policy_id', $client_policy_id)
->where('f.insurer_or_tpa', 'tpa')
->where('f.actions', 'export')
->get()
->getResultArray();
if (empty($batchFiles)) {
log_message('error', 'FHPL - TPA ID Pull FAILED | batchFiles is empty for this tpa id pull request');
if($function_calling_type == "job"){
return ['status' => false, 'message' => 'batchFiles not found'];
}else{
return $this->respond(['status' => false, 'message' => 'batchFiles not found']);
}
}
// Generate FHPL Token
$tokenResponse = json_decode($this->generateAuthToken()->getBody(), true);
if (empty($tokenResponse['data']['access_token'])) {
log_message('error', 'FHPL - TPA ID Pull | FHPL Token generation failed');
if($function_calling_type == "job"){
return ['status' => false, 'message' => 'FHPL Token generation failed'];
}else{
return $this->respond(['status' => false, 'message' => 'FHPL Token generation failed']);
}
}
$token = $tokenResponse['data']['access_token'];
$url = getenv('FHPL_BASE_URL') . "/api/GetEnrollmentDetailsPolicy";
$headers = [
"Authorization: Bearer " . $token,
"Content-Type: application/json"
];
$startIndex = 0;
$range = 100;
$allMembers = [];
while (true) {
$body = [
"UserName" => getenv('FHPL_USER_NAME'),
"Password" => getenv('FHPL_PASSWORD'),
"PolicyNumber" => $policyNo,
"StartIndex" => $startIndex,
"Range" => $range
];
log_message('error', "FHPL - TPA ID Pull | API parems " . json_encode([$url, 'POST', $headers, $body]));
$response = call_third_party_api($url, 'POST', $headers, $body);
if (($response['status'] ?? false) !== true) {
log_message('error', 'FHPL - TPA ID Pull API FAILED | response: ' . json_encode($response));
break;
}
if (
!isset($response['data']) ||
!is_array($response['data']) ||
count($response['data']) === 0 ||
!array_is_list($response['data']) // PHP 8+ safe check
) {
// STOP when data is not a valid list
break;
}
$allMembers = array_merge($allMembers, $response['data']);
// Move to next page
$startIndex += $range;
}
if(empty($allMembers))
{
// 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', 'FHPL - TPA ID Pull API FAILED | API failed: Empty menber data for this pull request');
if($function_calling_type == "job"){
return ['status' => false, 'message' => 'API call failed', 'data' => 'Empty menber data for this pull request'];
}else{
return $this->respond(['status' => false, 'message' => 'API call failed', 'data' => 'Empty menber data for this pull request']);
}
}
// dd($allMembers);
//save API data as JSON for analysis
$json = json_encode($allMembers, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
// $filename = time() . '.json';
$filePath = WRITEPATH . 'tmp/'.time().'_'.$requestData['file_id'].'.json';
file_put_contents($filePath, $json);
//call a job for dump JSON data to DB
$job_details = new Jobs();
$r = Jobs::addJob(['job_name' => 'saveFhplAPIData', 'payload' => [ 'file_id' => $requestData['file_id'],'json_file_path' => $filePath ]]);
// ================= MATCHING LOGIC =================
$employeePolicyModel = new EmployeePolicyModel();
$employeePolicyData = $employeePolicyModel
->join('employees', 'employees.id = employee_polices.employee_id')
->where('employee_polices.client_policy_id', $client_policy_id)
->where('employee_polices.tpa_id IS NULL')
->findAll();
$updated = 0;
foreach ($employeePolicyData as $policy) {
$hasMatchForThisPolicy = false;
foreach ($allMembers as $m) {
if (
strtolower(trim($policy['name'])) === strtolower(trim($m['EMPLOYEE_NAME'] ?? '')) &&
($policy['emp_code'] ?? '') == ($m['EMPLOYEE_ID'] ?? '') &&
strtolower($policy['relationship']) === strtolower($m['RELATION'] ?? '')
) {
$hasMatchForThisPolicy = true;
$sql = "UPDATE employee_polices SET tpa_id = ? WHERE id = ?";
$this->db->query($sql, [$m['TPA_TPADETAIL_ID'], $policy['emp_policy_id']]);
// for e-card send
if(strtolower(trim($policy['relationship'])) == 'self'){
$employee_policy_ids[] = $policy['emp_policy_id'];
}
if ($this->db->affectedRows() > 0) {
$updated++;
log_message('error', "FHPL - TPA ID Pull Updated tpa_id={$m['TPA_TPADETAIL_ID']} for emp_policy_id={$policy['emp_policy_id']} policy={$policyNo}");
} else {
log_message('error', "FHPL - TPA ID Pull No update (already set or not matched) for emp_policy_id={$policy['emp_policy_id']} policy={$policyNo}");
}
}
}
}
// Handle NO MATCH for this policy
if (!$hasMatchForThisPolicy) {
$nhanceSideData = [
'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,
];
$batch_file_success = 'partially success';
log_message(
'error',
"FHPL - TPA ID Pull No match for Nhance = " . json_encode($nhanceSideData)
);
}
// send e-card
if(!empty($employee_policy_ids)){
log_message('error', "sendMailForDownloadingECard JOB PUSHED.");
Jobs::addJob(['job_name' => 'sendMailForDownloadingECard', 'payload' => ['ids' => $employee_policy_ids, 'client_policy_id' => $client_policy_id]]);
}
// 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', $batch_file_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.");
}
$totalCount = count($allMembers);
log_message('error', "FHPL - TPA ID Pull SUCCESS | 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 FhplGetBenefDetails($requestData = null)
// {
// helper('api');
// $policyNo = $requestData['policy_no'] ?? "10/12/2025/16/17";
// $client_policy_id = $requestData['client_policy_id'] ?? 0;
// // Generate FHPL Token
// $tokenResponse = json_decode($this->generateAuthToken()->getBody(), true);
// if (empty($tokenResponse['data']['access_token'])) {
// return $this->response->setJSON(['status' => false,'message' => 'FHPL Token generation failed']);
// }
// $token = $tokenResponse['data']['access_token'];
// $url = getenv('FHPL_BASE_URL')."/api/GetEnrollmentDetailsPolicy";
// $headers = [
// "Authorization: Bearer ".$token,
// "Content-Type: application/json"
// ];
// $startIndex = 0;
// $range = 100;
// $allMembers = [];
// do {
// $body = [
// "UserName" => getenv('FHPL_USER_NAME'),
// "Password" => getenv('FHPL_PASSWORD'),
// "PolicyNumber" => $policyNo,
// "StartIndex" => $startIndex,
// "Range" => $range
// ];
// $response = call_third_party_api($url,'POST',$headers,$body);
// dd($response);
// if(empty($response['data']['Members'])){
// break;
// }
// $allMembers = array_merge($allMembers,$response['data']['Members']);
// $startIndex += $range;
// } while($startIndex < ($response['data']['Total'] ?? 0));
// dd($allMembers);
// // Now same matching logic you already have
// $employeePolicyModel = new EmployeePolicyModel();
// $employeePolicyData = $employeePolicyModel
// ->join('employees','employees.id=employee_polices.employee_id')
// ->where('employee_polices.client_policy_id',$client_policy_id)
// ->where('employee_polices.tpa_id IS NULL')
// ->findAll();
// $updated = 0;
// foreach($employeePolicyData as $policy){
// foreach($allMembers as $m){
// if(
// strtolower(trim($policy['name']))==strtolower(trim($m['Name'])) &&
// $policy['emp_code']==$m['MemberID'] &&
// strtolower($policy['relationship'])==strtolower($m['Relation'])
// ){
// $this->db->table('employee_polices')
// ->where('id',$policy['emp_policy_id'])
// ->update(['tpa_id'=>$m['UHID']]);
// $updated++;
// }
// }
// }
// return [
// 'status'=>true,
// 'total_fetched'=>count($allMembers),
// 'updated'=>$updated
// ];
// }
public function syncFhplClaimsToNhanceOld()
{
helper('api');
// Generate FHPL Token
$tokenResponse = json_decode($this->generateAuthToken()->getBody(), true);
if (empty($tokenResponse['data']['access_token'])) {
return $this->response->setJSON(['status' => false,'message' => 'FHPL Token generation failed']);
}
$token = $tokenResponse['data']['access_token'];
$url = getenv('FHPL_BASE_URL')."/api/GetTPA_ClaimsDetails";
$headers = [
"Authorization: Bearer ".$token,
"Content-Type: application/json"
];
$policies = $this->db->table('client_policy')
->where('tpa_id',$this->fhplTpaId)
->get()->getResultArray();
$finalResult=[];
foreach($policies as $policy){
$body = [
"UserName" => getenv('FHPL_USER_NAME'),
"Password" => getenv('FHPL_PASSWORD'),
"PolicyNumber" => $policy['policy_no'],
"Fromdate" => $policy['policy_start_date'],
"Todate" => $policy['policy_end_date']
];
$response = call_third_party_api($url,'POST',$headers,$body);
if(!empty($response['data'])){
log_message('error', 'FHPL - Sync TPA Claims | Exception thrown while calling GetTPA_ClaimsDetails API: ' . json_encode($response));
$finalResult = array_merge($finalResult,$response['data']);
}
}
// Insert / update ticket_master same way you already do for MediAssist
foreach($finalResult as $row){
$status = $row['CLAIM_STATUS'];
$map = [
"Under Process"=>5,
"Paid"=>11,
"Rejected"=>8,
"Approved"=>8
];
$claimStatus = $map[$status] ?? 1;
$this->db->table('ticket_master')->insert([
'policy_no'=>$row['POLICY_NO'],
'claim_number'=>$row['CLAIM_ID'],
'tpa_claim_id'=>$row['CLAIM_ID'],
'emp_code'=>$row['EMPLOYEE_NO'],
'insured_name'=>$row['PATIENT_NAME'],
'claim_amount'=>$row['CLAIM_AMOUNT'],
'hospital_name'=>$row['HOSPITAL_NAME'],
'doa'=>$row['DATE_OF_ADMISSION'],
'dod'=>$row['DATE_OF_DISCHARGE'],
'claim_status_id'=>$claimStatus,
'tpa_id'=>$this->fhplTpaId
]);
}
return ['status'=>true,'total'=>count($finalResult)];
}
public function syncFhplClaimsToNhance()
{
helper('api');
try {
// Generate FHPL Token
$tokenResponse = json_decode($this->generateAuthToken()->getBody(), true);
if (empty($tokenResponse['data']['access_token'])) {
return $this->response->setJSON(['status' => false,'message' => 'FHPL Token generation failed']);
}
$token = $tokenResponse['data']['access_token'];
$url = getenv('FHPL_BASE_URL')."/api/GetTPA_ClaimsDetails";
$headers = [
"Authorization: Bearer ".$token,
"Content-Type: application/json"
];
$policies = $this->db->table('client_policy')
->where('tpa_id',$this->fhplTpaId)
->get()->getResultArray();
$finalResult=[];
foreach($policies as $policy){
$body = [
"UserName" => getenv('FHPL_USER_NAME'),
"Password" => getenv('FHPL_PASSWORD'),
"PolicyNumber" => $policy['policy_no'],
"Fromdate" => $policy['policy_start_date'],
"Todate" => $policy['policy_end_date']
];
$response = call_third_party_api($url,'POST',$headers,$body);
if(!empty($response['data'])){
log_message('error', 'FHPL - Sync TPA Claims | Exception thrown while calling GetTPA_ClaimsDetails API: ' . json_encode($response));
$finalResult = array_merge($finalResult,$response['data']);
}
}
$insertedCount = 0;
// Insert into ticket_master with mandatory columns (reference: MediAssist syncTpaClaimToNhance)
foreach($finalResult as $row){
$status = $row['CLAIM_STATUS'] ?? null;
$map = [
"Under Process"=>5,
"Paid"=>11,
"Rejected"=>8,
"Approved"=>8
];
$claimStatus = $map[$status] ?? 61;
// Derive relationship (default to self)
$relationship = map_relationship(trim($row['RELATION'] ?? 'self'));
// Fetch client policy details
$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', $row['POLICY_NO'] ?? null)
->orderBy('client_rm.id','DESC')
->get()
->getRowArray();
if (!$clientpolicy) {
log_message(
'error',
'FHPL - Sync TPA Claims | Client policy not found for policy_no: ' . ($row['POLICY_NO'] ?? 'N/A')
);
continue;
}
// Fetch employee / insured details
$employee = $this->db->table('employees e')
->select("
e.id as emp_id,
e.emp_code ,
e.name as emp_name,
e2.id as insured_emp_id,
e2.name as insured_emp_name,
ep.tpa_id as tpa_no
")
->join(
'employees e2',
"e2.emp_code = e.emp_code AND e2.relationship = ".$this->db->escape($relationship),
'left'
)
->join( 'employee_polices ep', "ep.employee_id = e2.id ", 'left' )
->where('e.emp_code', $row['EMPLOYEE_NO'] ?? null)
->where('e.client_id', $clientpolicy['client_id'] ?? null)
->where('e.relationship', 'self')
->where('e.is_active', 1)
->where('e2.is_active', 1)
->where('ep.is_active', 1)
->get()
->getRowArray();
if (!$employee) {
log_message(
'error',
'FHPL - Sync TPA Claims | Employee data not found for emp_code: ' . ($row['EMPLOYEE_NO'] ?? 'N/A') .
' | policy_no: ' . ($row['POLICY_NO'] ?? 'N/A') .
' | relationship: ' . $relationship
);
continue;
}
$claimData = [
// Core
'ticket_type_id' => 1,
'claim_status_id' => $claimStatus,
'policy_no' => $row['POLICY_NO'] ?? null,
'claim_number' => $row['CLAIM_ID'] ?? null,
'tpa_claim_id' => $row['CLAIM_ID'] ?? null,
// local primary ids
'tpa_id' => $clientpolicy['tpa_id'] ?? $this->fhplTpaId,
'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,
'tpa_no' => $employee['tpa_no'] ?? null,
'emp_code' => $row['EMPLOYEE_NO'] ?? null,
'emp_name' => $employee['emp_name'] ?? null,
'insured_name' => $row['insured_emp_name'] ?? null,
'relationship' => $relationship,
// Claim info
'claim_type' => 1,
'mode_of_intimation' => 5,
'claim_amount' => $row['CLAIM_AMOUNT'] ?? null,
// Dates
'doa' => change_date_format($row['DATE_OF_ADMISSION'] ?? '', null, 'Y-m-d') ?? null,
'dod' => change_date_format($row['DATE_OF_DISCHARGE'] ?? '', null, 'Y-m-d') ?? null,
// Hospital
'hospital_name' => $row['HOSPITAL_NAME'] ?? null,
'hospital_state' => $row['HOSPITAL_STATE'] ?? null,
'hospital_city' => $row['HOSPITAL_CITY'] ?? null,
'hospital_address' => $row['Hospital Address'] ?? null,
'hospital_pincode' => $row['Hospital Pincode'] ?? null,
'registration_date' => $row['CLAIM_REGISTERED_DATE'] ?? null,
// Others
'tpa_claim_type' => $row['CLAIM_TYPE'] ?? null,
'tpa_ailments' => $row['AILMENT'] ?? null,
'created_at' => date('Y-m-d H:i:s'),
];
$this->db->table('ticket_master')->insert($claimData);
$insertedCount++;
}
log_message('error', 'FHPL - Sync TPA Claims | Fetched Data | Inserted Data: ' . count($finalResult) . ' | Inserted: ' . $insertedCount);
return ['status'=>true,'total'=>count($finalResult), 'inserted'=>$insertedCount];
} 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,
];
log_message('error', 'FHPL - Sync TPA Claims | Exception thrown: ' . json_encode($errorData));
return ['status'=>false,'message'=>$th->getMessage()];
}
}
public function saveFhplAPIData($array)
{
$file_id = $array['file_id'];
$json = file_get_contents($array['json_file_path']);
$records = json_decode($json, true);
// log_message('error','FHPL - saveFhplAPIData' . json_encode($array));//die();
$file_model = new BatchFileModel();
$file_info = $file_model->where('id', $file_id)->find();
$tpaApiDataModel = new TpaApiDataModel();
//deactivate old data
$tpaApiDataModel->set('is_active', 0)->where('file_id', $file_id)->update();
//covert tpa data to our model data
$mappedRows = [];
foreach ($records as $row) {
$mappedRows[] = [
'file_id' => $file_id, // ← pass from controller
'emp_code' => trim($row['EMPLOYEE_ID'] ?? ''),
'name' => trim($row['EMPLOYEE_NAME'] ?? ''),
'dob' => !empty($row['DATE_OF_BIRTH'] ?? null) ? date('Y-m-d', strtotime(str_replace('/', '-', $row['DATE_OF_BIRTH']))) : null,
'relation' => trim(strtolower($row['RELATION'] ?? '')),
'gender' => format_gender_v2($row['GENDER'] ?? null),
'self' => strtolower($row['RELATION'] ?? '') === 'self' ? 1 : 0,
'tpa_id' => trim($row['TPA_TPADETAIL_ID'] ?? null),
'age' => is_numeric($row['AGE'] ?? null) ? (int) $row['AGE'] : null,
'is_active' => 1,
'created_by' => $file_info[0]['created_by'] ?? null,
];
}
// log_message('error','FHPL - COUNT' . count($mappedRows));
$result = $tpaApiDataModel->insertBatchWithChunkLog($mappedRows,500,('FILE_ID_'.$file_id));
// unlink($file_array['json_file_path']); // delete temp json file
return $result;
}
}