mirror of
https://github.com/henry4682/finance_app.git
synced 2026-07-16 00:10:00 +00:00
feat: 前端分離+功能api化
1. 新增資產管理CRUD、googleOAuth登入 2. 更改原記帳controller
This commit is contained in:
54
app/Models/Account.php
Normal file
54
app/Models/Account.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?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');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user