<?php

namespace App\Models\Warehouse\IndirectGoods;

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

class IndirectGoodsRequestItem extends Model
{
    use HasFactory;

    protected $table = 'indirect_goods_request_items';

    protected $fillable = [
        'indirect_goods_request_id',
        'indirect_good_id',
        'invoice_number',
        'quantity_requested',
        'quantity_released',
        'quantity_balance',
        'unit_price',
        'total_price',
        'notes',
    ];

    protected $casts = [
        'quantity_requested' => 'decimal:2',
        'quantity_released' => 'decimal:2',
        'quantity_balance' => 'decimal:2',
        'unit_price' => 'decimal:2',
        'total_price' => 'decimal:2',
    ];

    protected static function boot()
    {
        parent::boot();

        static::creating(function ($item) {
            $item->quantity_balance = $item->quantity_requested;
        });
    }

    // Relationships

    public function request()
    {
        return $this->belongsTo(IndirectGoodsRequest::class, 'indirect_goods_request_id');
    }

    public function indirectGood()
    {
        return $this->belongsTo(IndirectGood::class, 'indirect_good_id');
    }
}
