get(); $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/{$accountNumber}_{$period}.pdf"); // Define temporary path for r23 files downloaded from SFTP $tempDir = storage_path("app/temp/{$period}"); if (!File::exists($tempDir)) { File::makeDirectory($tempDir, 0755, true); } $outputDir = storage_path("app/combine/{$period}/{$branchCode}"); $outputFilename = "{$accountNumber}_{$period}.pdf"; // Check if r14 file exists locally $r14Exists = File::exists($r14Path); // Check for multiple r23 files based on configuration $r23Files = []; $r23Exists = false; if ($file_r23 === 'local') { // 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; Log::info("Found " . count($r23Files) . " r23 files locally for account {$accountNumber}"); } } elseif ($file_r23 === 'sftp') { // Use SFTP r23 files - check for multiple files try { $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; Log::info("Downloaded " . count($r23Files) . " r23 files for account {$accountNumber} from SFTP"); } } catch (\Exception $e) { Log::error("Error downloading r23 files from SFTP for account {$accountNumber}: {$e->getMessage()}"); } } // Skip if neither file exists if (!$r14Exists && !$r23Exists) { //Log::warning("No PDF files found for account {$accountNumber}"); $skippedCount++; continue; } // Prepare file list for processing $pdfFiles = []; if ($r14Exists) { $pdfFiles[] = $r14Path; } if ($r23Exists) { // Add all r23 files to the list $pdfFiles = array_merge($pdfFiles, $r23Files); } try { // Generate password based on customer relation data $password = generatePassword($account); // Dispatch job to combine PDFs or apply password protection CombinePdfJob::dispatch($pdfFiles, $outputDir, $outputFilename, $password, $output_destination, $branchCode, $period); $processedCount++; 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++; } } 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, 'skipped' => $skippedCount, 'errors' => $errorCount, 'period' => $period ]); } }