From bf7206f927db099e1eef43bc84695cd4f313face Mon Sep 17 00:00:00 2001 From: Daeng Deni Mardaeni Date: Sat, 24 May 2025 19:19:33 +0700 Subject: [PATCH] refactor(webstatement): ubah parameter periode dari array menjadi string - Mengubah parameter pada metode `ProcessCategoryData` di `MigrasiController` dari array menjadi string untuk keseragaman dengan metode lainnya. - Memperbarui konstruksi parameter pada instansi `ProcessCategoryDataJob` untuk menerima tipe data string sebagai pengganti array. - Menghilangkan iterasi array `periods` pada `ProcessCategoryDataJob` dan menerapkan logika langsung pada single `period`. - Menyesuaikan validasi periode untuk mengabaikan folder `_parameter` dalam proses. - Memperlihatkan log lebih spesifik jika file tidak ditemukan, atau format kolom tidak sesuai ekspektasi. Signed-off-by: Daeng Deni Mardaeni --- app/Http/Controllers/MigrasiController.php | 2 +- app/Jobs/ProcessCategoryDataJob.php | 146 ++++++++++----------- 2 files changed, 73 insertions(+), 75 deletions(-) diff --git a/app/Http/Controllers/MigrasiController.php b/app/Http/Controllers/MigrasiController.php index 1909a3f..5af5eeb 100644 --- a/app/Http/Controllers/MigrasiController.php +++ b/app/Http/Controllers/MigrasiController.php @@ -252,7 +252,7 @@ class MigrasiController extends Controller return response()->json(["message" => "Period {$period} folder not found in SFTP storage"], 404); } - $this->ProcessCategoryData([$period]); + $this->ProcessCategoryData($period); $this->ProcessCompanyData([$period]); $this->processCustomerData([$period]); diff --git a/app/Jobs/ProcessCategoryDataJob.php b/app/Jobs/ProcessCategoryDataJob.php index 0fffdd0..35dbc1d 100644 --- a/app/Jobs/ProcessCategoryDataJob.php +++ b/app/Jobs/ProcessCategoryDataJob.php @@ -16,14 +16,14 @@ { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; - protected $periods; + protected $period; /** * Create a new job instance. */ - public function __construct(array $periods = []) + public function __construct(string $period = '') { - $this->periods = $periods; + $this->period = $period; } /** @@ -38,94 +38,92 @@ $processedCount = 0; $errorCount = 0; - if (empty($this->periods)) { - Log::warning('No periods provided for category data processing'); + if (empty($this->period)) { + Log::warning('No period provided for category data 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 filename based on the period folder name - $filename = "$period.ST.CATEGORY.csv"; - $filePath = "$period/$filename"; + // Construct the filename based on the period folder name + $filename = "{$this->period}.ST.CATEGORY.csv"; + $filePath = "{$this->period}/$filename"; - Log::info("Processing category file: $filePath"); + Log::info("Processing category 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_$filename"); - file_put_contents($tempFilePath, $disk->get($filePath)); + // Create a temporary local copy of the file + $tempFilePath = storage_path("app/temp_$filename"); + file_put_contents($tempFilePath, $disk->get($filePath)); - $handle = fopen($tempFilePath, "r"); + $handle = fopen($tempFilePath, "r"); - if ($handle !== false) { - // Get the headers from the first row - $headerRow = fgetcsv($handle, 0, "~"); + if ($handle !== false) { + // Get the headers from the first row + $headerRow = fgetcsv($handle, 0, "~"); - // Map the headers to our model fields - $headerMap = [ - 'id' => 'id_category', - 'date_time' => 'date_time', - 'description' => 'description', - 'short_name' => 'short_name', - 'system_ind' => 'system_ind', - 'record_status' => 'record_status', - 'co_code' => 'co_code', - 'curr_no' => 'curr_no', - 'l_db_cr_ind' => 'l_db_cr_ind', - 'category_code' => 'category_code' - ]; + // Map the headers to our model fields + $headerMap = [ + 'id' => 'id_category', + 'date_time' => 'date_time', + 'description' => 'description', + 'short_name' => 'short_name', + 'system_ind' => 'system_ind', + 'record_status' => 'record_status', + 'co_code' => 'co_code', + 'curr_no' => 'curr_no', + 'l_db_cr_ind' => 'l_db_cr_ind', + 'category_code' => 'category_code' + ]; - $rowCount = 0; + $rowCount = 0; - while (($row = fgetcsv($handle, 0, "~")) !== false) { - $rowCount++; + while (($row = fgetcsv($handle, 0, "~")) !== false) { + $rowCount++; - if (count($headerRow) === count($row)) { - // Combine the header row with the data row - $rawData = array_combine($headerRow, $row); + if (count($headerRow) === count($row)) { + // Combine the header row with the data row + $rawData = array_combine($headerRow, $row); - // Map the raw data to our model fields - $data = []; - foreach ($headerMap as $csvField => $modelField) { - $data[$modelField] = $rawData[$csvField] ?? null; - } - - try { - // Skip header row if it was included in the data - if ($data['id_category'] !== 'id') { - // Use firstOrNew instead of updateOrCreate - $category = Category::firstOrNew(['id_category' => $data['id_category']]); - $category->fill($data); - $category->save(); - $processedCount++; - } - } catch (Exception $e) { - $errorCount++; - Log::error("Error processing Category at row $rowCount in $filePath: " . $e->getMessage()); - } - } else { - Log::warning("Row $rowCount in $filePath has incorrect column count. Expected: " . count($headerRow) . ", Got: " . count($row)); + // Map the raw data to our model fields + $data = []; + foreach ($headerMap as $csvField => $modelField) { + $data[$modelField] = $rawData[$csvField] ?? null; } + + try { + // Skip header row if it was included in the data + if ($data['id_category'] !== 'id') { + // Use firstOrNew instead of updateOrCreate + $category = Category::firstOrNew(['id_category' => $data['id_category']]); + $category->fill($data); + $category->save(); + $processedCount++; + } + } catch (Exception $e) { + $errorCount++; + Log::error("Error processing Category at row $rowCount in $filePath: " . $e->getMessage()); + } + } else { + Log::warning("Row $rowCount in $filePath has incorrect column count. Expected: " . count($headerRow) . ", 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"); } + + 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("Category data processing completed. Total processed: $processedCount, Total errors: $errorCount");