- Tambahkan pengecekan untuk memastikan 'account_number' tidak sama dengan string 'account_number' sebelum menyimpan data akun. - Perbarui atribut yang dapat diisi pada model Account dengan mengganti 'customer_no' menjadi 'customer_code' dan 'co_code' menjadi 'branch_code'.
83 lines
3.0 KiB
PHP
83 lines
3.0 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 Log;
|
|
use Modules\Webstatement\Models\Account;
|
|
|
|
class ProcessAccountDataJob implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
/**
|
|
* Create a new job instance.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Execute the job.
|
|
*/
|
|
public function handle()
|
|
: void
|
|
{
|
|
$filePath = storage_path('app/20250207.ST.ACCOUNT.csv'); // Use Storage facade
|
|
try {
|
|
if (!file_exists($filePath)) {
|
|
throw new Exception("File not found: {$filePath}");
|
|
}
|
|
|
|
set_time_limit(24 * 60 * 60);
|
|
|
|
$handle = fopen($filePath, "r");
|
|
if ($handle !== false) {
|
|
|
|
// Use the header row to determine the order of columns
|
|
$headers = (new Account())->getFillable();
|
|
Log::info('Headers: ' . implode(", ", $headers));
|
|
$rowCount = 0;
|
|
while (($row = fgetcsv($handle, 0, "~")) !== false) {
|
|
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();
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
Log::error('Error processing Account: ' . $e->getMessage());
|
|
}
|
|
}
|
|
}
|
|
fclose($handle);
|
|
} else {
|
|
throw new Exception("Unable to open file: {$filePath}");
|
|
}
|
|
} catch (Exception $e) {
|
|
Log::error('Error in ProcessAccountDataJob: ' . $e->getMessage());
|
|
throw $e;
|
|
}
|
|
}
|
|
}
|