- Menambahkan job `ProcessTransactionDataJob` untuk memproses file CSV transaksi. - Membuat model `TempTransaction` untuk menyimpan data transaksi sementara. - Menambahkan migrasi untuk tabel `temp_transactions` dengan atribut yang diperlukan.
69 lines
2.2 KiB
PHP
69 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 Illuminate\Support\Facades\Log;
|
|
use Modules\Webstatement\Models\TempTransaction;
|
|
|
|
class ProcessTransactionDataJob 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.TRANSACTION.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 TempTransaction())->getFillable();
|
|
while (($row = fgetcsv($handle, 0, ";")) !== false) {
|
|
if (count($headers) === count($row)) {
|
|
$data = array_combine($headers, $row);
|
|
|
|
try {
|
|
TempTransaction::updateOrCreate(['_id' => $data['_id']], $data);
|
|
} catch (Exception $e) {
|
|
Log::error('Error processing transactions: ' . $e->getMessage());
|
|
}
|
|
}
|
|
}
|
|
fclose($handle);
|
|
} else {
|
|
throw new Exception("Unable to open file: {$filePath}");
|
|
}
|
|
} catch (Exception $e) {
|
|
Log::error('Error in ProcessTransctionDataJob: ' . $e->getMessage());
|
|
throw $e;
|
|
}
|
|
}
|
|
}
|