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 <ddeni05@gmail.com>
This commit is contained in:
@@ -9,6 +9,7 @@
|
|||||||
use Illuminate\Queue\InteractsWithQueue;
|
use Illuminate\Queue\InteractsWithQueue;
|
||||||
use Illuminate\Queue\SerializesModels;
|
use Illuminate\Queue\SerializesModels;
|
||||||
use Illuminate\Support\Facades\Log;
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
use Modules\Webstatement\Models\Atmcard;
|
use Modules\Webstatement\Models\Atmcard;
|
||||||
use RuntimeException;
|
use RuntimeException;
|
||||||
|
|
||||||
@@ -40,10 +41,20 @@
|
|||||||
: void
|
: void
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$filename = $this->generateAtmCardCsv();
|
$result = $this->generateAtmCardCsv();
|
||||||
Log::info('File CSV biaya kartu ATM berhasil dibuat: ' . $filename);
|
|
||||||
|
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) {
|
} 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(),
|
'file' => $e->getFile(),
|
||||||
'line' => $e->getLine()
|
'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()
|
private function generateAtmCardCsv()
|
||||||
: string
|
: array
|
||||||
{
|
{
|
||||||
$cards = $this->getEligibleAtmCards();
|
$cards = $this->getEligibleAtmCards();
|
||||||
$filename = storage_path('app/' . self::CSV_FILENAME);
|
$filename = storage_path('app/' . self::CSV_FILENAME);
|
||||||
@@ -79,7 +90,15 @@
|
|||||||
|
|
||||||
$this->cleanupCsvFile($filename);
|
$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");
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user