36 '=ID' => $documentElementId,
38 'select' => [
'*',
'DOCUMENT'],
44 if ($this->storeDocumentElement)
46 $this->productId = $this->storeDocumentElement->getElementId();
55 if ($this->storeDocumentElement)
57 $this->storeId = $this->storeDocumentElement->getStoreTo();
70 '=S_BATCH_ELEMENT.DOCUMENT_ELEMENT_ID' => $this->storeDocumentElement->getId(),
71 '>S_BATCH_ELEMENT.AMOUNT' => 0,
76 StoreBatchDocumentElementTable::class,
77 Join::on(
'this.ID',
'ref.PRODUCT_BATCH_ID')
80 'select' => [
'*',
'S_BATCH_ELEMENT'],
85 $batchIds = $storeBatches->getIdList();
91 foreach ($storeBatches as $batch)
93 if (CostPriceCalculator::getMethod() === CostPriceCalculator::METHOD_FIFO)
95 if ($this->storeDocumentElement->getAmount() > $batch->getAvailableAmount())
97 $this->rebindWriteOffValues($batch, $batchIds);
100 $resultDelete = $batch->delete();
101 if (!$resultDelete->isSuccess())
103 return $resultDelete;
109 $oldBatchElement = $batch->get(
'S_BATCH_ELEMENT');
110 $purchasingPrice = $oldBatchElement->getBatchPrice();
112 $this->storeDocumentElement->getDocument()
113 && $this->storeDocumentElement->getDocument()->getCurrency() !== $batch->getPurchasingCurrency()
114 && Loader::includeModule(
'currency')
117 $purchasingPrice = \CCurrencyRates::convertCurrency(
118 $this->storeDocumentElement->getPurchasingPrice(),
119 $this->storeDocumentElement->getDocument()->getCurrency(),
120 $batch->getPurchasingCurrency()
124 $newAmount = max($batch->getAvailableAmount() - $oldBatchElement->getAmount(), 0);
127 $newSum = $batch->getAvailableAmount() * $batch->getPurchasingPrice() - $oldBatchElement->getAmount() * $purchasingPrice;
128 $newPurchasingPrice = $newSum / $newAmount;
129 $precision = (int)Option::get(
'sale',
'value_precision', 2);
130 $newPurchasingPrice = round($newPurchasingPrice, $precision);
131 $batch->setPurchasingPrice($newPurchasingPrice);
133 $batch->setAvailableAmount($newAmount);
140 '=PRODUCT_BATCH_ID' => $batchIds,
141 '=DOCUMENT_ELEMENT_ID' => $this->storeDocumentElement->getId(),
146 while ($binding = $bindings->fetch())
162 private function rebindWriteOffValues(EO_StoreBatch $oldBatch, array $prohibitedBatchIds = []): void
164 $batchManager =
new BatchManager($oldBatch->getElementId());
165 $currentStoreCollection = $batchManager->getAvailableStoreCollection($oldBatch->getStoreId());
168 'PRODUCT_BATCH_ID' => $oldBatch->getId(),
173 $prohibitedBatchIds[] = $oldBatch->getId();
174 while ($binding = $bindings->fetch())
176 $amountBindingValue = abs($binding[
'AMOUNT']);
177 $oldAmount = $amountBindingValue;
178 foreach ($currentStoreCollection as $batchItem)
180 if (in_array($batchItem->getId(), $prohibitedBatchIds,
true))
185 $storeAmount = $batchItem->getAvailableAmount();
191 $outStoreQuantity = ($storeAmount > $oldAmount) ? $oldAmount : $storeAmount;
192 $newAvailableAmount = $storeAmount - $outStoreQuantity;
193 $batchItem->setAvailableAmount($newAvailableAmount);
194 if ($oldAmount === $amountBindingValue)
197 'PRODUCT_BATCH_ID' => $batchItem->getId(),
198 'AMOUNT' => -$outStoreQuantity,
206 'DOCUMENT_ELEMENT_ID' => $binding[
'DOCUMENT_ELEMENT_ID'],
207 'SHIPMENT_ITEM_STORE_ID' => $binding[
'SHIPMENT_ITEM_STORE_ID'],
208 'AMOUNT' => -$outStoreQuantity,
209 'PRODUCT_BATCH_ID' => $batchItem->getId(),
210 'BATCH_PRICE' => $binding[
'BATCH_PRICE'],
211 'BATCH_CURRENCY' => $binding[
'BATCH_CURRENCY'],
215 $oldAmount -= $storeAmount;
219 $currentStoreCollection->save();
static getList(array $parameters=array())
static update($primary, array $data)