1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
coupons.php
См. документацию.
1<?php
2
4
6{
7 public static function Generate($arParams = array())
8 {
9 global $DB, $USER, $APPLICATION;
10 if (!is_array($arParams))
11 {
12 $arParams = array();
13 }
14
15 if(array_key_exists('KEY_FORMAT', $arParams) && $arParams['KEY_FORMAT'] <> '')
16 {
17 $couponFormat = $arParams['KEY_FORMAT'];
18 }
19 else
20 {
21 $couponFormat = COption::GetOptionString('support', 'SUPERTICKET_COUPON_FORMAT');
22 }
23
24 $count = array_key_exists('COUNT_TICKETS', $arParams) ? intval($arParams['COUNT_TICKETS']) : 5;
25 if ($count <= 0)
26 {
27 $count = 5;
28 }
29
30 $slaID = array_key_exists('SLA_ID', $arParams) ? $arParams['SLA_ID'] : COption::GetOptionString("support", 'SUPERTICKET_DEFAULT_SLA');
31 $slaID = intval($slaID);
32 if ($slaID <= 0)
33 {
34 $slaID = false;
35 }
36
37 $DB->StartTransaction();
38 for ($i = 0; $i < 100; ++$i)
39 {
40 $coupon = preg_replace_callback('|#|u', array('CSupportSuperCoupon', '_getrandsymbol'), $couponFormat);
41 $rs = CSupportSuperCoupon::GetList(false, array('COUPON' => $coupon));
42 if ($rs->Fetch())
43 {
44 $coupon = false;
45 }
46 else
47 {
48 break;
49 }
50 }
51
52 if ($coupon !== false)
53 {
55 'COUPON' => $coupon,
56 'COUNT_TICKETS' => $count,
57 'SLA_ID' => $slaID,
58 'ACTIVE_FROM' => $arParams['ACTIVE_FROM'],
59 'ACTIVE_TO' => $arParams['ACTIVE_TO'],
60 'ACTIVE' => $arParams['ACTIVE'],
61 );
62
64 {
65 $DB->Rollback();
66 return false;
67 }
68 $DB->Commit();
69 }
70 else
71 {
72 $DB->Rollback();
73 $APPLICATION->ThrowException(GetMessage('SUP_ST_ERROR_NO_NEW_COUPON'));
74 }
75
76 return $coupon;
77 }
78
79 public static function Add($arFields)
80 {
81 global $DB, $USER;
82
84 return false;
85
86 $arFields['~TIMESTAMP_X'] = $DB->GetNowFunction();
87 $arFields['~DATE_CREATE'] = $DB->GetNowFunction();
88
89 if(isset($USER) && is_object($USER))
90 {
91 $arFields['CREATED_USER_ID'] = intval($USER->GetID());
92 }
93
94 if (array_key_exists('TIMESTAMP_X', $arFields))
95 {
96 unset($arFields['TIMESTAMP_X']);
97 }
98
99 if (array_key_exists('DATE_CREATE', $arFields))
100 {
101 unset($arFields['DATE_CREATE']);
102 }
103
104 return $DB->Add('b_ticket_supercoupons', $arFields);
105 }
106
107 public static function Update($ID, $arFields)
108 {
109 global $DB, $APPLICATION, $USER;
110
111 $ID = intval($ID);
113 return false;
114
115 $arFields['~TIMESTAMP_X'] = $DB->GetNowFunction();
116 if(isset($USER) && is_object($USER))
117 {
118 $arFields['UPDATED_USER_ID'] = $USER->GetID();
119 }
120
121 $strUpdate = $DB->PrepareUpdate('b_ticket_supercoupons', $arFields);
122 if ($strUpdate <> '')
123 {
124 $strSql = "UPDATE b_ticket_supercoupons SET $strUpdate WHERE ID=$ID";
125 $q = $DB->Query($strSql);
126 $rows = intval($q->AffectedRowsCount());
127 }
128 else
129 {
130 $APPLICATION->ThrowException(GetMessage('SUP_ST_ERROR_NO_UPDATE_DATA'));
131 return false;
132 }
133
134 if ($rows <= 0)
135 {
136 $APPLICATION->ThrowException(GetMessage('SUP_ST_ERROR_NO_UPDATES_ROWS'));
137 return false;
138 }
139
140 return true;
141 }
142
143 public static function UseCoupon($coupon)
144 {
145 global $DB, $USER;
146 $ret = false;
147
148 $arFields = false;
149 $arLogFields = false;
150 $arCoupon = false;
151
152 $curr = ConvertTimeStamp();
154 'LOGIC' => 'AND',
155
156 'COUPON' => $coupon,
157 '=ACTIVE' => 'Y',
158 array(
159 'LOGIC' => 'OR',
160
161 'ACTIVE_FROM' => false,
162 '<=ACTIVE_FROM' => $curr,
163 ),
164 array(
165 'LOGIC' => 'OR',
166
167 'ACTIVE_TO' => false,
168 '>=ACTIVE_TO' => $curr,
169 )
170 );
171
172
173 $DB->StartTransaction();
174
176 if ($arCoupon = $rs->Fetch())
177 {
178 $countTickets = intval($arCoupon['COUNT_TICKETS']);
179 if ($countTickets > 0)
180 {
181 $countTickets--;
183 'COUNT_TICKETS' => $countTickets,
184 'TIMESTAMP_X' => $DB->GetNowFunction(),
185 //'UPDATED_USER_ID' => (isset($USER) && is_object($USER)) ? $USER->GetID() : false,
186 'COUNT_USED' => 'COUNT_USED + 1'
187 );
188 if(isset($USER) && is_object($USER))
189 {
190 $arFields['UPDATED_USER_ID'] = $USER->GetID();
191 }
192 if ($aff_rows = $DB->Update('b_ticket_supercoupons', $arFields, 'WHERE ID=' . $arCoupon['ID']))
193 {
194 $ret = true;
195 }
196 }
197
198 $arLogFields = array(
199 '~TIMESTAMP_X' => $DB->GetNowFunction(),
200 'COUPON_ID' => $arCoupon['ID'],
201 'USER_ID' => ((isset($USER) && is_object($USER)) ? $USER->GetID() : false),
202 'SUCCESS' => $ret ? 'Y' : 'N',
203 'AFTER_COUNT' => $countTickets,
204 'SESSION_ID' => (array_key_exists('SESS_SESSION_ID', $_SESSION) ? $_SESSION['SESS_SESSION_ID'] : false),
205 'GUEST_ID' => (array_key_exists('SESS_GUEST_ID', $_SESSION) ? $_SESSION['SESS_GUEST_ID'] : false),
206 'AFFECTED_ROWS' => $aff_rows,
207 'COUPON' => $coupon,
208 );
209
210 $arInsert = $DB->PrepareInsert('b_ticket_supercoupons_log', $arLogFields);
211 $strSql =
212 "INSERT INTO b_ticket_supercoupons_log (".$arInsert[0].") ".
213 "VALUES(".$arInsert[1].")";
214 $DB->Query($strSql);
215
216 $arMail = array(
217 'COUPON' => $coupon,
218 'COUPON_ID' => $arCoupon['ID'],
219 'DATE' => ConvertTimeStamp(false, 'FULL'),
220 'USER_ID' => ((isset($USER) && is_object($USER)) ? $USER->GetID() : -1),
221 'SESSION_ID' => (array_key_exists('SESS_SESSION_ID', $_SESSION) ? $_SESSION['SESS_SESSION_ID'] : -1),
222 'GUEST_ID' => (array_key_exists('SESS_GUEST_ID', $_SESSION) ? $_SESSION['SESS_GUEST_ID'] : -1),
223 );
224
225 $rsEvents = GetModuleEvents('support', 'OnBeforeSendCouponEMail');
226 while ($arEvent = $rsEvents->Fetch())
227 {
228 $arMail = ExecuteModuleEventEx($arEvent, array($arMail));
229 }
230
231 if ($arMail)
232 {
233 $e = new CEvent();
234 $e->Send('TICKET_GENERATE_SUPERCOUPON', SITE_ID, $arMail);
235 }
236 }
237
238 $DB->Commit();
239
240 $rsEvents = GetModuleEvents('support', 'OnAfterUseCoupon');
241 while ($arEvent = $rsEvents->Fetch())
242 {
243 ExecuteModuleEventEx($arEvent, array($coupon, $arCoupon, $arFields, $arLogFields));
244 }
245
246 return $ret;
247 }
248
249 public static function GetList($arOrder = array(), $arFilter = array())
250 {
251 global $DB;
253 'ID' => array(
254 'TABLE_ALIAS' => 'C',
255 'FIELD_NAME' => 'C.ID',
256 'FIELD_TYPE' => 'int', //int, double, file, enum, int, string, date, datetime
257 'JOIN' => false,
258 ),
259 'COUPON' => array(
260 'TABLE_ALIAS' => 'C',
261 'FIELD_NAME' => 'C.COUPON',
262 'FIELD_TYPE' => 'string', //int, double, file, enum, int, string, date, datetime
263 'JOIN' => false,
264 ),
265 'COUNT_TICKETS' => array(
266 'TABLE_ALIAS' => 'C',
267 'FIELD_NAME' => 'C.COUNT_TICKETS',
268 'FIELD_TYPE' => 'int', //int, double, file, enum, int, string, date, datetime
269 'JOIN' => false,
270 ),
271 'TIMESTAMP_X' => array(
272 'TABLE_ALIAS' => 'C',
273 'FIELD_NAME' => 'C.TIMESTAMP_X',
274 'FIELD_TYPE' => 'datetime', //int, double, file, enum, int, string, date, datetime
275 'JOIN' => false,
276 ),
277 'DATE_CREATE' => array(
278 'TABLE_ALIAS' => 'C',
279 'FIELD_NAME' => 'C.DATE_CREATE',
280 'FIELD_TYPE' => 'datetime', //int, double, file, enum, int, string, date, datetime
281 'JOIN' => false,
282 ),
283 'CREATED_USER_ID' => array(
284 'TABLE_ALIAS' => 'C',
285 'FIELD_NAME' => 'C.CREATED_USER_ID',
286 'FIELD_TYPE' => 'int', //int, double, file, enum, int, string, date, datetime
287 'JOIN' => false,
288 ),
289 'UPDATED_USER_ID' => array(
290 'TABLE_ALIAS' => 'C',
291 'FIELD_NAME' => 'C.UPDATED_USER_ID',
292 'FIELD_TYPE' => 'int', //int, double, file, enum, int, string, date, datetime
293 'JOIN' => false,
294 ),
295 'ACTIVE' => array(
296 'TABLE_ALIAS' => 'C',
297 'FIELD_NAME' => 'C.ACTIVE',
298 'FIELD_TYPE' => 'string', //int, double, file, enum, int, string, date, datetime
299 'JOIN' => false,
300 ),
301 'ACTIVE_FROM' => array(
302 'TABLE_ALIAS' => 'C',
303 'FIELD_NAME' => 'C.ACTIVE_FROM',
304 'FIELD_TYPE' => 'date', //int, double, file, enum, int, string, date, datetime
305 'JOIN' => false,
306 ),
307 'ACTIVE_TO' => array(
308 'TABLE_ALIAS' => 'C',
309 'FIELD_NAME' => 'C.ACTIVE_TO',
310 'FIELD_TYPE' => 'date', //int, double, file, enum, int, string, date, datetime
311 'JOIN' => false,
312 ),
313 'COUNT_USED' => array(
314 'TABLE_ALIAS' => 'C',
315 'FIELD_NAME' => 'C.COUNT_USED',
316 'FIELD_TYPE' => 'date', //int, double, file, enum, int, string, date, datetime
317 'JOIN' => false,
318 ),
319 'SLA_ID' => array(
320 'TABLE_ALIAS' => 'S',
321 'FIELD_NAME' => 'S.ID',
322 'FIELD_TYPE' => 'int', //int, double, file, enum, int, string, date, datetime
323 'JOIN' => false,
324 ),
325 'SLA_NAME' => array(
326 'TABLE_ALIAS' => 'S',
327 'FIELD_NAME' => 'S.NAME',
328 'FIELD_TYPE' => 'string', //int, double, file, enum, int, string, date, datetime
329 'JOIN' => false,
330 ),
331 );
332
333 $obQueryWhere = new CSQLWhere;
334 $obQueryWhere->SetFields($arFields);
335
336 $where = $obQueryWhere->GetQuery($arFilter);
337
338 $order = '';
339 if (is_array($arOrder))
340 {
341 foreach ($arOrder as $k => $v)
342 {
343 if (array_key_exists($k, $arFields))
344 {
345 $v = mb_strtoupper($v);
346 if($v != 'DESC')
347 {
348 $v ='ASC';
349 }
350 if ($order <> '')
351 {
352 $order .= ', ';
353 }
354 $order .= $arFields[$k]['FIELD_NAME'] . ' ' . $v;
355 }
356 }
357 }
358
359 $strQuery = 'SELECT C.ID, C.COUPON, C.COUNT_TICKETS, C.CREATED_USER_ID, C.UPDATED_USER_ID, C.ACTIVE ACTIVE, C.COUNT_USED COUNT_USED,
360 '.$DB->DateToCharFunction('C.TIMESTAMP_X').' TIMESTAMP_X,
361 '.$DB->DateToCharFunction('C.DATE_CREATE').' DATE_CREATE,
362 '.$DB->DateToCharFunction('C.ACTIVE_FROM', 'SHORT').' ACTIVE_FROM,
363 '.$DB->DateToCharFunction('C.ACTIVE_TO', 'SHORT').' ACTIVE_TO,
364 UCR.LOGIN CREATED_LOGIN, UCR.NAME CREATED_FIRST_NAME, UCR.LAST_NAME CREATED_LAST_NAME,
365 UUP.LOGIN UPDATED_LOGIN, UUP.NAME UPDATED_FIRST_NAME, UUP.LAST_NAME UPDATED_LAST_NAME,
366 S.ID SLA_ID, S.NAME SLA_NAME
367 FROM b_ticket_supercoupons C
368 LEFT JOIN b_user UCR ON (C.CREATED_USER_ID IS NOT NULL AND C.CREATED_USER_ID = UCR.ID)
369 LEFT JOIN b_user UUP ON (C.CREATED_USER_ID IS NOT NULL AND C.UPDATED_USER_ID = UUP.ID)
370 LEFT JOIN b_ticket_sla S ON (C.SLA_ID IS NOT NULL AND C.SLA_ID = S.ID)
371 ';
372
373 if ($where <> '')
374 {
375 $strQuery .= ' WHERE ' . $where;
376 }
377
378 if ($order <> '')
379 {
380 $strQuery .= ' ORDER BY ' . $order;
381 }
382
383 return $DB->Query($strQuery);
384 }
385
386 public static function Delete($ID)
387 {
388 global $DB;
389 $ID = intval($ID);
390 if ($ID > 0)
391 {
392 $DB->Query('DELETE FROM b_ticket_supercoupons WHERE ID=' . $ID);
393 }
394 return true;
395 }
396
397 public static function GetLogList($arOrder = array(), $arFilter = array())
398 {
399 global $DB;
401 'COUPON_ID' => array(
402 'TABLE_ALIAS' => 'C',
403 'FIELD_NAME' => 'C.ID',
404 'FIELD_TYPE' => 'int', //int, double, file, enum, int, string, date, datetime
405 'JOIN' => false,
406 ),
407
408
409 'COUPON' => array(
410 'TABLE_ALIAS' => 'L',
411 'FIELD_NAME' => 'L.COUPON',
412 'FIELD_TYPE' => 'string', //int, double, file, enum, int, string, date, datetime
413 'JOIN' => false,
414 ),
415 'SUCCESS' => array(
416 'TABLE_ALIAS' => 'L',
417 'FIELD_NAME' => 'L.SUCCESS',
418 'FIELD_TYPE' => 'string', //int, double, file, enum, int, string, date, datetime
419 'JOIN' => false,
420 ),
421 'USER_ID' => array(
422 'TABLE_ALIAS' => 'L',
423 'FIELD_NAME' => 'L.USER_ID',
424 'FIELD_TYPE' => 'int', //int, double, file, enum, int, string, date, datetime
425 'JOIN' => false,
426 ),
427 'TIMESTAMP_X' => array(
428 'TABLE_ALIAS' => 'L',
429 'FIELD_NAME' => 'L.TIMESTAMP_X',
430 'FIELD_TYPE' => 'datetime', //int, double, file, enum, int, string, date, datetime
431 'JOIN' => false,
432 ),
433 );
434
435 $obQueryWhere = new CSQLWhere;
436 $obQueryWhere->SetFields($arFields);
437
438 $where = $obQueryWhere->GetQuery($arFilter);
439
440 $order = '';
441 if (is_array($arOrder))
442 {
443 foreach ($arOrder as $k => $v)
444 {
445 if (array_key_exists($k, $arFields))
446 {
447 $v = mb_strtoupper($v);
448 if($v != 'DESC')
449 {
450 $v ='ASC';
451 }
452 if ($order <> '')
453 {
454 $order .= ', ';
455 }
456 $order .= $arFields[$k]['FIELD_NAME'] . ' ' . $v;
457 }
458 }
459 }
460
461 $strQuery = "SELECT C.ID COUPON_ID, L.COUPON COUPON,
462 L.USER_ID USER_ID, L.SUCCESS SUCCESS, L.AFTER_COUNT AFTER_COUNT,
463 L.SESSION_ID SESSION_ID, L.GUEST_ID GUEST_ID,
464 U.LOGIN LOGIN, U.NAME FIRST_NAME, U.LAST_NAME LAST_NAME,
465 ".$DB->DateToCharFunction('L.TIMESTAMP_X')." TIMESTAMP_X
466 FROM b_ticket_supercoupons_log L
467 LEFT JOIN b_ticket_supercoupons C ON (L.COUPON_ID = C.ID)
468 LEFT JOIN b_user U ON (L.USER_ID IS NOT NULL AND L.USER_ID = U.ID)";
469
470 if ($where <> '')
471 {
472 $strQuery .= ' WHERE ' . $where;
473 }
474
475 if ($order <> '')
476 {
477 $strQuery .= ' ORDER BY ' . $order;
478 }
479
480 return $DB->Query($strQuery);
481
482
483 }
484
485 public static function _getrandsymbol($x)
486 {
487 return mb_strtoupper(randString(1));
488 }
489
490 public static function __CheckFields($arFields)
491 {
492 $aMsg = array();
493
494 if (is_set($arFields, "ACTIVE_FROM") && CheckDateTime($arFields['ACTIVE_FROM'], 'DD.MM.YYYY') &&
495 is_set($arFields, "ACTIVE_TO") && CheckDateTime($arFields['ACTIVE_TO'], 'DD.MM.YYYY'))
496 {
497 $dateElementsFrom = explode(".", $arFields["ACTIVE_FROM"]);
498 $_activeFrom = mktime(0,0,0, $dateElementsFrom[1], $dateElementsFrom[0], $dateElementsFrom[2]);
499
500 $dateElementsTo = explode(".", $arFields["ACTIVE_TO"]);
501 $_activeTo = mktime(0,0,0, $dateElementsTo[1], $dateElementsTo[0], $dateElementsTo[2]);
502 if ($_activeTo <= $_activeFrom)
503 $aMsg[] = array("id"=>"ACTIVE_TO", "text"=>GetMessage("SUP_ST_ERR_DATE_INTERVAL"));
504 }
505
506 if(is_set($arFields, "ACTIVE") && !in_array($arFields['ACTIVE'], Array('Y','N')))
507 {
508 $aMsg[] = array("id"=>"ACTIVE", "text"=>GetMessage("SUP_ST_ERR_ACTIVE"));
509 }
510
511 if(is_set($arFields, "SLA_ID") && intval($arFields['SLA_ID']) == 0)
512 {
513 $aMsg[] = array("id"=>"SLA_ID", "text"=>GetMessage("SUP_ST_ERR_SLA_ID"));
514 }
515
516 if(!empty($aMsg))
517 {
518 $e = new CAdminException($aMsg);
519 $GLOBALS["APPLICATION"]->ThrowException($e);
520 return false;
521 }
522
523 return true;
524 }
525}
$arParams
Определения access_dialog.php:21
$count
Определения admin_tab.php:4
global $APPLICATION
Определения include.php:80
SetFields($arFields)
Определения sqlwhere.php:239
Определения sqlwhere.php:1359
Определения coupons.php:6
static GetLogList($arOrder=array(), $arFilter=array())
Определения coupons.php:397
static _getrandsymbol($x)
Определения coupons.php:485
static Delete($ID)
Определения coupons.php:386
static Add($arFields)
Определения coupons.php:79
static __CheckFields($arFields)
Определения coupons.php:490
static UseCoupon($coupon)
Определения coupons.php:143
static Update($ID, $arFields)
Определения coupons.php:107
static GetList($arOrder=array(), $arFilter=array())
Определения coupons.php:249
static Generate($arParams=array())
Определения coupons.php:7
$arFields
Определения dblapprove.php:5
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
if($ajaxMode) $ID
Определения get_user.php:27
global $DB
Определения cron_frame.php:29
global $USER
Определения csv_new_run.php:40
ExecuteModuleEventEx($arEvent, $arParams=[])
Определения tools.php:5214
GetModuleEvents($MODULE_ID, $MESSAGE_ID, $bReturnArray=false)
Определения tools.php:5177
IncludeModuleLangFile($filepath, $lang=false, $bReturnArray=false)
Определения tools.php:3778
is_set($a, $k=false)
Определения tools.php:2133
GetMessage($name, $aReplace=null)
Определения tools.php:3397
CheckDateTime($datetime, $format=false)
Определения tools.php:398
randString($pass_len=10, $pass_chars=false)
Определения tools.php:2154
$order
Определения payment.php:8
$i
Определения factura.php:643
const SITE_ID
Определения sonet_set_content_view.php:12
$rows
Определения options.php:264
$rs
Определения action.php:82
$k
Определения template_pdf.php:567
$GLOBALS['_____370096793']
Определения update_client.php:1
$arFilter
Определения user_search.php:106