• Модуль: sale
  • Путь к файлу: ~/bitrix/modules/sale/lib/internals/transferprovidercompatibility.php
  • Класс: BitrixSaleInternalsTransferProviderCompatibility
  • Вызов: TransferProviderCompatibility::ship
public function ship(array $products)
{
	$basketItemList = array();
	$shipmentItemList = array();
	$basketItemShipmentItemList = array();
	$shipmentItemQuantityList = array();

	$oneReserveStatus = true;
	$needReserved = null;
	$reservedList = array();

	$resultList = array();

	foreach ($products as $productId => $itemData)
	{
		$fields = $itemData;

		if (!empty($fields['SHIPMENT_ITEM_LIST']))
		{
			/** @var SaleShipmentItem $shipmentItem */
			foreach ($fields['SHIPMENT_ITEM_LIST'] as $shipmentIndexItem => $shipmentItem)
			{
				$shipmentItemList[$shipmentIndexItem] = $shipmentItem;

				$basketItem = $shipmentItem->getBasketItem();
				if (!$basketItem)
				{
					throw new MainObjectNotFoundException('Entity "BasketItemBase" not found');
				}
//
				$basketCode = $basketItem->getBasketCode();
				$basketItemList[$basketCode] = $basketItem;
//
//					/** @var SaleShipmentItemCollection $shipmentItemCollection */
//					$shipmentItemCollection = $shipmentItem->getCollection();
//					if (!$shipmentItemCollection)
//					{
//						throw new MainObjectNotFoundException('Entity "ShipmentItemCollection" not found');
//					}
//
//					$shipment = $shipmentItemCollection->getShipment();
//					if (!$shipment)
//					{
//						throw new MainObjectNotFoundException('Entity "Shipment" not found');
//					}
//
				$basketItemShipmentItemList[$basketCode][$shipmentIndexItem] = $shipmentItem;
			}
		}

		if (!empty($fields['NEED_RESERVE_LIST']))
		{
			foreach ($fields['NEED_RESERVE_LIST'] as $shipmentItemIndex => $reserved)
			{
				$reserveValue = $reserved ? 'Y': 'N';

				$reservedList[$reserveValue][] = $shipmentItemIndex;
				if (!empty($reservedList) && !isset($reservedList[$reserveValue]))
				{
					$oneReserveStatus = false;
				}
				elseif ($needReserved === null)
				{
					$needReserved = $reserved;
				}

			}
		}

		if (!empty($fields['SHIPMENT_ITEM_QUANTITY_LIST']))
		{
			foreach ($fields['SHIPMENT_ITEM_QUANTITY_LIST'] as $shipmentItemIndex => $quantity)
			{
				$shipmentItemQuantityList[$fields['BASKET_CODE']][$shipmentItemIndex] = $quantity;
			}
		}

		$resultList[$productId] = false;
	}

	$result = new SaleResult();

	$r = static::tryShip($products);
	if (!$r->isSuccess())
	{
		$result->addErrors($r->getErrors());
	}
	elseif ($r->hasWarnings())
	{
		$result->addWarnings($r->getWarnings());
	}

	if (!empty($basketItemList))
	{
		/** @var SaleBasketItemBase $basketItem */
		foreach ($basketItemList as $basketItem)
		{
			$productId = $basketItem->getProductId();
			$productData = $products[$productId];
			$basketCode = $basketItem->getBasketCode();
			$quantity = $productData['QUANTITY_LIST'][$basketCode];

			$shipmentFieldsList = array();
			if (!$oneReserveStatus)
			{
				/**
				 * @var string $reserveValue
				 * @var SaleShipmentItem $shipmentItem
				 */
				foreach ($reservedList as $reserveValue => $shipmentItemIndexList)
				{
					$quantity = 0;
					foreach ($shipmentItemIndexList as $shipmentItemIndex)
					{
						if (isset($shipmentItemQuantityList[$basketCode][$shipmentItemIndex]))
						{
							$quantity += $shipmentItemQuantityList[$basketCode][$shipmentItemIndex];
						}
					}


					$shipmentFieldsList[] = array(
						'BASKET_ITEM' => $basketItem,
						'BASKET_CODE' => $basketItem->getBasketCode(),
						'PRODUCT_ID' => $productId,
						'QUANTITY' => abs($quantity),
						'DEDUCTED' => $quantity < 0,
						'RESERVED' => $reserveValue,
					);
				}
			}
			else
			{
				$shipmentFieldsList[] = array(
					'BASKET_ITEM' => $basketItem,
					'BASKET_CODE' => $basketItem->getBasketCode(),
					'PRODUCT_ID' => $productId,
					'QUANTITY' => abs($quantity),
					'DEDUCTED' => $quantity < 0,
					'RESERVED' => $needReserved,
				);
			}



			$provider = $basketItem->getProvider();

			$storeDataList = array();

			if (!empty($productData['STORE_DATA_LIST']))
			{
				$storeDataList = $productData['STORE_DATA_LIST'];
			}

			foreach ($shipmentFieldsList as $shipFields)
			{
				$r = SaleProvider::shipProductData($provider, $shipFields, $storeDataList);
				if ($r->isSuccess())
				{
					$productId = $basketItem->getProductId();
					$resultList[$productId] = true;
				}
				elseif ($r->hasWarnings())
				{
					$result->addWarnings($r->getWarnings());
				}
				else
				{
					$result->addErrors($r->getErrors());
				}
			}
		}
	}

	if (!empty($resultList))
	{
		$result->setData(
			array(
				'SHIPPED_PRODUCTS_LIST' => $resultList
			)
		);
	}

	return $result;
}