1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
discount_coupon.php
См. документацию.
1<?php
2
6
7require_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/catalog/general/discount_coupon.php");
8
10{
11 public static function Add($arFields, $bAffectDataFile = true)
12 {
13 static $eventOnBeforeAddExists = null;
14 static $eventOnAddExists = null;
15 global $DB;
16
17 if ($eventOnBeforeAddExists === true || $eventOnBeforeAddExists === null)
18 {
19 foreach (GetModuleEvents('catalog', 'OnBeforeCouponAdd', true) as $arEvent)
20 {
21 $eventOnBeforeAddExists = true;
22 if (ExecuteModuleEventEx($arEvent, array(&$arFields, &$bAffectDataFile)) === false)
23 return false;
24 }
25 if ($eventOnBeforeAddExists === null)
26 $eventOnBeforeAddExists = false;
27 }
28
29 $bAffectDataFile = false;
30
32 return false;
33
34 $arInsert = $DB->PrepareInsert("b_catalog_discount_coupon", $arFields);
35
36 $strSql = "INSERT INTO b_catalog_discount_coupon(".$arInsert[0].") VALUES(".$arInsert[1].")";
37 $DB->Query($strSql);
38
39 $ID = (int)$DB->LastID();
40
41 if ($ID > 0)
43
44 if ($eventOnAddExists === true || $eventOnAddExists === null)
45 {
46 foreach (GetModuleEvents('catalog', 'OnCouponAdd', true) as $arEvent)
47 {
48 $eventOnAddExists = true;
50 }
51 if ($eventOnAddExists === null)
52 $eventOnAddExists = false;
53 }
54
55 return $ID;
56 }
57
58 public static function Update($ID, $arFields)
59 {
60 static $eventOnBeforeUpdateExists = null;
61 static $eventOnUpdateExists = null;
62 global $DB;
63
64 $ID = (int)$ID;
65 if ($ID <= 0)
66 return false;
67
68 if ($eventOnBeforeUpdateExists === true || $eventOnBeforeUpdateExists === null)
69 {
70 foreach (GetModuleEvents('catalog', 'OnBeforeCouponUpdate', true) as $arEvent)
71 {
72 $eventOnBeforeUpdateExists = true;
73 if (ExecuteModuleEventEx($arEvent, array($ID, &$arFields)) === false)
74 return false;
75 }
76 if ($eventOnBeforeUpdateExists === null)
77 $eventOnBeforeUpdateExists = false;
78 }
79
81 return false;
82
83 $discountIds = array();
84 $strUpdate = $DB->PrepareUpdate("b_catalog_discount_coupon", $arFields);
85 if (!empty($strUpdate))
86 {
87 if (isset($arFields['DISCOUNT_ID']))
88 {
89 $iterator = Catalog\DiscountCouponTable::getList(array(
90 'select' => array('DISCOUNT_ID', 'ID'),
91 'filter' => array('=ID' => $ID)
92 ));
93 $row = $iterator->fetch();
94 unset($iterator);
95 if (!empty($row))
96 {
97 $row['DISCOUNT_ID'] = (int)$row['DISCOUNT_ID'];
98 if ($row['DISCOUNT_ID'] != $arFields['DISCOUNT_ID'])
99 {
100 $discountIds[] = $arFields['DISCOUNT_ID'];
101 $discountIds[] = $row['DISCOUNT_ID'];
102 }
103 }
104 unset($row);
105 }
106
107 $strSql = "UPDATE b_catalog_discount_coupon SET ".$strUpdate." WHERE ID = ".$ID;
108 $DB->Query($strSql);
109
110 if (!empty($discountIds))
111 {
112 $withoutCoupons = array_fill_keys($discountIds, true);
113 $withCoupons = array();
114 $couponIterator = Catalog\DiscountCouponTable::getList(array(
115 'select' => array('DISCOUNT_ID', new Main\Entity\ExpressionField('CNT', 'COUNT(*)')),
116 'filter' => array('@DISCOUNT_ID' => $discountIds),
117 'group' => array('DISCOUNT_ID')
118 ));
119 while ($coupon = $couponIterator->fetch())
120 {
121 $coupon['CNT'] = (int)$coupon['CNT'];
122 if ($coupon['CNT'] > 0)
123 {
124 $coupon['DISCOUNT_ID'] = (int)$coupon['DISCOUNT_ID'];
125 unset($withoutCoupons[$coupon['DISCOUNT_ID']]);
126 $withCoupons[$coupon['DISCOUNT_ID']] = true;
127 }
128 }
129 unset($coupon, $couponIterator);
130 if (!empty($withoutCoupons))
131 {
132 $withoutCoupons = array_keys($withoutCoupons);
133 Catalog\DiscountTable::setUseCoupons($withoutCoupons, 'N');
134 }
135 if (!empty($withCoupons))
136 {
137 $withCoupons = array_keys($withCoupons);
138 Catalog\DiscountTable::setUseCoupons($withCoupons, 'Y');
139 }
140 unset($withCoupons, $withoutCoupons);
141 }
142 }
143 unset($discountIds);
144
145 if ($eventOnUpdateExists === true || $eventOnUpdateExists === null)
146 {
147 foreach (GetModuleEvents('catalog', 'OnCouponUpdate', true) as $arEvent)
148 {
149 $eventOnUpdateExists = true;
151 }
152 if ($eventOnUpdateExists === null)
153 $eventOnUpdateExists = false;
154 }
155
156 return $ID;
157 }
158
159 public static function Delete($ID, $bAffectDataFile = true)
160 {
161 static $eventOnBeforeDeleteExists = null;
162 static $eventOnDeleteExists = null;
163 global $DB;
164
165 $ID = (int)$ID;
166 if ($ID <= 0)
167 return false;
168
169 if ($eventOnBeforeDeleteExists === true || $eventOnBeforeDeleteExists === null)
170 {
171 foreach (GetModuleEvents('catalog', 'OnBeforeCouponDelete', true) as $arEvent)
172 {
173 $eventOnBeforeDeleteExists = true;
174 if (ExecuteModuleEventEx($arEvent, array($ID, &$bAffectDataFile)) === false)
175 return false;
176 }
177 if ($eventOnBeforeDeleteExists === null)
178 $eventOnBeforeDeleteExists = false;
179 }
180
181 $bAffectDataFile = false;
182
183 $iterator = Catalog\DiscountCouponTable::getList(array(
184 'select' => array('DISCOUNT_ID', 'ID'),
185 'filter' => array('=ID' => $ID)
186 ));
187 $row = $iterator->fetch();
188 unset($iterator);
189
190 $DB->Query("DELETE FROM b_catalog_discount_coupon WHERE ID = ".$ID);
191
192 if (!empty($row))
193 {
194 $row['DISCOUNT_ID'] = (int)$row['DISCOUNT_ID'];
195 $iterator = Catalog\DiscountCouponTable::getList(array(
196 'select' => array('DISCOUNT_ID'),
197 'filter' => array('=DISCOUNT_ID' => $row['DISCOUNT_ID']),
198 'limit' => 1
199 ));
200 $existRow = $iterator->fetch();
201 unset($iterator);
203 $row['DISCOUNT_ID'],
204 (!empty($existRow) ? 'Y' : 'N')
205 );
206 unset($existRow);
207 }
208 unset($row);
209
210 if ($eventOnDeleteExists === true || $eventOnDeleteExists === null)
211 {
212 foreach (GetModuleEvents('catalog', 'OnCouponDelete', true) as $arEvent)
213 {
214 $eventOnDeleteExists = true;
215 ExecuteModuleEventEx($arEvent, array($ID));
216 }
217 if ($eventOnDeleteExists === null)
218 $eventOnDeleteExists = false;
219 }
220
221 return true;
222 }
223
224 public static function GetByID($ID)
225 {
226 global $DB;
227
228 $ID = intval($ID);
229 if ($ID <= 0)
230 return false;
231
232 $strSql =
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;
238
239 $db_res = $DB->Query($strSql);
240 if ($res = $db_res->Fetch())
241 return $res;
242
243 return false;
244 }
245
254 public static function GetList($arOrder = array(), $arFilter = array(), $arGroupBy = false, $arNavStartParams = false, $arSelectFields = array())
255 {
256 global $DB;
257
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"),
271 );
272
273 $arSqls = CCatalog::PrepareSql($arFields, $arOrder, $arFilter, $arGroupBy, $arSelectFields);
274
275 $arSqls["SELECT"] = str_replace("%%_DISTINCT_%%", "", $arSqls["SELECT"]);
276
277 if (empty($arGroupBy) && is_array($arGroupBy))
278 {
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"];
284
285 $dbRes = $DB->Query($strSql);
286 if ($arRes = $dbRes->Fetch())
287 return $arRes["CNT"];
288 else
289 return false;
290 }
291
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"];
299
300 $intTopCount = 0;
301 $boolNavStartParams = (!empty($arNavStartParams) && is_array($arNavStartParams));
302 if ($boolNavStartParams && array_key_exists('nTopCount', $arNavStartParams))
303 {
304 $intTopCount = intval($arNavStartParams["nTopCount"]);
305 }
306 if ($boolNavStartParams && 0 >= $intTopCount)
307 {
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"];
313
314 $dbRes = $DB->Query($strSql_tmp);
315 $cnt = 0;
316 if (empty($arSqls["GROUPBY"]))
317 {
318 if ($arRes = $dbRes->Fetch())
319 $cnt = $arRes["CNT"];
320 }
321 else
322 {
323 $cnt = $dbRes->SelectedRowsCount();
324 }
325
326 $dbRes = new CDBResult();
327
328 $dbRes->NavQuery($strSql, $cnt, $arNavStartParams);
329 }
330 else
331 {
332 if ($boolNavStartParams && 0 < $intTopCount)
333 {
334 $strSql .= " LIMIT ".$intTopCount;
335 }
336 $dbRes = $DB->Query($strSql);
337 }
338
339 return $dbRes;
340 }
341
346 public static function CouponApply($intUserID, $strCoupon)
347 {
348 if (self::$existCouponsManager === null)
350 if (self::$existCouponsManager)
351 {
352 $couponList = (is_array($strCoupon) ? $strCoupon : array($strCoupon));
353 return DiscountCouponsManager::setApplyByProduct(array('MODULE' => 'catalog'), $couponList, true);
354 }
355 else
356 {
357 global $DB;
358
359 $mxResult = false;
360
361 $intUserID = (int)$intUserID;
362 if ($intUserID < 0)
363 $intUserID = 0;
364
365 $arCouponList = array();
366 $arCheck = (is_array($strCoupon) ? $strCoupon : array($strCoupon));
367 foreach ($arCheck as &$strOneCheck)
368 {
369 $strOneCheck = (string)$strOneCheck;
370 if ('' != $strOneCheck)
371 $arCouponList[] = $strOneCheck;
372 }
373 if (isset($strOneCheck))
374 unset($strOneCheck);
375
376 if (empty($arCouponList))
377 return $mxResult;
378
379 $strDateFunction = $DB->GetNowFunction();
380 $boolFlag = false;
381 $couponIterator = Catalog\DiscountCouponTable::getList(array(
382 'select' => array('ID', 'TYPE', 'COUPON'),
383 'filter' => array('=COUPON' => $arCouponList, '=ACTIVE' => 'Y')
384 ));
385 while ($arCoupon = $couponIterator->fetch())
386 {
387 $arCoupon['ID'] = (int)$arCoupon['ID'];
389 "~DATE_APPLY" => $strDateFunction
390 );
391
392 if ($arCoupon['TYPE'] == Catalog\DiscountCouponTable::TYPE_ONE_ROW)
393 {
394 $arFields["ACTIVE"] = "N";
395 if (0 < $intUserID)
396 {
397 CCatalogDiscountCoupon::EraseCouponByManage($intUserID, $arCoupon['COUPON']);
398 }
399 else
400 {
401 CCatalogDiscountCoupon::EraseCoupon($arCoupon['COUPON']);
402 }
403 }
405 {
406 $boolFlag = true;
407 if (!isset(self::$arOneOrderCoupons[$arCoupon['ID']]))
408 self::$arOneOrderCoupons[$arCoupon['ID']] = array(
409 'COUPON' => $arCoupon['COUPON'],
410 'USER_ID' => $intUserID,
411 );
412 }
413
414 $strUpdate = $DB->PrepareUpdate("b_catalog_discount_coupon", $arFields);
415 if (!empty($strUpdate))
416 {
417 $strSql = "UPDATE b_catalog_discount_coupon SET ".$strUpdate." WHERE ID = ".$arCoupon['ID'];
418 $DB->Query($strSql);
419 $mxResult = true;
420 }
421 }
422 unset($arCoupon, $couponIterator);
423 if ($boolFlag)
424 {
425 AddEventHandler('sale', 'OnBasketOrder', array('CCatalogDiscountCoupon', 'CouponOneOrderDisable'));
426 AddEventHandler('sale', 'OnDoBasketOrder', array('CCatalogDiscountCoupon', 'CouponOneOrderDisable'));
427 }
428 return $mxResult;
429 }
430 }
431
436 public static function __CouponOneOrderDisable($arCoupons)
437 {
438 global $DB;
439 if (!is_array($arCoupons))
440 $arCoupons = array(intval($arCoupons));
441 CatalogClearArray($arCoupons, false);
442 if (empty($arCoupons))
443 return;
444 $strSql = "UPDATE b_catalog_discount_coupon SET ACTIVE='N' WHERE ID IN (".implode(', ', $arCoupons).") AND ONE_TIME='".self::TYPE_ONE_ORDER."'";
445 $DB->Query($strSql);
446 }
447
452 public static function CouponOneOrderDisable($intOrderID = 0)
453 {
454 if (self::$existCouponsManager === null)
456 if (self::$existCouponsManager)
457 return;
458
459 global $DB;
460 if (!empty(self::$arOneOrderCoupons))
461 {
462 $arCouponID = array_keys(self::$arOneOrderCoupons);
463 foreach (self::$arOneOrderCoupons as &$arCoupon)
464 {
465 $arCoupon['USER_ID'] = intval($arCoupon['USER_ID']);
466 if (0 < $arCoupon['USER_ID'])
467 {
468 CCatalogDiscountCoupon::EraseCouponByManage($arCoupon['USER_ID'], $arCoupon['COUPON']);
469 }
470 else
471 {
472 CCatalogDiscountCoupon::EraseCoupon($arCoupon['COUPON']);
473 }
474 }
475 if (isset($arCoupon))
476 unset($arCoupon);
477 CatalogClearArray($arCouponID, false);
478 if (!empty($arCouponID))
479 {
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'";
481 $DB->Query($strSql);
482 }
483 self::$arOneOrderCoupons = array();
484 }
485 }
486
491 public static function IsExistCoupon($strCoupon)
492 {
493 if (self::$existCouponsManager === null)
495 if (self::$existCouponsManager)
496 {
498 if (!empty($result))
499 return true;
500 return false;
501 }
502 else
503 {
504 global $DB;
505
506 if ($strCoupon == '')
507 return false;
508
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())
512 return true;
513 }
514 return false;
515 }
516}
$db_res
Определения options_user_settings.php:8
CatalogClearArray(&$arMap, $boolSort=true)
Определения include.php:2257
$boolFlag
Определения generate_coupon.php:21
static setUseCoupons($discountList, $use)
Определения discount.php:442
static setApplyByProduct($product, $couponsList, $oldMode=false)
static initCouponManager()
Определения discount_coupon.php:687
static EraseCoupon($strCoupon)
Определения discount_coupon.php:241
static CheckFields($ACTION, &$arFields, $ID=0)
Определения discount_coupon.php:30
static EraseCouponByManage($intUserID, $strCoupon)
Определения discount_coupon.php:408
static PrepareSql(&$arFields, $arOrder, $arFilter, $arGroupBy, $arSelectFields)
Определения catalog.php:389
static Delete($ID, $bAffectDataFile=true)
Определения discount_coupon.php:159
static CouponOneOrderDisable($intOrderID=0)
Определения discount_coupon.php:452
static GetByID($ID)
Определения discount_coupon.php:224
static GetList($arOrder=array(), $arFilter=array(), $arGroupBy=false, $arNavStartParams=false, $arSelectFields=array())
Определения discount_coupon.php:254
static CouponApply($intUserID, $strCoupon)
Определения discount_coupon.php:346
static IsExistCoupon($strCoupon)
Определения discount_coupon.php:491
static Add($arFields, $bAffectDataFile=true)
Определения discount_coupon.php:11
static Update($ID, $arFields)
Определения discount_coupon.php:58
static __CouponOneOrderDisable($arCoupons)
Определения discount_coupon.php:436
$arFields
Определения dblapprove.php:5
if($usePageNavigation) $couponIterator
Определения discount_coupon_list.php:338
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$res
Определения filter_act.php:7
$result
Определения get_property_values.php:14
if($ajaxMode) $ID
Определения get_user.php:27
$_SERVER["DOCUMENT_ROOT"]
Определения cron_frame.php:9
global $DB
Определения cron_frame.php:29
ExecuteModuleEventEx($arEvent, $arParams=[])
Определения tools.php:5214
AddEventHandler($FROM_MODULE_ID, $MESSAGE_ID, $CALLBACK, $SORT=100, $FULL_PATH=false)
Определения tools.php:5165
GetModuleEvents($MODULE_ID, $MESSAGE_ID, $bReturnArray=false)
Определения tools.php:5177
Определения ufield.php:9
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$arRes
Определения options.php:104
$arFilter
Определения user_search.php:106
$dbRes
Определения yandex_detail.php:168
$iterator
Определения yandex_run.php:610