input('period', date('Ym')); // Get all accounts $accounts = Account::all(); $processedCount = 0; $skippedCount = 0; $errorCount = 0; foreach ($accounts as $account) { $branchCode = $account->branch_code; $accountNumber = $account->account_number; // Define file paths $r14Path = storage_path("app/r14/{$period}/{$branchCode}/{$accountNumber}_{$period}.pdf"); $r23Path = storage_path("app/r23/{$period}/{$branchCode}/{$accountNumber}_{$period}.pdf"); $outputDir = storage_path("app/combine/{$period}/{$branchCode}"); $outputFilename = "{$accountNumber}_{$period}.pdf"; // Check if files exist $r14Exists = File::exists($r14Path); $r23Exists = File::exists($r23Path); // Skip if neither file exists if (!$r14Exists && !$r23Exists) { Log::warning("No PDF files found for account {$accountNumber}"); $skippedCount++; continue; } // If both files exist, combine them if ($r14Exists && $r23Exists) { Log::info("Combining PDFs for account {$accountNumber}"); $pdfFiles = [$r14Path, $r23Path]; } // If only one file exists, just apply password protection else { Log::info("Applying password protection to single PDF for account {$accountNumber}"); $pdfFile = $r14Exists ? $r14Path : $r23Path; $pdfFiles = [$pdfFile]; } try { // Use the account number as password $password = $accountNumber; // Dispatch job to combine PDFs or apply password protection CombinePdfJob::dispatch($pdfFiles, $outputDir, $outputFilename, $password); $processedCount++; } catch (\Exception $e) { Log::error("Error processing PDF for account {$accountNumber}: {$e->getMessage()}"); $errorCount++; } } return response()->json([ 'message' => 'PDF combination process has been queued', 'processed' => $processedCount, 'skipped' => $skippedCount, 'errors' => $errorCount, 'period' => $period ]); } }