refactor(jobs): simplify jobs and controllers by replacing period array with single period parameter
- Mengganti parameter `$periods` (array) menjadi `$period` (string) pada semua Job terkait: `ProcessCustomerDataJob`, `ProcessFundsTransferDataJob, etc`. - Menyederhanakan operasi loop dalam proses data dengan hanya memproses satu periode per eksekusi Job. - Memodifikasi fungsi controller di `MigrasiController` agar sesuai dengan perubahan parameter dari array ke string. - Menambahkan pengamanan jika `$period` kosong atau bernilai '_parameter' untuk mencegah proses yang tidak diperlukan. - Mengurangi duplikasi kode dengan mengeliminasi metode yang mengelola array periode dan menggantinya dengan pendekatan tunggal. Signed-off-by: Daeng Deni Mardaeni <ddeni05@gmail.com>
This commit is contained in:
@@ -1,63 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Webstatement\Jobs;
|
||||
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;
|
||||
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\TempBillDetail;
|
||||
|
||||
class ProcessBillDetailDataJob implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
protected $periods;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*/
|
||||
public function __construct(array $periods = [])
|
||||
class ProcessBillDetailDataJob implements ShouldQueue
|
||||
{
|
||||
$this->periods = $periods;
|
||||
}
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
try {
|
||||
set_time_limit(24 * 60 * 60);
|
||||
$disk = Storage::disk('sftpStatement');
|
||||
$processedCount = 0;
|
||||
$errorCount = 0;
|
||||
protected string $period;
|
||||
|
||||
if (empty($this->periods)) {
|
||||
Log::warning('No periods provided for bill detail data processing');
|
||||
return;
|
||||
}
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*/
|
||||
public function __construct(string $period = '')
|
||||
{
|
||||
$this->period = $period;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
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 bill detail data processing');
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($this->periods as $period) {
|
||||
// Skip the _parameter folder
|
||||
if ($period === '_parameter') {
|
||||
if ($this->period === '_parameter') {
|
||||
Log::info("Skipping _parameter folder");
|
||||
continue;
|
||||
return;
|
||||
}
|
||||
|
||||
// Construct the filename based on the period folder name
|
||||
$filename = "$period.ST.AA.BILL.DETAILS.csv";
|
||||
$filePath = "$period/$filename";
|
||||
$filename = "{$this->period}.ST.AA.BILL.DETAILS.csv";
|
||||
$filePath = "{$this->period}/$filename";
|
||||
|
||||
Log::info("Processing bill detail file: $filePath");
|
||||
|
||||
if (!$disk->exists($filePath)) {
|
||||
Log::warning("File not found: $filePath");
|
||||
continue;
|
||||
return;
|
||||
}
|
||||
|
||||
// Create a temporary local copy of the file
|
||||
@@ -67,7 +66,7 @@ class ProcessBillDetailDataJob implements ShouldQueue
|
||||
$handle = fopen($tempFilePath, "r");
|
||||
|
||||
if ($handle !== false) {
|
||||
$headers = (new TempBillDetail())->getFillable();
|
||||
$headers = (new TempBillDetail())->getFillable();
|
||||
$rowCount = 0;
|
||||
|
||||
while (($row = fgetcsv($handle, 0, "~")) !== false) {
|
||||
@@ -78,7 +77,7 @@ class ProcessBillDetailDataJob implements ShouldQueue
|
||||
try {
|
||||
if (isset($data['_id']) && $data['_id'] !== '_id') {
|
||||
TempBillDetail::updateOrCreate(
|
||||
['_id' => $data['_id']], // Fixed the syntax error here
|
||||
['_id' => $data['_id']],
|
||||
$data
|
||||
);
|
||||
$processedCount++;
|
||||
@@ -100,13 +99,12 @@ class ProcessBillDetailDataJob implements ShouldQueue
|
||||
} 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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user