• Модуль: sale
  • Путь к файлу: ~/bitrix/modules/sale/lib/providerbase.php
  • Класс: BitrixSaleProviderBase
  • Вызов: ProviderBase::checkAvailableProductQuantity
static function checkAvailableProductQuantity(BasketItemBase $basketItem, $deltaQuantity)
{
	global $APPLICATION;

	$result = new Result();

	$resultProductData = array();

	$orderId = null;
	$userId = null;
	$siteId = null;

	/** @var BasketItemCollection $collection */
	$collection = $basketItem->getCollection();

	/** @var Basket $basket */
	if (!$basket = $collection->getBasket())
	{
		throw new ObjectNotFoundException('Entity "Basket" not found');
	}


	if (($order = $basket->getOrder()) !== null)
	{
		$userId = $order->getUserId();
		$siteId = $order->getSiteId();
	}

	if ($siteId === null)
	{
		$siteId = $basket->getSiteId();
	}

	$provider = $basketItem->getProvider();

	if (!empty($provider))
	{
		if (array_key_exists("IBXSaleProductProvider", class_implements($provider)))
		{
			$needQuantity = $basketItem->getQuantity();
			if ($order && $order->getId() > 0)
			{
				$needQuantity = $deltaQuantity;
			}

			$poolQuantity = 0;

			if ($order)
			{
				$poolQuantity = static::getQuantityPoolItem($order->getInternalId(), $basketItem);
			}

			$checkQuantity = $needQuantity - floatval($poolQuantity);

			$data = array(
				"PRODUCT_ID" => $basketItem->getProductId(),
				"QUANTITY"   => $checkQuantity,
				"USER_ID"   => $userId,
				"SITE_ID"   => $siteId,
				"BASKET_ID" => $basketItem->getId(),
				"CHECK_QUANTITY" => "Y",
				"AVAILABLE_QUANTITY" => "Y",
				'CHECK_PRICE' => 'N',
				'CHECK_COUPONS' => 'N',
				"SELECT_QUANTITY_TRACE" => "Y",
			);

			// TODO: !
			if ($deltaQuantity <= 0 || $checkQuantity == 0)
			{
				$result->setData(array('AVAILABLE_QUANTITY' => $deltaQuantity));
				return $result;
			}

			$hasTrustData = false;

			$trustData = static::getTrustData($siteId, $basketItem->getField('MODULE'), $basketItem->getField('PRODUCT_ID'));

			if (static::isReadTrustData() === true
				&& !empty($trustData) && is_array($trustData))
			{
				$hasTrustData = true;
				$resultProductData = $trustData;
				$productDataRequiredFields = array_merge(static::getProductDataRequiredFields(), array('AVAILABLE_QUANTITY'));
				foreach ($productDataRequiredFields as $requiredField)
				{
					if (!array_key_exists($requiredField, $resultProductData))
					{
						$hasTrustData = false;
						break;
					}
				}

				if ($hasTrustData
					&& roundEx($checkQuantity, SALE_VALUE_PRECISION) > roundEx($resultProductData["AVAILABLE_QUANTITY"], SALE_VALUE_PRECISION))
				{
					$hasTrustData = false;
				}

			}

			if(!$hasTrustData)
			{
				$APPLICATION->ResetException();
				$resultProductData = $provider::GetProductData($data);
				$ex = $APPLICATION->GetException();
				if ($ex)
				{
					$result->addWarning( new ResultWarning($ex->GetString(), $ex->GetID()) );
				}
			}

		}
		elseif (class_exists($provider))
		{
			/** @var SaleProviderBase $providerClass */
			$providerClass = new $provider();
			if ($providerClass && $providerClass instanceof SaleProviderBase)
			{
				$productId = $basketItem->getProductId();
				$products = array(
					$productId => array(
						'ITEM_CODE' => $productId,
						'BASKET_CODE' => $basketItem->getBasketCode(),
						'QUANTITY' => $deltaQuantity,
					)
				);
				$r = $providerClass->getAvailableQuantity($products);
				if ($r->isSuccess())
				{
					$resultData = $r->getData();
					if (!empty($resultData['AVAILABLE_QUANTITY_LIST']))
					{
						$resultProductData = array(
							'AVAILABLE_QUANTITY' => reset($resultData['AVAILABLE_QUANTITY_LIST'])
						);
					}
				}
			}
		}
		else
		{
			$APPLICATION->ResetException();
			$resultProductData = CSaleBasket::ExecuteCallbackFunction(
				$basketItem->getField('CALLBACK_FUNC'),
				$basketItem->getField('MODULE'),
				$basketItem->getProductId(),
				$basketItem->getQuantity()
			);

			if ($ex = $APPLICATION->GetException())
			{
				$result->addWarning( new ResultWarning($ex->GetString(), $ex->GetID()) );
			}
		}
	}
	else
	{
		$availableQuantity = $basketItem->getQuantity();
		if ($deltaQuantity <= 0)
		{
			$availableQuantity = $deltaQuantity;
		}
		$result->setData([
			'AVAILABLE_QUANTITY' => $availableQuantity,
		]);

		return $result;
	}

	$fields = array();

	if (array_key_exists('AVAILABLE_QUANTITY', $resultProductData))
	{
		$fields['AVAILABLE_QUANTITY'] = $resultProductData['AVAILABLE_QUANTITY'];
	}

	if (array_key_exists('QUANTITY_TRACE', $resultProductData))
	{
		$fields['QUANTITY_TRACE'] = ($resultProductData['QUANTITY_TRACE'] == "Y");
	}

	if (!empty($fields))
	{
		$result->setData($fields);
	}

	return $result;
}