diff --git a/app/Jobs/ExportStatementPeriodJob.php b/app/Jobs/ExportStatementPeriodJob.php index 5035a0b..c92ef16 100644 --- a/app/Jobs/ExportStatementPeriodJob.php +++ b/app/Jobs/ExportStatementPeriodJob.php @@ -30,6 +30,7 @@ use Modules\Webstatement\Models\{ }; use Modules\Basicdata\Models\Branch; use Illuminate\Foundation\Bus\Dispatchable; +use Owenoj\PDFPasswordProtect\Facade\PDFPasswordProtect; class ExportStatementPeriodJob implements ShouldQueue { @@ -469,10 +470,10 @@ class ExportStatementPeriodJob implements ShouldQueue $saldoAwalBulan = (object) ['actual_balance' => (float) $this->saldo]; // Generate filename - $filename = "statement_{$this->account_number}_{$this->period}.pdf"; + $filename = "{$this->account_number}_{$this->period}.pdf"; // Tentukan path storage - $storagePath = "statements/{$this->period}/{$this->account_number}"; + $storagePath = "statements/{$this->period}/{$account->branch_code}"; $tempPath = storage_path("app/temp/{$filename}"); $fullStoragePath = "{$storagePath}/{$filename}"; @@ -502,6 +503,8 @@ class ExportStatementPeriodJob implements ShouldQueue 'html_length' => strlen($html) ]); + + // Di dalam fungsi generatePdf(), setelah Browsershot::html()->save($tempPath) // Generate PDF menggunakan Browsershot Browsershot::html($html) ->showBackground() @@ -518,6 +521,28 @@ class ExportStatementPeriodJob implements ShouldQueue throw new Exception('PDF file gagal dibuat'); } + $printLog = PrintStatementLog::find($this->statementId); + + // Apply password protection jika diperlukan + $password = $printLog->password ?? generatePassword($account); // Ambil dari config atau set default + if (!empty($password)) { + $tempProtectedPath = storage_path("app/temp/protected_{$filename}"); + + // Encrypt PDF dengan password + PDFPasswordProtect::encrypt($tempPath, $tempProtectedPath, $password); + + // Ganti file original dengan yang sudah diproteksi + if (file_exists($tempProtectedPath)) { + unlink($tempPath); // Hapus file original + rename($tempProtectedPath, $tempPath); // Rename protected file ke original path + + Log::info('ExportStatementPeriodJob: PDF password protection applied', [ + 'account_number' => $this->account_number, + 'period' => $this->period + ]); + } + } + $fileSize = filesize($tempPath); // Pindahkan file ke storage permanen @@ -525,7 +550,7 @@ class ExportStatementPeriodJob implements ShouldQueue Storage::put($fullStoragePath, $pdfContent); // Update print statement log - $printLog = PrintStatementLog::find($this->statementId); + if ($printLog) { $printLog->update([ 'is_available' => true, @@ -580,20 +605,12 @@ class ExportStatementPeriodJob implements ShouldQueue private function exportToCsv(): void { // Determine the base path based on client - $basePath = !empty($this->client) - ? "statements/{$this->client}" - : "statements"; + $account = Account::where('account_number', $this->account_number)->first(); - // Create client directory if it doesn't exist - if (!empty($this->client)) { - Storage::disk($this->disk)->makeDirectory($basePath); - } + $storagePath = "statements/{$this->period}/{$account->branch_code}"; + Storage::disk($this->disk)->makeDirectory($storagePath); - // Create account directory - $accountPath = "{$basePath}/{$this->account_number}"; - Storage::disk($this->disk)->makeDirectory($accountPath); - - $filePath = "{$accountPath}/{$this->fileName}"; + $filePath = "{$storagePath}/{$this->fileName}"; // Delete existing file if it exists if (Storage::disk($this->disk)->exists($filePath)) { diff --git a/app/Jobs/GenerateMultiAccountPdfJob.php b/app/Jobs/GenerateMultiAccountPdfJob.php index e95080f..35f0b56 100644 --- a/app/Jobs/GenerateMultiAccountPdfJob.php +++ b/app/Jobs/GenerateMultiAccountPdfJob.php @@ -247,12 +247,13 @@ class GenerateMultiAccountPdfJob implements ShouldQueue Browsershot::html($html) ->showBackground() ->setOption('addStyleTag', json_encode(['content' => '@page { margin: 0; }'])) + ->setOption('protocolTimeout', 2147483) // 2 menit timeout ->format('A4') ->margins(0, 0, 0, 0) - ->waitUntilNetworkIdle() - ->timeout(60000) + ->waitUntil('load') + ->timeout(2147483) ->save($pdfPath); - + // Verify file was created if (!file_exists($pdfPath)) { throw new Exception('PDF file was not created');