23 $negativeStoreItems = [];
25 foreach ($this->elementBatchTree as $entity)
27 $element = $entity->getElement();
29 if ($entity->isArrivalElement())
31 $newBatch =
new EO_StoreBatch();
32 $newBatch->setStoreId($entity->getStoreId());
33 $newBatch->setAvailableAmount($entity->getAmount());
34 $newBatch->setElementId($this->balancer->getProductId());
35 $newBatch->setPurchasingPrice($element->getBatchPrice());
36 $newBatch->setPurchasingCurrency($element->getBatchCurrency());
39 $entity->setStoreItemHash($newStoreItem->getHash());
40 $sortedItems->push($entity);
42 $negativeStoreItem = $negativeStoreItems[$entity->getStoreId()] ??
null;
43 if (!empty($negativeStoreItem))
46 foreach ($negativeStoreItem as $key => $negativeItem)
48 if ($negativeItem->getStoreId() !== $entity->getStoreId())
55 $currentStock = $storeCondition->getStoreBatch()->getAvailableAmount() - $negativeItem->getAmount();
57 clone($negativeItem->getElement()),
58 $negativeItem->getStoreId()
60 $element = $resortedEntity->getElement();
61 $element->setBatchPrice($storeCondition->getStoreBatch()->getPurchasingPrice());
62 $element->setBatchCurrency($storeCondition->getStoreBatch()->getPurchasingCurrency());
63 $resortedEntity->setStoreItemHash($storeCondition->getHash());
64 if ($currentStock >= 0)
66 $sortedItems->push($resortedEntity);
67 $storeCondition->getStoreBatch()->setAvailableAmount($currentStock);
69 unset($negativeStoreItem[$key]);
73 $element->setAmount($storeCondition->getStoreBatch()->getAvailableAmount());
74 $sortedItems->push($resortedEntity);
76 $storeCondition->getStoreBatch()->setAvailableAmount(0);
78 $negativeItem->getElement()->setAmount(-$currentStock);
86 $fullCompleted =
false;
90 $batch = $storeCondition->getStoreBatch();
91 if ($batch->getAvailableAmount() <= 0)
96 $currentStock = $batch->getAvailableAmount() - $entity->getAmount();
97 if ($currentStock >= 0)
99 $batch->setAvailableAmount($currentStock);
100 $entity->setStoreItemHash($storeCondition->getHash());
101 $element->setBatchPrice($storeCondition->getStoreBatch()->getPurchasingPrice());
102 $element->setBatchCurrency($storeCondition->getStoreBatch()->getPurchasingCurrency());
103 $sortedItems->push($entity);
104 $fullCompleted =
true;
111 $newBinding->setStoreItemHash($storeCondition->getHash());
112 $newElement = $newBinding->getElement();
113 $newElement->setAmount(-$batch->getAvailableAmount());
114 $newElement->setBatchPrice($storeCondition->getStoreBatch()->getPurchasingPrice());
115 $newElement->setBatchCurrency($storeCondition->getStoreBatch()->getPurchasingCurrency());
116 $sortedItems->push($newBinding);
117 $batch->setAvailableAmount(0);
118 $element->setAmount($currentStock);
124 $negativeItem = clone($entity);
125 $negativeStoreItems[$negativeItem->getStoreId()] ??= [];
126 $negativeStoreItems[$negativeItem->getStoreId()] = $negativeItem;
135 'filter' => [
'=ELEMENT_ID' => $this->balancer->getProductId()],
139 while ($batch = $oldBatches->fetch())
141 $oldBatchIds[] = $batch[
'ID'];
146 'filter' => [
'=PRODUCT_BATCH_ID' => $oldBatchIds],
150 while ($binding = $oldBindings->fetch())
155 foreach ($this->storeConditions as $storeCondition)
157 foreach ($storeCondition as $item)
159 $result = $item->save();
160 if (!$result->isSuccess())
168 foreach ($sortedItems as $item)
170 if (isset($this->storeConditions[$item->getStoreId()][$item->getStoreItemHash()]))
173 $storeItem = $this->storeConditions[$item->getStoreId()][$item->getStoreItemHash()];
174 $item->getElement()->setProductBatchId($storeItem->getStoreBatch()->getId());
175 $result = $item->save();
176 if (!$result->isSuccess())
188 if (isset($this->storeConditions[$entity->getStoreId()]))
190 return $this->storeConditions[$entity->getStoreId()];
193 $this->storeConditions[$entity->getStoreId()] = [];
195 return $this->storeConditions[$entity->getStoreId()];
198 private function setStoreConditions(
int $storeId, array
$storeConditions):
void
200 $this->storeConditions[$storeId] ??= [];