Files
webstatement/app/Http/Controllers/CombinePdfController.php
Daeng Deni Mardaeni 8a728d6c6e feat(webstatement): tambahkan fitur penggabungan dan proteksi file PDF
- Menambahkan command baru `webstatement:combine-pdf` melalui `CombinePdf` untuk menjalankan proses penggabungan file PDF.
  - Proses ini mencakup penggabungan file PDF dari folder r14 dan r23 berdasarkan periode tertentu.
  - File PDF yang dihasilkan juga dilindungi dengan password berbasis nomor rekening.
- Membuat controller `CombinePdfController` dengan fungsi utama `combinePdfs` untuk mengontrol alur penggabungan file PDF:
  - Mendapatkan daftar akun yang relevan.
  - Mengecek file dari folder r14 dan r23 untuk setiap akun.
  - Melakukan logging saat file tidak ditemukan atau jika terdapat error dalam proses.
  - Mendaftarkan job `CombinePdfJob` untuk memproses file secara async.
- Menambahkan job baru `CombinePdfJob`:
  - Menggunakan library `PDFMerger` untuk menggabungkan file.
  - Terapkan proteksi password menggunakan library `PDFPasswordProtect`.
  - Memastikan direktori output dibuat jika belum ada.
  - Melakukan logging saat proses berhasil maupun saat terjadi error.
- Memperbarui `WebstatementServiceProvider`:
  - Mendaftarkan command baru ke dalam provider.
  - Menambahkan penjadwalan otomatis untuk menjalankan perintah `webstatement:combine-pdf` setiap hari pada pukul 09:30.
  - Logging hasil eksekusi ke file log `logs/combine-pdf.log`.

Signed-off-by: Daeng Deni Mardaeni <ddeni05@gmail.com>
2025-06-02 10:31:51 +07:00

94 lines
3.1 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\Log;
use Modules\Webstatement\Jobs\CombinePdfJob;
use Modules\Webstatement\Models\Account;
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(Request $request)
{
// Get period from request or use current period
$period = $request->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
]);
}
}