feat(jobs): tambahkan job untuk memproses data pelanggan
- Menambahkan kelas ProcessCustomerDataJob untuk memproses file CSV cusromer. - Menggunakan metode updateOrCreate untuk menyimpan atau memperbarui data customer. - Menangani pengecualian jika file tidak ditemukan atau tidak dapat dibuka. - Mengatur batas waktu eksekusi job menjadi 24 jam.
This commit is contained in:
60
app/Jobs/ProcessCustomerDataJob.php
Normal file
60
app/Jobs/ProcessCustomerDataJob.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?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\TempCustomer;
|
||||
|
||||
class ProcessCustomerDataJob implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function handle()
|
||||
{
|
||||
$filePath = storage_path('app/20240901.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 TempCustomer())->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
|
||||
);
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user