<?php

namespace Modules\Inventory\Models\Traits;

use Modules\Inventory\Models\StockBatch;
use Illuminate\Database\Eloquent\Relations\HasMany;

/**
 * @mixin \Illuminate\Database\Eloquent\Model
 */

trait HasStockBatches
{
    public function stockBatches(): HasMany
    {
        return $this->hasMany(StockBatch::class);
    }

    public function availableBatches(): HasMany
    {
        return $this->hasMany(StockBatch::class)
            ->where('remaining_qty', '>', 0)
            ->where(function ($q) {
                $q->whereNull('expiry_date')
                    ->orWhere('expiry_date', '>=', now());
            })
            ->orderBy('expiry_date')
            ->orderBy('id');
    }
}
