<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Venta extends Model
{
    protected $table = 'ventas';

    protected $guarded = ['id'];

    protected $casts = ['fecha' => 'date'];

    public function cliente()
    {
        return $this->belongsTo(Cliente::class);
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function detalles()
    {
        return $this->hasMany(VentaDetalle::class);
    }

    public function comprobante()
    {
        return $this->hasOne(Comprobante::class);
    }

    public static function siguienteNumero(): string
    {
        $ultimo = static::max('id') ?? 0;

        return 'V-'.str_pad($ultimo + 1, 6, '0', STR_PAD_LEFT);
    }
}
