From 7bb320d718dc2430ce7fc364d248e09945e6ca51 Mon Sep 17 00:00:00 2001 From: Daeng Deni Mardaeni Date: Sat, 17 May 2025 16:48:57 +0700 Subject: [PATCH] feat(webstatement): tambah dukungan upload SFTP per cabang Menambahkan parameter "branch" untuk mendukung upload file CSV ke direktori SFTP berdasarkan cabang. Perubahan ini mencakup: - Memodifikasi fungsi `uploadToSftpKartu` agar menerima parameter cabang untuk menentukan direktori tujuan di SFTP. - Membuat log upload yang lebih spesifik untuk setiap cabang berdasarkan parameter "branch". - Memastikan direktori cabang pada SFTP dibuat jika belum ada. - Menyesuaikan fungsi `updateSftpLogSuccess` untuk mencatat log keberhasilan berdasarkan cabang. - Mengupdate log pembuatan dan upload CSV agar mencantumkan informasi cabang. - Menyesuaikan format log `updateCsvLogSuccess` untuk menangani pembuatan file CSV untuk beberapa cabang. Perubahan ini memastikan fleksibilitas dan akuntabilitas dalam mengelola file CSV biaya kartu per cabang. Signed-off-by: Daeng Deni Mardaeni --- app/Jobs/GenerateBiayaKartuCsvJob.php | 84 +++++++++++++++------------ 1 file changed, 48 insertions(+), 36 deletions(-) diff --git a/app/Jobs/GenerateBiayaKartuCsvJob.php b/app/Jobs/GenerateBiayaKartuCsvJob.php index 683586f..cb13989 100644 --- a/app/Jobs/GenerateBiayaKartuCsvJob.php +++ b/app/Jobs/GenerateBiayaKartuCsvJob.php @@ -273,8 +273,7 @@ * * @return bool True if upload successful, false otherwise */ - private function uploadToSftpKartu(string $localFilePath) - : bool + private function uploadToSftpKartu(string $localFilePath, string $branch = ''): bool { try { // Update status SFTP upload dimulai @@ -294,26 +293,37 @@ $disk = Storage::disk('sftpKartu'); // Tentukan path tujuan di server SFTP - $remotePath = env('BIAYA_KARTU_REMOTE_PATH', '/'); + $remotePath = env('BIAYA_KARTU_REMOTE_PATH', '/'); + + // Add branch directory if provided + if (!empty($branch)) { + $remotePath = rtrim($remotePath, '/') . '/' . $branch; + + // Create branch directory if it doesn't exist + if (!$disk->exists($remotePath)) { + $disk->makeDirectory($remotePath); + } + } + $remoteFilePath = rtrim($remotePath, '/') . '/' . $filename; // Upload file ke server SFTP $result = $disk->put($remoteFilePath, $fileContent); if ($result) { - $this->updateSftpLogSuccess(); - Log::info("File CSV biaya kartu ATM berhasil diunggah ke SFTP: {$remoteFilePath}"); + $this->updateSftpLogSuccess($branch); + Log::info("File CSV biaya kartu ATM untuk cabang {$branch} berhasil diunggah ke SFTP: {$remoteFilePath}"); return true; } else { $this->updateSftpLogFailed("Gagal mengunggah file CSV biaya kartu ATM ke SFTP: {$remoteFilePath}"); - Log::error("Gagal mengunggah file CSV biaya kartu ATM ke SFTP: {$remoteFilePath}"); + Log::error("Gagal mengunggah file CSV biaya kartu ATM untuk cabang {$branch} ke SFTP: {$remoteFilePath}"); return false; } } catch (Exception $e) { $this->updateSftpLogFailed($e->getMessage()); - Log::error("Error saat mengunggah file ke SFTP: " . $e->getMessage(), [ + Log::error("Error saat mengunggah file ke SFTP untuk cabang {$branch}: " . $e->getMessage(), [ 'file' => $e->getFile(), 'line' => $e->getLine(), 'periode' => $this->periode @@ -322,6 +332,23 @@ } } + private function updateSftpLogSuccess(string $branch = ''): void + { + $message = 'File berhasil diupload ke SFTP'; + if (!empty($branch)) { + $message .= " untuk cabang {$branch}"; + } + + $this->syncLog->update([ + 'is_ftp' => true, + 'ftp_at' => Carbon::now(), + 'ftp_notes' => $message, + 'ftp_destination' => env('SFTP_KARTU_HOST', '/'), + ]); + + Log::info($message . ' untuk periode ' . $this->periode); + } + /** * Update log saat upload SFTP dimulai */ @@ -337,22 +364,6 @@ Log::info('Memulai upload ke SFTP untuk periode ' . $this->periode); } - /** - * Update log saat upload SFTP berhasil - */ - private function updateSftpLogSuccess() - : void - { - $this->syncLog->update([ - 'is_ftp' => true, - 'ftp_at' => Carbon::now(), - 'ftp_notes' => 'File berhasil diupload ke SFTP', - 'ftp_destination' => env('SFTP_KARTU_HOST', '/'), - ]); - - Log::info('File berhasil diupload ke SFTP untuk periode ' . $this->periode); - } - /** * Update log saat upload SFTP gagal */ @@ -370,22 +381,23 @@ /** * Update log saat pembuatan CSV berhasil */ - private function updateCsvLogSuccess($result) - : void + private function updateCsvLogSuccess($results): void { - // Get file info - $fileInfo = pathinfo($result['localFilePath']); - $fileName = $fileInfo['basename']; + foreach ($results as $result) { + // Get file info + $fileInfo = pathinfo($result['localFilePath']); + $fileName = $fileInfo['basename']; - $this->syncLog->update([ - 'is_csv' => true, - 'csv_at' => Carbon::now(), - 'csv_notes' => 'File CSV berhasil dibuat: ' . $fileName, - 'file_path' => $result['localFilePath'], - 'file_name' => $fileName, - ]); + $this->syncLog->update([ + 'is_csv' => true, + 'csv_at' => Carbon::now(), + 'csv_notes' => 'File CSV untuk cabang ' . $result['branch'] . ' berhasil dibuat: ' . $fileName, + 'file_path' => $result['localFilePath'], + 'file_name' => $fileName, + ]); - Log::info('File CSV berhasil dibuat: ' . $result['localFilePath']); + Log::info('File CSV untuk cabang ' . $result['branch'] . ' berhasil dibuat: ' . $result['localFilePath']); + } } /**