diff --git a/app/Jobs/ExportStatementJob.php b/app/Jobs/ExportStatementJob.php index 93861e3..97a4fca 100644 --- a/app/Jobs/ExportStatementJob.php +++ b/app/Jobs/ExportStatementJob.php @@ -67,14 +67,15 @@ } } - private function processStatementData(): void + private function processStatementData() + : void { $accountQuery = [ 'account_number' => $this->account_number, - 'period' => $this->period + 'period' => $this->period ]; - $totalCount = $this->getTotalEntryCount($accountQuery); + $totalCount = $this->getTotalEntryCount($accountQuery); $existingDataCount = $this->getExistingProcessedCount($accountQuery); // Hanya proses jika data belum lengkap diproses @@ -84,28 +85,32 @@ } } - private function getTotalEntryCount(array $criteria): int + private function getTotalEntryCount(array $criteria) + : int { return StmtEntry::where('account_number', $criteria['account_number']) - ->where('booking_date', $criteria['period']) - ->count(); + ->where('booking_date', $criteria['period']) + ->count(); } - private function getExistingProcessedCount(array $criteria): int + private function getExistingProcessedCount(array $criteria) + : int { return ProcessedStatement::where('account_number', $criteria['account_number']) ->where('period', $criteria['period']) ->count(); } - private function deleteExistingProcessedData(array $criteria): void + private function deleteExistingProcessedData(array $criteria) + : void { ProcessedStatement::where('account_number', $criteria['account_number']) ->where('period', $criteria['period']) ->delete(); } - private function processAndSaveStatementEntries(int $totalCount): void + private function processAndSaveStatementEntries(int $totalCount) + : void { $runningBalance = (float) $this->saldo; $globalSequence = 0; @@ -113,20 +118,21 @@ Log::info("Processing {$totalCount} statement entries for account: {$this->account_number}"); StmtEntry::with(['ft', 'transaction']) - ->where('account_number', $this->account_number) - ->where('booking_date', $this->period) - ->orderBy('date_time', 'ASC') - ->orderBy('trans_reference', 'ASC') - ->chunk($this->chunkSize, function ($entries) use (&$runningBalance, &$globalSequence) { - $processedData = $this->prepareProcessedData($entries, $runningBalance, $globalSequence); + ->where('account_number', $this->account_number) + ->where('booking_date', $this->period) + ->orderBy('date_time', 'ASC') + ->orderBy('trans_reference', 'ASC') + ->chunk($this->chunkSize, function ($entries) use (&$runningBalance, &$globalSequence) { + $processedData = $this->prepareProcessedData($entries, $runningBalance, $globalSequence); - if (!empty($processedData)) { - DB::table('processed_statements')->insert($processedData); - } - }); + if (!empty($processedData)) { + DB::table('processed_statements')->insert($processedData); + } + }); } - private function prepareProcessedData($entries, &$runningBalance, &$globalSequence): array + private function prepareProcessedData($entries, &$runningBalance, &$globalSequence) + : array { $processedData = []; @@ -135,7 +141,7 @@ $runningBalance += (float) $item->amount_lcy; $transactionDate = $this->formatTransactionDate($item); - $actualDate = $this->formatActualDate($item); + $actualDate = $this->formatActualDate($item); $processedData[] = [ 'account_number' => $this->account_number, @@ -156,7 +162,8 @@ return $processedData; } - private function formatTransactionDate($item): string + private function formatTransactionDate($item) + : string { try { return Carbon::createFromFormat( @@ -169,7 +176,8 @@ } } - private function formatActualDate($item): string + private function formatActualDate($item) + : string { try { return Carbon::createFromFormat( @@ -281,16 +289,7 @@ } } - return str_replace('','',$result); - } - - /** - * Get transaction data by reference and field - */ - private function getTransaction($ref, $field) - { - $trans = TempFundsTransfer::where('ref_no', $ref)->first(); - return $trans->$field ?? ""; + return str_replace('', '', $result); } /** @@ -299,6 +298,11 @@ private function exportToCsv() : void { + // Delete existing file if it exists + if (Storage::disk($this->disk)->exists("statements/{$this->fileName}")) { + Storage::disk($this->disk)->delete("statements/{$this->fileName}"); + } + $csvContent = "NO|TRANSACTION.DATE|REFERENCE.NUMBER|TRANSACTION.AMOUNT|TRANSACTION.TYPE|DESCRIPTION|END.BALANCE|ACTUAL.DATE\n"; // Ambil data yang sudah diproses dalam chunk untuk mengurangi penggunaan memori @@ -326,4 +330,13 @@ Log::info("Statement exported to {$this->disk} disk: statements/{$this->fileName}"); } + + /** + * Get transaction data by reference and field + */ + private function getTransaction($ref, $field) + { + $trans = TempFundsTransfer::where('ref_no', $ref)->first(); + return $trans->$field ?? ""; + } }