• Модуль: sale
  • Путь к файлу: ~/bitrix/modules/sale/lib/providerbase.php
  • Класс: BitrixSaleProviderBase
  • Вызов: ProviderBase::shipShipment
static function shipShipment(Shipment $shipment)
{
	$result = new Result();

	/** @var ShipmentItemCollection $shipmentItemCollection */
	if (!$shipmentItemCollection = $shipment->getShipmentItemCollection())
	{
		throw new ObjectNotFoundException('Entity "ShipmentCollection" not found');
	}

	/** @var ShipmentCollection $shipmentCollection */
	if (!$shipmentCollection = $shipment->getCollection())
	{
		throw new ObjectNotFoundException('Entity "ShipmentCollection" not found');
	}

	/** @var Order $order */
	if (!$order = $shipmentCollection->getOrder())
	{
		throw new ObjectNotFoundException('Entity "Order" not found');
	}

	$pool = InternalsPoolQuantity::getInstance($order->getInternalId());
	$quantityPool = $pool->getQuantities(InternalsPoolQuantity::POOL_QUANTITY_TYPE);
	if (empty($quantityPool))
	{
		return $result;
	}

	$reverse = false;

	$resultList = array();

	$basketList = static::getBasketFromShipmentItemCollection($shipmentItemCollection);

	$basketProviderMap = static::createProviderBasketMap($basketList, array('QUANTITY', 'RESERVED'));
	$basketProviderList = static::redistributeToProviders($basketProviderMap);
	$storeDataList = array();

	if (Configuration::useStoreControl())
	{

		/** @var Result $r */
		$r = static::getStoreDataFromShipmentItemCollection($shipmentItemCollection);
		if (!$r->isSuccess())
		{
			$result->addErrors($r->getErrors());
		}
		else
		{
			$resultStoreData = $r->getData();
			if (!empty($resultStoreData['STORE_DATA_LIST']))
			{
				$storeDataList = $resultStoreData['STORE_DATA_LIST'];
			}

		}
	}

	if (!empty($basketProviderList))
	{
		foreach ($basketProviderList as $provider => $providerBasketItemList)
		{
			if ($provider && array_key_exists("IBXSaleProductProvider", class_implements($provider)))
			{

				foreach ($providerBasketItemList as $providerBasketItem)
				{

					if ($providerBasketItem['BASKET_ITEM']->isBundleParent())
					{
						continue;
					}

					$poolQuantity = static::getQuantityPoolItem($order->getInternalId(), $providerBasketItem['BASKET_ITEM']);

					if ($poolQuantity == 0)
						continue;

					if ($providerBasketItem['BASKET_ITEM']->getField('MODULE') != '')
					{
						$shipFields = array_merge($providerBasketItem, array(
							'DEDUCTION' => ($poolQuantity < 0)
						));

						$r = static::shipProductData($provider, $shipFields, $storeDataList);

						if (!$r->isSuccess())
						{
							$result->addErrors($r->getErrors());
						}
						$resultProductData = $r->getData();

					}
					else
					{
						$resultProductData['RESULT'] = true;
					}

					$resultList[$providerBasketItem['BASKET_CODE']] = $resultProductData;

					if (array_key_exists("RESULT", $resultProductData)
						&& $resultProductData['RESULT'] === false && $poolQuantity < 0)
					{
						$reverse = true;
						break;
					}

				}

			}
			elseif (class_exists($provider))
			{
				$context = array(
					'SITE_ID' => $order->getSiteId(),
					'CURRENCY' => $order->getCurrency(),
				);

				if ($order->getUserId() > 0)
				{
					$context['USER_ID'] = $order->getUserId();
				}
				else
				{
					global $USER;
					$context['USER_ID'] = $USER->getId();
				}

				$creator = InternalsProviderCreator::create($context);
				/** @var ShipmentItem $shipmentItem */
				foreach ($shipmentItemCollection as $shipmentItem)
				{
					$basketItem = $shipmentItem->getBasketItem();
					$providerClass = $basketItem->getProviderEntity();

					if ($providerClass instanceof SaleProviderBase)
					{
						$shipmentProductData = $creator->createItemForShip($shipmentItem);
						$creator->addProductData($shipmentProductData);
					}
				}

				$r = $creator->ship();
				if (!$r->isSuccess())
				{
					$result->addErrors($r->getErrors());
				}

				$r = $creator->setItemsResultAfterShip($r);
				if (!$r->isSuccess())
				{
					$result->addErrors($r->getErrors());
				}
			}
		}
	}

	if ($reverse === true)
	{
		static::reverseShipment($shipment, $resultList);
	}
	else
	{
		static::setShipmentItemReserved($shipment);
	}

	return $result;
}