diff --git a/app/Http/Controllers/CombinePdfController.php b/app/Http/Controllers/CombinePdfController.php index 30a67e6..9c27cf2 100644 --- a/app/Http/Controllers/CombinePdfController.php +++ b/app/Http/Controllers/CombinePdfController.php @@ -29,6 +29,9 @@ class CombinePdfController extends Controller */ public function combinePdfs($period) { + // Configuration: Set r23 file source - 'local' or 'sftp' + $file_r23 = 'local'; // Change this to 'sftp' to use SFTP for r23 files + // Get period from request or use current period $period = $period ?? date('Ym'); @@ -42,16 +45,19 @@ class CombinePdfController extends Controller $branchCode = $account->branch_code; $accountNumber = $account->account_number; - // Define file paths - r14 from local, r23 from SFTP + // 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 local temporary path for r23 file downloaded from SFTP + // Define temporary path for r23 file downloaded from SFTP $tempDir = storage_path("app/temp/{$period}"); if (!File::exists($tempDir)) { File::makeDirectory($tempDir, 0755, true); } - $r23LocalPath = "{$tempDir}/{$accountNumber}_r23.pdf"; + $r23TempPath = "{$tempDir}/{$accountNumber}_r23.pdf"; $outputDir = storage_path("app/combine/{$period}/{$branchCode}"); $outputFilename = "{$accountNumber}_{$period}.pdf"; @@ -59,17 +65,30 @@ class CombinePdfController extends Controller // Check if r14 file exists locally $r14Exists = File::exists($r14Path); - // Check if r23 file exists on SFTP and download it + // Check for r23 file based on configuration $r23Exists = false; - try { - if (Storage::disk('sftpStatement')->exists($r23SftpPath)) { - $r23Content = Storage::disk('sftpStatement')->get($r23SftpPath); - File::put($r23LocalPath, $r23Content); + $r23FinalPath = null; + + if ($file_r23 === 'local') { + // Use local r23 files + if (File::exists($r23LocalPath)) { $r23Exists = true; - Log::info("Downloaded r23 file for account {$accountNumber} from SFTP"); + $r23FinalPath = $r23LocalPath; + Log::info("Found r23 file locally for account {$accountNumber}"); + } + } elseif ($file_r23 === 'sftp') { + // Use SFTP r23 files + try { + if (Storage::disk('sftpStatement')->exists($r23SftpPath)) { + $r23Content = Storage::disk('sftpStatement')->get($r23SftpPath); + File::put($r23TempPath, $r23Content); + $r23Exists = true; + $r23FinalPath = $r23TempPath; + Log::info("Downloaded r23 file for account {$accountNumber} from SFTP"); + } + } catch (\Exception $e) { + Log::error("Error downloading r23 file from SFTP for account {$accountNumber}: {$e->getMessage()}"); } - } catch (\Exception $e) { - Log::error("Error downloading r23 file from SFTP for account {$accountNumber}: {$e->getMessage()}"); } // Skip if neither file exists @@ -85,7 +104,7 @@ class CombinePdfController extends Controller $pdfFiles[] = $r14Path; } if ($r23Exists) { - $pdfFiles[] = $r23LocalPath; + $pdfFiles[] = $r23FinalPath; } try { @@ -96,7 +115,7 @@ class CombinePdfController extends Controller CombinePdfJob::dispatch($pdfFiles, $outputDir, $outputFilename, $password); $processedCount++; - Log::info("Queued PDF processing for account {$accountNumber} - r14: local, r23: SFTP, password: {$password}"); + Log::info("Queued PDF processing for account {$accountNumber} - r14: local, r23: {$file_r23}, password: {$password}"); } catch (\Exception $e) { Log::error("Error processing PDF for account {$accountNumber}: {$e->getMessage()}"); $errorCount++; @@ -104,7 +123,7 @@ class CombinePdfController extends Controller } return response()->json([ - 'message' => 'PDF combination process has been queued (r14: local, r23: SFTP)', + 'message' => "PDF combination process has been queued (r14: local, r23: {$file_r23})", 'processed' => $processedCount, 'skipped' => $skippedCount, 'errors' => $errorCount,