Files
webstatement/app/Jobs/ProcessCompanyDataJob.php
Daeng Deni Mardaeni 8abb8f6901 fix(webstatement): perbaikan pengecekan variabel periode dalam processing data
Memperbaiki pengecekan variabel `$this->period` dari `empty()` menjadi `=== ''` pada berbagai job dalam modul Webstatement untuk memastikan logika validasi berfungsi dengan baik dan lebih eksplisit.

- Job yang diperbaiki:
  - `ProcessAccountDataJob`
  - `ProcessArrangementDataJob`
  - `ProcessAtmTransactionJob`
  - `ProcessBillDetailDataJob`
  - `ProcessCategoryDataJob`
  - `ProcessCompanyDataJob`
  - `ProcessCustomerDataJob`
  - `ProcessDataCaptureDataJob`
  - `ProcessFundsTransferDataJob`
  - `ProcessStmtEntryDataJob`
  - `ProcessTellerDataJob`

- Memastikan validasi terhadap variabel `$this->period` hanya memfokuskan pada apakah nilainya adalah string kosong (`''`).
- Menambahkan kejelasan pada logging apabila periode tidak tersedia pada setiap job.
- Perubahan ini bertujuan untuk menghindari potensi false negative pada pengecekan kondisi `empty()` yang dapat menyebabkan logika tidak berjalan sebagaimana mestinya.

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

158 lines
6.3 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\Basicdata\Models\Branch;
class ProcessCompanyDataJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $period;
protected $filename;
/**
* 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 ($this->period === '')) {
Log::warning('No period provided for company data processing');
return;
}
// Construct the filepath based on the period folder name
$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");
return;
}
// Create a temporary local copy of the file
$tempFilePath = storage_path("app/temp_{$this->filename}");
file_put_contents($tempFilePath, $disk->get($filePath));
$handle = fopen($tempFilePath, "r");
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'
];
// Field mapping from CSV to Branch model
$fieldMapping = [
'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'
];
$rowCount = 0;
while (($row = fgetcsv($handle, 0, "~")) !== false) {
$rowCount++;
// Skip header row if it exists
if ($rowCount === 1 && (strtolower($row[0]) === 'id' || strtolower($row[2]) === 'company_code')) {
continue;
}
if (count($csvHeaders) === count($row)) {
$csvData = array_combine($csvHeaders, $row);
// Map CSV data to Branch model fields
$branchData = [];
foreach ($fieldMapping as $csvField => $modelField) {
if (isset($csvData[$csvField])) {
// Convert string boolean values to actual booleans for boolean fields
if (in_array($modelField, ['l_vendor_atm', 'l_vendor_cpc'])) {
$branchData[$modelField] = filter_var($csvData[$csvField], FILTER_VALIDATE_BOOLEAN);
} else {
$branchData[$modelField] = $csvData[$csvField];
}
}
}
try {
if (!empty($branchData['code'])) {
Branch::updateOrCreate(
['code' => $branchData['code']],
$branchData
);
$processedCount++;
}
} catch (Exception $e) {
$errorCount++;
Log::error("Error processing Company data at row $rowCount in $filePath: " . $e->getMessage());
}
} else {
Log::warning("Row $rowCount in $filePath has incorrect column count. Expected: " . count($csvHeaders) . ", 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("Company data processing completed. Total processed: $processedCount, Total errors: $errorCount");
} catch (Exception $e) {
Log::error('Error in ProcessCompanyDataJob: ' . $e->getMessage());
throw $e;
}
}
}