Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
average.php
1<?php
3
10use Bitrix\Catalog\EO_StoreBatch;
11
17final class Average extends Base
18{
19 public function fill(): Result
20 {
21 $result = new Result();
22
23 $sortedItems = new ElementBatchTree();
24 $negativeStoreItem = new ElementBatchTree();
26 foreach ($this->elementBatchTree as $entity)
27 {
28 $currentCondition = $this->getStoreCondition($entity);
29 $element = $entity->getElement();
30 $batch = $currentCondition->getStoreBatch();
31 if ($entity->isArrivalElement())
32 {
33 $availableStoreStoke = $batch->getAvailableAmount() + $element->getAmount();
34 if (empty($batch->getPurchasingCurrency()))
35 {
36 $batch->setPurchasingCurrency($element->getBatchCurrency());
37 }
38
39 $documentPrice = $element->getBatchPrice();
40 if (
41 !empty($batch->getPurchasingCurrency())
42 && $element->getBatchCurrency() !== $batch->getPurchasingCurrency()
43 && Loader::includeModule('currency')
44 )
45 {
46 $documentPrice = \CCurrencyRates::ConvertCurrency(
47 $documentPrice,
48 $element->getBatchCurrency(),
49 $batch->getPurchasingCurrency()
50 );
51 }
52
53 $newPrice = (($batch->getAvailableAmount() * $batch->getPurchasingPrice()) + ($element->getAmount() * $documentPrice)) / $availableStoreStoke;
54 $batch->setAvailableAmount($availableStoreStoke);
55 $batch->setPurchasingPrice($newPrice);
56 $sortedItems->push($entity);
57
58 if (!empty($negativeStoreItem))
59 {
61 foreach ($negativeStoreItem->getIterator() as $key => $negativeItem)
62 {
63 if ($batch->getAvailableAmount() <= 0)
64 {
65 break;
66 }
67
68 $currentStock = $batch->getAvailableAmount() - $negativeItem->getAmount();
69 $entity = clone($negativeItem);
70 $newBindingElement = $entity->getElement();
71 if ($currentStock >= 0)
72 {
73 $newBindingElement->setBatchPrice($batch->getPurchasingPrice());
74 $newBindingElement->setBatchCurrency($batch->getPurchasingCurrency());
75 $batch->setAvailableAmount($currentStock);
76 $sortedItems->push($entity);
77 unset($negativeStoreItem[$key]);
78 }
79 else
80 {
81 $newBindingElement->setBatchPrice($batch->getPurchasingPrice());
82 $newBindingElement->setBatchCurrency($batch->getPurchasingCurrency());
83 $newBindingElement->setAmount($batch->getAvailableAmount());
84 $sortedItems->push($entity);
85
86 $negativeItem->getElement()->setAmount(-$currentStock);
87 $batch->setAvailableAmount(0);
88 }
89 }
90 }
91 }
92 else
93 {
94 $currentStock = $batch->getAvailableAmount() - $entity->getAmount();
95 if ($batch->getAvailableAmount() <= 0)
96 {
97 $negativeStoreItem[] = $entity;
98 }
99 elseif ($currentStock >= 0)
100 {
101 $element->setBatchPrice($batch->getPurchasingPrice());
102 $element->setBatchCurrency($batch->getPurchasingCurrency());
103 $sortedItems->push($entity);
104 }
105 else
106 {
107 $negativeItem = clone($entity);
108 $element->setBatchPrice($batch->getPurchasingPrice());
109 $element->setBatchCurrency($batch->getPurchasingCurrency());
110 $element->setAmount(-$batch->getAvailableAmount());
111 $sortedItems->push($entity);
112
113 $negativeItem->getElement()->setAmount(-$currentStock);
114 $negativeStoreItem->push($negativeItem);
115
116 $batch->setAvailableAmount(0);
117 }
118 }
119 }
120
122 foreach ($this->storeConditions as $item)
123 {
124 if ($item->getStoreBatch()->hasId())
125 {
127 'filter' => ['=PRODUCT_BATCH_ID' => $item->getStoreBatch()->getId()],
128 'select' => ['ID'],
129 ]);
130
131 while ($binding = $oldBindings->fetch())
132 {
134 }
135 }
136 $result = $item->save();
137
138 if (!$result->isSuccess())
139 {
140 return $result;
141 }
142 }
143
145 foreach ($sortedItems as $item)
146 {
148 $storeItem = $this->storeConditions[$item->getStoreId()];
149 if (!empty($storeItem) && $storeItem->getStoreBatch())
150 {
151 $item->getElement()->setProductBatchId($storeItem->getStoreBatch()->getId());
152 $result = $item->save();
153 if (!$result->isSuccess())
154 {
155 return $result;
156 }
157 }
158 }
159
160 return $result;
161 }
162
167 private function getStoreCondition(Entity\ElementBatchItem $entity): Entity\StoreItem
168 {
169 if (isset($this->storeConditions[$entity->getStoreId()]))
170 {
171 return $this->storeConditions[$entity->getStoreId()];
172 }
173
174 $batch = $this->loadBatch($entity->getStoreId());
175 $batch->setAvailableAmount(0);
176 $batch->setPurchasingPrice(0);
177 $newStoreItem = new Entity\StoreItem(
178 $batch
179 );
180
181 $this->storeConditions[$entity->getStoreId()] = $newStoreItem;
182
183 return $this->storeConditions[$entity->getStoreId()];
184 }
185
186 private function loadBatch(int $storeId): EO_StoreBatch
187 {
188 $batchRaw = StoreBatchTable::getList([
189 'filter' => [
190 '=STORE_ID' => $storeId,
191 '=ELEMENT_ID' => $this->balancer->getProductId(),
192 ],
193 'select' => ['ID'],
194 'limit' => 1,
195 ])
196 ->fetchObject()
197 ;
198
199 if ($batchRaw)
200 {
201 return $batchRaw;
202 }
203
204 $newBatch = new EO_StoreBatch();
205 $newBatch->setStoreId($storeId);
206 $newBatch->setElementId($this->balancer->getProductId());
207 $newBatch->setPurchasingPrice(0);
208
209 return $newBatch;
210 }
211}
static getList(array $parameters=array())