feat(transactions): tambahkan job dan model untuk memproses data transaksi

- 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.
This commit is contained in:
Daeng Deni Mardaeni
2025-01-29 19:42:28 +07:00
parent 7e6bfded58
commit f7362dbc5a
3 changed files with 126 additions and 0 deletions

View 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 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;
}
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace Modules\Webstatement\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
// use Modules\Webstatement\Database\Factories\TempTransactionFactory;
class TempTransaction extends Model
{
use HasFactory;
protected $table = 'temp_transactions';
/**
* The attributes that are mass assignable.
*/
protected $fillable = ['_id','date_time','transaction_code','narrative','data_capture','cheque_ind','mandatory_ref_no','debit_credit_ind','charge_key','immediate_charge','default_value_date','exposure_date','turnover_charge','swift_narrative','unit_cost','initiation','short_desc','stmt_narr','stmt_narr_ref','force_def_exp_date','narr_type','local_ref','combine_prod_cat','combine_system_id','combine_txn','exp_key','chq_type','update_int_pools'];
// protected static function newFactory(): TempTransactionFactory
// {
// // return TempTransactionFactory::new();
// }
}

View File

@@ -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,date_time,transaction_code,narrative,data_capture,cheque_ind,mandatory_ref_no,debit_credit_ind,charge_key,immediate_charge,default_value_date,exposure_date,turnover_charge,swift_narrative,unit_cost,initiation,short_desc,stmt_narr,stmt_narr_ref,force_def_exp_date,narr_type,local_ref,combine_prod_cat,combine_system_id,combine_txn,exp_key,chq_type,update_int_pools';
$fieldsArray = explode(',', $fields);
Schema::create('temp_transactions', function (Blueprint $table) use ($fieldsArray) {
$table->id();
foreach ($fieldsArray as $field) {
$field = trim($field);
$table->text($field)->nullable();
}
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('temp_transactions');
}
};