feat(webstatement): optimalkan proses migrasi data SFTP
- Menambahkan parameter `$periods` untuk mendukung proses dinamis berdasarkan folder periode dalam SFTP.
- Memperkenalkan penggunaan `Storage::disk('sftpStatement')` untuk membaca file dari SFTP.
- Menyortir folder periode secara menurun berdasarkan tanggal.
- Memproses tiap entitas data (Customer, Account, Arrangement, Bill Details, dll.) berdasarkan file dalam folder periode.
- Menambah log untuk mencatat jumlah proses yang berhasil, baris error, dan peringatan tentang ketidaksesuaian jumlah kolom.
- Menyisipkan mekanisme untuk mengabaikan folder `_parameter`.
- Mengubah pendekatan dari membaca file lokal menjadi menggunakan file sementara (temp).
- Memperbaiki bug pada metode yang sebelumnya menggunakan jalur file statis.
- Memastikan penanganan file CSV yang lebih fleksibel, termasuk kasus di mana jumlah kolom dalam baris melebihi ekspektasi.
- Menambah error handling untuk mencatat baris-baris bermasalah dan menghindari interupsi proses.
Signed-off-by: Daeng Deni Mardaeni <ddeni05@gmail.com>
This commit is contained in:
@@ -1,70 +1,112 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Webstatement\Jobs;
|
||||
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 Modules\Webstatement\Models\TempTransaction;
|
||||
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\TempTransaction;
|
||||
|
||||
class ProcessTransactionDataJob implements ShouldQueue
|
||||
class ProcessTransactionDataJob implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
protected $periods;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*/
|
||||
public function __construct(array $periods = [])
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
$this->periods = $periods;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
/**
|
||||
* Execute the job.
|
||||
*/
|
||||
public function handle(): void
|
||||
{
|
||||
try {
|
||||
set_time_limit(24 * 60 * 60);
|
||||
$disk = Storage::disk('sftpStatement');
|
||||
$processedCount = 0;
|
||||
$errorCount = 0;
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*/
|
||||
public function handle()
|
||||
: void
|
||||
{
|
||||
$filePath = storage_path('app/20250207.ST.TRANSACTION.csv'); // Adjust this path as needed
|
||||
try {
|
||||
if (!file_exists($filePath)) {
|
||||
throw new Exception("File not found: $filePath");
|
||||
if (empty($this->periods)) {
|
||||
Log::warning('No periods provided for transaction data processing');
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($this->periods as $period) {
|
||||
// Skip the _parameter folder
|
||||
if ($period === '_parameter') {
|
||||
Log::info("Skipping _parameter folder");
|
||||
continue;
|
||||
}
|
||||
|
||||
set_time_limit(24 * 60 * 60);
|
||||
// Construct the filename based on the period folder name
|
||||
$filename = "$period.ST.TRANSACTION.csv";
|
||||
$filePath = "$period/$filename";
|
||||
|
||||
if (!file_exists($filePath)) {
|
||||
throw new Exception("File not found: {$filePath}");
|
||||
Log::info("Processing transaction file: $filePath");
|
||||
|
||||
if (!$disk->exists($filePath)) {
|
||||
Log::warning("File not found: $filePath");
|
||||
continue;
|
||||
}
|
||||
|
||||
$handle = fopen($filePath, "r");
|
||||
// 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 TempTransaction())->getFillable();
|
||||
$rowCount = 0;
|
||||
|
||||
while (($row = fgetcsv($handle, 0, "~")) !== false) {
|
||||
$rowCount++;
|
||||
|
||||
if (count($headers) === count($row)) {
|
||||
$data = array_combine($headers, $row);
|
||||
|
||||
try {
|
||||
if($data['_id'] !== 'id') {
|
||||
TempTransaction::updateOrCreate(['_id' => $data['_id']], $data);
|
||||
if (isset($data['_id']) && $data['_id'] !== 'id' && $data['_id'] !== '_id') {
|
||||
TempTransaction::updateOrCreate(
|
||||
['_id' => $data['_id']],
|
||||
$data
|
||||
);
|
||||
$processedCount++;
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
Log::error('Error processing transactions: ' . $e->getMessage());
|
||||
$errorCount++;
|
||||
Log::error("Error processing Transaction 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 {
|
||||
throw new Exception("Unable to open file: {$filePath}");
|
||||
Log::error("Unable to open file: $filePath");
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
Log::error('Error in ProcessTransctionDataJob: ' . $e->getMessage());
|
||||
throw $e;
|
||||
}
|
||||
|
||||
Log::info("Transaction data processing completed. Total processed: $processedCount, Total errors: $errorCount");
|
||||
|
||||
} catch (Exception $e) {
|
||||
Log::error('Error in ProcessTransactionDataJob: ' . $e->getMessage());
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user