![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/Ecombricks/InventoryInventorySales/Plugin/StockState/ |
<?php /** * Copyright © eComBricks. All rights reserved. * See LICENSE.txt for license details. */ declare(strict_types=1); namespace Ecombricks\InventoryInventorySales\Plugin\StockState; /** * Suggest qty plugin */ class SuggestQtyPlugin { /** * Source manager * * @var \Ecombricks\Inventory\Model\SourceManagement */ protected $sourceManagement; /** * Product management * * @var \Ecombricks\InventoryInventoryCatalog\Model\ProductManagement */ protected $productManagement; /** * Get product salable qty * * @var \Ecombricks\InventoryInventorySales\Api\GetProductSalableQtyInterface */ protected $getProductSalableQty; /** * Get stock item configuration * * @var \Magento\InventoryConfigurationApi\Api\GetStockItemConfigurationInterface */ protected $getStockItemConfiguration; /** * Constructor * * @param \Ecombricks\Inventory\Model\SourceManagement $sourceManagement * @param \Ecombricks\InventoryInventoryCatalog\Model\ProductManagement $productManagement * @param \Ecombricks\InventoryInventorySales\Api\GetProductSalableQtyInterface $getProductSalableQty * @param \Magento\InventoryConfigurationApi\Api\GetStockItemConfigurationInterface $getStockItemConfiguration * @return void */ public function __construct( \Ecombricks\Inventory\Model\SourceManagement $sourceManagement, \Ecombricks\InventoryInventoryCatalog\Model\ProductManagement $productManagement, \Ecombricks\InventoryInventorySales\Api\GetProductSalableQtyInterface $getProductSalableQty, \Magento\InventoryConfigurationApi\Api\GetStockItemConfigurationInterface $getStockItemConfiguration ) { $this->sourceManagement = $sourceManagement; $this->productManagement = $productManagement; $this->getProductSalableQty = $getProductSalableQty; $this->getStockItemConfiguration = $getStockItemConfiguration; } /** * Suggest source qty * * @param \Closure $proceed * @param int $productId * @param string $sourceCode * @param float $qty * @return float * @throws \Magento\Framework\Exception\LocalizedException */ protected function suggestSourceQty($productId, $sourceCode, $qty) { $sku = $this->productManagement->getSku((int) $productId); $stockItemConfiguration = $this->getStockItemConfiguration->execute($sku, $this->sourceManagement->getSourceStockId($sourceCode)); $qtyIncrements = $stockItemConfiguration->getQtyIncrements(); if ($qty <= 0 || $stockItemConfiguration->isManageStock() === false || $qtyIncrements < 2) { throw new \Magento\Framework\Exception\LocalizedException(__('Wrong condition.')); } $minQty = max($stockItemConfiguration->getMinSaleQty(), $qtyIncrements); $divisibleMin = ceil($minQty / $qtyIncrements) * $qtyIncrements; $salableQty = $this->getProductSalableQty->execute($sku, $sourceCode); $maxQty = min($salableQty, $stockItemConfiguration->getMaxSaleQty()); $divisibleMax = floor($maxQty / $qtyIncrements) * $qtyIncrements; if ($qty < $minQty || $qty > $maxQty || $divisibleMin > $divisibleMax) { throw new \Magento\Framework\Exception\LocalizedException(__('Wrong condition.')); } $closestDivisibleLeft = floor($qty / $qtyIncrements) * $qtyIncrements; $closestDivisibleRight = $closestDivisibleLeft + $qtyIncrements; $acceptableLeft = min(max($divisibleMin, $closestDivisibleLeft), $divisibleMax); $acceptableRight = max(min($divisibleMax, $closestDivisibleRight), $divisibleMin); return abs($acceptableLeft - $qty) < abs($acceptableRight - $qty) ? $acceptableLeft : $acceptableRight; } /** * Around suggest qty * * @param \Magento\CatalogInventory\Api\StockStateInterface $subject * @param \Closure $proceed * @param int $productId * @param string $sourceCode * @param float $qty * @return float */ public function aroundSuggestSourceQty( \Magento\CatalogInventory\Api\StockStateInterface $subject, \Closure $proceed, $productId, $sourceCode, $qty ): float { try { return $this->suggestSourceQty($productId, $sourceCode, $qty); } catch (\Magento\Framework\Exception\LocalizedException $exception) { return $qty; } } }