Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
basketcompatibility.php
1<?php
2
3
5
10
11Main\Localization\Loc::loadMessages(__FILE__);
12
14{
15
16 private static $proxyBasket = array();
17
19 protected $orderCompatibility = null;
20
24 protected static function getRegistryType()
25 {
26 return Sale\Registry::REGISTRY_TYPE_ORDER;
27 }
28
34 protected static function getEntity()
35 {
36 return BasketTable::getEntity();
37 }
38
42 protected static function getOrderCompatibilityClassName()
43 {
44 return OrderCompatibility::class;
45 }
46
50 protected function __construct(array $fields = array())
51 {
53 $this->query = new OrderQuery(static::getEntity());
54 $this->fields = new Sale\Internals\Fields($fields);
55 }
56
62 public static function create(OrderCompatibility $orderCompatibility)
63 {
65 $basketCompatibility = new static();
66
67 $basketCompatibility->orderCompatibility = $orderCompatibility;
68
69 return $basketCompatibility;
70 }
71
81 public function fillBasket(Sale\Basket $basket, array $requestFields)
82 {
84
86 $order = $orderCompatibility->getOrder();
87
88 $result = new Sale\Result();
89
90 if (empty($requestFields['BASKET_ITEMS']))
91 return $result;
92
93 $isStartField = $order->isStartField();
94
95 $basketCodeList = array();
96
97 $r = $this->parseBasketItems($basket, $requestFields['BASKET_ITEMS']);
98 if (!$r->isSuccess())
99 {
100 $result->addErrors($r->getErrors());
101 return $result;
102 }
103
104 $resultData = $r->getData();
105
106 if (isset($resultData['BASKET']))
107 {
108 $basket = $resultData['BASKET'];
109 }
110
111 if (isset($resultData['BASKET_CODE_LIST']))
112 {
113 $basketCodeList = $resultData['BASKET_CODE_LIST'];
114 }
115
116 if (isset($resultData['BASKET_CHANGED']) && $resultData['BASKET_CHANGED'] === true)
117 {
118 $order->refreshVat();
119 }
120
121 if (!empty($basketCodeList) && is_array($basketCodeList))
122 {
123 foreach ($basketCodeList as $index => $basketCode)
124 {
125 DiscountCompatibility::setBasketCode($index, $basketCode);
126 }
127 }
128
129 if ($isStartField)
130 {
132 $r = $order->doFinalAction(true);
133 if (!$r->isSuccess())
134 {
135 $result->addErrors($r->getErrors());
136 }
137 else
138 {
139 if (($data = $r->getData())
140 && !empty($data) && is_array($data))
141 {
142 $result->setData($result->getData() + $data);
143 }
144 }
145 }
146
147 return $result;
148 }
149
160 protected function parseBasketItems(Sale\Basket $basket, array $requestBasketItems)
161 {
162 $result = new Sale\Result();
163
164 $basketCodeList = array();
165 $basketItemList = array();
166 $basketParentList = array();
167 $basketChildList = array();
168
169 $basketChanged = false;
170
171 $registry = Sale\Registry::getInstance(static::getRegistryType());
172 $basketItemEntity = $registry->getBasketItemClassName();
173
175 foreach ($requestBasketItems as $basketIndex => $basketItemData)
176 {
177 if (isset($basketItemData['SET_PARENT_ID']) && strval($basketItemData['SET_PARENT_ID']) != '')
178 {
179 $parentId = intval($basketItemData['SET_PARENT_ID']);
180 if ($basketItemData['TYPE'] != $basketItemEntity::TYPE_SET && !array_key_exists($parentId, $basketParentList))
181 {
182 $basketChildList[intval($basketItemData['SET_PARENT_ID'])] = $basketItemData['SET_PARENT_ID'];
183 }
184 }
185 }
186
188
190 $order = $orderCompatibility->getOrder();
191 $basketItemsIndexList = array();
192
193 foreach ($basket as $basketItem)
194 {
195 $basketItemsIndexList[$basketItem->getId()] = true;
196 }
197
198 $sort = 100;
199 foreach ($requestBasketItems as $basketIndex => $basketItemData)
200 {
201 if (!isset($basketItemData['BASE_PRICE']) && isset($basketItemData['PRICE']))
202 {
203 $basketItemData['BASE_PRICE'] = $basketItemData['PRICE'];
204 }
205
206 $basketItem = null;
207 if (isset($basketItemData['ID']) && intval($basketItemData['ID']) > 0)
208 {
210 if ($basketItem = $basket->getItemById($basketItemData['ID']))
211 {
212 if (isset($basketItemsIndexList[$basketItem->getId()]))
213 unset($basketItemsIndexList[$basketItem->getId()]);
214 }
215 }
216
217
218 if (!$basketItem)
219 {
221 $basketItem = $basketItemEntity::create($basket, $basketItemData['MODULE'], $basketItemData['PRODUCT_ID']);
222 $basketChanged = true;
223 }
224
225 $itemDuplicate = (isset($basketItemData['DUPLICATE']) && $basketItemData['DUPLICATE'] == "Y");
226
227 $basketItemData['SORT'] = $sort;
228 $sort += 100;
229
230 $basketFields = static::clearFields($basketItemData);
231
232 if ($order->getId() > 0)
233 {
235 if ($shipmentCollection = $order->getShipmentCollection())
236 {
237 if (count($shipmentCollection) == 2
238 && (isset($basketItemData['QUANTITY']) && floatval($basketItemData['QUANTITY']) <= $basketItem->getQuantity()))
239 {
240
242 foreach ($shipmentCollection as $shipment)
243 {
244 if ($shipment->isSystem())
245 continue;
246
247
248 $basketQuantity = $shipment->getBasketItemQuantity($basketItem);
249 if ($basketQuantity <= floatval($basketItemData['QUANTITY']))
250 continue;
251
252
254 if ($shipmentItemCollection = $shipment->getShipmentItemCollection())
255 {
257 if (!$shipmentItem = $shipmentItemCollection->getItemByBasketCode($basketItem->getBasketCode()))
258 continue;
259
260 $shipmentItem->setQuantity(floatval($basketItemData['QUANTITY']));
261 }
262 }
263 }
264 }
265 }
266
267
268 $isBasketItemCanBeAdded = true;
269 if ($publicMode)
270 {
271 if (\CSaleBasketHelper::isSetParent($basketItemData))
272 {
273 $parentId = (int)$basketItemData['ID'];
274 $parentCode = $basketItemData['ID'];
275 }
276 else
277 {
278 $parentId = (int)$basketItemData['SET_PARENT_ID'];
279 $parentCode = $basketItemData['SET_PARENT_ID'];
280 }
281 }
282 else
283 {
284 $parentId = (int)$basketItemData['SET_PARENT_ID'];
285 $parentCode = $basketItemData['SET_PARENT_ID'];
286 }
287
288 if ($parentId > 0)
289 {
290 if ($basketItem->isBundleParent())
291 {
292 $basketParentList[$parentCode] = $basketItem->getBasketCode();
293 }
294 else
295 {
296 $isBasketItemCanBeAdded = false;
297 $basketItemList[$parentCode][$basketIndex] = $basketItem;
298 }
299 }
300
301 if ($isBasketItemCanBeAdded)
302 {
303 $propList = array();
305 if ($propertyCollection = $basketItem->getPropertyCollection())
306 {
307 $propList = $propertyCollection->getPropertyValues();
308 }
309
311 $foundedBasketItem = null;
312
313 if ($basketItem->getId() > 0 && ($foundedBasketItem = $basket->getItemById($basketItem->getId())))
314 {
315 $basketCodeList[($publicMode ? $foundedBasketItem->getId() : $basketIndex)] = $foundedBasketItem->getBasketCode();
316 }
317 else
318 {
319 if (!$itemDuplicate && ($foundedBasketItem = $basket->getExistsItem($basketItem->getField('MODULE'), $basketItem->getProductId(), $propList)))
320 {
321 $basketCodeList[($publicMode ? $foundedBasketItem->getId() : $basketIndex)] = $foundedBasketItem->getBasketCode();
322 }
323 }
324
325 if ($foundedBasketItem === null)
326 {
327 $basket->addItem($basketItem);
328 $basketCodeList[($publicMode ? $basketItem->getId() : $basketIndex)] = $basketItem->getBasketCode();
329
330 $basketChanged = true;
331 }
332 }
333
334
336 $r = $basketItem->setFields($basketFields);
337 if ($r->isSuccess())
338 {
339 if (array_key_exists('PROPS', $basketItemData) && is_array($basketItemData))
340 {
342 if ($propertyCollection = $basketItem->getPropertyCollection())
343 {
344 $propertyCollection->setProperty($basketItemData['PROPS']);
345 }
346 }
347 }
348 else
349 {
350 $result->addErrors($r->getErrors());
351 }
352
353 }
354
355 if (!empty($basketChildList))
356 {
357 foreach($basketItemList as $parentBasketCode => $childBasketItemList)
358 {
359 $parentCode = null;
360 if (!empty($basketParentList[$parentBasketCode]))
361 $parentCode = $basketParentList[$parentBasketCode];
362
363 if (strval($parentCode) == '')
364 continue;
365
367 if (!$parentBasketItem = $basket->getItemByBasketCode($parentCode))
368 {
369 throw new Main\ObjectNotFoundException('Entity parent "BasketItem" not found');
370 }
371
372 if (!empty($childBasketItemList) && is_array($childBasketItemList))
373 {
375 foreach ($childBasketItemList as $indexChildBasketItem => $childBasketItem)
376 {
377 $basketCodeIndex = ($publicMode ? $childBasketItem->getId() : $indexChildBasketItem);
378 $childBasketCode = $childBasketItem->getBasketCode();
379
380 $propList = array();
382 if ($propertyCollection = $childBasketItem->getPropertyCollection())
383 {
384 $propList = $propertyCollection->getPropertyValues();
385 }
386
387 $bundleCollection = $parentBasketItem->getBundleCollection();
389 if ($foundedBasketItem = $bundleCollection->getExistsItem($childBasketItem->getField('MODULE'), $childBasketItem->getProductId(), $propList))
390 {
391 $childBasketCode = $foundedBasketItem->getBasketCode();
392 unset($childBasketItemList[$indexChildBasketItem]);
393 $basketCodeIndex = ($publicMode ? $foundedBasketItem->getId() : $indexChildBasketItem);
394 }
395
396 if (strval($childBasketCode) != '')
397 $basketCodeList[$basketCodeIndex] = $childBasketCode;
398 }
399
400 if (!empty($childBasketItemList))
401 {
402 $this->setChildBundleCollection($basket, $childBasketItemList, $parentBasketItem);
403 }
404 }
405 }
406 }
407
408 if (!empty($basketItemsIndexList) && is_array($basketItemsIndexList))
409 {
410 foreach ($basketItemsIndexList as $basketIndexId => $basketIndexValue)
411 {
412 if ($foundedBasketItem = $basket->getItemById($basketIndexId))
413 {
414 $foundedBasketItem->delete();
415 $basketChanged = true;
416 }
417 }
418 }
419
420 $result->setData(array(
421 'BASKET' => $basket,
422 'BASKET_CODE_LIST' => $basketCodeList,
423 'BASKET_CHANGED' => $basketChanged,
424 ));
425
426 return $result;
427 }
428
436 private function setChildBundleCollection(Sale\Basket $basket, array $basketItemList, Sale\BasketItem $externalParentBasketItem = null)
437 {
438 $order = null;
439
440 $isExternalBasketParent = false;
441
442 if ($externalParentBasketItem !== null)
443 {
444 if (!$externalParentBasketItem->isBundleParent())
445 {
446 throw new Main\ObjectException('basketItem not parent');
447 }
448 $isExternalBasketParent = true;
449 }
450
452 foreach ($basketItemList as $item)
453 {
454 if ($item->isBundleChild() || (!$item->isBundleParent() && $isExternalBasketParent && $externalParentBasketItem !== null))
455 {
457 $parentBasketItem = $item->getParentBasketItem();
458
459 if (!$parentBasketItem && $externalParentBasketItem !== null)
460 {
461 $parentBasketItem = $externalParentBasketItem;
462 }
463
464 if ($parentBasketItem)
465 {
467 $bundleCollection = $parentBasketItem->createBundleCollection();
468
469 if ($basket->getItemByBasketCode($parentBasketItem->getBasketCode()))
470 {
471 $bundleCollection->addItem($item);
472 }
473 else
474 {
475 $basket->addItem($item);
476 }
477
478 if ($order === null)
479 {
481 if (!$basket = $parentBasketItem->getCollection())
482 {
483 throw new Main\ObjectNotFoundException('Entity "Basket" not found');
484 }
485
487 $order = $basket->getOrder();
488 }
489
490 if ($bundleCollection->getOrder() === null && $order instanceof Sale\OrderBase)
491 $bundleCollection->setOrder($order);
492 }
493 }
494 }
495 }
496
507 public static function add(array $fields)
508 {
509 $result = new Sale\Result();
510
511 $order = null;
512 $basket = null;
513 $item = null;
514
515 $registry = Sale\Registry::getInstance(static::getRegistryType());
516
517 if (!array_key_exists('FUSER_ID', $fields) || intval($fields['FUSER_ID']) <= 0)
518 {
519 $fields['FUSER_ID'] = Sale\Fuser::getId(false);
520 }
521
522 if (!empty($fields['ORDER_ID']) && intval($fields['ORDER_ID']) > 0)
523 {
525 $orderClassName = $registry->getOrderClassName();
526 $order = $orderClassName::load(intval($fields['ORDER_ID']));
527
528 if ($order)
529 {
531 $basket = $order->getBasket();
532 }
533 }
534
535 if (!$basket)
536 {
538 $basketClassName = $registry->getBasketClassName();
539 $basket = $basketClassName::loadItemsForFUser($fields["FUSER_ID"], $fields['LID']);
540 }
541
542
544 if ($item = $basket->getExistsItem($fields["MODULE"], $fields["PRODUCT_ID"], ((!empty($fields["PROPS"]) && is_array($fields["PROPS"]))? $fields["PROPS"] : array())))
545 {
546 $item->setField('QUANTITY', $item->getQuantity() + $fields['QUANTITY']);
547
548 unset($fields['QUANTITY']);
549
551 $r = $item->setFields(static::clearFields($fields));
552 if (!$r->isSuccess())
553 {
554 $result->addErrors($r->getErrors());
555 return $result;
556 }
557 else
558 {
559 if (!$order)
560 $basket->refreshData(array('PRICE', 'COUPONS', 'QUANTITY'), $item);
561 }
562 }
563
564 if ($item === null)
565 {
567 $item = $basket->createItem($fields["MODULE"], $fields["PRODUCT_ID"]);
568
569 if (isset($fields["PROPS"]) && is_array($fields["PROPS"]))
570 {
572 $property = $item->getPropertyCollection();
573 $property->setProperty($fields["PROPS"]);
574 }
575
577 $r = $item->setFields(static::clearFields($fields));
578 if (!$r->isSuccess())
579 {
580 $result->addErrors($r->getErrors());
581 return $result;
582 }
583
584 if ($order)
585 {
586 $shipmentCollection = $order->getShipmentCollection();
587 $systemShipment = $shipmentCollection->getSystemShipment();
588 $systemShipmentItemCollection = $systemShipment->getShipmentItemCollection();
589 $systemShipmentItemCollection->resetCollection($basket);
590
591 if ($systemShipment->getDeliveryId() > 0)
592 {
594 $orderCompatibilityClassName = static::getOrderCompatibilityClassName();
596 $shipment = $orderCompatibilityClassName::getShipmentByDeliveryId($shipmentCollection, $systemShipment->getDeliveryId());
597
598 if (!$shipment)
599 {
600 if ($service = Sale\Delivery\Services\Manager::getObjectById($systemShipment->getDeliveryId()))
601 {
603 $shipment = $shipmentCollection->createItem($service);
604 }
605 }
606
607
608 if ($shipment)
609 {
610 $shipmentItemCollection = $shipment->getShipmentItemCollection();
611
613 $shipmentItem = $shipmentItemCollection->createItem($item);
614 if ($shipmentItem)
615 $shipmentItem->setQuantity($item->getQuantity());
616 }
617 }
618
620 $r = $order->refreshData(array('PRICE', 'PRICE_DELIVERY'));
621 if (!$r->isSuccess())
622 {
623 $result->addErrors($r->getErrors());
624 return $result;
625 }
626
627 }
628 }
629
630 if ($order !== null)
631 {
633 $r = $order->save();
634 if (!$r->isSuccess())
635 {
636 $result->addErrors($r->getErrors());
637 }
638 }
639 else
640 {
641 $isNew = false;
642 if ($item->getId() == 0)
643 {
644 $isNew = true;
645 }
646
647 $r = $basket->save();
648 if (!$r->isSuccess())
649 {
650 $result->addErrors($r->getErrors());
651 }
652 }
653
654 if ($r->isSuccess())
655 {
659 {
660 $id = $item->getId();
662 DiscountCompatibility::setBasketCode($id, $item->getBasketCode());
663 }
664
665 $result->setId($item->getId());
666 $result->addData(array(
667 'QUANTITY' => $item->getQuantity()
668 ));
669 }
670
671
672 return $result;
673 }
674
686 public static function update($id, array $fields)
687 {
688 $result = new Sale\Result();
689
690 $item = null;
691 $basket = null;
693 $order = null;
694 $orderId = null;
695
696 $registry = Sale\Registry::getInstance(static::getRegistryType());
697
699 $itemResult = static::loadEntityFromBasket($id);
700 if ($itemResult->isSuccess())
701 {
702 $itemResultData = $itemResult->getData();
703 if (isset($itemResultData['BASKET_ITEM']))
704 {
706 $item = $itemResultData['BASKET_ITEM'];
707 $basket = $item->getBasket();
708 }
709
710 if (isset($itemResultData['ORDER']))
711 {
712 $order = $itemResultData['ORDER'];
713 }
714 }
715
716 if (!$item)
717 {
718 $result->addError(new Sale\ResultError(Main\Localization\Loc::getMessage('SALE_BASKET_COMPATIBLE_BASKET_ITEM_NOT_FOUND'), 'BASKET_ITEM_NOT_FOUND'));
719 return $result;
720
721 }
722
723 if ($order !== null &&
724 $fields['ORDER_ID'] > 0 &&
725 (int)$fields['ORDER_ID'] !== (int)$order->getId()
726 )
727 {
728 $result->addError(
729 new Sale\ResultError(
730 Main\Localization\Loc::getMessage('SALE_BASKET_COMPATIBLE_BASKET_ITEM_ERROR_BIND_TO_ORDER'),
731 'BASKET_ITEM_NOT_FOUND'
732 )
733 );
734
735 return $result;
736 }
737
738 if (isset($fields["PROPS"]) && is_array($fields["PROPS"]))
739 {
741 $property = $item->getPropertyCollection();
742 $property->clearCollection();
743 $property->setProperty($fields["PROPS"]);
744 }
745
746 if ($order !== null && isset($fields['PRICE']))
747 {
748 if ($fields['PRICE'] != $item->getPrice())
749 $fields['CUSTOM_PRICE'] = 'Y';
750 }
751
752 if (!empty($fields['FUSER_ID']))
753 {
754 $item->setFieldNoDemand('FUSER_ID', intval($fields['FUSER_ID']));
755 unset($fields['FUSER_ID']);
756 }
757
758 $r = $item->setFields(static::clearFields($fields));
759 if (!$r->isSuccess())
760 {
761 $result->addErrors($r->getErrors());
762 }
763
765 {
769 {
771 DiscountCompatibility::setBasketCode($id, $item->getBasketCode());
772 }
773 }
774
775
776 if ($order === null && !empty($fields['ORDER_ID']) && intval($fields['ORDER_ID']) > 0)
777 {
778 $orderId = intval($fields['ORDER_ID']);
779
780 $orderClassName = $registry->getOrderClassName();
781 if ($order = $orderClassName::load($orderId))
782 {
784 if ($basket = $order->getBasket())
785 {
786 $basket->addItem($item);
787
789 if (!$shipmentCollection = $order->getShipmentCollection())
790 {
791 throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found');
792 }
793
795 $orderCompatibilityClassName = static::getOrderCompatibilityClassName();
796 $orderCompatibilityClassName::createShipmentFromShipmentSystem($shipmentCollection);
797
799 $r = static::syncShipmentCollectionAndBasket($shipmentCollection, $basket);
800 if (!$r->isSuccess())
801 {
802 $result->addErrors($r->getErrors());
803 return $result;
804 }
805
806 $r = $order->refreshData(array("PRICE_DELIVERY"));
807 if (!$r->isSuccess())
808 {
809 $result->addErrors($r->getErrors());
810 return $result;
811 }
812
813 $r = $order->doFinalAction(true);
814 if (!$r->isSuccess())
815 {
816 $result->addErrors($r->getErrors());
817 return $result;
818 }
819 }
820
821 }
822 }
823
824 if ($order !== null)
825 {
827 if ($shipmentCollection = $order->getShipmentCollection())
828 {
829 if (count($shipmentCollection) == 2 && $shipmentCollection->isExistsSystemShipment())
830 {
832 foreach ($shipmentCollection as $shipment)
833 {
834 if ($shipment->isSystem() || $shipment->isShipped())
835 continue;
836
838 $systemShipment = $shipmentCollection->getSystemShipment();
839
841 $systemShipmentItemCollection = $systemShipment->getShipmentItemCollection();
842
844 if (!$systemShipmentItem = $systemShipmentItemCollection->getItemByBasketCode($item->getBasketCode()))
845 continue;
846
848 if (!$shipmentItemCollection = $shipment->getShipmentItemCollection())
849 continue;
850
851
853 if (!$shipmentItem = $shipmentItemCollection->getItemByBasketCode($item->getBasketCode()))
854 continue;
855
856
857 if ($systemShipmentItem->getQuantity() > 0)
858 {
859 $r = $shipmentItem->setQuantity(($shipmentItem->getQuantity() + $systemShipmentItem->getQuantity()));
860 if (!$r->isSuccess())
861 {
862 $result->addErrors($r->getErrors());
863 }
864 }
865 }
866 }
867 }
868
869 if (!$result->isSuccess())
870 {
871 return $result;
872 }
873
874 $r = $order->save();
875 }
876 else
877 {
878 if (!$result->isSuccess())
879 {
880 return $result;
881 }
882
883 $r = $basket->save();
884 }
885
886 if (!$r->isSuccess())
887 {
888 $result->addErrors($r->getErrors());
889 }
890
891 return $result;
892 }
893
901 public static function delete($id)
902 {
903 $result = new Sale\Result();
904
905 $item = null;
906 $basket = null;
907 $order = null;
908
909 $registry = Sale\Registry::getInstance(static::getRegistryType());
911 $basketClassName = $registry->getBasketClassName();
912 $res = $basketClassName::getList(
913 array(
914 'filter' => array(
915 'ID' => $id
916 ),
917 'select' => array(
918 'ID', 'ORDER_ID', 'SET_PARENT_ID', 'TYPE', 'FUSER_ID', 'LID'
919 ),
920 ));
921 if (!$itemDat = $res->fetch())
922 {
923 $result->addError(new Sale\ResultError(Main\Localization\Loc::getMessage('SALE_BASKET_COMPATIBLE_BASKET_ITEM_NOT_FOUND'), 'BASKET_ITEM_NOT_FOUND'));
924 return $result;
925 }
926
927 if (intval($itemDat['ORDER_ID']) > 0)
928 {
930 $orderClassName = $registry->getOrderClassName();
932 if ($order = $orderClassName::load(intval($itemDat['ORDER_ID'])))
933 {
934 if ($basket = $order->getBasket())
935 {
937 $item = $basket->getItemById($id);
938 }
939 }
940 }
941 else
942 {
943 if (!array_key_exists('FUSER_ID', $itemDat) || intval($itemDat['FUSER_ID']) <= 0)
944 {
945 $itemDat['FUSER_ID'] = Sale\Fuser::getId();
946 }
947
949 $basketClassName = $registry->getBasketClassName();
950 if ($basket = $basketClassName::loadItemsForFUser($itemDat["FUSER_ID"], $itemDat['LID']))
951 {
953 $item = $basket->getItemById($id);
954 }
955 }
956
957 if ($basket === null)
958 {
959 $result->addError(new Sale\ResultError(Main\Localization\Loc::getMessage('SALE_BASKET_COMPATIBLE_BASKET_COLLECTION_NOT_FOUND'), 'BASKET_COLLECTION_NOT_FOUND'));
960 return $result;
961 }
962
963
964 if ($item === null)
965 {
966 $result->addError(new Sale\ResultError(Main\Localization\Loc::getMessage('SALE_BASKET_COMPATIBLE_BASKET_ITEM_PROPS_NOT_FOUND'), 'BASKET_ITEM_PROPS_NOT_FOUND'));
967 return $result;
968
969 }
970
972 $r = $item->delete();
973 if (!$r->isSuccess())
974 {
975 $result->addErrors($r->getErrors());
976 }
977
978 if ($order !== null)
979 {
980 if (!$result->isSuccess())
981 {
982 return $result;
983 }
984
986 $r = $order->save();
987 }
988 else
989 {
990 if (!$result->isSuccess())
991 {
992 return $result;
993 }
994
996 $r = $basket->save();
997
998 if ($r->isSuccess())
999 {
1000 Sale\BasketComponentHelper::clearFUserBasketQuantity($itemDat['FUSER_ID'], $itemDat['LID']);
1001 Sale\BasketComponentHelper::clearFUserBasketPrice($itemDat['FUSER_ID'], $itemDat['LID']);
1002 }
1003 }
1004
1005 if (!$r->isSuccess())
1006 {
1007 $result->addErrors($r->getErrors());
1008 }
1009
1010 return $result;
1011 }
1012
1017 public static function getAliasFields()
1018 {
1019 return array(
1020 'ORDER_STATUS' => 'ORDER.STATUS_ID',
1021 'ORDER_CANCELED' => 'ORDER.CANCELED',
1022 'ORDER_PRICE' => 'ORDER.PRICE',
1023 'ORDER_DATE' => 'ORDER.DATE_INSERT',
1024
1025 'USER_ID' => 'USER.ID',
1026
1027 'SUM_PRICE' => 'SUMMARY_PRICE',
1028
1029 'ORDER_ALLOW_DELIVERY' => 'SHIPMENT.ALLOW_DELIVERY',
1030 'ORDER_DATE_ALLOW_DELIVERY' => 'SHIPMENT.DATE_ALLOW_DELIVERY',
1031 'DEDUCTED' => 'SHIPMENT.DEDUCTED',
1032 'SHIPMENT_SYSTEM' => 'SHIPMENT.SYSTEM',
1033
1034 'ORDER_PAYED' => 'PAYMENT.PAID',
1035 'ORDER_DATE_PAYED' => 'PAYMENT.DATE_PAID',
1036
1037
1038 );
1039 }
1040
1044 protected static function getSelectFields()
1045 {
1046 return array_keys(static::getEntity()->getScalarFields());
1047 }
1048
1053 public static function getAvailableFields()
1054 {
1055 $registry = Sale\Registry::getInstance(static::getRegistryType());
1056 $basketItemClassName = $registry->getBasketItemClassName();
1057 $fields = $basketItemClassName::getAvailableFields();
1058
1059 if ($index = array_search('SET_PARENT_ID', $fields))
1060 unset($fields[$index]);
1061
1062 return $fields;
1063 }
1064
1071 protected static function loadEntityFromBasket($id)
1072 {
1073 $result = new Sale\Result();
1074
1075 $order = null;
1076 $basket = null;
1077 $item = null;
1078
1079 $registry = Sale\Registry::getInstance(static::getRegistryType());
1080 $basketClassName = $registry->getBasketClassName();
1081 $res = $basketClassName::getList(array(
1082 'filter' => array(
1083 'ID' => $id
1084 ),
1085 'select' => array(
1086 'ID', 'ORDER_ID', 'SET_PARENT_ID', 'TYPE', 'FUSER_ID', 'LID'
1087 ),
1088 ));
1089 if (!$itemDat = $res->fetch())
1090 {
1091 $result->addError(new Sale\ResultError(Main\Localization\Loc::getMessage('SALE_BASKET_COMPATIBLE_BASKET_ITEM_NOT_FOUND'), 'BASKET_ITEM_NOT_FOUND'));
1092 return $result;
1093 }
1094
1095 if (intval($itemDat['ORDER_ID']) > 0)
1096 {
1097 $orderClassName = $registry->getOrderClassName();
1099 if ($order = $orderClassName::load(intval($itemDat['ORDER_ID'])))
1100 {
1101 if ($basket = $order->getBasket())
1102 {
1104 $item = $basket->getItemById($id);
1105 }
1106 }
1107 }
1108 else
1109 {
1110 $basketClassName = $registry->getBasketClassName();
1112 $basket = $basketClassName::loadItemsForFUser($itemDat["FUSER_ID"], $itemDat['LID']);
1113
1114 if ($basket)
1115 {
1117 $item = $basket->getItemById($id);
1118 }
1119
1120 }
1121
1122 $data = array(
1123 'BASKET_ITEM' => $item
1124 );
1125
1126 if ($order !== null)
1127 {
1128 $data['ORDER'] = $order;
1129 }
1130
1131 $result->setData($data);
1132
1133 return $result;
1134 }
1135
1145 protected static function appendBasketToOrder(Sale\Order $order, Sale\Basket $basket)
1146 {
1147 $result = new Sale\Result();
1148
1149 $orderBasketCollection = $order->getBasket();
1150
1151 $shipmentCollection = $order->getShipmentCollection();
1152 $systemShipment = $shipmentCollection->getSystemShipment();
1153 $systemShipmentItemCollection = $systemShipment->getShipmentItemCollection();
1154
1156 foreach ($basket as $basketItem)
1157 {
1158 $basketProperty = array();
1159 if ($basketPropertyCollection = $basketItem->getPropertyCollection())
1160 {
1161 $basketProperty = $basketPropertyCollection->getPropertyValues();
1162 }
1163
1164 if ($orderBasketItem = $orderBasketCollection->getExistsItem($basketItem->getField('MODULE'), $basketItem->getField('PRODUCT_ID'), $basketProperty))
1165 {
1166 $fields = $basketItem->getFieldValues();
1167 $orderBasketItem->setFields(static::clearFields($fields));
1168 }
1169 else
1170 {
1172 $orderBasketCollection->addItem($basketItem);
1173 $basketItem->setCollection($orderBasketCollection);
1174
1175
1176 $systemShipmentItemCollection->resetCollection($orderBasketCollection);
1177 }
1178 }
1179
1181 foreach ($basket as $basketItem)
1182 {
1183 if ($systemShipment->getDeliveryId() > 0)
1184 {
1186 $orderCompatibilityClassName = static::getOrderCompatibilityClassName();
1187
1189 $shipment = $orderCompatibilityClassName::getShipmentByDeliveryId($shipmentCollection, $systemShipment->getDeliveryId());
1190
1191 if (!$shipment)
1192 {
1193 if ($service = Sale\Delivery\Services\Manager::getObjectById($systemShipment->getDeliveryId()))
1194 {
1196 $shipment = $shipmentCollection->createItem($service);
1197 }
1198 }
1199
1200
1201 if ($shipment)
1202 {
1203 $shipmentItemCollection = $shipment->getShipmentItemCollection();
1204
1205 if (!$shipmentItem = $shipmentItemCollection->getItemByBasketCode($basketItem->getBasketCode()))
1206 {
1207 $shipmentItem = $shipmentItemCollection->createItem($basketItem);
1208 }
1209
1211 $r = $shipmentItem->setQuantity($basketItem->getQuantity());
1212 if (!$r->isSuccess())
1213 {
1214 $result->addErrors($r->getErrors());
1215 }
1216 }
1217 }
1218 }
1219
1220 return $result;
1221
1222 }
1223
1229 public static function setBasketFields(array $list)
1230 {
1231 $result = new Sale\Result();
1232
1233 $order = null;
1234 $basket = null;
1235 $basketItem = null;
1236 $orderId = null;
1237
1238 foreach ($list as $basketId => $fields)
1239 {
1240 $basketItemResult = static::loadEntityFromBasket($basketId);
1241 if ($basketItemResult->isSuccess())
1242 {
1243 $basketItemResultList = $basketItemResult->getData();
1244 if (isset($basketItemResultList['BASKET']))
1245 {
1247 $basket = $basketItemResultList['BASKET'];
1248 }
1249
1250 if (isset($basketItemResultList['BASKET_ITEM']))
1251 {
1253 $basketItem = $basketItemResultList['BASKET_ITEM'];
1254 }
1255
1256 if (isset($basketItemResultList['ORDER']))
1257 {
1259 $order = $basketItemResultList['ORDER'];
1260 }
1261 }
1262
1263 if ($basketItem === null)
1264 {
1265 $result->addError(new Sale\ResultError(Main\Localization\Loc::getMessage('SALE_BASKET_COMPATIBLE_BASKET_ITEM_NOT_FOUND'), 'BASKET_ITEM_NOT_FOUND'));
1266 return $result;
1267 }
1268
1269 if ($orderId === null && isset($fields['ORDER_ID']) && intval($fields['ORDER_ID']) > 0)
1270 {
1271 $orderId = (int)$fields['ORDER_ID'];
1272 }
1273
1274 if (isset($fields['ORDER_ID']))
1275 unset($fields['ORDER_ID']);
1276
1277 $basketItem->setFields($fields);
1278
1279 if ($order === null && intval($orderId) > 0)
1280 {
1281 $registry = Sale\Registry::getInstance(static::getRegistryType());
1282 $orderClassName = $registry->getOrderClassName();
1284 $order = $orderClassName::load($orderId);
1285 }
1286
1287 }
1288
1289 if ($order === null)
1290 {
1291 return $result;
1292 }
1293
1294 if ($order !== null && $basket !== null)
1295 {
1296 $r = static::appendBasketToOrder($order, $basket->getOrderableItems());
1297 if(!$r->isSuccess())
1298 {
1299 $result->addErrors($r->getErrors());
1300 }
1301 }
1302
1304 $r = $order->save();
1305 if (!$r->isSuccess())
1306 {
1307 $result->addErrors($r->getErrors());
1308 }
1309 return $result;
1310 }
1311
1312
1316 protected function getOrderCompatibility()
1317 {
1319 }
1320
1333 public static function syncShipmentCollectionAndBasket(Sale\ShipmentCollection $shipmentCollection, Sale\Basket $basket)
1334 {
1335 $result = new Sale\Result();
1336
1337 if (count($shipmentCollection) > 2)
1338 {
1339 return $result;
1340 }
1341
1343 if (!$order = $shipmentCollection->getOrder())
1344 {
1345 throw new Main\ObjectNotFoundException('Entity "Order" not found');
1346 }
1347
1348 $baseShipment = null;
1349
1350 $shipmentCollection->setMathActionOnly(true);
1351
1352 if (count($shipmentCollection) == 1 && $shipmentCollection->isExistsSystemShipment())
1353 {
1355 if (!$systemShipment = $shipmentCollection->getSystemShipment())
1356 {
1357 throw new Main\ObjectNotFoundException('Entity system "Shipment" not found');
1358 }
1359
1360 $shipment = $shipmentCollection->createItem();
1361 $r = $shipmentCollection->cloneShipment($systemShipment, $shipment);
1362 if (!$r->isSuccess())
1363 {
1364 $result->addErrors($r->getErrors());
1365 return $result;
1366 }
1367 }
1368
1369
1371 foreach ($shipmentCollection as $shipment)
1372 {
1373 if ($shipment->isSystem() || $shipment->isShipped())
1374 continue;
1375
1377 foreach ($basket as $basketItem)
1378 {
1379 $shipmentItemCollection = $shipment->getShipmentItemCollection();
1380 if (!$shipmentItem = $shipmentItemCollection->getItemByBasketCode($basketItem->getBasketCode()))
1381 {
1382 $shipmentItem = $shipmentItemCollection->createItem($basketItem);
1383 }
1384
1385 if (!$shipmentItem)
1386 continue;
1387
1389 $r = $shipmentItem->setQuantity($basketItem->getQuantity());
1390 if (!$r->isSuccess())
1391 {
1392 $result->addErrors($r->getErrors());
1393 }
1394
1395 }
1396
1397 break;
1398 }
1399
1400 $shipmentCollection->setMathActionOnly(false);
1401
1402 return $result;
1403
1404 }
1405
1413 public static function convertBasketItemToArray(Sale\BasketItem $basketItem)
1414 {
1415 $fields = $basketItem->getFieldValues();
1416
1418 $collection = $basketItem->getCollection();
1419
1421 if (!$basket = $collection->getBasket())
1422 {
1423 throw new Main\ObjectNotFoundException('Entity "Basket" not found');
1424 }
1425
1426 if (empty($fields['LID']))
1427 $fields['LID'] = $basket->getSiteId();
1428
1429 if (empty($fields['LID']))
1430 {
1431 if ($order = $basket->getOrder())
1432 {
1433 $fields['LID'] = $order->getField('LID');
1434 }
1435 //$order->getField('LID')
1436
1437 }
1438
1439 if (empty($fields['FUSER_ID']))
1440 $fields['FUSER_ID'] = $basket->getFUserId(true);
1441
1442
1444 if ($propertyCollection = $basketItem->getPropertyCollection())
1445 {
1446 $fields['PROPS'] = $propertyCollection->getPropertyValues();
1447 }
1448
1449 return $fields;
1450 }
1451
1452 protected function getWhiteListFields()
1453 {
1454 return array_merge(parent::getWhiteListFields(), array('USER_ID'), array_keys(static::getAliasFields()));
1455 }
1456}
1457
1458
1460{
1461 public function adapt(array $row)
1462 {
1463 if(!empty($row["~DIMENSIONS"]) && is_array($row["~DIMENSIONS"]))
1464 $row["~DIMENSIONS"] = serialize($row["~DIMENSIONS"]);
1465
1466 if(!empty($row["DIMENSIONS"]) && is_array($row["DIMENSIONS"]))
1467 $row["DIMENSIONS"] = serialize($row["DIMENSIONS"]);
1468
1469 if(!empty($row["QUANTITY"]))
1470 $row["QUANTITY"] = Sale\BasketItem::formatQuantity($row['QUANTITY']);
1471
1472 if(!empty($row["~QUANTITY"]))
1473 $row["~QUANTITY"] = Sale\BasketItem::formatQuantity($row['~QUANTITY']);
1474
1475 return $row;
1476 }
1477}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static init($mode=self::MODE_CLIENT, $config=array())