FIX_MEDIASSIT_TPA_API

This commit is contained in:
velz 2026-04-30 15:29:46 +05:30
parent a06b74da6a
commit 9912aa747f
3 changed files with 435 additions and 6 deletions

View File

@ -992,6 +992,7 @@ class FhplApiController extends BaseController
'claim_created_by' => 'TPA',
'created_at' => date('Y-m-d H:i:s'),
'tpa_claim_push_reference_no' => $row['ClaimID'] ?? null,
];
$this->db->table('ticket_master')->insert($claimData);

View File

@ -1028,6 +1028,7 @@ class HealthIndiaApiController extends BaseController
'claim_created_by' => 'TPA',
'created_at' => date('Y-m-d H:i:s'),
'tpa_claim_push_reference_no' => $row['ccn'].'-'.$row['ccN_EXT'] ?? null,
];
$this->db->table('ticket_master')->insert($claimData);

View File

@ -503,7 +503,7 @@ class MediAssistApiController extends BaseController
}
}
public function ClaimDetail($claimId = null) // 585 this id for test
public function ClaimDetailOLD($claimId = null) // 585 this id for test
{
helper('api');
@ -523,7 +523,7 @@ class MediAssistApiController extends BaseController
tm.tpa_no as memberId,
tm.tpa_claim_push_reference_no as claimRefNo,
tm.tpa_claim_id,tm.doa,
tm.claim_number,
tm.claim_number,tm.claim_amount,
cp.policy_no as policyNo,
cp.policy_start_date as startDate,
cp.policy_end_date as endDate,
@ -661,7 +661,8 @@ class MediAssistApiController extends BaseController
}
if($ticket['doa'] == $this->mediDate($claimData['datE_OF_ADMISSION'] ?? null) || $ticket['claim_number'] == $tpa_claim_no){
if((isset($ticket['claim_number']) && $ticket['claim_number'] == $tpa_claim_no) || ($ticket['doa'] == $this->mediDate($claimData['datE_OF_ADMISSION'] ?? null) && $ticket['claimRefNo'] == $claimData['clM_COMP_REFNO']))
{
// UPDATE ticket_master
$this->db->table('ticket_master')->where('id', $claimId)->update($updateArray);
@ -674,6 +675,210 @@ class MediAssistApiController extends BaseController
}
return ['status' => true,'message' => 'Claim Status updated.','updated_status' => $currentStatus,'api_response' => $response];
}
public function ClaimDetail($claimId = null) // 585 this id for test
{
helper('api');
$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'),
];
// Fetch ticket master details
$ticket = $this->db->table('ticket_master tm')
->select("
tm.id,
tm.tpa_no as memberId,
tm.tpa_claim_push_reference_no as claimRefNo,
tm.tpa_claim_id,tm.doa,
tm.claim_number,tm.claim_amount,
cp.policy_no as policyNo,
cp.policy_start_date as startDate,
cp.policy_end_date as endDate,
e.emp_code as employeeCode
")
->join('employees e', 'e.id = tm.emp_id', 'left')
->join('client_policy cp', 'tm.client_policy_id = cp.id', 'left')
->where('tm.id', $claimId)
->get()
->getRowArray();
// print_rr($ticket);die;
if (!$ticket) {
return ['status' => false,'message' => 'Invalid Claim ID' ];
}
// REQUEST BODY
if($ticket['claimRefNo'] != null)
{
$body = [
"policyNo" => $ticket['policyNo'] ?? "",
"startDate" => "",
"endDate" => "",
"employeeCode" => $ticket['employeeCode'] ?? "",
"memberID" => "",
"claimNo" => "",
"claimRefNo" => $ticket['claimRefNo'] ?? "",
];
}else{
$body = [
"policyNo" => $ticket['policyNo'] ?? "",
"startDate" => "",
"endDate" => "",
"employeeCode" => $ticket['employeeCode'] ?? "",
"memberID" => "",
"claimNo" => "",
"claimRefNo" => "",
];
}
// $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);
if ($response['status'] != true || empty($response['data']['claimsData'][0])) {
log_message('error','MEDI_ASSIST - Claim Status FAILED | for ticket ID: ' . $claimId.' | response: '.json_encode($response));
return ['status' => false,'message' => 'API call failed.','data' => $response ];
}
// Extract Claim Status
$allClaimData = $response['data']['claimsData'];
$matched = false; // 🔥 Track if anything matched
foreach ($allClaimData as $claimData) {
$currentStatus = $claimData['claim_Current_Status'] ?? '';
$tpa_claim_no = $claimData['tpA_CLAIM_NO'] ?? '';
$tpa_claim_type = $claimData['typE_OF_CLAIM'] ?? '';
$tpa_ailments = ($claimData['ailment'] ?? '') . ' - ' . ($claimData['ailmenT_DESC'] ?? '');
$claimRefNo = $claimData['clM_COMP_REFNO'] ?? '';
$doa = $this->mediDate($claimData['datE_OF_ADMISSION'] ?? null);
$isMatch = false;
// -------------------------------
// ✅ Matching Logic
// -------------------------------
// Case 1: Claim number exists → match by claim number
if (!empty($ticket['claim_number'])) {
if ($ticket['claim_number'] == $tpa_claim_no) {
$isMatch = true;
}
}
// Case 2: No claim number → match by DOA + claimRefNo
else {
if (
$ticket['claimRefNo'] == $claimRefNo &&
$ticket['doa'] == $doa
) {
$isMatch = true;
}
}
if (!$isMatch) {
continue;
}
// ✅ Mark matched
$matched = true;
// -------------------------------
// ✅ Prepare update
// -------------------------------
$updateArray = [
'tpa_claim_status' => $currentStatus,
'updated_at' => date('Y-m-d H:i:s'),
'last_updated_by' => 'API',
];
if (isset($validStatuses[$currentStatus])) {
$updateArray['claim_status_id'] = $validStatuses[$currentStatus];
}
// Set claim number when first received
if (empty($ticket['claim_number']) && !empty($tpa_claim_no)) {
$updateArray['claim_number'] = $tpa_claim_no;
$updateArray['tpa_claim_id'] = $tpa_claim_no;
}
if (!empty($tpa_claim_type)) {
$updateArray['tpa_claim_type'] = $tpa_claim_type;
}
if (!empty($tpa_ailments)) {
$updateArray['tpa_ailments'] = $tpa_ailments;
}
// -------------------------------
// ✅ Update DB
// -------------------------------
$this->db->table('ticket_master')
->where('id', $claimId)
->update($updateArray);
log_message(
'error',
"MEDI_ASSIST Claim Status SUCCESS | Updated ticket ID ={$claimId} | Status={$currentStatus} | ClaimNo={$tpa_claim_no} | RefNo={$claimRefNo}"
);
// Stop after first valid match
break;
}
if ($matched) {
return [
'status' => true,
'message' => 'Claim Status updated.',
'updated_status' => $currentStatus,
'api_response' => $response
];
}
// -----------------------------------
// ❌ If nothing matched → log failure
// -----------------------------------
if (!$matched) {
log_message(
'error',
"MEDI_ASSIST | Fetch Claim Status | Claim Status NOT UPDATED | | TicketID={$claimId} | TicketDOA={$ticket['doa']} | ClaimDOA={$claimData['datE_OF_ADMISSION']} | Status={$currentStatus}"
);
return [
'status' => true,
'message' => 'No matching claim found.',
'updated_status' => null,
'api_response' => $response
];
}
}
public function IRSubmission($claimId = null) // 585 this id for test
@ -860,7 +1065,7 @@ class MediAssistApiController extends BaseController
// unlink($file_array['json_file_path']); // delete temp json file
}
public function ClaimStatusUpdate()
public function ClaimStatusUpdateOLD()
{
helper('api');
@ -879,7 +1084,7 @@ class MediAssistApiController extends BaseController
tm.id,
tm.tpa_no as memberId,
tm.tpa_claim_push_reference_no as claimRefNo,
tm.doa,tm.claim_no,
tm.doa,tm.claim_number,
cp.policy_no as policyNo,
cp.policy_start_date as startDate,
cp.policy_end_date as endDate,
@ -1026,7 +1231,8 @@ class MediAssistApiController extends BaseController
$updateArray['tpa_ailments'] = $tpa_ailments;
}
if($ticket['doa'] == $this->mediDate($claimData['datE_OF_ADMISSION'] ?? null) || $ticket['claim_number'] == $tpa_claim_no){
if((isset($ticket['claim_number']) && $ticket['claim_number'] == $tpa_claim_no) || ($ticket['doa'] == $this->mediDate($claimData['datE_OF_ADMISSION'] ?? null) && $ticket['claimRefNo'] == $claimData['clM_COMP_REFNO']))
{
// UPDATE ticket_master
$this->db->table('ticket_master')->where('id', $claimId)->update($updateArray);
$status_updated_count ++;
@ -1051,6 +1257,227 @@ class MediAssistApiController extends BaseController
]);
}
public function ClaimStatusUpdate()
{
helper('api');
$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'),
];
$TicketData = $this->db->table('ticket_master tm')
->select("
tm.id,
tm.tpa_no as memberId,
tm.tpa_claim_push_reference_no as claimRefNo,
tm.doa,
tm.claim_number,
tm.tpa_claim_id,
cp.policy_no as policyNo,
cp.policy_start_date as startDate,
cp.policy_end_date as endDate,
e.emp_code as employeeCode
")
->join('employees e', 'e.id = tm.emp_id', 'left')
->join('client_policy cp', 'tm.client_policy_id = cp.id', 'left')
->where('tm.tpa_claim_push_reference_no IS NOT NULL')
->where('tm.is_active', 1)
->whereNotIn('tm.claim_status_id', [8, 11, 12, 13, 66, 22, 24, 47, 49, 32, 34, 53, 55, 42, 44, 58, 60])
->where('tm.tpa_id', $this->mediAssistTpaId)
->get()
->getResultArray();
log_message('error','MEDI_ASSIST - Claim Status started | for ticket count: ' . count($TicketData));
if (!$TicketData) {
log_message('error',"MEDI_ASSIST - Claims not found to update Claim Status");
return $this->response->setJSON([
'status' => false,
'message' => 'Claims not found'
]);
}
$error_data = [];
$status_updated_count = 0;
// ✅ Move outside loop (performance)
$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" => 7,
"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,
"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,
];
foreach ($TicketData as $ticket) {
$claimId = $ticket['id'];
$matched = false;
$body = [
"policyNo" => $ticket['policyNo'] ?? "",
"startDate" => "",
"endDate" => "",
"employeeCode" => $ticket['employeeCode'] ?? "",
"memberID" => "",
"claimNo" => "",
"claimRefNo" => $ticket['claimRefNo'] ?? "",
];
$response = call_third_party_api($url, $method, $headers, $body);
log_message('error','MEDI_ASSIST - 2 hours Claim Status API Response: ' . json_encode($response));
if ($response['status'] != true || empty($response['data']['claimsData'])) {
log_message('error','MEDI_ASSIST - Claim Status FAILED | for ticket ID: ' . $claimId.' | response: '.json_encode($response));
$error_data[$claimId] = [
'response' => $response,
'body' => $body
];
continue;
}
$allClaimData = $response['data']['claimsData'];
foreach ($allClaimData as $claimData) {
$currentStatus = $claimData['claim_Current_Status'] ?? '';
$tpa_claim_no = $claimData['tpA_CLAIM_NO'] ?? '';
$tpa_claim_type = $claimData['typE_OF_CLAIM'] ?? '';
$tpa_ailments = ($claimData['ailment'] ?? '') . ' - ' . ($claimData['ailmenT_DESC'] ?? '');
$claimRefNo = $claimData['clM_COMP_REFNO'] ?? '';
// ✅ Normalize dates (critical fix)
$claimDOA = !empty($claimData['datE_OF_ADMISSION'])
? date('Y-m-d', strtotime($claimData['datE_OF_ADMISSION']))
: null;
$ticketDOA = !empty($ticket['doa'])
? date('Y-m-d', strtotime($ticket['doa']))
: null;
$isMatch = false;
// ================================
// ✅ STRICT MATCHING (NO LOOPHOLE)
// ================================
// Case 1: claim number exists → ONLY match by claim number
if (!empty($ticket['claim_number'])) {
if ($ticket['claim_number'] == $tpa_claim_no) {
$isMatch = true;
}
}
// Case 2: claim number empty → match by DOA + claimRefNo ONLY
else {
if (
!empty($ticket['claimRefNo']) &&
$ticket['claimRefNo'] == $claimRefNo &&
$ticketDOA == $claimDOA
) {
$isMatch = true;
}
}
// ❌ No fallback allowed
if (!$isMatch) {
continue;
}
// ================================
// ✅ MATCH FOUND
// ================================
$matched = true;
$updateArray = [
'tpa_claim_status' => $currentStatus,
'updated_at' => date('Y-m-d H:i:s'),
'last_updated_by' => 'API',
];
if (isset($validStatuses[$currentStatus])) {
$updateArray['claim_status_id'] = $validStatuses[$currentStatus];
}
// Set claim number only if empty
if (empty($ticket['claim_number']) && !empty($tpa_claim_no)) {
$updateArray['claim_number'] = $tpa_claim_no;
$updateArray['tpa_claim_id'] = $tpa_claim_no;
}
if (!empty($tpa_claim_type)) {
$updateArray['tpa_claim_type'] = $tpa_claim_type;
}
if (!empty($tpa_ailments)) {
$updateArray['tpa_ailments'] = $tpa_ailments;
}
// UPDATE DB
$this->db->table('ticket_master')
->where('id', $claimId)
->update($updateArray);
$status_updated_count++;
log_message('error',"MEDI_ASSIST - Claim Status SUCCESS | Updated ticket ID $claimId with Claim Status: $currentStatus");
break; // ✅ only inner loop
}
// ================================
// ❌ UNMATCHED LOG (once per ticket)
// ================================
if (!$matched) {
log_message('error', "MEDI_ASSIST | 2 hours | Claim Status NOT UPDATED | TicketID={$claimId} | TicketDOA={$ticket['doa']} | ClaimRefNo={$ticket['claimRefNo']}");
}
}
log_message('error',"MEDI_ASSIST - Claim Status ENDED | 2 hours | Total Tickets={".count($TicketData)."} | Status Updated={$status_updated_count} | Errors={".count($error_data)."}");
return $this->response->setJSON([
'status' => true,
'message' => 'Claim Status updated.',
'count' => $status_updated_count,
'error_data' => $error_data,
]);
}
public function syncTpaClaimToNhance()
{
helper(['api', 'date']);