<?php

namespace App\Models;

use App\Models\Scopes\ParentBranchScope;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class LoanPaymentAllocation extends Model
{
    protected $fillable = ['loan_payment_id','loan_installment_id','principal','interest','fees','penalties','total'];

    protected function casts(): array
    {
        return ['principal'=>'decimal:2','interest'=>'decimal:2','fees'=>'decimal:2','penalties'=>'decimal:2','total'=>'decimal:2'];
    }

    protected static function booted(): void
    {
        static::addGlobalScope(new ParentBranchScope('loan_payments', 'loan_payment_id'));
    }

    public function payment(): BelongsTo { return $this->belongsTo(LoanPayment::class, 'loan_payment_id'); }
    public function installment(): BelongsTo { return $this->belongsTo(LoanInstallment::class, 'loan_installment_id'); }
}
