57 $pool = Internals\PoolQuantity::getInstance($key);
68 $pool = Internals\PoolQuantity::getInstance($key);
79 $pool = Internals\PoolQuantity::getInstance($key);
90 $poolInstance = Internals\PoolQuantity::getInstance($key);
95 $pool->addItem($code, $item);
105 $pool = Internals\PoolQuantity::getInstance($key);
115 $pool = Internals\PoolQuantity::getInstance($key);
124 $pool = Internals\PoolQuantity::getInstance($key);
135 $pool = Internals\PoolQuantity::getInstance($key);
147 $poolInstance = Internals\PoolQuantity::getInstance($key);
151 $pool->addItem($code, $item);
163 $pool = Internals\PoolQuantity::getInstance($key);
173 public static function onOrderSave(
Order $order)
177 static::resetTrustData($order->getSiteId());
180 $r = Internals\Catalog\Provider::save($order);
181 if (!$r->isSuccess())
183 $result->addErrors($r->getErrors());
186 if ($r->hasWarnings())
188 $result->addWarnings($r->getWarnings());
189 EntityMarker::addMarker($order, $order, $r);
190 if ($order->getId() > 0)
192 Internals\OrderTable::update($order->getId(), array(
'MARKED' =>
'Y'));
196 static::refreshMarkers($order);
210 public static function shipBasketItem(BasketItemBase $basketItem)
213 $result =
new Result();
216 if (!$basket = $basketItem->getCollection())
218 throw new ObjectNotFoundException(
'Entity "Basket" not found');
222 if (!$order = $basket->getOrder())
224 throw new ObjectNotFoundException(
'Entity "Order" not found');
228 $shipmentCollection = $order->getShipmentCollection();
231 foreach ($shipmentCollection as $shipment)
233 $needShip = $shipment->needShip();
234 if ($needShip ===
null)
237 $r = static::shipShipment($shipment);
238 if (!$r->isSuccess())
240 $result->addErrors($r->getErrors());
242 elseif ($r->hasWarnings())
244 $result->addWarnings($r->getWarnings());
245 EntityMarker::addMarker($order, $shipment, $r);
246 if (!$shipment->isSystem())
248 $shipment->setField(
'MARKED',
'Y');
262 public static function shipShipment(Shipment $shipment)
264 $result =
new Result();
267 if (!$shipmentItemCollection = $shipment->getShipmentItemCollection())
269 throw new ObjectNotFoundException(
'Entity "ShipmentCollection" not found');
273 if (!$shipmentCollection = $shipment->getCollection())
275 throw new ObjectNotFoundException(
'Entity "ShipmentCollection" not found');
279 if (!$order = $shipmentCollection->getOrder())
281 throw new ObjectNotFoundException(
'Entity "Order" not found');
284 $pool = Internals\PoolQuantity::getInstance($order->getInternalId());
293 $resultList = array();
295 $basketList = static::getBasketFromShipmentItemCollection($shipmentItemCollection);
297 $basketProviderMap = static::createProviderBasketMap($basketList, array(
'QUANTITY',
'RESERVED'));
298 $basketProviderList = static::redistributeToProviders($basketProviderMap);
299 $storeDataList = array();
305 $r = static::getStoreDataFromShipmentItemCollection($shipmentItemCollection);
306 if (!$r->isSuccess())
308 $result->addErrors($r->getErrors());
312 $resultStoreData = $r->getData();
313 if (!empty($resultStoreData[
'STORE_DATA_LIST']))
315 $storeDataList = $resultStoreData[
'STORE_DATA_LIST'];
321 if (!empty($basketProviderList))
323 foreach ($basketProviderList as $provider => $providerBasketItemList)
325 if ($provider && array_key_exists(
"IBXSaleProductProvider", class_implements($provider)))
328 foreach ($providerBasketItemList as $providerBasketItem)
331 if ($providerBasketItem[
'BASKET_ITEM']->isBundleParent())
336 $poolQuantity = static::getQuantityPoolItem($order->getInternalId(), $providerBasketItem[
'BASKET_ITEM']);
338 if ($poolQuantity == 0)
341 if ($providerBasketItem[
'BASKET_ITEM']->getField(
'MODULE') !=
'')
343 $shipFields = array_merge($providerBasketItem, array(
344 'DEDUCTION' => ($poolQuantity < 0)
347 $r = static::shipProductData($provider, $shipFields, $storeDataList);
349 if (!$r->isSuccess())
351 $result->addErrors($r->getErrors());
353 $resultProductData = $r->getData();
358 $resultProductData[
'RESULT'] =
true;
361 $resultList[$providerBasketItem[
'BASKET_CODE']] = $resultProductData;
363 if (array_key_exists(
"RESULT", $resultProductData)
364 && $resultProductData[
'RESULT'] ===
false && $poolQuantity < 0)
373 elseif (class_exists($provider))
376 'SITE_ID' => $order->getSiteId(),
377 'CURRENCY' => $order->getCurrency(),
380 if ($order->getUserId() > 0)
382 $context[
'USER_ID'] = $order->getUserId();
387 $context[
'USER_ID'] = $USER->getId();
390 $creator = Internals\ProviderCreator::create($context);
392 foreach ($shipmentItemCollection as $shipmentItem)
394 $basketItem = $shipmentItem->getBasketItem();
395 $providerClass = $basketItem->getProviderEntity();
397 if ($providerClass instanceof SaleProviderBase)
399 $shipmentProductData = $creator->createItemForShip($shipmentItem);
400 $creator->addProductData($shipmentProductData);
404 $r = $creator->ship();
405 if (!$r->isSuccess())
407 $result->addErrors($r->getErrors());
410 $r = $creator->setItemsResultAfterShip($r);
411 if (!$r->isSuccess())
413 $result->addErrors($r->getErrors());
419 if ($reverse ===
true)
421 static::reverseShipment($shipment, $resultList);
425 static::setShipmentItemReserved($shipment);
438 public static function shipProductData($provider, array $fields, array $storeDataList = array())
440 $result =
new Result();
442 $quantity = $fields[
'QUANTITY'];
443 $basketCode = $fields[
'BASKET_CODE'];
446 $basketItem = $fields[
'BASKET_ITEM'];
449 $basket = $basketItem->getCollection();
452 $order = $basket->getOrder();
455 "BASKET_ITEM" => $basketItem,
456 "PRODUCT_ID" => $fields[
'PRODUCT_ID'],
457 "QUANTITY" => $quantity,
458 "PRODUCT_RESERVED" =>
"N",
459 'UNDO_DEDUCTION' => $fields[
'DEDUCTED']?
'N' :
'Y',
463 if ($data[
'UNDO_DEDUCTION'] ==
'N')
465 $data[
'PRODUCT_RESERVED'] =
"Y";
468 if (!empty($fields[
'RESERVED']))
470 $data[
'PRODUCT_RESERVED'] = $fields[
'RESERVED'] ?
'Y' :
'N';
473 $resultProductData = array();
478 if (!empty($storeDataList) && is_array($storeDataList) && isset($storeDataList[$basketCode]))
480 $data[
'STORE_DATA'] = $storeDataList[$basketCode];
483 if (!empty($data[
'STORE_DATA']))
485 $allBarcodeQuantity = 0;
486 foreach($data[
'STORE_DATA'] as $basketShipmentItemStore)
488 $allBarcodeQuantity += $basketShipmentItemStore[
'QUANTITY'];
491 if ($quantity > $allBarcodeQuantity)
493 $result->addWarning(
new ResultWarning(
Loc::getMessage(
'SALE_PROVIDER_SHIPMENT_SHIPPED_LESS_QUANTITY', array(
494 '#PRODUCT_NAME#' => $basketItem->getField(
'NAME')
495 )),
'SALE_PROVIDER_SHIPMENT_SHIPPED_LESS_QUANTITY'));
497 $resultProductData[
'RESULT'] =
false;
499 elseif ($quantity < $allBarcodeQuantity)
501 $result->addWarning(
new ResultWarning(
Loc::getMessage(
'SALE_PROVIDER_SHIPMENT_SHIPPED_MORE_QUANTITY', array(
502 '#PRODUCT_NAME#' => $basketItem->getField(
'NAME')
503 )),
'SALE_PROVIDER_SHIPMENT_SHIPPED_MORE_QUANTITY'));
505 $resultProductData[
'RESULT'] =
false;
511 if (!isset($resultProductData[
'RESULT'])
512 || $resultProductData[
'RESULT'] !==
false)
515 $APPLICATION->ResetException();
516 $resultProductData = $provider::DeductProduct($data);
518 $result->setData($resultProductData);
520 $needShip = $fields[
'DEDUCTED'];
521 $oldException = $APPLICATION->GetException();
522 if (!empty($oldException))
524 if ($needShip ===
true)
526 $result->addWarning(
new ResultWarning($oldException->GetString(), $oldException->GetID()) );
530 if (($oldException && $needShip ===
false) || !$oldException)
532 static::addQuantityPoolItem($order->getInternalId(), $basketItem, ($needShip? 1 : -1) * $quantity);
545 private static function reverseShipment(Shipment $shipment, array $shippedList)
547 $needShip = $shipment->needShip();
551 $shipmentItemCollection = $shipment->getShipmentItemCollection();
552 $basketList = static::getBasketFromShipmentItemCollection($shipmentItemCollection);
554 $bundleIndexList = static::getBundleIndexFromShipmentItemCollection($shipmentItemCollection);
556 $basketProviderMap = static::createProviderBasketMap($basketList, array(
'QUANTITY',
'RESERVED'));
557 $basketProviderList = static::redistributeToProviders($basketProviderMap);
562 $r = static::getStoreDataFromShipmentItemCollection($shipmentItemCollection);
565 if (!empty($basketProviderList))
567 foreach ($basketProviderList as $provider => $providerBasketItemList)
569 if ($provider && array_key_exists(
"IBXSaleProductProvider", class_implements($provider)))
572 foreach ($providerBasketItemList as $providerBasketItem)
574 if ($providerBasketItem[
'BASKET_ITEM']->isBundleParent())
579 $basketCode = $providerBasketItem[
'BASKET_CODE'];
580 if (!isset($shippedList[$basketCode])
581 || (array_key_exists(
"RESULT", $shippedList[$basketCode]) && $shippedList[$basketCode][
'RESULT'] ===
false))
583 if ($needShip && $shipment->isShipped())
590 if ($providerBasketItem[
'BASKET_ITEM']->getField(
'MODULE') !=
'')
593 "BASKET_ITEM" => $providerBasketItem[
'BASKET_ITEM'],
594 "PRODUCT_ID" => $providerBasketItem[
'PRODUCT_ID'],
595 "QUANTITY" => $providerBasketItem[
'QUANTITY'],
596 "PRODUCT_RESERVED" =>
"Y",
597 'UNDO_DEDUCTION' => $needShip?
'Y' :
'N',
603 $data[
'STORE_DATA'] = $storeData[$providerBasketItem[
'BASKET_CODE']];
605 $barcodeReverseList = array();
607 if (!empty($shippedList[$basketCode][
'BARCODE']) && is_array($shippedList[$basketCode][
'BARCODE']))
609 foreach ($shippedList[$basketCode][
'BARCODE'] as $barcodeValue => $barcodeShipped)
611 if ($barcodeShipped ===
true)
613 $barcodeReverseList[] = $barcodeValue;
617 foreach ($data[
'STORE_DATA'] as $storeId => $barcodeData)
619 if (!empty($barcodeData[
'BARCODE']) && is_array($barcodeData[
'BARCODE']))
621 if (empty($barcodeReverseList))
623 $data[
'STORE_DATA'][
$storeId][
'BARCODE'] = array();
627 foreach ($barcodeData[
'BARCODE'] as $barcodeId => $barcodeValue)
629 if (!in_array($barcodeValue, $barcodeReverseList))
631 unset($data[
'STORE_DATA'][$storeId][
'BARCODE'][$barcodeId]);
632 $data[
'STORE_DATA'][
$storeId][
'QUANTITY'] -= 1;
642 $resultProductData = $provider::DeductProduct($data);
646 $resultProductData[
'RESULT'] =
true;
650 $result[$providerBasketItem[
'BASKET_CODE']] = $resultProductData;
652 if (isset($resultProductData[
'RESULT'])
653 && $resultProductData[
'RESULT'] ===
true)
664 if ($correct ===
true)
666 $shipment->setFieldNoDemand(
'DEDUCTED', $needShip?
'N' :
'Y');
670 && !empty($bundleIndexList) && is_array($bundleIndexList))
673 foreach ($bundleIndexList as $bundleParentBasketCode => $bundleChildList)
675 $tryShipmentBundle =
false;
676 foreach($bundleChildList as $bundleChildBasketCode)
678 if (isset($result[$bundleChildBasketCode])
679 && $result[$bundleChildBasketCode][
'RESULT'] ===
true)
681 $tryShipmentBundle =
true;
685 $tryShipmentBundle =
false;
690 $result[$bundleParentBasketCode] = array(
691 'RESULT' => $tryShipmentBundle
704 private static function setShipmentItemReserved(Shipment $shipment)
707 $result =
new Result();
709 $needShip = $shipment->needShip();
711 if ($needShip ===
null
712 || ($needShip ===
false && !$shipment->isReserved()))
717 $order = $shipment->getParentOrder();
720 throw new ObjectNotFoundException(
'Entity "Order" not found');
725 && !$shipment->needReservation()
728 if ($needShip ===
false)
730 if (!Internals\ActionEntity::isTypeExists(
731 $order->getInternalId(),
736 Internals\ActionEntity::add(
737 $order->getInternalId(),
740 'METHOD' =>
'Bitrix\Sale\ShipmentCollection::updateReservedFlag',
741 'PARAMS' => array($shipment->getCollection())
751 $shipmentItemCollection = $shipment->getShipmentItemCollection();
754 foreach ($shipmentItemCollection as $shipmentItem)
758 $basketItem = $shipmentItem->getBasketItem();
761 $result->addError(
new ResultError(
763 'SALE_PROVIDER_BASKET_ITEM_NOT_FOUND',
765 '#BASKET_ITEM_ID#' => $shipmentItem->getBasketId(),
766 '#SHIPMENT_ID#' => $shipment->getId(),
767 '#SHIPMENT_ITEM_ID#' => $shipmentItem->getId(),
770 'PROVIDER_SET_SHIPMENT_ITEM_RESERVED_WRONG_BASKET_ITEM')
776 $providerName = $basketItem->getProvider();
777 $providerClass =
null;
779 if (class_exists($providerName))
781 $providerClass =
new $providerName();
783 if ($providerClass && ($providerClass instanceof SaleProviderBase))
788 $setReservedQuantity = 0;
789 if ($needShip ===
false)
791 if ($basketItem->isBundleParent())
795 $setReservedQuantity = $shipmentItem->getQuantity();
798 $shipmentItem->setFieldNoDemand(
'RESERVED_QUANTITY', $setReservedQuantity);
801 if ($needShip ===
false)
803 if (!Internals\ActionEntity::isTypeExists(
804 $order->getInternalId(),
809 Internals\ActionEntity::add(
810 $order->getInternalId(),
813 'METHOD' =>
'Bitrix\Sale\ShipmentCollection::updateReservedFlag',
814 'PARAMS' => array($shipment->getCollection())
829 public static function getProductAvailableQuantity(Basket $basketCollection, BasketItem $refreshItem =
null)
832 static $proxyProductAvailableQuantity = array();
833 $resultList = array();
836 if (($order = $basketCollection->getOrder()) !==
null)
841 $basketList = static::makeArrayFromBasketCollection($basketCollection, $refreshItem);
843 $basketProviderMap = static::createProviderBasketMap($basketList);
844 $basketProviderList = static::redistributeToProviders($basketProviderMap);
847 $productsList = array();
848 $providerList = array();
849 $basketCodeIndex = array();
850 if (!empty($basketProviderList))
852 foreach ($basketProviderList as $provider => $providerBasketItemList)
854 if (array_key_exists(
"IBXSaleProductProvider", class_implements($provider)))
856 foreach ($providerBasketItemList as $providerBasketItemData)
859 $proxyProductKey = $providerBasketItemData[
'PRODUCT_ID'].
"|".
$userId;
860 if (!empty($proxyProductAvailableQuantity[$proxyProductKey]) && is_array($proxyProductAvailableQuantity[$proxyProductKey]))
862 $resultProductData = $proxyProductAvailableQuantity[$proxyProductKey];
866 $resultProductData = $resultProductData = $provider::getProductAvailableQuantity($providerBasketItemData[
'PRODUCT_ID'], $userId);
867 $proxyProductAvailableQuantity[$proxyProductKey] = $resultProductData;
871 $basketCode = $providerBasketItemData[
'BASKET_ITEM']->getBasketCode();
872 $resultList[$basketCode] = $resultProductData;
875 elseif (class_exists($provider))
882 'USER_ID' => $order->getUserId(),
883 'SITE_ID' => $order->getSiteId(),
884 'CURRENCY' => $order->getCurrency(),
891 'USER_ID' => $USER->getId(),
892 'SITE_ID' => SITE_ID,
893 'CURRENCY' => Currency\CurrencyManager::getBaseCurrency(),
898 $providerClass =
new $provider($context);
899 if (!$providerClass instanceof SaleProviderBase)
905 foreach ($providerBasketItemList as $providerBasketItemData)
907 $basketItem = $providerBasketItemData[
'BASKET_ITEM'];
910 $basketCode = $basketItem->getBasketCode();
911 $basketCodeIndex[
$productId][] = $basketItem->getBasketCode();
913 $providerList[$provider] = $providerClass;
915 if (empty($productsList[$provider][$productId]))
917 $productsList[$provider][
$productId] = $providerBasketItemData;
920 $productsList[$provider][
$productId][
'QUANTITY_LIST'][$basketCode] = $basketItem->getQuantity();
921 $resultList[$basketCode] = 0;
926 foreach ($providerBasketItemList as $providerBasketItemData)
928 $resultProductData = \CSaleBasket::ExecuteCallbackFunction(
929 $providerBasketItemData[
'CALLBACK_FUNC'],
930 $providerBasketItemData[
'MODULE'],
931 $providerBasketItemData[
'PRODUCT_ID']
934 $basketCode = $providerBasketItemData[
'BASKET_ITEM']->getBasketCode();
935 $resultList[$basketCode] = $resultProductData;
941 if (!empty($productsList))
943 foreach ($productsList as $providerName => $products)
946 $providerClass = $providerList[$providerName];
948 $r = $providerClass->getAvailableQuantity($products);
951 $resultData = $r->getData();
952 if (!empty($resultData[
'AVAILABLE_QUANTITY_LIST']))
955 foreach ($resultData[
'AVAILABLE_QUANTITY_LIST'] as $productId => $availableQuantity)
957 if (!empty($basketCodeIndex[$productId]))
959 foreach ($basketCodeIndex[$productId] as $basketCode)
961 $resultList[$basketCode] = $availableQuantity;
985 $resultList = array();
992 if (($order = $basketCollection->
getOrder()) !==
null)
994 $userId = $order->getUserId();
995 $siteId = $order->getSiteId();
996 $currency = $order->getCurrency();
999 if ($siteId ===
null)
1001 $basket = $basketCollection->
getBasket();
1002 $siteId = $basket->getSiteId();
1005 if ($siteId ===
null)
1008 if ($currency ===
null)
1010 $currency = Internals\SiteCurrencyTable::getSiteCurrency($siteId);
1012 $currency = Currency\CurrencyManager::getBaseCurrency();
1016 "USER_ID" => $userId,
1017 "SITE_ID" => $siteId,
1018 "CURRENCY" => $currency,
1021 $basketList = static::makeArrayFromBasketCollection($basketCollection, $refreshItem);
1024 $basketProviderMap = static::createProviderBasketMap($basketList, array(
'QUANTITY',
'RENEWAL',
'SITE_ID',
'USER_ID'));
1025 foreach ($basketProviderMap as $basketProviderMapItem)
1027 $basketProviderList = static::redistributeToProviders([
1028 $basketProviderMapItem,
1031 if (!empty($basketProviderList))
1037 foreach ($basketProviderList as $providerClassName => $productValueList)
1039 $r = static::getProductDataByList($productValueList, $providerClassName, $select, $context, $options);
1040 if ($r->isSuccess())
1042 $resultData = $r->getData();
1043 if (!empty($resultData[
'PRODUCT_DATA_LIST']))
1045 $resultList = $resultData[
'PRODUCT_DATA_LIST'] + $resultList;
1066 public static function getProductDataByList(array $products, $providerClassName, array $select, array $context, array $options = array())
1070 $resultList = array();
1072 $needPrice = in_array(
'PRICE', $select);
1073 $needBasePrice = in_array(
'BASE_PRICE', $select);
1074 $needCoupons = in_array(
'COUPONS', $select);
1076 'USER_ID' => $context[
'USER_ID'],
1077 'SITE_ID' => $context[
'SITE_ID'],
1078 'CURRENCY' => $context[
'CURRENCY'],
1079 'CHECK_QUANTITY' => (in_array(
'QUANTITY', $select) ?
'Y' :
'N'),
1080 'AVAILABLE_QUANTITY' => (in_array(
'AVAILABLE_QUANTITY', $select) ?
'Y' :
'N'),
1081 'CHECK_PRICE' => ($needPrice ?
'Y' :
'N'),
1082 'CHECK_COUPONS' => ($needCoupons ?
'Y' :
'N'),
1083 'RENEWAL' => (in_array(
'RENEWAL', $select) ?
'Y' :
'N')
1087 $data[
'CHECK_DISCOUNT'] =
'N';
1089 $useOrderProduct =
false;
1091 $useOrderProduct =
true;
1094 $useOrderProduct =
false;
1096 $data[
'USE_ORDER_PRODUCT'] = $useOrderProduct;
1098 unset($needCoupons, $needPrice);
1101 if ($providerClassName)
1103 if (array_key_exists(
"IBXSaleProductProvider", class_implements($providerClassName)))
1105 $resultProductList = static::getProductProviderData($products, $providerClassName, $data, $select);
1106 if (in_array(
'RETURN_BASKET_ID', $options))
1108 $basketList = array();
1112 $basketList[] = $basketItem;
1115 $resultProductList = static::createItemsAfterGetProductData($basketList, $resultProductList, $select);
1118 elseif (class_exists($providerClassName))
1120 $basketList = array();
1126 $r = Internals\Catalog\Provider::getProductData($basketList, $context);
1127 if ($r->isSuccess())
1129 $resultProductData = $r->getData();
1130 if (!empty($resultProductData[
'PRODUCT_DATA_LIST']))
1132 $itemsList = $resultProductData[
'PRODUCT_DATA_LIST'];
1133 $resultItemsList = array();
1134 $resultProductList = array();
1136 foreach ($itemsList as $providerName => $products)
1138 $resultItemsList = static::createItemsAfterGetProductData($basketList, $products, $select);
1141 $resultProductList = $resultProductList + $resultItemsList;
1147 if (!empty($resultProductList))
1149 if (!empty($resultList) && is_array($resultList))
1151 $resultList = $resultList + $resultProductList;
1155 $resultList = $resultProductList;
1161 $priceFields = static::getPriceFields();
1165 $callbackFunction =
null;
1171 $quantityList = array();
1183 foreach($quantityList as $basketCode => $quantity)
1185 if (!empty($callbackFunction))
1187 $resultProductData = \CSaleBasket::executeCallbackFunction(
1196 $resultProductData = array(
1197 'QUANTITY' => $quantity,
1198 'AVAILABLE_QUANTITY' => $quantity,
1202 $itemCode = $productId;
1203 if (in_array(
'RETURN_BASKET_ID', $options))
1205 $itemCode = $basketCode;
1208 if (empty($resultList[$itemCode]))
1210 $resultList[$itemCode] = $resultProductData;
1213 if (!empty($resultProductData))
1215 $resultList[$itemCode][
'PRICE_LIST'][$basketCode] = array(
1216 'QUANTITY' => $resultProductData[
'QUANTITY'],
1217 'AVAILABLE_QUANTITY' => $resultProductData[
'AVAILABLE_QUANTITY'],
1218 "ITEM_CODE" => $productId,
1219 "BASKET_CODE" => $basketCode,
1222 foreach ($priceFields as $fieldName)
1224 if (isset($resultProductData[$fieldName]))
1226 $resultList[$itemCode][
'PRICE_LIST'][$basketCode][$fieldName] = $resultProductData[$fieldName];
1236 if (!empty($resultList))
1240 'PRODUCT_DATA_LIST' => $resultList
1256 private static function createItemsAfterGetProductData($basketList, array $productDataList, array $select = array())
1258 $resultList = array();
1259 $basketIndexList = array();
1260 $basketMap = array();
1262 if (!is_array($basketList) && !($basketList instanceof
BasketBase))
1268 foreach ($basketList as $basketItem)
1270 $basketCode = $basketItem->getBasketCode();
1271 $productId = $basketItem->getProductId();
1273 $basketIndexList[$productId][] = $basketCode;
1274 $basketMap[$basketCode] = $basketItem;
1277 if (empty($productDataList))
1282 foreach ($productDataList as $productId =>
$productData)
1284 if (empty($basketIndexList[$productId]))
1290 foreach ($basketIndexList[$productId] as $basketCode)
1296 if (array_key_exists(
'AVAILABLE_QUANTITY', $priceData)
1297 && !array_key_exists(
'QUANTITY', $priceData))
1299 $priceData[
'QUANTITY'] = $priceData[
'AVAILABLE_QUANTITY'];
1303 $basketItem = $basketMap[$basketCode];
1305 if (in_array(
'PRICE', $select) || $basketItem->getId() == 0)
1311 if (isset($priceData[
'QUANTITY']))
1316 if (isset($priceData[
'AVAILABLE_QUANTITY']))
1318 $productData[
'AVAILABLE_QUANTITY'] = $priceData[
'AVAILABLE_QUANTITY'];
1325 if (in_array(
'AVAILABLE_QUANTITY', $select) && isset(
$productData[
'AVAILABLE_QUANTITY']))
1346 public static function getProductProviderData(array $products, $provider, array $data, array $select = array())
1352 $productSelect = array_fill_keys($select,
true);
1355 $currentUseOrderProduct = $data[
'USE_ORDER_PRODUCT'];
1358 $currentUseOrderProduct =
false;
1365 $fields[
'CHECK_COUPONS'] =
'Y';
1369 $fields[
'CHECK_COUPONS'] =
'N';
1374 $fields[
'CHECK_DISCOUNT'] =
'N';
1375 $fields[
'CHECK_COUPONS'] =
'N';
1382 unset($productSelect[
'QUANTITY'], $productSelect[
'AVAILABLE_QUANTITY']);
1384 $fields[
'CHECK_QUANTITY'] =
'N';
1385 $fields[
'AVAILABLE_QUANTITY'] =
'N';
1388 $quantityList = array();
1407 if (intval($basketId) == 0)
1413 $basketId = $basketItem->getId();
1417 if (intval($basketId) > 0)
1419 $fields[
'BASKET_ID'] = $basketId;
1422 $hasTrustData =
false;
1425 $resultProductData = array();
1427 if (static::isReadTrustData() ===
true
1430 $hasTrustData =
true;
1433 foreach (static::getProductDataRequiredFields() as $requiredField)
1435 if (!array_key_exists($requiredField, $resultProductData))
1437 $hasTrustData =
false;
1443 if ($hasTrustData && isset($productSelect[
'PRICE']))
1445 foreach (static::getProductDataRequiredPriceFields() as $requiredField)
1447 if (!array_key_exists($requiredField, $resultProductData))
1449 $hasTrustData =
false;
1458 $resultProviderDataList = array();
1462 foreach($quantityList as $basketCode => $quantity)
1464 if (!empty($resultProviderDataList[$quantity]))
1466 $resultProviderDataList[$quantity][
'BASKET_CODE'][] = $basketCode;
1470 $requestFields = $fields;
1471 $requestFields[
'QUANTITY'] = $quantity;
1473 $resultProviderDataList[$quantity] = array(
1474 'BASKET_CODE' => array($basketCode),
1475 'DATA' => ($currentUseOrderProduct ? $provider::OrderProduct(
1477 ) : $provider::GetProductData($requestFields))
1486 if (!isset($productSelect[
'AVAILABLE_QUANTITY']) && array_key_exists(
"AVAILABLE_QUANTITY", $resultProductData))
1488 unset($resultProductData[
'AVAILABLE_QUANTITY']);
1491 $productQuantity = floatval($resultProductData[
'QUANTITY']);
1493 $resultProviderDataList[$productQuantity] = array(
1495 'DATA' => $resultProductData
1500 $priceFields = static::getPriceFields();
1502 foreach ($resultProviderDataList as $quantity => $providerData)
1504 if (empty($result[$itemCode]))
1506 $result[$itemCode] = $providerData[
'DATA'];
1509 $basketCodeList = $providerData[
'BASKET_CODE'];
1511 foreach ($basketCodeList as $basketCode)
1513 $result[$itemCode][
'PRICE_LIST'][$basketCode] = array(
1514 "ITEM_CODE" => $itemCode,
1515 "BASKET_CODE" => $basketCode,
1518 if (isset($providerData[
'DATA'][
'QUANTITY']) && $providerData[
'DATA'][
'QUANTITY'] > 0)
1520 $result[$itemCode][
'PRICE_LIST'][$basketCode][
'QUANTITY'] = $providerData[
'DATA'][
'QUANTITY'];
1523 if (isset($providerData[
'DATA'][
'AVAILABLE_QUANTITY']))
1525 $result[$itemCode][
'PRICE_LIST'][$basketCode][
'AVAILABLE_QUANTITY'] = $providerData[
'DATA'][
'AVAILABLE_QUANTITY'];
1529 foreach ($priceFields as $fieldName)
1531 if (isset($providerData[
'DATA'][$fieldName]))
1533 foreach ($basketCodeList as $basketCode)
1535 $result[$itemCode][
'PRICE_LIST'][$basketCode][$fieldName] = $providerData[
'DATA'][$fieldName];
1545 $result[$itemCode][
"BUNDLE_ITEMS"] = array();
1547 $bundleChildDataList = static::getBundleChildItemsByProductData($provider,
$productData);
1548 if (!empty($bundleChildDataList) && is_array($bundleChildDataList))
1552 foreach ($bundleChildDataList[
"ITEMS"] as &$itemData)
1554 $itemData[
'QUANTITY'] = $itemData[
'QUANTITY'] * $quantity;
1557 $result[$itemCode][
"BUNDLE_ITEMS"] = $bundleChildDataList[
"ITEMS"];
1572 public static function getCatalogData(array $basketProviderList, array $context, array $select = array())
1574 $needPrice = in_array(
'PRICE', $select);
1575 $needBasePrice = in_array(
'BASE_PRICE', $select);
1576 $needCoupons = in_array(
'COUPONS', $select);
1584 if (!empty($context[
'USER_ID']) && intval($context[
'USER_ID']) > 0)
1586 $userId = $context[
'USER_ID'];
1589 if (array_key_exists(
'SITE_ID', $context))
1591 $siteId = $context[
'SITE_ID'];
1594 if (array_key_exists(
'CURRENCY', $context))
1596 $currency = $context[
'CURRENCY'];
1600 'USER_ID' => $userId,
1601 'SITE_ID' => $siteId,
1602 'CURRENCY' => $currency,
1603 'CHECK_QUANTITY' => (in_array(
'QUANTITY', $select) ?
'Y' :
'N'),
1604 'AVAILABLE_QUANTITY' => (in_array(
'AVAILABLE_QUANTITY', $select) ?
'Y' :
'N'),
1605 'CHECK_PRICE' => ($needPrice ?
'Y' :
'N'),
1606 'CHECK_COUPONS' => ($needCoupons ?
'Y' :
'N'),
1607 'RENEWAL' => (in_array(
'RENEWAL', $select) ?
'Y' :
'N')
1611 $data[
'CHECK_DISCOUNT'] =
'N';
1613 $useOrderProduct =
false;
1615 $useOrderProduct =
true;
1618 $useOrderProduct =
false;
1620 unset($needCoupons, $needPrice);
1622 foreach ($basketProviderList as $provider => $providerBasketItemList)
1624 if ($provider && array_key_exists(
"IBXSaleProductProvider", class_implements($provider)))
1626 foreach ($providerBasketItemList as $providerBasketItem)
1628 $currentUseOrderProduct = $useOrderProduct;
1629 if (!isset($providerBasketItem[
'BASKET_ID']) || (
int)$providerBasketItem[
'BASKET_ID'] <= 0)
1630 $currentUseOrderProduct =
false;
1632 $providerFields = $data;
1634 if ($providerBasketItem[
'BASKET_ITEM']->isBundleChild())
1636 $providerFields[
'CHECK_DISCOUNT'] =
'N';
1639 if ($providerBasketItem[
'BASKET_ITEM']->getField(
"CAN_BUY") ==
"N"
1640 || $providerBasketItem[
'BASKET_ITEM']->getField(
"DELAY") ==
"Y"
1641 || $providerBasketItem[
'BASKET_ITEM']->getField(
"SUBSCRIBE") ==
"Y"
1644 $providerFields[
'CHECK_COUPONS'] =
'N';
1648 $providerFields[
'CHECK_COUPONS'] =
'Y';
1651 $providerFields[
'PRODUCT_ID'] = $providerBasketItem[
'PRODUCT_ID'];
1652 $providerFields[
'QUANTITY'] = $providerBasketItem[
'QUANTITY'];
1654 if (intval($providerBasketItem[
'BASKET_ID']) > 0)
1656 $providerFields[
'BASKET_ID'] = $providerBasketItem[
'BASKET_ID'];
1659 $hasTrustData =
false;
1661 $trustData = static::getTrustData($siteId, $providerBasketItem[
'MODULE'], $providerBasketItem[
'PRODUCT_ID']);
1663 if (static::isReadTrustData() ===
true
1666 $hasTrustData =
true;
1669 foreach (static::getProductDataRequiredFields() as $requiredField)
1671 if (!array_key_exists($requiredField, $resultProductData))
1673 $hasTrustData =
false;
1679 if ($hasTrustData && in_array(
'PRICE', $select))
1681 foreach (static::getProductDataRequiredPriceFields() as $requiredField)
1683 if (!array_key_exists($requiredField, $resultProductData))
1685 $hasTrustData =
false;
1695 $resultProductData = ($currentUseOrderProduct ? $provider::OrderProduct($providerFields) : $provider::GetProductData($providerFields));
1699 if (!in_array(
'AVAILABLE_QUANTITY', $select) && array_key_exists(
"AVAILABLE_QUANTITY", $resultProductData))
1701 unset($resultProductData[
'AVAILABLE_QUANTITY']);
1705 $basketCode = $providerBasketItem[
'BASKET_ITEM']->getBasketCode();
1706 $result[$basketCode] = $resultProductData;
1708 if ($providerBasketItem[
'BASKET_ITEM']->isBundleParent())
1711 $result[$basketCode][
"BUNDLE_ITEMS"] = array();
1713 $bundleChildDataList = static::getSetItems($providerBasketItem[
'BASKET_ITEM']);
1714 if (!empty($bundleChildDataList) && is_array($bundleChildDataList))
1716 $bundleChildList = reset($bundleChildDataList);
1718 foreach ($bundleChildList[
"ITEMS"] as &$itemData)
1720 $itemData[
'QUANTITY'] = $itemData[
'QUANTITY'] * $providerBasketItem[
'BASKET_ITEM']->getQuantity();
1723 $result[$basketCode][
"BUNDLE_ITEMS"] = $bundleChildList[
"ITEMS"];
1731 foreach ($providerBasketItemList as $providerBasketItem)
1733 $resultProductData = \CSaleBasket::executeCallbackFunction(
1734 $providerBasketItem[
'CALLBACK_FUNC'],
1735 $providerBasketItem[
'MODULE'],
1736 $providerBasketItem[
'PRODUCT_ID'],
1737 $providerBasketItem[
'QUANTITY']
1740 $basketCode = $providerBasketItem[
'BASKET_ITEM']->getBasketCode();
1741 $result[$basketCode] = $resultProductData;
1757 public static function tryShipment(Shipment $shipment)
1759 $result =
new Result();
1760 $needShip = $shipment->needShip();
1761 if ($needShip ===
null)
1764 $resultList = array();
1765 $storeData = array();
1768 $shipmentItemCollection = $shipment->getShipmentItemCollection();
1769 if (!$shipmentItemCollection)
1771 throw new ObjectNotFoundException(
'Entity "ShipmentItemCollection" not found');
1775 $shipment = $shipmentItemCollection->getShipment();
1778 throw new ObjectNotFoundException(
'Entity "ShipmentCollection" not found');
1782 $shipmentCollection = $shipment->getCollection();
1783 if (!$shipmentCollection)
1785 throw new ObjectNotFoundException(
'Entity "ShipmentCollection" not found');
1788 $r = static::tryShipmentItemList($shipmentItemCollection);
1790 $basketList = static::getBasketFromShipmentItemCollection($shipmentItemCollection);
1792 $bundleIndexList = static::getBundleIndexFromShipmentItemCollection($shipmentItemCollection);
1794 $basketCountList = static::getBasketCountFromShipmentItemCollection($shipmentItemCollection);
1796 $basketProviderMap = static::createProviderBasketMap($basketList, array(
'RESERVED',
'SITE_ID'));
1797 $basketProviderList = static::redistributeToProviders($basketProviderMap);
1802 $r = static::getStoreDataFromShipmentItemCollection($shipmentItemCollection);
1803 if ($r->isSuccess())
1805 $resultStoreData = $r->getData();
1806 if (!empty($resultStoreData[
'STORE_DATA_LIST']))
1808 $storeDataList = $resultStoreData[
'STORE_DATA_LIST'];
1813 $result->addErrors($r->getErrors());
1818 if (!empty($basketProviderList))
1820 foreach ($basketProviderList as $provider => $providerBasketItemList)
1822 if ($provider && array_key_exists(
"IBXSaleProductProvider", class_implements($provider)))
1824 foreach ($providerBasketItemList as $providerBasketItem)
1826 if ($providerBasketItem[
'BASKET_ITEM']->isBundleParent())
1831 $resultProduct =
new Result();
1835 $basketStoreData = array();
1837 $basketCode = $providerBasketItem[
'BASKET_CODE'];
1840 if (!$basketItem = $providerBasketItem[
'BASKET_ITEM'])
1842 throw new ObjectNotFoundException(
'Entity "BasketItem" not found');
1847 $quantity = $basketCountList[$basketCode];
1849 if (!empty($storeDataList) && is_array($storeDataList)
1850 && isset($storeDataList[$basketCode]))
1852 $basketStoreData = $storeDataList[$basketCode];
1855 if (!empty($basketStoreData))
1857 $allBarcodeQuantity = 0;
1858 foreach($basketStoreData as $basketShipmentItemStore)
1860 $allBarcodeQuantity += $basketShipmentItemStore[
'QUANTITY'];
1863 if ($quantity > $allBarcodeQuantity)
1865 $resultProduct->addError(
new ResultError(
Loc::getMessage(
'SALE_PROVIDER_SHIPMENT_SHIPPED_LESS_QUANTITY', array(
1866 '#PRODUCT_NAME#' => $basketItem->getField(
'NAME')
1867 )),
'SALE_PROVIDER_SHIPMENT_SHIPPED_LESS_QUANTITY'));
1869 elseif ($quantity < $allBarcodeQuantity)
1871 $resultProduct->addError(
new ResultError(
Loc::getMessage(
'SALE_PROVIDER_SHIPMENT_SHIPPED_MORE_QUANTITY', array(
1872 '#PRODUCT_NAME#' => $basketItem->getField(
'NAME')
1873 )),
'SALE_PROVIDER_SHIPMENT_SHIPPED_MORE_QUANTITY'));
1879 if ($resultProduct->isSuccess())
1882 if ($needShip ===
true)
1884 if (method_exists($provider,
'tryShipmentProduct'))
1887 $resultProduct = $provider::tryShipmentProduct($basketItem, $providerBasketItem[
'RESERVED'], $basketStoreData, $quantity);
1892 if (method_exists($provider,
'tryUnshipmentProduct'))
1895 $resultProduct = $provider::tryUnshipmentProduct($providerBasketItem[
'PRODUCT_ID']);
1900 $resultList[$basketCode] = $resultProduct;
1904 elseif (class_exists($provider))
1908 if (!$shipmentCollection = $shipment->getCollection())
1910 throw new ObjectNotFoundException(
'Entity "ShipmentCollection" not found');
1914 if (!$order = $shipmentCollection->getOrder())
1916 throw new ObjectNotFoundException(
'Entity "Order" not found');
1919 $pool = Internals\PoolQuantity::getInstance($order->getInternalId());
1922 'SITE_ID' => $order->getSiteId(),
1923 'CURRENCY' => $order->getCurrency(),
1926 if ($order->getUserId() > 0)
1928 $context[
'USER_ID'] = $order->getUserId();
1933 $context[
'USER_ID'] = $USER->getId();
1936 $creator = Internals\ProviderCreator::create($context);
1938 $tryShipProductList = array();
1940 foreach ($shipmentItemCollection as $shipmentItem)
1942 $basketItem = $shipmentItem->getBasketItem();
1943 $providerClass = $basketItem->getProviderEntity();
1945 if ($providerClass instanceof SaleProviderBase)
1947 $shipmentProductData = $creator->createItemForShip($shipmentItem);
1948 $creator->addProductData($shipmentProductData);
1952 $r = $creator->tryShip();
1953 if ($r->isSuccess())
1955 if ($r->hasWarnings())
1957 $result->addWarnings($r->getWarnings());
1961 $data = $r->getData();
1962 if (array_key_exists(
'TRY_SHIP_PRODUCTS_LIST', $data))
1964 $tryShipProductList = $data[
'TRY_SHIP_PRODUCTS_LIST'] + $tryShipProductList;
1966 $creator->setItemsResultAfterTryShip($pool, $tryShipProductList);
1973 $result->addWarnings($r->getErrors());
1979 if (!empty($resultList)
1980 && !empty($bundleIndexList) && is_array($bundleIndexList))
1983 foreach ($bundleIndexList as $bundleParentBasketCode => $bundleChildList)
1986 foreach($bundleChildList as $bundleChildBasketCode)
1988 if (!isset($resultList[$bundleChildBasketCode]))
1990 if (!isset($resultList[$bundleParentBasketCode]))
1992 $resultList[$bundleParentBasketCode] =
new Result();
1995 $resultList[$bundleParentBasketCode]->addError(
new ResultError(
'Bundle child item not found',
'SALE_PROVIDER_SHIPMENT_SHIPPED_BUNDLE_CHILD_ITEM_NOT_FOUND'));
2003 if (!empty($resultList))
2006 if (!$shipmentCollection = $shipment->getCollection())
2008 throw new ObjectNotFoundException(
'Entity "ShipmentCollection" not found');
2012 if (!$order = $shipmentCollection->getOrder())
2014 throw new ObjectNotFoundException(
'Entity "Order" not found');
2020 foreach ($shipmentItemCollection as $shipmentItem)
2023 if(!$basketItem = $shipmentItem->getBasketItem())
2025 throw new ObjectNotFoundException(
'Entity "BasketItem" not found');
2028 if (isset($resultList[$basketItem->getBasketCode()]) && !$resultList[$basketItem->getBasketCode()]->isSuccess())
2038 foreach ($shipmentItemCollection as $shipmentItem)
2041 if(!$basketItem = $shipmentItem->getBasketItem())
2043 throw new ObjectNotFoundException(
'Entity "BasketItem" not found');
2046 if (isset($resultList[$basketItem->getBasketCode()]) && $resultList[$basketItem->getBasketCode()]->isSuccess())
2048 static::addQuantityPoolItem($order->getInternalId(), $basketItem, ($needShip? -1 : 1) * $shipmentItem->getQuantity());
2051 $shipmentItem->setFieldNoDemand(
"RESERVED_QUANTITY", 0);
2057 $result->setData($resultList);
2069 public static function tryShipmentItemList($shipmentItemList)
2071 $result =
new Result();
2073 $resultList = array();
2074 $bundleIndexList = static::getBundleIndexFromShipmentItemCollection($shipmentItemList);
2079 $r = static::getStoreDataFromShipmentItemCollection($shipmentItemList);
2080 if ($r->isSuccess())
2082 $resultStoreData = $r->getData();
2083 if (!empty($resultStoreData[
'STORE_DATA_LIST']))
2085 $storeDataList = $resultStoreData[
'STORE_DATA_LIST'];
2090 $result->addErrors($r->getErrors());
2095 $shipmentItemParentsList = array();
2097 $tryShipProductList = array();
2100 foreach ($shipmentItemList as $shipmentItem)
2102 $itemIndex = $shipmentItem->getInternalIndex();
2103 $basketItem = $shipmentItem->getBasketItem();
2104 $providerName = $basketItem->getProviderName();
2107 $shipmentItemCollection = $shipmentItem->getCollection();
2108 if (!$shipmentItemCollection)
2110 throw new ObjectNotFoundException(
'Entity "ShipmentItemCollection" not found');
2114 $shipment = $shipmentItemCollection->getShipment();
2117 throw new ObjectNotFoundException(
'Entity "Shipment" not found');
2120 $shipmentItemParentsList[$itemIndex] = array(
2121 'BASKET_ITEM' => $basketItem,
2122 'SHIPMENT' => $shipment,
2123 'SHIPMENT_ITEM_COLLECTION' => $shipmentItemCollection,
2126 $needShip = $shipment->needShip();
2127 if ($needShip ===
null)
2131 if ($providerName && array_key_exists(
"IBXSaleProductProvider", class_implements($providerName)))
2133 $basketItem = $shipmentItem->getBasketItem();
2136 throw new ObjectNotFoundException(
'Entity "BasketItem" not found');
2139 if ($basketItem->isBundleParent())
2144 $basketCode = $basketItem->getBasketCode();
2145 $quantity = $shipmentItem->getQuantity();
2146 $basketStoreData = array();
2148 $resultProduct =
new Result();
2152 if (!empty($storeDataList) && is_array($storeDataList)
2153 && isset($storeDataList[$basketCode]))
2155 $basketStoreData = $storeDataList[$basketCode];
2158 if (!empty($basketStoreData))
2160 $allBarcodeQuantity = 0;
2161 foreach($basketStoreData as $basketShipmentItemStore)
2163 $allBarcodeQuantity += $basketShipmentItemStore[
'QUANTITY'];
2166 if ($quantity > $allBarcodeQuantity)
2168 $resultProduct->addError(
new ResultError(
Loc::getMessage(
'SALE_PROVIDER_SHIPMENT_SHIPPED_LESS_QUANTITY', array(
2169 '#PRODUCT_NAME#' => $basketItem->getField(
'NAME')
2170 )),
'SALE_PROVIDER_SHIPMENT_SHIPPED_LESS_QUANTITY'));
2172 elseif ($quantity < $allBarcodeQuantity)
2174 $resultProduct->addError(
new ResultError(
Loc::getMessage(
'SALE_PROVIDER_SHIPMENT_SHIPPED_MORE_QUANTITY', array(
2175 '#PRODUCT_NAME#' => $basketItem->getField(
'NAME')
2176 )),
'SALE_PROVIDER_SHIPMENT_SHIPPED_MORE_QUANTITY'));
2182 if ($resultProduct->isSuccess())
2185 if ($needShip ===
true)
2187 if (method_exists($providerName,
'tryShipmentProduct'))
2190 $resultProduct = $providerName::tryShipmentProduct($basketItem, $basketItem->getField(
'RESERVED'), $basketStoreData, $quantity);
2195 if (method_exists($providerName,
'tryUnshipmentProduct'))
2198 $resultProduct = $providerName::tryUnshipmentProduct($basketItem->getProductId());
2203 $resultList[$basketCode] = $resultProduct;
2206 elseif ($providerName && class_exists($providerName))
2209 $shipmentCollection = $shipment->getCollection();
2210 if (!$shipmentCollection)
2212 throw new ObjectNotFoundException(
'Entity "ShipmentCollection" not found');
2216 $order = $shipmentCollection->getOrder();
2219 throw new ObjectNotFoundException(
'Entity "Order" not found');
2222 $shipmentItemParentsList[$itemIndex][
'SHIPMENT_COLLECTION'] = $shipmentCollection;
2223 $shipmentItemParentsList[$itemIndex][
'ORDER'] = $order;
2225 $pool = Internals\PoolQuantity::getInstance($order->getInternalId());
2228 'SITE_ID' => $order->getSiteId(),
2229 'CURRENCY' => $order->getCurrency(),
2232 if ($order->getUserId() > 0)
2234 $context[
'USER_ID'] = $order->getUserId();
2239 $context[
'USER_ID'] = $USER->getId();
2242 $creator = Internals\ProviderCreator::create($context);
2244 $shipmentProductData = $creator->createItemForShip($shipmentItem);
2245 $creator->addProductData($shipmentProductData);
2247 $r = $creator->tryShip();
2248 if ($r->isSuccess())
2250 if ($r->hasWarnings())
2252 $result->addWarnings($r->getWarnings());
2256 $data = $r->getData();
2257 if (array_key_exists(
'TRY_SHIP_PRODUCTS_LIST', $data))
2259 $tryShipProductList = $data[
'TRY_SHIP_PRODUCTS_LIST'] + $tryShipProductList;
2260 $creator->setItemsResultAfterTryShip($pool, $tryShipProductList);
2266 $result->addWarnings($r->getErrors());
2271 if (!empty($resultList)
2272 && !empty($bundleIndexList) && is_array($bundleIndexList))
2275 foreach ($bundleIndexList as $bundleParentBasketCode => $bundleChildList)
2277 foreach($bundleChildList as $bundleChildBasketCode)
2279 if (!isset($resultList[$bundleChildBasketCode]))
2281 if (!isset($resultList[$bundleParentBasketCode]))
2283 $resultList[$bundleParentBasketCode] =
new Result();
2286 $resultList[$bundleParentBasketCode]->addError(
new ResultError(
'Bundle child item not found',
'SALE_PROVIDER_SHIPMENT_SHIPPED_BUNDLE_CHILD_ITEM_NOT_FOUND'));
2294 if (!empty($resultList))
2300 foreach ($shipmentItemList as $shipmentItem)
2302 $itemIndex = $shipmentItem->getInternalIndex();
2305 $basketItem = $shipmentItemParentsList[$itemIndex][
'BASKET_ITEM'];
2307 if (isset($resultList[$basketItem->getBasketCode()]) && !$resultList[$basketItem->getBasketCode()]->isSuccess())
2317 foreach ($shipmentItemList as $shipmentItem)
2319 $itemIndex = $shipmentItem->getInternalIndex();
2322 $basketItem = $shipmentItemParentsList[$itemIndex][
'BASKET_ITEM'];
2326 if (isset($resultList[$basketItem->getBasketCode()]) && $resultList[$basketItem->getBasketCode()]->isSuccess())
2329 $shipment = $shipmentItemParentsList[$itemIndex][
'SHIPMENT'];
2332 $order = $shipmentItemParentsList[$itemIndex][
'ORDER'];
2337 $shipmentCollection = $shipment->getCollection();
2338 if (!$shipmentCollection)
2340 throw new ObjectNotFoundException(
'Entity "ShipmentCollection" not found');
2344 $order = $shipmentCollection->getOrder();
2347 throw new ObjectNotFoundException(
'Entity "Order" not found');
2350 $shipmentItemParentsList[$itemIndex][
'SHIPMENT_COLLECTION'] = $shipmentCollection;
2351 $shipmentItemParentsList[$itemIndex][
'ORDER'] = $order;
2354 $needShip = $shipment->needShip();
2356 static::addQuantityPoolItem($order->getInternalId(), $basketItem, ($needShip? -1 : 1) * $shipmentItem->getQuantity());
2360 $shipmentItem->setFieldNoDemand(
"RESERVED_QUANTITY", 0);
2365 $poolItems = Internals\ItemsPool::get($order->getInternalId(), $productId);
2366 if (!empty($poolItems))
2369 foreach ($poolItems as $poolItem)
2371 if ($poolItem->getInternalIndex() == $shipmentItem->getInternalIndex())
2381 Internals\ItemsPool::add($order->getInternalId(), $productId, $shipmentItem);
2388 $result->setData($resultList);
2400 protected static function getBundleIndexFromShipmentItemCollection($shipmentItemList)
2402 $bundleIndexList = array();
2404 foreach ($shipmentItemList as $shipmentItem)
2407 if (!$basketItem = $shipmentItem->getBasketItem())
2413 if ($basketItem->isBundleChild())
2416 $parentBasketItem = $basketItem->getParentBasketItem();
2417 $parentBasketCode = $parentBasketItem->getBasketCode();
2419 if (!array_key_exists($parentBasketCode, $bundleIndexList))
2421 $bundleIndexList[$parentBasketCode] = array();
2424 $bundleIndexList[$parentBasketCode][] = $basketItem->getBasketCode();
2428 return $bundleIndexList;
2437 protected static function getBasketFromShipmentItemCollection($shipmentItemList)
2440 $basketList = array();
2442 foreach ($shipmentItemList as $shipmentItem)
2446 if (!$basketItem = $shipmentItem->getBasketItem())
2452 $shipmentItemCollection = $shipmentItem->getCollection();
2453 if (!$shipmentItemCollection)
2455 throw new ObjectNotFoundException(
'Entity "ShipmentItemCollection" not found');
2459 $shipment = $shipmentItemCollection->getShipment();
2463 throw new ObjectNotFoundException(
'Entity "Shipment" not found');
2466 $needShip = $shipment->needShip();
2467 if ($needShip ===
null)
2472 $reserved = ((($shipmentItem->getQuantity() - $shipmentItem->getReservedQuantity()) == 0)
2473 || ($shipment->getField(
'RESERVED') ==
"Y"));
2475 if ($basketItem->isBundleParent()
2476 || (!$basketItem->isBundleParent() && !$basketItem->isBundleChild()))
2479 $basketList[$basketItem->getBasketCode()] = array(
2480 'BASKET_ITEM' => $basketItem,
2481 'RESERVED' => ($reserved ?
"Y" :
"N"),
2482 'NEED_SHIP' => $needShip,
2483 'SHIPMENT_ITEM' => $shipmentItem
2487 if($basketItem->isBundleParent())
2490 foreach ($shipmentItemCollection as $bundleShipmentItem)
2493 $bundleBasketItem = $bundleShipmentItem->getBasketItem();
2495 if($bundleBasketItem->isBundleChild())
2497 $bundleParentBasketItem = $bundleBasketItem->getParentBasketItem();
2498 if ($bundleParentBasketItem->getBasketCode() == $basketItem->getBasketCode())
2501 $basketList[$bundleBasketItem->getBasketCode()] = array(
2502 'BASKET_ITEM' => $bundleBasketItem,
2503 'RESERVED' => ($reserved ?
"Y" :
"N"),
2504 'NEED_SHIP' => $needShip,
2505 'SHIPMENT_ITEM' => $shipmentItem
2524 protected static function getBasketCountFromShipmentItemCollection($shipmentItemList)
2527 $basketCountList = array();
2529 foreach ($shipmentItemList as $shipmentItem)
2533 if (!$basketItem = $shipmentItem->getBasketItem())
2538 if ($basketItem->isBundleParent()
2539 || (!$basketItem->isBundleParent() && !$basketItem->isBundleChild()))
2541 $basketCountList[$basketItem->getBasketCode()] = floatval($shipmentItem->getQuantity());
2545 if($basketItem->isBundleParent())
2548 foreach ($shipmentItemList as $bundleShipmentItem)
2551 $bundleBasketItem = $bundleShipmentItem->getBasketItem();
2553 if($bundleBasketItem->isBundleChild())
2555 $bundleParentBasketItem = $bundleBasketItem->getParentBasketItem();
2556 if ($bundleParentBasketItem->getBasketCode() == $basketItem->getBasketCode())
2558 $basketCountList[$bundleBasketItem->getBasketCode()] = floatval($bundleShipmentItem->getQuantity());
2566 return $basketCountList;
2577 $list = Internals\Catalog\Provider::createMapShipmentItemCollectionStoreData($shipmentItemList);
2580 $result->setData(array(
2581 'STORE_DATA_LIST' => $list
2595 $basketList = array();
2597 foreach ($basketCollection as $basketItem)
2599 if ($refreshItem !==
null)
2602 if ($basketItem->getBasketCode() != $refreshItem->getBasketCode() && $basketItem->isBundleParent())
2604 if ($bundleCollection = $basketItem->getBundleCollection())
2608 foreach ($bundleCollection as $bundleBasketItem)
2610 if ($bundleBasketItem->getBasketCode() == $refreshItem->getBasketCode())
2620 $basketList[] = $bundleBasketItem;
2624 elseif ($basketItem->getBasketCode() != $refreshItem->getBasketCode())
2629 $basketList[] = $basketItem;
2634 $basketList[] = $basketItem;
2647 public static function tryReserveShipment(Shipment $shipment)
2649 $result =
new Result();
2652 $shipmentItemCollection = $shipment->getShipmentItemCollection();
2654 $shipmentItemList = $shipmentItemCollection->getShippableItems();
2656 foreach ($shipmentItemList as $shipmentItem)
2661 $r = static::tryReserveShipmentItem($shipmentItem);
2662 if (!$r->isSuccess())
2664 $result->addErrors($r->getErrors());
2666 elseif ($r->hasWarnings())
2668 $result->addWarnings($r->getWarnings());
2671 catch(\Exception $e)
2674 if (!$shipment = $shipmentItemCollection->getShipment())
2676 throw new ObjectNotFoundException(
'Entity "Shipment" not found');
2696 public static function tryUnreserveShipment(Shipment $shipment)
2698 $result =
new Result();
2700 if (!$shipmentItemCollection = $shipment->getShipmentItemCollection())
2702 throw new ObjectNotFoundException(
'Entity "ShipmentItemCollection" not found');
2706 if (!($shipmentCollection = $shipment->getCollection()))
2708 throw new ObjectNotFoundException(
'Entity "ShipmentCollection" not found');
2712 if (!($order = $shipmentCollection->getOrder()))
2714 throw new ObjectNotFoundException(
'Entity "Order" not found');
2718 foreach ($shipmentItemCollection as $shipmentItem)
2721 $r = static::tryUnreserveShipmentItem($shipmentItem);
2722 if (!$r->isSuccess())
2724 $result->addErrors($r->getErrors());
2725 EntityMarker::addMarker($order, $shipment, $r);
2726 if (!$shipment->isSystem())
2728 $shipment->setField(
'MARKED',
'Y');
2731 elseif ($r->hasWarnings())
2733 $result->addWarnings($r->getWarnings());
2747 public static function tryReserveShipmentItem(ShipmentItem $shipmentItem)
2749 $result =
new Result();
2751 if (floatval($shipmentItem->getQuantity()) == floatval($shipmentItem->getReservedQuantity()))
2757 if (!$shipmentItemCollection = $shipmentItem->getCollection())
2759 throw new ObjectNotFoundException(
'Entity "ShipmentItemCollection" not found');
2763 if (!$shipment = $shipmentItemCollection->getShipment())
2765 throw new ObjectNotFoundException(
'Entity "Shipment" not found');
2768 if (!$shipmentCollection = $shipment->getCollection())
2770 throw new ObjectNotFoundException(
'Entity "ShipmentCollection" not found');
2774 if (!$order = $shipmentCollection->getOrder())
2776 throw new ObjectNotFoundException(
'Entity "Order" not found');
2780 if (!$basketItem = $shipmentItem->getBasketItem())
2782 $result->addError(
new ResultError(
2784 'SALE_PROVIDER_BASKET_ITEM_NOT_FOUND',
2786 '#BASKET_ITEM_ID#' => $shipmentItem->getBasketId(),
2787 '#SHIPMENT_ID#' => $shipment->getId(),
2788 '#SHIPMENT_ITEM_ID#' => $shipmentItem->getId(),
2791 'PROVIDER_RESERVE_SHIPMENT_ITEM_WRONG_BASKET_ITEM')
2797 if ($basketItem->isBundleParent())
2802 $needQuantity = ($shipmentItem->getQuantity() - $shipmentItem->getReservedQuantity());
2803 $canReserve =
false;
2805 $providerName = $basketItem->getProvider();
2807 if (class_exists($providerName))
2809 if (empty($context))
2814 'USER_ID' => $order->getUserId(),
2815 'SITE_ID' => $order->getSiteId(),
2816 'CURRENCY' => $order->getCurrency(),
2823 'USER_ID' => $USER->getId(),
2824 'SITE_ID' => SITE_ID,
2825 'CURRENCY' => Currency\CurrencyManager::getBaseCurrency(),
2830 $availableQuantityData = array();
2832 $providerClass =
new $providerName($context);
2833 if ($providerClass instanceof SaleProviderBase)
2835 $creator = Internals\ProviderCreator::create($context);
2836 $shipmentProductData = $creator->createItemForReserveByShipmentItem($shipmentItem);
2837 $creator->addProductData($shipmentProductData);
2839 $r = $creator->getAvailableQuantity();
2840 if ($r->isSuccess())
2842 $resultData = $r->getData();
2843 if (!empty($resultData[
'AVAILABLE_QUANTITY_LIST']))
2847 $resultAvailableQuantityList = $resultData[
'AVAILABLE_QUANTITY_LIST'];
2848 if (mb_substr($providerName, 0, 1) ==
"\\")
2850 $providerName = mb_substr($providerName, 1);
2853 if (isset($resultAvailableQuantityList[$providerName]) && isset($resultAvailableQuantityList[$providerName][$productId]))
2855 $availableQuantityData = array(
2856 'HAS_PROVIDER' =>
true,
2857 'AVAILABLE_QUANTITY' => $resultAvailableQuantityList[$providerName][$productId]
2865 $result->addErrors($r->getErrors());
2872 $r = static::tryReserveBasketItem($basketItem, $needQuantity);
2874 $availableQuantityData = $r->getData();
2880 $r = static::tryReserveBasketItem($basketItem, $needQuantity);
2882 $availableQuantityData = $r->getData();
2885 if (!$r->isSuccess())
2887 $result->addErrors($r->getErrors());
2890 elseif ($r->hasWarnings())
2892 $result->addWarnings($r->getWarnings());
2896 if (array_key_exists(
'AVAILABLE_QUANTITY', $availableQuantityData))
2898 $availableQuantity = $availableQuantityData[
'AVAILABLE_QUANTITY'];
2902 $result->addWarning(
new ResultWarning(
Loc::getMessage(
'SALE_PROVIDER_RESERVE_SHIPMENT_ITEM_WRONG_AVAILABLE_QUANTITY', array(
2903 '#PRODUCT_NAME#' => $basketItem->getField(
'NAME')
2904 )),
'SALE_PROVIDER_RESERVE_SHIPMENT_ITEM_WRONG_AVAILABLE_QUANTITY') );
2908 if (array_key_exists(
'HAS_PROVIDER', $availableQuantityData))
2910 $canReserve = $availableQuantityData[
'HAS_PROVIDER'];
2913 if ($canReserve && array_key_exists(
'QUANTITY_TRACE', $availableQuantityData))
2915 $canReserve = $availableQuantityData[
'QUANTITY_TRACE'];
2920 if ($r->isSuccess() && ($needQuantity > 0) && ($needQuantity > $availableQuantity)
2923 $result->addWarning(
new ResultWarning(
Loc::getMessage(
"SALE_PROVIDER_RESERVE_SHIPMENT_ITEM_QUANTITY_NOT_ENOUGH", array(
2924 '#PRODUCT_NAME#' => $basketItem->getField(
'NAME')
2925 )),
"SALE_PROVIDER_RESERVE_SHIPMENT_ITEM_QUANTITY_NOT_ENOUGH"));
2930 if (($availableQuantity < 0) && ($shipmentItem->getReservedQuantity() + $availableQuantity < 0))
2932 $availableQuantity = -1 * $shipmentItem->getReservedQuantity();
2938 $reservedQuantity = ($availableQuantity >= $needQuantity ? $needQuantity : $availableQuantity);
2940 static::addReservationPoolItem($order->getInternalId(), $shipmentItem->getBasketItem(), $reservedQuantity);
2942 $r = $shipmentItem->setField(
'RESERVED_QUANTITY', $shipmentItem->getReservedQuantity() + $reservedQuantity);
2943 if (!$r->isSuccess())
2945 $result->addErrors($r->getErrors());
2951 'CAN_RESERVE' => $canReserve,
2967 public static function tryUnreserveShipmentItem(ShipmentItem $shipmentItem)
2969 $result =
new Result();
2972 if (!$shipmentItemCollection = $shipmentItem->getCollection())
2974 throw new ObjectNotFoundException(
'Entity "ShipmentItemCollection" not found');
2978 if (!$shipment = $shipmentItemCollection->getShipment())
2980 throw new ObjectNotFoundException(
'Entity "Shipment" not found');
2984 if (!$shipmentCollection = $shipment->getCollection())
2986 throw new ObjectNotFoundException(
'Entity "ShipmentCollection" not found');
2990 if (!$order = $shipmentCollection->getOrder())
2992 throw new ObjectNotFoundException(
'Entity "Order" not found');
2996 if (!$basketItem = $shipmentItem->getBasketItem())
2998 $result->addError(
new ResultError(
3000 'SALE_PROVIDER_BASKET_ITEM_NOT_FOUND',
3002 '#BASKET_ITEM_ID#' => $shipmentItem->getBasketId(),
3003 '#SHIPMENT_ID#' => $shipment->getId(),
3004 '#SHIPMENT_ITEM_ID#' => $shipmentItem->getId(),
3007 'PROVIDER_TRY_UNRESERVED_SHIPMENT_ITEM_WRONG_BASKET_ITEM')
3013 if ($basketItem->isBundleParent())
3018 $quantity = $shipmentItem->getReservedQuantity();
3020 $canReserve =
false;
3022 $providerName = $basketItem->getProvider();
3024 $providerExists =
false;
3025 $availableQuantityData = array(
3026 'HAS_PROVIDER' =>
true,
3027 'AVAILABLE_QUANTITY' => $quantity
3030 if (class_exists($providerName))
3032 $providerClass =
new $providerName();
3033 if ($providerClass instanceof SaleProviderBase)
3035 $providerExists =
true;
3039 if (!$providerExists)
3041 if (!array_key_exists(
"IBXSaleProductProvider", class_implements($providerName)))
3043 $availableQuantityData[
'HAS_PROVIDER'] =
false;
3047 if (array_key_exists(
'HAS_PROVIDER', $availableQuantityData))
3049 $canReserve = $availableQuantityData[
'HAS_PROVIDER'];
3055 static::addReservationPoolItem($order->getInternalId(), $shipmentItem->getBasketItem(), $quantity);
3057 $reservedQuantity = ($shipmentItem->getReservedQuantity() > 0 ? $shipmentItem->getReservedQuantity() + $quantity : 0);
3059 $needShip = $shipment->needShip();
3062 $shipmentItem->setFieldNoDemand(
'RESERVED_QUANTITY', $reservedQuantity);
3066 $r = $shipmentItem->setField(
'RESERVED_QUANTITY', $reservedQuantity);
3067 if (!$r->isSuccess())
3069 $result->addErrors($r->getErrors());
3075 'CAN_RESERVE' => $canReserve,
3087 protected static function tryReserveBasketItem(BasketItem $basketItem, $quantity)
3089 $result =
new Result();
3091 $provider = $basketItem->getProvider();
3093 if (!$basketItem->isBundleChild())
3096 $basket = $basketItem->getCollection();
3101 $parentBasketItem = $basketItem->getParentBasketItem();
3104 $basket = $parentBasketItem->getCollection();
3107 $order = $basket->getOrder();
3108 $hasProvider =
false;
3109 $quantityTrace =
null;
3111 $poolQuantity = static::getReservationPoolItem($order->getInternalId(), $basketItem);
3112 $tryQuantity = $quantity + $poolQuantity;
3114 if ($provider && array_key_exists(
"IBXSaleProductProvider", class_implements($provider)))
3116 $hasProvider =
true;
3117 $r = static::checkAvailableProductQuantity($basketItem, $tryQuantity);
3119 $availableQuantityData = $r->getData();
3120 if (array_key_exists(
'AVAILABLE_QUANTITY', $availableQuantityData))
3122 $availableQuantity = floatval($availableQuantityData[
'AVAILABLE_QUANTITY']);
3126 $result->addWarning(
new ResultWarning(
Loc::getMessage(
'SALE_PROVIDER_BASKET_ITEM_WRONG_AVAILABLE_QUANTITY', array(
3127 '#PRODUCT_NAME#' => $basketItem->getField(
'NAME')
3128 )),
'PROVIDER_BASKET_ITEM_WRONG_AVAILABLE_QUANTITY'));
3132 if (array_key_exists(
'QUANTITY_TRACE', $availableQuantityData))
3134 $quantityTrace = $availableQuantityData[
'QUANTITY_TRACE'];
3137 if (!$r->isSuccess())
3139 $result->addErrors($r->getErrors());
3141 elseif ($r->hasWarnings())
3143 $result->addWarnings($r->getWarnings());
3146 $availableQuantity -= floatval($poolQuantity);
3150 $availableQuantity = $quantity;
3154 'AVAILABLE_QUANTITY' => $availableQuantity,
3155 'HAS_PROVIDER' => $hasProvider,
3158 if ($quantityTrace !==
null)
3160 $fields[
'QUANTITY_TRACE'] = $quantityTrace;
3163 $result->setData($fields);
3175 private static function reserveBasketItem(BasketItem $basketItem, $quantity)
3177 $result =
new Result();
3179 $provider = $basketItem->getProvider();
3182 if (!$basket = $basketItem->getCollection())
3184 throw new ObjectNotFoundException(
'Entity "Basket" not found');
3188 if (!$order = $basket->getOrder())
3190 throw new ObjectNotFoundException(
'Entity "Order" not found');
3193 $r = static::reserveProduct($provider, $basketItem->getProductId(), $quantity);
3195 if ($r->hasWarnings() || !$r->isSuccess())
3197 if (!$r->isSuccess())
3199 $result->addWarnings($r->getErrors());
3202 if ($r->hasWarnings())
3204 $result->addWarnings($r->getWarnings());
3208 if (!$basket = $basketItem->getCollection())
3210 throw new ObjectNotFoundException(
'Entity "Basket" not found');
3213 if ($order = $basket->getOrder())
3216 if (!$shipmentCollection = $order->getShipmentCollection())
3218 throw new ObjectNotFoundException(
'Entity "ShipmentCollection" not found');
3222 foreach ($shipmentCollection as $shipment)
3225 if (!$shipmentItemCollection = $shipment->getShipmentItemCollection())
3227 throw new ObjectNotFoundException(
'Entity "ShipmentItemCollection" not found');
3230 if($shipmentItemCollection->getItemByBasketCode($basketItem->getBasketCode()))
3232 EntityMarker::addMarker($order, $shipment, $result);
3233 if (!$shipment->isSystem())
3235 $shipment->setField(
'MARKED',
'Y');
3255 global $APPLICATION;
3260 if ($provider && array_key_exists(
"IBXSaleProductProvider", class_implements($provider)))
3262 $hasProvider =
true;
3263 $data = array(
"PRODUCT_ID" => $productId);
3267 $data[
"UNDO_RESERVATION"] =
"N";
3268 $data[
"QUANTITY_ADD"] = $quantity;
3272 $data[
"UNDO_RESERVATION"] =
"Y";
3273 $data[
"QUANTITY_ADD"] = abs($quantity);
3276 $APPLICATION->ResetException();
3277 if (($resultReserveData = $provider::ReserveProduct($data)))
3279 if ($resultReserveData[
'RESULT'])
3281 $fields[
'QUANTITY'] = $resultReserveData[
'QUANTITY_RESERVED'];
3285 $fields[
'QUANTITY'] = $quantity;
3288 $fields[
'HAS_PROVIDER'] = $hasProvider;
3289 $result->setData($fields);
3290 $exception = $APPLICATION->GetException();
3293 $result->addWarning(
new ResultWarning($exception->GetString(), $exception->GetID()));
3299 $exception = $APPLICATION->GetException();
3302 $result->addWarning(
new ResultWarning($exception->GetString(), $exception->GetID()));
3306 $result->addError(
new ResultError(
Loc::getMessage(
'SALE_PROVIDER_RESERVE_BASKET_ITEM_ERROR'),
'SALE_PROVIDER_RESERVE_BASKET_ITEM_ERROR')) ;
3313 $result->addError(
new ResultError(
Loc::getMessage(
'SALE_PROVIDER_RESERVE_BASKET_ITEM_ERROR'),
'SALE_PROVIDER_RESERVE_BASKET_ITEM_ERROR')) ;
3319 $fields[
'QUANTITY'] = $quantity;
3320 $result->setData($fields);
3334 public static function reserveShipmentItem(
ShipmentItem $shipmentItem, $quantity)
3336 global $APPLICATION;
3342 if (!$shipmentItemCollection)
3348 $shipment = $shipmentItemCollection->getShipment();
3351 throw new ObjectNotFoundException(
'Entity "Shipment" not found');
3358 $result->addError(
new ResultError(
3360 'SALE_PROVIDER_BASKET_ITEM_NOT_FOUND',
3362 '#BASKET_ITEM_ID#' => $shipmentItem->
getBasketId(),
3363 '#SHIPMENT_ID#' => $shipment->getId(),
3364 '#SHIPMENT_ITEM_ID#' => $shipmentItem->getId(),
3367 'PROVIDER_RESERVE_SHIPMENT_ITEM_WRONG_BASKET_ITEM')
3373 $provider = $basketItem->getProvider();
3375 if ($provider && array_key_exists(
"IBXSaleProductProvider", class_implements($provider)))
3378 "PRODUCT_ID" => $basketItem->getProductId(),
3379 "UNDO_RESERVATION" =>
"N",
3380 "QUANTITY_ADD" => $quantity,
3381 "ORDER_DEDUCTED" => $shipment->isShipped()?
"Y" :
"N",
3384 $APPLICATION->ResetException();
3385 if (($resultReserveData = $provider::ReserveProduct($data)))
3387 if ($resultReserveData[
'RESULT'])
3389 $fields[
'QUANTITY'] = $resultReserveData[
'QUANTITY_RESERVED'];
3391 if (isset($resultReserveData[
'QUANTITY_NOT_RESERVED']) && floatval($resultReserveData[
'QUANTITY_NOT_RESERVED']) > 0)
3396 $result->setData($fields);
3401 if ($ex = $APPLICATION->GetException())
3403 if ($ex->GetID() !=
"ALREADY_FLAG")
3404 $result->addError(
new ResultError($ex->GetString())) ;
3408 $result->addError(
new ResultError(
Loc::getMessage(
'SALE_PROVIDER_RESERVE_BASKET_ITEM_ERROR'),
'SALE_PROVIDER_RESERVE_BASKET_ITEM_ERROR')) ;
3415 elseif (class_exists($provider))
3418 $shipmentCollection = $shipment->getCollection();
3419 if (!$shipmentCollection)
3421 throw new ObjectNotFoundException(
'Entity "ShipmentCollection" not found');
3425 $order = $shipmentCollection->getOrder();
3428 throw new ObjectNotFoundException(
'Entity "Order" not found');
3432 'SITE_ID' => $order->getSiteId(),
3433 'CURRENCY' => $order->getCurrency(),
3436 if ($order->getUserId() > 0)
3438 $context[
'USER_ID'] = $order->getUserId();
3443 $context[
'USER_ID'] = $USER->getId();
3447 $providerClass =
new $provider($context);
3448 if ($providerClass && $providerClass instanceof SaleProviderBase)
3451 $creator = Internals\ProviderCreator::create($context);
3452 $creator->addShipmentItem($shipmentItem);
3454 $r = $creator->reserve();
3455 if (!$r->isSuccess())
3457 $result->addErrors($r->getErrors());
3463 if (!empty($fields))
3465 $result->setData($fields);
3477 public static function reduceProductQuantity(ShipmentCollection $shipmentCollection, array $shipmentReserveList = array())
3480 if (!$order = $shipmentCollection->getOrder())
3482 throw new ObjectNotFoundException(
'Entity "Order" not found');
3486 'ORDER_DEDUCTED' => $order->isShipped()
3489 $shipmentReserveListKeys = array_keys($shipmentReserveList);
3491 foreach ($shipmentCollection as $shipmentKey => $shipment)
3493 if (!in_array($shipment->getId(), $shipmentReserveListKeys))
3495 unset($shipmentCollection[$shipmentKey]);
3500 foreach ($shipmentCollection as $shipment)
3502 $basketProviderList = static::getProviderBasketFromShipment($shipment);
3504 $productList = static::getProductListFromBasketProviderList($basketProviderList);
3506 if (!empty($basketProviderList))
3508 foreach ($basketProviderList as $provider => $providerBasketItemList)
3510 $shipmentReserveListData = array();
3511 if (!empty($shipmentReserveList)
3512 && !empty($shipmentReserveList[$shipment->getId()]) && is_array($shipmentReserveList[$shipment->getId()]))
3514 $shipmentReserveListData = $shipmentReserveList[$shipment->getId()];
3517 $result = $provider::reduceProductQuantity($providerBasketItemList, $productList, $shipmentReserveListData, $options);
3531 public static function increaseProductQuantity(ShipmentCollection $shipmentCollection, array $shipmentReserveList = array())
3534 if (!$order = $shipmentCollection->getOrder())
3536 throw new ObjectNotFoundException(
'Entity "Order" not found');
3540 'ORDER_DEDUCTED' => $order->isShipped()
3543 $shipmentReserveListKeys = array_keys($shipmentReserveList);
3545 foreach ($shipmentCollection as $shipmentKey => $shipment)
3547 if (!in_array($shipment->getId(), $shipmentReserveListKeys))
3549 unset($shipmentCollection[$shipmentKey]);
3554 foreach ($shipmentCollection as $shipment)
3556 $basketProviderList = static::getProviderBasketFromShipment($shipment);
3558 $productList = static::getProductListFromBasketProviderList($basketProviderList);
3560 if (!empty($basketProviderList))
3562 foreach ($basketProviderList as $provider => $providerBasketItemList)
3564 $shipmentReserveListData = array();
3565 if (!empty($shipmentReserveList)
3566 && !empty($shipmentReserveList[$shipment->getId()]) && is_array($shipmentReserveList[$shipment->getId()]))
3568 $shipmentReserveListData = $shipmentReserveList[$shipment->getId()];
3571 $result = $provider::increaseProductQuantity($providerBasketItemList, $productList, $shipmentReserveListData, $options);
3584 public static function getProductStores(BasketItem $basketItem)
3586 $result =
new Result();
3588 $basketItemProviderMap = static::createProviderBasketItemMap($basketItem, array(
'SITE_ID'));
3590 if (!empty($basketItemProviderMap))
3592 $provider = $basketItemProviderMap[
'PROVIDER'];
3594 if (array_key_exists(
"IBXSaleProductProvider", class_implements($provider)))
3596 $productId = $basketItemProviderMap[
"PRODUCT_ID"];
3598 "PRODUCT_ID" => $productId,
3599 "SITE_ID" => $basketItemProviderMap[
"SITE_ID"],
3600 'BASKET_ID' => $basketItemProviderMap[
'BASKET_ID']
3603 $r = static::getStores($provider, $data);
3604 if ($r->isSuccess())
3606 $resultProductData = $r->getData();
3607 if (array_key_exists($productId, $resultProductData))
3609 $result->setData($resultProductData);
3614 elseif (class_exists($provider))
3617 $basket = $basketItem->getCollection();
3620 throw new ObjectNotFoundException(
'Entity "Basket" not found');
3624 $order = $basket->getOrder();
3627 throw new ObjectNotFoundException(
'Entity "Order" not found');
3631 'SITE_ID' => $order->getSiteId(),
3632 'CURRENCY' => $order->getCurrency(),
3635 if ($order->getUserId() > 0)
3637 $context[
'USER_ID'] = $order->getUserId();
3642 $context[
'USER_ID'] = $USER->getId();
3646 $providerClass =
new $provider($context);
3647 if ($providerClass && $providerClass instanceof SaleProviderBase)
3650 $creator = Internals\ProviderCreator::create($context);
3651 $creator->addBasketItem($basketItem);
3653 $r = $creator->getProductStores();
3654 if ($r->isSuccess())
3656 $result->setData($r->getData());
3660 $result->addErrors($r->getErrors());
3680 $resultData = $provider::getProductStores($fields);
3684 $fields[
'PRODUCT_ID'] => $resultData
3698 public static function checkProductBarcode(
BasketItem $basketItem, array $params = array())
3704 'BARCODE' => $params[
'BARCODE'],
3705 'STORE_ID' => $params[
'STORE_ID'],
3706 'PRODUCT_ID' => $productId
3710 if ($provider && array_key_exists(
"IBXSaleProductProvider", class_implements($provider)))
3712 $r = static::checkBarcode($provider, $data);
3713 if ($r->isSuccess())
3715 $resultData = $r->getData();
3716 if (!empty($resultData) && array_key_exists($productId, $resultData))
3718 $result = $resultData[$productId];
3722 elseif (class_exists($provider))
3728 throw new ObjectNotFoundException(
'Entity "Basket" not found');
3731 $order = $basket->getOrder();
3736 'USER_ID' => $order->getUserId(),
3737 'SITE_ID' => $order->getSiteId(),
3738 'CURRENCY' => $order->getCurrency(),
3745 'USER_ID' => $USER->getId(),
3746 'SITE_ID' => SITE_ID,
3747 'CURRENCY' => Currency\CurrencyManager::getBaseCurrency(),
3751 $creator = Internals\ProviderCreator::create($context);
3754 if ($providerClass instanceof SaleProviderBase)
3756 $creator->addBasketItemBarcodeData($basketItem, $data);
3759 $r = $creator->checkBarcode();
3760 if ($r->isSuccess())
3762 if (!empty($providerClass))
3764 $reflect = new \ReflectionClass($provider);
3765 $providerName = $reflect->getName();
3772 $resultData = $r->getData();
3773 if (!empty($resultData) && array_key_exists(
'BARCODE_CHECK_LIST', $resultData))
3775 $resultList = $resultData[
'BARCODE_CHECK_LIST'];
3776 if (isset($resultList[$providerName]) && isset($resultList[$providerName][$data[
'BARCODE']]))
3778 $result = $resultList[$providerName][$data[
'BARCODE']];
3796 if (!array_key_exists(
"IBXSaleProductProvider", class_implements($provider)))
3801 $resultData = $provider::checkProductBarcode($barcodeParams);
3805 $barcodeParams[
"PRODUCT_ID"] => $resultData
3818 public static function viewProduct(
BasketItem $basketItem)
3821 $basketProviderData = static::createProviderBasketItemMap($basketItem, array(
'SITE_ID',
'USER_ID'));
3822 $provider = $basketProviderData[
'PROVIDER'];
3823 if (!empty($provider))
3825 if (array_key_exists(
"IBXSaleProductProvider", class_implements($provider)))
3827 $productId = $basketProviderData[
'PRODUCT_ID'];
3829 'PRODUCT_ID' => $productId,
3830 'USER_ID' => $basketProviderData[
'USER_ID'],
3831 'SITE_ID' => $basketProviderData[
'SITE_ID'],
3834 $r = static::getViewProduct($provider, $data);
3835 if ($r->isSuccess())
3837 $resultProductData = $r->getData();
3838 if (array_key_exists($productId, $resultProductData))
3840 $result->setData($resultProductData);
3845 elseif (class_exists($provider))
3851 throw new ObjectNotFoundException(
'Entity "Basket" not found');
3854 $order = $basket->getOrder();
3859 'USER_ID' => $order->getUserId(),
3860 'SITE_ID' => $order->getSiteId(),
3861 'CURRENCY' => $order->getCurrency(),
3868 'USER_ID' => $USER->getId(),
3869 'SITE_ID' => SITE_ID,
3870 'CURRENCY' => Currency\CurrencyManager::getBaseCurrency(),
3874 $creator = Internals\ProviderCreator::create($context);
3877 if ($providerClass instanceof SaleProviderBase)
3879 $creator->addBasketItem($basketItem);
3882 $r = $creator->viewProduct();
3883 if ($r->isSuccess())
3885 $data = $r->getData();
3886 if (array_key_exists(
'VIEW_PRODUCTS_LIST', $data))
3888 $resultList = $data[
'VIEW_PRODUCTS_LIST'];
3890 if (!empty($resultList))
3893 $result = reset($resultList);
3897 $productId => reset($resultList)
3921 if (!array_key_exists(
"IBXSaleProductProvider", class_implements($provider)))
3926 $resultData = $provider::viewProduct($fields);
3929 $fields[
'PRODUCT_ID'] => $resultData
3941 public static function recurringOrderProduct(
BasketItem $basketItem)
3944 $basketProviderData = static::createProviderBasketItemMap($basketItem, array(
'SITE_ID',
'USER_ID'));
3945 $provider = $basketProviderData[
'PROVIDER'];
3946 if (!empty($provider))
3948 if (array_key_exists(
"IBXSaleProductProvider", class_implements($provider)))
3951 'PRODUCT_ID' => $basketProviderData[
'PRODUCT_ID'],
3952 'USER_ID' => $basketProviderData[
'USER_ID'],
3955 $r = static::recurringProduct($provider, $data);
3956 if ($r->isSuccess())
3958 $resultProductData = $r->getData();
3959 if (array_key_exists($basketProviderData[
'PRODUCT_ID'], $resultProductData))
3961 $result->setData($resultProductData);
3967 elseif (class_exists($provider))
3973 throw new ObjectNotFoundException(
'Entity "Basket" not found');
3976 $order = $basket->getOrder();
3981 'USER_ID' => $order->getUserId(),
3982 'SITE_ID' => $order->getSiteId(),
3983 'CURRENCY' => $order->getCurrency(),
3990 'USER_ID' => $USER->getId(),
3991 'SITE_ID' => SITE_ID,
3992 'CURRENCY' => Currency\CurrencyManager::getBaseCurrency(),
3996 $creator = Internals\ProviderCreator::create($context);
3999 if ($providerClass instanceof SaleProviderBase)
4001 $creator->addBasketItem($basketItem);
4004 $r = $creator->recurring();
4005 if ($r->isSuccess())
4007 $data = $r->getData();
4008 if (array_key_exists(
'RECURRING_PRODUCTS_LIST', $data))
4010 $resultList = $data[
'RECURRING_PRODUCTS_LIST'];
4012 if (!empty($resultList))
4015 $result = reset($resultList);
4019 $productId => reset($resultList)
4041 if (!array_key_exists(
"IBXSaleProductProvider", class_implements($provider)))
4046 $resultData = $provider::recurringOrderProduct($fields);
4049 $fields[
'PRODUCT_ID'] => $resultData
4063 $bundleChildList = array();
4067 if (array_key_exists(
"IBXSaleProductProvider", class_implements($provider)))
4071 elseif (class_exists($provider))
4077 $basket = $collection->getBasket();
4080 throw new ObjectNotFoundException(
'Entity "Basket" not found');
4083 $order = $basket->getOrder();
4088 'SITE_ID' => $order->getSiteId(),
4089 'USER_ID' => $order->getUserId(),
4090 'CURRENCY' => $order->getCurrency(),
4097 'SITE_ID' => SITE_ID,
4098 'USER_ID' => $USER && $USER->GetID() > 0 ? $USER->GetID() : 0,
4099 'CURRENCY' => Currency\CurrencyManager::getBaseCurrency(),
4102 $creator = Internals\ProviderCreator::create($context);
4104 $creator->addBasketItem($basketItem);
4106 $r = $creator->getBundleItems();
4107 if ($r->isSuccess())
4109 $resultProductListData = $r->getData();
4110 if (!empty($resultProductListData[
'BUNDLE_LIST']))
4112 $bundleChildList = $resultProductListData[
'BUNDLE_LIST'];
4116 $order = $basket->getOrder();
4121 'SITE_ID' => $order->getSiteId(),
4122 'USER_ID' => $order->getUserId(),
4123 'CURRENCY' => $order->getCurrency(),
4130 'SITE_ID' => SITE_ID,
4131 'USER_ID' => $USER && $USER->GetID() > 0 ? $USER->GetID() : 0,
4132 'CURRENCY' => Currency\CurrencyManager::getBaseCurrency(),
4135 $creator = Internals\ProviderCreator::create($context);
4137 $creator->addBasketItem($basketItem);
4139 $r = $creator->getBundleItems();
4140 if ($r->isSuccess())
4142 $resultProductListData = $r->getData();
4143 if (!empty($resultProductListData[
'BUNDLE_LIST']))
4145 $bundleChildList = $resultProductListData[
'BUNDLE_LIST'];
4151 $bundleChildList = \CSaleBasket::executeCallbackFunction(
4152 $basketItem->
getField(
'CALLBACK_FUNC'),
4154 $basketItem->
getField(
'PRODUCT_ID'),
4159 return $bundleChildList;
4171 private static function getBundleChildItemsByProductData($providerName, array
$productData)
4173 if (array_key_exists(
"IBXSaleProductProvider", class_implements($providerName)))
4179 $bundleChildList = \CSaleBasket::executeCallbackFunction(
4187 if (is_array($bundleChildList))
4189 $bundleChildList = reset($bundleChildList);
4192 return $bundleChildList;
4205 $resultList = array();
4209 $resultList[$productId] = static::getBundleChildItemsByProductData($providerName,
$productData);
4212 if (!empty($resultList))
4216 'BUNDLE_LIST' => $resultList,
4235 'NEGATIVE_AMOUNT_TRACE',
4241 $providerProductList = array();
4243 if (!empty($basketProviderList))
4245 foreach ($basketProviderList as $provider => $providerBasketItemList)
4247 $providerProductList = $provider::getProductList($providerBasketItemList, $productList, $select) + $providerProductList;
4251 return (!empty($providerProductList) && is_array($providerProductList) ? $providerProductList :
false);
4262 public static function checkAvailableProductQuantity(
BasketItemBase $basketItem, $deltaQuantity)
4264 global $APPLICATION;
4268 $resultProductData = array();
4278 if (!$basket = $collection->getBasket())
4284 if (($order = $basket->getOrder()) !==
null)
4286 $userId = $order->getUserId();
4287 $siteId = $order->getSiteId();
4290 if ($siteId ===
null)
4292 $siteId = $basket->getSiteId();
4297 if (!empty($provider))
4299 if (array_key_exists(
"IBXSaleProductProvider", class_implements($provider)))
4302 if ($order && $order->getId() > 0)
4304 $needQuantity = $deltaQuantity;
4311 $poolQuantity = static::getQuantityPoolItem($order->getInternalId(), $basketItem);
4314 $checkQuantity = $needQuantity - floatval($poolQuantity);
4318 "QUANTITY" => $checkQuantity,
4319 "USER_ID" => $userId,
4320 "SITE_ID" => $siteId,
4321 "BASKET_ID" => $basketItem->getId(),
4322 "CHECK_QUANTITY" =>
"Y",
4323 "AVAILABLE_QUANTITY" =>
"Y",
4324 'CHECK_PRICE' =>
'N',
4325 'CHECK_COUPONS' =>
'N',
4326 "SELECT_QUANTITY_TRACE" =>
"Y",
4330 if ($deltaQuantity <= 0 || $checkQuantity == 0)
4332 $result->setData(array(
'AVAILABLE_QUANTITY' => $deltaQuantity));
4336 $hasTrustData =
false;
4340 if (static::isReadTrustData() ===
true
4343 $hasTrustData =
true;
4345 $productDataRequiredFields = array_merge(static::getProductDataRequiredFields(), array(
'AVAILABLE_QUANTITY'));
4346 foreach ($productDataRequiredFields as $requiredField)
4348 if (!array_key_exists($requiredField, $resultProductData))
4350 $hasTrustData =
false;
4356 && roundEx($checkQuantity, SALE_VALUE_PRECISION) > roundEx($resultProductData[
"AVAILABLE_QUANTITY"], SALE_VALUE_PRECISION))
4358 $hasTrustData =
false;
4365 $APPLICATION->ResetException();
4366 $resultProductData = $provider::GetProductData($data);
4367 $ex = $APPLICATION->GetException();
4370 $result->addWarning(
new ResultWarning($ex->GetString(), $ex->GetID()) );
4375 elseif (class_exists($provider))
4378 $providerClass =
new $provider();
4379 if ($providerClass && $providerClass instanceof SaleProviderBase)
4383 $productId => array(
4384 'ITEM_CODE' => $productId,
4386 'QUANTITY' => $deltaQuantity,
4389 $r = $providerClass->getAvailableQuantity($products);
4390 if ($r->isSuccess())
4392 $resultData = $r->getData();
4393 if (!empty($resultData[
'AVAILABLE_QUANTITY_LIST']))
4395 $resultProductData = array(
4396 'AVAILABLE_QUANTITY' => reset($resultData[
'AVAILABLE_QUANTITY_LIST'])
4404 $APPLICATION->ResetException();
4405 $resultProductData = \CSaleBasket::ExecuteCallbackFunction(
4406 $basketItem->
getField(
'CALLBACK_FUNC'),
4412 if ($ex = $APPLICATION->GetException())
4414 $result->addWarning(
new ResultWarning($ex->GetString(), $ex->GetID()) );
4421 if ($deltaQuantity <= 0)
4423 $availableQuantity = $deltaQuantity;
4426 'AVAILABLE_QUANTITY' => $availableQuantity,
4434 if (array_key_exists(
'AVAILABLE_QUANTITY', $resultProductData))
4436 $fields[
'AVAILABLE_QUANTITY'] = $resultProductData[
'AVAILABLE_QUANTITY'];
4439 if (array_key_exists(
'QUANTITY_TRACE', $resultProductData))
4441 $fields[
'QUANTITY_TRACE'] = ($resultProductData[
'QUANTITY_TRACE'] ==
"Y");
4444 if (!empty($fields))
4446 $result->setData($fields);
4460 private static function getAvailableQuantityByProductData($providerClass,
$productData, array $context)
4462 global $APPLICATION;
4464 $result =
new Result();
4466 $callbackFunction =
null;
4478 $resultProductData = array();
4480 $userId = $context[
'USER_ID'];
4481 $siteId = $context[
'SITE_ID'];
4485 $productQuantity = 0;
4492 foreach (
$productData[
'QUANTITY_LIST'] as $basketCode => $quantity)
4494 $productQuantity += $quantity;
4498 if (!empty($providerClass) && array_key_exists(
"IBXSaleProductProvider", class_implements($providerClass)))
4500 if ($productQuantity <= 0)
4504 'AVAILABLE_QUANTITY' => $productQuantity
4513 $basketId = $basketItem->getId();
4517 "PRODUCT_ID" => $productId,
4518 "QUANTITY" => $productQuantity,
4519 "USER_ID" => $userId,
4520 "SITE_ID" => $siteId,
4521 "BASKET_ID" => $basketId,
4522 "CHECK_QUANTITY" =>
"Y",
4523 "AVAILABLE_QUANTITY" =>
"Y",
4524 'CHECK_PRICE' =>
'N',
4525 'CHECK_COUPONS' =>
'N',
4526 "SELECT_QUANTITY_TRACE" =>
"Y",
4536 $hasTrustData =
false;
4540 if (static::isReadTrustData() ===
true
4543 $hasTrustData =
true;
4545 $productDataRequiredFields = array_merge(static::getProductDataRequiredFields(), array(
'AVAILABLE_QUANTITY'));
4546 foreach ($productDataRequiredFields as $requiredField)
4548 if (!array_key_exists($requiredField, $resultProductData))
4550 $hasTrustData =
false;
4556 && roundEx($productQuantity, SALE_VALUE_PRECISION) > roundEx($resultProductData[
"AVAILABLE_QUANTITY"], SALE_VALUE_PRECISION))
4558 $hasTrustData =
false;
4565 $APPLICATION->ResetException();
4566 $resultProductData = $providerClass::GetProductData($data);
4567 if ($ex = $APPLICATION->GetException())
4569 $result->addWarning(
new ResultWarning($ex->GetString(), $ex->GetID()) );
4574 elseif (!empty($callbackFunction))
4576 $APPLICATION->ResetException();
4577 $resultProductData = \CSaleBasket::ExecuteCallbackFunction(
4584 if ($ex = $APPLICATION->GetException())
4586 $result->addWarning(
new ResultWarning($ex->GetString(), $ex->GetID()) );
4593 'AVAILABLE_QUANTITY' => $productQuantity
4601 if (!empty($resultProductData))
4603 if (array_key_exists(
'AVAILABLE_QUANTITY', $resultProductData))
4605 $fields[
'AVAILABLE_QUANTITY'] = $resultProductData[
'AVAILABLE_QUANTITY'];
4608 if (array_key_exists(
'QUANTITY_TRACE', $resultProductData))
4610 $fields[
'QUANTITY_TRACE'] = ($resultProductData[
'QUANTITY_TRACE'] ==
"Y");
4614 if (!empty($fields))
4616 $result->setData($fields);
4630 private static function getProviderDataByProductData($providerClass,
$productData, array $context)
4632 $result =
new Result();
4634 $providerName =
null;
4635 if (!empty($providerClass))
4637 $reflect = new \ReflectionClass($providerClass);
4638 $providerName = $reflect->getName();
4645 $r = static::getProductDataByList($items, $providerName, array(
'PRICE',
'COUPONS',
'AVAILABLE_QUANTITY',
'QUANTITY'), $context);
4647 if ($r->isSuccess())
4649 $resultData = $r->getData();
4650 $isExistsProductDataList = isset($resultData[
'PRODUCT_DATA_LIST']) && !empty($resultData[
'PRODUCT_DATA_LIST']);
4651 $isExistsProductData = isset($resultData[
'PRODUCT_DATA_LIST'][$productId]);
4653 if ($isExistsProductDataList && $isExistsProductData)
4655 $result->setData($resultData[
'PRODUCT_DATA_LIST'][$productId]);
4669 public static function deliverShipment(Shipment $shipment)
4672 $result =
new Result();
4674 $needDeliver =
null;
4675 if ($shipment->getFields()->isChanged(
'ALLOW_DELIVERY'))
4677 $needDeliver = $shipment->getField(
'ALLOW_DELIVERY') ===
"Y";
4680 if ($needDeliver ===
null || ($needDeliver ===
false && $shipment->getId() <= 0))
4683 $resultList = array();
4686 if (!$shipmentItemCollection = $shipment->getShipmentItemCollection())
4688 throw new ObjectNotFoundException(
'Entity "ShipmentItemCollection" not found');
4692 if (!$shipmentCollection = $shipment->getCollection())
4694 throw new ObjectNotFoundException(
'Entity "ShipmentCollection" not found');
4698 if (!$order = $shipmentCollection->getOrder())
4700 throw new ObjectNotFoundException(
'Entity "Order" not found');
4704 if (!$basket = $order->getBasket())
4709 $basketList = static::getBasketFromShipmentItemCollection($shipmentItemCollection);
4711 $basketProviderMap = static::createProviderBasketMap($basketList, array(
'ORDER_ID',
'USER_ID',
'QUANTITY',
'ALLOW_DELIVERY',
'PAY_CALLBACK',
'PAID'));
4712 $basketProviderList = static::redistributeToProviders($basketProviderMap);
4714 if (!empty($basketProviderList))
4716 foreach ($basketProviderList as $provider => $providerBasketItemList)
4718 if (array_key_exists(
"IBXSaleProductProvider", class_implements($provider)))
4721 foreach ($providerBasketItemList as $providerBasketItem)
4724 if ($providerBasketItem[
'BASKET_ITEM']->isBundleParent())
4729 if ($providerBasketItem[
'BASKET_ITEM']->getField(
'MODULE') !=
'')
4732 "PRODUCT_ID" => $providerBasketItem[
"PRODUCT_ID"],
4733 "USER_ID" => $providerBasketItem[
"USER_ID"],
4734 "PAID" => $providerBasketItem[
"PAID"],
4735 "ORDER_ID" => $providerBasketItem[
"ORDER_ID"],
4736 "BASKET_ID" => $providerBasketItem[
'BASKET_ID']
4739 $r = static::deliverProductData($provider, $data);
4740 if ($r->isSuccess())
4742 $resultData = $r->getData();
4744 if (array_key_exists($providerBasketItem[
"PRODUCT_ID"], $resultData))
4746 $resultProductData = $resultData[$providerBasketItem[
"PRODUCT_ID"]];
4751 $result->addErrors($r->getErrors());
4754 if (!empty($resultProductData) && is_array($resultProductData))
4756 $resultProductData[
'ORDER_ID'] = $providerBasketItem[
'ORDER_ID'];
4761 $resultProductData =
true;
4764 $resultList[$providerBasketItem[
'BASKET_CODE']] = $resultProductData;
4769 elseif (class_exists($provider))
4772 'SITE_ID' => $order->getSiteId(),
4773 'CURRENCY' => $order->getCurrency(),
4776 if ($order->getUserId() > 0)
4778 $context[
'USER_ID'] = $order->getUserId();
4783 $context[
'USER_ID'] = $USER->getId();
4786 $creator = Internals\ProviderCreator::create($context);
4789 foreach ($shipmentItemCollection as $shipmentItem)
4791 $basketItem = $shipmentItem->getBasketItem();
4794 if ($providerClass instanceof SaleProviderBase)
4796 $creator->addShipmentItem($shipmentItem);
4800 $r = $creator->deliver();
4801 if ($r->isSuccess())
4803 $r = $creator->createItemsResultAfterDeliver($r);
4804 if ($r->isSuccess())
4806 $data = $r->getData();
4807 if (array_key_exists(
'RESULT_AFTER_DELIVER_LIST', $data))
4809 $resultList = $data[
'RESULT_AFTER_DELIVER_LIST'] + $resultList;
4815 $result->addErrors($r->getErrors());
4820 foreach ($providerBasketItemList as $providerBasketItem)
4822 $resultProductData = \CSaleBasket::ExecuteCallbackFunction(
4823 $providerBasketItem[
'CALLBACK_FUNC'],
4824 $providerBasketItem[
'MODULE'],
4825 $providerBasketItem[
'PRODUCT_ID'],
4826 $providerBasketItem[
'USER_ID'],
4827 $providerBasketItem[
"ALLOW_DELIVERY"],
4828 $providerBasketItem[
'ORDER_ID'],
4829 $providerBasketItem[
"QUANTITY"]
4832 $basketCode = $providerBasketItem[
'BASKET_ITEM']->getBasketCode();
4834 if (!empty($resultProductData) && is_array($resultProductData))
4836 $resultProductData[
'ORDER_ID'] = $providerBasketItem[
'ORDER_ID'];
4839 $resultList[$basketCode] = $resultProductData;
4844 if (!empty($resultList) && is_array($resultList))
4846 $recurringID = intval($order->getField(
"RECURRING_ID"));
4847 foreach ($resultList as $basketCode => $resultData)
4849 if ($order->isPaid())
4851 if (!empty($resultData) && is_array($resultData))
4853 if (empty($resultData[
'ORDER_ID']) || intval($resultData[
'ORDER_ID']) < 0)
4854 $resultData[
"ORDER_ID"] = $order->getId();
4856 $resultData[
"REMAINING_ATTEMPTS"] = (defined(
"SALE_PROC_REC_ATTEMPTS") ? SALE_PROC_REC_ATTEMPTS : 3);
4857 $resultData[
"SUCCESS_PAYMENT"] =
"Y";
4859 if ($recurringID > 0)
4860 \CSaleRecurring::Update($recurringID, $resultData);
4862 \CSaleRecurring::Add($resultData);
4864 elseif ($recurringID > 0)
4866 \CSaleRecurring::Delete($recurringID);
4872 if (!$basketItem = $basket->getItemByBasketCode($basketCode))
4874 throw new ObjectNotFoundException(
'Entity "BasketItem" not found');
4877 $resRecurring = \CSaleRecurring::GetList(
4880 "USER_ID" => $order->getUserId(),
4882 "MODULE" => $basketItem->
getField(
"MODULE")
4885 while ($recurringData = $resRecurring->Fetch())
4887 \CSaleRecurring::Delete($recurringData[
"ID"]);
4894 if (!empty($resultList))
4896 $result->setData($resultList);
4910 global $APPLICATION;
4913 $APPLICATION->ResetException();
4914 $resultProductData =
false;
4916 if ($provider && array_key_exists(
"IBXSaleProductProvider", class_implements($provider)))
4918 $resultProductData = $provider::DeliverProduct($fields);
4922 $resultProductData = \CSaleBasket::ExecuteCallbackFunction(
4923 $fields[
'CALLBACK_FUNC'],
4925 $fields[
'PRODUCT_ID'],
4927 $fields[
"ALLOW_DELIVERY"],
4928 $fields[
'ORDER_ID'],
4932 if (!empty($resultProductData) && is_array($resultProductData))
4934 $resultProductData[
'ORDER_ID'] = $fields[
'ORDER_ID'];
4939 $ex = $APPLICATION->GetException();
4942 $result->addError(
new ResultError($ex->GetString(), $ex->GetID()) );
4946 $resultList[$fields[
'PRODUCT_ID']] = $resultProductData;
4949 if (!empty($resultList) && is_array($resultList))
4951 $result->setData($resultList);
4964 protected static function createProviderBasketMap(array $basketList, array $select = array())
4966 $basketProviderMap = array();
4972 foreach($basketList as $basketIndex => $basketItemDat)
4974 if (is_array($basketItemDat) && isset($basketItemDat[
'BASKET_ITEM']))
4976 $basketItem = $basketItemDat[
'BASKET_ITEM'];
4980 $basketItem = $basketItemDat;
4983 $basketProviderData = static::createProviderBasketItemMap($basketItem, $select);
4984 if (!$basketProviderData)
4989 $basketProviderMap[$basketIndex] = $basketProviderData;
4993 return $basketProviderMap;
4997 protected static function createProviderBasketItemMap(BasketItem $basketItem, array $select = array())
5000 $basketProviderData = array(
5001 'BASKET_ITEM' => $basketItem,
5002 'BASKET_ID' => $basketItem->getId(),
5003 'BASKET_CODE' => $basketItem->getBasketCode(),
5004 'PRODUCT_ID' => $basketItem->getProductId(),
5005 'MODULE' => $basketItem->getField(
'MODULE'),
5008 $provider = $basketItem->getProvider();
5009 $providerClass = $basketItem->getProviderEntity();
5012 if (array_key_exists(
"IBXSaleProductProvider", class_implements($provider))
5013 || $providerClass instanceof SaleProviderBase)
5015 $basketProviderData[
'PROVIDER'] = $provider;
5018 elseif (strval($basketItem->getField(
'CALLBACK_FUNC')) !=
'')
5020 $basketProviderData[
'CALLBACK_FUNC'] = $basketItem->getField(
'CALLBACK_FUNC');
5022 elseif (strval($basketItem->getField(
'PAY_CALLBACK_FUNC')) !=
'' && in_array(
'PAY_CALLBACK', $select))
5024 $basketProviderData[
'CALLBACK_FUNC'] = $basketItem->getField(
'PAY_CALLBACK_FUNC');
5027 if (in_array(
'QUANTITY', $select))
5029 $basketProviderData[
'QUANTITY'] = $basketItem->getQuantity();
5032 if (in_array(
'RENEWAL', $select))
5034 $basketProviderData[
'RENEWAL'] = $basketItem->getField(
'RENEWAL')!==
null && $basketItem->getField(
'RENEWAL') !=
'N'?
'Y' :
'N';
5037 if (in_array(
'RESERVED', $select))
5039 $basketProviderData[
'RESERVED'] = $basketItem->getField(
'RESERVED');
5042 if (in_array(
'SITE_ID', $select))
5044 $basketProviderData[
'SITE_ID'] = $basketItem->getField(
'LID');
5047 if (in_array(
'ORDER_ID', $select))
5050 if (!$basket = $basketItem->getCollection())
5052 throw new ObjectNotFoundException(
'Entity "Basket" not found');
5055 if ($basket->getOrder() && $basket->getOrderId() > 0)
5057 $basketProviderData[
'ORDER_ID'] = $basket->getOrderId();
5062 if (in_array(
'USER_ID', $select))
5065 if (!$basket = $basketItem->getCollection())
5067 throw new ObjectNotFoundException(
'Entity "Basket" not found');
5070 if ($order = $basket->getOrder())
5072 $userId = $order->getUserId();
5074 if ($userId ===
null)
5076 $userId = Sale\Fuser::getUserIdById($basket->getFUserId());
5081 $basketProviderData[
'USER_ID'] =
$userId;
5087 if (in_array(
'PAID', $select))
5090 if (!$basket = $basketItem->getCollection())
5092 throw new ObjectNotFoundException(
'Entity "Basket" not found');
5095 if ($basket->getOrder() && $basket->getOrderId() > 0)
5097 $order = $basket->getOrder();
5098 $basketProviderData[
'PAID'] = $order->isPaid();
5103 if (in_array(
'ALLOW_DELIVERY', $select))
5106 if (!$basket = $basketItem->getCollection())
5108 throw new ObjectNotFoundException(
'Entity "Basket" not found');
5111 if ($basket->getOrder() && $basket->getOrderId() > 0)
5114 $order = $basket->getOrder();
5117 if ($shipmentCollection = $order->getShipmentCollection())
5119 $basketProviderData[
'ALLOW_DELIVERY'] = $shipmentCollection->isAllowDelivery();
5125 return $basketProviderData;
5131 private static function getProviderBasketFromShipment(Shipment $shipment)
5133 $shipmentItemCollection = $shipment->getShipmentItemCollection();
5135 $basketList = static::getBasketFromShipmentItemCollection($shipmentItemCollection);
5137 $basketProviderMap = static::createProviderBasketMap($basketList, array(
'QUANTITY',
'PRODUCT_ID'));
5139 $basketProviderList = static::redistributeToProviders($basketProviderMap);
5141 return $basketProviderList;
5151 $basketProviderList = array();
5152 foreach($basketProviderMap as $basketProviderItem)
5154 $providerName = $basketProviderItem[
'PROVIDER'] ??
'';
5155 $productId = $basketProviderItem[
'BASKET_ITEM']->getProductId();
5156 $quantity = floatval($basketProviderItem[
'QUANTITY']);
5157 unset($basketProviderItem[
'QUANTITY']);
5159 $basketCode = $basketProviderItem[
'BASKET_CODE'];
5161 if (!isset($basketProviderList[$providerName][$productId]))
5163 $basketProviderList[$providerName][$productId] = $basketProviderItem;
5166 if (isset($basketProviderList[$providerName][$productId][
'QUANTITY_LIST'][$basketCode]))
5168 $basketProviderList[$providerName][$productId][
'QUANTITY_LIST'][$basketCode] += $quantity;
5172 $basketProviderList[$providerName][$productId][
'QUANTITY_LIST'][$basketCode] = $quantity;
5179 return $basketProviderList;
5188 static::$useReadTrustData = (bool)$value;
5197 return (
bool)static::$useReadTrustData;
5211 return (!empty(static::$trustData[$siteId][$module][$productId]) && is_array(static::$trustData[$siteId][$module][$productId]));
5222 public static function setTrustData($siteId, $module, $productId, array $fields)
5224 static::$trustData[$siteId][$module][$productId] = $fields;
5238 if (static::isExistsTrustData($siteId, $module, $productId))
5239 return static::$trustData[$siteId][$module][$productId];
5250 public static function resetTrustData($siteId =
null, $module =
null, $productId =
null)
5252 if (strval($siteId) !=
'')
5254 if (!empty(static::$trustData[$siteId]))
5256 if (intval($productId) > 0 )
5258 if (strval($module) ==
'')
5260 foreach (static::$trustData[$siteId] as $moduleName => $data)
5262 if (isset(static::$trustData[$siteId][$moduleName][$productId]))
5263 unset(static::$trustData[$siteId][$moduleName][$productId]);
5268 if (isset(static::$trustData[$siteId][$module][$productId]))
5269 unset(static::$trustData[$siteId][$module][$productId]);
5272 elseif (strval($module) !=
'')
5274 if (isset(static::$trustData[$siteId][$module]))
5275 unset(static::$trustData[$siteId][$module]);
5279 if (isset(static::$trustData[$siteId]))
5280 unset(static::$trustData[$siteId]);
5286 static::$trustData = array();
5302 protected static function refreshMarkers(
Order $order)
5304 if ($order->getId() == 0)
5309 if (!$shipmentCollection = $order->getShipmentCollection())
5311 throw new ObjectNotFoundException(
'Entity "ShipmentCollection" not found');
5314 if (!$paymentCollection = $order->getPaymentCollection())
5316 throw new ObjectNotFoundException(
'Entity "PaymentCollection" not found');
5319 if (!$basket = $order->getBasket())
5321 throw new ObjectNotFoundException(
'Entity "Basket" not found');
5324 $markList = array();
5326 $markerEntityList = array();
5330 '=ORDER_ID' => $order->getId(),
5333 'select' => array(
'ID',
'ENTITY_TYPE',
'ENTITY_ID',
'CODE',
'SUCCESS'),
5334 'order' => array(
'ID' =>
'DESC')
5337 while($markerData = $res->fetch())
5339 if (!empty($markList[$markerData[
'ENTITY_TYPE']])
5340 && !empty($markList[$markerData[
'ENTITY_TYPE']][$markerData[
'ENTITY_ID']])
5341 && $markerData[
'CODE'] == $markList[$markerData[
'ENTITY_TYPE']][$markerData[
'ENTITY_ID']]
5349 $markList[$markerData[
'ENTITY_TYPE']][$markerData[
'ENTITY_ID']][] = $markerData[
'CODE'];
5352 if ($poolItemSuccess =
EntityMarker::getPoolItemSuccess($order, $markerData[
'ID'], $markerData[
'ENTITY_TYPE'], $markerData[
'ENTITY_ID'], $markerData[
'CODE']))
5356 foreach ($markList[$markerData[
'ENTITY_TYPE']][$markerData[
'ENTITY_ID']] as $markerIndex => $markerCode)
5358 if ($markerData[
'CODE'] == $markerCode)
5360 unset($markList[$markerData[
'ENTITY_TYPE']][$markerData[
'ENTITY_ID']][$markerIndex]);
5364 if (empty($markList[$markerData[
'ENTITY_TYPE']][$markerData[
'ENTITY_ID']]))
5366 unset($markList[$markerData[
'ENTITY_TYPE']][$markerData[
'ENTITY_ID']]);
5371 if (empty($markList[$markerData[
'ENTITY_TYPE']]))
5373 unset($markList[$markerData[
'ENTITY_TYPE']]);
5377 if (!empty($markList))
5379 foreach ($markList as $markEntityType => $markEntityList)
5381 foreach ($markEntityList as $markEntityId => $markEntityCodeList)
5383 if (empty($markEntityCodeList))
5385 if (($entity = EntityMarker::getEntity($order, $markEntityType, $markEntityId)) && ($entity instanceof \
IEntityMarker))
5387 if ($entity->canMarked())
5389 $markedField = $entity->getMarkField();
5390 $entity->setField($markedField,
'N');
5400 if ($shipmentCollection->isMarked())
5403 foreach ($shipmentCollection as $shipment)
5405 if ($shipment->isMarked())
5407 $shipment->setField(
'MARKED',
'N');
5411 if ($paymentCollection->isMarked())
5414 foreach ($paymentCollection as $payment)
5416 if ($payment->isMarked())
5418 $payment->setField(
'MARKED',
'N');
5423 $order->setField(
'MARKED',
'N');
5448 static::getUpdatableFields()
5471 'QUANTITY_RESERVED',
5519 $resultList = array();
5523 $r = static::getAvailableQuantityByProductData($providerClass,
$productData, $context);
5524 if (!$r->isSuccess())
5526 $result->addErrors($r->getErrors());
5528 elseif ($r->hasWarnings())
5530 $result->addWarnings($r->getWarnings());
5533 $availableQuantityData = $r->getData();
5534 if (array_key_exists(
'AVAILABLE_QUANTITY', $availableQuantityData))
5536 $resultList[$productId] ??= 0;
5538 $resultList[$productId] += floatval($availableQuantityData[
'AVAILABLE_QUANTITY']);
5543 '#PRODUCT_ID#' => $productId
5544 )),
'PROVIDER_BASKET_ITEM_WRONG_AVAILABLE_QUANTITY'));
5549 if (!empty($resultList))
5553 'AVAILABLE_QUANTITY_LIST' => $resultList,
5570 public static function getAvailableQuantityAndPrice($providerClass, array $products, array $context)
5573 $availableQuantityList = array();
5574 $priceData = array();
5575 $providerName =
null;
5586 $callbackFunction =
null;
5592 $isCustomItem = !($providerClass || $callbackFunction);
5596 $providerData = $basketItem->getFieldValues();
5597 $providerData[
'AVAILABLE_QUANTITY'] = $basketItem->getQuantity();
5601 $r = static::getProviderDataByProductData($providerClass,
$productData, $context);
5602 if (!$r->isSuccess())
5604 $result->addErrors($r->getErrors());
5606 elseif ($r->hasWarnings())
5608 $result->addWarnings($r->getWarnings());
5610 $providerData = $r->getData();
5613 if (!empty($providerData))
5615 if (isset($providerData[
'AVAILABLE_QUANTITY']))
5618 $availableQuantityList[
$productId] += (float)$providerData[
'AVAILABLE_QUANTITY'];
5622 $result->addWarning(
new ResultWarning(
Loc::getMessage(
'SALE_PROVIDER_BASKET_ITEM_WRONG_AVAILABLE_QUANTITY', array(
5623 '#PRODUCT_ID#' => $productId
5624 )),
'PROVIDER_BASKET_ITEM_WRONG_AVAILABLE_QUANTITY'));
5630 $priceFields = static::getPriceFields();
5632 foreach ($priceFields as $fieldName)
5634 if (array_key_exists($fieldName, $providerData))
5636 $priceData[
$productId][$basketItem->getBasketCode()][$fieldName] = $providerData[$fieldName];
5646 'PRODUCT_DATA_LIST' => array(
5647 'PRICE_LIST' => $priceData,
5648 'AVAILABLE_QUANTITY_LIST' => $availableQuantityList
5663 public static function isNeedShip($shipmentItemList)
5665 $result =
new Result();
5667 $resultList = array();
5670 foreach ($shipmentItemList as $shipmentItem)
5672 $basketItem = $shipmentItem->getBasketItem();
5673 $providerName = $basketItem->getProviderName();
5675 if ($providerName && array_key_exists(
"IBXSaleProductProvider", class_implements($providerName)))
5678 $isNeedShip =
false;
5680 if (method_exists($providerName,
'isNeedShip'))
5682 $isNeedShip = $providerName::isNeedShip();
5685 $resultList[$providerName] = $isNeedShip;
5690 if (!empty($resultList))
5692 $result->setData($resultList);