From 3720a2469031370e5bcc060f2bac35ec421a692a Mon Sep 17 00:00:00 2001 From: Daeng Deni Mardaeni Date: Sun, 8 Jun 2025 10:33:27 +0700 Subject: [PATCH] feat(webstatement): dukung multiple file r23 pada proses combine PDF - Memperbarui `CombinePdfController`: - Menambahkan logika untuk mendukung multiple file r23, baik dari `local` maupun `sftp`. - Mengimplementasikan pencarian file secara dinamis menggunakan pola glob untuk file lokal (`local`). - Menambahkan proses pengunduhan file r23 secara bertahap berdasarkan urutan dari SFTP. - Menyortir file r23 berdasarkan urutan numerik untuk memastikan urutan yang benar dalam penggabungan PDF. - Memindahkan semua file r23 ke direktori sementara sebelum proses gabungan. - Memungkinkan konfigurasi sumber file r23: - Jika `local`, sistem akan mencari semua file dengan nama akun dan variasi urutan di folder penyimpanan lokal. - Jika `sftp`, sistem akan mengunduh semua file r23 dengan pola tertentu dari SFTP dan menyimpannya secara temporer. - Memperbarui log: - Menambahkan informasi jumlah file r23 yang ditemukan untuk setiap akun. - Menambahkan log error detail saat terjadi kegagalan pengunduhan file dari SFTP. Signed-off-by: Daeng Deni Mardaeni --- app/Http/Controllers/CombinePdfController.php | 65 +++++++++++++------ 1 file changed, 45 insertions(+), 20 deletions(-) diff --git a/app/Http/Controllers/CombinePdfController.php b/app/Http/Controllers/CombinePdfController.php index 6574f08..37b8c44 100644 --- a/app/Http/Controllers/CombinePdfController.php +++ b/app/Http/Controllers/CombinePdfController.php @@ -51,16 +51,11 @@ class CombinePdfController extends Controller // Define file paths $r14Path = storage_path("app/r14/{$accountNumber}_{$period}.pdf"); - // Define r23 paths based on configuration - $r23LocalPath = storage_path("app/r23/{$accountNumber}.1.pdf"); - $r23SftpPath = "r23/{$accountNumber}.1.pdf"; - - // Define temporary path for r23 file downloaded from SFTP + // Define temporary path for r23 files downloaded from SFTP $tempDir = storage_path("app/temp/{$period}"); if (!File::exists($tempDir)) { File::makeDirectory($tempDir, 0755, true); } - $r23TempPath = "{$tempDir}/{$accountNumber}_r23.pdf"; $outputDir = storage_path("app/combine/{$period}/{$branchCode}"); $outputFilename = "{$accountNumber}_{$period}.pdf"; @@ -68,29 +63,56 @@ class CombinePdfController extends Controller // Check if r14 file exists locally $r14Exists = File::exists($r14Path); - // Check for r23 file based on configuration + // Check for multiple r23 files based on configuration + $r23Files = []; $r23Exists = false; - $r23FinalPath = null; if ($file_r23 === 'local') { - // Use local r23 files - if (File::exists($r23LocalPath)) { + // Use local r23 files - check for multiple files + $r23Pattern = storage_path("app/r23/{$accountNumber}.*.pdf"); + $foundR23Files = glob($r23Pattern); + + if (!empty($foundR23Files)) { + // Sort files numerically by their sequence number + usort($foundR23Files, function($a, $b) { + preg_match('/\.(\d+)\.pdf$/', $a, $matchesA); + preg_match('/\.(\d+)\.pdf$/', $b, $matchesB); + return (int)$matchesA[1] - (int)$matchesB[1]; + }); + + $r23Files = $foundR23Files; $r23Exists = true; - $r23FinalPath = $r23LocalPath; - Log::info("Found r23 file locally for account {$accountNumber}"); + Log::info("Found " . count($r23Files) . " r23 files locally for account {$accountNumber}"); } } elseif ($file_r23 === 'sftp') { - // Use SFTP r23 files + // Use SFTP r23 files - check for multiple files try { - if (Storage::disk('sftpStatement')->exists($r23SftpPath)) { - $r23Content = Storage::disk('sftpStatement')->get($r23SftpPath); - File::put($r23TempPath, $r23Content); + $sftpFiles = Storage::disk('sftpStatement')->files('r23'); + $accountR23Files = array_filter($sftpFiles, function($file) use ($accountNumber) { + return preg_match("/r23\/{$accountNumber}\.(\d+)\.pdf$/", $file); + }); + + if (!empty($accountR23Files)) { + // Sort files numerically by their sequence number + usort($accountR23Files, function($a, $b) { + preg_match('/\.(\d+)\.pdf$/', $a, $matchesA); + preg_match('/\.(\d+)\.pdf$/', $b, $matchesB); + return (int)$matchesA[1] - (int)$matchesB[1]; + }); + + // Download all r23 files + foreach ($accountR23Files as $index => $sftpFile) { + $r23Content = Storage::disk('sftpStatement')->get($sftpFile); + $tempFileName = "{$tempDir}/{$accountNumber}_r23_" . ($index + 1) . ".pdf"; + File::put($tempFileName, $r23Content); + $r23Files[] = $tempFileName; + } + $r23Exists = true; - $r23FinalPath = $r23TempPath; - Log::info("Downloaded r23 file for account {$accountNumber} from SFTP"); + Log::info("Downloaded " . count($r23Files) . " r23 files for account {$accountNumber} from SFTP"); } } catch (\Exception $e) { - Log::error("Error downloading r23 file from SFTP for account {$accountNumber}: {$e->getMessage()}"); + Log::error("Error downloading r23 files from SFTP for account {$accountNumber}: {$e->getMessage()}"); } } @@ -107,7 +129,8 @@ class CombinePdfController extends Controller $pdfFiles[] = $r14Path; } if ($r23Exists) { - $pdfFiles[] = $r23FinalPath; + // Add all r23 files to the list + $pdfFiles = array_merge($pdfFiles, $r23Files); } try { @@ -125,6 +148,8 @@ class CombinePdfController extends Controller } } + Log::info("Processed {$processedCount} accounts, skipped {$skippedCount} accounts, and encountered {$errorCount} errors."); + return response()->json([ 'message' => "PDF combination process has been queued (r14: local, r23: {$file_r23}, output: {$output_destination})", 'processed' => $processedCount,