<?php

namespace App\Models;

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

class MemberSubscription extends Model
{
    use HasFactory;

    protected $fillable = [
        'member_id',
        'subscription_id',
        'date_from',
        'date_to',
        'price',
        'course_subscription_id',
        'subscription_price',
        'notes'
    ];

    public function member()
    {
        return $this->belongsTo(Member::class);
    }

    public function subscription()
    {
        return $this->belongsTo(Subscription::class);
    }

    public function course_subscription()
    {
        return $this->belongsTo(CourseSubscription::class);
    }

    public function getStatus()
    {
        $status = 0;
        $rates = \App\Models\Rate::where('member_subscription_id', $this->id)->orderBy('date')->get();
        foreach($rates as $rate)
        {
            if ($rate->date > date("Y-m-d"))
            {
                break;
            }
            $status = $rate->status;
        }
        return $status;
    }
}
