- Модуль: catalog
- Путь к файлу: ~/bitrix/modules/catalog/lib/product/catalogprovider.php
- Класс: BitrixCatalogProductCatalogProvider
- Вызов: CatalogProvider::checkProductQuantityInStore
protected function checkProductQuantityInStore(array $products)
{
$result = new SaleResult();
$resultList = array();
$productQuantityList = array();
$usedProductStoreQuantity = [];
$productStoreDataList = self::loadCurrentProductStores(array_keys($products));
foreach ($products as $productId => $productData)
{
if (empty($productData['CATALOG']))
continue;
if (
isset($productData['PRODUCT'])
&& !$productData['PRODUCT']['USED_STORE_INVENTORY'] //product types without stores
)
{
continue;
}
$productStoreData = $productStoreDataList[$productId] ?? [];
$storeDataList = $productData['STORE_DATA_LIST'];
if (!empty($storeDataList))
{
foreach ($storeDataList as $barcodeList)
{
foreach($barcodeList as $storeId => $storeDataValue)
{
if (!empty($storeDataValue))
{
if (
!isset($productStoreData[$storeId])
|| ($productStoreData[$storeId]["AMOUNT"] < $storeDataValue["QUANTITY"])
)
{
$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,
]
)
), 'DDCT_DEDUCTION_QUANTITY_STORE_ERROR'
)
);
}
else
{
if (!isset($productQuantityList[$productId]))
{
$productQuantityList[$productId] = 0;
}
$productQuantityList[$productId] += $storeDataValue["QUANTITY"];
if (!isset($usedProductStoreQuantity[$productId]))
{
$usedProductStoreQuantity[$productId] = [];
}
$usedProductStoreQuantity[$productId][$storeId] = true;
$r = static::checkProductBarcodes($productData, $productStoreData[$storeId], $storeDataValue);
if ($r->isSuccess())
{
if (!array_key_exists($productId, $resultList))
{
$resultList[$productId] = true;
}
}
else
{
$result->addErrors($r->getErrors());
}
}
}
else
{
if (!array_key_exists($productId, $resultList))
{
$resultList[$productId] = true;
}
}
}
}
}
else
{
$resultList[$productId] = true;
if (!isset($productQuantityList[$productId]))
{
$productQuantityList[$productId] = 0;
}
if (
!empty($productData[Base::FLAT_QUANTITY_LIST])
&& is_array($productData[Base::FLAT_QUANTITY_LIST])
)
{
$productQuantityList[$productId] = array_sum(
$productData[Base::FLAT_QUANTITY_LIST]
);
}
}
}
if (!empty($productQuantityList))
{
foreach ($productQuantityList as $amountProductId => $amountValue)
{
$product = $products[$amountProductId];
$catalogData = $product['CATALOG'];
$catalogQuantity = self::getTotalAmountFromPriceList($catalogData);
$catalogReservedQuantity = (float)$catalogData['QUANTITY_RESERVED'];
if ($product[Base::FLAT_RESERVED_QUANTITY_LIST][$product['BASKET_CODE']] > 0)
{
$catalogQuantity += $catalogReservedQuantity;
}
else
{
$unusedReserve = 0.0;
if (isset($usedProductStoreQuantity[$amountProductId]))
{
$usedProductStores = $usedProductStoreQuantity[$amountProductId];
$productStores = $productStoreDataList[$amountProductId];
foreach (array_keys($productStores) as $storeId)
{
if (isset($usedProductStores[$storeId]))
{
continue;
}
$unusedReserve += $productStores[$storeId]['QUANTITY_RESERVED'];
}
unset($storeId);
unset($productStores, $usedProductStores);
}
$catalogQuantity += $unusedReserve;
unset($unusedReserve);
}
if ($amountValue > $catalogQuantity)
{
$result->addError(
new SaleResultError(
MainLocalizationLoc::getMessage(
"DDCT_DEDUCTION_SHIPMENT_QUANTITY_NOT_ENOUGH",
self::getProductCatalogInfo($amountProductId)
), "SALE_PROVIDER_SHIPMENT_QUANTITY_NOT_ENOUGH"
)
);
}
}
}
if (!empty($resultList))
{
$result->setData(
array(
'PRODUCTS_LIST_REQUIRED_QUANTITY_IN_STORE' => $resultList,
)
);
}
return $result;
}