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 <ddeni05@gmail.com>
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
59
app/Models/AccountBalance.php
Normal file
59
app/Models/AccountBalance.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Webstatement\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
|
||||
class AccountBalance extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*/
|
||||
protected $fillable = [
|
||||
'account_number',
|
||||
'period',
|
||||
'actual_balance',
|
||||
'cleared_balance',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the account that owns the balance.
|
||||
*/
|
||||
public function account()
|
||||
{
|
||||
return $this->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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user