diff --git a/app/Jobs/ExportStatementJob.php b/app/Jobs/ExportStatementJob.php index 9005df7..15cc849 100644 --- a/app/Jobs/ExportStatementJob.php +++ b/app/Jobs/ExportStatementJob.php @@ -81,10 +81,10 @@ $existingDataCount = $this->getExistingProcessedCount($accountQuery); // Hanya proses jika data belum lengkap diproses - if ($existingDataCount !== $totalCount) { + //if ($existingDataCount !== $totalCount) { $this->deleteExistingProcessedData($accountQuery); $this->processAndSaveStatementEntries($totalCount); - } + //} } private function getTotalEntryCount(array $criteria) @@ -357,8 +357,7 @@ /** * Export processed data to CSV file */ - private function exportToCsv() - : void + private function exportToCsv(): void { // Determine the base path based on client $basePath = !empty($this->client) @@ -367,14 +366,11 @@ $accountPath = "{$basePath}/{$this->account_number}"; - // Create client directory if it doesn't exist - if (!empty($this->client)) { - // Di fungsi exportToCsv untuk basePath - Storage::disk($this->disk)->makeDirectory($basePath); + // PERBAIKAN: Selalu pastikan direktori dibuat + Storage::disk($this->disk)->makeDirectory($basePath); + Storage::disk($this->disk)->makeDirectory($accountPath); + - // Untuk accountPath - Storage::disk($this->disk)->makeDirectory($accountPath); - } $filePath = "{$accountPath}/{$this->fileName}"; @@ -383,13 +379,38 @@ Storage::disk($this->disk)->delete($filePath); } - $csvContent = "NO|TRANSACTION.DATE|REFERENCE.NUMBER|TRANSACTION.AMOUNT|TRANSACTION.TYPE|DESCRIPTION|END.BALANCE|ACTUAL.DATE|NO.RECEIPT\n"; - // Ambil data yang sudah diproses dalam chunk untuk mengurangi penggunaan memori + // Tambahkan di awal fungsi exportToCsv + Log::info("Starting CSV export", [ + 'disk' => $this->disk, + 'client' => $this->client, + 'account_number' => $this->account_number, + 'period' => $this->period, + 'base_path' => $basePath, + 'account_path' => $accountPath, + 'file_path' => $filePath + ]); + + // Cek apakah disk storage berfungsi + $testFile = 'test_' . time() . '.txt'; + Storage::disk($this->disk)->put($testFile, 'test content'); + if (Storage::disk($this->disk)->exists($testFile)) { + Log::info("Storage disk is working"); + Storage::disk($this->disk)->delete($testFile); + } else { + Log::error("Storage disk is not working properly"); + } + + // PERBAIKAN: Buat file header terlebih dahulu + $csvContent = "NO|TRANSACTION.DATE|REFERENCE.NUMBER|TRANSACTION.AMOUNT|TRANSACTION.TYPE|DESCRIPTION|END.BALANCE|ACTUAL.DATE|NO.RECEIPT\n"; + Storage::disk($this->disk)->put($filePath, $csvContent); + + // Ambil data yang sudah diproses dalam chunk ProcessedStatement::where('account_number', $this->account_number) ->where('period', $this->period) ->orderBy('sequence_no') - ->chunk($this->chunkSize, function ($statements) use (&$csvContent, $filePath) { + ->chunk($this->chunkSize, function ($statements) use ($filePath) { + $csvContent = ''; foreach ($statements as $statement) { $csvContent .= implode('|', [ $statement->sequence_no, @@ -404,12 +425,31 @@ ]) . "\n"; } - // Tulis ke file secara bertahap untuk mengurangi penggunaan memori - Storage::disk($this->disk)->append($filePath, $csvContent); - $csvContent = ''; // Reset content setelah ditulis + // Append ke file + if (!empty($csvContent)) { + Storage::disk($this->disk)->append($filePath, $csvContent); + } }); - Log::info("Statement exported to {$this->disk} disk: {$filePath}"); + // PERBAIKAN: Verifikasi file benar-benar ada + if (Storage::disk($this->disk)->exists($filePath)) { + $fileSize = Storage::disk($this->disk)->size($filePath); + Log::info("Statement exported successfully", [ + 'disk' => $this->disk, + 'file_path' => $filePath, + 'file_size' => $fileSize, + 'account_number' => $this->account_number, + 'period' => $this->period + ]); + } else { + Log::error("File was not created despite successful processing", [ + 'disk' => $this->disk, + 'file_path' => $filePath, + 'account_number' => $this->account_number, + 'period' => $this->period + ]); + throw new \Exception("Failed to create CSV file: {$filePath}"); + } } /**