function Generate($arParams = array()) { global $DB, $USER, $APPLICATION; if (!is_array($arParams)) { $arParams = array(); } if (array_key_exists('KEY_FORMAT', $arParams) && strlen($arParams['KEY_FORMAT']) > 0) { $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; } $coupon = 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']); $ID = CSupportSuperCoupon::Add($arFields); if ($ID === false) { $DB->Rollback(); return $ID; } } else { $DB->Rollback(); $APPLICATION->ThrowException(GetMessage('SUP_ST_ERROR_NO_NEW_COUPON')); } $DB->Commit(); return $coupon; }