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

	$resultList = array();
	$bundleIndexList = static::getBundleIndexFromShipmentItemCollection($shipmentItemList);

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

	}

	$shipmentItemParentsList = array();

	$tryShipProductList = array();

	/** @var ShipmentItem $shipmentItem */
	foreach ($shipmentItemList as $shipmentItem)
	{
		$itemIndex = $shipmentItem->getInternalIndex();
		$basketItem = $shipmentItem->getBasketItem();
		$providerName = $basketItem->getProviderName();

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

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

		$shipmentItemParentsList[$itemIndex] = array(
			'BASKET_ITEM' => $basketItem,
			'SHIPMENT' => $shipment,
			'SHIPMENT_ITEM_COLLECTION' => $shipmentItemCollection,
		);

		$needShip = $shipment->needShip();
		if ($needShip === null)
			continue;


		if ($providerName && array_key_exists("IBXSaleProductProvider", class_implements($providerName)))
		{
			$basketItem = $shipmentItem->getBasketItem();
			if (!$basketItem)
			{
				throw new ObjectNotFoundException('Entity "BasketItem" not found');
			}

			if ($basketItem->isBundleParent())
			{
				continue;
			}

			$basketCode = $basketItem->getBasketCode();
			$quantity = $shipmentItem->getQuantity();
			$basketStoreData = array();

			$resultProduct = new Result();

			if (Configuration::useStoreControl())
			{
				if (!empty($storeDataList) && is_array($storeDataList)
					&& isset($storeDataList[$basketCode]))
				{
					$basketStoreData = $storeDataList[$basketCode];
				}

				if (!empty($basketStoreData))
				{
					$allBarcodeQuantity = 0;
					foreach($basketStoreData as $basketShipmentItemStore)
					{
						$allBarcodeQuantity += $basketShipmentItemStore['QUANTITY'];
					}

					if ($quantity > $allBarcodeQuantity)
					{
						$resultProduct->addError(new ResultError(Loc::getMessage('SALE_PROVIDER_SHIPMENT_SHIPPED_LESS_QUANTITY', array(
							'#PRODUCT_NAME#' => $basketItem->getField('NAME')
						)), 'SALE_PROVIDER_SHIPMENT_SHIPPED_LESS_QUANTITY'));
					}
					elseif ($quantity < $allBarcodeQuantity)
					{
						$resultProduct->addError(new ResultError(Loc::getMessage('SALE_PROVIDER_SHIPMENT_SHIPPED_MORE_QUANTITY', array(
							'#PRODUCT_NAME#' => $basketItem->getField('NAME')
						)), 'SALE_PROVIDER_SHIPMENT_SHIPPED_MORE_QUANTITY'));
					}
				}

			}

			if ($resultProduct->isSuccess())
			{

				if ($needShip === true)
				{
					if (method_exists($providerName, 'tryShipmentProduct'))
					{
						/** @var Result $resultProductData */
						$resultProduct = $providerName::tryShipmentProduct($basketItem, $basketItem->getField('RESERVED'), $basketStoreData, $quantity);
					}
				}
				else
				{
					if (method_exists($providerName, 'tryUnshipmentProduct'))
					{
						/** @var Result $resultProductData */
						$resultProduct = $providerName::tryUnshipmentProduct($basketItem->getProductId());
					}
				}
			}

			$resultList[$basketCode] = $resultProduct;

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

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

			$shipmentItemParentsList[$itemIndex]['SHIPMENT_COLLECTION'] = $shipmentCollection;
			$shipmentItemParentsList[$itemIndex]['ORDER'] = $order;

			$pool = InternalsPoolQuantity::getInstance($order->getInternalId());

			$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);

			$shipmentProductData = $creator->createItemForShip($shipmentItem);
			$creator->addProductData($shipmentProductData);

			$r = $creator->tryShip();
			if ($r->isSuccess())
			{
				if ($r->hasWarnings())
				{
					$result->addWarnings($r->getWarnings());
				}
				else
				{
					$data = $r->getData();
					if (array_key_exists('TRY_SHIP_PRODUCTS_LIST', $data))
					{
						$tryShipProductList = $data['TRY_SHIP_PRODUCTS_LIST'] + $tryShipProductList;
						$creator->setItemsResultAfterTryShip($pool, $tryShipProductList);
					}
				}
			}
			else
			{
				$result->addWarnings($r->getErrors());
			}
		}
	}

	if (!empty($resultList)
		&& !empty($bundleIndexList) && is_array($bundleIndexList))
	{

		foreach ($bundleIndexList as $bundleParentBasketCode => $bundleChildList)
		{
			foreach($bundleChildList as $bundleChildBasketCode)
			{
				if (!isset($resultList[$bundleChildBasketCode]))
				{
					if (!isset($resultList[$bundleParentBasketCode]))
					{
						$resultList[$bundleParentBasketCode] = new Result();
					}

					$resultList[$bundleParentBasketCode]->addError(new ResultError('Bundle child item not found', 'SALE_PROVIDER_SHIPMENT_SHIPPED_BUNDLE_CHILD_ITEM_NOT_FOUND'));
				}

			}
		}

	}

	if (!empty($resultList))
	{

		$hasErrors = false;

		/** @var ShipmentItem $shipmentItem */
		foreach ($shipmentItemList as $shipmentItem)
		{
			$itemIndex = $shipmentItem->getInternalIndex();

			/** @var BasketItem $basketItem */
			$basketItem = $shipmentItemParentsList[$itemIndex]['BASKET_ITEM'];

			if (isset($resultList[$basketItem->getBasketCode()]) && !$resultList[$basketItem->getBasketCode()]->isSuccess())
			{
				$hasErrors = true;
				break;
			}
		}

		if (!$hasErrors)
		{
			/** @var ShipmentItem $shipmentItem */
			foreach ($shipmentItemList as $shipmentItem)
			{
				$itemIndex = $shipmentItem->getInternalIndex();

				/** @var BasketItem $basketItem */
				$basketItem = $shipmentItemParentsList[$itemIndex]['BASKET_ITEM'];

				$productId = $shipmentItem->getProductId();

				if (isset($resultList[$basketItem->getBasketCode()]) && $resultList[$basketItem->getBasketCode()]->isSuccess())
				{
					/** @var Shipment $shipment */
					$shipment = $shipmentItemParentsList[$itemIndex]['SHIPMENT'];

					/** @var Order $order */
					$order = $shipmentItemParentsList[$itemIndex]['ORDER'];

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

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

						$shipmentItemParentsList[$itemIndex]['SHIPMENT_COLLECTION'] = $shipmentCollection;
						$shipmentItemParentsList[$itemIndex]['ORDER'] = $order;
					}

					$needShip = $shipment->needShip();

					static::addQuantityPoolItem($order->getInternalId(), $basketItem, ($needShip? -1 : 1) * $shipmentItem->getQuantity());

					if ($needShip)
					{
						$shipmentItem->setFieldNoDemand("RESERVED_QUANTITY", 0);
					}


					$foundItem = false;
					$poolItems = InternalsItemsPool::get($order->getInternalId(), $productId);
					if (!empty($poolItems))
					{
						/** @var ShipmentItem $poolItem */
						foreach ($poolItems as $poolItem)
						{
							if ($poolItem->getInternalIndex() == $shipmentItem->getInternalIndex())
							{
								$foundItem = true;
								break;
							}
						}
					}

					if (!$foundItem)
					{
						InternalsItemsPool::add($order->getInternalId(), $productId, $shipmentItem);
					}

				}
			}
		}

		$result->setData($resultList);
	}

	return $result;
}