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:
@@ -8,7 +8,8 @@ use Illuminate\Contracts\Queue\ShouldQueue;
|
|||||||
use Illuminate\Foundation\Bus\Dispatchable;
|
use Illuminate\Foundation\Bus\Dispatchable;
|
||||||
use Illuminate\Queue\InteractsWithQueue;
|
use Illuminate\Queue\InteractsWithQueue;
|
||||||
use Illuminate\Queue\SerializesModels;
|
use Illuminate\Queue\SerializesModels;
|
||||||
use Modules\Webstatement\Models\TempCustomer;
|
use Modules\Webstatement\Models\Customer;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
|
||||||
class ProcessCustomerDataJob implements ShouldQueue
|
class ProcessCustomerDataJob implements ShouldQueue
|
||||||
{
|
{
|
||||||
@@ -21,7 +22,7 @@ class ProcessCustomerDataJob implements ShouldQueue
|
|||||||
|
|
||||||
public function handle()
|
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 {
|
try {
|
||||||
if (!file_exists($filePath)) {
|
if (!file_exists($filePath)) {
|
||||||
throw new Exception("File not found: $filePath");
|
throw new Exception("File not found: $filePath");
|
||||||
@@ -36,15 +37,20 @@ class ProcessCustomerDataJob implements ShouldQueue
|
|||||||
$handle = fopen($filePath, "r");
|
$handle = fopen($filePath, "r");
|
||||||
|
|
||||||
if ($handle !== false) {
|
if ($handle !== false) {
|
||||||
$headers = (new TempCustomer())->getFillable();
|
$headers = (new Customer())->getFillable();
|
||||||
while (($row = fgetcsv($handle, 0, "~")) !== false) {
|
while (($row = fgetcsv($handle, 0, "~")) !== false) {
|
||||||
unset($row[0]); // Remove the first empty column if present
|
|
||||||
if (count($headers) === count($row)) {
|
if (count($headers) === count($row)) {
|
||||||
$data = array_combine($headers, $row);
|
$data = array_combine($headers, $row);
|
||||||
TempCustomer::updateOrCreate(
|
try {
|
||||||
['customer_code' => $data['customer_code']], // key to find existing record
|
if( $data['customer_code'] !== 'customer_code') {
|
||||||
$data // data to update or create
|
$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);
|
fclose($handle);
|
||||||
|
|||||||
Reference in New Issue
Block a user