<?php

namespace App;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

class Subscription extends \Laravel\Cashier\Subscription {
    protected $fillable = ["company_id", "plan_id", "start_date", "end_date"];

    protected $with = ["plan"];

    protected $appends = ["active"];

    protected $casts = [
        "active" => "boolean",
        "start_date" => "date",
        "end_date" => "date"
    ];


    /**
     * ATRIBUTS FICTICIS
     */
    public function getActiveAttribute() {
        $now = Carbon::now();
        return $this->end_date ?
            $this->start_date <= $now && $this->end_date >= $now :
            null;
    }


    /**
     * RELATIONSHIPS
     */
    public function plan() {
        return $this->belongsTo("App\Plan");
    }

    public function company() {
        return $this->belongsTo("App\Company");
    }

    public function company_orders() {
        return $this->hasMany("App\CompanyOrder")->orderByDesc("created_at");
    }
}
