Files
webstatement/app/Jobs/ProcessStmtEntryDataJob.php
Daeng Deni Mardaeni 429df7035c fix(webstatement): optimalkan proses pembersihan data dan update balance
Melakukan perbaikan serta optimisasi pada proses pembersihan data dan pengolahan saldo akun.

Perubahan utama:
- **ProcessStmtEntryDataJob**:
    - Mengganti `str_replace` dengan `preg_replace` untuk pembersihan `trans_reference`, sehingga lebih fleksibel dalam menghapus karakter setelah `\` (termasuk keseluruhan pattern yang lebih kompleks).
    - Mengevaluasi penghapusan potensi substring logic yang tidak digunakan, memperkuat pembersihan menjadi lebih konsisten.
- **ProcessAccountDataJob**:
    - Mengganti penggunaan `firstOrNew` dan `save` dengan `updateOrInsert` untuk mengurangi jumlah query ke database.
    - Menambahkan pembuatan data `created_at` dan `updated_at` untuk memastikan data yang di-update memiliki timestamp konsisten.
    - Menjamin default value pada `actual_balance` dan `cleared_balance` apabila data masukan kosong agar sistem tetap dapat berjalan tanpa error.

Perubahan ini dilakukan untuk meningkatkan efisiensi proses pengolahan data, mengurangi overhead query, serta memastikan data yang diproses tetap konsisten dan lebih aman.

Signed-off-by: Daeng Deni Mardaeni <ddeni05@gmail.com>
2025-05-26 08:28:28 +07:00

117 lines
4.6 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 Illuminate\Support\Facades\Storage;
use Modules\Webstatement\Models\StmtEntry;
class ProcessStmtEntryDataJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $period;
/**
* Create a new job instance.
*/
public function __construct(string $period = '')
{
$this->period = $period;
}
/**
* Execute the job.
*/
public function handle()
: void
{
try {
set_time_limit(24 * 60 * 60);
$disk = Storage::disk('sftpStatement');
$processedCount = 0;
$errorCount = 0;
if (empty($this->period)) {
Log::warning('No period provided for statement entry data processing');
return;
}
// Construct the filename based on the period folder name
$filename = "{$this->period}.ST.STMT.ENTRY.csv";
$filePath = "{$this->period}/$filename";
Log::info("Processing statement entry file: $filePath");
if (!$disk->exists($filePath)) {
Log::warning("File not found: $filePath");
return;
}
// Create a temporary local copy of the file
$tempFilePath = storage_path("app/temp_$filename");
file_put_contents($tempFilePath, $disk->get($filePath));
$handle = fopen($tempFilePath, "r");
if ($handle !== false) {
$headers = (new StmtEntry())->getFillable();
$rowCount = 0;
while (($row = fgetcsv($handle, 0, "~")) !== false) {
$rowCount++;
if (count($headers) === count($row)) {
$data = array_combine($headers, $row);
try {
if ($data['stmt_entry_id'] !== 'stmt_entry_id') {
// Bersihkan trans_reference dari \\BNK jika ada
if (isset($data['trans_reference'])) {
$cleanedRef = preg_replace('/\\\\.*$/', '', $data['trans_reference']);
//$firstTwoChars = substr($cleanedRef, 0, 2);
/*$data['trans_reference'] = (in_array($firstTwoChars, ['FT', 'TT','AA']))
? substr($cleanedRef, 0, 12)
: $cleanedRef;*/
$data['trans_reference'] = $cleanedRef;
}
StmtEntry::updateOrCreate(
['stmt_entry_id' => $data['stmt_entry_id']],
$data
);
$processedCount++;
}
} catch (Exception $e) {
$errorCount++;
Log::error("Error processing Statement Entry at row $rowCount in $filePath: " . $e->getMessage());
}
} else {
Log::warning("Row $rowCount in $filePath has incorrect column count. Expected: " . count($headers) . ", Got: " . count($row));
}
}
fclose($handle);
Log::info("Completed processing $filePath. Processed $processedCount records with $errorCount errors.");
// Clean up the temporary file
unlink($tempFilePath);
} else {
Log::error("Unable to open file: $filePath");
}
Log::info("Statement Entry data processing completed. Total processed: $processedCount, Total errors: $errorCount");
} catch (Exception $e) {
Log::error('Error in ProcessStmtEntryDataJob: ' . $e->getMessage());
throw $e;
}
}
}