<?php

namespace App\Models;

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

class Cotizacion extends Model
{
    use HasFactory;
    protected $appends = ['client_name', 'fecha_ingreso', 'fecha_vence', 'vencida'];
    
    public function client() 
    {
        return $this->hasOne(Cliente::class, 'id', 'client_id')->withTrashed();
    }
    public function getClientNameAttribute()
    {
        return $this->client->name;
    }

    public function getFechaIngresoAttribute()
    {
        return $this->created_at->format('d/m/Y');
    }
    public function getFechaVenceAttribute()
    {
        
        return \Carbon\Carbon::parse($this->validez)->format('d/m/Y');
    }

    public function getVencidaAttribute()
    {        
        
        $vencida = false;

        if($this->status == '0'){
            $vencida = \Carbon\Carbon::parse($this->validez)->isPast();
        }

        return $vencida;
        
    }

    public function items()
    {
        return $this->hasMany(CotizacionItem::class, 'cotizacion_id', 'id');
    }


}
