44 abstract protected function getRegistryItems(): EO_StoreBatchDocumentElement_Collection;
48 $storeCollection = $this->batchManager->getAvailableStoreCollection($this->storeId);
50 foreach ($storeCollection as $batchItem)
52 $amount = $batchItem->getAvailableAmount();
58 $outStoreQuantity = ($amount > $quantity) ? $quantity : $amount;
59 $newAvailableAmount = $amount - $outStoreQuantity;
60 $batchItem->setAvailableAmount($newAvailableAmount);
67 return $storeCollection->save();
75 if ($items->isEmpty())
77 $result->addError(
new Error(
'Shipment item was not found'));
82 $storeCollection = $this->batchManager->getStoreCollection([
83 '=ID' => $items->getProductBatchIdList(),
86 foreach ($items as $item)
88 $batchItem = $storeCollection->getByPrimary($item->getProductBatchId());
89 if ($batchItem ===
null)
96 $newPurchasingPrice = $this->recalculateAveragePurchasingPrice($batchItem, $item);
98 $batchItem->setPurchasingPrice($newPurchasingPrice);
101 $currentAmount = $batchItem->getAvailableAmount();
102 $batchItem->setAvailableAmount($currentAmount + abs($item->getAmount()));
105 $result = $storeCollection->save();
106 if ($result->isSuccess())
108 foreach ($items as $item)
117 private function recalculateAveragePurchasingPrice(
118 EO_StoreBatch $batch,
119 EO_StoreBatchDocumentElement $item
122 $itemBatchPrice = $item->getBatchPrice();
124 $item->getBatchCurrency() !== $batch->getPurchasingCurrency()
125 && Loader::includeModule(
'currency')
128 $itemBatchPrice = \CCurrencyRates::convertCurrency(
130 $item->getBatchCurrency(),
131 $batch->getPurchasingCurrency()
135 $itemAmount = abs($item->getAmount());
136 $sum = $itemBatchPrice * $itemAmount + $batch->getPurchasingPrice() * $batch->getAvailableAmount();
137 $newPurchasingPrice = $sum / ($itemAmount + $batch->getAvailableAmount());
138 $precision = (int)Option::get(
'sale',
'value_precision', 2);
140 return round($newPurchasingPrice, $precision);