<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class StockTransfer extends Model
{
    protected $fillable = [
        'transfer_no', 'transfer_date', 'from_warehouse_id', 'to_warehouse_id',
        'notes', 'status', 'total_cost', 'prepared_by', 'approved_by',
        'received_by', 'approved_at', 'received_at',
    ];

    protected $casts = [
        'transfer_date' => 'date',
        'total_cost'    => 'decimal:2',
        'approved_at'   => 'datetime',
        'received_at'   => 'datetime',
    ];

    public function fromWarehouse()
    {
        return $this->belongsTo(Warehouse::class, 'from_warehouse_id');
    }

    public function toWarehouse()
    {
        return $this->belongsTo(Warehouse::class, 'to_warehouse_id');
    }

    public function items()
    {
        return $this->hasMany(StockTransferItem::class, 'transfer_id');
    }

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

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

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