feat(webstatement): tambah fungsi generatePdf ke ExportStatementPeriodJob
Perubahan yang dilakukan: - Menambahkan fungsi generatePdf() untuk proses generate PDF dalam job ExportStatementPeriodJob. - Mengintegrasikan logika PDF generation dari PrintStatementController ke dalam job. - Menggunakan data ProcessedStatement yang telah diproses sebagai sumber untuk pembuatan PDF. - Menambahkan import statement untuk Browsershot, Account, Customer, dan Branch. - Mengimplementasikan fungsi prepareHeaderTableBackground() untuk mengonversi gambar header menjadi base64. - Menggunakan database transaction untuk menjaga konsistensi saat generate dan menyimpan PDF. - Menyimpan PDF ke storage dengan struktur direktori yang terorganisir berdasarkan parameter tertentu. - Memperbarui PrintStatementLog dengan status akhir dan path file PDF yang dihasilkan. - Menambahkan error handling dan logging secara menyeluruh untuk memantau proses. - Menghapus file sementara (temporary) setelah PDF berhasil disimpan ke storage. - Menambahkan dukungan timeout dan konfigurasi Browsershot yang optimal. - Melakukan validasi terhadap data account, customer, dan branch sebelum proses generate PDF dilakukan. Tujuan perubahan: - Memindahkan logika generate PDF ke dalam background job agar lebih efisien dan terstruktur. - Menjamin integritas data dan hasil PDF yang valid melalui proses terstandarisasi. - Mengurangi beban proses di controller serta mendukung proses batch secara asynchronous.
This commit is contained in:
@@ -825,9 +825,6 @@ ini_set('max_execution_time', 300000);
|
||||
$period = $statement->period_from;
|
||||
$format='pdf';
|
||||
|
||||
|
||||
|
||||
|
||||
// Generate nama file PDF
|
||||
$filename = $this->generatePdfFileName($norek, $period);
|
||||
|
||||
@@ -967,10 +964,11 @@ ini_set('max_execution_time', 300000);
|
||||
Browsershot::html($html)
|
||||
->showBackground()
|
||||
->setOption('addStyleTag', json_encode(['content' => '@page { margin: 0; }']))
|
||||
->setOption('protocolTimeout', 2147483) // 120000 ms = 2 menit
|
||||
->format('A4')
|
||||
->margins(0, 0, 0, 0)
|
||||
->waitUntilNetworkIdle()
|
||||
->timeout(6000)
|
||||
->waitUntil('load')
|
||||
->timeout(2147483)
|
||||
->save($tempPath);
|
||||
|
||||
// Verifikasi file berhasil dibuat
|
||||
|
||||
@@ -4,20 +4,32 @@ namespace Modules\Webstatement\Jobs;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Exception;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Bus\{
|
||||
Queueable
|
||||
};
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Queue\{
|
||||
InteractsWithQueue,
|
||||
SerializesModels
|
||||
};
|
||||
use Illuminate\Support\Facades\{
|
||||
DB,
|
||||
Log,
|
||||
Storage
|
||||
};
|
||||
use Spatie\Browsershot\Browsershot;
|
||||
use Modules\Webstatement\Models\{
|
||||
PrintStatementLog,
|
||||
ProcessedStatement,
|
||||
StmtEntry,
|
||||
TempFundsTransfer,
|
||||
TempStmtNarrFormat,
|
||||
TempStmtNarrParam,
|
||||
Account,
|
||||
Customer
|
||||
};
|
||||
use Modules\Basicdata\Models\Branch;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Modules\Webstatement\Models\PrintStatementLog;
|
||||
use Modules\Webstatement\Models\ProcessedStatement;
|
||||
use Modules\Webstatement\Models\StmtEntry;
|
||||
use Modules\Webstatement\Models\TempFundsTransfer;
|
||||
use Modules\Webstatement\Models\TempStmtNarrFormat;
|
||||
use Modules\Webstatement\Models\TempStmtNarrParam;
|
||||
|
||||
class ExportStatementPeriodJob implements ShouldQueue
|
||||
{
|
||||
@@ -92,6 +104,10 @@ class ExportStatementPeriodJob implements ShouldQueue
|
||||
if($this->toCsv){
|
||||
$this->exportToCsv();
|
||||
}
|
||||
|
||||
// Generate PDF setelah data diproses
|
||||
$this->generatePdf();
|
||||
|
||||
Log::info("Export statement period job completed successfully for account: {$this->account_number}, period: {$this->period}");
|
||||
} catch (Exception $e) {
|
||||
Log::error("Error in ExportStatementPeriodJob: " . $e->getMessage());
|
||||
@@ -110,10 +126,10 @@ class ExportStatementPeriodJob implements ShouldQueue
|
||||
$existingDataCount = $this->getExistingProcessedCount($accountQuery);
|
||||
|
||||
// Only process if data is not fully processed
|
||||
//if ($existingDataCount !== $totalCount) {
|
||||
if ($existingDataCount !== $totalCount) {
|
||||
$this->deleteExistingProcessedData($accountQuery);
|
||||
$this->processAndSaveStatementEntries($totalCount);
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
private function getTotalEntryCount(): int
|
||||
@@ -398,6 +414,166 @@ class ExportStatementPeriodJob implements ShouldQueue
|
||||
return str_replace('<NL>', ' ', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate PDF statement untuk account yang diproses
|
||||
* Menggunakan data yang sudah diproses dari ProcessedStatement
|
||||
*
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
private function generatePdf(): void
|
||||
{
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
|
||||
Log::info('ExportStatementPeriodJob: Memulai generate PDF', [
|
||||
'account_number' => $this->account_number,
|
||||
'period' => $this->period,
|
||||
'statement_id' => $this->statementId
|
||||
]);
|
||||
|
||||
// Ambil data account dan customer
|
||||
$account = Account::where('account_number', $this->account_number)->first();
|
||||
if (!$account) {
|
||||
throw new Exception("Account tidak ditemukan: {$this->account_number}");
|
||||
}
|
||||
|
||||
$customer = Customer::where('customer_code', $account->customer_code)->first();
|
||||
if (!$customer) {
|
||||
throw new Exception("Customer tidak ditemukan untuk account: {$this->account_number}");
|
||||
}
|
||||
|
||||
// Ambil data branch
|
||||
$branch = Branch::where('code', $account->branch_code)->first();
|
||||
if (!$branch) {
|
||||
throw new Exception("Branch tidak ditemukan: {$account->branch_code}");
|
||||
}
|
||||
|
||||
// Ambil statement entries yang sudah diproses
|
||||
$stmtEntries = ProcessedStatement::where('account_number', $this->account_number)
|
||||
->where('period', $this->period)
|
||||
->orderBy('sequence_no')
|
||||
->get();
|
||||
|
||||
if ($stmtEntries->isEmpty()) {
|
||||
throw new Exception("Tidak ada data statement yang diproses untuk account: {$this->account_number}");
|
||||
}
|
||||
|
||||
// Prepare header table background (convert to base64 if needed)
|
||||
$headerImagePath = public_path('assets/media/images/bg-header-table.png');
|
||||
$headerTableBg = file_exists($headerImagePath)
|
||||
? base64_encode(file_get_contents($headerImagePath))
|
||||
: null;
|
||||
|
||||
// Hitung saldo awal bulan
|
||||
$saldoAwalBulan = (object) ['actual_balance' => (float) $this->saldo];
|
||||
|
||||
// Generate filename
|
||||
$filename = "statement_{$this->account_number}_{$this->period}.pdf";
|
||||
|
||||
// Tentukan path storage
|
||||
$storagePath = "statements/{$this->period}/{$this->account_number}";
|
||||
$tempPath = storage_path("app/temp/{$filename}");
|
||||
$fullStoragePath = "{$storagePath}/{$filename}";
|
||||
|
||||
// Pastikan direktori temp ada
|
||||
if (!file_exists(dirname($tempPath))) {
|
||||
mkdir(dirname($tempPath), 0755, true);
|
||||
}
|
||||
|
||||
// Pastikan direktori storage ada
|
||||
Storage::makeDirectory($storagePath);
|
||||
|
||||
$period = $this->period;
|
||||
|
||||
// Render HTML view
|
||||
$html = view('webstatement::statements.stmt', compact(
|
||||
'stmtEntries',
|
||||
'account',
|
||||
'customer',
|
||||
'headerTableBg',
|
||||
'branch',
|
||||
'period',
|
||||
'saldoAwalBulan'
|
||||
))->render();
|
||||
|
||||
Log::info('ExportStatementPeriodJob: HTML view berhasil di-render', [
|
||||
'account_number' => $this->account_number,
|
||||
'html_length' => strlen($html)
|
||||
]);
|
||||
|
||||
// Generate PDF menggunakan Browsershot
|
||||
Browsershot::html($html)
|
||||
->showBackground()
|
||||
->setOption('addStyleTag', json_encode(['content' => '@page { margin: 0; }']))
|
||||
->setOption('protocolTimeout', 2147483) // 2 menit timeout
|
||||
->format('A4')
|
||||
->margins(0, 0, 0, 0)
|
||||
->waitUntil('load')
|
||||
->timeout(2147483)
|
||||
->save($tempPath);
|
||||
|
||||
// Verifikasi file berhasil dibuat
|
||||
if (!file_exists($tempPath)) {
|
||||
throw new Exception('PDF file gagal dibuat');
|
||||
}
|
||||
|
||||
$fileSize = filesize($tempPath);
|
||||
|
||||
// Pindahkan file ke storage permanen
|
||||
$pdfContent = file_get_contents($tempPath);
|
||||
Storage::put($fullStoragePath, $pdfContent);
|
||||
|
||||
// Update print statement log
|
||||
$printLog = PrintStatementLog::find($this->statementId);
|
||||
if ($printLog) {
|
||||
$printLog->update([
|
||||
'is_available' => true,
|
||||
'is_generated' => true,
|
||||
'pdf_path' => $fullStoragePath,
|
||||
'file_size' => $fileSize
|
||||
]);
|
||||
}
|
||||
|
||||
// Hapus file temporary
|
||||
if (file_exists($tempPath)) {
|
||||
unlink($tempPath);
|
||||
}
|
||||
|
||||
Log::info('ExportStatementPeriodJob: PDF berhasil dibuat dan disimpan', [
|
||||
'account_number' => $this->account_number,
|
||||
'period' => $this->period,
|
||||
'storage_path' => $fullStoragePath,
|
||||
'file_size' => $fileSize,
|
||||
'statement_id' => $this->statementId
|
||||
]);
|
||||
|
||||
DB::commit();
|
||||
|
||||
} catch (Exception $e) {
|
||||
DB::rollBack();
|
||||
|
||||
Log::error('ExportStatementPeriodJob: Gagal generate PDF', [
|
||||
'error' => $e->getMessage(),
|
||||
'account_number' => $this->account_number,
|
||||
'period' => $this->period,
|
||||
'statement_id' => $this->statementId,
|
||||
'trace' => $e->getTraceAsString()
|
||||
]);
|
||||
|
||||
// Update print statement log dengan status error
|
||||
$printLog = PrintStatementLog::find($this->statementId);
|
||||
if ($printLog) {
|
||||
$printLog->update([
|
||||
'is_available' => false,
|
||||
'error_message' => $e->getMessage()
|
||||
]);
|
||||
}
|
||||
|
||||
throw new Exception('Gagal generate PDF: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Export processed data to CSV file
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user