feat(webstatement): tambah fitur pemrosesan data kategori
- Menambahkan `ProcessCategoryDataJob` untuk memproses data kategori dari file CSV yang diambil melalui SFTP. - Membuat model `Category` dengan atribut-atribut: - `id_category` - `date_time` - `description` - `short_name` - `system_ind` - `record_status` - `co_code` - `curr_no` - `l_db_cr_ind` - `category_code` - Menambahkan endpoint baru `ProcessCategoryData` di `MigrasiController` untuk memanggil job pemrosesan data kategori. - Menambahkan migrasi untuk membuat tabel `categories` dengan kolom-kolom yang relevan. - Memperbaiki bug pada `ProcessStmtEntryDataJob` dengan menambahkan validasi khusus untuk menghindari pemrosesan baris header yang tidak valid. - Menghapus pemanggilan job yang tidak diperlukan di `MigrasiController`. - Mengupdate logika pemrosesan file untuk memastikan integritas data dalam job kategori. Signed-off-by: Daeng Deni Mardaeni <ddeni05@gmail.com>
This commit is contained in:
138
app/Jobs/ProcessCategoryDataJob.php
Normal file
138
app/Jobs/ProcessCategoryDataJob.php
Normal file
@@ -0,0 +1,138 @@
|
||||
<?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\Category;
|
||||
|
||||
class ProcessCategoryDataJob implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
protected $periods;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*/
|
||||
public function __construct(array $periods = [])
|
||||
{
|
||||
$this->periods = $periods;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*/
|
||||
public function handle()
|
||||
: void
|
||||
{
|
||||
try {
|
||||
set_time_limit(24 * 60 * 60);
|
||||
$disk = Storage::disk('sftpStatement');
|
||||
$processedCount = 0;
|
||||
$errorCount = 0;
|
||||
|
||||
if (empty($this->periods)) {
|
||||
Log::warning('No periods provided for category data processing');
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($this->periods as $period) {
|
||||
// Skip the _parameter folder
|
||||
if ($period === '_parameter') {
|
||||
Log::info("Skipping _parameter folder");
|
||||
continue;
|
||||
}
|
||||
|
||||
// Construct the filename based on the period folder name
|
||||
$filename = "$period.ST.CATEGORY.csv";
|
||||
$filePath = "$period/$filename";
|
||||
|
||||
Log::info("Processing category file: $filePath");
|
||||
|
||||
if (!$disk->exists($filePath)) {
|
||||
Log::warning("File not found: $filePath");
|
||||
continue;
|
||||
}
|
||||
|
||||
// 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");
|
||||
|
||||
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'
|
||||
];
|
||||
|
||||
$rowCount = 0;
|
||||
|
||||
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);
|
||||
|
||||
// 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");
|
||||
}
|
||||
}
|
||||
|
||||
Log::info("Category data processing completed. Total processed: $processedCount, Total errors: $errorCount");
|
||||
|
||||
} catch (Exception $e) {
|
||||
Log::error('Error in ProcessCategoryDataJob: ' . $e->getMessage());
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user