<?php

namespace App\Models;

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

class PurchaseItem extends Model
{
    use HasFactory;

    protected $fillable = [
        'purchase_id', 'yarn_id', 'lot_no', 'bags',
        'quantity', 'unit', 'rate', 'amount',
    ];

    protected $casts = [
        'quantity' => 'decimal:2',
        'rate'     => 'decimal:2',
        'amount'   => 'decimal:2',
        'bags'     => 'integer',
    ];

    protected static function booted()
    {
        static::saving(function ($item) {
            $item->amount = $item->quantity * $item->rate;
        });
    }

    public function purchase()
    {
        return $this->belongsTo(Purchase::class);
    }

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