<?php

namespace App\Repositories\Shop;

use App\Models\Shop\Offer;

class OfferStocks
{
    public static function decreaseStock($item)
    {
        $offer = Offers::get($item['offer_id']);
        $offer->stock_current = $offer->stock_current - $item['quantity'];
        if ($offer->stock_current <= 0) {
            $offer->stock_current = 0;
        }

        return $offer->save();
    }

    public static function getStockCurrent($id)
    {
        return Offers::getField($id, 'stock_current');
    }

    public static function getAlerts()
    {
        return Offer::active()->where('stock_current', '<', 15)->get();
    }

    public static function countAlerts()
    {
        return Offer::active()->where('stock_current', '<', 15)->count();
    }
}
