feat(job): perbarui logika pemrosesan data customer

- Ganti model dari TempCustomer ke Customer untuk pemrosesan data.
- Perbarui jalur file CSV yang digunakan untuk pemrosesan.
- Tambahkan penanganan kesalahan untuk mencatat kesalahan saat memproses customer.
This commit is contained in:
Daeng Deni Mardaeni
2025-04-08 18:56:39 +07:00
parent 90d7c017f0
commit 79c4aa1d39

View File

@@ -8,7 +8,8 @@ use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Modules\Webstatement\Models\TempCustomer;
use Modules\Webstatement\Models\Customer;
use Illuminate\Support\Facades\Log;
class ProcessCustomerDataJob implements ShouldQueue
{
@@ -21,7 +22,7 @@ class ProcessCustomerDataJob implements ShouldQueue
public function handle()
{
$filePath = storage_path('app/20240901.ST.CUSTOMER.csv'); // Adjust this path as needed
$filePath = storage_path('app/20250207.ST.CUSTOMER.csv'); // Adjust this path as needed
try {
if (!file_exists($filePath)) {
throw new Exception("File not found: $filePath");
@@ -36,15 +37,20 @@ class ProcessCustomerDataJob implements ShouldQueue
$handle = fopen($filePath, "r");
if ($handle !== false) {
$headers = (new TempCustomer())->getFillable();
$headers = (new Customer())->getFillable();
while (($row = fgetcsv($handle, 0, "~")) !== false) {
unset($row[0]); // Remove the first empty column if present
if (count($headers) === count($row)) {
$data = array_combine($headers, $row);
TempCustomer::updateOrCreate(
['customer_code' => $data['customer_code']], // key to find existing record
$data // data to update or create
);
try {
if( $data['customer_code'] !== 'customer_code') {
$customer = Customer::firstOrNew(['customer_code' => $data['customer_code']]);
$customer->fill($data);
$customer->save();
}
}catch (Exception $e) {
Log::error('Error processing Customer: ' . $e->getMessage());
}
}
}
fclose($handle);