• Модуль: sale
  • Путь к файлу: ~/bitrix/modules/sale/lib/discount/runtimecache/discountcache.php
  • Класс: BitrixSaleDiscountRuntimeCacheDiscountCache
  • Вызов: DiscountCache::getDiscounts
public function getDiscounts(array $discountIds, array $executeModuleFilter, $siteId, array $couponList = array())
{
	$cacheKey = 'D'.implode('_', $discountIds).'-S'.$siteId;
	if(!empty($couponList))
	{
		$cacheKey .= '-C'. implode('_', array_keys($couponList));
	}

	$cacheKey .= '-MF'.implode('_', $executeModuleFilter);
	$cacheKey = md5($cacheKey);

	if(!isset($this->discounts[$cacheKey]))
	{
		$currentList = array();

		CTimeZone::Disable();
		$currentDatetime = new DateTime();
		$discountSelect = array(
			'ID', 'PRIORITY', 'SORT', 'LAST_DISCOUNT', 'LAST_LEVEL_DISCOUNT', 'UNPACK', 'APPLICATION', 'USE_COUPONS', 'EXECUTE_MODULE',
			'NAME', 'CONDITIONS_LIST', 'ACTIONS_LIST', 'ACTIVE_FROM', 'ACTIVE_TO', 'PREDICTIONS_LIST', 'PREDICTIONS_APP',
			'PREDICTION_TEXT', 'PRESET_ID', 'CURRENCY', 'LID', 'SHORT_DESCRIPTION', 'SHORT_DESCRIPTION_STRUCTURE',
		);
		$discountFilter = array(
			'@ID' => $discountIds,
			'=LID' => $siteId,
			'@EXECUTE_MODULE' => $executeModuleFilter,
			array(
				'LOGIC' => 'OR',
				'=ACTIVE_FROM' => null,
				'<=ACTIVE_FROM' => $currentDatetime
			),
			array(
				'LOGIC' => 'OR',
				'=ACTIVE_TO' => null,
				'>=ACTIVE_TO' => $currentDatetime
			)
		);

		$couponsDiscount = array();
		if (!empty($couponList))
		{
			$iterator = DiscountCouponTable::getList(array(
				'select' => array('DISCOUNT_ID', 'COUPON'),
				'filter' => array('@DISCOUNT_ID' => $discountIds,'@COUPON' => array_keys($couponList)),
				'order' => array('DISCOUNT_ID' => 'ASC')
			));
			while ($row = $iterator->fetch())
			{
				$id = (int)$row['DISCOUNT_ID'];
				if (isset($couponsDiscount[$id]))
					continue;
				$couponsDiscount[$id] = $row['COUPON'];
			}
			unset($id, $row, $iterator);
		}

		if (empty($couponsDiscount))
		{
			$discountFilter['=USE_COUPONS'] = 'N';
		}
		else
		{
			$discountFilter[] = array(
				'LOGIC' => 'OR',
				'=USE_COUPONS' => 'N',
				array(
					'=USE_COUPONS' => 'Y',
					'@ID' => array_keys($couponsDiscount)
				)
			);
		}

		//todo remove order. It's unnecessary because we rearrange discounts after by benefit for client.
		$discountIterator = DiscountTable::getList(array(
			'select' => $discountSelect,
			'filter' => $discountFilter,
			'order' => array('PRIORITY' => 'DESC', 'SORT' => 'ASC', 'ID' => 'ASC')
		));

		while($discount = $discountIterator->fetch())
		{
			$discount['ID'] = (int)$discount['ID'];
			if ($discount['USE_COUPONS'] == 'Y')
				$discount['COUPON'] = $couponList[$couponsDiscount[$discount['ID']]];
			$discount['CONDITIONS'] = $discount['CONDITIONS_LIST'];
			$discount['ACTIONS'] = $discount['ACTIONS_LIST'];
			$discount['PREDICTIONS'] = $discount['PREDICTIONS_LIST'];
			$discount['MODULE_ID'] = 'sale';
			$discount['MODULES'] = array();
			unset($discount['ACTIONS_LIST'], $discount['CONDITIONS_LIST'], $discount['PREDICTIONS_LIST']);
			$currentList[$discount['ID']] = $discount;
		}
		unset($discount, $discountIterator);

		CTimeZone::Enable();

		if (!empty($currentList))
		{
			$discountModules = static::getDiscountModules(array_keys($currentList));
			foreach ($discountModules as $id => $modules)
				$currentList[$id]['MODULES'] = $modules;
			unset($id, $modules, $discountModules);
		}

		$this->discounts[$cacheKey] = $currentList;
	}

	return $this->discounts[$cacheKey];
}