• Модуль: catalog
  • Путь к файлу: ~/bitrix/modules/catalog/lib/product/catalogprovider.php
  • Класс: BitrixCatalogProductCatalogProvider
  • Вызов: CatalogProvider::shipQuantityWithStoreControl
static function shipQuantityWithStoreControl(array $productData): SaleResult
	{
		$result = new SaleResult();

		$productId = (int)$productData['PRODUCT_ID'];

		$productQuantity = self::getTotalAmountFromQuantityList($productData);

		$catalogData = $productData['CATALOG'];

		$isExistsReserve = static::isExistsCommonStoreReserve($productData) && static::isReservationEnabled();
		$isNeedShip = ($productQuantity < 0);

		$productQuantity = abs($productQuantity);

		$fields = array();

		$catalogReservedQuantity = (float)$catalogData['QUANTITY_RESERVED'];
		$catalogQuantity = self::getTotalAmountFromPriceList($catalogData);

		$sumCatalogQuantity = $catalogReservedQuantity + $catalogQuantity;

		if ($isNeedShip)
		{
			if ($isExistsReserve)
			{
				if ($catalogReservedQuantity >= $productQuantity)
				{
					$fields["QUANTITY_RESERVED"] = $catalogReservedQuantity - $productQuantity;
				}
				elseif ($sumCatalogQuantity >= $productQuantity)
				{
					$fields["QUANTITY_RESERVED"] = 0;
					$fields["QUANTITY"] = $catalogQuantity - ($productQuantity - $catalogReservedQuantity);
				}
				else
				{
					$result->addError(
						new SaleResultError(
							MainLocalizationLoc::getMessage(
								"DDCT_DEDUCTION_NOT_ENOUGHT_QUANTITY_PRODUCT",
								array_merge(
									self::getProductCatalogInfo($productId),
									array("#PRODUCT_ID#" => $productId)
								)
							), "DDCT_DEDUCTION_NOT_ENOUGHT_QUANTITY_PRODUCT"
						)
					);
					return $result;
				}
			}
			else
			{
				if ($productQuantity <= $catalogQuantity)
				{
					$fields["QUANTITY"] = $catalogQuantity - $productQuantity;
				}
				elseif ($productQuantity <= $sumCatalogQuantity)
				{
					$fields["QUANTITY"] = 0;
					$fields["QUANTITY_RESERVED"] = $catalogReservedQuantity - ($productQuantity - $catalogQuantity);
				}
				else
				{
					$result->addError(
						new SaleResultError(
							MainLocalizationLoc::getMessage(
								"DDCT_DEDUCTION_NOT_ENOUGHT_QUANTITY_PRODUCT",
								array_merge(
									self::getProductCatalogInfo($productId),
									array("#PRODUCT_ID#" => $productId)
								)
							), "DDCT_DEDUCTION_NOT_ENOUGHT_QUANTITY_PRODUCT"
						)
					);
					return $result;
				}
			}
		}
		else
		{
			if ($isExistsReserve)
			{
				$fields["QUANTITY_RESERVED"] = $catalogReservedQuantity + $productQuantity;
			}
			else
			{
				$fields["QUANTITY"] = $catalogQuantity + $productQuantity;
			}
		}

		if (!$productData['PRODUCT']['USED_RESERVATION'])
		{
			if (isset($fields['QUANTITY_RESERVED']))
			{
				unset($fields['QUANTITY_RESERVED']);
			}
		}

		$isUpdated = false;
		if (!empty($fields))
		{
			$internalResult = CatalogModelProduct::update($productId, $fields);

			if ($internalResult->isSuccess())
			{
				$isUpdated = true;
				$quantityValues = array();

				if (isset($fields['QUANTITY']))
				{
					$quantityValues[QuantityControl::QUANTITY] = $fields['QUANTITY'];
					QuantityControl::resetAvailableQuantity($productId);
				}

				if (isset($fields['QUANTITY_RESERVED']))
				{
					$quantityValues[QuantityControl::RESERVED_QUANTITY] = $fields['QUANTITY_RESERVED'];
				}

				if (!empty($quantityValues))
				{
					QuantityControl::setValues($productId, $quantityValues);
				}
			}
			else
			{
				self::convertErrors($internalResult);
			}
			unset($internalResult);
		}

		$result->setData(
			array(
				'IS_UPDATED' => $isUpdated
			)
		);

		return $result;
	}