diff --git a/app/Http/Controllers/CombinePdfController.php b/app/Http/Controllers/CombinePdfController.php index 9c27cf2..6574f08 100644 --- a/app/Http/Controllers/CombinePdfController.php +++ b/app/Http/Controllers/CombinePdfController.php @@ -32,6 +32,9 @@ class CombinePdfController extends Controller // Configuration: Set r23 file source - 'local' or 'sftp' $file_r23 = 'local'; // Change this to 'sftp' to use SFTP for r23 files + // Configuration: Set output destination - 'local' or 'sftp' + $output_destination = 'local'; // Change this to 'sftp' to upload combined PDFs to SFTP + // Get period from request or use current period $period = $period ?? date('Ym'); @@ -112,10 +115,10 @@ class CombinePdfController extends Controller $password = $this->generatePassword($account); // Dispatch job to combine PDFs or apply password protection - CombinePdfJob::dispatch($pdfFiles, $outputDir, $outputFilename, $password); + CombinePdfJob::dispatch($pdfFiles, $outputDir, $outputFilename, $password, $output_destination, $branchCode, $period); $processedCount++; - Log::info("Queued PDF processing for account {$accountNumber} - r14: local, r23: {$file_r23}, password: {$password}"); + Log::info("Queued PDF processing for account {$accountNumber} - r14: local, r23: {$file_r23}, output: {$output_destination}, password: {$password}"); } catch (\Exception $e) { Log::error("Error processing PDF for account {$accountNumber}: {$e->getMessage()}"); $errorCount++; @@ -123,7 +126,7 @@ class CombinePdfController extends Controller } return response()->json([ - 'message' => "PDF combination process has been queued (r14: local, r23: {$file_r23})", + 'message' => "PDF combination process has been queued (r14: local, r23: {$file_r23}, output: {$output_destination})", 'processed' => $processedCount, 'skipped' => $skippedCount, 'errors' => $errorCount, diff --git a/app/Jobs/CombinePdfJob.php b/app/Jobs/CombinePdfJob.php index d8e67a0..ac02662 100644 --- a/app/Jobs/CombinePdfJob.php +++ b/app/Jobs/CombinePdfJob.php @@ -10,6 +10,7 @@ use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Storage; +use Illuminate\Support\Facades\File; use Owenoj\PDFPasswordProtect\Facade\PDFPasswordProtect; use Webklex\PDFMerger\Facades\PDFMergerFacade as PDFMerger; @@ -21,6 +22,9 @@ class CombinePdfJob implements ShouldQueue protected $outputPath; protected $outputFilename; protected $password; + protected $outputDestination; + protected $branchCode; + protected $period; /** * Create a new job instance. @@ -29,13 +33,19 @@ class CombinePdfJob implements ShouldQueue * @param string $outputPath Directory where the combined PDF will be saved * @param string $outputFilename Filename for the combined PDF * @param string $password Password to protect the PDF + * @param string $outputDestination Output destination: 'local' or 'sftp' + * @param string $branchCode Branch code for SFTP path + * @param string $period Period for SFTP path */ - public function __construct(array $pdfFiles, string $outputPath, string $outputFilename, string $password) + public function __construct(array $pdfFiles, string $outputPath, string $outputFilename, string $password, string $outputDestination = 'local', string $branchCode = '', string $period = '') { $this->pdfFiles = $pdfFiles; $this->outputPath = $outputPath; $this->outputFilename = $outputFilename; $this->password = $password; + $this->outputDestination = $outputDestination; + $this->branchCode = $branchCode; + $this->period = $period; } /** @@ -86,10 +96,43 @@ class CombinePdfJob implements ShouldQueue Log::info("PDF password protection applied successfully."); } - Log::info("PDFs combined successfully. Output file: {$fullPath}"); + // Handle output destination + if ($this->outputDestination === 'sftp') { + $this->uploadToSftp($fullPath); + } + + Log::info("PDFs combined successfully. Output file: {$fullPath}, Destination: {$this->outputDestination}"); } catch (Exception $e) { Log::error("Error combining PDFs: " . $e->getMessage()); throw $e; } } + + /** + * Upload the combined PDF to SFTP server + * + * @param string $localFilePath + */ + private function uploadToSftp(string $localFilePath): void + { + try { + // Define SFTP path: combine/{period}/{branchCode}/{filename} + $sftpPath = "combine/{$this->period}/{$this->branchCode}/{$this->outputFilename}"; + + // Read the local file content + $fileContent = File::get($localFilePath); + + // Upload to SFTP + Storage::disk('sftpStatement')->put($sftpPath, $fileContent); + + Log::info("Combined PDF uploaded to SFTP: {$sftpPath}"); + + // Optionally, remove the local file after successful upload + // File::delete($localFilePath); + + } catch (\Exception $e) { + Log::error("Error uploading combined PDF to SFTP: {$e->getMessage()}"); + throw $e; + } + } }