Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
base.php
1<?php
3
4use Bitrix\Catalog\EO_StoreBatchDocumentElement;
5use Bitrix\Catalog\EO_StoreBatchDocumentElement_Collection;
6use Bitrix\Catalog\EO_StoreBatch;
14
20abstract class Base
21{
22 protected ?int $storeId;
24
26 {
27 $this->batchManager = $batchManager;
28 $this->storeId = $storeId;
29 }
30
31 public function setStoreId(int $storeId): static
32 {
33 if ($storeId <= 0)
34 {
35 return $this;
36 }
37
38 $this->storeId = $storeId;
39
40 return $this;
41 }
42
43 abstract protected function addRegistryItem(EO_StoreBatch $batchItem, float $amount): Result;
44 abstract protected function getRegistryItems(): EO_StoreBatchDocumentElement_Collection;
45
46 public function writeOff(float $quantity): Result
47 {
48 $storeCollection = $this->batchManager->getAvailableStoreCollection($this->storeId);
49
50 foreach ($storeCollection as $batchItem)
51 {
52 $amount = $batchItem->getAvailableAmount();
53 if ($quantity <= 0)
54 {
55 break;
56 }
57
58 $outStoreQuantity = ($amount > $quantity) ? $quantity : $amount;
59 $newAvailableAmount = $amount - $outStoreQuantity;
60 $batchItem->setAvailableAmount($newAvailableAmount);
61
62 $this->addRegistryItem($batchItem, $outStoreQuantity);
63
64 $quantity -= $amount;
65 }
66
67 return $storeCollection->save();
68 }
69
70 public function return(): Result
71 {
72 $result = new Result();
73 $items = $this->getRegistryItems();
74
75 if ($items->isEmpty())
76 {
77 $result->addError(new Error('Shipment item was not found'));
78
79 return $result;
80 }
81
82 $storeCollection = $this->batchManager->getStoreCollection([
83 '=ID' => $items->getProductBatchIdList(),
84 ]);
85
86 foreach ($items as $item)
87 {
88 $batchItem = $storeCollection->getByPrimary($item->getProductBatchId());
89 if ($batchItem === null)
90 {
91 continue;
92 }
93
95 {
96 $newPurchasingPrice = $this->recalculateAveragePurchasingPrice($batchItem, $item);
97
98 $batchItem->setPurchasingPrice($newPurchasingPrice);
99 }
100
101 $currentAmount = $batchItem->getAvailableAmount();
102 $batchItem->setAvailableAmount($currentAmount + abs($item->getAmount()));
103 }
104
105 $result = $storeCollection->save();
106 if ($result->isSuccess())
107 {
108 foreach ($items as $item)
109 {
111 }
112 }
113
114 return $result;
115 }
116
117 private function recalculateAveragePurchasingPrice(
118 EO_StoreBatch $batch,
119 EO_StoreBatchDocumentElement $item
120 ): float
121 {
122 $itemBatchPrice = $item->getBatchPrice();
123 if (
124 $item->getBatchCurrency() !== $batch->getPurchasingCurrency()
125 && Loader::includeModule('currency')
126 )
127 {
128 $itemBatchPrice = \CCurrencyRates::convertCurrency(
129 $itemBatchPrice,
130 $item->getBatchCurrency(),
131 $batch->getPurchasingCurrency()
132 );
133 }
134
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);
139
140 return round($newPurchasingPrice, $precision);
141 }
142}
addRegistryItem(EO_StoreBatch $batchItem, float $amount)
__construct(BatchManager $batchManager, int $storeId)
Definition base.php:25