Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
firstschemebuilder.php
1<?php
3
8
16{
17 public function tryUnpack()
18 {
19 $result = new Main\Result();
20 if ($this->packedOrder)
21 {
22 $r = $this->packedOrder->tryUnpack();
23 if (!$r->isSuccess())
24 {
25 $errorData = [
26 'TYPE' => 'ORDER',
27 'FIELD' => $this->packedOrder->getPackedValue()
28 ];
29 $result->addError(new Main\Error('Unavailable order data for unpacking', 0, $errorData));
30 }
31 }
32 if ($this->packedBasketItems)
33 {
35 foreach ($this->packedBasketItems as $itemId => $item)
36 {
37 $r = $item->tryUnpack();
38 if (!$r->isSuccess())
39 {
40 $errorData = [
41 'TYPE' => 'BASKET_ITEM',
42 'ID' => $itemId,
43 'FIELD' => $item->getPackedValue()
44 ];
45 $result->addError(new Main\Error('Unavailable basket item data for unpacking', 0, $errorData));
46 }
47 }
48 }
49
50 return $result;
51 }
52
53 public function buildOrder()
54 {
55 $archivedOrderData = [];
56 $orderFields = $this->entitiesFields['ORDER'];
57 if (!empty($this->packedOrder))
58 {
59 $archivedOrderData = $this->packedOrder->unpack();
60 if (is_array($archivedOrderData['ORDER']))
61 {
62 $orderFields = array_merge($archivedOrderData['ORDER'], $orderFields);
63 }
64 }
65
66 $this->order = Archive\Order::create($orderFields['LID'], $orderFields['USER_ID'], $orderFields['CURRENCY']);
67 $this->order->setPersonTypeId($orderFields['PERSON_TYPE_ID']);
68
69 $registry = Sale\Registry::getInstance(Sale\Registry::REGISTRY_TYPE_ORDER);
70
72 $basketClassName = $registry->getBasketClassName();
73
74 $basket = $basketClassName::create($orderFields['LID']);
75 $this->order->setBasket($basket);
76 $basketItemsFields = $this->entitiesFields['BASKET'];
77 if (!empty($this->packedBasketItems))
78 {
79 foreach ($this->packedBasketItems as $basketArchiveId => $basketItem)
80 {
82 $basketItemData = $basketItem->unpack();
83 if (is_array($basketItemData))
84 {
85 $basketItemsFields[$basketArchiveId] = array_merge($basketItemsFields[$basketArchiveId], $basketItemData);
86 }
87 }
88 }
89 $basketItemsMap = $this->riseBasket($basketItemsFields);
90
91 $this->order->initFields($orderFields);
92 if (is_array($archivedOrderData['PAYMENT']) && !empty($archivedOrderData['PAYMENT']))
93 {
94 $this->risePayment($archivedOrderData['PAYMENT']);
95 }
96 if (is_array($archivedOrderData['SHIPMENT']) && !empty($archivedOrderData['SHIPMENT']))
97 {
98 $basketItemStoreMap = [];
99 if (is_array($basketItemsFields))
100 {
101 foreach ($basketItemsFields as $item)
102 {
103 $basketItemStoreMap[$item['ID']] = $item['SHIPMENT_BARCODE_ITEMS'];
104 }
105 }
106
107 $this->riseShipment($archivedOrderData['SHIPMENT'], $basketItemsMap, $basketItemStoreMap);
108 }
109
110 if (is_array($archivedOrderData['PROPERTIES']))
111 {
112 $this->riseOrderProperties($archivedOrderData['PROPERTIES']);
113 }
114 if (is_array($archivedOrderData['DISCOUNT']))
115 {
116 $this->riseDiscount($archivedOrderData['DISCOUNT']);
117 }
118 return $this->order;
119 }
120
121
129 protected function riseOrderProperties(array $propertyCollectionArchived = [])
130 {
131 $propertyCollection = $this->order->getPropertyCollection();
132 foreach ($propertyCollectionArchived as $propertyArchived)
133 {
134 $property = $propertyCollection->getItemByOrderPropertyId($propertyArchived['ORDER_PROPS_ID']);
135 if ($property)
136 {
137 $property->setField('VALUE', $propertyArchived['VALUE']);
138 }
139 }
140 return;
141 }
142
150 protected function riseBasket(array $archivedBasketItems = [])
151 {
152 $basketItemsMap = array();
153 $basket = $this->order->getBasket();
154 foreach ($archivedBasketItems as &$archivedItem)
155 {
156 if (empty($archivedItem['SET_PARENT_ID']))
157 {
159 $item = $basket->createItem($archivedItem['MODULE'], $archivedItem['PRODUCT_ID'], $archivedItem['ID']);
160 $this->riseBasketItem($item, $archivedItem);
161 $basketItemsMap[$archivedItem['ID']] = $item;
162 $type = $archivedItem['TYPE'];
163 unset($archivedItem);
164
165 if ($type == Sale\BasketItem::TYPE_SET)
166 {
167 $bundleCollection = $item->getBundleCollection();
168 foreach ($archivedBasketItems as &$bundle)
169 {
170 if ($item->getId() !== (int)$bundle['SET_PARENT_ID'])
171 continue;
172
174 $itemBundle = $bundleCollection->createItem($bundle['MODULE'], $bundle['PRODUCT_ID']);
175 $this->riseBasketItem($itemBundle, $bundle);
176 $basketItemsMap[$bundle['ID']] = $itemBundle;
177 unset($bundle);
178 }
179 }
180 }
181 }
182
183 return $basketItemsMap;
184 }
185
194 protected function riseBasketItem(&$item, $data = array())
195 {
196 $basketItemProperties = $data["PROPERTY_ITEMS"];
197 unset($data["PROPERTY_ITEMS"], $data["SHIPMENT_BARCODE_ITEMS"]);
198 $item->setFieldsNoDemand($data);
199 $newPropertyCollection = $item->getPropertyCollection();
200 if (is_array($basketItemProperties))
201 {
202 foreach ($basketItemProperties as $oldPropertyFields)
203 {
204 $propertyItem = $newPropertyCollection->createItem();
205 unset($oldPropertyFields['ID'], $oldPropertyFields['BASKET_ID']);
206
208 $propertyItem->setFieldsNoDemand($oldPropertyFields);
209 }
210 }
211 }
212
220 protected function risePayment(array $paymentCollectionArchived)
221 {
222 $paymentCollection = $this->order->getPaymentCollection();
223 foreach ($paymentCollectionArchived as $oldPayment)
224 {
226 $newPaymentItem = $paymentCollection->createItem();
227 $newPaymentItem->setFieldsNoDemand($oldPayment);
228 }
229 return;
230 }
231
241 protected function riseShipment(array $shipmentCollectionArchived, array $basketItemsMap, array $itemsStoreMap = [])
242 {
244 $shipmentCollection = $this->order->getShipmentCollection();
245 foreach ($shipmentCollectionArchived as $oldShipment)
246 {
247 $oldShipmentCollections = $oldShipment['SHIPMENT_ITEM'];
248 unset($oldShipment['SHIPMENT_ITEM']);
250 $newShipmentItem = $shipmentCollection->createItem();
251 $newShipmentItemCollection = $newShipmentItem->getShipmentItemCollection();
252 if (is_array($oldShipmentCollections))
253 {
254 foreach ($oldShipmentCollections as $oldItemStore)
255 {
256 $basketItemId = $oldItemStore['BASKET_ID'];
257
258 if (empty($basketItemsMap[$basketItemId]))
259 continue;
260
262 $shipmentItem = $newShipmentItemCollection->createItem($basketItemsMap[$basketItemId]);
263 $shipmentItem->setFieldsNoDemand($oldItemStore);
264 $shipmentItemStoreCollection = $shipmentItem->getShipmentItemStoreCollection();
265 if ($shipmentItemStoreCollection)
266 {
268 $itemStore = $shipmentItemStoreCollection->createItem($basketItemsMap[$basketItemId]);
269 $oldBasketBarcodeData = $itemsStoreMap[$basketItemId][$oldItemStore['ID']];
270 if (is_array($oldBasketBarcodeData) && empty($oldBasketBarcodeData))
271 {
272 $itemStore->setFieldsNoDemand($oldBasketBarcodeData);
273 }
274 }
275 }
276 }
277
278 $newShipmentItem->setFieldsNoDemand($oldShipment);
279 }
280 }
281
290 protected function riseDiscount($discountData)
291 {
292 $discountResultList = array();
293
294 $resultData = array(
295 'BASKET' => array(),
296 'ORDER' => array(),
297 'APPLY_BLOCKS' => array(),
298 'DISCOUNT_LIST' => array(),
299 'DISCOUNT_MODULES' => array(),
300 'COUPON_LIST' => array(),
301 'SHIPMENTS_ID' => array(),
302 'DATA' => array()
303 );
304
305 $resultData['DATA'] = $this->prepareDiscountOrderData($discountData);
306
307 $orderDiscountData = $resultData['DATA']['ORDER'];
308
309 $orderDiscountIndex =
310 $appliedBlocks =
311 $orderDiscountLink =
312 $couponAppliedList = [];
313
314 foreach ($discountData['RULES_DATA'] as $rule)
315 {
316 $discountList[] = $rule["DISCOUNT_DATA"];
317
318 if ($rule['APPLY_BLOCK_COUNTER'] < 0)
319 continue;
320
321 if ($rule['APPLY'] === 'Y' && (int)$rule['COUPON_ID'] > 0)
322 {
323 $couponAppliedList[] = (int)$rule['COUPON_ID'];
324 }
325
326 $blockCounter = $rule['APPLY_BLOCK_COUNTER'];
327
328 if (!isset($orderDiscountIndex[$blockCounter]))
329 $orderDiscountIndex[$blockCounter] = 0;
330
331 if (!isset($appliedBlocks[$blockCounter]))
332 {
333 $appliedBlocks[$blockCounter] = array(
334 'BASKET' => array(),
335 'BASKET_ROUND' => array(),
336 'ORDER' => array(),
337 'ORDER_ROUND' => array()
338 );
339 }
340
341 if ($rule['MODULE_ID'] == 'sale')
342 {
343 $orderDiscountId = (int)$rule['ORDER_DISCOUNT_ID'];
344 $orderDiscountItem = $orderDiscountLink[$orderDiscountId];
345 if (!isset($orderDiscountItem))
346 {
347 $appliedBlocks[$blockCounter]['ORDER'][$orderDiscountIndex[$blockCounter]] = array(
348 'ORDER_ID' => $rule['ORDER_ID'],
349 'DISCOUNT_ID' => $rule['ORDER_DISCOUNT_ID'],
350 'ORDER_COUPON_ID' => $rule['ORDER_COUPON_ID'],
351 'COUPON_ID' => ($rule['COUPON_ID'] > 0 ? $rule['COUPON_ID'] : ''),
352 'RESULT' => array(),
353 'ACTION_BLOCK_LIST' => true
354 );
355 $orderDiscountItem = &$appliedBlocks[$blockCounter]['ORDER'][$orderDiscountIndex[$blockCounter]];
356 $orderDiscountIndex[$blockCounter]++;
357 }
358
359 $ruleItem = array(
360 'RULE_ID' => $rule['ID'],
361 'APPLY' => $rule['APPLY'],
362 'RULE_DESCR_ID' => $rule['RULE_DESCR_ID'],
363 'ACTION_BLOCK_LIST' => (!empty($rule['ACTION_BLOCK_LIST']) && is_array($rule['ACTION_BLOCK_LIST']) ? $rule['ACTION_BLOCK_LIST'] : null)
364 );
365
366 if (!empty($rule['RULE_DESCR']) && is_array($rule['RULE_DESCR']))
367 {
368 $ruleItem['DESCR_DATA'] = $rule['RULE_DESCR'];
369 $ruleItem['DESCR'] = Sale\OrderDiscountManager::formatArrayDescription($rule['RULE_DESCR']);
370 $ruleItem['DESCR_ID'] = $rule['RULE_DESCR_ID'];
371 }
372
373 switch ($rule['ENTITY_TYPE'])
374 {
375 case Internals\OrderRulesTable::ENTITY_TYPE_BASKET_ITEM:
376 $ruleItem['BASKET_ID'] = $rule['ENTITY_ID'];
377 $index = $rule['ENTITY_ID'];
378 if (!isset($orderDiscountItem['RESULT']['BASKET']))
379 $orderDiscountItem['RESULT']['BASKET'] = array();
380
381 $orderDiscountItem['RESULT']['BASKET'][$index] = $ruleItem;
382 if ($ruleItem['ACTION_BLOCK_LIST'] === null)
383 $orderDiscountItem['ACTION_BLOCK_LIST'] = false;
384
385 $discountResultList['BASKET'][$ruleItem['BASKET_ID']][] = array(
386 'DISCOUNT_ID' => $orderDiscountItem['DISCOUNT_ID'],
387 'COUPON_ID' => $orderDiscountItem['COUPON_ID'],
388 'APPLY' => $ruleItem['APPLY'],
389 'DESCR' => $ruleItem['DESCR']
390 );
391 break;
392
393 case Internals\OrderRulesTable::ENTITY_TYPE_DELIVERY:
394 if (!isset($orderDiscountItem['RESULT']['DELIVERY']))
395 $orderDiscountItem['RESULT']['DELIVERY'] = array();
396
397 $ruleItem['DELIVERY_ID'] = ($rule['ENTITY_ID'] > 0 ? $rule['ENTITY_ID'] : (string)$rule['ENTITY_VALUE']);
398 $orderDiscountItem['RESULT']['DELIVERY'] = $ruleItem;
399
400 $discountResultList['DELIVERY'][] = array(
401 'DISCOUNT_ID' => $orderDiscountItem['DISCOUNT_ID'],
402 'COUPON_ID' => $orderDiscountItem['COUPON_ID'],
403 'APPLY' => $orderDiscountItem['RESULT']['DELIVERY']['APPLY'],
404 'DESCR' => $orderDiscountItem['RESULT']['DELIVERY']['DESCR'][0],
405 'DELIVERY_ID' => $orderDiscountItem['RESULT']['DELIVERY']['DELIVERY_ID']
406 );
407
408 foreach ($orderDiscountData as $data)
409 {
410 if ((int)$data['DELIVERY_ID'] == (int)$orderDiscountItem['RESULT']['DELIVERY']['DELIVERY_ID'])
411 $resultData['SHIPMENTS_ID'][] = (int)$data['SHIPMENT_ID'];
412 }
413 break;
414 }
415
416 $orderDiscountLink[$orderDiscountId] = $orderDiscountItem;
417 unset($ruleItem, $orderDiscountId);
418 }
419 else
420 {
421 if ($rule['ENTITY_ID'] <= 0 || $rule['ENTITY_TYPE'] != Internals\OrderRulesTable::ENTITY_TYPE_BASKET_ITEM)
422 continue;
423
424 $index = $rule['ENTITY_ID'];
425
426 $ruleResult = array(
427 'BASKET_ID' => $rule['ENTITY_ID'],
428 'RULE_ID' => $rule['ID'],
429 'ORDER_ID' => $rule['ORDER_ID'],
430 'DISCOUNT_ID' => $rule['ORDER_DISCOUNT_ID'],
431 'ORDER_COUPON_ID' => $rule['ORDER_COUPON_ID'],
432 'COUPON_ID' => ($rule['ORDER_COUPON_ID'] > 0 ? $rule['COUPON_ID'] : ''),
433 'RESULT' => array(
434 'APPLY' => $rule['APPLY']
435 ),
436 'RULE_DESCR_ID' => $rule['RULE_DESCR_ID'],
437 'ACTION_BLOCK_LIST' => (isset($rule['ACTION_BLOCK_LIST']) ? $rule['ACTION_BLOCK_LIST'] : null)
438 );
439
440 if (!empty($rule['RULE_DESCR']) && is_array($rule['RULE_DESCR']))
441 {
442 $ruleResult['RESULT']['DESCR_DATA'] = $rule['RULE_DESCR'];
443 $ruleResult['RESULT']['DESCR'] = Sale\OrderDiscountManager::formatArrayDescription($rule['RULE_DESCR']);
444 $ruleResult['DESCR_ID'] = $rule['RULE_DESCR_ID'];
445 }
446
447 if (!isset($appliedBlocks[$blockCounter]['BASKET'][$index]))
448 $appliedBlocks[$blockCounter]['BASKET'][$index] = array();
449 $appliedBlocks[$blockCounter]['BASKET'][$index][] = $ruleResult;
450
451 $discountResultList['BASKET'][$index][] = array(
452 'DISCOUNT_ID' => $ruleResult['DISCOUNT_ID'],
453 'COUPON_ID' => $ruleResult['COUPON_ID'],
454 'APPLY' => $ruleResult['RESULT']['APPLY'],
455 'DESCR' => $ruleResult['RESULT']['DESCR']
456 );
457
458 unset($ruleResult);
459 }
460 }
461
462 $resultData['APPLY_BLOCKS'] = $appliedBlocks;
463
464 $resultData['COUPON_LIST'] = is_array($discountData['COUPON_LIST']) ? $discountData['COUPON_LIST'] : [];
465 foreach ($resultData['COUPON_LIST'] as &$coupon)
466 {
467 if (in_array((int)$coupon['ID'], $couponAppliedList))
468 {
469 $coupon['APPLY'] = 'Y';
470 $coupon['JS_STATUS'] = 'APPLYED';
471 }
472 }
473
474 $resultData['PRICES'] = $this->prepareDiscountPrices();
475 $resultData['RESULT'] = $this->prepareDiscountResult($discountResultList);
476
477 if (!empty($discountList))
478 {
479 $resultData['DISCOUNT_LIST'] = $this->prepareDiscountList($discountList, $resultData['RESULT']);
480 }
481
482 $this->order->setDiscountData($resultData);
483 return;
484 }
485
491 protected function prepareDiscountPrices()
492 {
493 $resultData = array();
494 $basket = $this->order->getBasket();
495 $basketItems = $basket->getBasketItems();
496
498 foreach ($basketItems as $item)
499 {
500 $resultData['BASKET'][$item->getId()] = array(
501 'BASE_PRICE' => $item->getBasePrice(),
502 'PRICE' => $item->getPrice(),
503 'DISCOUNT' => $item->getDiscountPrice(),
504 );
505 }
506
507 $shipmentCollection = $this->order->getShipmentCollection();
508
510 foreach ($shipmentCollection as $shipment)
511 {
512 if ($shipment->isSystem())
513 continue;
514
515 $resultData['DELIVERY'][$shipment->getId()] = array(
516 'BASE_PRICE' => $shipment->getField("BASE_PRICE_DELIVERY"),
517 'PRICE' => $shipment->getPrice(),
518 'DISCOUNT' => $shipment->getField("BASE_PRICE_DELIVERY") - $shipment->getPrice(),
519 );
520 }
521
522 return $resultData;
523 }
524
532 protected function prepareDiscountResult($discountData)
533 {
534 $resultData = array();
535 $basket = $this->order->getBasket();
536 $basketItems = $basket->getBasketItems();
537
539 foreach ($basketItems as $item)
540 {
541 if (is_array($discountData['BASKET'][$item->getId()]))
542 {
543 $resultData['BASKET'][$item->getId()] = $discountData['BASKET'][$item->getId()];
544 }
545 }
546 $resultData['DELIVERY'] = $discountData['DELIVERY'];
547
548 return $resultData;
549 }
550
558 protected function prepareDiscountOrderData($dataRow)
559 {
560 $resultData = array();
561
562 foreach ($dataRow['ORDER_DATA'] as $data)
563 {
564 if (
565 $data['ENTITY_TYPE'] = Internals\OrderDiscountDataTable::ENTITY_TYPE_ORDER
566 && $data['ENTITY_ID'] = $this->order->getId()
567 && isset($data['ENTITY_DATA']['DELIVERY']['SHIPMENT_ID'])
568 )
569 {
570 $resultData['ORDER'][$data['ENTITY_DATA']['DELIVERY']['SHIPMENT_ID']] = $data['ENTITY_DATA']['DELIVERY'];
571 }
572
573 if ($data['ENTITY_TYPE'] == Internals\OrderDiscountDataTable::ENTITY_TYPE_BASKET_ITEM)
574 {
575 if (!isset($resultData['DATA']['BASKET']))
576 $resultData['BASKET'] = array();
577 $data['ENTITY_ID'] = (int)$data['ENTITY_ID'];
578 $resultData['BASKET'][$data['ENTITY_ID']] = $data['ENTITY_DATA'];
579 }
580 }
581 return $resultData;
582 }
583
592 protected function prepareDiscountList(array $discounts, array $discountResult)
593 {
594 $resultData = [];
595 $appliedDiscountIds = $this->getAppliedDiscountIds($discountResult);
596
597 foreach ($discounts as $discount)
598 {
599 $discount['ID'] = (int)$discount['ID'];
600 $discount['APPLY'] = in_array($discount['ID'], $appliedDiscountIds) ? 'Y' : 'N';
601 $discount['ORDER_DISCOUNT_ID'] = $discount['ID'];
602 $discount['SIMPLE_ACTION'] = true;
603 if (is_array($discount['ACTIONS_DESCR']['BASKET']))
604 {
605 foreach ($discount['ACTIONS_DESCR']['BASKET'] as &$description)
606 {
607 $description = Sale\OrderDiscountManager::formatDescription($description);
608 }
609 }
610
611 if ($discount['MODULE_ID'] == 'sale')
612 {
613 $discount['EDIT_PAGE_URL'] = Sale\OrderDiscountManager::getEditUrl(array('ID' => $discount['DISCOUNT_ID']));
614 }
615 $discount['DISCOUNT_ID'] = $discount['ID'];
616 $resultData[$discount['ID']] = $discount;
617 }
618
619 return $resultData;
620 }
621
627 protected function getAppliedDiscountIds(array $discountResult)
628 {
629 $idList = [];
630 if (is_array($discountResult['BASKET']))
631 {
632 foreach ($discountResult['BASKET'] as $discountList)
633 {
634 if (is_array($discountList))
635 {
636 foreach ($discountList as $discount)
637 {
638 if ($discount['APPLY'] === 'Y')
639 $idList[] = $discount['DISCOUNT_ID'];
640 }
641 }
642 }
643 }
644
645 if (is_array($discountResult['DELIVERY']))
646 {
647 foreach ($discountResult['DELIVERY'] as $discount)
648 {
649 if ($discount['APPLY'] === 'Y')
650 $idList[] = $discount['DISCOUNT_ID'];
651 }
652 }
653
654 return array_unique($idList);
655 }
656}
riseOrderProperties(array $propertyCollectionArchived=[])
prepareDiscountList(array $discounts, array $discountResult)