From a7a55a92a1af2282099a8e3585e039198f7a64f6 Mon Sep 17 00:00:00 2001 From: Daeng Deni Mardaeni Date: Fri, 23 May 2025 19:27:44 +0700 Subject: [PATCH] feat(webstatement): tambah model AccountBalance dan relasi balances pada Account - Menambahkan model `AccountBalance` dengan fitur berikut: - Properti `fillable` meliputi: `account_number`, `period`, `actual_balance`, `cleared_balance`. - Relasi `belongsTo` dengan model `Account`. - Scope query untuk filter berdasarkan `account_number` (`scopeForAccount`) dan `period` (`scopeForPeriod`). - Fungsi statis `getBalance` untuk mendapatkan saldo berdasarkan `account_number` dan `period`. - Menambahkan method berikut pada model `Account`: - Relasi `hasMany` dengan `AccountBalance` untuk mendapatkan semua saldo terkait. - Method `getBalanceForPeriod` untuk mendapatkan saldo pada periode tertentu. - Membuat migrasi untuk tabel `account_balances` dengan spesifikasi berikut: - Kolom: `account_number`, `period` (format: YYYY-MM), `actual_balance` (decimal), `cleared_balance` (decimal), `timestamps`. - Konstrain unik untuk pasangan `account_number` dan `period`. - Indeks pada kolom `account_number`, `period`, dan `created_at`. Signed-off-by: Daeng Deni Mardaeni --- app/Models/Account.php | 19 ++++++ app/Models/AccountBalance.php | 59 +++++++++++++++++++ ...3_122342_create_account_balances_table.php | 42 +++++++++++++ 3 files changed, 120 insertions(+) create mode 100644 app/Models/AccountBalance.php create mode 100644 database/migrations/2025_05_23_122342_create_account_balances_table.php diff --git a/app/Models/Account.php b/app/Models/Account.php index bd76681..9f6e0fd 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -34,4 +34,23 @@ class Account extends Model { return $this->belongsTo(Customer::class, 'customer_code', 'customer_code'); } + + /** + * Get all balances for this account. + */ + public function balances() + { + return $this->hasMany(AccountBalance::class, 'account_number', 'account_number'); + } + + /** + * Get balance for a specific period. + * + * @param string $period Format: YYYY-MM + * @return AccountBalance|null + */ + public function getBalanceForPeriod($period) + { + return $this->balances()->where('period', $period)->first(); + } } diff --git a/app/Models/AccountBalance.php b/app/Models/AccountBalance.php new file mode 100644 index 0000000..3ab0320 --- /dev/null +++ b/app/Models/AccountBalance.php @@ -0,0 +1,59 @@ +belongsTo(Account::class, 'account_number', 'account_number'); + } + + /** + * Scope a query to filter by account number. + */ + public function scopeForAccount($query, $accountNumber) + { + return $query->where('account_number', $accountNumber); + } + + /** + * Scope a query to filter by period. + */ + public function scopeForPeriod($query, $period) + { + return $query->where('period', $period); + } + + /** + * Get balance for a specific account and period. + * + * @param string $accountNumber + * @param string $period Format: YYYY-MM + * @return AccountBalance|null + */ + public static function getBalance($accountNumber, $period) + { + return self::where('account_number', $accountNumber) + ->where('period', $period) + ->first(); + } +} diff --git a/database/migrations/2025_05_23_122342_create_account_balances_table.php b/database/migrations/2025_05_23_122342_create_account_balances_table.php new file mode 100644 index 0000000..0177d40 --- /dev/null +++ b/database/migrations/2025_05_23_122342_create_account_balances_table.php @@ -0,0 +1,42 @@ +id(); + $table->string('account_number'); + $table->string('period'); // Format: YYYY-MM + $table->decimal('actual_balance', 20, 2)->default(0); + $table->decimal('cleared_balance', 20, 2)->default(0); + $table->timestamps(); + + // Create a unique constraint to ensure one record per account per period + $table->unique(['account_number', 'period']); + + // Add indexes for faster queries + $table->index('account_number'); + $table->index('period'); + $table->index('created_at'); + + // Add foreign key if needed + // $table->foreign('account_number')->references('account_number')->on('accounts'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('account_balances'); + } +};