Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
order.php
1<?php
2
4
12
18final class Order
19{
26 public static function getAggregateOrder(Sale\Order $order)
27 {
28 $profile = self::getProfileList([
29 'USER_ID' => $order->getUserId(),
30 'PERSON_TYPE_ID' => $order->getPersonTypeId()
31 ]);
32
33 return [
34 'ORDER' => $order->toArray(),
35 'PERSON_TYPE' => self::getPersonTypeList([
36 'ID'=>$order->getPersonTypeId()
37 ]),
38 'USER_PROFILE' => $profile,
39 'USER_PROFILE_VALUES' => self::getProfileListValues([
40 'USER_PROPS_ID' => ($profile['ID'] ?? 0),
41 ]),
42 'BASKET_ITEMS' => self::getOrderProducts($order),
43 'ORDER_PRICE_TOTAL' => self::getTotal($order),
44 'PAY_SYSTEMS' => self::getPaySystemListWithRestrictions($order),
45 'DELIVERY_SERVICES' => self::getDeliveryServiceListWithRestrictions($order),
46 'PROPERTIES' => self::getOrderProperties($order),
47 'VARIANTS' => self::getVariants($order),
48 'PAYMENTS' => self::getPayments($order),
49 'CHECKS' => self::getChecks($order),
50 ];
51 }
52
53 private static function getProfileList(array $filter = [])
54 {
55 $result = [];
56 $r = \CSaleOrderUserProps::GetList(
57 [
58 'ID' => 'DESC',
59 ],
60 $filter,
61 false,
62 false,
63 ['ID', 'USER_ID', 'NAME', 'PERSON_TYPE_ID']
64 );
65 if ($profile = $r->fetch())
66 {
67 $result = $profile;
68 }
69
70 return $result;
71 }
72
73 private static function getProfileListValues(array $filter = []): array
74 {
75 $result = [];
76 $r = \CSaleOrderUserPropsValue::GetList(
77 [
78 'ID' => 'DESC',
79 ],
80 $filter,
81 false,
82 false,
83 ['ID', 'ORDER_PROPS_ID', 'VALUE', 'SORT']
84 );
85 while ($profileValue = $r->fetch())
86 {
87 $result[] = $profileValue;
88 }
89
90 return $result;
91 }
92
93 private static function getPersonTypeList(array $filter = [])
94 {
95 $registry = Sale\Registry::getInstance(Sale\Registry::REGISTRY_TYPE_ORDER);
97 $personTypeClassName = $registry->getPersonTypeClassName();
98 return $personTypeClassName::getList([
99 'select' => ['*'],
100 'filter' => $filter,
101 'order' => [
102 'SORT' => 'ASC',
103 'NAME' => 'ASC',
104 ],
105 ])->fetch();
106 }
107
108 private static function getPaySystemListWithRestrictions(Sale\Order $order): array
109 {
110 $result = [];
111
112 $paySystemList = Sale\PaySystem\Manager::getListWithRestrictionsByOrder($order);
113 foreach ($paySystemList as $paySystemItem)
114 {
115 $paySystemItem['LOGOTYPE'] = $paySystemItem['LOGOTIP'];
116 $paySystemItem['TARIFF'] = $paySystemItem['TARIF'];
117
118 $paySystemItem['LOGOTYPE_SRC'] = '';
119 if (!empty($paySystemItem['LOGOTYPE']))
120 {
121 $paySystemItem['LOGOTYPE_SRC'] = \CFile::GetPath($paySystemItem['LOGOTYPE']);
122 }
123
124 unset(
125 $paySystemItem['PAY_SYSTEM_ID'],
126 $paySystemItem['PERSON_TYPE_ID'],
127 $paySystemItem['PARAMS'],
128 $paySystemItem['TARIF'],
129 $paySystemItem['LOGOTIP'],
130 $paySystemItem['ENTITY_REGISTRY_TYPE']
131 );
132
133 $result[] = $paySystemItem;
134 }
135
136 return $result;
137 }
138
139 private static function getDeliveryServiceListWithRestrictions(Sale\Order $order): array
140 {
141 $result = [];
142 foreach ($order->getShipmentCollection() as $shipment)
143 {
144 $deliveryList = Sale\Delivery\Services\Manager::getRestrictedObjectsList($shipment);
145 foreach ($deliveryList as $deliveryItem)
146 {
147 $result[] = [
148 'ID' => $deliveryItem->getId(),
149 'SORT' => $deliveryItem->getSort(),
150 'NAME' => $deliveryItem->getName(),
151 'DESCRIPTION' => $deliveryItem->getDescription(),
152 'LOGOTYPE' => $deliveryItem->getLogotip(),
153 'LOGOTYPE_SRC' => \CFile::GetPath($deliveryItem->getLogotip()),
154 ];
155 }
156 }
157
158 return $result;
159 }
160
161 public static function getTotal(Sale\Order $order)
162 {
164 //$basket = $order->getBasket();
165
166 $calculateBasket = $order->getBasket()->createClone();
167
168 $discounts = $order->getDiscount();
169 $showPrices = $discounts->getShowPrices();
170 if (!empty($showPrices['BASKET']))
171 {
172 foreach ($showPrices['BASKET'] as $basketCode => $data)
173 {
174 $basketItem = $calculateBasket->getItemByBasketCode($basketCode);
175 if ($basketItem instanceof Sale\BasketItemBase)
176 {
177 $basketItem->setFieldNoDemand('BASE_PRICE', $data['SHOW_BASE_PRICE']);
178 $basketItem->setFieldNoDemand('PRICE', $data['SHOW_PRICE']);
179 $basketItem->setFieldNoDemand('DISCOUNT_PRICE', $data['SHOW_DISCOUNT']);
180 }
181 }
182 }
183 unset($showPrices);
184
185 $result = [
186 'WEIGHT_UNIT' => Config\Option::get('sale', 'weight_unit', false, $order->getSiteId()),
187 'WEIGHT_KOEF' => Config\Option::get('sale', 'weight_koef', 1, $order->getSiteId()),
188 ];
189
190/* $result['BASKET_POSITIONS'] = $basket->count();
191 $result['ORDER_PRICE'] = PriceMaths::roundPrecision($basket->getPrice());
192 $result['ORDER_WEIGHT'] = $basket->getWeight();
193
194 $result['PRICE_WITHOUT_DISCOUNT_VALUE'] = $basket->getBasePrice();
195 $result['BASKET_PRICE_DISCOUNT_DIFF_VALUE'] = PriceMaths::roundPrecision(
196 $basket->getBasePrice() - $basket->getPrice()
197 );
198 $result['DISCOUNT_PRICE'] = PriceMaths::roundPrecision(
199 $order->getDiscountPrice() + ($result['PRICE_WITHOUT_DISCOUNT_VALUE'] - $result['ORDER_PRICE'])
200 ); */
201
202 $result['BASKET_POSITIONS'] = $calculateBasket->count();
203 $result['ORDER_PRICE'] = PriceMaths::roundPrecision($calculateBasket->getPrice());
204 $result['ORDER_WEIGHT'] = $calculateBasket->getWeight();
205
206 $result['PRICE_WITHOUT_DISCOUNT_VALUE'] = $calculateBasket->getBasePrice();
207 $result['BASKET_PRICE_DISCOUNT_DIFF_VALUE'] = PriceMaths::roundPrecision(
208 $calculateBasket->getBasePrice() - $calculateBasket->getPrice()
209 );
210 $result['DISCOUNT_PRICE'] = PriceMaths::roundPrecision(
211 $order->getDiscountPrice() + ($result['PRICE_WITHOUT_DISCOUNT_VALUE'] - $result['ORDER_PRICE'])
212 );
213
214 $result['DELIVERY_PRICE'] = PriceMaths::roundPrecision($order->getDeliveryPrice());
215 $result['ORDER_TOTAL_PRICE'] = PriceMaths::roundPrecision($order->getPrice());
216
217 return $result;
218 }
219
220 private static function getOrderProducts(Sale\Order $order): array
221 {
222 $result = [];
223
224 $basket = $order->getBasket();
225 if ($basket)
226 {
227 $result = static::getOrderProductsByBasket($basket);
228 }
229
230 return $result;
231 }
232
233 public static function getOrderProductsByBasket(Sale\BasketBase $basket): array
234 {
235 $result = [];
236
237 $basketClone = $basket->createClone();
238
239 $order = $basketClone->getOrder();
240 if (!$order)
241 {
242 $registry = Sale\Registry::getInstance(Sale\Registry::REGISTRY_TYPE_ORDER);
244 $orderClassName = $registry->getOrderClassName();
245 $order = $orderClassName::create($basketClone->getSiteId());
246 $order->setBasket($basketClone);
247 }
248
249 if ($order)
250 {
251 $discounts = $order->getDiscount();
252 $showPrices = $discounts->getShowPrices();
253 if (!empty($showPrices['BASKET']))
254 {
255 foreach ($showPrices['BASKET'] as $basketCode => $data)
256 {
257 $basketItem = $basketClone->getItemByBasketCode($basketCode);
258 if ($basketItem instanceof Sale\BasketItemBase)
259 {
260 $basketItem->setFieldNoDemand('BASE_PRICE', $data['SHOW_BASE_PRICE']);
261 $basketItem->setFieldNoDemand('PRICE', $data['SHOW_PRICE']);
262 $basketItem->setFieldNoDemand('DISCOUNT_PRICE', $data['SHOW_DISCOUNT']);
263 }
264 }
265 }
266 }
267
268 $basketData = static::getBasketProducts($basketClone);
269 foreach ($basketClone as $item)
270 {
271 $result[] = array_merge(
272 $basketData[$item->getId()],
273 [
274 'CATALOG_PRODUCT' => static::getCatalogProduct($basketData[$item->getId()])
275 ]
276 );
277 }
278
279 return $result;
280 }
281
282 public static function getOrderProductByBasketItem(Sale\BasketItemBase $basketItem): array
283 {
284 $basket = $basketItem->getBasket();
285 $basketClone = $basket->createClone();
286 $calculateBasketItem = $basketClone->getItemByBasketCode($basketItem->getBasketCode());
287
288 $order = $basketClone->getOrder();
289 if (!$order)
290 {
291 $registry = Sale\Registry::getInstance(Sale\Registry::REGISTRY_TYPE_ORDER);
293 $orderClassName = $registry->getOrderClassName();
294 $order = $orderClassName::create($basketClone->getSiteId());
295 $order->setBasket($basketClone);
296 }
297
298 if ($calculateBasketItem && $order)
299 {
300 $discounts = $order->getDiscount();
301 $showPrices = $discounts->getShowPrices();
302 if (!empty($showPrices['BASKET']))
303 {
304 $data = $showPrices['BASKET'][$calculateBasketItem->getBasketCode()] ?? null;
305 if ($data)
306 {
307 $calculateBasketItem->setFieldNoDemand('BASE_PRICE', $data['SHOW_BASE_PRICE']);
308 $calculateBasketItem->setFieldNoDemand('PRICE', $data['SHOW_PRICE']);
309 $calculateBasketItem->setFieldNoDemand('DISCOUNT_PRICE', $data['SHOW_DISCOUNT']);
310 }
311 }
312 }
313
314 $basketItemData = static::getBasketProduct($calculateBasketItem);
315 return array_merge(
316 $basketItemData,
317 [
318 'CATALOG_PRODUCT' => static::getCatalogProduct($basketItemData)
319 ]
320 );
321 }
322
323 private static function getCatalogProduct(array $basketItemData): array
324 {
325 $result = [];
326
327 $repositoryFacade = Catalog\v2\IoC\ServiceContainer::getRepositoryFacade();
328 $product = $repositoryFacade->loadVariation($basketItemData['PRODUCT_ID']);
329 if ($product)
330 {
331 $result = $product->getFields();
332
333 $result['TYPE'] = ($result['TYPE'] === Catalog\ProductTable::TYPE_SERVICE) ? 'service' : 'product';
334
335 $result['PREVIEW_PICTURE'] ??= null;
336 if ((int)$result['PREVIEW_PICTURE'] > 0)
337 {
338 $result['PREVIEW_PICTURE_SRC'] = \CFile::GetPath($result['PREVIEW_PICTURE']);
339 }
340
341 $result['DETAIL_PICTURE'] ??= null;
342 if ((int)$result['DETAIL_PICTURE'] > 0)
343 {
344 $result['DETAIL_PICTURE_SRC'] = \CFile::GetPath($result['DETAIL_PICTURE']);
345 }
346
347 $result['AVAILABLE_QUANTITY'] = $result['QUANTITY'];
348 unset($result['QUANTITY']);
349
350 if ($result['QUANTITY_TRACE'] === Catalog\ProductTable::STATUS_DEFAULT)
351 {
352 $result['QUANTITY_TRACE'] = (Main\Config\Option::get('catalog', 'default_quantity_trace') === 'Y') ? 'Y' : 'N';
353 }
354 if ($result['CAN_BUY_ZERO'] === Catalog\ProductTable::STATUS_DEFAULT)
355 {
356 $result['CAN_BUY_ZERO'] = (Main\Config\Option::get('catalog', 'default_can_buy_zero') === 'Y') ? 'Y' : 'N';
357 }
358
359 $checkMaxQuantity = ($result['QUANTITY_TRACE'] === 'Y' && $result['CAN_BUY_ZERO'] === 'N') ? 'Y' : 'N';
360 $result['CHECK_MAX_QUANTITY'] = $checkMaxQuantity;
361
362 $result['RATIO'] = 1;
363 $ratioItem = $product->getMeasureRatioCollection()->findDefault();
364 if (!$ratioItem)
365 {
366 $ratioItem = $product->getMeasureRatioCollection()->getFirst();
367 }
368
369 if ($ratioItem)
370 {
371 $result['RATIO'] = $ratioItem->getRatio();
372 }
373
375 $propertyCollection = $product->getPropertyCollection();
376 foreach ($propertyCollection as $propertyItem)
377 {
378 $values = $propertyItem->getPropertyValueCollection()->getValues();
379
380 if ($propertyItem->getPropertyType() === Iblock\PropertyTable::TYPE_LIST)
381 {
382 $enumPropData = Iblock\PropertyEnumerationTable::getList([
383 'select' => ['ID', 'VALUE', 'SORT', 'XML_ID'],
384 'filter' => [
385 '=ID' => $values,
386 '=PROPERTY_ID' => $propertyItem->getId(),
387 ],
388 ])->fetchAll();
389
390 if ($enumPropData)
391 {
392 if (!$propertyItem->isMultiple())
393 {
394 $values = reset($enumPropData);
395 }
396 }
397 else
398 {
399 $values = null;
400 }
401 }
402 elseif ($propertyItem->getPropertyType() === Iblock\PropertyTable::TYPE_FILE)
403 {
404 $imageSrcValues = null;
405 if ($propertyItem->isMultiple())
406 {
407 $imageSrcValues = [];
408 foreach ($values as $value)
409 {
410 $imageSrcValues[] = [
411 'FILE_ID' => $value,
412 'SRC' => \CFile::GetPath($value),
413 ];
414 }
415 }
416 else
417 {
418 $imageSrcValues = [
419 'FILE_ID' => $values,
420 'SRC' => \CFile::GetPath($values),
421 ];
422 }
423
424 $values = $imageSrcValues;
425 }
426
427 $result['PRODUCT_PROPERTIES'][$propertyItem->getId()] = [
428 'TYPE' => $propertyItem->getPropertyType(),
429 'CODE' => $propertyItem->getCode(),
430 'NAME' => $propertyItem->getName(),
431 'VALUES' => $values,
432 ];
433 }
434
436 $imageCollection = $product->getImageCollection();
437 $frontImage = $imageCollection->getFrontImage();
438
439 $frontImageData = null;
440 if ($frontImage)
441 {
442 $frontImageData = $frontImage->getFields();
443 }
444 else
445 {
447 $parent = $product->getParent();
448 if ($parent)
449 {
450 $imageCollection = $parent->getImageCollection();
451 $parentFrontImage = $imageCollection->getFrontImage();
452 if ($parentFrontImage)
453 {
454 $frontImageData = $parentFrontImage->getFields();
455 }
456 }
457 }
458
459 $result['FRONT_IMAGE'] = $frontImageData;
460
461 $result['IMAGE_COLLECTION'] = [];
462 foreach ($imageCollection as $imageItem)
463 {
464 $result['IMAGE_COLLECTION'][] = $imageItem->getFields();
465 }
466
467 $result['SKU'] = self::getSkuTree($product->getIblockId(), $product->getId());
468 }
469
470 return $result;
471 }
472
473 private static function getSkuTree(int $iblockId, int $productId): array
474 {
475 $result = [];
476
477 $skuRepository = Catalog\v2\IoC\ServiceContainer::getSkuRepository($iblockId);
478 if ($skuRepository)
479 {
480 $sku = $skuRepository->getEntityById($productId);
481 if ($sku)
482 {
483 $parentProduct = $sku->getParent();
484 if ($parentProduct)
485 {
487 $skuTree = Catalog\v2\IoC\ServiceContainer::make('sku.tree', ['iblockId' => $iblockId]);
488
489 $parentProductId = $parentProduct->getId();
490 $skuId = $sku->getId();
491
492 $tree = $skuTree->loadJsonOffers([$parentProductId => $skuId]);
493 if (isset($tree[$parentProductId][$skuId]))
494 {
495 $result = [
496 'TREE' => $tree[$parentProductId][$skuId],
497 'PARENT_PRODUCT_ID' => $parentProductId,
498 ];
499 }
500 }
501 }
502 }
503
504 return $result;
505 }
506
507 private static function getBasketProducts(Sale\BasketBase $basket): array
508 {
509 $result = [];
510
512 foreach ($basket as $basketItem)
513 {
514 $result[$basketItem->getId()] = self::getBasketProduct($basketItem);
515 }
516
517 return $result;
518 }
519
520 private static function getBasketProduct(Sale\BasketItemBase $basketItem)
521 {
522 $arBasketItem = $basketItem->getFieldValues();
523 if ($basketItem->getVatRate() > 0)
524 {
525 $arBasketItem['VAT_VALUE'] = PriceMaths::roundPrecision($basketItem->getVat());
526 }
527
528 $arBasketItem['QUANTITY'] = $basketItem->getQuantity();
529 $arBasketItem['DISCOUNT_PRICE'] = PriceMaths::roundPrecision($basketItem->getDiscountPrice());
530
531 $arBasketItem['DISCOUNT_PRICE_PERCENT'] = 0;
532 if ($arBasketItem['CUSTOM_PRICE'] !== 'Y')
533 {
534 $arBasketItem['DISCOUNT_PRICE_PERCENT'] = Sale\Discount::calculateDiscountPercent(
535 $arBasketItem['BASE_PRICE'],
536 $arBasketItem['DISCOUNT_PRICE']
537 );
538 if ($arBasketItem['DISCOUNT_PRICE_PERCENT'] === null)
539 {
540 $arBasketItem['DISCOUNT_PRICE_PERCENT'] = 0;
541 }
542 else
543 {
544 $arBasketItem['DISCOUNT_PRICE_PERCENT'] = PriceMaths::roundPrecision($arBasketItem['DISCOUNT_PRICE_PERCENT']);
545 }
546 }
547
548 $arBasketItem['PROPS'] = [];
550 $propertyCollection = $basketItem->getPropertyCollection();
551 $propList = $propertyCollection->getPropertyValues();
552 foreach ($propList as &$prop)
553 {
554 if ($prop['CODE'] === 'CATALOG.XML_ID'
555 || $prop['CODE'] === 'PRODUCT.XML_ID'
556 || $prop['CODE'] === 'SUM_OF_CHARGE'
557 )
558 {
559 continue;
560 }
561
562 $prop = array_filter($prop, ['CSaleBasketHelper', 'filterFields']);
563 $arBasketItem['PROPS'][] = $prop;
564 }
565 unset($prop);
566
567 $arBasketItem['PRICE'] = PriceMaths::roundPrecision($basketItem->getPrice());
568 $arBasketItem['BASE_PRICE'] = PriceMaths::roundPrecision($basketItem->getBasePrice());
569
570 $arBasketItem['SUM'] = PriceMaths::roundPrecision($arBasketItem['PRICE'] * $basketItem->getQuantity());
571 $arBasketItem['SUM_BASE'] = PriceMaths::roundPrecision($basketItem->getBasePrice() * $basketItem->getQuantity());
572
573 $arBasketItem['SUM_DISCOUNT_DIFF'] = PriceMaths::roundPrecision($arBasketItem['SUM_BASE'] - $arBasketItem['SUM']);
574
575 $dimension = $basketItem->getField('DIMENSIONS');
576 if($dimension && \is_string($dimension) && \CheckSerializedData($dimension))
577 {
578 $arBasketItem['DIMENSIONS'] = unserialize($dimension, ['allowed_classes' => false]);
579 }
580
581 if (!empty($arBasketItem) && static::useCatalog())
582 {
583 $measure = getMeasures([$basketItem->getId() => $arBasketItem]);
584 $arBasketItem = $measure[$basketItem->getId()];
585 }
586
587 return $arBasketItem;
588 }
589
590 private static function useCatalog()
591 {
592 return Loader::includeModule('catalog');
593 }
594
595 private static function getOrderProperties(Sale\Order $order): array
596 {
597 $result = [];
598
599 $propertyCollection = $order->getPropertyCollection();
600 if ($propertyCollection)
601 {
602 $propertyCollectionData = $propertyCollection->getArray();
603 foreach ($propertyCollectionData['properties'] as $property)
604 {
605 if ($property['UTIL'] === 'Y')
606 {
607 continue;
608 }
609
610 $result[] = $property;
611 }
612 }
613
614 return $result;
615 }
616
617 private static function getVariants(Sale\Order $order): array
618 {
619 $propertyCollection = $order->getPropertyCollection();
620 if (!$propertyCollection)
621 {
622 return [];
623 }
624
625 $propertyCollectionData = $propertyCollection->getArray();
626 $propertyEnumIds = [];
627 foreach ($propertyCollectionData['properties'] as $property)
628 {
629 if ($property['TYPE'] === 'ENUM')
630 {
631 $propertyEnumIds[] = $property['ID'];
632 }
633 }
634
635 if (empty($propertyEnumIds))
636 {
637 return [];
638 }
639
640 $variants = Sale\Internals\OrderPropsVariantTable::getList([
641 'filter' => [
642 '=ORDER_PROPS_ID' => $propertyEnumIds,
643 ],
644 'order' => ['SORT' => 'ASC'],
645 ])->fetchAll();
646
647 return $variants;
648 }
649
650 private static function getPayments(Sale\Order $order): array
651 {
653 $orderClone = $order->createClone();
654 return $orderClone->getPaymentCollection()->toArray();
655 }
656
657 private static function getChecks(Sale\Order $order): array
658 {
659 $checks = [];
660
662 $orderClone = $order->createClone();
663
665 foreach ($orderClone->getPaymentCollection() as $payment)
666 {
667 $checkList = Sale\Cashbox\CheckManager::getCheckInfo($payment);
668 foreach ($checkList as $check)
669 {
670 $checks[] = $check;
671 }
672 }
673
674 return $checks;
675 }
676}
static getAggregateOrder(Sale\Order $order)
Definition order.php:26
static roundPrecision($value)