fix(webstatement): perbaikan parameter dan refactor pada ProcessCompanyDataJob

- Mengubah parameter pada pemanggilan `$this->ProcessCompanyData()` dari array menjadi string untuk konsistensi data.
- Mengubah properti `protected` pada `ProcessCompanyDataJob` dari `$periods` menjadi `$period` untuk menggunakan string daripada array.
- Menyesuaikan constructor `__construct` untuk menerima parameter string `$period` alih-alih array `$periods`.
- Memperbaiki mekanisme validasi pada `handle()`, mengganti pengecekan array kosong `$this->periods` menjadi validasi string `$this->period` dengan nilai kosong.
- Menghapus iterasi `foreach` untuk mengakomodasi perubahan dari array ke string sederhana.
- Memastikan mapping data CSV tetap konsisten dan menambahkan identasi untuk peningkatan keterbacaan.
- Memperbaiki nama variabel dan properti agar lebih eksplisit (`$periods` menjadi `$period`, `$fileName` menjadi `$fileName`, dsb.).
- Menambahkan logging yang lebih jelas mengenai proses data dan kondisi error pada job `ProcessCompanyDataJob`.

Signed-off-by: Daeng Deni Mardaeni <ddeni05@gmail.com>
This commit is contained in:
Daeng Deni Mardaeni
2025-05-24 19:22:30 +07:00
parent bf7206f927
commit 85b8bfa07b
2 changed files with 74 additions and 65 deletions

View File

@@ -253,7 +253,7 @@ class MigrasiController extends Controller
}
$this->ProcessCategoryData($period);
$this->ProcessCompanyData([$period]);
$this->ProcessCompanyData($period);
$this->processCustomerData([$period]);
$this->processAccountData([$period]);

View File

@@ -1,65 +1,65 @@
<?php
namespace Modules\Webstatement\Jobs;
namespace Modules\Webstatement\Jobs;
use Exception;
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\Basicdata\Models\Branch;
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\Basicdata\Models\Branch;
class ProcessCompanyDataJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $periods;
protected $filename;
/**
* Create a new job instance.
*/
public function __construct(array $periods = [], string $filename = "ST.COMPANY.csv")
class ProcessCompanyDataJob implements ShouldQueue
{
$this->periods = $periods;
$this->filename = $filename;
}
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Execute the job.
*/
public function handle(): void
{
try {
set_time_limit(24 * 60 * 60);
$disk = Storage::disk('sftpStatement');
$processedCount = 0;
$errorCount = 0;
protected $period;
protected $filename;
if (empty($this->periods)) {
Log::warning('No periods provided for company data processing');
return;
}
/**
* Create a new job instance.
*/
public function __construct(string $period = '', string $filename = "ST.COMPANY.csv")
{
$this->period = $period;
$this->filename = $filename;
}
/**
* 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 company 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 filepath based on the period folder name
$fileName = "$period.$this->filename";
$filePath = "$period/$fileName";
$fileName = "{$this->period}.{$this->filename}";
$filePath = "{$this->period}/$fileName";
Log::info("Processing company file: $filePath");
if (!$disk->exists($filePath)) {
Log::warning("File not found: $filePath");
continue;
return;
}
// Create a temporary local copy of the file
@@ -71,24 +71,34 @@ class ProcessCompanyDataJob implements ShouldQueue
if ($handle !== false) {
// CSV headers from the file
$csvHeaders = [
'id', 'date_time', 'company_code', 'company_name', 'name_address',
'mnemonic', 'customer_company', 'customer_mnemonic', 'company_group',
'curr_no', 'co_code', 'l_vendor_atm', 'l_vendor_cpc'
'id',
'date_time',
'company_code',
'company_name',
'name_address',
'mnemonic',
'customer_company',
'customer_mnemonic',
'company_group',
'curr_no',
'co_code',
'l_vendor_atm',
'l_vendor_cpc'
];
// Field mapping from CSV to Branch model
$fieldMapping = [
'company_code' => 'code',
'company_name' => 'name',
'name_address' => 'address',
'mnemonic' => 'mnemonic',
'customer_company' => 'customer_company',
'company_code' => 'code',
'company_name' => 'name',
'name_address' => 'address',
'mnemonic' => 'mnemonic',
'customer_company' => 'customer_company',
'customer_mnemonic' => 'customer_mnemonic',
'company_group' => 'company_group',
'curr_no' => 'curr_no',
'co_code' => 'co_code',
'l_vendor_atm' => 'l_vendor_atm',
'l_vendor_cpc' => 'l_vendor_cpc'
'company_group' => 'company_group',
'curr_no' => 'curr_no',
'co_code' => 'co_code',
'l_vendor_atm' => 'l_vendor_atm',
'l_vendor_cpc' => 'l_vendor_cpc'
];
$rowCount = 0;
@@ -142,13 +152,12 @@ class ProcessCompanyDataJob implements ShouldQueue
} else {
Log::error("Unable to open file: $filePath");
}
Log::info("Company data processing completed. Total processed: $processedCount, Total errors: $errorCount");
} catch (Exception $e) {
Log::error('Error in ProcessCompanyDataJob: ' . $e->getMessage());
throw $e;
}
Log::info("Company data processing completed. Total processed: $processedCount, Total errors: $errorCount");
} catch (Exception $e) {
Log::error('Error in ProcessCompanyDataJob: ' . $e->getMessage());
throw $e;
}
}
}