From 47ad46f6644529133fcd947058ac79f7cc40f855 Mon Sep 17 00:00:00 2001 From: Daeng Deni Mardaeni Date: Fri, 9 May 2025 09:48:38 +0700 Subject: [PATCH] feat(webstatement): tambahkan fitur upload CSV biaya kartu ATM ke SFTP - Menambah fungsi `uploadToSftpKartu` untuk mengunggah file CSV ke server SFTP. - Memperbarui fungsi `generateAtmCardCsv` agar mengembalikan informasi terkait file yang dihasilkan dan status upload. - Log ditingkatkan untuk mencatat status upload dan error secara lebih rinci. - Menambahkan mekanisme pengecekan dan logging jika upload ke SFTP gagal, file tetap tersedia secara lokal. Signed-off-by: Daeng Deni Mardaeni --- app/Jobs/GenerateBiayaKartuCsvJob.php | 81 ++++++++++++++++++++++++--- 1 file changed, 74 insertions(+), 7 deletions(-) diff --git a/app/Jobs/GenerateBiayaKartuCsvJob.php b/app/Jobs/GenerateBiayaKartuCsvJob.php index 4f2ad97..6814080 100644 --- a/app/Jobs/GenerateBiayaKartuCsvJob.php +++ b/app/Jobs/GenerateBiayaKartuCsvJob.php @@ -9,6 +9,7 @@ use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\Log; + use Illuminate\Support\Facades\Storage; use Modules\Webstatement\Models\Atmcard; use RuntimeException; @@ -40,10 +41,20 @@ : void { try { - $filename = $this->generateAtmCardCsv(); - Log::info('File CSV biaya kartu ATM berhasil dibuat: ' . $filename); + $result = $this->generateAtmCardCsv(); + + Log::info('Pembuatan dan upload file CSV biaya kartu ATM selesai', [ + 'file' => $result['localFilePath'], + 'jumlah_kartu' => $result['recordCount'], + 'upload_sftp' => $result['uploadToSftp'] ? 'Berhasil' : 'Gagal', + 'waktu' => $result['timestamp'] + ]); + + if (!$result['uploadToSftp']) { + Log::warning('File CSV biaya kartu ATM tidak berhasil diunggah ke SFTP, tetapi tersedia secara lokal di: ' . $result['localFilePath']); + } } catch (Exception $e) { - Log::error('Gagal membuat file CSV biaya kartu ATM: ' . $e->getMessage(), [ + Log::error('Gagal membuat atau mengunggah file CSV biaya kartu ATM: ' . $e->getMessage(), [ 'file' => $e->getFile(), 'line' => $e->getLine() ]); @@ -52,12 +63,12 @@ } /** - * Generate CSV file with ATM card data + * Generate CSV file with ATM card data and upload to SFTP * - * @return string Filename of generated CSV + * @return array Information about the generated file and upload status */ private function generateAtmCardCsv() - : string + : array { $cards = $this->getEligibleAtmCards(); $filename = storage_path('app/' . self::CSV_FILENAME); @@ -79,7 +90,15 @@ $this->cleanupCsvFile($filename); - return $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') + ]; } /** @@ -175,4 +194,52 @@ throw new RuntimeException("Tidak dapat menulis ke file CSV: $filename"); } } + + /** + * Upload the generated CSV file to SFTP server + * + * @param string $localFilePath Path to the local CSV file + * + * @return bool True if upload successful, false otherwise + */ + private function uploadToSftpKartu(string $localFilePath) + : bool + { + try { + // Ambil nama file dari path + $filename = basename($localFilePath); + + // Ambil konten file + $fileContent = file_get_contents($localFilePath); + if ($fileContent === false) { + Log::error("Tidak dapat membaca file untuk upload: {$localFilePath}"); + return false; + } + + // Dapatkan disk SFTP + $disk = Storage::disk('sftpKartu'); + + // Tentukan path tujuan di server SFTP + $remotePath = env('BIAYA_KARTU_REMOTE_PATH', '/'); + $remoteFilePath = rtrim($remotePath, '/') . '/' . $filename; + + // Upload file ke server SFTP + $result = $disk->put($remoteFilePath, $fileContent); + + if ($result) { + Log::info("File CSV biaya kartu ATM berhasil diunggah ke SFTP: {$remoteFilePath}"); + return true; + } else { + Log::error("Gagal mengunggah file CSV biaya kartu ATM ke SFTP: {$remoteFilePath}"); + return false; + } + } catch (Exception $e) { + Log::error("Error saat mengunggah file ke SFTP: " . $e->getMessage(), [ + 'file' => $e->getFile(), + 'line' => $e->getLine() + ]); + return false; + } + } + }