From 7e6bfded58c08e79fb33820b2a949fea18ef9a1b Mon Sep 17 00:00:00 2001 From: Daeng Deni Mardaeni Date: Wed, 29 Jan 2025 19:41:45 +0700 Subject: [PATCH] feat(jobs): tambahkan job untuk memproses data akun - Menambahkan kelas ProcessAccountDataJob untuk memproses data akun dari file CSV. - Mengimplementasikan logika untuk membaca file dan memperbarui atau membuat entri di tabel TempAccount. - Menangani kesalahan dan mencatat log jika terjadi masalah saat memproses data. - Menambahkan migrasi untuk membuat tabel temp_accounts dengan kolom yang diperlukan. --- app/Jobs/ProcessAccountDataJob.php | 68 +++++++++++++++++++ app/Models/TempAccount.php | 27 ++++++++ ...1_28_032418_create_temp_accounts_table.php | 36 ++++++++++ 3 files changed, 131 insertions(+) create mode 100644 app/Jobs/ProcessAccountDataJob.php create mode 100644 app/Models/TempAccount.php create mode 100644 database/migrations/2025_01_28_032418_create_temp_accounts_table.php diff --git a/app/Jobs/ProcessAccountDataJob.php b/app/Jobs/ProcessAccountDataJob.php new file mode 100644 index 0000000..6fd7cde --- /dev/null +++ b/app/Jobs/ProcessAccountDataJob.php @@ -0,0 +1,68 @@ +getFillable(); + while (($row = fgetcsv($handle, 0, ";")) !== false) { + if (count($headers) === count($row)) { + $data = array_combine($headers, $row); + + try { + TempAccount::updateOrCreate(['account_number' => $data['account_number']], $data); + } catch (Exception $e) { + Log::error('Error processing Account: ' . $e->getMessage()); + } + } + } + fclose($handle); + } else { + throw new Exception("Unable to open file: {$filePath}"); + } + } catch (Exception $e) { + Log::error('Error in ProcessAccountDataJob: ' . $e->getMessage()); + throw $e; + } + } + } diff --git a/app/Models/TempAccount.php b/app/Models/TempAccount.php new file mode 100644 index 0000000..2776a63 --- /dev/null +++ b/app/Models/TempAccount.php @@ -0,0 +1,27 @@ +id(); + + foreach ($fieldArray as $field) { + $field = trim($field); + $table->text($field)->nullable(); + } + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('temp_accounts'); + } +};