From 011f749786e8f0fc7b69fb2d761ca2001604c967 Mon Sep 17 00:00:00 2001 From: Daeng Deni Mardaeni Date: Thu, 10 Jul 2025 19:30:58 +0700 Subject: [PATCH] feat(webstatement): tambahkan hubungan branch dan account di model Perubahan yang dilakukan: - Menambahkan relasi branch di model Account berdasarkan kolom branch_code. - Menambahkan relasi account di model PrintStatementLog untuk akses data account dari log. - Memperbaiki referensi branch_name di PrintStatementController agar menggunakan relasi dari model Account. - Menonaktifkan eager loading pada query di PrintStatementController untuk optimasi performa. Tujuan perubahan: - Memastikan data branch dan account dapat diakses langsung melalui relasi antar model. - Menghindari potensi masalah N+1 query saat mengambil data terkait branch. - Meningkatkan efisiensi kode dan menjaga konsistensi data dalam proses statement. --- app/Http/Controllers/PrintStatementController.php | 4 ++-- app/Models/Account.php | 11 ++++++++--- app/Models/PrintStatementLog.php | 4 ++++ resources/views/statements/index.blade.php | 8 +------- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/app/Http/Controllers/PrintStatementController.php b/app/Http/Controllers/PrintStatementController.php index 90487e3..a9a1622 100644 --- a/app/Http/Controllers/PrintStatementController.php +++ b/app/Http/Controllers/PrintStatementController.php @@ -584,7 +584,7 @@ ini_set('max_execution_time', 300000); $filteredRecords = $query->count(); // Eager load relationships to avoid N+1 query problems - $query->with(['user', 'branch', 'authorizer']); + //$query->with(['user', 'branch', 'authorizer']); // Get the data for the current page $data = $query->get()->map(function ($item) { @@ -592,7 +592,7 @@ ini_set('max_execution_time', 300000); return [ 'id' => $item->id, 'branch_code' => $item->branch_code, - 'branch_name' => $item->branch->name ?? 'N/A', + 'branch_name' => $item->account->branch->name ?? 'N/A', 'account_number' => $item->account_number, 'period_from' => $item->period_from, 'period_to' => $item->is_period_range ? $item->period_to : null, diff --git a/app/Models/Account.php b/app/Models/Account.php index 9f6e0fd..ee92857 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -4,6 +4,7 @@ namespace Modules\Webstatement\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Factories\HasFactory; +use Modules\Basicdata\Models\Branch; // use Modules\Webstatement\Database\Factories\AccountFactory; class Account extends Model @@ -34,7 +35,7 @@ class Account extends Model { return $this->belongsTo(Customer::class, 'customer_code', 'customer_code'); } - + /** * Get all balances for this account. */ @@ -42,10 +43,10 @@ class Account extends Model { return $this->hasMany(AccountBalance::class, 'account_number', 'account_number'); } - + /** * Get balance for a specific period. - * + * * @param string $period Format: YYYY-MM * @return AccountBalance|null */ @@ -53,4 +54,8 @@ class Account extends Model { return $this->balances()->where('period', $period)->first(); } + + public function branch(){ + return $this->belongsTo(Branch::class, 'branch_code','code'); + } } diff --git a/app/Models/PrintStatementLog.php b/app/Models/PrintStatementLog.php index 55b2fa7..27e43f4 100644 --- a/app/Models/PrintStatementLog.php +++ b/app/Models/PrintStatementLog.php @@ -293,4 +293,8 @@ class PrintStatementLog extends Model { return $query->where('request_type', 'single_account'); } + + public function account(){ + return $this->belongsTo(Account::class, 'account_number','account_number'); + } } diff --git a/resources/views/statements/index.blade.php b/resources/views/statements/index.blade.php index 8d5d67f..0c08519 100644 --- a/resources/views/statements/index.blade.php +++ b/resources/views/statements/index.blade.php @@ -388,13 +388,7 @@ title: 'Branch', }, account_number: { - title: 'Account Number', - render: (item, data) => { - if (data.request_type == "multi_account") { - return data.stmt_sent_type ?? 'N/A'; - } - return data.account_number ?? ''; - }, + title: 'Account Number' }, period: { title: 'Period',