SourceDeductionService.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\InventorySourceDeductionApi\Model;
  8. use Magento\InventoryApi\Api\SourceItemsSaveInterface;
  9. use Magento\InventoryConfigurationApi\Api\GetStockItemConfigurationInterface;
  10. use Magento\InventorySalesApi\Api\GetStockBySalesChannelInterface;
  11. use Magento\InventorySalesApi\Api\StockResolverInterface;
  12. use Magento\Framework\Exception\LocalizedException;
  13. /**
  14. * @inheritdoc
  15. */
  16. class SourceDeductionService implements SourceDeductionServiceInterface
  17. {
  18. /**
  19. * @var SourceItemsSaveInterface
  20. */
  21. private $sourceItemsSave;
  22. /**
  23. * @var GetSourceItemBySourceCodeAndSku
  24. */
  25. private $getSourceItemBySourceCodeAndSku;
  26. /**
  27. * @var GetStockItemConfigurationInterface
  28. */
  29. private $getStockItemConfiguration;
  30. /**
  31. * @var GetStockBySalesChannelInterface
  32. */
  33. private $getStockBySalesChannel;
  34. /**
  35. * @param SourceItemsSaveInterface $sourceItemsSave
  36. * @param GetSourceItemBySourceCodeAndSku $getSourceItemBySourceCodeAndSku
  37. * @param GetStockItemConfigurationInterface $getStockItemConfiguration
  38. * @param GetStockBySalesChannelInterface $getStockBySalesChannel
  39. */
  40. public function __construct(
  41. SourceItemsSaveInterface $sourceItemsSave,
  42. GetSourceItemBySourceCodeAndSku $getSourceItemBySourceCodeAndSku,
  43. GetStockItemConfigurationInterface $getStockItemConfiguration,
  44. GetStockBySalesChannelInterface $getStockBySalesChannel
  45. ) {
  46. $this->sourceItemsSave = $sourceItemsSave;
  47. $this->getSourceItemBySourceCodeAndSku = $getSourceItemBySourceCodeAndSku;
  48. $this->getStockItemConfiguration = $getStockItemConfiguration;
  49. $this->getStockBySalesChannel = $getStockBySalesChannel;
  50. }
  51. /**
  52. * @inheritdoc
  53. */
  54. public function execute(SourceDeductionRequestInterface $sourceDeductionRequest): void
  55. {
  56. $sourceItems = [];
  57. $sourceCode = $sourceDeductionRequest->getSourceCode();
  58. $salesChannel = $sourceDeductionRequest->getSalesChannel();
  59. $stockId = $this->getStockBySalesChannel->execute($salesChannel)->getStockId();
  60. foreach ($sourceDeductionRequest->getItems() as $item) {
  61. $itemSku = $item->getSku();
  62. $qty = $item->getQty();
  63. $stockItemConfiguration = $this->getStockItemConfiguration->execute(
  64. $itemSku,
  65. $stockId
  66. );
  67. if (!$stockItemConfiguration->isManageStock()) {
  68. //We don't need to Manage Stock
  69. continue;
  70. }
  71. $sourceItem = $this->getSourceItemBySourceCodeAndSku->execute($sourceCode, $itemSku);
  72. if (($sourceItem->getQuantity() - $qty) >= 0) {
  73. $sourceItem->setQuantity($sourceItem->getQuantity() - $qty);
  74. $sourceItems[] = $sourceItem;
  75. } else {
  76. throw new LocalizedException(
  77. __('Not all of your products are available in the requested quantity.')
  78. );
  79. }
  80. }
  81. if (!empty($sourceItems)) {
  82. $this->sourceItemsSave->execute($sourceItems);
  83. }
  84. }
  85. }