<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class LoanPayment extends Model
{
    protected $fillable = [
        'receipt_number','loan_id','customer_id','branch_id','financial_account_id','financial_account_transaction_id',
        'amount','principal_amount','interest_amount','fees_amount','penalties_amount','payment_date','method','reference','notes','received_by',
    ];

    protected function casts(): array
    {
        return [
            'amount'=>'decimal:2','principal_amount'=>'decimal:2','interest_amount'=>'decimal:2','fees_amount'=>'decimal:2',
            'penalties_amount'=>'decimal:2','payment_date'=>'datetime',
        ];
    }

    public function loan(): BelongsTo { return $this->belongsTo(Loan::class); }
    public function customer(): BelongsTo { return $this->belongsTo(Customer::class); }
    public function branch(): BelongsTo { return $this->belongsTo(Branch::class); }
    public function financialAccount(): BelongsTo { return $this->belongsTo(FinancialAccount::class); }
    public function accountTransaction(): BelongsTo { return $this->belongsTo(FinancialAccountTransaction::class, 'financial_account_transaction_id'); }
    public function receiver(): BelongsTo { return $this->belongsTo(User::class, 'received_by'); }
    public function allocations(): HasMany { return $this->hasMany(LoanPaymentAllocation::class); }
}
