Files
webstatement/app/Jobs/ProcessAccountDataJob.php
Daeng Deni Mardaeni 59d186e3b5 feat(webstatement): simpan saldo pembukaan ke model AccountBalance
- Tambahkan use statement untuk model `AccountBalance` di `ProcessAccountDataJob`.
- Simpan saldo pembukaan (`open_actual_bal` dan `open_cleared_bal`) dari data akun yang diproses ke model `AccountBalance`.
- Gunakan `firstOrNew` untuk memastikan data saldo pembukaan unik berdasarkan nomor akun dan periode tertentu.
- Tambahkan log untuk mencatat penyimpanan saldo pembukaan yang berhasil dilakukan.
- Pastikan penyimpanan hanya dilakukan jika data saldo tersedia di input (`isset` pada `open_actual_bal` atau `

Signed-off-by: Daeng Deni Mardaeni <ddeni05@gmail.com>
2025-05-23 19:34:39 +07:00

146 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\Webstatement\Models\Account;
use Modules\Webstatement\Models\AccountBalance;
class ProcessAccountDataJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $periods;
/**
* Create a new job instance.
*/
public function __construct(array $periods = [])
{
$this->periods = $periods;
}
/**
* 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->periods)) {
Log::warning('No periods provided for account data processing');
return;
}
foreach ($this->periods as $period) {
// Skip the _parameter folder
if ($period === '_parameter') {
Log::info("Skipping _parameter folder");
continue;
}
// Construct the filename based on the period folder name
$filename = "$period.ST.ACCOUNT.csv";
$filePath = "$period/$filename";
Log::info("Processing account file: $filePath");
if (!$disk->exists($filePath)) {
Log::warning("File not found: $filePath");
continue;
}
// 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 Account())->getFillable();
Log::info('Headers: ' . implode(", ", $headers));
$rowCount = 0;
while (($row = fgetcsv($handle, 0, "~")) !== false) {
$rowCount++;
if (count($headers) === count($row)) {
$data = array_combine($headers, $row);
// Check if start_year_bal is empty and set it to 0 if so
if (empty($data['start_year_bal']) || $data['start_year_bal'] == "" || $data['start_year_bal'] == null) {
$data['start_year_bal'] = 0;
}
if (empty($data['closure_date']) || $data['closure_date'] == "" || $data['closure_date'] == null) {
$data['closure_date'] = null;
}
try {
if ($data['account_number'] !== 'account_number') {
// Use firstOrNew instead of updateOrCreate
$account = Account::firstOrNew(['account_number' => $data['account_number']]);
$account->fill($data);
$account->save();
// Store the opening balances in the AccountBalance model for this period
if (isset($data['open_actual_bal']) || isset($data['open_cleared_bal'])) {
$accountBalance = AccountBalance::firstOrNew([
'account_number' => $data['account_number'],
'period' => $period
]);
// Set the balances
if (isset($data['open_actual_bal'])) {
$accountBalance->actual_balance = $data['open_actual_bal'];
}
if (isset($data['open_cleared_bal'])) {
$accountBalance->cleared_balance = $data['open_cleared_bal'];
}
$accountBalance->save();
Log::info("Saved balance for account {$data['account_number']} for period $period");
}
$processedCount++;
}
} catch (Exception $e) {
$errorCount++;
Log::error("Error processing Account 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("Account data processing completed. Total processed: $processedCount, Total errors: $errorCount");
} catch (Exception $e) {
Log::error('Error in ProcessAccountDataJob: ' . $e->getMessage());
throw $e;
}
}
}