feat(jobs): tambahkan job untuk memproses data format narasi
- Menambahkan kelas ProcessStmtNarrFormatDataJob untuk memproses file CSV. - Menambahkan model TempStmtNarrFormat untuk menyimpan data format narasi. - Membuat migrasi untuk tabel temp_stmt_narr_format dengan atribut yang diperlukan.
This commit is contained in:
67
app/Jobs/ProcessStmtNarrFormatDataJob.php
Normal file
67
app/Jobs/ProcessStmtNarrFormatDataJob.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?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\TempStmtNarrFormat;
|
||||
|
||||
class ProcessStmtNarrFormatDataJob 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.NARR.FORMAT.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 TempStmtNarrFormat())->getFillable();
|
||||
while (($row = fgetcsv($handle, 0, ";")) !== false) {
|
||||
if (count($headers) === count($row)) {
|
||||
$data = array_combine($headers, $row);
|
||||
try {
|
||||
TempStmtNarrFormat::updateOrCreate(['_id' => $data['_id']], $data);
|
||||
} catch (Exception $e) {
|
||||
Log::error('Error processing stmt narr format: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
fclose($handle);
|
||||
} else {
|
||||
throw new Exception("Unable to open file: {$filePath}");
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
Log::error('Error in ProcessStmtNarrFormatDataJob: ' . $e->getMessage());
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
24
app/Models/TempStmtNarrFormat.php
Normal file
24
app/Models/TempStmtNarrFormat.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Webstatement\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
// use Modules\Webstatement\Database\Factories\TempStmtNarrFormatFactory;
|
||||
|
||||
class TempStmtNarrFormat extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $table = 'temp_stmt_narr_format';
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*/
|
||||
protected $fillable = ['_id','narr_format_id','description','text_data','line_length','max_lines','wrap_truncate','record_status','curr_no','inputter','date_time','authoriser','co_code','dept_code','auditor_code','audit_date_time','k_file','k_format','file','format'];
|
||||
|
||||
// protected static function newFactory(): TempStmtNarrFormatFactory
|
||||
// {
|
||||
// // return TempStmtNarrFormatFactory::new();
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
$fields = '_id,narr_format_id,description,text_data,line_length,max_lines,wrap_truncate,record_status,curr_no,inputter,date_time,authoriser,co_code,dept_code,auditor_code,audit_date_time,k_file,k_format,file,format';
|
||||
|
||||
$fieldsArray = explode(',', $fields);
|
||||
|
||||
Schema::create('temp_stmt_narr_format', function (Blueprint $table) use ($fieldsArray) {
|
||||
$table->id();
|
||||
foreach ($fieldsArray as $field) {
|
||||
$field = trim($field);
|
||||
$table->string($field)->nullable();
|
||||
}
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('temp_stmt_narr_format');
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user