• Модуль: catalog
  • Путь к файлу: ~/bitrix/modules/catalog/lib/discountcoupon.php
  • Класс: BitrixCatalogDiscountCouponTable
  • Вызов: DiscountCouponTable::saveApplied
static function saveApplied($coupons, $userId, MainTypeDateTime $currentTime)
{
	$currentTimestamp = $currentTime->getTimestamp();
	if ($userId === null || (int)$userId == 0)
		return false;
	if (!is_array($coupons))
		$coupons = array($coupons);
	if (empty($coupons))
		return false;
	MainTypeCollection::normalizeArrayValuesByInt($coupons);
	if (empty($coupons))
		return false;

	$deactivateCoupons = array();
	$multiCoupons = array();
	$couponIterator = self::getList(array(
		'select' => array(
			'ID', 'COUPON', 'DISCOUNT_ID', 'TYPE', 'ACTIVE',
			'DISCOUNT_ACTIVE' => 'DISCOUNT.ACTIVE',
			'DISCOUNT_ACTIVE_FROM' => 'DISCOUNT.ACTIVE_FROM', 'DISCOUNT_ACTIVE_TO' => 'DISCOUNT.ACTIVE_TO'
		),
		'filter' => array('@ID' => $coupons, '=ACTIVE' => 'Y'),
		'order' => array('ID' => 'ASC')
	));
	while ($existCoupon = $couponIterator->fetch())
	{
		if ($existCoupon['DISCOUNT_ACTIVE'] != 'Y')
			continue;
		if (
			($existCoupon['DISCOUNT_ACTIVE_FROM'] instanceof MainTypeDateTime && $existCoupon['DISCOUNT_ACTIVE_FROM']->getTimestamp() > $currentTimestamp)
			||
			($existCoupon['DISCOUNT_ACTIVE_TO'] instanceof MainTypeDateTime && $existCoupon['DISCOUNT_ACTIVE_TO']->getTimestamp() < $currentTimestamp)
		)
			continue;

		if (
			$existCoupon['TYPE'] == self::TYPE_ONE_ROW
			|| $existCoupon['TYPE'] == self::TYPE_ONE_ORDER
		)
		{
			$deactivateCoupons[$existCoupon['COUPON']] = $existCoupon['ID'];
		}
		else
		{
			$multiCoupons[$existCoupon['COUPON']] = $existCoupon['ID'];
		}
	}
	unset($existCoupon, $couponIterator);
	if (!empty($deactivateCoupons) || !empty($multiCoupons))
	{
		$conn = Application::getConnection();
		$helper = $conn->getSqlHelper();
		$tableName = $helper->quote(self::getTableName());
		if (!empty($deactivateCoupons))
		{
			$conn->queryExecute(
				'update '.$tableName.' set '.$helper->quote('ACTIVE').' = 'N', '.$helper->quote('DATE_APPLY').' = '.$helper->getCurrentDateTimeFunction().
				' where '.$helper->quote('ID').' in ('.implode(',', $deactivateCoupons).')'
			);
		}
		if (!empty($multiCoupons))
		{
			$conn->queryExecute(
				'update '.$tableName.' set '.$helper->quote('DATE_APPLY').' = '.$helper->getCurrentDateTimeFunction().
				' where '.$helper->quote('ID').' in ('.implode(',', $multiCoupons).')'
			);
		}
		unset($tableName, $helper);
	}
	return array(
		'DEACTIVATE' => $deactivateCoupons,
		'INCREMENT' => $multiCoupons
	);
}