refactor(jobs): simplify jobs and controllers by replacing period array with single period parameter

- Mengganti parameter `$periods` (array) menjadi `$period` (string) pada semua Job terkait: `ProcessCustomerDataJob`, `ProcessFundsTransferDataJob, etc`.
- Menyederhanakan operasi loop dalam proses data dengan hanya memproses satu periode per eksekusi Job.
- Memodifikasi fungsi controller di `MigrasiController` agar sesuai dengan perubahan parameter dari array ke string.
- Menambahkan pengamanan jika `$period` kosong atau bernilai '_parameter' untuk mencegah proses yang tidak diperlukan.
- Mengurangi duplikasi kode dengan mengeliminasi metode yang mengelola array periode dan menggantinya dengan pendekatan tunggal.

Signed-off-by: Daeng Deni Mardaeni <ddeni05@gmail.com>
This commit is contained in:
Daeng Deni Mardaeni
2025-05-24 19:40:40 +07:00
parent 85b8bfa07b
commit cd447eb019
9 changed files with 761 additions and 799 deletions

View File

@@ -16,15 +16,15 @@
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $periods;
protected $period;
protected $filename;
/**
* Create a new job instance.
*/
public function __construct(array $periods = [], string $filename = "ST.DATA.CAPTURE.csv")
public function __construct(string $period = '', string $filename = "ST.DATA.CAPTURE.csv")
{
$this->periods = $periods;
$this->period = $period;
$this->filename = $filename;
}
@@ -40,139 +40,137 @@
$processedCount = 0;
$errorCount = 0;
if (empty($this->periods)) {
Log::warning('No periods provided for data capture processing');
if (empty($this->period)) {
Log::warning('No period provided for data capture processing');
return;
}
foreach ($this->periods as $period) {
// Skip the _parameter folder
if ($period === '_parameter') {
Log::info("Skipping _parameter folder");
continue;
}
// Skip the _parameter folder
if ($this->period === '_parameter') {
Log::info("Skipping _parameter folder");
return;
}
// Construct the filepath based on the period folder name
$fileName = "$period.$this->filename";
$filePath = "$period/$fileName";
// Construct the filepath based on the period folder name
$fileName = "{$this->period}.{$this->filename}";
$filePath = "{$this->period}/$fileName";
Log::info("Processing data capture file: $filePath");
Log::info("Processing data capture file: $filePath");
if (!$disk->exists($filePath)) {
Log::warning("File not found: $filePath");
continue;
}
if (!$disk->exists($filePath)) {
Log::warning("File not found: $filePath");
return;
}
// Create a temporary local copy of the file
$tempFilePath = storage_path("app/temp_{$this->filename}");
file_put_contents($tempFilePath, $disk->get($filePath));
// Create a temporary local copy of the file
$tempFilePath = storage_path("app/temp_{$this->filename}");
file_put_contents($tempFilePath, $disk->get($filePath));
$handle = fopen($tempFilePath, "r");
$handle = fopen($tempFilePath, "r");
if ($handle !== false) {
// CSV headers from the file
$csvHeaders = [
'id',
'account_number',
'sign',
'amount_lcy',
'transaction_code',
'their_reference',
'narrative',
'pl_category',
'customer_id',
'account_officer',
'product_category',
'value_date',
'currency',
'amount_fcy',
'exchange_rate',
'neg_ref_no',
'position_type',
'our_reference',
'reversal_marker',
'exposure_date',
'currency_market',
'iblc_country',
'last_version',
'otor_version',
'department_code',
'dealer_desk',
'bank_sort_cde',
'cheque_number',
'accounting_date',
'contingent_acct',
'cheq_type',
'tfs_reference',
'accounting_company',
'stmt_no',
'curr_no',
'inputter',
'authoriser',
'co_code',
'date_time'
];
if ($handle !== false) {
// CSV headers from the file
$csvHeaders = [
'id',
'account_number',
'sign',
'amount_lcy',
'transaction_code',
'their_reference',
'narrative',
'pl_category',
'customer_id',
'account_officer',
'product_category',
'value_date',
'currency',
'amount_fcy',
'exchange_rate',
'neg_ref_no',
'position_type',
'our_reference',
'reversal_marker',
'exposure_date',
'currency_market',
'iblc_country',
'last_version',
'otor_version',
'department_code',
'dealer_desk',
'bank_sort_cde',
'cheque_number',
'accounting_date',
'contingent_acct',
'cheq_type',
'tfs_reference',
'accounting_company',
'stmt_no',
'curr_no',
'inputter',
'authoriser',
'co_code',
'date_time'
];
$rowCount = 0;
$rowCount = 0;
while (($row = fgetcsv($handle, 0, "~")) !== false) {
$rowCount++;
while (($row = fgetcsv($handle, 0, "~")) !== false) {
$rowCount++;
// Skip header row if it exists
if ($rowCount === 1 && strtolower($row[0]) === 'id') {
continue;
}
if (count($csvHeaders) === count($row)) {
$data = array_combine($csvHeaders, $row);
try {
// Format dates if they exist
foreach (['value_date', 'exposure_date', 'accounting_date'] as $dateField) {
if (!empty($data[$dateField])) {
try {
$data[$dateField] = date('Y-m-d', strtotime($data[$dateField]));
} catch (Exception $e) {
// If date parsing fails, keep the original value
Log::warning("Failed to parse date for $dateField: {$data[$dateField]}");
}
}
}
// Format datetime if it exists
if (!empty($data['date_time'])) {
try {
$data['date_time'] = date('Y-m-d H:i:s', strtotime($data['date_time']));
} catch (Exception $e) {
// If datetime parsing fails, keep the original value
Log::warning("Failed to parse datetime for date_time: {$data['date_time']}");
}
}
if (!empty($data['id'])) {
DataCapture::updateOrCreate(
['id' => $data['id']],
$data
);
$processedCount++;
}
} catch (Exception $e) {
$errorCount++;
Log::error("Error processing Data Capture at row $rowCount in $filePath: " . $e->getMessage());
}
} else {
Log::warning("Row $rowCount in $filePath has incorrect column count. Expected: " . count($csvHeaders) . ", Got: " . count($row));
}
// Skip header row if it exists
if ($rowCount === 1 && strtolower($row[0]) === 'id') {
continue;
}
fclose($handle);
Log::info("Completed processing $filePath. Processed $processedCount records with $errorCount errors.");
if (count($csvHeaders) === count($row)) {
$data = array_combine($csvHeaders, $row);
// Clean up the temporary file
unlink($tempFilePath);
} else {
Log::error("Unable to open file: $filePath");
try {
// Format dates if they exist
foreach (['value_date', 'exposure_date', 'accounting_date'] as $dateField) {
if (!empty($data[$dateField])) {
try {
$data[$dateField] = date('Y-m-d', strtotime($data[$dateField]));
} catch (Exception $e) {
// If date parsing fails, keep the original value
Log::warning("Failed to parse date for $dateField: {$data[$dateField]}");
}
}
}
// Format datetime if it exists
if (!empty($data['date_time'])) {
try {
$data['date_time'] = date('Y-m-d H:i:s', strtotime($data['date_time']));
} catch (Exception $e) {
// If datetime parsing fails, keep the original value
Log::warning("Failed to parse datetime for date_time: {$data['date_time']}");
}
}
if (!empty($data['id'])) {
DataCapture::updateOrCreate(
['id' => $data['id']],
$data
);
$processedCount++;
}
} catch (Exception $e) {
$errorCount++;
Log::error("Error processing Data Capture at row $rowCount in $filePath: " . $e->getMessage());
}
} else {
Log::warning("Row $rowCount in $filePath has incorrect column count. Expected: " . count($csvHeaders) . ", Got: " . count($row));
}
}
fclose($handle);
Log::info("Completed processing $filePath. Processed $processedCount records with $errorCount errors.");
// Clean up the temporary file
unlink($tempFilePath);
} else {
Log::error("Unable to open file: $filePath");
}
Log::info("Data capture processing completed. Total processed: $processedCount, Total errors: $errorCount");