53 lines
1.2 KiB
PHP
53 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\PaymentType;
|
|
use Illuminate\Database\Eloquent\Concerns\HasUuids;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class PaymentScheduleItem extends Model
|
|
{
|
|
use HasUuids, HasFactory;
|
|
|
|
protected $guarded = [];
|
|
|
|
protected $casts = [
|
|
'type' => PaymentType::class,
|
|
'amount' => 'decimal:2',
|
|
'percentage' => 'decimal:2',
|
|
'due_date' => 'date',
|
|
];
|
|
|
|
public function getPaidAmountAttribute(): float
|
|
{
|
|
// Nếu đã eager load payments, dùng collection sum để tránh query thêm
|
|
if ($this->relationLoaded('payments')) {
|
|
return (float) $this->payments->sum('amount');
|
|
}
|
|
|
|
return (float) $this->payments()->sum('amount');
|
|
}
|
|
|
|
public function getRemainingAmountAttribute(): float
|
|
{
|
|
return (float) $this->amount - $this->paid_amount;
|
|
}
|
|
|
|
public function template()
|
|
{
|
|
return $this->belongsTo(PaymentTemplate::class);
|
|
}
|
|
|
|
public function schedule()
|
|
{
|
|
return $this->belongsTo(PaymentSchedule::class);
|
|
}
|
|
|
|
public function payments()
|
|
{
|
|
return $this->hasMany(Payment::class, 'schedule_item_id');
|
|
}
|
|
}
|