<?php

namespace App\Models;

use App\Traits\ModelCounterTrait;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class TreasuryEntry extends Model
{
    use HasFactory, softDeletes, ModelCounterTrait;

    protected $fillable = [
        'category_id',
        'date',
        'amount',
        'title',
        'description',
        'attachment',
        'code',
        'is_valid',
        'validation_date',
    ];

    public function category()
    {
        return $this->belongsTo(TreasuryCategory::class);
    }

    public static function booted(): void {
        static::creating(function(TreasuryEntry $entry) {
            $entry->code = $entry->codeGenerator([
                "prefix" => "TRE",
                "model" => "TreasuryEntry",
                "length" => 5,
            ]);
        });
    }
}
