<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Pengeluaran extends Model
{
    protected $table = 'pengeluaran';
    protected $primaryKey = 'id_pengeluaran';
    public $incrementing = false;
    protected $keyType = 'string';

    protected static function booted()
    {
        static::creating(function ($pengeluaran) {
            if (!$pengeluaran->id_pengeluaran) {
                do {
                    $randomId = 'EXP' . date('Ymd') . mt_rand(1000, 9999);
                } while (self::where('id_pengeluaran', $randomId)->exists());
                $pengeluaran->id_pengeluaran = $randomId;
            }
        });
    }

    public function getIdAttribute()
    {
        return $this->attributes[$this->primaryKey] ?? null;
    }

    public function setIdAttribute($value)
    {
        $this->attributes[$this->primaryKey] = $value;
    }

    protected $fillable = [
        'tanggal',
        'keterangan',
        'jumlah',
        'kategori',
        'nama_pengirim',
    ];

    protected $casts = [
        'tanggal' => 'date',
        'jumlah'  => 'integer',
    ];
}
