26 protected function loadOrder($archivedOrder)
28 $this->order = Archive\Order::create($archivedOrder[
'ORDER'][
'LID'], $archivedOrder[
'ORDER'][
'USER_ID'], $archivedOrder[
'ORDER'][
'CURRENCY']);
29 $this->order->setPersonTypeId($archivedOrder[
'ORDER'][
'PERSON_TYPE_ID']);
34 $basketClassName = $registry->getBasketClassName();
36 $basket = $basketClassName::create($archivedOrder[
'ORDER'][
'LID']);
37 $this->order->setBasket($basket);
38 $basketItemsMap = $this->
riseBasket($archivedOrder);
39 $this->risePayment($archivedOrder);
40 $this->riseShipment($archivedOrder, $basketItemsMap);
55 $oldOrderFields = $archivedOrder[
'ORDER'];
56 $this->order->setFieldsNoDemand($oldOrderFields);
57 $propertyCollection = $this->order->getPropertyCollection();
58 $propertyCollectionArchived = $archivedOrder[
'PROPERTIES'];
59 if (is_array($propertyCollectionArchived))
61 foreach ($propertyCollectionArchived as $propertyArchived)
63 $property = $propertyCollection->getItemByOrderPropertyId($propertyArchived[
'ORDER_PROPS_ID']);
66 $property->setField(
'VALUE', $propertyArchived[
'VALUE']);
82 $basketItemsMap = array();
83 $basket = $this->order->getBasket();
84 foreach ($archivedOrder[
'BASKET_ITEMS'] as &$archivedItem)
86 if (empty($archivedItem[
'SET_PARENT_ID']))
88 $item = $basket->createItem($archivedItem[
'MODULE'], $archivedItem[
'PRODUCT_ID'], $archivedItem[
'ID']);
89 $this->riseBasketItem($item, $archivedItem);
90 $basketItemsMap[$archivedItem[
'ID']] = $item;
91 $type = $archivedItem[
'TYPE'];
96 $bundleCollection = $item->getBundleCollection();
97 foreach ($archivedOrder[
'BASKET_ITEMS'] as &$bundle)
99 if ($item->getId() !== (
int)$bundle[
'SET_PARENT_ID'])
102 $itemBundle = $bundleCollection->createItem($bundle[
'MODULE'], $bundle[
'PRODUCT_ID']);
103 $this->riseBasketItem($itemBundle, $bundle);
104 $basketItemsMap[$bundle[
'ID']] = $itemBundle;
111 return $basketItemsMap;
122 protected function riseBasketItem(&$item, $data = array())
124 $basketItemProperties = $data[
"PROPERTY_ITEMS"];
125 unset($data[
"PROPERTY_ITEMS"], $data[
"SHIPMENT_BARCODE_ITEMS"]);
126 $item->setFieldsNoDemand($data);
127 $newPropertyCollection = $item->getPropertyCollection();
128 if (is_array($basketItemProperties))
130 foreach ($basketItemProperties as $oldPropertyFields)
132 $propertyItem = $newPropertyCollection->createItem();
133 unset($oldPropertyFields[
'ID'], $oldPropertyFields[
'BASKET_ID']);
136 $propertyItem->setFieldsNoDemand($oldPropertyFields);
148 protected function risePayment($archivedOrder)
150 $paymentCollection = $this->order->getPaymentCollection();
151 $paymentCollectionArchived = $archivedOrder[
'PAYMENT'];
153 if (empty($paymentCollectionArchived))
156 foreach ($paymentCollectionArchived as $oldPayment)
159 $newPaymentItem = $paymentCollection->createItem();
160 $newPaymentItem->setFieldsNoDemand($oldPayment);
173 protected function riseShipment($archivedOrder, $basketItemsMap)
176 $shipmentCollection = $this->order->getShipmentCollection();
177 $shipmentCollectionArchived = $archivedOrder[
'SHIPMENT'];
179 if (empty($shipmentCollectionArchived))
182 foreach ($shipmentCollectionArchived as $oldShipment)
184 $oldShipmentCollections = $oldShipment[
'SHIPMENT_ITEM'];
185 unset($oldShipment[
'SHIPMENT_ITEM']);
187 $newShipmentItem = $shipmentCollection->createItem();
188 $newShipmentItemCollection = $newShipmentItem->getShipmentItemCollection();
189 if (is_array($oldShipmentCollections))
191 foreach ($oldShipmentCollections as $oldItemStore)
193 $basketItemId = $oldItemStore[
'BASKET_ID'];
195 if (empty($basketItemsMap[$basketItemId]))
199 $shipmentItem = $newShipmentItemCollection->createItem($basketItemsMap[$basketItemId]);
200 $shipmentItem->setFieldsNoDemand($oldItemStore);
201 $shipmentItemStoreCollection = $shipmentItem->getShipmentItemStoreCollection();
202 if ($shipmentItemStoreCollection)
205 $itemStore = $shipmentItemStoreCollection->createItem($basketItemsMap[$basketItemId]);
206 $oldBasketBarcodeData = $archivedOrder[
'BASKET_ITEMS'][$basketItemId][
'SHIPMENT_BARCODE_ITEMS'][$oldItemStore[
'ID']];
207 if (count($oldBasketBarcodeData))
209 $itemStore->setFieldsNoDemand($oldBasketBarcodeData);
215 $newShipmentItem->setFieldsNoDemand($oldShipment);
229 $discountDataRow = $archivedOrder[
'DISCOUNT'];
230 $discountResultList = array();
235 'APPLY_BLOCKS' => array(),
236 'DISCOUNT_LIST' => array(),
237 'DISCOUNT_MODULES' => array(),
238 'COUPON_LIST' => array(),
239 'SHIPMENTS_ID' => array(),
245 $orderDiscountData = $resultData[
'DATA'][
'ORDER'];
247 $orderDiscountIndex =
250 $couponAppliedList = [];
252 foreach ($discountDataRow[
'RULES_DATA'] as $rule)
254 $discountList[] = $rule[
"DISCOUNT_DATA"];
256 if ($rule[
'APPLY_BLOCK_COUNTER'] < 0)
259 if ($rule[
'APPLY'] ===
'Y' && (
int)$rule[
'COUPON_ID'] > 0)
261 $couponAppliedList[] = (int)$rule[
'COUPON_ID'];
264 $blockCounter = $rule[
'APPLY_BLOCK_COUNTER'];
266 if (!isset($orderDiscountIndex[$blockCounter]))
267 $orderDiscountIndex[$blockCounter] = 0;
269 if (!isset($appliedBlocks[$blockCounter]))
271 $appliedBlocks[$blockCounter] = array(
273 'BASKET_ROUND' => array(),
275 'ORDER_ROUND' => array()
279 if ($rule[
'MODULE_ID'] ==
'sale')
281 $orderDiscountId = (int)$rule[
'ORDER_DISCOUNT_ID'];
282 $orderDiscountItem = $orderDiscountLink[$orderDiscountId];
283 if (!isset($orderDiscountItem))
285 $appliedBlocks[$blockCounter][
'ORDER'][$orderDiscountIndex[$blockCounter]] = array(
286 'ORDER_ID' => $rule[
'ORDER_ID'],
287 'DISCOUNT_ID' => $rule[
'ORDER_DISCOUNT_ID'],
288 'ORDER_COUPON_ID' => $rule[
'ORDER_COUPON_ID'],
289 'COUPON_ID' => ($rule[
'COUPON_ID'] > 0 ? $rule[
'COUPON_ID'] :
''),
291 'ACTION_BLOCK_LIST' => true
293 $orderDiscountItem = &$appliedBlocks[$blockCounter][
'ORDER'][$orderDiscountIndex[$blockCounter]];
294 $orderDiscountIndex[$blockCounter]++;
298 'RULE_ID' => $rule[
'ID'],
299 'APPLY' => $rule[
'APPLY'],
300 'RULE_DESCR_ID' => $rule[
'RULE_DESCR_ID'],
301 'ACTION_BLOCK_LIST' => (!empty($rule[
'ACTION_BLOCK_LIST']) && is_array($rule[
'ACTION_BLOCK_LIST']) ? $rule[
'ACTION_BLOCK_LIST'] :
null)
304 if (!empty($rule[
'RULE_DESCR']) && is_array($rule[
'RULE_DESCR']))
306 $ruleItem[
'DESCR_DATA'] = $rule[
'RULE_DESCR'];
307 $ruleItem[
'DESCR'] = Sale\OrderDiscountManager::formatArrayDescription($rule[
'RULE_DESCR']);
308 $ruleItem[
'DESCR_ID'] = $rule[
'RULE_DESCR_ID'];
311 switch ($rule[
'ENTITY_TYPE'])
313 case Internals\OrderRulesTable::ENTITY_TYPE_BASKET_ITEM:
314 $ruleItem[
'BASKET_ID'] = $rule[
'ENTITY_ID'];
315 $index = $rule[
'ENTITY_ID'];
316 if (!isset($orderDiscountItem[
'RESULT'][
'BASKET']))
317 $orderDiscountItem[
'RESULT'][
'BASKET'] = array();
319 $orderDiscountItem[
'RESULT'][
'BASKET'][$index] = $ruleItem;
320 if ($ruleItem[
'ACTION_BLOCK_LIST'] ===
null)
321 $orderDiscountItem[
'ACTION_BLOCK_LIST'] =
false;
323 $discountResultList[
'BASKET'][$ruleItem[
'BASKET_ID']][] = array(
324 'DISCOUNT_ID' => $orderDiscountItem[
'DISCOUNT_ID'],
325 'COUPON_ID' => $orderDiscountItem[
'COUPON_ID'],
326 'APPLY' => $ruleItem[
'APPLY'],
327 'DESCR' => $ruleItem[
'DESCR']
331 case Internals\OrderRulesTable::ENTITY_TYPE_DELIVERY:
332 if (!isset($orderDiscountItem[
'RESULT'][
'DELIVERY']))
333 $orderDiscountItem[
'RESULT'][
'DELIVERY'] = array();
335 $ruleItem[
'DELIVERY_ID'] = ($rule[
'ENTITY_ID'] > 0 ? $rule[
'ENTITY_ID'] : (string)$rule[
'ENTITY_VALUE']);
336 $orderDiscountItem[
'RESULT'][
'DELIVERY'] = $ruleItem;
338 $discountResultList[
'DELIVERY'][] = array(
339 'DISCOUNT_ID' => $orderDiscountItem[
'DISCOUNT_ID'],
340 'COUPON_ID' => $orderDiscountItem[
'COUPON_ID'],
341 'APPLY' => $orderDiscountItem[
'RESULT'][
'DELIVERY'][
'APPLY'],
342 'DESCR' => $orderDiscountItem[
'RESULT'][
'DELIVERY'][
'DESCR'][0],
343 'DELIVERY_ID' => $orderDiscountItem[
'RESULT'][
'DELIVERY'][
'DELIVERY_ID']
346 foreach ($orderDiscountData as $data)
348 if ((
int)$data[
'DELIVERY_ID'] == (
int)$orderDiscountItem[
'RESULT'][
'DELIVERY'][
'DELIVERY_ID'])
349 $resultData[
'SHIPMENTS_ID'][] = (int)$data[
'SHIPMENT_ID'];
354 $orderDiscountLink[$orderDiscountId] = $orderDiscountItem;
355 unset($ruleItem, $orderDiscountId);
362 $index = $rule[
'ENTITY_ID'];
365 'BASKET_ID' => $rule[
'ENTITY_ID'],
366 'RULE_ID' => $rule[
'ID'],
367 'ORDER_ID' => $rule[
'ORDER_ID'],
368 'DISCOUNT_ID' => $rule[
'ORDER_DISCOUNT_ID'],
369 'ORDER_COUPON_ID' => $rule[
'ORDER_COUPON_ID'],
370 'COUPON_ID' => ($rule[
'ORDER_COUPON_ID'] > 0 ? $rule[
'COUPON_ID'] :
''),
372 'APPLY' => $rule[
'APPLY']
374 'RULE_DESCR_ID' => $rule[
'RULE_DESCR_ID'],
375 'ACTION_BLOCK_LIST' => (isset($rule[
'ACTION_BLOCK_LIST']) ? $rule[
'ACTION_BLOCK_LIST'] :
null)
378 if (!empty($rule[
'RULE_DESCR']) && is_array($rule[
'RULE_DESCR']))
380 $ruleResult[
'RESULT'][
'DESCR_DATA'] = $rule[
'RULE_DESCR'];
381 $ruleResult[
'RESULT'][
'DESCR'] = Sale\OrderDiscountManager::formatArrayDescription($rule[
'RULE_DESCR']);
382 $ruleResult[
'DESCR_ID'] = $rule[
'RULE_DESCR_ID'];
385 if (!isset($appliedBlocks[$blockCounter][
'BASKET'][$index]))
386 $appliedBlocks[$blockCounter][
'BASKET'][$index] = array();
387 $appliedBlocks[$blockCounter][
'BASKET'][$index][] = $ruleResult;
389 $discountResultList[
'BASKET'][$index][] = array(
390 'DISCOUNT_ID' => $ruleResult[
'DISCOUNT_ID'],
391 'COUPON_ID' => $ruleResult[
'COUPON_ID'],
392 'APPLY' => $ruleResult[
'RESULT'][
'APPLY'],
393 'DESCR' => $ruleResult[
'RESULT'][
'DESCR']
400 $resultData[
'APPLY_BLOCKS'] = $appliedBlocks;
402 $resultData[
'COUPON_LIST'] = is_array($discountDataRow[
'COUPON_LIST']) ? $discountDataRow[
'COUPON_LIST'] : [];
403 foreach ($resultData[
'COUPON_LIST'] as &$coupon)
405 if (in_array((
int)$coupon[
'ID'], $couponAppliedList))
407 $coupon[
'APPLY'] =
'Y';
408 $coupon[
'JS_STATUS'] =
'APPLYED';
412 $resultData[
'PRICES'] = $this->prepareDiscountPrices();
413 $resultData[
'RESULT'] = $this->prepareDiscountResult($discountResultList);
415 if (!empty($discountList))
417 $resultData[
'DISCOUNT_LIST'] = $this->
prepareDiscountList($discountList, $resultData[
'RESULT']);
420 $this->order->setDiscountData($resultData);
429 protected function prepareDiscountPrices()
431 $resultData = array();
432 $basket = $this->order->getBasket();
433 $basketItems = $basket->getBasketItems();
436 foreach ($basketItems as $item)
438 $resultData[
'BASKET'][$item->getId()] = array(
439 'BASE_PRICE' => $item->getBasePrice(),
440 'PRICE' => $item->getPrice(),
441 'DISCOUNT' => $item->getDiscountPrice(),
445 $shipmentCollection = $this->order->getShipmentCollection();
448 foreach ($shipmentCollection as $shipment)
450 if ($shipment->isSystem())
453 $resultData[
'DELIVERY'][$shipment->getId()] = array(
454 'BASE_PRICE' => $shipment->getField(
"BASE_PRICE_DELIVERY"),
455 'PRICE' => $shipment->getPrice(),
456 'DISCOUNT' => $shipment->getField(
"BASE_PRICE_DELIVERY") - $shipment->getPrice(),
470 protected function prepareDiscountResult($discountData)
472 $resultData = array();
473 $basket = $this->order->getBasket();
474 $basketItems = $basket->getBasketItems();
477 foreach ($basketItems as $item)
479 if (is_array($discountData[
'BASKET'][$item->getId()]))
481 $resultData[
'BASKET'][$item->getId()] = $discountData[
'BASKET'][$item->getId()];
484 $resultData[
'DELIVERY'] = $discountData[
'DELIVERY'];
498 $resultData = array();
500 foreach ($dataRow[
'ORDER_DATA'] as $data)
504 && $data[
'ENTITY_ID'] = $this->order->getId()
505 && isset($data[
'ENTITY_DATA'][
'DELIVERY'][
'SHIPMENT_ID'])
508 $resultData[
'ORDER'][$data[
'ENTITY_DATA'][
'DELIVERY'][
'SHIPMENT_ID']] = $data[
'ENTITY_DATA'][
'DELIVERY'];
513 if (!isset($resultData[
'DATA'][
'BASKET']))
514 $resultData[
'BASKET'] = array();
515 $data[
'ENTITY_ID'] = (int)$data[
'ENTITY_ID'];
516 $resultData[
'BASKET'][$data[
'ENTITY_ID']] = $data[
'ENTITY_DATA'];
535 foreach ($discounts as $discount)
537 $discount[
'ID'] = (int)$discount[
'ID'];
538 $discount[
'APPLY'] = in_array($discount[
'ID'], $appliedDiscountIds) ?
'Y' :
'N';
539 $discount[
'ORDER_DISCOUNT_ID'] = $discount[
'ID'];
540 $discount[
'SIMPLE_ACTION'] =
true;
541 if (is_array($discount[
'ACTIONS_DESCR'][
'BASKET']))
543 foreach ($discount[
'ACTIONS_DESCR'][
'BASKET'] as &$description)
545 $description = Sale\OrderDiscountManager::formatDescription($description);
549 if ($discount[
'MODULE_ID'] ==
'sale')
551 $discount[
'EDIT_PAGE_URL'] = Sale\OrderDiscountManager::getEditUrl(array(
'ID' => $discount[
'DISCOUNT_ID']));
553 $discount[
'DISCOUNT_ID'] = $discount[
'ID'];
554 $resultData[$discount[
'ID']] = $discount;
568 if (is_array($discountResult[
'BASKET']))
570 foreach ($discountResult[
'BASKET'] as $discountList)
572 if (is_array($discountList))
574 foreach ($discountList as $discount)
576 if ($discount[
'APPLY'] ===
'Y')
577 $idList[] = $discount[
'DISCOUNT_ID'];
583 if (is_array($discountResult[
'DELIVERY']))
585 foreach ($discountResult[
'DELIVERY'] as $discount)
587 if ($discount[
'APPLY'] ===
'Y')
588 $idList[] = $discount[
'DISCOUNT_ID'];
592 return array_unique($idList);