• Модуль: catalog
  • Путь к файлу: ~/bitrix/modules/catalog/lib/model/price.php
  • Класс: BitrixCatalogModelPrice
  • Вызов: Price::recountPricesFromBase
static function recountPricesFromBase($id): bool
{
	$id = (int)$id;
	if ($id <= 0)
		return false;

	if (self::$separateSkuMode === null)
	{
		self::loadSettings();
	}

	if (empty(self::$extraList) || self::$basePriceType == 0)
	{
		return false;
	}

	$iterator = CatalogPriceTable::getList([
		'select' => ['ID', 'PRODUCT_ID', 'CATALOG_GROUP_ID', 'PRICE', 'CURRENCY', 'QUANTITY_FROM', 'QUANTITY_TO'],
		'filter' => ['=ID' => $id]
	]);
	$price = $iterator->fetch();
	unset($iterator);
	if (empty($price))
		return false;

	$price['CATALOG_GROUP_ID'] = (int)$price['CATALOG_GROUP_ID'];
	if ($price['CATALOG_GROUP_ID'] != self::$basePriceType)
		return false;

	$productId = (int)$price['PRODUCT_ID'];
	$product = Product::getCacheItem($productId, true);
	if (empty($product))
		return false;

	if (
		!self::$separateSkuMode
		&& ($product['TYPE'] == CatalogProductTable::TYPE_SKU || $product['TYPE'] == CatalogProductTable::TYPE_EMPTY_SKU)
	)
		return false;

	//TODO: replace CCurrency::GetByID to d7 cached method
	$currency = CCurrency::GetByID($price['CURRENCY']);
	if (empty($currency))
		return false;

	$filter = ORMQueryQuery::filter();
	$filter->where('PRODUCT_ID', '=', $productId);
	$filter->where('CATALOG_GROUP_ID', '!=', self::$basePriceType);
	$filter->whereIn('EXTRA_ID', array_keys(self::$extraList));
	if ($price['QUANTITY_FROM'] === null)
		$filter->whereNull('QUANTITY_FROM');
	else
		$filter->where('QUANTITY_FROM', '=', (int)$price['QUANTITY_FROM']);

	if ($price['QUANTITY_TO'] === null)
		$filter->whereNull('QUANTITY_TO');
	else
		$filter->where('QUANTITY_TO', '=', (int)$price['QUANTITY_TO']);

	$datetime = new MainTypeDateTime();
	$updatePriceTypes = [];
	$iterator = CatalogPriceTable::getList([
		'select' => ['ID', 'EXTRA_ID', 'CATALOG_GROUP_ID', 'QUANTITY_FROM', 'QUANTITY_TO'],
		'filter' => $filter
	]);
	while ($row = $iterator->fetch())
	{
		$fields = [
			'PRICE' => $price['PRICE']*self::$extraList[$row['EXTRA_ID']],
			'CURRENCY' => $price['CURRENCY'],
			'TIMESTAMP_X' => $datetime
		];
		$fields['PRICE_SCALE'] = $fields['PRICE']*$currency['CURRENT_BASE_RATE'];

		$result = CatalogPriceTable::update($row['ID'], $fields);
		if ($result->isSuccess())
			$updatePriceTypes[$row['CATALOG_TYPE_ID']] = $row['CATALOG_TYPE_ID'];
	}
	unset($result, $fields, $currency, $index);
	unset($row, $iterator);

	if (!empty($updatePriceTypes) && $product['TYPE'] == CatalogProductTable::TYPE_OFFER)
		CatalogProductSku::calculatePrice($productId, null, CatalogProductTable::TYPE_OFFER, $updatePriceTypes);

	return true;
}