Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
quantitycontrol.php
1<?php
2
4
6{
7 private static $values = [];
8
9 public const QUANTITY = 'quantity';
10 public const AVAILABLE_QUANTITY = 'available_quantity';
11 public const RESERVED_QUANTITY = 'reserved_quantity';
12 public const RESERVER_STORE_QUANTITY = 'reserved_store_quantity';
13
20
26 public static function resetAllQuantity($productId = null): void
27 {
28 static::resetValue(static::QUANTITY, $productId);
29 static::resetValue(static::AVAILABLE_QUANTITY, $productId);
30 static::resetValue(static::RESERVED_QUANTITY, $productId);
31 static::resetValue(static::RESERVER_STORE_QUANTITY, $productId);
32 }
33
40 public static function setQuantity($productId, $value): void
41 {
42 self::setValue(
43 static::QUANTITY,
44 $productId,
45 CatalogProvider::getDefaultStoreId(),
46 $value
47 );
48 }
49
56 public static function addQuantity($productId, $value): void
57 {
58 static::setValue(
59 static::QUANTITY,
60 $productId,
61 CatalogProvider::getDefaultStoreId(),
62 (float)$value + (float)static::getQuantity($productId)
63 );
64 }
65
71 public static function getQuantity($productId): ?float
72 {
73 return self::getValue(
74 static::QUANTITY,
75 CatalogProvider::getDefaultStoreId(),
76 $productId
77 );
78 }
79
85 public static function resetQuantity($productId): void
86 {
87 self::resetValue(static::QUANTITY, $productId);
88 }
89
96 public static function setReservedQuantity($productId, $value): void
97 {
98 self::setValue(
99 static::RESERVED_QUANTITY,
100 $productId,
101 CatalogProvider::getDefaultStoreId(),
102 $value
103 );
104 }
105
112 public static function addReservedQuantity($productId, $value): void
113 {
114 self::setValue(
115 static::RESERVED_QUANTITY,
116 $productId,
117 CatalogProvider::getDefaultStoreId(),
118 (float)$value + (float)static::getReservedQuantity($productId)
119 );
120 }
121
127 public static function getReservedQuantity($productId): ?float
128 {
129 return self::getValue(
130 static::RESERVED_QUANTITY,
131 CatalogProvider::getDefaultStoreId(),
132 $productId
133 );
134 }
135
141 public static function resetReservedQuantity($productId): void
142 {
143 self::resetValue(static::RESERVED_QUANTITY, $productId);
144 }
145
151 public static function setReservedQuantityFromStores($productId): void
152 {
153 self::setValue(
154 static::RESERVED_QUANTITY,
155 $productId,
156 CatalogProvider::getDefaultStoreId(),
157 self::getSummaryValue(
158 static::RESERVER_STORE_QUANTITY,
159 $productId
160 )
161 );
162 }
163
170 public static function setAvailableQuantity($productId, $value): void
171 {
172 self::setValue(
173 static::AVAILABLE_QUANTITY,
174 $productId,
175 CatalogProvider::getDefaultStoreId(),
176 $value
177 );
178 }
179
186 public static function addAvailableQuantity($productId, $value): void
187 {
188 self::setValue(
189 static::AVAILABLE_QUANTITY,
190 $productId,
191 CatalogProvider::getDefaultStoreId(),
192 (float)$value + (float)static::getAvailableQuantity($productId)
193 );
194 }
195
201 public static function getAvailableQuantity($productId): ?float
202 {
203 return self::getValue(
204 static::AVAILABLE_QUANTITY,
205 $productId,
206 CatalogProvider::getDefaultStoreId()
207 );
208 }
209
213 public static function resetAvailableQuantity($productId)
214 {
215 self::resetValue(static::AVAILABLE_QUANTITY, $productId);
216 }
217
226 public static function setReservedStoreQuantity($productId, int $storeId, $value, bool $summary = false): void
227 {
228 self::setValue(
229 static::RESERVER_STORE_QUANTITY,
230 $productId,
231 $storeId,
232 $value
233 );
234
235 if ($summary)
236 {
237 static::setReservedQuantity(
238 $productId,
239 self::getSummaryValue(
240 static::RESERVER_STORE_QUANTITY,
241 $productId
242 )
243 );
244 }
245 }
246
255 public static function addReservedStoreQuantity($productId, int $storeId, $value, bool $summary = false): void
256 {
257 self::setValue(
258 static::RESERVER_STORE_QUANTITY,
259 $productId,
260 $storeId,
261 (float)$value
262 + (float)static::getReservedStoreQuantity(
263 $productId,
264 $storeId,
265 )
266 );
267
268 if ($summary)
269 {
270 static::addReservedQuantity($productId, $value);
271 }
272 }
273
280 public static function getReservedStoreQuantity($productId, int $storeId): ?float
281 {
282 return self::getValue(
283 static::RESERVER_STORE_QUANTITY,
284 $productId,
285 $storeId
286 );
287 }
288
292 public static function resetReservedStoreQuantity($productId)
293 {
294 self::resetValue(static::RESERVER_STORE_QUANTITY, $productId);
295 }
296
303 private static function setValue(string $type, $productId, int $storeId, $value): void
304 {
305 if (!isset(self::$values[$type]))
306 {
307 self::$values[$type] = [];
308 }
309 if (isset(self::$values[$type][$productId]))
310 {
311 self::$values[$type][$productId] = [];
312 }
313 self::$values[$type][$productId][$storeId] = (float)$value;
314 }
315
323 private static function getValue(string $type, $productId, int $storeId): ?float
324 {
325 $value = null;
326 if (isset(self::$values[$type][$productId][$storeId]))
327 {
328 $value = self::$values[$type][$productId][$storeId];
329 }
330
331 return $value;
332 }
333
340 private static function resetValue(string $type, $productId = null): void
341 {
342 if ($productId == null)
343 {
344 unset(self::$values[$type]);
345 }
346 else
347 {
348 unset(self::$values[$type][$productId]);
349 }
350 }
351
352 private static function getSummaryValue(string $type, $productId): ?float
353 {
354 $result = null;
355 if (
356 isset(self::$values[$type][$productId])
357 && is_array(self::$values[$type][$productId])
358 )
359 {
360 $result = (float)array_sum(self::$values[$type][$productId]);
361 }
362
363 return $result;
364 }
365
372 public static function setValues($productId, array $values): void
373 {
374 static::resetAllQuantity($productId);
375
376 if (isset($values[static::QUANTITY]))
377 {
378 self::setValue(
379 static::QUANTITY,
380 $productId,
381 CatalogProvider::getDefaultStoreId(),
382 $values[static::QUANTITY]
383 );
384 }
385
386 if (isset($values[static::AVAILABLE_QUANTITY]))
387 {
388 self::setValue(
389 static::AVAILABLE_QUANTITY,
390 $productId,
391 CatalogProvider::getDefaultStoreId(),
392 $values[static::AVAILABLE_QUANTITY]
393 );
394 }
395
396 if (isset($values[static::RESERVED_QUANTITY]))
397 {
398 self::setValue(
399 static::RESERVED_QUANTITY,
400 $productId,
401 CatalogProvider::getDefaultStoreId(),
402 $values[static::RESERVED_QUANTITY]
403 );
404 }
405
406 if (
407 isset($values[static::RESERVER_STORE_QUANTITY])
408 && is_array($values[static::RESERVER_STORE_QUANTITY])
409 )
410 {
411 foreach ($values[static::RESERVER_STORE_QUANTITY] as $storeId => $storeValue)
412 {
413 self::setValue(
414 static::RESERVER_STORE_QUANTITY,
415 $productId,
416 $storeId,
417 $storeValue
418 );
419 }
420 }
421 }
422}
static setQuantity($productId, $value)
static addReservedQuantity($productId, $value)
static addReservedStoreQuantity($productId, int $storeId, $value, bool $summary=false)
static setAvailableQuantity($productId, $value)
static setValues($productId, array $values)
static setReservedQuantity($productId, $value)
static addAvailableQuantity($productId, $value)
static addQuantity($productId, $value)
static getReservedStoreQuantity($productId, int $storeId)
static setReservedStoreQuantity($productId, int $storeId, $value, bool $summary=false)