512 lines
17 KiB
PHP
512 lines
17 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 = 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', "TPA CLAIM PUSH FAILED FHPL | claimId: '.$claimId.' - Claim or File Missing");
|
|
return $this->response->setJSON(['status' => false,'message' => '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', "TPA CLAIM PUSH FAILED FHPL | claimId: '.$claimId.' - PDF not found on server");
|
|
return $this->response->setJSON(['status' => false,'message' => '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'])) {
|
|
return $this->response->setJSON(['status' => false,'message' => '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" => "111700-TATAMTORS", // $data['policyNo'],
|
|
"UhidNo" => "OIC40830846", //$data['dependentUniqueId'],
|
|
"ClaimID" => (string) $data['id'],
|
|
"DOA" => "2025-10-11",//date('Y-m-d', strtotime($data['admissionDate'])),
|
|
"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', 'TPA CLAIM PUSH FHPL | claimId: '.$claimId.' | payload: '.json_encode($body));
|
|
|
|
$response = call_third_party_api($url, 'POST', $headers, $body);
|
|
|
|
log_message('error', 'FHPL RESPONSE | ' . json_encode($response));
|
|
|
|
if($response['status'] != true){
|
|
log_message('error', 'TPA CLAIM PUSH FAILED FHPL | claimId: '.$claimId.' | response: '.json_encode($response));
|
|
$this->db->table('ticket_master')
|
|
->where('id',$claimId)
|
|
->update([ 'tpa_push_response' => json_encode($response) ]);
|
|
return;
|
|
}
|
|
|
|
|
|
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', 'TPA CLAIM PUSH SUCCESS FHPL | claimId: '.$claimId.' | claimNO: '.$fhplClaimNo);
|
|
|
|
}
|
|
}
|
|
|
|
|
|
return $this->response->setJSON($response);
|
|
}
|
|
|
|
public function ClaimDetail($claimId = 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 $this->response->setJSON(['status'=>false,'message'=>'Invalid claim']);
|
|
|
|
// 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";
|
|
|
|
$body = [
|
|
"UserName" => getenv('FHPL_USER_NAME'),
|
|
"Password" => getenv('FHPL_PASSWORD'),
|
|
"PolicyNumber" => "GHI-81-25-00087313-000",//$ticket['policy_no'],
|
|
"Fromdate" => "2025-04-26",//$ticket['claimNo'],
|
|
"Todate" => "2025-04-27",//$ticket['claimNo'],
|
|
];
|
|
|
|
$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', 'CLAIM STATUS FAILED | for ticket ID: ' . $claimId.' | response: '.json_encode($response));
|
|
|
|
return $this->response->setJSON([
|
|
'status' => false,
|
|
'message' => 'API call failed.',
|
|
'data' => $response
|
|
]);
|
|
}
|
|
|
|
|
|
$status = null;
|
|
|
|
// find this claim
|
|
foreach($response['data'] as $row){
|
|
if($row['CLAIM_ID']==$ticket['claimNo']){
|
|
$status = $row['CLAIM_STATUS'];
|
|
}
|
|
}
|
|
|
|
$map = [
|
|
"In-Progress" => 5,
|
|
"Under Process" => 5,
|
|
"Query" => 4,
|
|
"Paid" => 11,
|
|
"Rejected" => 8,
|
|
"Approved" => 8,
|
|
"Required Information" => 4,
|
|
];
|
|
|
|
if( $status != null && isset($map[$status]))
|
|
{
|
|
$this->db->table('ticket_master')->where('id',$claimId)->update(['claim_status_id'=>$map[$status],'tpa_claim_status'=>$status]);
|
|
// LOG UPDATE
|
|
log_message('error', "CLAIM STATUS SUCCESS | Updated ticket ID $claimId with claim status: $status");
|
|
}
|
|
|
|
|
|
|
|
return $this->response->setJSON([
|
|
'status' => true,
|
|
'message' => 'Claim status updated.',
|
|
'updated_status' => $status,
|
|
'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_id IS NOT NULL')
|
|
->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,$policyNo,$uhid)
|
|
{
|
|
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']);
|
|
}
|
|
|
|
// 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/GetEcard";
|
|
|
|
$body = [
|
|
"UserName" => getenv('FHPL_USER_NAME'),
|
|
"Password" => getenv('FHPL_PASSWORD'),
|
|
"PolicyNumber" => $policyNo,
|
|
"EmployeeID" => $employeeId
|
|
];
|
|
|
|
$headers = ["Authorization: Bearer ".$token,"Content-Type: application/json"];
|
|
|
|
$response = call_third_party_api($url,'POST',$headers,$body);
|
|
|
|
if(($response['data']['STATUS'] ?? '')=='SUCCESS'){
|
|
return $response['data']['E_Card'];
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
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 syncFhplClaimsToNhance()
|
|
{
|
|
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'])){
|
|
$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)];
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|