- Membuat kelas ProcessBillDetailDataJob untuk memproses file CSV detail tagihan. - Menangani pembacaan file dan pemrosesan data dengan penanganan kesalahan. - Menggunakan model TempBillDetail untuk menyimpan data yang diproses.
62 lines
1.8 KiB
PHP
62 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace Modules\Webstatement\Jobs;
|
|
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Queue\SerializesModels;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Modules\Webstatement\Models\TempBillDetail;
|
|
use Exception;
|
|
class ProcessBillDetailDataJob implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
/**
|
|
* Create a new job instance.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Execute the job.
|
|
*/
|
|
public function handle()
|
|
{
|
|
$filePath = storage_path('app/20240901.ST.AA.BILL.DETAILS.csv');
|
|
try {
|
|
if (!file_exists($filePath)) {
|
|
throw new Exception("File not found: $filePath");
|
|
}
|
|
|
|
set_time_limit(24 * 60 * 60);
|
|
|
|
$handle = fopen($filePath, "r");
|
|
|
|
if ($handle !== false) {
|
|
$headers = (new TempBillDetail())->getFillable();
|
|
while (($row = fgetcsv($handle, 0, ";")) !== false) {
|
|
if (count($headers) === count($row)) {
|
|
$data = array_combine($headers, $row);
|
|
|
|
try {
|
|
TempBillDetail::upadteOrCreate(['_id',$data['_id']],$data);
|
|
} catch (Exception $e) {
|
|
\Log::error('Error processing bill detail: ' . $e->getMessage());
|
|
}
|
|
}
|
|
}
|
|
fclose($handle);
|
|
} else {
|
|
throw new Exception("Unable to open file: {$filePath}");
|
|
}
|
|
} catch (Exception $e) {
|
|
\Log::error('Error in ProcessBillDetailDataJob: ' . $e->getMessage());
|
|
throw $e;
|
|
}
|
|
}
|
|
}
|