FIX_ISSUE_2

This commit is contained in:
VENKATESHWARAN 2026-02-01 22:18:12 +05:30
parent b9df99c032
commit e00e55c517
2 changed files with 149 additions and 147 deletions

View File

@ -1863,9 +1863,9 @@ class TicketServiceController extends BaseController
public function tpaClaimDumpImporter($params) public function tpaClaimDumpImporter($params)
{ {
try { $file_id = $params['file_id'] ?? null; // Move outside try to ensure catch can see it
$file_id = $params['file_id'] ?? null; try {
$file_path = WRITEPATH . 'uploads' . DIRECTORY_SEPARATOR . 'claim_dump_excel' . DIRECTORY_SEPARATOR; $file_path = WRITEPATH . 'uploads' . DIRECTORY_SEPARATOR . 'claim_dump_excel' . DIRECTORY_SEPARATOR;
if (!$file_id) { if (!$file_id) {
@ -1873,13 +1873,11 @@ class TicketServiceController extends BaseController
} }
$fileData = $this->claimDumpFileModel->find((int)$file_id); $fileData = $this->claimDumpFileModel->find((int)$file_id);
if (!$fileData) { if (!$fileData) {
return ['status' => false, 'message' => 'Invalid file ID. No file data found']; return ['status' => false, 'message' => 'Invalid file ID. No file data found'];
} }
$file_full_path = $file_path . $fileData['file_name']; $file_full_path = $file_path . $fileData['file_name'];
if (!is_file($file_full_path)) { if (!is_file($file_full_path)) {
return ['status' => false, 'message' => 'Claim dump file not found']; return ['status' => false, 'message' => 'Claim dump file not found'];
} }
@ -1888,106 +1886,96 @@ class TicketServiceController extends BaseController
$result = $handler->runTpaClaimDumpInsert($file_full_path, $file_id); $result = $handler->runTpaClaimDumpInsert($file_full_path, $file_id);
if (!empty($result['status']) && $result['status'] === true) { if (!empty($result['status']) && $result['status'] === true) {
// mark file as processing and queue next job
Jobs::addJob(['job_name' => 'tpaClaimDumpToTicketMasterImporters', 'payload' => ['file_id' => $file_id]]); Jobs::addJob(['job_name' => 'tpaClaimDumpToTicketMasterImporters', 'payload' => ['file_id' => $file_id]]);
} else { } else {
$errorMessage = $result['message'] ?? 'Unknown import error'; // FORCE FAIL LOGIC
$reason = [ $this->markAsFailed($file_id, $result['message'] ?? 'System error contact admin', $fileData['created_by'] ?? null);
'error_summary' => [5 => 1],
'error_data' => $errorMessage
];
$reasonJson = json_encode($reason, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
// mark the file as failed using the model
$updated = $this->claimDumpFileModel->update($file_id, [
'status' => 'failed',
'reason' => $reasonJson,
'updated_by' => $fileData['created_by'] ?? null
]);
if ($updated === false) {
$this->myLogger->logme('error', 'Claim dump file update to failed status failed for file_id: ' . $file_id);
} else {
$this->myLogger->logme('error', 'Claim dump file marked failed for file_id: ' . $file_id);
}
} }
return $result; return $result;
} catch (\Throwable $th) { } catch (\Throwable $th) {
$this->myLogger->logme("error", 'TPA_CLAIM_IMPORTER_JOB : ' . $th->getMessage());
$this->myLogger->logme(
"error",
'TPA_CLAIM_IMPORTER_JOB : ' .
$th->getMessage() . ' | Line: ' . $th->getLine()
);
// attempt to mark file as failed if file_id available
if (!empty($file_id)) { if (!empty($file_id)) {
$reason = json_encode(['error_summary' => [5 => 1], 'error_data' => 'TPA import crashed: ' . $th->getMessage()], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); $this->markAsFailed($file_id, 'System error contact admin');
$this->claimDumpFileModel->update($file_id, ['status' => 'failed', 'reason' => $reason]);
} }
return [ return [
'status' => false, 'status' => false,
'message' => 'TPA Claim dump import failed', 'message' => 'TPA Claim dump import failed',
'error_data' => [ 'error_data' => $th->getMessage()
'message' => $th->getMessage(),
'file' => $th->getFile(),
'line' => $th->getLine(),
'trace' => $th->getTraceAsString()
]
]; ];
} }
} }
private function markAsFailed($file_id, $message, $user_id = null)
{
$reason = json_encode([
'error_summary' => [5 => 1],
'error_data' => $message
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$data = [
'status' => 'failed',
'reason' => $reason,
'updated_by' => $user_id
];
// Using the direct update(id, data) method is often more reliable inside try/catch
return $this->claimDumpFileModel->update($file_id, $data);
}
public function tpaClaimDumpToTicketMasterImporters($params) public function tpaClaimDumpToTicketMasterImporters($params)
{ {
try {
$file_id = $params['file_id'] ?? null; $file_id = $params['file_id'] ?? null;
$fileData = null;
try {
if (!$file_id) {
return ['status' => false, 'message' => 'File ID is missing'];
}
$fileData = $this->claimDumpFileModel->where('id', $file_id)->first(); $fileData = $this->claimDumpFileModel->where('id', $file_id)->first();
if (!$fileData) { if (!$fileData) {
return ['status' => false, 'message' => 'Invalid file ID No file data found to import']; return ['status' => false, 'message' => 'Invalid file ID. No file data found to import'];
} }
$handler = TpaClaimsImportFactory::make($fileData['tpa_id'] ?? 0); $handler = TpaClaimsImportFactory::make($fileData['tpa_id'] ?? 0);
$result = $handler->runTicketMasterInsert($params); $result = $handler->runTicketMasterInsert($params);
if (!empty($result['status']) && $result['status'] === true) { if (!empty($result['status']) && $result['status'] === true) {
$updated = $this->claimDumpFileModel->where('id', $file_id)->set(['status' => 'success', 'reason' => null])->update(); // Success: Update the status to success
$this->claimDumpFileModel->update($file_id, [
'status' => 'success',
'reason' => null
]);
} else { } else {
// Logic failure: The runTicketMasterInsert returned status false
$errorMessage = $result['message'] ?? 'Unknown import error C'; $this->markAsFailed(
$reason = [ $file_id,
'error_summary' => [5 => 1], // 🔒 always fixed $result['message'] ?? 'System error contact admin',
'error_data' => $errorMessage $fileData['created_by'] ?? null
]; );
$reason = json_encode($reason);
$updated = $this->claimDumpFileModel->where('id', $file_id)->set(['status' => 'failed', 'reason' => $reason])->update();
} }
return $result; return $result;
} catch (\Throwable $th) { } catch (\Throwable $th) {
// Log the full error
$this->myLogger->logme("error", 'TICKET_MASTER_CLAIM_IMPORTER_JOB :' . $th->getMessage() . ' at line ' . $th->getLine());
$this->myLogger->logme("error", 'TICKET_MASTER_CLAIM_IMPORTER_JOB :' .($th->getMessage() . ' --- ' . $th->getLine() . '----' . $th->getTraceAsString())); // CRITICAL: Even if the code crashes, try to mark the file as failed
if ($file_id) {
$errorData = [ $this->markAsFailed($file_id, 'Fatal Crash: ' . $th->getMessage(), $fileData['created_by'] ?? null);
'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,
];
return [ return [
'status' => false, 'status' => false,
'message' => $th->getMessage(), 'message' => $th->getMessage(),
'error_data' => json_encode($errorData, JSON_PRETTY_PRINT) 'error_data' => [
'line' => $th->getLine(),
'file' => $th->getFile()
]
]; ];
} }
} }

View File

@ -27,55 +27,49 @@ abstract class BaseTpaClaimImportService
*/ */
public function runTpaClaimDumpInsert(string $filePath, int $fileId): array public function runTpaClaimDumpInsert(string $filePath, int $fileId): array
{ {
$this->db->transStart(); // 1. Start Transaction
$this->db->transBegin();
try {
$fileData = $this->claimDumpFileModel->where('id', $fileId)->first(); $fileData = $this->claimDumpFileModel->where('id', $fileId)->first();
// Determine sheet name logic...
if (env('FHPL_PRIMARY_KEY_CONSTANT') == $fileData['tpa_id']) { if (env('FHPL_PRIMARY_KEY_CONSTANT') == $fileData['tpa_id']) {
$rows = $this->readExcelBySheetName($filePath, 'Claims&Preauth'); $rows = $this->readExcelBySheetName($filePath, 'Claims&Preauth');
} else if (env('R_CARE_PRIMARY_KEY_CONSTANT') == $fileData['tpa_id']) { } else if (env('R_CARE_PRIMARY_KEY_CONSTANT') == $fileData['tpa_id']) {
$rows = $this->readExcelBySheetName($filePath, 'CL'); $rows = $this->readExcelBySheetName($filePath, 'CL');
// $AL = $this->readExcelBySheetName($filePath, 'AL');
// $rows = array_merge($CL, $AL);
} else { } else {
$rows = $this->readExcel($filePath); $rows = $this->readExcel($filePath);
} }
// dd($rows);
if (empty($rows)) { if (empty($rows)) {
$this->db->transRollback(); // ROLLBACK BEFORE RETURN
return ['status' => false, 'message' => 'Excel file contains no data or wrong file upload']; return ['status' => false, 'message' => 'Excel file contains no data or wrong file upload'];
} }
$tpaInsertData = $this->mapTPAData($rows, $fileId); $tpaInsertData = $this->mapTPAData($rows, $fileId);
// dd($tpaInsertData);
if (empty($tpaInsertData)) { if (empty($tpaInsertData)) {
return ['status' => false, 'message' => 'These records already exist in the system. Please check the file']; $this->db->transRollback(); // ROLLBACK BEFORE RETURN
return ['status' => false, 'message' => 'These records already exist in the system.'];
} }
$return_res = $this->bulkInsertTPATable($tpaInsertData); $return_res = $this->bulkInsertTPATable($tpaInsertData);
if ($return_res !== true) { if ($return_res !== true) {
$this->db->transRollback(); // ROLLBACK BEFORE RETURN
return ['status' => false, 'message' => 'TPA Import bulk insert failed']; return ['status' => false, 'message' => 'TPA Import bulk insert failed'];
} }
$this->db->transComplete(); // 2. Commit if everything is fine
$this->db->transCommit();
return ['status' => true, 'message' => 'File uploaded successfully', 'record_count' => count($tpaInsertData)];
if ($this->db->transStatus() === false) { } catch (\Throwable $e) {
// 3. Rollback on any crash/exception
$error = $this->db->error(); $this->db->transRollback();
$error_data = [ return ['status' => false, 'message' => 'System error : ' . $e->getMessage()];
'message' => $error['message'] ?: 'Unknown DB error',
'code' => $error['code'] ?? null,
'last_query' => (string) $this->db->getLastQuery()
];
// dd($error_data);
unset($error_data['last_query']);
return ['status' => false, 'message' => 'TPA Import transaction failed', 'error_data' => $error_data];
} }
return ['status' => true, 'message' => 'File uploaded successfully', 'record_count' => count($tpaInsertData ?? [])];
} }
/** /**
@ -83,45 +77,65 @@ abstract class BaseTpaClaimImportService
*/ */
public function runTicketMasterInsert(array $params): array public function runTicketMasterInsert(array $params): array
{ {
$this->db->transStart(); // 1. Start manual transaction
$this->db->transBegin();
try {
$file_id = $params['file_id']; $file_id = $params['file_id'];
$ticketMasterData = $this->mapClaimMasterData($file_id); $ticketMasterData = $this->mapClaimMasterData($file_id);
// dd($ticketMasterData);
// Check if mapping failed
if (!$ticketMasterData['status']) { if (!$ticketMasterData['status']) {
$this->db->transRollback(); // ALWAYS rollback before early return
return $ticketMasterData; return $ticketMasterData;
} }
$message = ''; $message = '';
$return_res = false; $hasExecutedTask = false;
if(!empty($ticketMasterData['mapped_array'])){
$return_res = $this->importClaimMaster($ticketMasterData['mapped_array']); // Process Mapped Data
if (!empty($ticketMasterData['mapped_array'])) {
$insert_res = $this->importClaimMaster($ticketMasterData['mapped_array']);
if (!$insert_res) {
$this->db->transRollback();
return ['status' => false, 'message' => 'Ticket Master Claim bulk insert failed'];
}
$message .= 'Ticket Master Claim bulk insert success. '; $message .= 'Ticket Master Claim bulk insert success. ';
$hasExecutedTask = true;
} }
if(!empty($ticketMasterData['rejected_reason_array'])){ // Process Rejected Reasons
$return_res = $this->updateTicketMasterRejectedReasonInTPATable($ticketMasterData['rejected_reason_array']); if (!empty($ticketMasterData['rejected_reason_array'])) {
if(empty($ticketMasterData['mapped_array'])){ $update_res = $this->updateTicketMasterRejectedReasonInTPATable($ticketMasterData['rejected_reason_array']);
$message .= 'Those employees not in our system. '; if (!$update_res) {
}else{ $this->db->transRollback();
$message .= 'Ticket Master Claim rejected reason updated successfully. '; return ['status' => false, 'message' => 'Updating rejected reasons failed'];
}
} }
if(!$return_res){ $message .= empty($ticketMasterData['mapped_array'])
return ['status' => false, 'message' => 'Ticket Master Claim bulk insert failed']; ? 'Those employees not in our system. '
: 'Ticket Master Claim rejected reason updated successfully. ';
$hasExecutedTask = true;
} }
$this->db->transComplete(); // If nothing was processed but no error occurred
if (!$hasExecutedTask) {
if ($this->db->transStatus() === false) { $this->db->transRollback();
return ['status' => false, 'message' => 'Ticket Master Claim bulk insert failed']; return ['status' => false, 'message' => 'No data found to process.'];
} }
return ['status' => true, 'message' => $message]; // 2. Commit the transaction
$this->db->transCommit();
return ['status' => true, 'message' => trim($message)];
} catch (\Throwable $th) {
// 3. Rollback on crash
$this->db->transRollback();
return [
'status' => false,
'message' => 'System error during Ticket Master Insert: ' . $th->getMessage()
];
}
} }
/** /**