![]() 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/old/app/code/Cnc/Sales/Preference/Model/ |
<?php /** * Copyright (c) 2021 Kaliop Digital Commerce (https://digitalcommerce.kaliop.com) All Rights Reserved. * https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) * Cnc * Radosław Stępień <[email protected]> <[email protected]> */ namespace Cnc\Sales\Preference\Model; use Cnc\Checkout\Helper\Cart as CncCartHelper; use Magento\Framework\Exception\CouldNotSaveException; use Magento\Framework\Exception\InputException; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\Validation\ValidationException; use Magento\InventoryApi\Api\Data\SourceItemInterface; use Magento\InventoryApi\Api\SourceItemsSaveInterface; use Magento\InventoryConfigurationApi\Api\Data\StockItemConfigurationInterface; use Magento\InventoryConfigurationApi\Api\GetStockItemConfigurationInterface; use Magento\InventoryConfigurationApi\Exception\SkuIsNotAssignedToStockException; use Magento\InventorySalesApi\Api\GetStockBySalesChannelInterface; use Magento\InventorySourceDeductionApi\Model\GetSourceItemBySourceCodeAndSku; use Magento\InventorySourceDeductionApi\Model\SourceDeductionRequestInterface; use Magento\InventorySourceDeductionApi\Model\SourceDeductionService as BaseSourceDeductionService; use Magento\Inventory\Model\SourceItem\Command\DecrementSourceItemQty; class SourceDeductionService extends BaseSourceDeductionService { /** Constant for zero stock quantity value. */ private const ZERO_STOCK_QUANTITY = 0.0; /** @var SourceItemsSaveInterface */ private $sourceItemsSave; /** @var GetSourceItemBySourceCodeAndSku */ private $getSourceItemBySourceCodeAndSku; /** @var GetStockBySalesChannelInterface */ private $getStockBySalesChannel; /** @var GetStockItemConfigurationInterface */ private $getStockItemConfiguration; /** @var CncCartHelper */ private $cncCartHelper; /** * @param SourceItemsSaveInterface $sourceItemsSave * @param GetSourceItemBySourceCodeAndSku $getSourceItemBySourceCodeAndSku * @param GetStockItemConfigurationInterface $getStockItemConfiguration * @param GetStockBySalesChannelInterface $getStockBySalesChannel * @param CncCartHelper $cncCartHelper * @param DecrementSourceItemQty $decrementSourceItem */ public function __construct( SourceItemsSaveInterface $sourceItemsSave, GetSourceItemBySourceCodeAndSku $getSourceItemBySourceCodeAndSku, GetStockItemConfigurationInterface $getStockItemConfiguration, GetStockBySalesChannelInterface $getStockBySalesChannel, CncCartHelper $cncCartHelper, DecrementSourceItemQty $decrementSourceItem ) { parent::__construct( $getSourceItemBySourceCodeAndSku, $getStockItemConfiguration, $getStockBySalesChannel, $decrementSourceItem ); $this->sourceItemsSave = $sourceItemsSave; $this->getSourceItemBySourceCodeAndSku = $getSourceItemBySourceCodeAndSku; $this->getStockItemConfiguration = $getStockItemConfiguration; $this->getStockBySalesChannel = $getStockBySalesChannel; $this->cncCartHelper = $cncCartHelper; } /** * @Override due to avoid throw an exception about stock qty, for products with vhs enabled * * @param SourceDeductionRequestInterface $sourceDeductionRequest * @throws LocalizedException * @throws CouldNotSaveException * @throws InputException * @throws NoSuchEntityException * @throws ValidationException * @throws SkuIsNotAssignedToStockException */ public function execute(SourceDeductionRequestInterface $sourceDeductionRequest): void { $sourceItems = []; $sourceCode = $sourceDeductionRequest->getSourceCode(); $salesChannel = $sourceDeductionRequest->getSalesChannel(); $stockId = $this->getStockBySalesChannel->execute($salesChannel)->getStockId(); foreach ($sourceDeductionRequest->getItems() as $item) { $itemSku = $item->getSku(); $qty = $item->getQty(); $stockItemConfiguration = $this->getStockItemConfiguration->execute( $itemSku, $stockId ); if (!$stockItemConfiguration->isManageStock()) { //We don't need to Manage Stock continue; } $sourceItem = $this->getSourceItemBySourceCodeAndSku->execute($sourceCode, $itemSku); if (($sourceItem->getQuantity() - $qty) >= 0) { $sourceItem->setQuantity($sourceItem->getQuantity() - $qty); $stockStatus = $this->getSourceStockStatus( $stockItemConfiguration, $sourceItem ); $sourceItem->setStatus($stockStatus); $sourceItems[] = $sourceItem; //KDC MODIFICATION here - check if backorders are enabled for product before throw an exception } elseif ($this->cncCartHelper->isBackorderEnabled() && $this->cncCartHelper->isBackorderEnabledForProductAndSource($itemSku, $sourceCode)) { $sourceItem->setQuantity(0); $sourceItems[] = $sourceItem; //KDC MODIFICATION end } else { throw new LocalizedException( __('Not all of your products are available in the requested quantity.') ); } } if (!empty($sourceItems)) { $this->sourceItemsSave->execute($sourceItems); } } /** * @Override due to private access only * Get source item stock status after quantity deduction. * * @param StockItemConfigurationInterface $stockItemConfiguration * @param SourceItemInterface $sourceItem * @return int */ private function getSourceStockStatus( StockItemConfigurationInterface $stockItemConfiguration, SourceItemInterface $sourceItem ): int { $sourceItemQty = $sourceItem->getQuantity() ?: self::ZERO_STOCK_QUANTITY; return $sourceItemQty === $stockItemConfiguration->getMinQty() && !$stockItemConfiguration->getBackorders() ? SourceItemInterface::STATUS_OUT_OF_STOCK : SourceItemInterface::STATUS_IN_STOCK; } }