• Модуль: support
  • Путь к файлу: ~/bitrix/modules/support/classes/general/coupons.php
  • Класс: CSupportSuperCoupon
  • Вызов: CSupportSuperCoupon::Generate
static function Generate($arParams = array())
{
	global $DB, $USER, $APPLICATION;
	if (!is_array($arParams))
	{
		$arParams = array();
	}

	if(array_key_exists('KEY_FORMAT', $arParams) && $arParams['KEY_FORMAT'] <> '')
	{
		$couponFormat = $arParams['KEY_FORMAT'];
	}
	else 
	{ 
		$couponFormat = COption::GetOptionString('support', 'SUPERTICKET_COUPON_FORMAT');
	}
	
	$count = array_key_exists('COUNT_TICKETS', $arParams) ? intval($arParams['COUNT_TICKETS']) : 5;
	if ($count <= 0)
	{
		$count = 5;
	}
	
	$slaID = array_key_exists('SLA_ID', $arParams) ? $arParams['SLA_ID'] : COption::GetOptionString("support", 'SUPERTICKET_DEFAULT_SLA');
	$slaID = intval($slaID);
	if ($slaID <= 0)
	{
		$slaID = false;
	}
	
	$DB->StartTransaction();
	for ($i = 0; $i < 100; ++$i)
	{
		$coupon = preg_replace_callback('|#|'.BX_UTF_PCRE_MODIFIER, array('CSupportSuperCoupon', '_getrandsymbol'), $couponFormat);
		$rs = CSupportSuperCoupon::GetList(false, array('COUPON' => $coupon));
		if ($rs->Fetch())
		{
			$coupon = false;
		}
		else 
		{
			break;
		}
	}
	
	if ($coupon !== false)
	{
		$arFields = array(
			'COUPON' => $coupon,
			'COUNT_TICKETS' => $count,
			'SLA_ID' => $slaID,
			'ACTIVE_FROM' => $arParams['ACTIVE_FROM'],
			'ACTIVE_TO' => $arParams['ACTIVE_TO'],
			'ACTIVE' => $arParams['ACTIVE'],
		);

		if (CSupportSuperCoupon::Add($arFields) === false)
		{	
			$DB->Rollback();
			return false;
		}
		$DB->Commit();
	}
	else 
	{
		$DB->Rollback();
		$APPLICATION->ThrowException(GetMessage('SUP_ST_ERROR_NO_NEW_COUPON'));
	}

	return $coupon;
}