26 foreach ($this->elementBatchTree as $entity)
28 $currentCondition = $this->getStoreCondition($entity);
29 $element = $entity->getElement();
30 $batch = $currentCondition->getStoreBatch();
31 if ($entity->isArrivalElement())
33 $availableStoreStoke = $batch->getAvailableAmount() + $element->getAmount();
34 if (empty($batch->getPurchasingCurrency()))
36 $batch->setPurchasingCurrency($element->getBatchCurrency());
39 $documentPrice = $element->getBatchPrice();
41 !empty($batch->getPurchasingCurrency())
42 && $element->getBatchCurrency() !== $batch->getPurchasingCurrency()
43 && Loader::includeModule(
'currency')
46 $documentPrice = \CCurrencyRates::ConvertCurrency(
48 $element->getBatchCurrency(),
49 $batch->getPurchasingCurrency()
53 $newPrice = (($batch->getAvailableAmount() * $batch->getPurchasingPrice()) + ($element->getAmount() * $documentPrice)) / $availableStoreStoke;
54 $batch->setAvailableAmount($availableStoreStoke);
55 $batch->setPurchasingPrice($newPrice);
56 $sortedItems->push($entity);
58 if (!empty($negativeStoreItem))
61 foreach ($negativeStoreItem->getIterator() as $key => $negativeItem)
63 if ($batch->getAvailableAmount() <= 0)
68 $currentStock = $batch->getAvailableAmount() - $negativeItem->getAmount();
69 $entity = clone($negativeItem);
70 $newBindingElement = $entity->getElement();
71 if ($currentStock >= 0)
73 $newBindingElement->setBatchPrice($batch->getPurchasingPrice());
74 $newBindingElement->setBatchCurrency($batch->getPurchasingCurrency());
75 $batch->setAvailableAmount($currentStock);
76 $sortedItems->push($entity);
77 unset($negativeStoreItem[$key]);
81 $newBindingElement->setBatchPrice($batch->getPurchasingPrice());
82 $newBindingElement->setBatchCurrency($batch->getPurchasingCurrency());
83 $newBindingElement->setAmount($batch->getAvailableAmount());
84 $sortedItems->push($entity);
86 $negativeItem->getElement()->setAmount(-$currentStock);
87 $batch->setAvailableAmount(0);
94 $currentStock = $batch->getAvailableAmount() - $entity->getAmount();
95 if ($batch->getAvailableAmount() <= 0)
97 $negativeStoreItem[] = $entity;
99 elseif ($currentStock >= 0)
101 $element->setBatchPrice($batch->getPurchasingPrice());
102 $element->setBatchCurrency($batch->getPurchasingCurrency());
103 $sortedItems->push($entity);
107 $negativeItem = clone($entity);
108 $element->setBatchPrice($batch->getPurchasingPrice());
109 $element->setBatchCurrency($batch->getPurchasingCurrency());
110 $element->setAmount(-$batch->getAvailableAmount());
111 $sortedItems->push($entity);
113 $negativeItem->getElement()->setAmount(-$currentStock);
114 $negativeStoreItem->push($negativeItem);
116 $batch->setAvailableAmount(0);
122 foreach ($this->storeConditions as $item)
124 if ($item->getStoreBatch()->hasId())
127 'filter' => [
'=PRODUCT_BATCH_ID' => $item->getStoreBatch()->getId()],
131 while ($binding = $oldBindings->fetch())
136 $result = $item->save();
138 if (!$result->isSuccess())
145 foreach ($sortedItems as $item)
148 $storeItem = $this->storeConditions[$item->getStoreId()];
149 if (!empty($storeItem) && $storeItem->getStoreBatch())
151 $item->getElement()->setProductBatchId($storeItem->getStoreBatch()->getId());
152 $result = $item->save();
153 if (!$result->isSuccess())
169 if (isset($this->storeConditions[$entity->getStoreId()]))
171 return $this->storeConditions[$entity->getStoreId()];
174 $batch = $this->loadBatch($entity->getStoreId());
175 $batch->setAvailableAmount(0);
176 $batch->setPurchasingPrice(0);
181 $this->storeConditions[$entity->getStoreId()] = $newStoreItem;
183 return $this->storeConditions[$entity->getStoreId()];
186 private function loadBatch(
int $storeId): EO_StoreBatch
190 '=STORE_ID' => $storeId,
191 '=ELEMENT_ID' => $this->balancer->getProductId(),
204 $newBatch =
new EO_StoreBatch();
205 $newBatch->setStoreId($storeId);
206 $newBatch->setElementId($this->balancer->getProductId());
207 $newBatch->setPurchasingPrice(0);