diff --git a/app/Http/Controllers/SyncLogsController.php b/app/Http/Controllers/SyncLogsController.php index 3476b7e..fd3a76d 100644 --- a/app/Http/Controllers/SyncLogsController.php +++ b/app/Http/Controllers/SyncLogsController.php @@ -123,4 +123,32 @@ class SyncLogsController extends Controller return response()->json($syncLog, 200, ['Content-Type' => 'application/json'], JSON_PRETTY_PRINT); } + + + /** + * Download file + */ + public function downloadFile($id) + { + $log = KartuSyncLog::findOrFail($id); + + // Periksa apakah file sudah dibuat + if (!$log->is_csv || !$log->file_name) { + return redirect()->back()->with('error', 'File CSV belum dibuat'); + } + + // Periksa apakah file ada di lokasi yang ditunjukkan + if ($log->file_path && File::exists($log->file_path)) { + return response()->download($log->file_path, $log->file_name); + } + + // Jika tidak ada di path spesifik, coba cari di direktori umum + $path = storage_path('app/biaya_kartu/' . $log->file_name); + if (File::exists($path)) { + return response()->download($path, $log->file_name); + } + + // Jika masih tidak ditemukan, beri pesan error + return redirect()->back()->with('error', 'File tidak ditemukan di server'); + } } diff --git a/resources/views/sync-logs/index.blade.php b/resources/views/sync-logs/index.blade.php index 5e41a11..69cda4e 100644 --- a/resources/views/sync-logs/index.blade.php +++ b/resources/views/sync-logs/index.blade.php @@ -1,4 +1,3 @@ - @extends('layouts.main') @section('breadcrumbs') @@ -168,8 +167,15 @@ + + @endsection @push('scripts') @@ -180,7 +186,7 @@ const filterCsv = document.getElementById('filter-csv'); const filterFtp = document.getElementById('filter-ftp'); const detailModal = document.getElementById('detail-modal'); - console.log(detailModal); + const btnDownloadFile = document.getElementById('btn-download-file'); const apiUrl = element.getAttribute('data-api-url'); const dataTableOptions = { @@ -250,6 +256,14 @@ actions += ``; + + if (data.is_csv && data.file_name) { + actions += ` + + `; + } + + actions += ``; return actions; }, @@ -287,7 +301,7 @@ filterFtp.addEventListener('change', applyFilters); // Detail modal functionality - document.addEventListener('click', function(e) { + document.addEventListener('click', function (e) { if (e.target.closest('.btn-detail')) { const id = e.target.closest('.btn-detail').getAttribute('data-id'); @@ -295,7 +309,13 @@ fetch(`{{ url('sync-logs') }}/${id}`) .then(response => response.json()) .then(data => { - console.log(data); + if (data.is_csv && data.file_name) { + btnDownloadFile.classList.remove('hidden'); + btnDownloadFile.href = `{{ url('sync-logs/download') }}/${data.id}`; + } else { + btnDownloadFile.classList.add('hidden'); + } + // Populate modal with data document.getElementById('detail-periode').textContent = data.periode; document.getElementById('detail-total-records').textContent = data.total_records; @@ -329,7 +349,6 @@ document.getElementById('detail-ftp-notes').textContent = data.ftp_notes || '-'; // Show modal - const modalEl = KTDom.getElement('#detail-modal'); const modal = KTModal.getInstance(modalEl); modal.show(); diff --git a/routes/web.php b/routes/web.php index 3ecfc07..b87843b 100644 --- a/routes/web.php +++ b/routes/web.php @@ -46,6 +46,8 @@ Route::middleware(['auth'])->group(function () { Route::prefix('sync-logs')->name('sync-logs.')->group(function () { Route::get('datatables', [SyncLogsController::class, 'dataForDatatables'])->name('datatables'); + Route::get('/download/{id}', [SyncLogsController::class, 'downloadFile'])->name('download'); + }); Route::resource('sync-logs', SyncLogsController::class);