<?php

namespace App\Models\Warehouse\IndirectGoods;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\Warehouse\ProjectDivision;
use App\Models\User;

class IndirectGoodsStockMovement extends Model
{
    use HasFactory;

    protected $table = 'indirect_goods_stock_movements';

    protected $fillable = [
        'indirect_good_id',
        'movement_type',
        'reference_type',
        'reference_id',
        'quantity_before',
        'quantity_moved',
        'quantity_after',
        'invoice_number',
        'division_id',
        'user_id',
        'notes',
    ];

    protected $casts = [
        'quantity_before' => 'decimal:2',
        'quantity_moved' => 'decimal:2',
        'quantity_after' => 'decimal:2',
    ];

    // Relationships

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

    public function division()
    {
        return $this->belongsTo(ProjectDivision::class, 'division_id');
    }

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    // Scopes

    public function scopeOfType($query, string $type)
    {
        return $query->where('movement_type', $type);
    }

    public function scopeByInvoice($query, string $invoiceNumber)
    {
        return $query->where('invoice_number', $invoiceNumber);
    }
}
