diff --git a/app/Jobs/GenerateBiayaKartuCsvJob.php b/app/Jobs/GenerateBiayaKartuCsvJob.php index 02334bd..683586f 100644 --- a/app/Jobs/GenerateBiayaKartuCsvJob.php +++ b/app/Jobs/GenerateBiayaKartuCsvJob.php @@ -110,46 +110,51 @@ * * @return array Information about the generated file and upload status */ - private function generateAtmCardCsv() - : array + private function generateAtmCardCsv(): array { $cards = $this->getEligibleAtmCards(); - //$filename = storage_path('app/' . $this->csvFilename); + $cardsByBranch = $cards->groupBy('branch'); + $results = []; - // Add date-time to filename - $dateTime = now()->format('Ymd_Hi'); - $filenameWithDate = pathinfo($this->csvFilename, PATHINFO_FILENAME) . - '_' . $dateTime . '.' . - pathinfo($this->csvFilename, PATHINFO_EXTENSION); + foreach ($cardsByBranch as $branch => $branchCards) { + $dateTime = now()->format('Ymd_Hi'); + $branchFilename = pathinfo($this->csvFilename, PATHINFO_FILENAME) + . '_' . $branch + . '_' . $dateTime . '.' + . pathinfo($this->csvFilename, PATHINFO_EXTENSION); - $filename = storage_path('app/' . $filenameWithDate); + $filename = storage_path('app/' . $branchFilename); - $handle = fopen($filename, 'w+'); - if (!$handle) { - throw new RuntimeException("Tidak dapat membuat file CSV: $filename"); - } - - try { - foreach ($cards as $card) { - $fee = $this->determineCardFee($card); - $csvRow = $this->createCsvRow($card, $fee); - fputcsv($handle, $csvRow, '|'); + $handle = fopen($filename, 'w+'); + if (!$handle) { + throw new RuntimeException("Tidak dapat membuat file CSV: $filename"); } - } finally { - fclose($handle); + + try { + foreach ($branchCards as $card) { + $fee = $this->determineCardFee($card); + $csvRow = $this->createCsvRow($card, $fee); + fputcsv($handle, $csvRow, '|'); + } + } finally { + fclose($handle); + } + + $this->cleanupCsvFile($filename); + + // Upload file ke SFTP + $uploadSuccess = $this->uploadToSftpKartu($filename, $card->branch); + + $results[] = [ + 'branch' => $branch, + 'localFilePath' => $filename, + 'recordCount' => count($branchCards), + 'uploadToSftp' => $uploadSuccess, + 'timestamp' => now()->format('Y-m-d H:i:s') + ]; } - $this->cleanupCsvFile($filename); - - // Upload file ke SFTP - $uploadSuccess = $this->uploadToSftpKartu($filename); - - return [ - 'localFilePath' => $filename, - 'recordCount' => count($cards), - 'uploadToSftp' => $uploadSuccess, - 'timestamp' => now()->format('Y-m-d H:i:s') - ]; + return $results; } /**