Files
webstatement/app/Http/Controllers/CombinePdfController.php
Daeng Deni Mardaeni d4e6a3d73d feat(webstatement): ekstrak generatePassword ke helper
Perubahan yang dilakukan:
- Memindahkan fungsi `generatePassword` dari `CombinePdfController` ke `helpers.php` untuk peningkatan reusabilitas.
- Menambahkan dependency `use Carbon\Carbon` dan `use Modules\Webstatement\Models\Account` di `helpers.php`.
- Menyesuaikan pemanggilan fungsi `generatePassword` di `CombinePdfController` dengan versi helper.

Tujuan perubahan:
- Mengurangi duplikasi kode dengan menjadikan fungsi `generatePassword` dapat diakses secara global.
- Mempermudah perawatan kode melalui pemisahan tanggung jawab fungsi.

Signed-off-by: Daeng Deni Mardaeni <ddeni05@gmail.com>
2025-07-10 14:12:10 +07:00

162 lines
6.5 KiB
PHP

<?php
namespace Modules\Webstatement\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Log;
use Modules\Webstatement\Jobs\CombinePdfJob;
use Modules\Webstatement\Models\Account;
use Carbon\Carbon;
class CombinePdfController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
return view('webstatement::index');
}
/**
* Combine PDF files from r14 and r23 folders for all accounts
*
* @param Request $request
* @return \Illuminate\Http\JsonResponse
*/
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
// 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');
// Get all accounts with customer relation
$accounts = Account::where('branch_code','ID0010052')->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
]);
}
}