Files
webstatement/app/Jobs/ProcessArrangementDataJob.php
Daeng Deni Mardaeni 5a66cb6ade feat(jobs): tambahkan job untuk memproses data arrangement
- Membuat kelas ProcessArrangementDataJob untuk memproses file CSV data arrangement.
- Menangani pembacaan file dan pembaruan data menggunakan model TempArrangement.
- Menangani kesalahan dengan logging untuk memudahkan debugging.
2025-02-18 16:36:20 +07:00

69 lines
2.3 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 Log;
use Modules\Webstatement\Models\TempArrangement;
class ProcessArrangementDataJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*/
public function __construct()
{
//
}
/**
* Execute the job.
*/
public function handle()
: void
{
$filePath = storage_path('app/20240901.ST.AA.ARRANGEMENT.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 TempArrangement())->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);
TempArrangement::updateOrCreate(
['arrangement_id' => $data['arrangement_id']], // key to find existing record
$data // data to update or create
);
}
}
fclose($handle);
} else {
throw new Exception("Unable to open file: {$filePath}");
}
} catch (Exception $e) {
Log::error('Error in ProcessArrangementDataJob: ' . $e->getMessage());
throw $e;
}
}
}