Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
version1.php
1<?php
3
8
17class Version1 extends Base
18{
26 protected function loadOrder($archivedOrder)
27 {
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']);
30
31 $registry = Sale\Registry::getInstance(Sale\Registry::REGISTRY_TYPE_ORDER);
32
34 $basketClassName = $registry->getBasketClassName();
35
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);
41 $this->riseOrder($archivedOrder);
42 $this->riseDiscount($archivedOrder);
43 return $this->order;
44 }
45
53 protected function riseOrder($archivedOrder)
54 {
55 $oldOrderFields = $archivedOrder['ORDER'];
56 $this->order->setFieldsNoDemand($oldOrderFields);
57 $propertyCollection = $this->order->getPropertyCollection();
58 $propertyCollectionArchived = $archivedOrder['PROPERTIES'];
59 if (is_array($propertyCollectionArchived))
60 {
61 foreach ($propertyCollectionArchived as $propertyArchived)
62 {
63 $property = $propertyCollection->getItemByOrderPropertyId($propertyArchived['ORDER_PROPS_ID']);
64 if ($property)
65 {
66 $property->setField('VALUE', $propertyArchived['VALUE']);
67 }
68 }
69 }
70 return;
71 }
72
80 protected function riseBasket($archivedOrder)
81 {
82 $basketItemsMap = array();
83 $basket = $this->order->getBasket();
84 foreach ($archivedOrder['BASKET_ITEMS'] as &$archivedItem)
85 {
86 if (empty($archivedItem['SET_PARENT_ID']))
87 {
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'];
92 unset($archivedItem);
93
94 if ($type == Sale\BasketItem::TYPE_SET)
95 {
96 $bundleCollection = $item->getBundleCollection();
97 foreach ($archivedOrder['BASKET_ITEMS'] as &$bundle)
98 {
99 if ($item->getId() !== (int)$bundle['SET_PARENT_ID'])
100 continue;
101
102 $itemBundle = $bundleCollection->createItem($bundle['MODULE'], $bundle['PRODUCT_ID']);
103 $this->riseBasketItem($itemBundle, $bundle);
104 $basketItemsMap[$bundle['ID']] = $itemBundle;
105 unset($bundle);
106 }
107 }
108 }
109 }
110
111 return $basketItemsMap;
112 }
113
122 protected function riseBasketItem(&$item, $data = array())
123 {
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))
129 {
130 foreach ($basketItemProperties as $oldPropertyFields)
131 {
132 $propertyItem = $newPropertyCollection->createItem();
133 unset($oldPropertyFields['ID'], $oldPropertyFields['BASKET_ID']);
134
136 $propertyItem->setFieldsNoDemand($oldPropertyFields);
137 }
138 }
139 }
140
148 protected function risePayment($archivedOrder)
149 {
150 $paymentCollection = $this->order->getPaymentCollection();
151 $paymentCollectionArchived = $archivedOrder['PAYMENT'];
152
153 if (empty($paymentCollectionArchived))
154 return;
155
156 foreach ($paymentCollectionArchived as $oldPayment)
157 {
159 $newPaymentItem = $paymentCollection->createItem();
160 $newPaymentItem->setFieldsNoDemand($oldPayment);
161 }
162 return;
163 }
164
173 protected function riseShipment($archivedOrder, $basketItemsMap)
174 {
176 $shipmentCollection = $this->order->getShipmentCollection();
177 $shipmentCollectionArchived = $archivedOrder['SHIPMENT'];
178
179 if (empty($shipmentCollectionArchived))
180 return;
181
182 foreach ($shipmentCollectionArchived as $oldShipment)
183 {
184 $oldShipmentCollections = $oldShipment['SHIPMENT_ITEM'];
185 unset($oldShipment['SHIPMENT_ITEM']);
187 $newShipmentItem = $shipmentCollection->createItem();
188 $newShipmentItemCollection = $newShipmentItem->getShipmentItemCollection();
189 if (is_array($oldShipmentCollections))
190 {
191 foreach ($oldShipmentCollections as $oldItemStore)
192 {
193 $basketItemId = $oldItemStore['BASKET_ID'];
194
195 if (empty($basketItemsMap[$basketItemId]))
196 continue;
197
199 $shipmentItem = $newShipmentItemCollection->createItem($basketItemsMap[$basketItemId]);
200 $shipmentItem->setFieldsNoDemand($oldItemStore);
201 $shipmentItemStoreCollection = $shipmentItem->getShipmentItemStoreCollection();
202 if ($shipmentItemStoreCollection)
203 {
205 $itemStore = $shipmentItemStoreCollection->createItem($basketItemsMap[$basketItemId]);
206 $oldBasketBarcodeData = $archivedOrder['BASKET_ITEMS'][$basketItemId]['SHIPMENT_BARCODE_ITEMS'][$oldItemStore['ID']];
207 if (count($oldBasketBarcodeData))
208 {
209 $itemStore->setFieldsNoDemand($oldBasketBarcodeData);
210 }
211 }
212 }
213 }
214
215 $newShipmentItem->setFieldsNoDemand($oldShipment);
216 }
217 }
218
227 protected function riseDiscount($archivedOrder)
228 {
229 $discountDataRow = $archivedOrder['DISCOUNT'];
230 $discountResultList = array();
231
232 $resultData = array(
233 'BASKET' => array(),
234 'ORDER' => array(),
235 'APPLY_BLOCKS' => array(),
236 'DISCOUNT_LIST' => array(),
237 'DISCOUNT_MODULES' => array(),
238 'COUPON_LIST' => array(),
239 'SHIPMENTS_ID' => array(),
240 'DATA' => array()
241 );
242
243 $resultData['DATA'] = $this->prepareDiscountOrderData($discountDataRow);
244
245 $orderDiscountData = $resultData['DATA']['ORDER'];
246
247 $orderDiscountIndex =
248 $appliedBlocks =
249 $orderDiscountLink =
250 $couponAppliedList = [];
251
252 foreach ($discountDataRow['RULES_DATA'] as $rule)
253 {
254 $discountList[] = $rule["DISCOUNT_DATA"];
255
256 if ($rule['APPLY_BLOCK_COUNTER'] < 0)
257 continue;
258
259 if ($rule['APPLY'] === 'Y' && (int)$rule['COUPON_ID'] > 0)
260 {
261 $couponAppliedList[] = (int)$rule['COUPON_ID'];
262 }
263
264 $blockCounter = $rule['APPLY_BLOCK_COUNTER'];
265
266 if (!isset($orderDiscountIndex[$blockCounter]))
267 $orderDiscountIndex[$blockCounter] = 0;
268
269 if (!isset($appliedBlocks[$blockCounter]))
270 {
271 $appliedBlocks[$blockCounter] = array(
272 'BASKET' => array(),
273 'BASKET_ROUND' => array(),
274 'ORDER' => array(),
275 'ORDER_ROUND' => array()
276 );
277 }
278
279 if ($rule['MODULE_ID'] == 'sale')
280 {
281 $orderDiscountId = (int)$rule['ORDER_DISCOUNT_ID'];
282 $orderDiscountItem = $orderDiscountLink[$orderDiscountId];
283 if (!isset($orderDiscountItem))
284 {
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'] : ''),
290 'RESULT' => array(),
291 'ACTION_BLOCK_LIST' => true
292 );
293 $orderDiscountItem = &$appliedBlocks[$blockCounter]['ORDER'][$orderDiscountIndex[$blockCounter]];
294 $orderDiscountIndex[$blockCounter]++;
295 }
296
297 $ruleItem = array(
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)
302 );
303
304 if (!empty($rule['RULE_DESCR']) && is_array($rule['RULE_DESCR']))
305 {
306 $ruleItem['DESCR_DATA'] = $rule['RULE_DESCR'];
307 $ruleItem['DESCR'] = Sale\OrderDiscountManager::formatArrayDescription($rule['RULE_DESCR']);
308 $ruleItem['DESCR_ID'] = $rule['RULE_DESCR_ID'];
309 }
310
311 switch ($rule['ENTITY_TYPE'])
312 {
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();
318
319 $orderDiscountItem['RESULT']['BASKET'][$index] = $ruleItem;
320 if ($ruleItem['ACTION_BLOCK_LIST'] === null)
321 $orderDiscountItem['ACTION_BLOCK_LIST'] = false;
322
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']
328 );
329 break;
330
331 case Internals\OrderRulesTable::ENTITY_TYPE_DELIVERY:
332 if (!isset($orderDiscountItem['RESULT']['DELIVERY']))
333 $orderDiscountItem['RESULT']['DELIVERY'] = array();
334
335 $ruleItem['DELIVERY_ID'] = ($rule['ENTITY_ID'] > 0 ? $rule['ENTITY_ID'] : (string)$rule['ENTITY_VALUE']);
336 $orderDiscountItem['RESULT']['DELIVERY'] = $ruleItem;
337
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']
344 );
345
346 foreach ($orderDiscountData as $data)
347 {
348 if ((int)$data['DELIVERY_ID'] == (int)$orderDiscountItem['RESULT']['DELIVERY']['DELIVERY_ID'])
349 $resultData['SHIPMENTS_ID'][] = (int)$data['SHIPMENT_ID'];
350 }
351 break;
352 }
353
354 $orderDiscountLink[$orderDiscountId] = $orderDiscountItem;
355 unset($ruleItem, $orderDiscountId);
356 }
357 else
358 {
359 if ($rule['ENTITY_ID'] <= 0 || $rule['ENTITY_TYPE'] != Internals\OrderRulesTable::ENTITY_TYPE_BASKET_ITEM)
360 continue;
361
362 $index = $rule['ENTITY_ID'];
363
364 $ruleResult = array(
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'] : ''),
371 'RESULT' => array(
372 'APPLY' => $rule['APPLY']
373 ),
374 'RULE_DESCR_ID' => $rule['RULE_DESCR_ID'],
375 'ACTION_BLOCK_LIST' => (isset($rule['ACTION_BLOCK_LIST']) ? $rule['ACTION_BLOCK_LIST'] : null)
376 );
377
378 if (!empty($rule['RULE_DESCR']) && is_array($rule['RULE_DESCR']))
379 {
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'];
383 }
384
385 if (!isset($appliedBlocks[$blockCounter]['BASKET'][$index]))
386 $appliedBlocks[$blockCounter]['BASKET'][$index] = array();
387 $appliedBlocks[$blockCounter]['BASKET'][$index][] = $ruleResult;
388
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']
394 );
395
396 unset($ruleResult);
397 }
398 }
399
400 $resultData['APPLY_BLOCKS'] = $appliedBlocks;
401
402 $resultData['COUPON_LIST'] = is_array($discountDataRow['COUPON_LIST']) ? $discountDataRow['COUPON_LIST'] : [];
403 foreach ($resultData['COUPON_LIST'] as &$coupon)
404 {
405 if (in_array((int)$coupon['ID'], $couponAppliedList))
406 {
407 $coupon['APPLY'] = 'Y';
408 $coupon['JS_STATUS'] = 'APPLYED';
409 }
410 }
411
412 $resultData['PRICES'] = $this->prepareDiscountPrices();
413 $resultData['RESULT'] = $this->prepareDiscountResult($discountResultList);
414
415 if (!empty($discountList))
416 {
417 $resultData['DISCOUNT_LIST'] = $this->prepareDiscountList($discountList, $resultData['RESULT']);
418 }
419
420 $this->order->setDiscountData($resultData);
421 return;
422 }
423
429 protected function prepareDiscountPrices()
430 {
431 $resultData = array();
432 $basket = $this->order->getBasket();
433 $basketItems = $basket->getBasketItems();
434
436 foreach ($basketItems as $item)
437 {
438 $resultData['BASKET'][$item->getId()] = array(
439 'BASE_PRICE' => $item->getBasePrice(),
440 'PRICE' => $item->getPrice(),
441 'DISCOUNT' => $item->getDiscountPrice(),
442 );
443 }
444
445 $shipmentCollection = $this->order->getShipmentCollection();
446
448 foreach ($shipmentCollection as $shipment)
449 {
450 if ($shipment->isSystem())
451 continue;
452
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(),
457 );
458 }
459
460 return $resultData;
461 }
462
470 protected function prepareDiscountResult($discountData)
471 {
472 $resultData = array();
473 $basket = $this->order->getBasket();
474 $basketItems = $basket->getBasketItems();
475
477 foreach ($basketItems as $item)
478 {
479 if (is_array($discountData['BASKET'][$item->getId()]))
480 {
481 $resultData['BASKET'][$item->getId()] = $discountData['BASKET'][$item->getId()];
482 }
483 }
484 $resultData['DELIVERY'] = $discountData['DELIVERY'];
485
486 return $resultData;
487 }
488
496 protected function prepareDiscountOrderData($dataRow)
497 {
498 $resultData = array();
499
500 foreach ($dataRow['ORDER_DATA'] as $data)
501 {
502 if (
503 $data['ENTITY_TYPE'] = Internals\OrderDiscountDataTable::ENTITY_TYPE_ORDER
504 && $data['ENTITY_ID'] = $this->order->getId()
505 && isset($data['ENTITY_DATA']['DELIVERY']['SHIPMENT_ID'])
506 )
507 {
508 $resultData['ORDER'][$data['ENTITY_DATA']['DELIVERY']['SHIPMENT_ID']] = $data['ENTITY_DATA']['DELIVERY'];
509 }
510
511 if ($data['ENTITY_TYPE'] == Internals\OrderDiscountDataTable::ENTITY_TYPE_BASKET_ITEM)
512 {
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'];
517 }
518 }
519 return $resultData;
520 }
521
530 protected function prepareDiscountList(array $discounts, array $discountResult)
531 {
532 $resultData = [];
533 $appliedDiscountIds = $this->getAppliedDiscountIds($discountResult);
534
535 foreach ($discounts as $discount)
536 {
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']))
542 {
543 foreach ($discount['ACTIONS_DESCR']['BASKET'] as &$description)
544 {
545 $description = Sale\OrderDiscountManager::formatDescription($description);
546 }
547 }
548
549 if ($discount['MODULE_ID'] == 'sale')
550 {
551 $discount['EDIT_PAGE_URL'] = Sale\OrderDiscountManager::getEditUrl(array('ID' => $discount['DISCOUNT_ID']));
552 }
553 $discount['DISCOUNT_ID'] = $discount['ID'];
554 $resultData[$discount['ID']] = $discount;
555 }
556
557 return $resultData;
558 }
559
565 protected function getAppliedDiscountIds(array $discountResult)
566 {
567 $idList = [];
568 if (is_array($discountResult['BASKET']))
569 {
570 foreach ($discountResult['BASKET'] as $discountList)
571 {
572 if (is_array($discountList))
573 {
574 foreach ($discountList as $discount)
575 {
576 if ($discount['APPLY'] === 'Y')
577 $idList[] = $discount['DISCOUNT_ID'];
578 }
579 }
580 }
581 }
582
583 if (is_array($discountResult['DELIVERY']))
584 {
585 foreach ($discountResult['DELIVERY'] as $discount)
586 {
587 if ($discount['APPLY'] === 'Y')
588 $idList[] = $discount['DISCOUNT_ID'];
589 }
590 }
591
592 return array_unique($idList);
593 }
594}
getAppliedDiscountIds(array $discountResult)
Definition version1.php:565
prepareDiscountList(array $discounts, array $discountResult)
Definition version1.php:530