Files
webstatement/app/Http/Controllers/CombinePdfController.php
Daeng Deni Mardaeni 336ef8cf3a feat(webstatement): perbarui logika pemrosesan combine PDF dan parameter periode
- Memperbarui fungsi `combinePdfs` di `CombinePdfController`:
  - Menghapus penggunaan parameter dari request dan mengganti dengan parameter langsung `$period`.
  - Menambahkan filter `branch_code` `ID0010001` pada pemanggilan data akun untuk memastikan hanya akun tertentu yang diproses.
  - Mengubah jalur pencarian file PDF ke direktori baru: `app/STMT/r14` dan `app/STMT/r23` untuk menyelaraskan struktur penyimpanan file.

- Memperbarui command `webstatement:combine-pdf`:
  - Menambahkan opsi baru `--period` untuk menyederhanakan pengaturan periode penggabungan PDF melalui format `Ym` (contoh: 202506).
  - Menghapus penggunaan `request()` pada command untuk memaksimalkan pengelolaan periode langsung dari opsi command-line.

- Tujuan pembaruan ini:
  - Memastikan proses combine PDF hanya memproses data relevan berdasarkan filter branch dan struktur direktori baru.
  - Menyempurnakan fleksibilitas parameter periode pada command-line untuk mengurangi dependensi terhadap input request.
  - Meningkatkan konsistensi dan efisiensi dalam pengelolaan file PDF sesuai periode dan filter branch tertentu.

Signed-off-by: Daeng Deni Mardaeni <ddeni05@gmail.com>
2025-06-04 18:55:29 +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($period)
{
// Get period from request or use current period
$period = $period ?? date('Ym');
// Get all accounts
$accounts = Account::where('branch_code','ID0010001')->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/STMT/r14/{$period}/{$branchCode}/{$accountNumber}_{$period}.pdf");
$r23Path = storage_path("app/STMT/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
]);
}
}