20 public function tryShip(array $products)
22 Main\Loader::includeModule(
'catalog');
25 $tryShipmentItemList = array();
26 $reservedQuantityList = array();
27 $shipmentItemParents = array();
29 $shipmentItemList = static::getShipmentItemListFromProducts($products);
30 if (!empty($shipmentItemList))
33 foreach ($shipmentItemList as $shipmentItem)
36 $shipmentItemCollection = $shipmentItem->getCollection();
37 if (!$shipmentItemCollection)
42 $shipment = $shipmentItemCollection->getShipment();
48 $shipmentItemParents[$shipmentItem->getInternalIndex()] = $shipment;
50 $basketItem = $shipmentItem->getBasketItem();
52 $provider = $basketItem->getProviderEntity();
54 if ($provider instanceof Catalog\
Product\CatalogProvider)
59 if ($shipment->needShip() ===
Sale\Internals\Catalog\Provider::SALE_TRANSFER_PROVIDER_SHIPMENT_NEED_SHIP
60 && !array_key_exists($shipment->getInternalIndex(), $reservedQuantityList))
62 $reservedQuantityList[$shipment->getInternalIndex()] = static::getReservedQuantity($shipment);
65 $tryShipmentItemList[$shipmentItem->getInternalIndex()] = $shipmentItem;
69 if (!empty($tryShipmentItemList))
72 $reservedQuantityList = array();
74 $r = Sale\Provider::tryShipmentItemList($tryShipmentItemList);
78 $result->addErrors($r->getErrors());
81 $data = $r->getData();
82 if (!empty($data) && is_array($data))
88 foreach ($data as $basketCode => $resultTryShipment)
90 if (!$resultTryShipment->isSuccess())
92 $result->addErrors($resultTryShipment->getErrors());
97 if ($result->isSuccess())
99 if (!empty($reservedQuantityList))
101 static::setReservedQuantityToShipmentItem($tryShipmentItemList, $reservedQuantityList);
104 $resultList = static::createListFromTryShipmentResult($tryShipmentItemList, $r);
108 if (!empty($resultList))
112 'TRY_SHIP_PRODUCTS_LIST' => $resultList
128 Main\Loader::includeModule(
'catalog');
131 $resultNeedShipList = array();
132 $needShipmentItemList = array();
134 $shipmentItemList = static::getShipmentItemListFromProducts($products);
135 if (!empty($shipmentItemList))
138 foreach ($shipmentItemList as $shipmentItem)
141 $shipmentItemCollection = $shipmentItem->getCollection();
142 if (!$shipmentItemCollection)
147 $shipment = $shipmentItemCollection->getShipment();
153 $basketItem = $shipmentItem->getBasketItem();
155 $provider = $basketItem->getProviderEntity();
157 if ($provider instanceof Catalog\
Product\CatalogProvider)
162 $needShipmentItemList[$shipmentItem->getInternalIndex()] = $shipmentItem;
166 if (!empty($needShipmentItemList))
168 $r = Sale\Provider::isNeedShip($needShipmentItemList);
169 if (!$r->isSuccess())
171 $result->addErrors($r->getErrors());
174 $data = $r->getData();
175 if (!empty($data) && is_array($data))
177 $needShipmentItemList = $data;
182 if (!empty($needShipmentItemList))
186 'IS_NEED_SHIP' => $needShipmentItemList
200 private static function getReservedQuantity(
Sale\
Shipment $shipment)
202 $reservedQuantityList = array();
203 $shipmentItemCollection = $shipment->getShipmentItemCollection();
204 if (!$shipmentItemCollection)
210 foreach ($shipmentItemCollection as $shipmentItem)
212 $reservedQuantityList[$shipmentItem->getInternalIndex()] = $shipmentItem->getReservedQuantity();
215 return $reservedQuantityList;
224 private static function setReservedQuantityToShipmentItem($shipmentItemList, array $reservedQuantityList)
227 foreach ($shipmentItemList as $shipmentItem)
229 $shipmentItemIndex = $shipmentItem->getInternalIndex();
230 if (!empty($reservedQuantityList[$shipmentItemIndex]))
232 $shipmentItem->setFieldNoDemand(
'RESERVED_QUANTITY', $reservedQuantityList[$shipmentItemIndex]);
244 public function ship(array $products)
246 $basketItemList = array();
247 $shipmentItemList = array();
248 $basketItemShipmentItemList = array();
249 $shipmentItemQuantityList = array();
251 $oneReserveStatus =
true;
252 $needReserved =
null;
253 $reservedList = array();
255 $resultList = array();
257 foreach ($products as $productId => $itemData)
261 if (!empty($fields[
'SHIPMENT_ITEM_LIST']))
264 foreach ($fields[
'SHIPMENT_ITEM_LIST'] as $shipmentIndexItem => $shipmentItem)
266 $shipmentItemList[$shipmentIndexItem] = $shipmentItem;
268 $basketItem = $shipmentItem->getBasketItem();
274 $basketCode = $basketItem->getBasketCode();
275 $basketItemList[$basketCode] = $basketItem;
290 $basketItemShipmentItemList[$basketCode][$shipmentIndexItem] = $shipmentItem;
294 if (!empty($fields[
'NEED_RESERVE_LIST']))
296 foreach ($fields[
'NEED_RESERVE_LIST'] as $shipmentItemIndex => $reserved)
298 $reserveValue = $reserved ?
'Y':
'N';
300 $reservedList[$reserveValue][] = $shipmentItemIndex;
301 if (!empty($reservedList) && !isset($reservedList[$reserveValue]))
303 $oneReserveStatus =
false;
305 elseif ($needReserved ===
null)
307 $needReserved = $reserved;
313 if (!empty($fields[
'SHIPMENT_ITEM_QUANTITY_LIST']))
315 foreach ($fields[
'SHIPMENT_ITEM_QUANTITY_LIST'] as $shipmentItemIndex => $quantity)
317 $shipmentItemQuantityList[$fields[
'BASKET_CODE']][$shipmentItemIndex] = $quantity;
321 $resultList[$productId] =
false;
326 $r = static::tryShip($products);
327 if (!$r->isSuccess())
329 $result->addErrors($r->getErrors());
331 elseif ($r->hasWarnings())
333 $result->addWarnings($r->getWarnings());
336 if (!empty($basketItemList))
339 foreach ($basketItemList as $basketItem)
341 $productId = $basketItem->getProductId();
342 $productData = $products[$productId];
343 $basketCode = $basketItem->getBasketCode();
344 $quantity = $productData[
'QUANTITY_LIST'][$basketCode];
346 $shipmentFieldsList = array();
347 if (!$oneReserveStatus)
353 foreach ($reservedList as $reserveValue => $shipmentItemIndexList)
356 foreach ($shipmentItemIndexList as $shipmentItemIndex)
358 if (isset($shipmentItemQuantityList[$basketCode][$shipmentItemIndex]))
360 $quantity += $shipmentItemQuantityList[$basketCode][$shipmentItemIndex];
365 $shipmentFieldsList[] = array(
366 'BASKET_ITEM' => $basketItem,
367 'BASKET_CODE' => $basketItem->getBasketCode(),
368 'PRODUCT_ID' => $productId,
369 'QUANTITY' => abs($quantity),
370 'DEDUCTED' => $quantity < 0,
371 'RESERVED' => $reserveValue,
377 $shipmentFieldsList[] = array(
378 'BASKET_ITEM' => $basketItem,
379 'BASKET_CODE' => $basketItem->getBasketCode(),
380 'PRODUCT_ID' => $productId,
381 'QUANTITY' => abs($quantity),
382 'DEDUCTED' => $quantity < 0,
383 'RESERVED' => $needReserved,
389 $provider = $basketItem->getProvider();
391 $storeDataList = array();
393 if (!empty($productData[
'STORE_DATA_LIST']))
395 $storeDataList = $productData[
'STORE_DATA_LIST'];
398 foreach ($shipmentFieldsList as $shipFields)
400 $r = Sale\Provider::shipProductData($provider, $shipFields, $storeDataList);
403 $productId = $basketItem->getProductId();
404 $resultList[$productId] =
true;
406 elseif ($r->hasWarnings())
408 $result->addWarnings($r->getWarnings());
412 $result->addErrors($r->getErrors());
418 if (!empty($resultList))
422 'SHIPPED_PRODUCTS_LIST' => $resultList
436 public function reserve(array $products)
439 $resultList = array();
441 foreach ($products as $productId => $productData)
443 $productQuantity = 0;
444 if (array_key_exists(
'QUANTITY', $productData))
446 $productQuantity = $productData[
'QUANTITY'];
448 elseif (!empty($productData[
'QUANTITY_LIST']))
450 foreach ($productData[
'QUANTITY_LIST'] as $basketCode => $quantity)
452 $productQuantity += $quantity;
460 $r = Sale\Provider::reserveProduct($this->
getProviderClass(), $productId, $productQuantity);
463 $fields = $r->getData();
466 $resultList[$productId] = array(
467 'QUANTITY_RESERVED' => $fields[
'QUANTITY']
473 $result->addErrors($r->getErrors());
476 if ($r->hasWarnings())
478 $result->addWarnings($r->getWarnings());
482 if (!empty($resultList))
486 'RESERVED_PRODUCTS_LIST' => $resultList
499 private static function getShipmentItemListFromProducts(array $products)
501 $resultList = array();
503 foreach ($products as $productData)
506 foreach ($productData[
'SHIPMENT_ITEM_LIST'] as $shipmentItem)
508 if (!array_key_exists($shipmentItem->getInternalIndex(), $resultList))
510 $resultList[$shipmentItem->getInternalIndex()] = $shipmentItem;
525 private static function createListFromTryShipmentResult($shipmentItemList,
Sale\
Result $result)
527 if (!$result->isSuccess())
532 $basketCodeList = array();
533 $basketItemList = array();
534 foreach ($shipmentItemList as $shipmentItem)
536 $basketItem = $shipmentItem->getBasketItem();
537 $basketCodeList[$shipmentItem->getInternalIndex()] = $basketItem->getBasketCode();
539 $basketItemList[$basketItem->getBasketCode()] = $basketItem;
542 $resultList = array();
544 $data = $result->getData();
551 foreach ($data as $basketCode => $resultTryShipment)
553 if (!isset($basketItemList[$basketCode]))
558 $basketItem = $basketItemList[$basketCode];
560 $resultList[$basketItem->getProductId()] = $resultTryShipment->isSuccess();
625 $providerName =
null;
630 $providerName = $reflect->getName();
633 return Sale\Provider::getProductDataByList($products, $providerName, array(
'PRICE',
'QUANTITY',
'CHECK_DISCOUNT',
'AVAILABLE_QUANTITY',
'COUPONS'), $this->
getContext());
643 return Sale\Provider::getBundleChildItems($this->
getProviderClass(), $products);
652 $parameters = (isset(
$context[
'SITE_ID'])? array(
"SITE_ID" =>
$context[
'SITE_ID']) : array());
653 return Sale\Provider::getStoresCount($parameters);
663 public function deliver(array $products)
665 Main\Loader::includeModule(
'catalog');
667 $resultList = array();
669 $productOrderList = Catalog\Product\CatalogProvider::createOrderListFromProducts($products);
670 foreach ($products as $productId => $productData)
672 if (empty($productOrderList[$productId]))
678 foreach ($productOrderList[$productId] as $order)
680 $resultList[$productId] =
false;
681 if (!empty($productData[
'SHIPMENT_ITEM_LIST']))
684 $quantityList = array();
686 if (isset($productData[
'QUANTITY_LIST']))
688 $quantityList = $productData[
'QUANTITY_LIST'];
694 foreach ($productData[
'SHIPMENT_ITEM_LIST'] as $shipmentIndex => $shipmentItem)
698 $shipmentItemCollection = $shipmentItem->getCollection();
699 if (!$shipmentItemCollection)
704 $shipment = $shipmentItemCollection->getShipment();
706 $basketItem = $shipmentItem->getBasketItem();
708 $basketCode = $basketItem->getBasketCode();
711 if (isset($quantityList[$basketCode]))
713 $quantity = $quantityList[$basketCode];
717 "PRODUCT_ID" => $productId,
718 "USER_ID" => $order->getUserId(),
719 "PAID" => $order->isPaid() ?
'Y' :
'N',
720 "ORDER_ID" => $order->getId(),
722 "BASKET_CODE" => $basketCode,
723 "CALLBACK_FUNC" => $basketItem->getField(
'CALLBACK_FUNC'),
724 "MODULE" => $basketItem->getField(
'MODULE'),
725 "ALLOW_DELIVERY" => $shipment->getField(
'ALLOW_DELIVERY'),
726 "QUANTITY" => $quantity,
732 $resultData = $r->getData();
734 if (array_key_exists($productId, $resultData))
747 if (!empty($resultList))
751 'DELIVER_PRODUCTS_LIST' => $resultList
768 Main\Loader::includeModule(
'catalog');
769 $result =
new Sale\Result();
770 $resultList = array();
772 $productOrderList = Catalog\Product\CatalogProvider::createOrderListFromProducts($products);
773 foreach ($products as $productId => $productData)
775 $productParamsList = array();
776 if (!empty($productOrderList[$productId]))
779 foreach ($productOrderList[$productId] as $order)
781 $hash = $order->getUserId().
"|".$order->getSiteId();
783 if (!isset($productParamsList[$hash]))
785 $productParamsList[$hash] = array(
786 'PRODUCT_ID' => $productId,
787 'USER_ID' => $order->getUserId(),
788 'SITE_ID' => $order->getSiteId(),
796 $hash = $USER->getId() .
"|" .SITE_ID;
797 if (!isset($productParamsList[$hash]))
799 $productParamsList[$hash] = array(
800 'PRODUCT_ID' => $productId,
801 'USER_ID' => $USER->getId(),
802 'SITE_ID' => SITE_ID,
807 foreach ($productParamsList as $productParams)
809 $r = Sale\Provider::getViewProduct($this->
getProviderClass(), $productParams);
812 $resultData = $r->getData();
813 if (array_key_exists($productId, $resultData))
824 if (!empty($resultList))
828 'VIEW_PRODUCTS_LIST' => $resultList
844 Main\Loader::includeModule(
'catalog');
845 $result =
new Sale\Result();
846 $resultList = array();
849 $productOrderList = Catalog\Product\CatalogProvider::createOrderListFromProducts($products);
850 foreach ($products as $productId => $productData)
852 $productParamsList = array();
853 if (!empty($productOrderList[$productId]))
856 foreach ($productOrderList[$productId] as $order)
858 $hash = $order->getSiteId();
860 if (!isset($productParamsList[$hash]))
862 $productParamsList[$hash] = array(
863 'PRODUCT_ID' => $productId,
864 'SITE_ID' => $order->getSiteId(),
873 if (!isset($productParamsList[$hash]))
875 $productParamsList[$hash] = array(
876 'PRODUCT_ID' => $productId,
877 'SITE_ID' => SITE_ID,
882 foreach ($productParamsList as $productParams)
887 $resultData = $r->getData();
888 if (array_key_exists($productId, $resultData))
898 if (!empty($resultList))
902 'PRODUCT_STORES_LIST' => $resultList
916 public function checkBarcode(array $items)
918 $result =
new Sale\Result();
919 $resultList = array();
921 foreach ($items as $productId => $barcodeParams)
927 $r = Sale\Provider::checkBarcode($this->
getProviderClass(), $barcodeParams);
930 $resultData = $r->getData();
931 if (!empty($resultData) && array_key_exists($productId, $resultData))
933 $resultList[$barcodeParams[
'BARCODE']] = $resultData[
$productId];
938 $result->addErrors($r->getErrors());
941 if ($r->hasWarnings())
943 $result->addWarnings($r->getWarnings());
947 if (!empty($resultList))
951 'RESERVED_PRODUCTS_LIST' => $resultList
965 public function recurring(array $products)
968 Main\Loader::includeModule(
'catalog');
970 $result =
new Sale\Result();
971 $resultList = array();
973 $productOrderList = Catalog\Product\CatalogProvider::createOrderListFromProducts($products);
974 foreach ($products as $productId => $productData)
976 $productParamsList = array();
977 if (!empty($productOrderList[$productId]))
980 foreach ($productOrderList[$productId] as $order)
982 $hash = $order->getUserId();
984 if (!isset($productParamsList[$hash]))
986 $productParamsList[$hash] = array(
987 'PRODUCT_ID' => $productId,
988 'USER_ID' => $order->getUserId(),
996 $hash = $USER->getId();
997 if (!isset($productParamsList[$hash]))
999 $productParamsList[$hash] = array(
1000 'PRODUCT_ID' => $productId,
1001 'USER_ID' => $USER->getId(),
1006 foreach ($productParamsList as $productParams)
1008 $r = Sale\Provider::recurringProduct($this->
getProviderClass(), $productParams);
1009 if ($r->isSuccess())
1011 $resultData = $r->getData();
1013 if (array_key_exists($productId, $resultData))
1023 if (!empty($resultList))
1027 'RECURRING_PRODUCTS_LIST' => $resultList
1045 $resultData = $result->getData();
1046 if (!array_key_exists(
'AVAILABLE_QUANTITY_LIST', $resultData))
1051 $quantityByStore = [];
1053 foreach ($products as $productId => $item)
1055 $quantityByStore[$productId] = $this->distributeQuantityByStore(
1056 $item[
'QUANTITY_LIST_BY_STORE'],
1057 $resultData[
'AVAILABLE_QUANTITY_LIST'][$productId]
1061 return $result->setData([
'AVAILABLE_QUANTITY_LIST_BY_STORE' => $quantityByStore]);
1064 private function distributeQuantityByStore($needQuantityList, $availableQuantity) : array
1068 foreach ($needQuantityList as $quantityByStore)
1070 if (is_array($quantityByStore))
1072 foreach ($quantityByStore as $storeId => $quantity)
1074 if (abs($quantity) < abs($availableQuantity))
1076 $result[$storeId] = $quantity;
1077 $availableQuantity -= $quantity;
1081 $result[$storeId] = $availableQuantity;
1082 $availableQuantity = 0;