Files
webstatement/app/Jobs/ProcessAtmTransactionJob.php
Daeng Deni Mardaeni 38987ce8e3 refactor(webstatement): remove _parameter folder skipping logic from processing jobs
- Menghapus logika pengecekan dan pengabaian folder `_parameter` di seluruh job pemrosesan data berikut:
  - `ProcessAccountDataJob`
  - `ProcessAtmTransactionJob`
  - `ProcessBillDetailDataJob`
  - `ProcessCategoryDataJob`
  - `ProcessCompanyDataJob`
  - `ProcessCustomerDataJob`
  - `ProcessDataCaptureDataJob`
  - `ProcessFundsTransferDataJob`
  - `ProcessStmtEntryDataJob`
  - `ProcessTellerDataJob`
- Menghapus konstanta `PARAMETER_FOLDER` yang terkait dengan folder `_parameter` pada beberapa job.
- Membersihkan code redundancy yang tidak relevan untuk meningkatkan keterbacaan dan efisiensi.
- Logika ini dianggap tidak diperlukan lagi dalam proses pemrosesan data.

Signed-off-by: Daeng Deni Mardaeni <ddeni05@gmail.com>
2025-05-24 19:43:54 +07:00

213 lines
7.3 KiB
PHP

<?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 Illuminate\Support\Facades\Storage;
use Modules\Webstatement\Models\AtmTransaction;
class ProcessAtmTransactionJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
// Konstanta untuk nilai-nilai statis
private const FILE_EXTENSION = '.ST.ATM.TRANSACTION.csv';
private const CSV_DELIMITER = '~';
private const DISK_NAME = 'sftpStatement';
private const HEADER_MAP = [
'id' => 'transaction_id',
'card_acc_id' => 'card_acc_id',
'pan_number' => 'pan_number',
'txn_type' => 'txn_type',
'merchant_id' => 'merchant_id',
'txn_amount' => 'txn_amount',
'booking_date' => 'booking_date',
'trans_ref' => 'trans_ref',
'retrieval_ref_no' => 'retrieval_ref_no',
'stmt_nos' => 'stmt_nos',
'debit_acct_no' => 'debit_acct_no',
'credit_acct_no' => 'credit_acct_no',
'chrg_amount' => 'chrg_amount',
'value_date' => 'value_date',
'stan_no' => 'stan_no',
'trans_status' => 'trans_status',
'proc_code' => 'proc_code'
];
// Pemetaan bidang header ke kolom model
protected string $period;
/**
* Create a new job instance.
*/
public function __construct(string $period = '')
{
$this->period = $period;
}
/**
* Execute the job.
*/
public function handle()
: void
{
try {
set_time_limit(24 * 60 * 60);
if (empty($this->period)) {
Log::warning('No period provided for ATM transaction data processing');
return;
}
$stats = $this->processPeriodFile();
Log::info("ProcessAtmTransactionJob completed. Total processed: {$stats['processed']}, Total errors: {$stats['errors']}");
} catch (Exception $e) {
Log::error("Error in ProcessAtmTransactionJob: " . $e->getMessage());
throw $e;
}
}
/**
* Process a single period file
*/
private function processPeriodFile()
: array
{
$disk = Storage::disk(self::DISK_NAME);
$filename = $this->period . self::FILE_EXTENSION;
$filePath = "{$this->period}/$filename";
$processedCount = 0;
$errorCount = 0;
Log::info("Processing ATM transaction file: $filePath");
if (!$disk->exists($filePath)) {
Log::warning("File not found: $filePath");
return ['processed' => 0, 'errors' => 0];
}
$tempFilePath = $this->createTempFile($disk, $filePath, $filename);
$result = $this->processCSVFile($tempFilePath, $filePath);
$processedCount += $result['processed'];
$errorCount += $result['errors'];
// Clean up the temporary file
if (file_exists($tempFilePath)) {
unlink($tempFilePath);
}
Log::info("Completed processing $filePath. Processed {$result['processed']} records with {$result['errors']} errors.");
return [
'processed' => $processedCount,
'errors' => $errorCount
];
}
/**
* Create a temporary file for processing
*/
private function createTempFile($disk, string $filePath, string $filename)
: string
{
$tempFilePath = storage_path("app/temp_$filename");
file_put_contents($tempFilePath, $disk->get($filePath));
return $tempFilePath;
}
/**
* Process a CSV file and import data
*/
private function processCSVFile(string $tempFilePath, string $originalFilePath)
: array
{
$processedCount = 0;
$errorCount = 0;
$handle = fopen($tempFilePath, "r");
if ($handle === false) {
Log::error("Unable to open file: $originalFilePath");
return ['processed' => 0, 'errors' => 0];
}
// Get the headers from the first row
$headerRow = fgetcsv($handle, 0, self::CSV_DELIMITER);
if (!$headerRow) {
fclose($handle);
return ['processed' => 0, 'errors' => 0];
}
$rowCount = 0;
while (($row = fgetcsv($handle, 0, self::CSV_DELIMITER)) !== false) {
$rowCount++;
if (count($headerRow) !== count($row)) {
Log::warning("Row $rowCount in $originalFilePath has incorrect column count. Expected: " . count($headerRow) . ", Got: " . count($row));
continue;
}
$result = $this->processRow($headerRow, $row, $rowCount, $originalFilePath);
$processedCount += $result['processed'];
$errorCount += $result['errors'];
}
fclose($handle);
return [
'processed' => $processedCount,
'errors' => $errorCount
];
}
/**
* Process a single row from the CSV file
*/
private function processRow(array $headerRow, array $row, int $rowCount, string $filePath)
: array
{
// Combine the header row with the data row
$rawData = array_combine($headerRow, $row);
// Map the raw data to our model fields
$data = [];
foreach (self::HEADER_MAP as $csvField => $modelField) {
$data[$modelField] = $rawData[$csvField] ?? null;
}
// Skip header row if it was included in the data
if ($data['transaction_id'] === 'id') {
return ['processed' => 0, 'errors' => 0];
}
try {
// Format dates if needed
/*if (!empty($data['booking_date'])) {
$data['booking_date'] = date('Y-m-d H:i:s', strtotime($data['booking_date']));
}
if (!empty($data['value_date'])) {
$data['value_date'] = date('Y-m-d H:i:s', strtotime($data['value_date']));
}*/
// Create or update the record
AtmTransaction::updateOrCreate(
['transaction_id' => $data['transaction_id']],
$data
);
return ['processed' => 1, 'errors' => 0];
} catch (Exception $e) {
Log::error("Error processing row $rowCount in $filePath: " . $e->getMessage());
return ['processed' => 0, 'errors' => 1];
}
}
}