diff --git a/app/Http/Controllers/AtmTransactionReportController.php b/app/Http/Controllers/AtmTransactionReportController.php index 330c820..fd68075 100644 --- a/app/Http/Controllers/AtmTransactionReportController.php +++ b/app/Http/Controllers/AtmTransactionReportController.php @@ -210,11 +210,17 @@ class AtmTransactionReportController extends Controller // Get the data for the current page $data = $query->get()->map(function ($item) { + $processingHours = $item->status === 'processing' ? $item->updated_at->diffInHours(now()) : 0; + $isProcessingTimeout = $item->status === 'processing' && $processingHours >= 1; + return [ 'id' => $item->id, 'period' => $item->period, 'report_date' => Carbon::createFromFormat('Ymd', $item->period)->format('Y-m-d'), 'status' => $item->status, + 'status_display' => $item->status . ($isProcessingTimeout ? ' (Timeout)' : ''), + 'processing_hours' => $processingHours, + 'is_processing_timeout' => $isProcessingTimeout, 'authorization_status' => $item->authorization_status, 'is_downloaded' => $item->is_downloaded, 'created_at' => dateFormat($item->created_at, 1, 1), @@ -222,6 +228,7 @@ class AtmTransactionReportController extends Controller 'authorized_by' => $item->authorizer ? $item->authorizer->name : null, 'authorized_at' => $item->authorized_at ? $item->authorized_at->format('Y-m-d H:i:s') : null, 'file_path' => $item->file_path, + 'can_retry' => in_array($item->status, ['failed', 'pending']) || $isProcessingTimeout || ($item->status === 'completed' && !$item->file_path), ]; }); @@ -298,13 +305,26 @@ class AtmTransactionReportController extends Controller */ public function retry(AtmTransactionReportLog $atmReport) { - // Check if retry is allowed (only for failed or pending status) - if (!in_array($atmReport->status, ['failed', 'pending'])) { - return back()->with('error', 'Report can only be retried if status is failed or pending.'); + // Check if retry is allowed (failed, pending, or processing for more than 1 hour) + $allowedStatuses = ['failed', 'pending']; + $isProcessingTooLong = $atmReport->status === 'processing' && + $atmReport->updated_at->diffInHours(now()) >= 1; + + if (!in_array($atmReport->status, $allowedStatuses) && !$isProcessingTooLong) { + return back()->with('error', 'Report can only be retried if status is failed, pending, or processing for more than 1 hour.'); } try { - // Reset the report status and clear error message + // If it was processing for too long, mark it as failed first + if ($isProcessingTooLong) { + $atmReport->update([ + 'status' => 'failed', + 'error_message' => 'Processing timeout - exceeded 1 hour limit', + 'updated_by' => Auth::id() + ]); + } + + // Reset the report status and clear previous data $atmReport->update([ 'status' => 'processing', 'error_message' => null, @@ -329,4 +349,18 @@ class AtmTransactionReportController extends Controller return back()->with('error', 'Failed to retry report generation: ' . $e->getMessage()); } } + + /** + * Check if report can be retried + */ + public function canRetry(AtmTransactionReportLog $atmReport) + { + $allowedStatuses = ['failed', 'pending']; + $isProcessingTooLong = $atmReport->status === 'processing' && + $atmReport->updated_at->diffInHours(now()) >= 1; + + return in_array($atmReport->status, $allowedStatuses) || + $isProcessingTooLong || + ($atmReport->status === 'completed' && !$atmReport->file_path); + } } diff --git a/resources/views/atm-reports/index.blade.php b/resources/views/atm-reports/index.blade.php index 9b3dbab..f49cf02 100644 --- a/resources/views/atm-reports/index.blade.php +++ b/resources/views/atm-reports/index.blade.php @@ -133,7 +133,7 @@ type: 'DELETE' }).then((response) => { swal.fire('Deleted!', 'ATM Transaction report has been deleted.', 'success').then( - () => { + () => { window.location.reload(); }); }).catch((error) => { @@ -143,6 +143,40 @@ } }) } + + // Add the missing retryReport function + function retryReport(id) { + Swal.fire({ + title: 'Are you sure?', + text: 'This will reset the current job and start a new one.', + icon: 'warning', + showCancelButton: true, + confirmButtonColor: '#3085d6', + cancelButtonColor: '#d33', + confirmButtonText: 'Yes, retry it!' + }).then((result) => { + if (result.isConfirmed) { + $.ajaxSetup({ + headers: { + 'X-CSRF-TOKEN': '{{ csrf_token() }}' + } + }); + + $.ajax(`atm-reports/${id}/retry`, { + type: 'POST' + }).then((response) => { + Swal.fire('Success!', 'Report retry initiated successfully.', 'success').then( + () => { + window.location.reload(); + }); + }).catch((error) => { + console.error('Error:', error); + Swal.fire('Error!', 'Failed to retry report: ' + (error.responseJSON?.message || + 'Unknown error'), 'error'); + }); + } + }) + }