<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class Plane extends Model
{
    protected $guarded = [];

    public $timestamps = false;

    public function planPlantel() : BelongsToMany
    {
        return $this->belongsToMany(Plantel::class)->withPivot('plantel_id');
    }

    public function nivelPlane() : BelongsToMany
    {
        return $this->belongsToMany(Nivel::class);
    }

    public function existe($plantelId = null)
    {
        if (!$plantelId) {
            return false;
        }

        return $this->planPlantel()
            ->wherePivot('plantel_id', $plantelId)
            ->exists();
    }

    public function nivel($nivelId = null)
    {
        if (!$nivelId) {
            return false;
        }

        return $this->nivelPlane()
            ->wherePivot('nivel_id', $nivelId)
            ->exists();
    }

     public function conceptos(): BelongsToMany
    {
        return $this->belongsToMany(Concepto::class)
        ->orderBy('conceptos.tipo', 'asc')
        ->orderBy('conceptos.fecha_pago', 'asc');
    }
}
