- 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>
104 lines
3.9 KiB
PHP
104 lines
3.9 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\Webstatement\Models\Customer;
|
|
|
|
class ProcessCustomerDataJob implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
protected $period;
|
|
|
|
public function __construct(string $period = '')
|
|
{
|
|
$this->period = $period;
|
|
}
|
|
|
|
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 customer data processing');
|
|
return;
|
|
}
|
|
|
|
// Skip the _parameter folder
|
|
if ($this->period === '_parameter') {
|
|
Log::info("Skipping _parameter folder");
|
|
return;
|
|
}
|
|
|
|
// Construct the filename based on the period folder name
|
|
$filename = "{$this->period}.ST.CUSTOMER.csv";
|
|
$filePath = "{$this->period}/$filename";
|
|
|
|
Log::info("Processing customer 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_$filename");
|
|
file_put_contents($tempFilePath, $disk->get($filePath));
|
|
|
|
$handle = fopen($tempFilePath, "r");
|
|
|
|
if ($handle !== false) {
|
|
$headers = (new Customer())->getFillable();
|
|
$rowCount = 0;
|
|
|
|
while (($row = fgetcsv($handle, 0, "~")) !== false) {
|
|
$rowCount++;
|
|
|
|
if (count($headers) === count($row)) {
|
|
$data = array_combine($headers, $row);
|
|
try {
|
|
if ($data['customer_code'] !== 'customer_code') {
|
|
$customer = Customer::firstOrNew(['customer_code' => $data['customer_code']]);
|
|
$customer->fill($data);
|
|
$customer->save();
|
|
$processedCount++;
|
|
}
|
|
} catch (Exception $e) {
|
|
$errorCount++;
|
|
Log::error("Error processing Customer 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("Customer data processing completed. Total processed: $processedCount, Total errors: $errorCount");
|
|
|
|
} catch (Exception $e) {
|
|
Log::error('Error in ProcessCustomerDataJob: ' . $e->getMessage());
|
|
throw $e;
|
|
}
|
|
}
|
|
}
|