feat(webstatement): tambahkan job untuk generate file CSV biaya kartu ATM
- Menambahkan job `GenerateBiayaKartuCsvJob` dengan kemampuan untuk membuat file CSV berisi data biaya kartu ATM. - File CSV dihasilkan berdasarkan data kartu ATM yang memenuhi kriteria tertentu dari database. - Penentuan biaya kartu berdasarkan jenis kartu atau nilai default jika tidak ada biaya spesifik. - Melakukan pembersihan isi file CSV untuk menghapus tanda kutip ganda. - Logging disertakan untuk mencatat keberhasilan atau kegagalan proses. Signed-off-by: Daeng Deni Mardaeni <ddeni05@gmail.com>
This commit is contained in:
178
app/Jobs/GenerateBiayaKartuCsvJob.php
Normal file
178
app/Jobs/GenerateBiayaKartuCsvJob.php
Normal file
@@ -0,0 +1,178 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Webstatement\Jobs;
|
||||
|
||||
use Exception;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Modules\Webstatement\Models\Atmcard;
|
||||
use RuntimeException;
|
||||
|
||||
class GenerateBiayaKartuCsvJob implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
private const DEFAULT_FEES = [
|
||||
'CLASSIC' => 3000,
|
||||
'CLAS' => 3000,
|
||||
'SILVER' => 5000,
|
||||
'SILV' => 5000,
|
||||
'GOLD' => 10000
|
||||
];
|
||||
private const CSV_FILENAME = 'atmcards.csv';
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*/
|
||||
public function handle()
|
||||
: void
|
||||
{
|
||||
try {
|
||||
$filename = $this->generateAtmCardCsv();
|
||||
Log::info('File CSV biaya kartu ATM berhasil dibuat: ' . $filename);
|
||||
} catch (Exception $e) {
|
||||
Log::error('Gagal membuat file CSV biaya kartu ATM: ' . $e->getMessage(), [
|
||||
'file' => $e->getFile(),
|
||||
'line' => $e->getLine()
|
||||
]);
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate CSV file with ATM card data
|
||||
*
|
||||
* @return string Filename of generated CSV
|
||||
*/
|
||||
private function generateAtmCardCsv()
|
||||
: string
|
||||
{
|
||||
$cards = $this->getEligibleAtmCards();
|
||||
$filename = storage_path('app/' . 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user