20 if ($this->packedOrder)
22 $r = $this->packedOrder->tryUnpack();
27 'FIELD' => $this->packedOrder->getPackedValue()
29 $result->addError(
new Main\
Error(
'Unavailable order data for unpacking', 0, $errorData));
32 if ($this->packedBasketItems)
35 foreach ($this->packedBasketItems as $itemId => $item)
37 $r = $item->tryUnpack();
41 'TYPE' =>
'BASKET_ITEM',
43 'FIELD' => $item->getPackedValue()
45 $result->addError(
new Main\
Error(
'Unavailable basket item data for unpacking', 0, $errorData));
55 $archivedOrderData = [];
56 $orderFields = $this->entitiesFields[
'ORDER'];
57 if (!empty($this->packedOrder))
59 $archivedOrderData = $this->packedOrder->unpack();
60 if (is_array($archivedOrderData[
'ORDER']))
62 $orderFields = array_merge($archivedOrderData[
'ORDER'], $orderFields);
66 $this->order = Archive\Order::create($orderFields[
'LID'], $orderFields[
'USER_ID'], $orderFields[
'CURRENCY']);
67 $this->order->setPersonTypeId($orderFields[
'PERSON_TYPE_ID']);
72 $basketClassName = $registry->getBasketClassName();
74 $basket = $basketClassName::create($orderFields[
'LID']);
75 $this->order->setBasket($basket);
76 $basketItemsFields = $this->entitiesFields[
'BASKET'];
77 if (!empty($this->packedBasketItems))
79 foreach ($this->packedBasketItems as $basketArchiveId => $basketItem)
82 $basketItemData = $basketItem->unpack();
83 if (is_array($basketItemData))
85 $basketItemsFields[$basketArchiveId] = array_merge($basketItemsFields[$basketArchiveId], $basketItemData);
89 $basketItemsMap = $this->riseBasket($basketItemsFields);
91 $this->order->initFields($orderFields);
92 if (is_array($archivedOrderData[
'PAYMENT']) && !empty($archivedOrderData[
'PAYMENT']))
94 $this->risePayment($archivedOrderData[
'PAYMENT']);
96 if (is_array($archivedOrderData[
'SHIPMENT']) && !empty($archivedOrderData[
'SHIPMENT']))
98 $basketItemStoreMap = [];
99 if (is_array($basketItemsFields))
101 foreach ($basketItemsFields as $item)
103 $basketItemStoreMap[$item[
'ID']] = $item[
'SHIPMENT_BARCODE_ITEMS'];
107 $this->riseShipment($archivedOrderData[
'SHIPMENT'], $basketItemsMap, $basketItemStoreMap);
110 if (is_array($archivedOrderData[
'PROPERTIES']))
114 if (is_array($archivedOrderData[
'DISCOUNT']))
131 $propertyCollection = $this->order->getPropertyCollection();
132 foreach ($propertyCollectionArchived as $propertyArchived)
134 $property = $propertyCollection->getItemByOrderPropertyId($propertyArchived[
'ORDER_PROPS_ID']);
137 $property->setField(
'VALUE', $propertyArchived[
'VALUE']);
150 protected function riseBasket(array $archivedBasketItems = [])
152 $basketItemsMap = array();
153 $basket = $this->order->getBasket();
154 foreach ($archivedBasketItems as &$archivedItem)
156 if (empty($archivedItem[
'SET_PARENT_ID']))
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);
167 $bundleCollection = $item->getBundleCollection();
168 foreach ($archivedBasketItems as &$bundle)
170 if ($item->getId() !== (
int)$bundle[
'SET_PARENT_ID'])
174 $itemBundle = $bundleCollection->createItem($bundle[
'MODULE'], $bundle[
'PRODUCT_ID']);
175 $this->riseBasketItem($itemBundle, $bundle);
176 $basketItemsMap[$bundle[
'ID']] = $itemBundle;
183 return $basketItemsMap;
194 protected function riseBasketItem(&$item, $data = array())
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))
202 foreach ($basketItemProperties as $oldPropertyFields)
204 $propertyItem = $newPropertyCollection->createItem();
205 unset($oldPropertyFields[
'ID'], $oldPropertyFields[
'BASKET_ID']);
208 $propertyItem->setFieldsNoDemand($oldPropertyFields);
220 protected function risePayment(array $paymentCollectionArchived)
222 $paymentCollection = $this->order->getPaymentCollection();
223 foreach ($paymentCollectionArchived as $oldPayment)
226 $newPaymentItem = $paymentCollection->createItem();
227 $newPaymentItem->setFieldsNoDemand($oldPayment);
241 protected function riseShipment(array $shipmentCollectionArchived, array $basketItemsMap, array $itemsStoreMap = [])
244 $shipmentCollection = $this->order->getShipmentCollection();
245 foreach ($shipmentCollectionArchived as $oldShipment)
247 $oldShipmentCollections = $oldShipment[
'SHIPMENT_ITEM'];
248 unset($oldShipment[
'SHIPMENT_ITEM']);
250 $newShipmentItem = $shipmentCollection->createItem();
251 $newShipmentItemCollection = $newShipmentItem->getShipmentItemCollection();
252 if (is_array($oldShipmentCollections))
254 foreach ($oldShipmentCollections as $oldItemStore)
256 $basketItemId = $oldItemStore[
'BASKET_ID'];
258 if (empty($basketItemsMap[$basketItemId]))
262 $shipmentItem = $newShipmentItemCollection->createItem($basketItemsMap[$basketItemId]);
263 $shipmentItem->setFieldsNoDemand($oldItemStore);
264 $shipmentItemStoreCollection = $shipmentItem->getShipmentItemStoreCollection();
265 if ($shipmentItemStoreCollection)
268 $itemStore = $shipmentItemStoreCollection->createItem($basketItemsMap[$basketItemId]);
269 $oldBasketBarcodeData = $itemsStoreMap[$basketItemId][$oldItemStore[
'ID']];
270 if (is_array($oldBasketBarcodeData) && empty($oldBasketBarcodeData))
272 $itemStore->setFieldsNoDemand($oldBasketBarcodeData);
278 $newShipmentItem->setFieldsNoDemand($oldShipment);
292 $discountResultList = array();
297 'APPLY_BLOCKS' => array(),
298 'DISCOUNT_LIST' => array(),
299 'DISCOUNT_MODULES' => array(),
300 'COUPON_LIST' => array(),
301 'SHIPMENTS_ID' => array(),
307 $orderDiscountData = $resultData[
'DATA'][
'ORDER'];
309 $orderDiscountIndex =
312 $couponAppliedList = [];
314 foreach ($discountData[
'RULES_DATA'] as $rule)
316 $discountList[] = $rule[
"DISCOUNT_DATA"];
318 if ($rule[
'APPLY_BLOCK_COUNTER'] < 0)
321 if ($rule[
'APPLY'] ===
'Y' && (
int)$rule[
'COUPON_ID'] > 0)
323 $couponAppliedList[] = (int)$rule[
'COUPON_ID'];
326 $blockCounter = $rule[
'APPLY_BLOCK_COUNTER'];
328 if (!isset($orderDiscountIndex[$blockCounter]))
329 $orderDiscountIndex[$blockCounter] = 0;
331 if (!isset($appliedBlocks[$blockCounter]))
333 $appliedBlocks[$blockCounter] = array(
335 'BASKET_ROUND' => array(),
337 'ORDER_ROUND' => array()
341 if ($rule[
'MODULE_ID'] ==
'sale')
343 $orderDiscountId = (int)$rule[
'ORDER_DISCOUNT_ID'];
344 $orderDiscountItem = $orderDiscountLink[$orderDiscountId];
345 if (!isset($orderDiscountItem))
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'] :
''),
353 'ACTION_BLOCK_LIST' => true
355 $orderDiscountItem = &$appliedBlocks[$blockCounter][
'ORDER'][$orderDiscountIndex[$blockCounter]];
356 $orderDiscountIndex[$blockCounter]++;
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)
366 if (!empty($rule[
'RULE_DESCR']) && is_array($rule[
'RULE_DESCR']))
368 $ruleItem[
'DESCR_DATA'] = $rule[
'RULE_DESCR'];
369 $ruleItem[
'DESCR'] = Sale\OrderDiscountManager::formatArrayDescription($rule[
'RULE_DESCR']);
370 $ruleItem[
'DESCR_ID'] = $rule[
'RULE_DESCR_ID'];
373 switch ($rule[
'ENTITY_TYPE'])
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();
381 $orderDiscountItem[
'RESULT'][
'BASKET'][$index] = $ruleItem;
382 if ($ruleItem[
'ACTION_BLOCK_LIST'] ===
null)
383 $orderDiscountItem[
'ACTION_BLOCK_LIST'] =
false;
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']
393 case Internals\OrderRulesTable::ENTITY_TYPE_DELIVERY:
394 if (!isset($orderDiscountItem[
'RESULT'][
'DELIVERY']))
395 $orderDiscountItem[
'RESULT'][
'DELIVERY'] = array();
397 $ruleItem[
'DELIVERY_ID'] = ($rule[
'ENTITY_ID'] > 0 ? $rule[
'ENTITY_ID'] : (string)$rule[
'ENTITY_VALUE']);
398 $orderDiscountItem[
'RESULT'][
'DELIVERY'] = $ruleItem;
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']
408 foreach ($orderDiscountData as $data)
410 if ((
int)$data[
'DELIVERY_ID'] == (
int)$orderDiscountItem[
'RESULT'][
'DELIVERY'][
'DELIVERY_ID'])
411 $resultData[
'SHIPMENTS_ID'][] = (int)$data[
'SHIPMENT_ID'];
416 $orderDiscountLink[$orderDiscountId] = $orderDiscountItem;
417 unset($ruleItem, $orderDiscountId);
424 $index = $rule[
'ENTITY_ID'];
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'] :
''),
434 'APPLY' => $rule[
'APPLY']
436 'RULE_DESCR_ID' => $rule[
'RULE_DESCR_ID'],
437 'ACTION_BLOCK_LIST' => (isset($rule[
'ACTION_BLOCK_LIST']) ? $rule[
'ACTION_BLOCK_LIST'] :
null)
440 if (!empty($rule[
'RULE_DESCR']) && is_array($rule[
'RULE_DESCR']))
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'];
447 if (!isset($appliedBlocks[$blockCounter][
'BASKET'][$index]))
448 $appliedBlocks[$blockCounter][
'BASKET'][$index] = array();
449 $appliedBlocks[$blockCounter][
'BASKET'][$index][] = $ruleResult;
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']
462 $resultData[
'APPLY_BLOCKS'] = $appliedBlocks;
464 $resultData[
'COUPON_LIST'] = is_array($discountData[
'COUPON_LIST']) ? $discountData[
'COUPON_LIST'] : [];
465 foreach ($resultData[
'COUPON_LIST'] as &$coupon)
467 if (in_array((
int)$coupon[
'ID'], $couponAppliedList))
469 $coupon[
'APPLY'] =
'Y';
470 $coupon[
'JS_STATUS'] =
'APPLYED';
474 $resultData[
'PRICES'] = $this->prepareDiscountPrices();
475 $resultData[
'RESULT'] = $this->prepareDiscountResult($discountResultList);
477 if (!empty($discountList))
479 $resultData[
'DISCOUNT_LIST'] = $this->
prepareDiscountList($discountList, $resultData[
'RESULT']);
482 $this->order->setDiscountData($resultData);
491 protected function prepareDiscountPrices()
493 $resultData = array();
494 $basket = $this->order->getBasket();
495 $basketItems = $basket->getBasketItems();
498 foreach ($basketItems as $item)
500 $resultData[
'BASKET'][$item->getId()] = array(
501 'BASE_PRICE' => $item->getBasePrice(),
502 'PRICE' => $item->getPrice(),
503 'DISCOUNT' => $item->getDiscountPrice(),
507 $shipmentCollection = $this->order->getShipmentCollection();
510 foreach ($shipmentCollection as $shipment)
512 if ($shipment->isSystem())
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(),
532 protected function prepareDiscountResult($discountData)
534 $resultData = array();
535 $basket = $this->order->getBasket();
536 $basketItems = $basket->getBasketItems();
539 foreach ($basketItems as $item)
541 if (is_array($discountData[
'BASKET'][$item->getId()]))
543 $resultData[
'BASKET'][$item->getId()] = $discountData[
'BASKET'][$item->getId()];
546 $resultData[
'DELIVERY'] = $discountData[
'DELIVERY'];
560 $resultData = array();
562 foreach ($dataRow[
'ORDER_DATA'] as $data)
566 && $data[
'ENTITY_ID'] = $this->order->getId()
567 && isset($data[
'ENTITY_DATA'][
'DELIVERY'][
'SHIPMENT_ID'])
570 $resultData[
'ORDER'][$data[
'ENTITY_DATA'][
'DELIVERY'][
'SHIPMENT_ID']] = $data[
'ENTITY_DATA'][
'DELIVERY'];
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'];
597 foreach ($discounts as $discount)
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']))
605 foreach ($discount[
'ACTIONS_DESCR'][
'BASKET'] as &$description)
607 $description = Sale\OrderDiscountManager::formatDescription($description);
611 if ($discount[
'MODULE_ID'] ==
'sale')
613 $discount[
'EDIT_PAGE_URL'] = Sale\OrderDiscountManager::getEditUrl(array(
'ID' => $discount[
'DISCOUNT_ID']));
615 $discount[
'DISCOUNT_ID'] = $discount[
'ID'];
616 $resultData[$discount[
'ID']] = $discount;
630 if (is_array($discountResult[
'BASKET']))
632 foreach ($discountResult[
'BASKET'] as $discountList)
634 if (is_array($discountList))
636 foreach ($discountList as $discount)
638 if ($discount[
'APPLY'] ===
'Y')
639 $idList[] = $discount[
'DISCOUNT_ID'];
645 if (is_array($discountResult[
'DELIVERY']))
647 foreach ($discountResult[
'DELIVERY'] as $discount)
649 if ($discount[
'APPLY'] ===
'Y')
650 $idList[] = $discount[
'DISCOUNT_ID'];
654 return array_unique($idList);