- Refactor `ProcessArrangementDataJob`:
- Mengubah parameter dari array periods menjadi string period untuk simplifikasi proses.
- Mengadaptasi logika proses file CSV dari multiple periods menjadi single period.
- Menghapus logika iterasi folder `_parameter` dan menyederhanakan nama file dengan menggunakan single period.
- Menambahkan validasi dan penanganan error jika file tidak ditemukan atau tidak dapat dibuka.
- Menyederhanakan proses membaca dan memproses row dari file CSV dengan pendekatan baru.
- Memperbaiki logging untuk mencatat catatan processing dan error yang lebih tepat.
- Update Model `StmtEntry`:
- Menambahkan relasi baru:
- `tt`: Relasi dengan model `Teller` berdasarkan `trans_reference`.
- `dc`: Relasi dengan model `DataCapture` berdasarkan `trans_reference`.
- `aa`: Relasi dengan model `TempArrangement` berdasarkan `trans_reference`.
Signed-off-by: Daeng Deni Mardaeni <ddeni05@gmail.com>
105 lines
3.9 KiB
PHP
105 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\TempArrangement;
|
|
|
|
class ProcessArrangementDataJob implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
protected string $period;
|
|
|
|
/**
|
|
* Create a new job instance.
|
|
*/
|
|
public function __construct(string $period = '')
|
|
{
|
|
$this->period = $period;
|
|
}
|
|
|
|
/**
|
|
* 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 arrangement data processing');
|
|
return;
|
|
}
|
|
|
|
// Construct the filename based on the period folder name
|
|
$filename = "{$this->period}.ST.AA.ARRANGEMENT.csv";
|
|
$filePath = "{$this->period}/$filename";
|
|
|
|
Log::info("Processing arrangement 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 TempArrangement())->getFillable();
|
|
$rowCount = 0;
|
|
|
|
while (($row = fgetcsv($handle, 0, "~")) !== false) {
|
|
$rowCount++;
|
|
|
|
if (count($headers) === count($row)) {
|
|
$data = array_combine($headers, $row);
|
|
try {
|
|
if ($data['arrangement_id'] !== 'arrangement_id') {
|
|
TempArrangement::updateOrCreate(
|
|
['arrangement_id' => $data['arrangement_id']], // key to find existing record
|
|
$data // data to update or create
|
|
);
|
|
$processedCount++;
|
|
}
|
|
} catch (Exception $e) {
|
|
$errorCount++;
|
|
Log::error("Error processing Arrangement 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("Arrangement data processing completed. Total processed: $processedCount, Total errors: $errorCount");
|
|
|
|
} catch (Exception $e) {
|
|
Log::error('Error in ProcessArrangementDataJob: ' . $e->getMessage());
|
|
throw $e;
|
|
}
|
|
}
|
|
}
|