feat(jobs): tambahkan job untuk memproses data entry statement
- Menambahkan kelas ProcessStmtEntryDataJob untuk memproses file CSV yang berisi data entry statement. - Membuat model TempStmtEntry untuk menyimpan data entry statement ke dalam database. - Menambahkan migrasi untuk membuat tabel temp_stmt_entry dengan kolom yang sesuai.
This commit is contained in:
68
app/Jobs/ProcessStmtEntryDataJob.php
Normal file
68
app/Jobs/ProcessStmtEntryDataJob.php
Normal file
@@ -0,0 +1,68 @@
|
||||
<?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\TempStmtEntry;
|
||||
|
||||
class ProcessStmtEntryDataJob 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.STMT.ENTRY.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 TempStmtEntry())->getFillable();
|
||||
while (($row = fgetcsv($handle, 0, "/")) !== false) {
|
||||
if (count($headers) === count($row)) {
|
||||
$data = array_combine($headers, $row);
|
||||
|
||||
try {
|
||||
TempStmtEntry::updateOrCreate(['_id' => $data['_id']], $data);
|
||||
} catch (Exception $e) {
|
||||
Log::error('Error processing stmt entry: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
fclose($handle);
|
||||
} else {
|
||||
throw new Exception("Unable to open file: {$filePath}");
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
Log::error('Error in ProcessStmtEntryDataJob: ' . $e->getMessage());
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user