Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
discountcoupon.php
1<?php
2namespace Bitrix\Catalog;
3
8
9Loc::loadMessages(__FILE__);
10
48class DiscountCouponTable extends Main\Entity\DataManager
49{
50 const TYPE_ONE_ROW = 'Y';
51 const TYPE_ONE_ORDER = 'O';
52 const TYPE_NO_LIMIT = 'N';
53
54 protected static $existCouponsManager = null;
55 protected static $types = array();
56 protected static $typeUnknown = false;
57
63 public static function getTableName()
64 {
65 return 'b_catalog_discount_coupon';
66 }
67
73 public static function getMap()
74 {
75 return array(
76 'ID' => new Main\Entity\IntegerField('ID', array(
77 'primary' => true,
78 'autocomplete' => true,
79 'title' => Loc::getMessage('DISCOUNT_COUPON_ENTITY_ID_FIELD')
80 )),
81 'DISCOUNT_ID' => new Main\Entity\IntegerField('DISCOUNT_ID', array(
82 'required' => true,
83 'validation' => array(__CLASS__, 'validateDiscountId'),
84 'title' => Loc::getMessage('DISCOUNT_COUPON_ENTITY_DISCOUNT_ID_FIELD')
85 )),
86 'ACTIVE' => new Main\Entity\BooleanField('ACTIVE', array(
87 'values' => array('N', 'Y'),
88 'default_value' => 'Y',
89 'title' => Loc::getMessage('DISCOUNT_COUPON_ENTITY_ACTIVE_FIELD')
90 )),
91 'COUPON' => new Main\Entity\StringField('COUPON', array(
92 'required' => true,
93 'validation' => array(__CLASS__, 'validateCoupon'),
94 'title' => Loc::getMessage('DISCOUNT_COUPON_ENTITY_COUPON_FIELD')
95 )),
96 'DATE_APPLY' => new Main\Entity\DatetimeField('DATE_APPLY', array(
97 'default_value' => null,
98 'title' => Loc::getMessage('DISCOUNT_COUPON_ENTITY_DATE_APPLY_FIELD')
99 )),
100 'TYPE' => new Main\Entity\EnumField('TYPE', array(
101 'column_name' => 'ONE_TIME',
102 'values' => array(self::TYPE_ONE_ROW, self::TYPE_ONE_ORDER, self::TYPE_NO_LIMIT),
103 'default_value' => self::TYPE_ONE_ORDER,
104 'title' => Loc::getMessage('DISCOUNT_COUPON_ENTITY_ONE_TIME_FIELD')
105 )),
106 'TIMESTAMP_X' => new Main\Entity\DatetimeField('TIMESTAMP_X', array(
107 'default_value' => function()
108 {
109 return new Main\Type\DateTime();
110 },
111 'title' => Loc::getMessage('DISCOUNT_COUPON_ENTITY_TIMESTAMP_X_FIELD')
112 )),
113 'MODIFIED_BY' => new Main\Entity\IntegerField('MODIFIED_BY', array(
114 'default_value' => null,
115 'title' => Loc::getMessage('DISCOUNT_COUPON_ENTITY_MODIFIED_BY_FIELD')
116 )),
117 'DATE_CREATE' => new Main\Entity\DatetimeField('DATE_CREATE', array(
118 'default_value' => function()
119 {
120 return new Main\Type\DateTime();
121 },
122 'title' => Loc::getMessage('DISCOUNT_COUPON_ENTITY_DATE_CREATE_FIELD')
123 )),
124 'CREATED_BY' => new Main\Entity\IntegerField('CREATED_BY', array(
125 'default_value' => null,
126 'title' => Loc::getMessage('DISCOUNT_COUPON_ENTITY_CREATED_BY_FIELD')
127 )),
128 'DESCRIPTION' => new Main\Entity\TextField('DESCRIPTION', array(
129 'default_value' => null,
130 'title' => Loc::getMessage('DISCOUNT_COUPON_ENTITY_DESCRIPTION_FIELD')
131 )),
132 'CREATED_BY_USER' => new Main\Entity\ReferenceField(
133 'CREATED_BY_USER',
134 '\Bitrix\Main\User',
135 array('=this.CREATED_BY' => 'ref.ID')
136 ),
137 'MODIFIED_BY_USER' => new Main\Entity\ReferenceField(
138 'MODIFIED_BY_USER',
139 '\Bitrix\Main\User',
140 array('=this.MODIFIED_BY' => 'ref.ID')
141 ),
142 'DISCOUNT' => new Main\Entity\ReferenceField(
143 'DISCOUNT',
144 '\Bitrix\Catalog\Discount',
145 array('=this.DISCOUNT_ID' => 'ref.ID'),
146 array('join_type' => 'LEFT')
147 )
148 );
149 }
150
156 public static function validateDiscountId()
157 {
158 return array(
159 array(__CLASS__, 'checkDiscountId')
160 );
161 }
162
168 public static function validateCoupon()
169 {
170 return array(
171 new Main\Entity\Validator\Length(null, 32),
172 array(__CLASS__, 'checkCoupon')
173 );
174 }
175
185 public static function checkDiscountId($value, $primary, array $row, Main\Entity\Field $field)
186 {
187 if ((int)$value <= 0)
188 return Loc::getMessage('DISCOUNT_COUPON_VALIDATOR_DISCOUNT_ID');
189
190 return true;
191 }
192
202 public static function checkCoupon($value, $primary, array $row, Main\Entity\Field $field)
203 {
204 $value = trim((string)$value);
205 if ($value == '')
206 return Loc::getMessage('DISCOUNT_COUPON_VALIDATOR_COUPON_EMPTY');
207
208 if (self::$existCouponsManager === null)
210 if (self::$existCouponsManager)
211 {
212 $existCoupon = Sale\DiscountCouponsManager::isExist($value);
213 if (!empty($existCoupon))
214 {
215 $currentId = (int)(is_array($primary) ? $primary['ID'] : $primary);
216 if ($existCoupon['MODULE'] != 'catalog' || $currentId != $existCoupon['ID'])
217 return Loc::getMessage('DISCOUNT_COUPON_VALIDATOR_COUPON_EXIST');
218 }
219 }
220 else
221 {
222 $currentId = (int)(is_array($primary) ? $primary['ID'] : $primary);
223 $couponIterator = self::getList(array(
224 'select' => array('ID'),
225 'filter' => array('=COUPON' => $value, '!=ID' => $currentId),
226 'limit' => 1
227 ));
228 if ($existCoupon = $couponIterator->fetch())
229 return Loc::getMessage('DISCOUNT_COUPON_VALIDATOR_COUPON_EXIST');
230 }
231 return true;
232 }
233
240 public static function onBeforeAdd(Main\Entity\Event $event)
241 {
242 $result = new Main\Entity\EventResult;
243 $data = $event->getParameter('fields');
244
245 $modifyFieldList = array();
246 self::setUserID($modifyFieldList, $data, array('CREATED_BY', 'MODIFIED_BY'));
247 self::setTimestamp($modifyFieldList, $data, array('DATE_CREATE', 'TIMESTAMP_X'));
248
249 if (!empty($modifyFieldList))
250 $result->modifyFields($modifyFieldList);
251 unset($modifyFieldList);
252
253 return $result;
254 }
255
262 public static function onBeforeUpdate(Main\Entity\Event $event)
263 {
264 $result = new Main\Entity\EventResult;
265 $data = $event->getParameter('fields');
266
267 $modifyFieldList = array();
268 self::setUserID($modifyFieldList, $data, array('MODIFIED_BY'));
269 self::setTimestamp($modifyFieldList, $data, array('TIMESTAMP_X'));
270
271 if (!empty($modifyFieldList))
272 $result->modifyFields($modifyFieldList);
273 unset($modifyFieldList);
274
275 return $result;
276 }
277
284 public static function deleteByDiscount($discount)
285 {
286 $discount = (int)$discount;
287 if ($discount <= 0)
288 return;
289 $conn = Main\Application::getConnection();
290 $helper = $conn->getSqlHelper();
291 $conn->queryExecute(
292 'delete from '.$helper->quote(self::getTableName()).' where '.$helper->quote('DISCOUNT_ID').' = '.$discount
293 );
294 unset($helper, $conn);
295 }
296
303 public static function couponManager(Main\Event $event)
304 {
305 if (self::$existCouponsManager === null)
307 if (!self::$existCouponsManager)
308 {
309 $result = new Main\EventResult(Main\EventResult::ERROR, null, 'catalog');
310 }
311 else
312 {
313 self::initTypes($event);
314 $result = new Main\EventResult(
315 Main\EventResult::SUCCESS,
316 array(
317 'mode' => Sale\DiscountCouponsManager::COUPON_MODE_SIMPLE,
318 'getData' => array('\Bitrix\Catalog\DiscountCouponTable', 'getData'),
319 'isExist' => array('\Bitrix\Catalog\DiscountCouponTable', 'isExist'),
320 'saveApplied' => array('\Bitrix\Catalog\DiscountCouponTable', 'saveApplied'),
321 ),
322 'catalog'
323 );
324 }
325 return $result;
326 }
327
334 public static function getData($coupon)
335 {
336 if (self::$existCouponsManager === null)
338
339 $coupon = trim($coupon);
340 if ($coupon === '')
341 return false;
342
343 $couponIterator = self::getList(array(
344 'select' => array(
345 'ID', 'COUPON', 'DISCOUNT_ID', 'TYPE', 'ACTIVE',
346 'DISCOUNT_NAME' => 'DISCOUNT.NAME', 'DISCOUNT_ACTIVE' => 'DISCOUNT.ACTIVE',
347 'DISCOUNT_ACTIVE_FROM' => 'DISCOUNT.ACTIVE_FROM', 'DISCOUNT_ACTIVE_TO' => 'DISCOUNT.ACTIVE_TO'
348 ),
349 'filter' => array('=COUPON' => $coupon)
350 ));
351 $existCoupon = $couponIterator->fetch();
352 unset($couponIterator);
353 if (!empty($existCoupon))
354 {
355 if (!empty(self::$types))
356 {
357 $existCoupon['TYPE'] = (
358 isset(self::$types[$existCoupon['TYPE']])
359 ? self::$types[$existCoupon['TYPE']]
361 );
362 }
363 return $existCoupon;
364 }
365 return false;
366 }
367
374 public static function isExist($coupon)
375 {
376 $coupon = trim($coupon);
377 if ($coupon === '')
378 return false;
379
380 $couponIterator = self::getList(array(
381 'select' => array('ID', 'COUPON'),
382 'filter' => array('=COUPON' => $coupon)
383 ));
384 $existCoupon = $couponIterator->fetch();
385 unset($couponIterator);
386 if (!empty($existCoupon))
387 {
388 return array(
389 'ID' => $existCoupon['ID'],
390 'COUPON' => $existCoupon['COUPON'],
391 'MODULE' => 'catalog'
392 );
393 }
394 return false;
395 }
396
405 public static function saveApplied($coupons, $userId, Main\Type\DateTime $currentTime)
406 {
407 $currentTimestamp = $currentTime->getTimestamp();
408 if ($userId === null || (int)$userId == 0)
409 return false;
410 if (!is_array($coupons))
411 $coupons = array($coupons);
412 if (empty($coupons))
413 return false;
414 Main\Type\Collection::normalizeArrayValuesByInt($coupons);
415 if (empty($coupons))
416 return false;
417
418 $deactivateCoupons = array();
419 $multiCoupons = array();
420 $couponIterator = self::getList(array(
421 'select' => array(
422 'ID', 'COUPON', 'DISCOUNT_ID', 'TYPE', 'ACTIVE',
423 'DISCOUNT_ACTIVE' => 'DISCOUNT.ACTIVE',
424 'DISCOUNT_ACTIVE_FROM' => 'DISCOUNT.ACTIVE_FROM', 'DISCOUNT_ACTIVE_TO' => 'DISCOUNT.ACTIVE_TO'
425 ),
426 'filter' => array('@ID' => $coupons, '=ACTIVE' => 'Y'),
427 'order' => array('ID' => 'ASC')
428 ));
429 while ($existCoupon = $couponIterator->fetch())
430 {
431 if ($existCoupon['DISCOUNT_ACTIVE'] != 'Y')
432 continue;
433 if (
434 ($existCoupon['DISCOUNT_ACTIVE_FROM'] instanceof Main\Type\DateTime && $existCoupon['DISCOUNT_ACTIVE_FROM']->getTimestamp() > $currentTimestamp)
435 ||
436 ($existCoupon['DISCOUNT_ACTIVE_TO'] instanceof Main\Type\DateTime && $existCoupon['DISCOUNT_ACTIVE_TO']->getTimestamp() < $currentTimestamp)
437 )
438 continue;
439
440 if (
441 $existCoupon['TYPE'] == self::TYPE_ONE_ROW
442 || $existCoupon['TYPE'] == self::TYPE_ONE_ORDER
443 )
444 {
445 $deactivateCoupons[$existCoupon['COUPON']] = $existCoupon['ID'];
446 }
447 else
448 {
449 $multiCoupons[$existCoupon['COUPON']] = $existCoupon['ID'];
450 }
451 }
452 unset($existCoupon, $couponIterator);
453 if (!empty($deactivateCoupons) || !empty($multiCoupons))
454 {
456 $helper = $conn->getSqlHelper();
457 $tableName = $helper->quote(self::getTableName());
458 if (!empty($deactivateCoupons))
459 {
460 $conn->queryExecute(
461 'update '.$tableName.' set '.$helper->quote('ACTIVE').' = \'N\', '.$helper->quote('DATE_APPLY').' = '.$helper->getCurrentDateTimeFunction().
462 ' where '.$helper->quote('ID').' in ('.implode(',', $deactivateCoupons).')'
463 );
464 }
465 if (!empty($multiCoupons))
466 {
467 $conn->queryExecute(
468 'update '.$tableName.' set '.$helper->quote('DATE_APPLY').' = '.$helper->getCurrentDateTimeFunction().
469 ' where '.$helper->quote('ID').' in ('.implode(',', $multiCoupons).')'
470 );
471 }
472 unset($tableName, $helper);
473 }
474 return array(
475 'DEACTIVATE' => $deactivateCoupons,
476 'INCREMENT' => $multiCoupons
477 );
478 }
479
486 public static function getCouponTypes($extendedMode = false)
487 {
488 $extendedMode = ($extendedMode === true);
489 if ($extendedMode)
490 {
491 return array(
492 self::TYPE_ONE_ROW => Loc::getMessage('DISCOUNT_COUPON_TABLE_TYPE_ONE_ROW'),
493 self::TYPE_ONE_ORDER => Loc::getMessage('DISCOUNT_COUPON_TABLE_TYPE_ONE_ORDER'),
494 self::TYPE_NO_LIMIT => Loc::getMessage('DISCOUNT_COUPON_TABLE_TYPE_NO_LIMIT')
495 );
496 }
497 return array(self::TYPE_ONE_ROW, self::TYPE_ONE_ORDER, self::TYPE_NO_LIMIT);
498 }
499
505 protected static function initUseMode()
506 {
507 if (self::$existCouponsManager === null)
508 self::$existCouponsManager = Main\ModuleManager::isModuleInstalled('sale') && Main\Loader::includeModule('sale');
509 }
510
517 protected static function initTypes(Main\Event $event)
518 {
519 if (self::$existCouponsManager === null)
521 if (!self::$existCouponsManager)
522 return;
523 self::$types = array(
524 self::TYPE_ONE_ROW => Sale\Internals\DiscountCouponTable::TYPE_BASKET_ROW,
525 self::TYPE_ONE_ORDER => Sale\Internals\DiscountCouponTable::TYPE_ONE_ORDER,
526 self::TYPE_NO_LIMIT => Sale\Internals\DiscountCouponTable::TYPE_MULTI_ORDER
527 );
528 self::$typeUnknown = $event->getParameter('COUPON_UNKNOWN');
529 }
530
539 protected static function setUserID(&$result, $data, $keys)
540 {
541 static $currentUserID = false;
542 if ($currentUserID === false)
543 {
544 global $USER;
545 $currentUserID = (isset($USER) && $USER instanceof \CUser ? (int)$USER->getID() : null);
546 }
547 foreach ($keys as &$oneKey)
548 {
549 $setField = true;
550 if (array_key_exists($oneKey, $data))
551 $setField = ($data[$oneKey] !== null && (int)$data[$oneKey] <= 0);
552
553 if ($setField)
554 $result[$oneKey] = $currentUserID;
555 }
556 unset($oneKey);
557 }
558
567 protected static function setTimestamp(&$result, $data, $keys)
568 {
569 foreach ($keys as &$oneKey)
570 {
571 $setField = true;
572 if (array_key_exists($oneKey, $data))
573 $setField = ($data[$oneKey] !== null && !is_object($data[$oneKey]));
574
575 if ($setField)
576 $result[$oneKey] = new Main\Type\DateTime();
577 }
578 unset($oneKey);
579 }
580}
static checkCoupon($value, $primary, array $row, Main\Entity\Field $field)
static getCouponTypes($extendedMode=false)
static setUserID(&$result, $data, $keys)
static onBeforeAdd(Main\Entity\Event $event)
static setTimestamp(&$result, $data, $keys)
static initTypes(Main\Event $event)
static couponManager(Main\Event $event)
static onBeforeUpdate(Main\Entity\Event $event)
static saveApplied($coupons, $userId, Main\Type\DateTime $currentTime)
static checkDiscountId($value, $primary, array $row, Main\Entity\Field $field)
static getConnection($name="")
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29