Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
All Data Structures Namespaces Files Functions Variables Pages
PriorityBasedAlgorithm.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
16 use Magento\InventorySourceSelectionApi\Api\Data\SourceSelectionItemInterfaceFactory;
17 use Magento\InventorySourceSelectionApi\Api\Data\SourceSelectionResultInterfaceFactory;
20 
26 {
30  private $getSourcesAssignedToStockOrderedByPriority;
31 
35  private $sourceSelectionItemFactory;
36 
40  private $sourceSelectionResultFactory;
41 
45  private $searchCriteriaBuilder;
46 
50  private $sourceItemRepository;
51 
59  public function __construct(
60  GetSourcesAssignedToStockOrderedByPriorityInterface $getSourcesAssignedToStockOrderedByPriority,
61  SourceSelectionItemInterfaceFactory $sourceSelectionItemFactory,
62  SourceSelectionResultInterfaceFactory $sourceSelectionResultFactory,
63  SearchCriteriaBuilder $searchCriteriaBuilder,
64  SourceItemRepositoryInterface $sourceItemRepository
65  ) {
66  $this->getSourcesAssignedToStockOrderedByPriority = $getSourcesAssignedToStockOrderedByPriority;
67  $this->sourceSelectionItemFactory = $sourceSelectionItemFactory;
68  $this->sourceSelectionResultFactory = $sourceSelectionResultFactory;
69  $this->searchCriteriaBuilder = $searchCriteriaBuilder;
70  $this->sourceItemRepository = $sourceItemRepository;
71  }
72 
77  {
78  $isShippable = true;
79  $stockId = $inventoryRequest->getStockId();
80  $sources = $this->getEnabledSourcesOrderedByPriorityByStockId($stockId);
81  $sourceItemSelections = [];
82 
83  foreach ($inventoryRequest->getItems() as $item) {
84  $itemSku = $item->getSku();
85  $qtyToDeliver = $item->getQty();
86  foreach ($sources as $source) {
87  $sourceItem = $this->getSourceItemBySourceCodeAndSku($source->getSourceCode(), $itemSku);
88  if (null === $sourceItem) {
89  continue;
90  }
91 
93  continue;
94  }
95 
96  $sourceItemQty = $sourceItem->getQuantity();
97  $qtyToDeduct = min($sourceItemQty, $qtyToDeliver);
98 
99  // check if source has some qty of SKU, so it's possible to take them into account
100  if ($this->isZero((float)$sourceItemQty)) {
101  continue;
102  }
103 
104  $sourceItemSelections[] = $this->sourceSelectionItemFactory->create([
105  'sourceCode' => $sourceItem->getSourceCode(),
106  'sku' => $itemSku,
107  'qtyToDeduct' => $qtyToDeduct,
108  'qtyAvailable' => $sourceItemQty
109  ]);
110 
111  $qtyToDeliver -= $qtyToDeduct;
112  }
113 
114  // if we go throw all sources from the stock and there is still some qty to delivery,
115  // then it doesn't have enough items to delivery
116  if (!$this->isZero($qtyToDeliver)) {
117  $isShippable = false;
118  }
119  }
120 
121  return $this->sourceSelectionResultFactory->create(
122  [
123  'sourceItemSelections' => $sourceItemSelections,
124  'isShippable' => $isShippable
125  ]
126  );
127  }
128 
136  private function isZero(float $floatNumber): bool
137  {
138  return $floatNumber < 0.0000001;
139  }
140 
150  private function getEnabledSourcesOrderedByPriorityByStockId(int $stockId): array
151  {
152  $sources = $this->getSourcesAssignedToStockOrderedByPriority->execute($stockId);
153  $sources = array_filter($sources, function (SourceInterface $source) {
154  return $source->isEnabled();
155  });
156  return $sources;
157  }
158 
166  private function getSourceItemBySourceCodeAndSku(string $sourceCode, string $sku)
167  {
168  $searchCriteria = $this->searchCriteriaBuilder
170  ->addFilter(SourceItemInterface::SKU, $sku)
171  ->create();
172  $sourceItemsResult = $this->sourceItemRepository->getList($searchCriteria);
173 
174  return $sourceItemsResult->getTotalCount() > 0 ? current($sourceItemsResult->getItems()) : null;
175  }
176 }
__construct(GetSourcesAssignedToStockOrderedByPriorityInterface $getSourcesAssignedToStockOrderedByPriority, SourceSelectionItemInterfaceFactory $sourceSelectionItemFactory, SourceSelectionResultInterfaceFactory $sourceSelectionResultFactory, SearchCriteriaBuilder $searchCriteriaBuilder, SourceItemRepositoryInterface $sourceItemRepository)
$source
Definition: source.php:23
$searchCriteria
$sourceCode
Definition: inventory.phtml:11
$searchCriteriaBuilder
$sourceItemRepository