<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class DetailPayment extends Model
{
    use HasFactory;

    protected $guarded = ['id'];
    protected $table = 'detail_payments';

    public static function cekTunggakan($santri_id)
    {
        return self::where('status', 'belum bayar')
            ->whereHas('payment', function ($query) use ($santri_id) {
                $query->where('santri_id', $santri_id);
            })->orderBy('tahun_pembayaran', 'asc')
            ->orderBy('bulan_pembayaran', 'asc')->first();
    }

    public function payment()
    {
        return $this->belongsTo(Payment::class, 'payment_id', 'id');
    }

    public function paymentType()
    {
        return $this->belongsTo(PaymentType::class, 'type_id', 'id');
    }
}
