- Модуль: catalog
- Путь к файлу: ~/bitrix/modules/catalog/lib/product/catalogprovider.php
- Класс: BitrixCatalogProductCatalogProvider
- Вызов: CatalogProvider::getAvailableQuantityByStore
public function getAvailableQuantityByStore(array $products): SaleResult
{
$result = new SaleResult();
$resultList = [];
$isGotQuantityDataList = [];
foreach ($products as $productId => $productData)
{
$catalogAvailableQuantity = QuantityControl::getAvailableQuantity($productId);
$catalogQuantity = QuantityControl::getQuantity($productId);
if ($catalogQuantity === null || $catalogAvailableQuantity === null)
{
continue;
}
$productQuantity = self::getStoreAmountFromQuantityList($productData);
if ($productQuantity === null)
{
continue;
}
if ($catalogAvailableQuantity < array_sum($productQuantity))
{
continue;
}
$isGotQuantityDataList[$productId] = true;
$resultList[$productId] = $productQuantity;
}
if (count($resultList) != count($products))
{
if ($this->isExistsCatalogData($products))
{
$items = $products;
}
else
{
$items = $this->createProductsListWithCatalogData($products);
}
foreach ($items as $productId => $productData)
{
if (isset($isGotQuantityDataList[$productId]))
{
continue;
}
if (empty($productData['CATALOG']) || !is_array($productData['CATALOG']))
{
continue;
}
$catalogData = $productData['CATALOG'];
$productQuantity = self::getStoreAmountFromQuantityList($productData);
if ($productQuantity === null)
{
$resultList[$productId] = [];
continue;
}
$resultList[$productId] = $productQuantity;
$catalogQuantity = self::getTotalAmountFromPriceList($catalogData, false);
QuantityControl::setQuantity($productId, $catalogQuantity);
if ($catalogData['CHECK_QUANTITY'])
{
$totalReservedQuantity = 0;
$reservedQuantity = self::getStoreReservedQuantityFromProduct($productData);
if ($reservedQuantity !== null)
{
$totalReservedQuantity = array_sum($reservedQuantity);
}
$needQuantity = (array_sum($productQuantity) - $totalReservedQuantity);
if ($catalogQuantity < $needQuantity)
{
$limitQuantity = $catalogQuantity;
$availableList = $resultList[$productId];
arsort($availableList, SORT_NUMERIC);
foreach (array_keys($availableList) as $storeId)
{
$storeQuantity = $resultList[$productId][$storeId];
if ($limitQuantity > $storeQuantity)
{
$limitQuantity -= $storeQuantity;
}
else
{
$storeQuantity = $limitQuantity;
if ($limitQuantity > 0)
{
$limitQuantity = 0;
}
}
$resultList[$productId][$storeId] = $storeQuantity;
}
}
}
}
}
if (!empty($resultList))
{
$result->setData([
Base::STORE_AVAILABLE_QUANTITY_LIST => $resultList,
]);
}
return $result;
}