From e3d92582e00ef3f893a15eba2640b7d9b542ac48 Mon Sep 17 00:00:00 2001 From: Daeng Deni Mardaeni Date: Thu, 8 May 2025 17:20:52 +0700 Subject: [PATCH] feat(webstatement): tambahkan fitur ekspor data kartu ATM ke CSV - Menambahkan `BiayaKartuController` untuk mengelola data kartu ATM. - Menyediakan endpoint baru `biaya-kartu.index` untuk mengunduh data kartu ATM dalam format CSV. - Implementasi fungsi untuk: - Mengambil data kartu ATM yang memenuhi syarat dari database. - Menentukan biaya kartu berdasarkan tipe kartu. - Membuat file CSV dengan format khusus dan membersihkan konten dari tanda kutip ganda. - Memodifikasi logika pembaruan data di `BiayaKartu.php` untuk memperbaiki penempatan pemanggilan fungsi `updateOrCreate`. Signed-off-by: Daeng Deni Mardaeni --- app/Http/Controllers/BiayaKartuController.php | 199 ++++++++++++++++++ app/Jobs/BiayaKartu.php | 4 +- routes/web.php | 1 + 3 files changed, 202 insertions(+), 2 deletions(-) create mode 100644 app/Http/Controllers/BiayaKartuController.php diff --git a/app/Http/Controllers/BiayaKartuController.php b/app/Http/Controllers/BiayaKartuController.php new file mode 100644 index 0000000..d40167e --- /dev/null +++ b/app/Http/Controllers/BiayaKartuController.php @@ -0,0 +1,199 @@ + 3000, + 'CLAS' => 3000, + 'SILVER' => 5000, + 'SILV' => 5000, + 'GOLD' => 10000 + ]; + private const CSV_FILENAME = 'atmcards.csv'; + + /** + * Display a listing of the resource. + */ + public function index() + { + $filename = $this->generateAtmCardCsv(); + return response()->download($filename)->deleteFileAfterSend(true); + } + + /** + * Generate CSV file with ATM card data + * + * @return string Filename of generated CSV + */ + private function generateAtmCardCsv() + : string + { + $cards = $this->getEligibleAtmCards(); + $filename = self::CSV_FILENAME; + + $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, '|'); + } + } finally { + fclose($handle); + } + + $this->cleanupCsvFile($filename); + + return $filename; + } + + /** + * Get eligible ATM cards from database + * + * @return \Illuminate\Database\Eloquent\Collection + */ + private function getEligibleAtmCards() + { + return Atmcard::where('crsts', 1) + ->whereNotNull('accflag') + ->whereNotNull('branch') + ->whereNotNull('currency') + ->whereIn('ctdesc', array_keys(self::DEFAULT_FEES)) + ->get(); + } + + /** + * Determine fee for a card based on its type + * + * @param Atmcard $card + * + * @return int + */ + private function determineCardFee(Atmcard $card) + : int + { + if ($card->fee) { + return $card->fee; + } + + return self::DEFAULT_FEES[$card->ctdesc] ?? 0; + } + + /** + * Create CSV row data for a card + * + * @param Atmcard $card + * @param int $fee + * + * @return array + */ + private function createCsvRow(Atmcard $card, int $fee) + : array + { + $today = date('Ymd'); + + return [ + '', + $card->accflag, + $card->currency ?? 'IDR', + $fee, + 'PL65129', + '', + '', + $card->branch, + $today, + $today, + '', + '', + 'ADMIN FEE ATM::' . $card->crdno . '::' . $today, + '', + '', + '', + '', + '', + '', + 'AC' + ]; + } + + /** + * Remove double quotes from CSV file + * + * @param string $filename + * + * @return void + */ + private function cleanupCsvFile(string $filename) + : void + { + $fileContent = file_get_contents($filename); + if ($fileContent === false) { + throw new RuntimeException("Tidak dapat membaca file CSV: $filename"); + } + + $fileContent = str_replace('"', '', $fileContent); + + if (file_put_contents($filename, $fileContent) === false) { + throw new RuntimeException("Tidak dapat menulis ke file CSV: $filename"); + } + } + + /** + * Show the form for creating a new resource. + */ + public function create() + { + return view('webstatement::create'); + } + + /** + * Store a newly created resource in storage. + */ + public function store(Request $request) + { + // + } + + /** + * Show the specified resource. + */ + public function show($id) + { + return view('webstatement::show'); + } + + /** + * Show the form for editing the specified resource. + */ + public function edit($id) + { + return view('webstatement::edit'); + } + + /** + * Update the specified resource in storage. + */ + public function update(Request $request, $id) + { + // + } + + /** + * Remove the specified resource from storage. + */ + public function destroy($id) + { + // + } + } diff --git a/app/Jobs/BiayaKartu.php b/app/Jobs/BiayaKartu.php index 290e92a..af5c795 100644 --- a/app/Jobs/BiayaKartu.php +++ b/app/Jobs/BiayaKartu.php @@ -101,14 +101,14 @@ // Perbarui data kartu dasar $cardData = $this->getCardBaseData($card); + Atmcard::updateOrCreate(['crdno' => $card->crdno], $cardData); + // Periksa jika perlu memperbarui branch dan currency if ($this->needBranchAndCurrencyUpdate($existingCard)) { $accountInfo = $this->getAccountInfo($card->accflag); if($accountInfo['responseCode'] === '00') { $this->updateBranchAndCurrency($card->crdno, $accountInfo); } - } else { - Atmcard::updateOrCreate(['crdno' => $card->crdno], $cardData); } } catch (Exception $e) { Log::warning("Gagal memproses kartu {$card->crdno}: " . $e->getMessage()); diff --git a/routes/web.php b/routes/web.php index db2a773..e75ffdb 100644 --- a/routes/web.php +++ b/routes/web.php @@ -37,5 +37,6 @@ Route::middleware(['auth'])->group(function () { }); Route::get('migrasi', [MigrasiController::class, 'index'])->name('migrasi.index'); +Route::get('biaya-kartu', [BiayaKartuController::class, 'index'])->name('biaya-kartu.index'); Route::get('/stmt-entries/{accountNumber}', [MigrasiController::class, 'getStmtEntryByAccount']);