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'); + } +};