7require_once(
$_SERVER[
"DOCUMENT_ROOT"].
"/bitrix/modules/catalog/general/discount_coupon.php");
13 static $eventOnBeforeAddExists =
null;
14 static $eventOnAddExists =
null;
17 if ($eventOnBeforeAddExists ===
true || $eventOnBeforeAddExists ===
null)
19 foreach (
GetModuleEvents(
'catalog',
'OnBeforeCouponAdd',
true) as $arEvent)
21 $eventOnBeforeAddExists =
true;
25 if ($eventOnBeforeAddExists ===
null)
26 $eventOnBeforeAddExists =
false;
29 $bAffectDataFile =
false;
34 $arInsert =
$DB->PrepareInsert(
"b_catalog_discount_coupon",
$arFields);
36 $strSql =
"INSERT INTO b_catalog_discount_coupon(".$arInsert[0].
") VALUES(".$arInsert[1].
")";
44 if ($eventOnAddExists ===
true || $eventOnAddExists ===
null)
48 $eventOnAddExists =
true;
51 if ($eventOnAddExists ===
null)
52 $eventOnAddExists =
false;
60 static $eventOnBeforeUpdateExists =
null;
61 static $eventOnUpdateExists =
null;
68 if ($eventOnBeforeUpdateExists ===
true || $eventOnBeforeUpdateExists ===
null)
70 foreach (
GetModuleEvents(
'catalog',
'OnBeforeCouponUpdate',
true) as $arEvent)
72 $eventOnBeforeUpdateExists =
true;
76 if ($eventOnBeforeUpdateExists ===
null)
77 $eventOnBeforeUpdateExists =
false;
83 $discountIds =
array();
84 $strUpdate =
$DB->PrepareUpdate(
"b_catalog_discount_coupon",
$arFields);
85 if (!empty($strUpdate))
90 'select' =>
array(
'DISCOUNT_ID',
'ID'),
97 $row[
'DISCOUNT_ID'] = (int)$row[
'DISCOUNT_ID'];
98 if ($row[
'DISCOUNT_ID'] !=
$arFields[
'DISCOUNT_ID'])
100 $discountIds[] =
$arFields[
'DISCOUNT_ID'];
101 $discountIds[] = $row[
'DISCOUNT_ID'];
107 $strSql =
"UPDATE b_catalog_discount_coupon SET ".$strUpdate.
" WHERE ID = ".
$ID;
110 if (!empty($discountIds))
112 $withoutCoupons = array_fill_keys($discountIds,
true);
113 $withCoupons =
array();
116 'filter' =>
array(
'@DISCOUNT_ID' => $discountIds),
117 'group' =>
array(
'DISCOUNT_ID')
121 $coupon[
'CNT'] = (int)$coupon[
'CNT'];
122 if ($coupon[
'CNT'] > 0)
124 $coupon[
'DISCOUNT_ID'] = (int)$coupon[
'DISCOUNT_ID'];
125 unset($withoutCoupons[$coupon[
'DISCOUNT_ID']]);
126 $withCoupons[$coupon[
'DISCOUNT_ID']] =
true;
130 if (!empty($withoutCoupons))
132 $withoutCoupons = array_keys($withoutCoupons);
135 if (!empty($withCoupons))
137 $withCoupons = array_keys($withCoupons);
140 unset($withCoupons, $withoutCoupons);
145 if ($eventOnUpdateExists ===
true || $eventOnUpdateExists ===
null)
147 foreach (
GetModuleEvents(
'catalog',
'OnCouponUpdate',
true) as $arEvent)
149 $eventOnUpdateExists =
true;
152 if ($eventOnUpdateExists ===
null)
153 $eventOnUpdateExists =
false;
159 public static function Delete(
$ID, $bAffectDataFile =
true)
161 static $eventOnBeforeDeleteExists =
null;
162 static $eventOnDeleteExists =
null;
169 if ($eventOnBeforeDeleteExists ===
true || $eventOnBeforeDeleteExists ===
null)
171 foreach (
GetModuleEvents(
'catalog',
'OnBeforeCouponDelete',
true) as $arEvent)
173 $eventOnBeforeDeleteExists =
true;
177 if ($eventOnBeforeDeleteExists ===
null)
178 $eventOnBeforeDeleteExists =
false;
181 $bAffectDataFile =
false;
184 'select' =>
array(
'DISCOUNT_ID',
'ID'),
190 $DB->Query(
"DELETE FROM b_catalog_discount_coupon WHERE ID = ".
$ID);
194 $row[
'DISCOUNT_ID'] = (int)$row[
'DISCOUNT_ID'];
196 'select' =>
array(
'DISCOUNT_ID'),
197 'filter' =>
array(
'=DISCOUNT_ID' => $row[
'DISCOUNT_ID']),
204 (!empty($existRow) ?
'Y' :
'N')
210 if ($eventOnDeleteExists ===
true || $eventOnDeleteExists ===
null)
212 foreach (
GetModuleEvents(
'catalog',
'OnCouponDelete',
true) as $arEvent)
214 $eventOnDeleteExists =
true;
217 if ($eventOnDeleteExists ===
null)
218 $eventOnDeleteExists =
false;
233 "SELECT CD.ID, CD.DISCOUNT_ID, CD.ACTIVE, CD.COUPON, CD.ONE_TIME, ".
234 $DB->DateToCharFunction(
"CD.DATE_APPLY",
"FULL").
" as DATE_APPLY, ".
235 $DB->DateToCharFunction(
"CD.TIMESTAMP_X",
"FULL").
" as TIMESTAMP_X, ".
236 "CD.CREATED_BY, CD.MODIFIED_BY, ".$DB->DateToCharFunction(
'CD.DATE_CREATE',
'FULL').
' as DATE_CREATE, '.
237 "CD.DESCRIPTION FROM b_catalog_discount_coupon CD WHERE CD.ID = ".$ID;
259 "ID" =>
array(
"FIELD" =>
"CD.ID",
"TYPE" =>
"int"),
260 "DISCOUNT_ID" =>
array(
"FIELD" =>
"CD.DISCOUNT_ID",
"TYPE" =>
"string"),
261 "ACTIVE" =>
array(
"FIELD" =>
"CD.ACTIVE",
"TYPE" =>
"char"),
262 "ONE_TIME" =>
array(
"FIELD" =>
"CD.ONE_TIME",
"TYPE" =>
"char"),
263 "COUPON" =>
array(
"FIELD" =>
"CD.COUPON",
"TYPE" =>
"string"),
264 "DATE_APPLY" =>
array(
"FIELD" =>
"CD.DATE_APPLY",
"TYPE" =>
"datetime"),
265 "DISCOUNT_NAME" =>
array(
"FIELD" =>
"CDD.NAME",
"TYPE" =>
"string",
"FROM" =>
"LEFT JOIN b_catalog_discount CDD ON (CD.DISCOUNT_ID = CDD.ID)"),
266 "DESCRIPTION" =>
array(
"FIELD" =>
"CD.DESCRIPTION",
"TYPE" =>
"string"),
267 "TIMESTAMP_X" =>
array(
"FIELD" =>
"CD.TIMESTAMP_X",
"TYPE" =>
"datetime"),
268 "MODIFIED_BY" =>
array(
"FIELD" =>
"CD.MODIFIED_BY",
"TYPE" =>
"int"),
269 "DATE_CREATE" =>
array(
"FIELD" =>
"CD.DATE_CREATE",
"TYPE" =>
"datetime"),
270 "CREATED_BY" =>
array(
"FIELD" =>
"CD.CREATED_BY",
"TYPE" =>
"int"),
275 $arSqls[
"SELECT"] = str_replace(
"%%_DISTINCT_%%",
"", $arSqls[
"SELECT"]);
277 if (empty($arGroupBy) && is_array($arGroupBy))
279 $strSql =
"SELECT ".$arSqls[
"SELECT"].
" FROM b_catalog_discount_coupon CD ".$arSqls[
"FROM"];
280 if (!empty($arSqls[
"WHERE"]))
281 $strSql .=
" WHERE ".$arSqls[
"WHERE"];
282 if (!empty($arSqls[
"GROUPBY"]))
283 $strSql .=
" GROUP BY ".$arSqls[
"GROUPBY"];
292 $strSql =
"SELECT ".$arSqls[
"SELECT"].
" FROM b_catalog_discount_coupon CD ".$arSqls[
"FROM"];
293 if (!empty($arSqls[
"WHERE"]))
294 $strSql .=
" WHERE ".$arSqls[
"WHERE"];
295 if (!empty($arSqls[
"GROUPBY"]))
296 $strSql .=
" GROUP BY ".$arSqls[
"GROUPBY"];
297 if (!empty($arSqls[
"ORDERBY"]))
298 $strSql .=
" ORDER BY ".$arSqls[
"ORDERBY"];
301 $boolNavStartParams = (!empty($arNavStartParams) && is_array($arNavStartParams));
302 if ($boolNavStartParams && array_key_exists(
'nTopCount', $arNavStartParams))
304 $intTopCount = intval($arNavStartParams[
"nTopCount"]);
306 if ($boolNavStartParams && 0 >= $intTopCount)
308 $strSql_tmp =
"SELECT COUNT('x') as CNT FROM b_catalog_discount_coupon CD ".$arSqls[
"FROM"];
309 if (!empty($arSqls[
"WHERE"]))
310 $strSql_tmp .=
" WHERE ".$arSqls[
"WHERE"];
311 if (!empty($arSqls[
"GROUPBY"]))
312 $strSql_tmp .=
" GROUP BY ".$arSqls[
"GROUPBY"];
316 if (empty($arSqls[
"GROUPBY"]))
323 $cnt =
$dbRes->SelectedRowsCount();
328 $dbRes->NavQuery($strSql, $cnt, $arNavStartParams);
332 if ($boolNavStartParams && 0 < $intTopCount)
334 $strSql .=
" LIMIT ".$intTopCount;
348 if (self::$existCouponsManager ===
null)
350 if (self::$existCouponsManager)
352 $couponList = (is_array($strCoupon) ? $strCoupon :
array($strCoupon));
361 $intUserID = (int)$intUserID;
365 $arCouponList =
array();
366 $arCheck = (is_array($strCoupon) ? $strCoupon :
array($strCoupon));
367 foreach ($arCheck as &$strOneCheck)
369 $strOneCheck = (string)$strOneCheck;
370 if (
'' != $strOneCheck)
371 $arCouponList[] = $strOneCheck;
373 if (isset($strOneCheck))
376 if (empty($arCouponList))
379 $strDateFunction =
$DB->GetNowFunction();
382 'select' =>
array(
'ID',
'TYPE',
'COUPON'),
383 'filter' =>
array(
'=COUPON' => $arCouponList,
'=ACTIVE' =>
'Y')
387 $arCoupon[
'ID'] = (int)$arCoupon[
'ID'];
389 "~DATE_APPLY" => $strDateFunction
407 if (!isset(self::$arOneOrderCoupons[$arCoupon[
'ID']]))
408 self::$arOneOrderCoupons[$arCoupon[
'ID']] =
array(
409 'COUPON' => $arCoupon[
'COUPON'],
410 'USER_ID' => $intUserID,
414 $strUpdate =
$DB->PrepareUpdate(
"b_catalog_discount_coupon",
$arFields);
415 if (!empty($strUpdate))
417 $strSql =
"UPDATE b_catalog_discount_coupon SET ".$strUpdate.
" WHERE ID = ".$arCoupon[
'ID'];
425 AddEventHandler(
'sale',
'OnBasketOrder',
array(
'CCatalogDiscountCoupon',
'CouponOneOrderDisable'));
426 AddEventHandler(
'sale',
'OnDoBasketOrder',
array(
'CCatalogDiscountCoupon',
'CouponOneOrderDisable'));
439 if (!is_array($arCoupons))
440 $arCoupons =
array(intval($arCoupons));
442 if (empty($arCoupons))
444 $strSql =
"UPDATE b_catalog_discount_coupon SET ACTIVE='N' WHERE ID IN (".implode(
', ', $arCoupons).
") AND ONE_TIME='".self::TYPE_ONE_ORDER.
"'";
454 if (self::$existCouponsManager ===
null)
456 if (self::$existCouponsManager)
460 if (!empty(self::$arOneOrderCoupons))
462 $arCouponID = array_keys(self::$arOneOrderCoupons);
463 foreach (self::$arOneOrderCoupons as &$arCoupon)
465 $arCoupon[
'USER_ID'] = intval($arCoupon[
'USER_ID']);
466 if (0 < $arCoupon[
'USER_ID'])
475 if (isset($arCoupon))
478 if (!empty($arCouponID))
480 $strSql =
"UPDATE b_catalog_discount_coupon SET ACTIVE='N' WHERE ID IN (".implode(
', ', $arCouponID).
") AND ONE_TIME='".self::TYPE_ONE_ORDER.
"' AND ACTIVE='Y'";
483 self::$arOneOrderCoupons =
array();
493 if (self::$existCouponsManager ===
null)
495 if (self::$existCouponsManager)
506 if ($strCoupon ==
'')
509 $strSql =
"select ID, COUPON from b_catalog_discount_coupon where COUPON='".$DB->ForSql($strCoupon).
"' limit 1";
510 $rsCoupons =
$DB->Query($strSql);
511 if ($arCoupon = $rsCoupons->Fetch())
CatalogClearArray(&$arMap, $boolSort=true)
static setUseCoupons($discountList, $use)
static setApplyByProduct($product, $couponsList, $oldMode=false)
static initCouponManager()
static EraseCoupon($strCoupon)
static CheckFields($ACTION, &$arFields, $ID=0)
static EraseCouponByManage($intUserID, $strCoupon)
static PrepareSql(&$arFields, $arOrder, $arFilter, $arGroupBy, $arSelectFields)
static Delete($ID, $bAffectDataFile=true)
static CouponOneOrderDisable($intOrderID=0)
static GetList($arOrder=array(), $arFilter=array(), $arGroupBy=false, $arNavStartParams=false, $arSelectFields=array())
static CouponApply($intUserID, $strCoupon)
static IsExistCoupon($strCoupon)
static Add($arFields, $bAffectDataFile=true)
static Update($ID, $arFields)
static __CouponOneOrderDisable($arCoupons)
if($usePageNavigation) $couponIterator
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
$_SERVER["DOCUMENT_ROOT"]
ExecuteModuleEventEx($arEvent, $arParams=[])
AddEventHandler($FROM_MODULE_ID, $MESSAGE_ID, $CALLBACK, $SORT=100, $FULL_PATH=false)
GetModuleEvents($MODULE_ID, $MESSAGE_ID, $bReturnArray=false)
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)