Files
webstatement/app/Jobs/ProcessCustomerDataJob.php
Daeng Deni Mardaeni 79c4aa1d39 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.
2025-04-08 18:56:57 +07:00

67 lines
2.2 KiB
PHP

<?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 Modules\Webstatement\Models\Customer;
use Illuminate\Support\Facades\Log;
class ProcessCustomerDataJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function __construct()
{
}
public function handle()
{
$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");
}
set_time_limit(24 * 60 * 60);
if (!file_exists($filePath)) {
throw new Exception("File not found: {$filePath}");
}
$handle = fopen($filePath, "r");
if ($handle !== false) {
$headers = (new Customer())->getFillable();
while (($row = fgetcsv($handle, 0, "~")) !== false) {
if (count($headers) === count($row)) {
$data = array_combine($headers, $row);
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);
} else {
throw new Exception("Unable to open file: {$filePath}");
}
return response()->json(['message' => 'Data Customer processing job has been successfully']);
} catch (Exception $e) {
return response()->json(['error' => $e->getMessage()], 500);
}
}
}