Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
availablequantitycalculator.php
1<?php
2
4
41{
47 private $storeProductQuantity = [];
53 private $reservationHistory = [];
54
63 public function setStoreQuantity(int $storeId, int $productId, float $quantity): void
64 {
65 $this->storeProductQuantity[$storeId][$productId] = $quantity;
66 }
67
78 public function addReservationHistory(int $storeId, int $productId, int $basketId, float $quantity): void
79 {
80 $this->reservationHistory[] = compact(
81 'storeId',
82 'productId',
83 'basketId',
84 'quantity',
85 );
86 }
87
96 private function getPreparedReservationHistory(): array
97 {
98 $reverseHistory = array_reverse($this->reservationHistory);
99 $negativeReservations = [];
100
101 $tmp = [];
102 foreach ($reverseHistory as $item)
103 {
104 $storeId = $item['storeId'];
105 $productId = $item['productId'];
106 $basketId = $item['basketId'];
107 $quantity = $item['quantity'];
108
109 $key = join("_", [
110 $storeId,
111 $productId,
112 $basketId,
113 ]);
114
115 if ($quantity < 0.0)
116 {
117 $negativeReservations[$key] = $negativeReservations[$key] ?? 0.0;
118 $negativeReservations[$key] += abs($quantity);
119 continue;
120 }
121 elseif (isset($negativeReservations[$key]))
122 {
123 $negativeQuantity = $negativeReservations[$key];
124 if ($negativeQuantity >= $quantity)
125 {
126 $negativeQuantity -= $quantity;
127 if ($negativeQuantity > 0)
128 {
129 $negativeReservations[$key] = $negativeQuantity;
130 }
131 else
132 {
133 unset($negativeReservations[$key]);
134 }
135 continue;
136 }
137 else
138 {
139 $item['quantity'] -= $negativeQuantity;
140 unset($negativeReservations[$key]);
141 }
142 }
143
144 $tmp[] = $item;
145 }
146
147 return array_reverse($tmp);
148 }
149
158 public function getQuantityForItem(int $productId, int $basketId, int $storeId): float
159 {
160 $basketItemsStoreQuantity = $this->getQuantityForBatch([
161 $basketId => $productId,
162 ]);
163 return $basketItemsStoreQuantity[$basketId][$storeId] ?? 0.0;
164 }
165
172 public function getQuantityForBatch(array $basket2productId): array
173 {
174 $basketItemsStoreQuantity = [];
175 $currentStoreProductQuantity = $this->storeProductQuantity;
176 $preparedReservationHistory = $this->getPreparedReservationHistory();
177
178 foreach ($preparedReservationHistory as $item)
179 {
180 $storeId = $item['storeId'];
181 $productId = $item['productId'];
182 $basketId = $item['basketId'];
183
184 $reservationQuantity = $item['quantity'];
185 $storeQuantity = $currentStoreProductQuantity[$storeId][$productId] ?? 0.0;
186
187 $isNeedBasketReservation = isset($basket2productId[$basketId]);
188 if ($isNeedBasketReservation)
189 {
190 if ($storeQuantity > 0)
191 {
192 $basketItemsStoreQuantity[$basketId][$storeId] ??= 0.0;
193 $basketItemsStoreQuantity[$basketId][$storeId] += min($storeQuantity, $reservationQuantity);
194 }
195 }
196
197 if (isset($currentStoreProductQuantity[$storeId][$productId]))
198 {
199 $currentStoreProductQuantity[$storeId][$productId] -= $reservationQuantity;
200 }
201 }
202
203 foreach ($basket2productId as $basketId => $productId)
204 {
205 foreach ($currentStoreProductQuantity as $storeId => $quantities)
206 {
207 if (
208 isset($basketItemsStoreQuantity[$basketId])
209 && !isset($basketItemsStoreQuantity[$basketId][$storeId])
210 )
211 {
212 continue;
213 }
214 $storeQuantity = $quantities[$productId] ?? 0.0;
215 if ($storeQuantity > 0.0)
216 {
217 $basketItemsStoreQuantity[$basketId][$storeId] ??= 0.0;
218 $basketItemsStoreQuantity[$basketId][$storeId] += $storeQuantity;
219 }
220 }
221 }
222
223 return $basketItemsStoreQuantity;
224 }
225}
setStoreQuantity(int $storeId, int $productId, float $quantity)
getQuantityForItem(int $productId, int $basketId, int $storeId)
addReservationHistory(int $storeId, int $productId, int $basketId, float $quantity)