Files
finance_app/app/Models/Account.php
henry yo 85972f950c feat: 前端分離+功能api化
1. 新增資產管理CRUD、googleOAuth登入
2. 更改原記帳controller
2026-06-25 14:19:07 +08:00

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