mirror of
https://github.com/henry4682/finance_app.git
synced 2026-07-16 00:10:00 +00:00
55 lines
1.2 KiB
PHP
55 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class Account extends Model
|
|
{
|
|
protected $fillable = [
|
|
'user_id',
|
|
'name',
|
|
'type',
|
|
'currency',
|
|
'balance',
|
|
'note',
|
|
'billing_day',
|
|
'payment_day',
|
|
];
|
|
|
|
public function user()
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
public function holdings()
|
|
{
|
|
return $this->hasMany(InvestmentHolding::class);
|
|
}
|
|
|
|
public function expenses()
|
|
{
|
|
return $this->hasMany(Expense::class);
|
|
}
|
|
|
|
public function unpaidAmount()
|
|
{
|
|
$billingDay = $this->billing_day ?? 1;
|
|
|
|
$now = now();
|
|
|
|
// 如果今天還沒到結帳日,帳單週期是上個月結帳日到這個月結帳日
|
|
if ($now->day < $billingDay) {
|
|
$start = $now->copy()->subMonth()->setDay($billingDay);
|
|
$end = $now->copy()->setDay($billingDay)->subDay();
|
|
} else {
|
|
$start = $now->copy()->setDay($billingDay);
|
|
$end = $now->copy()->addMonth()->setDay($billingDay)->subDay();
|
|
}
|
|
|
|
return $this->expenses()
|
|
->whereBetween('date', [$start->toDateString(), $end->toDateString()])
|
|
->sum('amount');
|
|
}
|
|
}
|