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->ProcessCategoryData($period);
$this->ProcessCompanyData([$period]); $this->ProcessCompanyData($period);
$this->processCustomerData([$period]); $this->processCustomerData([$period]);
$this->processAccountData([$period]); $this->processAccountData([$period]);

View File

@@ -1,65 +1,65 @@
<?php <?php
namespace Modules\Webstatement\Jobs; namespace Modules\Webstatement\Jobs;
use Exception; use Exception;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use Modules\Basicdata\Models\Branch; use Modules\Basicdata\Models\Branch;
class ProcessCompanyDataJob implements ShouldQueue 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")
{ {
$this->periods = $periods; use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
$this->filename = $filename;
}
/** protected $period;
* Execute the job. protected $filename;
*/
public function handle(): void
{
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 company data processing'); * Create a new job instance.
return; */
} 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 // Skip the _parameter folder
if ($period === '_parameter') { if ($this->period === '_parameter') {
Log::info("Skipping _parameter folder"); Log::info("Skipping _parameter folder");
continue; return;
} }
// Construct the filepath based on the period folder name // Construct the filepath based on the period folder name
$fileName = "$period.$this->filename"; $fileName = "{$this->period}.{$this->filename}";
$filePath = "$period/$fileName"; $filePath = "{$this->period}/$fileName";
Log::info("Processing company file: $filePath"); Log::info("Processing company file: $filePath");
if (!$disk->exists($filePath)) { if (!$disk->exists($filePath)) {
Log::warning("File not found: $filePath"); Log::warning("File not found: $filePath");
continue; return;
} }
// Create a temporary local copy of the file // Create a temporary local copy of the file
@@ -71,24 +71,34 @@ class ProcessCompanyDataJob implements ShouldQueue
if ($handle !== false) { if ($handle !== false) {
// CSV headers from the file // CSV headers from the file
$csvHeaders = [ $csvHeaders = [
'id', 'date_time', 'company_code', 'company_name', 'name_address', 'id',
'mnemonic', 'customer_company', 'customer_mnemonic', 'company_group', 'date_time',
'curr_no', 'co_code', 'l_vendor_atm', 'l_vendor_cpc' '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 // Field mapping from CSV to Branch model
$fieldMapping = [ $fieldMapping = [
'company_code' => 'code', 'company_code' => 'code',
'company_name' => 'name', 'company_name' => 'name',
'name_address' => 'address', 'name_address' => 'address',
'mnemonic' => 'mnemonic', 'mnemonic' => 'mnemonic',
'customer_company' => 'customer_company', 'customer_company' => 'customer_company',
'customer_mnemonic' => 'customer_mnemonic', 'customer_mnemonic' => 'customer_mnemonic',
'company_group' => 'company_group', 'company_group' => 'company_group',
'curr_no' => 'curr_no', 'curr_no' => 'curr_no',
'co_code' => 'co_code', 'co_code' => 'co_code',
'l_vendor_atm' => 'l_vendor_atm', 'l_vendor_atm' => 'l_vendor_atm',
'l_vendor_cpc' => 'l_vendor_cpc' 'l_vendor_cpc' => 'l_vendor_cpc'
]; ];
$rowCount = 0; $rowCount = 0;
@@ -142,13 +152,12 @@ class ProcessCompanyDataJob implements ShouldQueue
} else { } else {
Log::error("Unable to open file: $filePath"); 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;
} }
} }
}