From d4ef7280ce8b90ff73b0f68fd3aef1ebd1d50811 Mon Sep 17 00:00:00 2001 From: Daeng Deni Mardaeni Date: Fri, 9 May 2025 09:07:39 +0700 Subject: [PATCH] feat(webstatement): tambahkan command dan job untuk generate CSV biaya kartu ATM - Tambahkan `GenerateBiayaKartuCsvCommand` untuk membuat file CSV biaya kartu ATM melalui console command. - Implementasikan job `GenerateBiayaKartuCsvJob` sebagai pengganti proses manual pembuatan CSV di controller. - Hapus logika pembuatan CSV manual di `BiayaKartuController`. - Update scheduler untuk menjalankan command baru (`webstatement:generate-biaya-kartu-csv`) setiap tanggal 15 pukul 00:00. - Perbarui waktu schedule command `webstatement:generate-biaya-kartu` menjadi pukul 22:00 setiap tanggal 14. Signed-off-by: Daeng Deni Mardaeni --- app/Console/GenerateBiayaKartuCsvCommand.php | 45 +++++ app/Http/Controllers/BiayaKartuController.php | 191 +----------------- app/Providers/WebstatementServiceProvider.php | 9 +- 3 files changed, 62 insertions(+), 183 deletions(-) create mode 100644 app/Console/GenerateBiayaKartuCsvCommand.php diff --git a/app/Console/GenerateBiayaKartuCsvCommand.php b/app/Console/GenerateBiayaKartuCsvCommand.php new file mode 100644 index 0000000..4e4fa5d --- /dev/null +++ b/app/Console/GenerateBiayaKartuCsvCommand.php @@ -0,0 +1,45 @@ +info('Memulai proses pembuatan file CSV biaya kartu ATM'); + + try { + GenerateBiayakartuCsvJob::dispatch(); + $this->info('Job pembuatan file CSV biaya kartu ATM berhasil dijadwalkan'); + Log::info('Job pembuatan file CSV biaya kartu ATM berhasil dijadwalkan oleh scheduler'); + return Command::SUCCESS; + } catch (\Exception $e) { + $this->error('Terjadi kesalahan: ' . $e->getMessage()); + Log::error('Gagal menjalankan job pembuatan file CSV biaya kartu ATM: ' . $e->getMessage()); + return Command::FAILURE; + } + } + } diff --git a/app/Http/Controllers/BiayaKartuController.php b/app/Http/Controllers/BiayaKartuController.php index e1d3075..2bf45cf 100644 --- a/app/Http/Controllers/BiayaKartuController.php +++ b/app/Http/Controllers/BiayaKartuController.php @@ -4,199 +4,26 @@ use App\Http\Controllers\Controller; use Illuminate\Http\Request; + use Modules\Webstatement\Jobs\GenerateBiayaKartuCsvJob; use Modules\Webstatement\Models\Atmcard; use RuntimeException; class BiayaKartuController extends Controller { - private const DEFAULT_FEES = [ - 'CLASSIC' => 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(); + // Memicu job untuk dijalankan dan menunggu hasilnya + $job = new GenerateBiayaKartuCsvJob(); + $filename = $job->handle(); + + // Alternatif jika Anda ingin menjalankannya secara asinkron + // GenerateBiayaAtmCsvJob::dispatch(); + // return response()->json(['message' => 'Proses pembuatan file CSV sedang berjalan di background']); + 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') - ->where('accflag', '!=', '') - ->whereNotNull('branch') - ->where('branch', '!=', '') - ->whereNotNull('currency') - ->where('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/Providers/WebstatementServiceProvider.php b/app/Providers/WebstatementServiceProvider.php index b37fa25..ae60745 100644 --- a/app/Providers/WebstatementServiceProvider.php +++ b/app/Providers/WebstatementServiceProvider.php @@ -48,7 +48,9 @@ class WebstatementServiceProvider extends ServiceProvider protected function registerCommands(): void { $this->commands([ + GenerateBiayakartuCommand::class, GenerateBiayakartuCommand::class + ]); } @@ -59,9 +61,14 @@ class WebstatementServiceProvider extends ServiceProvider { $schedule = $this->app->make(Schedule::class); $schedule->command('webstatement:generate-biaya-kartu') - ->monthlyOn(14, '18:00') + ->monthlyOn(14, '22:00') ->appendOutputTo(storage_path('logs/biaya-kartu-scheduler.log')); + $schedule->command('webstatement:generate-biaya-kartu-csv') + ->monthlyOn(15, '00:00') + ->appendOutputTo(storage_path('logs/biaya-kartu-csv-scheduler.log')); + + } /**