Files
webstatement/app/Jobs/ProcessBillDetailDataJob.php
Daeng Deni Mardaeni 39e356e2ff refactor(webstatement): update data processing and migration logic
- Menonaktifkan pemanggilan fungsi-fungsi yang tidak diperlukan untuk data processing di `MigrasiController`:
  - `processTransactionData`, `processStmtNarrFormatData`, `processAccountData`, dan `ProcessCompanyData`.
- Mengubah argumen di beberapa pemanggilan fungsi untuk data processing agar lebih spesifik ke periode tertentu:
  - Mengganti `$periods` menjadi `['20250512']` untuk `processArrangementData`, `processBillDetailData`, `processFundsTransferData`, dan `processStmtEntryData`.
- Memodifikasi logika skipping folder `_parameter` pada `ProcessFtTxnTypeConditionJob` dengan mengomentari proses pengecekan.
- Mengubah delimiter `fgetcsv` pada `ProcessBillDetailDataJob` dari `;` menjadi `~`.
- Menambahkan file migrasi baru untuk mengubah tipe kolom pada tabel `temp_fund_transfer`:
  - Mengubah semua kolom pada tabel menjadi tipe `text` untuk mendukung data yang lebih besar.
  - Menyediakan metode `down` untuk rollback tipe data kembali ke `string`.

Perubahan ini bertujuan untuk meningkatkan efisiensi proses data, mendukung fleksibilitas data lebih besar pada migrasi database, serta membuka jalan untuk refaktor atau penghapusan fungsi yang tidak digunakan.

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

113 lines
3.9 KiB
PHP

<?php
namespace Modules\Webstatement\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Modules\Webstatement\Models\TempBillDetail;
use Exception;
class ProcessBillDetailDataJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $periods;
/**
* Create a new job instance.
*/
public function __construct(array $periods = [])
{
$this->periods = $periods;
}
/**
* Execute the job.
*/
public function handle()
{
try {
set_time_limit(24 * 60 * 60);
$disk = Storage::disk('sftpStatement');
$processedCount = 0;
$errorCount = 0;
if (empty($this->periods)) {
Log::warning('No periods provided for bill detail data processing');
return;
}
foreach ($this->periods as $period) {
// Skip the _parameter folder
if ($period === '_parameter') {
Log::info("Skipping _parameter folder");
continue;
}
// Construct the filename based on the period folder name
$filename = "$period.ST.AA.BILL.DETAILS.csv";
$filePath = "$period/$filename";
Log::info("Processing bill detail file: $filePath");
if (!$disk->exists($filePath)) {
Log::warning("File not found: $filePath");
continue;
}
// 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 TempBillDetail())->getFillable();
$rowCount = 0;
while (($row = fgetcsv($handle, 0, "~")) !== false) {
$rowCount++;
if (count($headers) === count($row)) {
$data = array_combine($headers, $row);
try {
if (isset($data['_id']) && $data['_id'] !== '_id') {
TempBillDetail::updateOrCreate(
['_id' => $data['_id']], // Fixed the syntax error here
$data
);
$processedCount++;
}
} catch (Exception $e) {
$errorCount++;
Log::error("Error processing Bill Detail 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("Bill Detail data processing completed. Total processed: $processedCount, Total errors: $errorCount");
} catch (Exception $e) {
Log::error('Error in ProcessBillDetailDataJob: ' . $e->getMessage());
throw $e;
}
}
}