42 if (!State::isUsedInventoryManagement())
46 'Inventory management is not enabled',
54 if (!Manager::isOnecMode())
58 'Inventory management is not in 1C mode',
69 $storeIds = array_flip(
71 static fn($row) => (
int)$row[
'ID'],
74 'cache' => [
'ttl' => 86400],
79 foreach (array_chunk(
$items, self::BULK_SAVE_CHUNK_SIZE) as $chunkItems)
81 $chunkProductIds = array_unique(
83 \Closure::fromCallable(
'intval'),
84 array_column($chunkItems,
'productId')
90 $productIds = array_flip(
92 static fn($row) => (
int)$row[
'ID'],
95 ->whereIn(
'ID', $chunkProductIds)
102 foreach ($chunkItems as $item)
104 $productId = isset($item[
'productId']) ? (int)$item[
'productId'] : 0;
105 if (!isset($productIds[$productId]))
110 $storeId = isset($item[
'storeId']) ? (int)$item[
'storeId'] : 0;
116 $amount = isset($item[
'value'][
'amount']) ? (float)$item[
'value'][
'amount'] : 0;
117 $quantityReserved = isset($item[
'value'][
'quantityReserved'])
118 ? (float)$item[
'value'][
'quantityReserved']
123 'PRODUCT_ID' => $productId,
126 'QUANTITY_RESERVED' => $quantityReserved,
135 $sqls = $sqlHelper->prepareMergeMultiple(
143 foreach ($sqls as $sql)
149 $allProductIds = array_unique(
151 \Closure::fromCallable(
'intval'),
152 array_column(
$items,
'productId')
155 foreach (array_chunk($allProductIds, self::BULK_SAVE_PRODUCTS_CHUNK_SIZE) as $chunkProductIds)
157 Application::getInstance()->addBackgroundJob(
function() use ($chunkProductIds) {
158 \CCatalogStore::recalculateProductsBalances($chunkProductIds);