<?php

namespace App\Models;

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

class StockLedger extends Model
{
    use HasFactory;

    protected $fillable = [
        'transaction_date', 'yarn_id', 'lot_no', 'type',
        'reference_type', 'reference_id', 'voucher_no',
        'in_qty', 'out_qty', 'balance_qty', 'rate', 'value', 'remarks',
    ];

    protected $casts = [
        'transaction_date' => 'date',
        'in_qty'           => 'decimal:2',
        'out_qty'          => 'decimal:2',
        'balance_qty'      => 'decimal:2',
        'rate'             => 'decimal:2',
        'value'            => 'decimal:2',
    ];

    public function yarn()
    {
        return $this->belongsTo(Yarn::class);
    }

    public function reference()
    {
        return $this->morphTo();
    }

    public function scopeBetween($query, $from, $to)
    {
        return $query->whereBetween('transaction_date', [$from, $to]);
    }
}
