File "LimitProductQuantityIncrease.php"

Full Path: /home/clickysoft/public_html/jmapi5.clickysoft.net/app/Rules/LimitProductQuantityIncrease.php
File size: 1.38 KB
MIME-type: text/x-php
Charset: utf-8

<?php

namespace App\Rules;

use App\Models\OrderItems;
use App\Models\ProductVariationRange;
use Illuminate\Contracts\Validation\InvokableRule;
use Illuminate\Support\Facades\Log;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class LimitProductQuantityIncrease implements InvokableRule
{
    /**
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function __invoke($attribute, $value, $fail)
    {
        if ($value) {
            $index = explode('.', $attribute)[1];
            $item = OrderItems::find($value);
            $price_range = ProductVariationRange::where('product_price_id', $item->price_id)
                ->where('qty_from', '<=', $item->quantity)
                ->where('qty_to', '>=', $item->quantity)
                ->first();

            if (!$price_range) {
                $fail("Product combination for product #:position not found.");
            } else {
                if ($price_range->price != $item->price) {
                    if (isset(request()->get('products')[$index]['quantity']) && request()->get('products')[$index]['quantity'] > $item->quantity) {
                        $fail("Product quantity can not be increased due to change in price. Please add the item as a new product to proceed.");
                    }
                }
            }
        }
    }
}