78 'autocomplete' =>
true,
83 'validation' => array(__CLASS__,
'validateDiscountId'),
87 'values' => array(
'N',
'Y'),
88 'default_value' =>
'Y',
93 'validation' => array(__CLASS__,
'validateCoupon'),
97 'default_value' =>
null,
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,
107 'default_value' =>
function()
113 'MODIFIED_BY' =>
new Main\Entity\IntegerField(
'MODIFIED_BY', array(
114 'default_value' =>
null,
117 'DATE_CREATE' =>
new Main\Entity\DatetimeField(
'DATE_CREATE', array(
118 'default_value' =>
function()
124 'CREATED_BY' =>
new Main\Entity\IntegerField(
'CREATED_BY', array(
125 'default_value' =>
null,
128 'DESCRIPTION' =>
new Main\Entity\TextField(
'DESCRIPTION', array(
129 'default_value' =>
null,
132 'CREATED_BY_USER' =>
new Main\Entity\ReferenceField(
135 array(
'=this.CREATED_BY' =>
'ref.ID')
137 'MODIFIED_BY_USER' =>
new Main\Entity\ReferenceField(
140 array(
'=this.MODIFIED_BY' =>
'ref.ID')
142 'DISCOUNT' =>
new Main\Entity\ReferenceField(
144 '\Bitrix\Catalog\Discount',
145 array(
'=this.DISCOUNT_ID' =>
'ref.ID'),
146 array(
'join_type' =>
'LEFT')
204 $value = trim((
string)$value);
208 if (self::$existCouponsManager ===
null)
210 if (self::$existCouponsManager)
212 $existCoupon = Sale\DiscountCouponsManager::isExist($value);
213 if (!empty($existCoupon))
215 $currentId = (int)(is_array($primary) ? $primary[
'ID'] : $primary);
216 if ($existCoupon[
'MODULE'] !=
'catalog' || $currentId != $existCoupon[
'ID'])
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),
228 if ($existCoupon = $couponIterator->fetch())
242 $result =
new Main\Entity\EventResult;
243 $data = $event->getParameter(
'fields');
245 $modifyFieldList = array();
246 self::setUserID($modifyFieldList, $data, array(
'CREATED_BY',
'MODIFIED_BY'));
249 if (!empty($modifyFieldList))
250 $result->modifyFields($modifyFieldList);
251 unset($modifyFieldList);
264 $result =
new Main\Entity\EventResult;
265 $data = $event->getParameter(
'fields');
267 $modifyFieldList = array();
271 if (!empty($modifyFieldList))
272 $result->modifyFields($modifyFieldList);
273 unset($modifyFieldList);
305 if (self::$existCouponsManager ===
null)
307 if (!self::$existCouponsManager)
315 Main\EventResult::SUCCESS,
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'),
336 if (self::$existCouponsManager ===
null)
339 $coupon = trim($coupon);
343 $couponIterator = self::getList(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'
349 'filter' => array(
'=COUPON' => $coupon)
351 $existCoupon = $couponIterator->fetch();
352 unset($couponIterator);
353 if (!empty($existCoupon))
355 if (!empty(self::$types))
357 $existCoupon[
'TYPE'] = (
358 isset(self::$types[$existCoupon[
'TYPE']])
359 ? self::$types[$existCoupon[
'TYPE']]
407 $currentTimestamp = $currentTime->getTimestamp();
408 if ($userId ===
null || (
int)$userId == 0)
410 if (!is_array($coupons))
411 $coupons = array($coupons);
414 Main\Type\Collection::normalizeArrayValuesByInt($coupons);
418 $deactivateCoupons = array();
419 $multiCoupons = array();
420 $couponIterator = self::getList(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'
426 'filter' => array(
'@ID' => $coupons,
'=ACTIVE' =>
'Y'),
427 'order' => array(
'ID' =>
'ASC')
429 while ($existCoupon = $couponIterator->fetch())
431 if ($existCoupon[
'DISCOUNT_ACTIVE'] !=
'Y')
434 ($existCoupon[
'DISCOUNT_ACTIVE_FROM'] instanceof Main\
Type\
DateTime && $existCoupon[
'DISCOUNT_ACTIVE_FROM']->getTimestamp() > $currentTimestamp)
436 ($existCoupon[
'DISCOUNT_ACTIVE_TO'] instanceof Main\
Type\
DateTime && $existCoupon[
'DISCOUNT_ACTIVE_TO']->getTimestamp() < $currentTimestamp)
441 $existCoupon[
'TYPE'] == self::TYPE_ONE_ROW
442 || $existCoupon[
'TYPE'] == self::TYPE_ONE_ORDER
445 $deactivateCoupons[$existCoupon[
'COUPON']] = $existCoupon[
'ID'];
449 $multiCoupons[$existCoupon[
'COUPON']] = $existCoupon[
'ID'];
452 unset($existCoupon, $couponIterator);
453 if (!empty($deactivateCoupons) || !empty($multiCoupons))
456 $helper = $conn->getSqlHelper();
457 $tableName = $helper->quote(self::getTableName());
458 if (!empty($deactivateCoupons))
461 'update '.$tableName.
' set '.$helper->quote(
'ACTIVE').
' = \'N\', '.$helper->quote(
'DATE_APPLY').
' = '.$helper->getCurrentDateTimeFunction().
462 ' where '.$helper->quote(
'ID').
' in ('.implode(
',', $deactivateCoupons).
')'
465 if (!empty($multiCoupons))
468 'update '.$tableName.
' set '.$helper->quote(
'DATE_APPLY').
' = '.$helper->getCurrentDateTimeFunction().
469 ' where '.$helper->quote(
'ID').
' in ('.implode(
',', $multiCoupons).
')'
472 unset($tableName, $helper);
475 'DEACTIVATE' => $deactivateCoupons,
476 'INCREMENT' => $multiCoupons
539 protected static function setUserID(&$result, $data, $keys)
541 static $currentUserID =
false;
542 if ($currentUserID ===
false)
545 $currentUserID = (isset($USER) && $USER instanceof \CUser ? (int)$USER->getID() :
null);
547 foreach ($keys as &$oneKey)
550 if (array_key_exists($oneKey, $data))
551 $setField = ($data[$oneKey] !==
null && (
int)$data[$oneKey] <= 0);
554 $result[$oneKey] = $currentUserID;