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

		$setQuantityList = array();
		$productQuantity = self::getTotalAmountFromQuantityList($productData);
		$isNeedShip = ($productQuantity < 0);

		$quantityByStore = self::getQuantityDataFromStore($productData);
		$needQuantityList = $quantityByStore['AMOUNT'];

		if (empty($needQuantityList))
		{
			$autoShipStore = static::getAutoShipStoreData($productData, $productStoreDataList);

			if (!empty($autoShipStore))
			{
				$needQuantityList[$autoShipStore['STORE_ID']] = ($productQuantity > $autoShipStore['AMOUNT'] ? $autoShipStore['AMOUNT'] : abs($productQuantity));

				$shipmentItemList = $productData['SHIPMENT_ITEM_LIST'];
				/** @var SaleShipmentItem $shipmentItem */
				foreach ($shipmentItemList as $index => $shipmentItem)
				{
					$shipmentItemStoreCollection = $shipmentItem->getShipmentItemStoreCollection();
					if ($shipmentItemStoreCollection && $shipmentItemStoreCollection->count() === 0)
					{
						$item = $shipmentItemStoreCollection->createItem($shipmentItem->getBasketItem());
						$item->setField('STORE_ID', $autoShipStore['STORE_ID']);
						$item->setField('QUANTITY', abs($productData['SHIPMENT_ITEM_QUANTITY_LIST'][$index]));
					}
				}
			}
		}

		if (!empty($productStoreDataList))
		{
			$isReservationEnabled = MainConfigOption::get("sale", "product_reserve_condition") != "S";
			$compileReserve = self::getCompileReserve($productData);
			foreach ($productStoreDataList as $storeId => $productStoreData)
			{
				$productId = $productStoreData['PRODUCT_ID'];

				if ($isNeedShip && (isset($needQuantityList[$storeId]) && $productStoreData['AMOUNT'] < $needQuantityList[$storeId]))
				{
					$result->addError(
						new SaleResultError(
							MainLocalizationLoc::getMessage(
								'DDCT_DEDUCTION_QUANTITY_STORE_ERROR_2',
								array_merge(
									self::getProductCatalogInfo($productId),
									[
										'#STORE_NAME#' => CCatalogStoreControlUtil::getStoreName($storeId),
										'#STORE_ID#' => $storeId,
										'#PRODUCT_ID#' => $productId,
									]
								)
							), 'DDCT_DEDUCTION_QUANTITY_STORE_ERROR'
						)
					);
				}
				else
				{
					$storeConfig = self::getUpdateStoreConfig(
						$storeId,
						$needQuantityList,
						$compileReserve,
						[
							'RESERVATION_ENABLED' => $isReservationEnabled,
						]
					);
					if (!$storeConfig['AMOUNT'] && !$storeConfig['QUANTITY_RESERVED'])
					{
						continue;
					}

					$storeUpdate = [];
					if ($storeConfig['AMOUNT'])
					{
						$setQuantity = $productQuantity;

						if (isset($needQuantityList[$storeId]))
						{
							$setQuantity = ($isNeedShip ? -1 : 1) * $needQuantityList[$storeId];
						}

						$storeUpdate['AMOUNT'] = $productStoreData['AMOUNT'] + $setQuantity;
						$storeUpdate['DELTA'] = $setQuantity;
						$storeUpdate['OLD_AMOUNT'] = $productStoreData['AMOUNT'];
						unset($setQuantity);
					}
					if ($storeConfig['QUANTITY_RESERVED'])
					{
						$setReserveQuantity = 0;
						if (isset($needQuantityList[$storeId]))
						{
							$setReserveQuantity = ($isNeedShip ? -1 : 1) * $needQuantityList[$storeId];
						}
						if (isset($quantityByStore['QUANTITY_RESERVED'][$storeId]))
						{
							$setReserveQuantity = ($isNeedShip ? -1 : 1) * $quantityByStore['QUANTITY_RESERVED'][$storeId];
						}
						if ($setReserveQuantity != 0)
						{
							$storeUpdate['QUANTITY_RESERVED'] = $productStoreData['QUANTITY_RESERVED']
								+ $setReserveQuantity;
							$storeUpdate['OLD_QUANTITY_RESERVED'] = $productStoreData['QUANTITY_RESERVED'];
							$storeUpdate['QUANTITY_RESERVED_DELTA'] = $setReserveQuantity;
						}
						unset($setReserveQuantity);
					}
					if (!empty($storeUpdate))
					{
						$setQuantityList[$productStoreData['ID']] = $storeUpdate;
					}
					unset($storeUpdate, $storeConfig);
				}
			}
		}

		if (!empty($setQuantityList))
		{
			$result->addData(
				array(
					Base::FLAT_QUANTITY_LIST => $setQuantityList
				)
			);
		}

		return $result;
	}