<?php

namespace Modules\PurchaseRequisition\Entities;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class PrRequest extends Model
{
    use SoftDeletes;

    protected $table = 'pr_requests';

    protected $guarded = ['id'];

    protected $casts = [
        'transaction_date' => 'datetime',
    ];

    /**
     * Get the business that owns the requisition.
     */
    public function business()
    {
        return $this->belongsTo(\App\Business::class);
    }

    /**
     * Get the location for this requisition.
     */
    public function location()
    {
        return $this->belongsTo(\App\BusinessLocation::class);
    }

    /**
     * Get the products for this requisition.
     */
    public function products()
    {
        return $this->hasMany(PrProduct::class, 'pr_request_id');
    }

    /**
     * Get the supplier responses for this requisition.
     */
    public function supplierResponses()
    {
        return $this->hasMany(PrSupplierResponse::class, 'pr_request_id');
    }

    /**
     * Get the user who created this requisition.
     */
    public function createdBy()
    {
        return $this->belongsTo(\App\User::class, 'created_by');
    }

    /**
     * Scope to filter by business.
     */
    public function scopeForBusiness($query, $businessId)
    {
        return $query->where('business_id', $businessId);
    }

    /**
     * Scope to filter by status.
     */
    public function scopeByStatus($query, $status)
    {
        return $query->where('status', $status);
    }
}
