Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
shipmentcollection.php
1<?php
2
3namespace Bitrix\Sale;
4
11
12Loc::loadMessages(__FILE__);
13
20{
22 protected $order;
23
28 protected function getEntityParent()
29 {
30 return $this->getOrder();
31 }
32
43 public function resetCollection()
44 {
45 $result = new Result();
46
47 $deliveryInfo = array();
48
49 if (count($this->collection) > 0)
50 {
52 foreach ($this->collection as $shipment)
53 {
54 if (empty($deliveryInfo))
55 {
56 if ($shipment->isSystem() && $shipment->getDeliveryId() > 0)
57 {
58 foreach (static::getClonedFields() as $field)
59 {
60 if (strval(trim($shipment->getField($field))) != '')
61 $deliveryInfo[$field] = trim($shipment->getField($field));
62 }
63 }
64 }
65 $shipment->delete();
66 }
67 }
68
69 $systemShipment = $this->getSystemShipment();
70
72 $systemShipmentItemCollection = $systemShipment->getShipmentItemCollection();
73
75 $basket = $this->getOrder()->getBasket();
76 $systemShipmentItemCollection->resetCollection($basket);
77
78 if (!empty($deliveryInfo))
79 {
80 $systemShipment->setFieldsNoDemand($deliveryInfo);
81 }
82
83 if (
85 && Configuration::getProductReservationCondition() == ReserveCondition::ON_CREATE
86 )
87 {
88 $r = $this->tryReserve();
89 if (!$r->isSuccess())
90 {
91 $result->addErrors($r->getErrors());
92 }
93 }
94
95 return $result;
96 }
97
101 protected function getDeletableItems()
102 {
103 return $this->getNotSystemItems();
104 }
105
114 public function createItem(Delivery\Services\Base $delivery = null)
115 {
117 $shipmentClassName = static::getItemCollectionClassName();
118 $shipment = $shipmentClassName::create($this, $delivery);
119 $this->addItem($shipment);
120
121 return $shipment;
122 }
123
131 protected function addItem(Internals\CollectableEntity $shipment)
132 {
134 $shipment = parent::addItem($shipment);
135
137 if (!$order = $this->getOrder())
138 {
139 throw new Main\ObjectNotFoundException('Entity "Order" not found');
140 }
141
142 $order->onShipmentCollectionModify(EventActions::ADD, $shipment);
143
144 return $shipment;
145 }
146
154 public function deleteItem($index)
155 {
156 $result = new Result();
158 $oldItem = parent::deleteItem($index);
159
161 if ($oldItem->getId() > 0 && !$oldItem->isSystem() && ($systemShipment = $this->getSystemShipment()) && $systemShipment->getId() == 0)
162 {
163 $r = $this->cloneShipment($oldItem, $systemShipment);
164 if (!$r->isSuccess())
165 {
166 $result->addErrors($r->getErrors());
167 }
168 }
169
170 $order = $this->getOrder();
171 $order->onShipmentCollectionModify(EventActions::DELETE, $oldItem);
172 }
173
190 public function onItemModify(Internals\CollectableEntity $item, $name = null, $oldValue = null, $value = null)
191 {
193 $order = $this->getOrder();
194
195 if ($item instanceof Shipment)
196 {
197 return $order->onShipmentCollectionModify(EventActions::UPDATE, $item, $name, $oldValue, $value);
198 }
199
200 return new Result();
201 }
202
208 public function getOrder()
209 {
210 return $this->order;
211 }
212
220 public static function load(Order $order)
221 {
223 $shipmentCollection = static::createShipmentCollectionObject();
224 $shipmentCollection->setOrder($order);
225
226 if ($order->getId() > 0)
227 {
229 $shipmentClassName = static::getItemCollectionClassName();
230 $shipmentList = $shipmentClassName::loadForOrder($order->getId());
232 foreach ($shipmentList as $shipment)
233 {
234 $shipment->setCollection($shipmentCollection);
235 $shipmentCollection->addItem($shipment);
236 }
237
238 $controller = Internals\CustomFieldsController::getInstance();
239 $controller->initializeCollection($shipmentCollection);
240 }
241
242 return $shipmentCollection;
243 }
244
248 private static function createShipmentCollectionObject()
249 {
250 $registry = Registry::getInstance(static::getRegistryType());
251 $className = $registry->getShipmentCollectionClassName();
252
253 return new $className();
254 }
255
259 public static function getRegistryType()
260 {
262 }
263
269 public function getSystemShipment()
270 {
272 foreach ($this->collection as $shipment)
273 {
274 if ($shipment->isSystem())
275 {
276 return $shipment;
277 }
278 }
279
281 $shipmentClassName = static::getItemCollectionClassName();
282 $shipment = $shipmentClassName::createSystem($this);
283 $this->addItem($shipment);
284
285 return $shipment;
286 }
287
293 public function isExistsSystemShipment()
294 {
296 foreach ($this->collection as $shipment)
297 {
298 if ($shipment->isSystem())
299 return true;
300 }
301
302 return false;
303 }
304
313 public function save()
314 {
315 $result = new Entity\Result();
316
318 if (!$order = $this->getOrder())
319 {
320 throw new Main\ObjectNotFoundException('Entity "Order" not found');
321 }
322
323 $itemsFromDb = array();
324 if ($order->getId() > 0)
325 {
326 $itemsFromDbList = static::getList(
327 array(
328 "filter" => array("ORDER_ID" => $order->getId()),
329 "select" => array("ID" , "DELIVERY_NAME", "DELIVERY_ID")
330 )
331 );
332 while ($itemsFromDbItem = $itemsFromDbList->fetch())
333 $itemsFromDb[$itemsFromDbItem["ID"]] = $itemsFromDbItem;
334 }
335
337 foreach ($this->collection as $shipment)
338 {
339 if ($shipment->isSystem())
340 continue;
341
342 if (($systemShipment = $this->getSystemShipment()) && $systemShipment->getId() == 0)
343 {
345 $r = $this->cloneShipment($shipment, $systemShipment);
346 if ($r->isSuccess())
347 {
348 break;
349 }
350 else
351 {
352 $result->addErrors($r->getErrors());
353 }
354 }
355 }
356
357 $changeMeaningfulFields = array(
358 "DELIVERY_LOCATION",
359 "PRICE_DELIVERY",
360 "CUSTOM_PRICE_DELIVERY",
361 "ALLOW_DELIVERY",
362 "DEDUCTED",
363 "RESERVED",
364 "DELIVERY_NAME",
365 "DELIVERY_ID",
366 "CANCELED",
367 "MARKED",
368 "SYSTEM",
369 "COMPANY_ID",
370 "DISCOUNT_PRICE",
371 "BASE_PRICE_DELIVERY",
372 "EXTERNAL_DELIVERY",
373 );
374
376 foreach ($this->collection as $shipment)
377 {
378 $isNew = (bool)($shipment->getId() <= 0);
379 $isChanged = $shipment->isChanged();
380
381 if ($order->getId() > 0 && $isChanged)
382 {
383 $logFields = array();
384
385
386 $fields = $shipment->getFields();
387 $originalValues = $fields->getOriginalValues();
388
389 foreach($originalValues as $originalFieldName => $originalFieldValue)
390 {
391 if (in_array($originalFieldName, $changeMeaningfulFields) && $shipment->getField($originalFieldName) != $originalFieldValue)
392 {
393 $logFields[$originalFieldName] = $shipment->getField($originalFieldName);
394 if (!$isNew)
395 $logFields['OLD_'.$originalFieldName] = $originalFieldValue;
396 }
397 }
398
399 }
400
401 $r = $shipment->save();
402 if ($r->isSuccess())
403 {
404 if ($order->getId() > 0)
405 {
406 if ($isChanged)
407 {
408 $registry = Registry::getInstance(static::getRegistryType());
409
411 $orderHistory = $registry->getOrderHistoryClassName();
412 $orderHistory::addLog(
413 'SHIPMENT',
414 $order->getId(),
415 $isNew ? 'SHIPMENT_ADD' : 'SHIPMENT_UPDATE',
416 $shipment->getId(),
417 $shipment,
418 $logFields,
419 $orderHistory::SALE_ORDER_HISTORY_LOG_LEVEL_1
420 );
421
422 $orderHistory::addAction(
423 'SHIPMENT',
424 $order->getId(),
425 "SHIPMENT_SAVED",
426 $shipment->getId(),
427 $shipment,
428 array(),
430 );
431 }
432 }
433
434 }
435 else
436 {
437 $result->addErrors($r->getErrors());
438 }
439
440 if (isset($itemsFromDb[$shipment->getId()]))
441 unset($itemsFromDb[$shipment->getId()]);
442 }
443
444 foreach ($itemsFromDb as $k => $v)
445 {
446 $v['ENTITY_REGISTRY_TYPE'] = static::getRegistryType();
447
449 $event = new Main\Event('sale', "OnBeforeSaleShipmentDeleted", array(
450 'VALUES' => $v,
451 ));
452 $event->send();
453
454 $this->deleteInternal($k);
456
458 $event = new Main\Event('sale', "OnSaleShipmentDeleted", array(
459 'VALUES' => $v,
460 ));
461 $event->send();
462
463 if ($order->getId() > 0)
464 {
465 $registry = Registry::getInstance(static::getRegistryType());
466
468 $orderHistory = $registry->getOrderHistoryClassName();
469 $orderHistory::addAction(
470 'SHIPMENT',
471 $order->getId(),
472 'SHIPMENT_REMOVED',
473 $k,
474 null,
475 array(
476 'ID' => $k,
477 'DELIVERY_NAME' => $v['DELIVERY_NAME'],
478 'DELIVERY_ID' => $v['DELIVERY_ID'],
479 )
480 );
481
482 $registry = Registry::getInstance(static::getRegistryType());
483
485 $entityMarker = $registry->getEntityMarkerClassName();
486 $entityMarker::deleteByFilter(array(
487 '=ORDER_ID' => $order->getId(),
488 '=ENTITY_TYPE' => $entityMarker::ENTITY_TYPE_SHIPMENT,
489 '=ENTITY_ID' => $k,
490 ));
491 }
492
493 }
494
495 if ($order->getId() > 0)
496 {
497 $registry = Registry::getInstance(static::getRegistryType());
498
500 $orderHistory = $registry->getOrderHistoryClassName();
501 $orderHistory::collectEntityFields('SHIPMENT', $order->getId());
502 }
503
504 return $result;
505 }
506
512 public function setOrder(Order $order)
513 {
514 $this->order = $order;
515 }
516
524 public function cloneShipment(Shipment $parentShipment, Shipment $childShipment)
525 {
526 foreach (static::getClonedFields() as $fieldName)
527 {
529 $childShipment->setFieldNoDemand($fieldName, $parentShipment->getField($fieldName));
530 }
531
532 $childShipment->setExtraServices($parentShipment->getExtraServices());
533 $childShipment->setStoreId($parentShipment->getStoreId());
534 return new Result();
535 }
536
542 protected static function getClonedFields()
543 {
544 return array(
545 'DELIVERY_LOCATION',
546 'PARAMS',
547 'DELIVERY_ID',
548 'DELIVERY_NAME',
549 );
550 }
551
557 public function isShipped()
558 {
559 if (!empty($this->collection) && is_array($this->collection))
560 {
562 foreach ($this->collection as $shipment)
563 {
564 if ($shipment->isSystem())
565 {
566 if (!$shipment->isEmpty())
567 {
568 return false;
569 }
570
571 continue;
572 }
573
574 if (!$shipment->isShipped() && !$shipment->isEmpty())
575 {
576 return false;
577 }
578 }
579
580 return true;
581 }
582
583 return false;
584 }
585
591 public function hasShipped()
592 {
593 if (!empty($this->collection) && is_array($this->collection))
594 {
596 foreach ($this->collection as $shipment)
597 {
598 if ($shipment->isSystem())
599 {
600 continue;
601 }
602
603 if ($shipment->isShipped() && !$shipment->isEmpty())
604 {
605 return true;
606 }
607 }
608 }
609
610 return false;
611 }
612
618 public function isMarked()
619 {
620 if (!empty($this->collection) && is_array($this->collection))
621 {
623 foreach ($this->collection as $shipment)
624 {
625 if ($shipment->isSystem())
626 continue;
627
628 if ($shipment->isMarked())
629 return true;
630 }
631 }
632
633 return false;
634 }
635
641 public function isReserved()
642 {
643 if (!empty($this->collection) && is_array($this->collection))
644 {
646 foreach ($this->collection as $shipment)
647 {
648 if ($shipment->isSystem())
649 {
650 if (count($this->collection) == 1)
651 return $shipment->isReserved();
652
653 continue;
654 }
655
656 if (!$shipment->isReserved())
657 return false;
658 }
659
660 return true;
661 }
662
663 return false;
664 }
665
671 public function isAllowDelivery()
672 {
673 if (!empty($this->collection) && is_array($this->collection))
674 {
676 foreach ($this->collection as $shipment)
677 {
678 if ($shipment->isSystem())
679 {
680 if (!$shipment->isEmpty())
681 {
682 return false;
683 }
684
685 continue;
686 }
687
688 if (!$shipment->isAllowDelivery() && !$shipment->isEmpty())
689 {
690 return false;
691 }
692 }
693
694 return true;
695 }
696
697 return false;
698 }
699
703 public function hasAllowDelivery()
704 {
706
708 foreach ($collection as $shipment)
709 {
710 if ($shipment->isAllowDelivery())
711 {
712 return true;
713 }
714 }
715
716 return false;
717 }
718
724 public function isEmptySystemShipment()
725 {
726 return $this->getSystemShipment()->isEmpty();
727 }
728
734 public function allowDelivery()
735 {
736 $result = new Result();
737
739
741 foreach ($collection as $shipment)
742 {
743 $r = $shipment->allowDelivery();
744 if (!$r->isSuccess())
745 {
746 $result->addErrors($r->getErrors());
747 }
748 }
749 return $result;
750 }
751
756 public function disallowDelivery()
757 {
758 $result = new Result();
759
761
763 foreach ($collection as $shipment)
764 {
765 $r = $shipment->disallowDelivery();
766 if (!$r->isSuccess())
767 {
768 $result->addErrors($r->getErrors());
769 }
770 }
771
772 return $result;
773 }
774
779 public function tryReserve()
780 {
781 $result = new Result();
782
784 foreach ($this->collection as $shipment)
785 {
786 if ($shipment->isReserved() || $shipment->isShipped())
787 continue;
788
789 $r = $shipment->tryReserve();
790 if (!$r->isSuccess())
791 {
792 $result->addErrors($r->getErrors());
793 }
794 elseif ($r->hasWarnings())
795 {
796 $result->addWarnings($r->getWarnings());
797
798 $registry = Registry::getInstance(static::getRegistryType());
800 $entityMarker = $registry->getEntityMarkerClassName();
801 $entityMarker::addMarker($this->getOrder(), $shipment, $r);
802 if (!$shipment->isSystem())
803 {
804 $shipment->setField('MARKED', 'Y');
805 }
806 }
807 }
808 return $result;
809 }
810
820 public function tryUnreserve()
821 {
822 $result = new Result();
823
824 if (!$order = $this->getOrder())
825 {
826 throw new Main\ObjectNotFoundException('Entity "Order" not found');
827 }
829 foreach ($this->collection as $shipment)
830 {
831 if ($shipment->isShipped())
832 {
833 if ($order &&
834 !Internals\ActionEntity::isTypeExists(
835 $order->getInternalId(),
837 )
838 )
839 {
840 Internals\ActionEntity::add(
841 $order->getInternalId(),
843 array(
844 'METHOD' => 'Bitrix\Sale\Shipment::updateReservedFlag',
845 'PARAMS' => array($shipment)
846 )
847 );
848 }
849
850 continue;
851 }
852
853 $r = $shipment->tryUnreserve();
854 if (!$r->isSuccess())
855 {
856 if (!$shipment->isSystem())
857 {
858 $registry = Registry::getInstance(static::getRegistryType());
859
861 $entityMarker = $registry->getEntityMarkerClassName();
862 $entityMarker::addMarker($order, $shipment, $r);
863
864 $shipment->setField('MARKED', 'Y');
865 }
866 $result->addErrors($r->getErrors());
867 }
868 elseif ($r->hasWarnings())
869 {
870 $result->addWarnings($r->getWarnings());
871 }
872 }
873
874 return $result;
875 }
876
889 public function onBeforeBasketItemDelete(BasketItem $basketItem)
890 {
891 $result = new Result();
892
894 foreach ($this->collection as $shipment)
895 {
896 $r = $shipment->onBeforeBasketItemDelete($basketItem);
897 if (!$r->isSuccess())
898 {
899 $result->addErrors($r->getErrors());
900 }
901 }
902
903 return $result;
904 }
905
920 public function onBasketModify($action, BasketItemBase $basketItem, $name = null, $oldValue = null, $value = null) : Result
921 {
922 $result = new Result();
923
924 if (!($basketItem instanceof BasketItem))
925 {
926 return $result;
927 }
928
929 if ($action === EventActions::DELETE)
930 {
931 $order = $this->getOrder();
932 if ($order->getId() == 0 && !$order->isMathActionOnly())
933 {
934 $this->refreshData();
935 }
936
937 return $result;
938 }
939 elseif ($action === EventActions::ADD)
940 {
941 return $this->getSystemShipment()->onBasketModify($action, $basketItem, $name, $oldValue, $value);
942 }
943 elseif ($action !== EventActions::UPDATE)
944 {
945 return $result;
946 }
947
948 if ($name == 'QUANTITY')
949 {
950 if (!$this->isAllowAutoEdit($basketItem))
951 {
952 $result = $this->checkDistributedQuantity($basketItem, $value);
953 if (!$result->isSuccess())
954 {
955 return $result;
956 }
957 }
958
959 $shipment = $this->getItemForAutoEdit($basketItem);
960
961 if ($value - $oldValue > 0)
962 {
963 $r = $this->getSystemShipment()->onBasketModify($action, $basketItem, $name, $oldValue, $value);
964 if (!$r->isSuccess())
965 {
966 return $result->addErrors($r->getErrors());
967 }
968 }
969
970 if ($shipment)
971 {
972 $r = $shipment->onBasketModify($action, $basketItem, $name, $oldValue, $value);
973 if (!$r->isSuccess())
974 {
975 $result->addErrors($r->getErrors());
976 return $result;
977 }
978 }
979
980 if ($value - $oldValue < 0)
981 {
982 $r = $this->getSystemShipment()->onBasketModify($action, $basketItem, $name, $oldValue, $value);
983 if (!$r->isSuccess())
984 {
985 return $result->addErrors($r->getErrors());
986 }
987 }
988 }
989 elseif (in_array($name, ['WEIGHT', 'PRICE']))
990 {
992 foreach ($this->getNotSystemItems() as $shipment)
993 {
994 $shipment->onBasketModify($action, $basketItem, $name, $value, $oldValue);
995 }
996 }
997
998 return $result;
999 }
1000
1007 private function getItemForAutoEdit(BasketItem $basketItem)
1008 {
1009 if ($this->isAllowAutoEdit($basketItem))
1010 {
1012 foreach ($this->getNotSystemItems() as $shipment)
1013 {
1014 return $shipment;
1015 }
1016 }
1017
1018 return null;
1019 }
1020
1027 protected function isAllowAutoEdit(BasketItem $basketItem)
1028 {
1029 if ($this->count() === 1
1030 ||
1031 (
1032 $this->count() === 2
1033 &&
1034 $this->isExistsSystemShipment()
1035 )
1036 )
1037 {
1038 if (!$this->getSystemShipment()->isExistBasketItem($basketItem)
1039 || (int)$basketItem->getId() === 0
1040 )
1041 {
1042 foreach ($this->getNotSystemItems() as $shipment)
1043 {
1044 if (!$shipment->isAllowDelivery()
1045 && !$shipment->isCanceled()
1046 && !$shipment->isShipped()
1047 )
1048 {
1050 if ($deliveryService = $shipment->getDelivery())
1051 {
1052 return $deliveryService->isAllowEditShipment();
1053 }
1054 }
1055 }
1056 }
1057 }
1058
1059 return false;
1060 }
1061
1070 private function checkDistributedQuantity(BasketItem $basketItem, $value)
1071 {
1072 $result = new Result();
1073
1074 $basketItemQuantity = $this->getBasketItemDistributedQuantity($basketItem);
1075 if ($basketItemQuantity > $value)
1076 {
1077 $result->addError(new ResultError(
1078 Loc::getMessage('SALE_ORDER_SYSTEM_SHIPMENT_LESS_QUANTITY',
1079 array(
1080 '#PRODUCT_NAME#' => $basketItem->getField("NAME"),
1081 '#BASKET_ITEM_QUANTITY#' => $basketItemQuantity,
1082 '#BASKET_ITEM_MEASURE#' => $basketItem->getField("MEASURE_NAME"),
1083 '#QUANTITY#' => $basketItemQuantity - $value
1084 )
1085 ),
1086 'SALE_ORDER_SYSTEM_SHIPMENT_LESS_QUANTITY')
1087 );
1088 }
1089
1090 return $result;
1091 }
1092
1104 public function onOrderModify($name, $oldValue, $value)
1105 {
1106 $result = new Result();
1107
1108 switch($name)
1109 {
1110 case "CANCELED":
1111 if ($value == "Y")
1112 {
1113 $isShipped = false;
1115 foreach ($this->collection as $shipment)
1116 {
1117 if ($shipment->isShipped())
1118 {
1119 $isShipped = true;
1120 break;
1121 }
1122 }
1123
1124 if ($isShipped)
1125 {
1126 $result->addError(
1127 new ResultError(
1128 Loc::getMessage('SALE_ORDER_CANCEL_SHIPMENT_EXIST_SHIPPED'),
1129 'SALE_ORDER_CANCEL_SHIPMENT_EXIST_SHIPPED'
1130 )
1131 );
1132
1133 return $result;
1134 }
1135
1136 $this->tryUnreserve();
1137 }
1139 {
1141 foreach ($this->collection as $shipment)
1142 {
1143 if ($shipment->needReservation())
1144 {
1146 $r = $shipment->tryReserve();
1147 if (!$r->isSuccess())
1148 {
1149 $registry = Registry::getInstance(static::getRegistryType());
1150
1152 $entityMarker = $registry->getEntityMarkerClassName();
1153 $entityMarker::addMarker($this->getOrder(), $shipment, $r);
1154 if (!$shipment->isSystem())
1155 {
1156 $shipment->setField('MARKED', 'Y');
1157 }
1158
1159 $result->addErrors($r->getErrors());
1160 }
1161 }
1162 }
1163
1164 }
1165 break;
1166
1167 case "MARKED":
1168 if ($value == "N")
1169 {
1171 foreach ($this->collection as $shipment)
1172 {
1173 if ($shipment->isSystem())
1174 continue;
1175
1176 $shipment->setField('MARKED', $value);
1177 }
1178 }
1179 break;
1180 }
1181
1182 return $result;
1183 }
1184
1189 public function refreshData()
1190 {
1191 $result = new Result();
1192
1193 $this->resetData();
1194
1195 $r = $this->calculateDelivery();
1196 if (!$r->isSuccess())
1197 {
1198 $result->addErrors($r->getErrors());
1199 }
1200
1201 return $result;
1202 }
1203
1207 public function calculateDelivery()
1208 {
1210 $result = new Result();
1211
1212 $calculatedDeliveries = [];
1213
1214 $collection = $this->getNotSystemItems();
1215
1217 foreach ($collection as $shipment)
1218 {
1219 if ($shipment->getDeliveryId() == 0)
1220 continue;
1221
1222 if ($shipment->isCustomPrice())
1223 {
1224 $priceDelivery = $shipment->getPrice();
1225
1226 $calcResult = new Delivery\CalculationResult();
1227 $calcResult->setDeliveryPrice($priceDelivery);
1228 }
1229 else
1230 {
1232 $calcResult = $shipment->calculateDelivery();
1233 if (!$calcResult->isSuccess())
1234 {
1235 $result->addErrors($calcResult->getErrors());
1236 continue;
1237 }
1238
1239 $priceDelivery = $calcResult->getPrice();
1240 if ($priceDelivery < 0)
1241 {
1242 $result->addError(new ResultError(Loc::getMessage('SALE_ORDER_SHIPMENT_WRONG_DELIVERY_PRICE'), 'WRONG_DELIVERY_PRICE'));
1243 continue;
1244 }
1245 }
1246
1247 $priceDelivery = PriceMaths::roundPrecision($priceDelivery);
1248 $shipment->setField('BASE_PRICE_DELIVERY', $priceDelivery);
1249
1250 $calculatedDeliveries[] = $calcResult;
1251 }
1252
1253 $result->setData(['CALCULATED_DELIVERIES' => $calculatedDeliveries]);
1254
1255 return $result;
1256 }
1257
1261 public function resetData()
1262 {
1263 $collection = $this->getNotSystemItems();
1264
1266 foreach ($collection as $shipment)
1267 {
1268 $shipment->resetData();
1269 }
1270 }
1271
1279 public function getBasketItemDistributedQuantity(BasketItem $basketItem)
1280 {
1281 $collection = $this->getNotSystemItems();
1282
1283 $allQuantity = 0;
1284
1286 foreach ($collection as $shipment)
1287 {
1288 $allQuantity += $shipment->getBasketItemQuantity($basketItem);
1289 }
1290
1291 return $allQuantity;
1292 }
1293
1301 public function getBasketItemShippedQuantity(BasketItem $basketItem)
1302 {
1303 $quantity = 0;
1304
1306 foreach ($this->collection as $shipment)
1307 {
1308 if ($shipment->isShipped())
1309 {
1310 $quantity += $shipment->getShipmentItemCollection()->getBasketItemQuantity($basketItem);
1311 }
1312 }
1313
1314 return $quantity;
1315 }
1316
1324 public function isExistBasketItem(BasketItem $basketItem, $includeSystemShipment = false)
1325 {
1327 foreach ($this->collection as $shipment)
1328 {
1329 if (!$includeSystemShipment && $shipment->isSystem())
1330 {
1331 continue;
1332 }
1333
1334 return $shipment->isExistBasketItem($basketItem);
1335 }
1336
1337 return false;
1338 }
1342 public function getBasePriceDelivery()
1343 {
1344 $collection = $this->getNotSystemItems();
1345
1346 $sum = 0;
1348 foreach ($collection as $shipment)
1349 {
1350 $sum += $shipment->getField('BASE_PRICE_DELIVERY');
1351 }
1352
1353 return $sum;
1354 }
1355
1359 public function getPriceDelivery()
1360 {
1361 $collection = $this->getNotSystemItems();
1362
1363 $sum = 0;
1365 foreach ($collection as $shipment)
1366 {
1367 $sum += $shipment->getPrice();
1368 }
1369
1370
1371 return $sum;
1372 }
1373
1378 public function getItemByShipmentCode($itemCode)
1379 {
1381 foreach ($this->collection as $shipment)
1382 {
1383 $shipmentCode = $shipment->getShipmentCode();
1384 if ($itemCode == $shipmentCode)
1385 return $shipment;
1386
1387 }
1388
1389 return null;
1390 }
1391
1400 public function verify()
1401 {
1402 $result = new Result();
1403
1405 foreach ($this->collection as $shipment)
1406 {
1407 if ($shipment->isSystem())
1408 {
1409 continue;
1410 }
1411
1412 $r = $shipment->verify();
1413 if (!$r->isSuccess())
1414 {
1415 $result->addErrors($r->getErrors());
1416
1417 $registry = Registry::getInstance(static::getRegistryType());
1418
1420 $entityMarker = $registry->getEntityMarkerClassName();
1421 $entityMarker::addMarker($this->getOrder(), $shipment, $r);
1422
1423 $shipment->setField('MARKED', 'Y');
1424 }
1425 }
1426
1427 return $result;
1428 }
1429
1436 public function createClone(\SplObjectStorage $cloneEntity)
1437 {
1438 if ($this->isClone() && $cloneEntity->contains($this))
1439 {
1440 return $cloneEntity[$this];
1441 }
1442
1444 $shipmentCollectionClone = parent::createClone($cloneEntity);
1445
1446 if ($this->order)
1447 {
1448 if ($cloneEntity->contains($this->order))
1449 {
1450 $shipmentCollectionClone->order = $cloneEntity[$this->order];
1451 }
1452 }
1453
1454 return $shipmentCollectionClone;
1455 }
1456
1462 public function getErrorEntity($value)
1463 {
1464 $className = null;
1466 foreach ($this->collection as $shipment)
1467 {
1468 if ($className = $shipment->getErrorEntity($value))
1469 {
1470 break;
1471 }
1472 }
1473
1474 return $className;
1475 }
1476
1482 public function canAutoFixError($value)
1483 {
1484 $autoFix = false;
1485
1487 foreach ($this->collection as $shipment)
1488 {
1489 if ($autoFix = $shipment->canAutoFixError($value))
1490 {
1491 break;
1492 }
1493 }
1494 return $autoFix;
1495 }
1496
1502 public static function updateReservedFlag(ShipmentCollection $collection)
1503 {
1504 $result = new Result();
1506 foreach ($collection as $shipment)
1507 {
1509 $shipmentClassName = static::getItemCollectionClassName();
1510 $r = $shipmentClassName::updateReservedFlag($shipment);
1511 if (!$r->isSuccess())
1512 {
1513 $result->addErrors($r->getErrors());
1514 }
1515
1516 if ($r->hasWarnings())
1517 {
1518 $result->addWarnings($r->getWarnings());
1519 }
1520 }
1521
1522 return $result;
1523 }
1524
1528 private static function getItemCollectionClassName()
1529 {
1530 $registry = Registry::getInstance(static::getRegistryType());
1531 return $registry->getShipmentClassName();
1532 }
1533
1538 public static function getList(array $parameters = array())
1539 {
1540 return Internals\ShipmentTable::getList($parameters);
1541 }
1542
1547 protected function deleteInternal($primary)
1548 {
1549 return Internals\ShipmentTable::deleteWithItems($primary);
1550 }
1551
1555 protected function deleteExtraServiceInternal($shipmentId)
1556 {
1557 Internals\ShipmentExtraServiceTable::deleteByShipmentId($shipmentId);
1558 }
1559
1560
1564 public function getNotSystemItems()
1565 {
1566 $callback = function (Shipment $shipment)
1567 {
1568 return !$shipment->isSystem();
1569 };
1570
1571 return new Internals\CollectionFilterIterator($this->getIterator(), $callback);
1572 }
1573
1574}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
onItemModify(CollectableEntity $item, $name=null, $oldValue=null, $value=null)
const SALE_ORDER_HISTORY_ACTION_LOG_LEVEL_1
static roundPrecision($value)
static getInstance($type)
Definition registry.php:183
static getList(array $parameters=array())
setFieldNoDemand($name, $value)
Definition shipment.php:863
setExtraServices(array $extraServices)