12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- declare(strict_types=1);
- namespace Magento\InventorySourceDeductionApi\Model;
- use Magento\InventoryApi\Api\SourceItemsSaveInterface;
- use Magento\InventoryConfigurationApi\Api\GetStockItemConfigurationInterface;
- use Magento\InventorySalesApi\Api\GetStockBySalesChannelInterface;
- use Magento\InventorySalesApi\Api\StockResolverInterface;
- use Magento\Framework\Exception\LocalizedException;
- /**
- * @inheritdoc
- */
- class SourceDeductionService implements SourceDeductionServiceInterface
- {
- /**
- * @var SourceItemsSaveInterface
- */
- private $sourceItemsSave;
- /**
- * @var GetSourceItemBySourceCodeAndSku
- */
- private $getSourceItemBySourceCodeAndSku;
- /**
- * @var GetStockItemConfigurationInterface
- */
- private $getStockItemConfiguration;
- /**
- * @var GetStockBySalesChannelInterface
- */
- private $getStockBySalesChannel;
- /**
- * @param SourceItemsSaveInterface $sourceItemsSave
- * @param GetSourceItemBySourceCodeAndSku $getSourceItemBySourceCodeAndSku
- * @param GetStockItemConfigurationInterface $getStockItemConfiguration
- * @param GetStockBySalesChannelInterface $getStockBySalesChannel
- */
- public function __construct(
- SourceItemsSaveInterface $sourceItemsSave,
- GetSourceItemBySourceCodeAndSku $getSourceItemBySourceCodeAndSku,
- GetStockItemConfigurationInterface $getStockItemConfiguration,
- GetStockBySalesChannelInterface $getStockBySalesChannel
- ) {
- $this->sourceItemsSave = $sourceItemsSave;
- $this->getSourceItemBySourceCodeAndSku = $getSourceItemBySourceCodeAndSku;
- $this->getStockItemConfiguration = $getStockItemConfiguration;
- $this->getStockBySalesChannel = $getStockBySalesChannel;
- }
- /**
- * @inheritdoc
- */
- 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);
- $sourceItems[] = $sourceItem;
- } else {
- throw new LocalizedException(
- __('Not all of your products are available in the requested quantity.')
- );
- }
- }
- if (!empty($sourceItems)) {
- $this->sourceItemsSave->execute($sourceItems);
- }
- }
- }
|