87 'autocomplete' =>
true,
92 'validation' => array(__CLASS__,
'validateDiscountId'),
96 'values' => array(
'N',
'Y'),
97 'default_value' =>
'Y',
101 'default_value' =>
null,
105 'default_value' =>
null,
110 'validation' => array(__CLASS__,
'validateCoupon'),
114 'validation' => array(__CLASS__,
'validateType'),
118 'default_value' => 0,
122 'default_value' => 0,
126 'default_value' => 0,
130 'default_value' =>
null,
137 'MODIFIED_BY' =>
new Main\Entity\IntegerField(
'MODIFIED_BY', array(
138 'default_value' =>
null,
141 'DATE_CREATE' =>
new Main\Entity\DatetimeField(
'DATE_CREATE', array(
145 'CREATED_BY' =>
new Main\Entity\IntegerField(
'CREATED_BY', array(
146 'default_value' =>
null,
149 'DESCRIPTION' =>
new Main\Entity\TextField(
'DESCRIPTION', array(
150 'default_value' =>
null,
153 'CREATED_BY_USER' =>
new Main\Entity\ReferenceField(
156 array(
'=this.CREATED_BY' =>
'ref.ID'),
157 array(
'join_type' =>
'LEFT')
159 'MODIFIED_BY_USER' =>
new Main\Entity\ReferenceField(
162 array(
'=this.MODIFIED_BY' =>
'ref.ID'),
163 array(
'join_type' =>
'LEFT')
165 'DISCOUNT' =>
new Main\Entity\ReferenceField(
167 'Bitrix\Sale\Internals\Discount',
168 array(
'=this.DISCOUNT_ID' =>
'ref.ID'),
169 array(
'join_type' =>
'LEFT')
260 $value = trim((
string)$value);
264 $existCoupon = Sale\DiscountCouponsManager::isExist($value);
265 if (!empty($existCoupon))
267 $currentId = (int)(is_array($primary) ? $primary[
'ID'] : $primary);
268 if ($existCoupon[
'MODULE'] !=
'sale' || $currentId != $existCoupon[
'ID'])
282 $result =
new Main\Entity\EventResult;
283 $data = $event->getParameter(
'fields');
285 $modifyFieldList = array();
286 self::setUserID($modifyFieldList, $data, array(
'CREATED_BY',
'MODIFIED_BY'));
289 if (!empty($modifyFieldList))
290 $result->modifyFields($modifyFieldList);
291 unset($modifyFieldList, $data);
321 $result =
new Main\Entity\EventResult;
322 $data = $event->getParameter(
'fields');
324 $modifyFieldList = array();
328 if (!empty($modifyFieldList))
329 $result->modifyFields($modifyFieldList);
330 unset($modifyFieldList, $data);
343 if (!self::isCheckedCouponsUse())
345 $data = $event->getParameter(
'fields');
346 if (isset($data[
'DISCOUNT_ID']))
348 $data[
'DISCOUNT_ID'] = (int)$data[
'DISCOUNT_ID'];
349 $coupon = static::getList(array(
350 'select' => array(
'ID',
'DISCOUNT_ID'),
351 'filter' => array(
'=ID' => $event->getParameter(
'id'))
355 $coupon[
'DISCOUNT_ID'] = (int)$coupon[
'DISCOUNT_ID'];
356 if ($coupon[
'DISCOUNT_ID'] !== $data[
'DISCOUNT_ID'])
358 self::$discountCheckList[$data[
'DISCOUNT_ID']] = $data[
'DISCOUNT_ID'];
359 self::$discountCheckList[$coupon[
'DISCOUNT_ID']] = $coupon[
'DISCOUNT_ID'];
492 if (!self::isCheckedCouponsUse() || empty(self::$discountCheckList))
495 Main\Type\Collection::normalizeArrayValuesByInt(self::$discountCheckList);
496 if (empty(self::$discountCheckList))
499 $withoutCoupons = array_fill_keys(self::$discountCheckList,
true);
500 $withCoupons = array();
501 $couponIterator = DiscountCouponTable::getList(array(
503 'filter' => array(
'@DISCOUNT_ID' => self::$discountCheckList),
504 'group' => array(
'DISCOUNT_ID')
506 while ($coupon = $couponIterator->fetch())
508 $coupon[
'CNT'] = (int)$coupon[
'CNT'];
509 if ($coupon[
'CNT'] > 0)
511 $coupon[
'DISCOUNT_ID'] = (int)$coupon[
'DISCOUNT_ID'];
512 unset($withoutCoupons[$coupon[
'DISCOUNT_ID']]);
513 $withCoupons[$coupon[
'DISCOUNT_ID']] =
true;
516 unset($coupon, $couponIterator);
517 if (!empty($withoutCoupons))
519 $withoutCoupons = array_keys($withoutCoupons);
522 if (!empty($withCoupons))
524 $withCoupons = array_keys($withCoupons);
527 unset($withCoupons, $withoutCoupons);
529 static::clearDiscountCheckList();
540 $discount = (int)$discount;
544 $couponsList = array();
545 $couponIterator = self::getList(array(
546 'select' => array(
'ID'),
547 'filter' => array(
'=DISCOUNT_ID' => $discount)
549 while ($coupon = $couponIterator->fetch())
550 $couponsList[] = $coupon[
'ID'];
551 unset($coupon, $couponIterator);
552 if (!empty($couponsList))
555 $helper = $conn->getSqlHelper();
557 'delete from '.$helper->quote(self::getTableName()).
' where '.$helper->quote(
'DISCOUNT_ID').
' = '.$discount
559 $event =
new Main\Event(
'sale', self::EVENT_ON_AFTER_DELETE_DISCOUNT, array($discount, $couponsList));
574 $currentTimestamp = $currentTime->getTimestamp();
575 if ($userId ===
null || (
int)$userId == 0)
577 $userId = (int)$userId;
578 if (!is_array($coupons))
579 $coupons = array($coupons);
582 Main\Type\Collection::normalizeArrayValuesByInt($coupons);
587 $deactivateCoupons = array();
588 $incrementalCoupons = array();
589 $limitedCoupons = array();
590 $couponIterator = self::getList(array(
592 'ID',
'COUPON',
'DISCOUNT_ID',
'TYPE',
'ACTIVE',
'MAX_USE',
'USE_COUNT',
'USER_ID',
'ACTIVE_TO',
'ACTIVE_FROM',
593 'DISCOUNT_ACTIVE' =>
'DISCOUNT.ACTIVE',
594 'DISCOUNT_ACTIVE_FROM' =>
'DISCOUNT.ACTIVE_FROM',
'DISCOUNT_ACTIVE_TO' =>
'DISCOUNT.ACTIVE_TO'
596 'filter' => array(
'@ID' => $coupons),
597 'order' => array(
'ID' =>
'ASC')
599 while ($existCoupon = $couponIterator->fetch())
601 $couponCode = $existCoupon[
'COUPON'];
602 if ($existCoupon[
'DISCOUNT_ACTIVE'] !=
'Y')
604 $errorList[$couponCode] =
Loc::getMessage(
'DISCOUNT_COUPON_SAVE_ERROR_DISCOUNT_INACTIVE');
608 ($existCoupon[
'DISCOUNT_ACTIVE_FROM'] instanceof Main\
Type\
DateTime && $existCoupon[
'DISCOUNT_ACTIVE_FROM']->getTimestamp() > $currentTimestamp)
610 ($existCoupon[
'DISCOUNT_ACTIVE_TO'] instanceof Main\
Type\
DateTime && $existCoupon[
'DISCOUNT_ACTIVE_TO']->getTimestamp() < $currentTimestamp)
613 $errorList[$couponCode] =
Loc::getMessage(
'DISCOUNT_COUPON_SAVE_ERROR_DISCOUNT_WRONG_ACTIVE_PERIOD');
617 $existCoupon[
'USER_ID'] = (int)$existCoupon[
'USER_ID'];
618 if ($existCoupon[
'USER_ID'] > 0 && $existCoupon[
'USER_ID'] != $userId)
620 $errorList[$couponCode] =
Loc::getMessage(
'DISCOUNT_COUPON_SAVE_ERROR_WRONG_USER_COUPON');
624 ($existCoupon[
'ACTIVE_FROM'] instanceof Main\
Type\
DateTime && $existCoupon[
'ACTIVE_FROM']->getTimestamp() > $currentTimestamp)
626 ($existCoupon[
'ACTIVE_TO'] instanceof Main\
Type\
DateTime && $existCoupon[
'ACTIVE_TO']->getTimestamp() < $currentTimestamp)
629 $errorList[$couponCode] =
Loc::getMessage(
'DISCOUNT_COUPON_SAVE_ERROR_COUPON_WRONG_ACTIVE_PERIOD');
632 if ($existCoupon[
'ACTIVE'] !=
'Y')
634 switch ($existCoupon[
'TYPE'])
636 case self::TYPE_BASKET_ROW:
637 $errorList[$couponCode] =
Loc::getMessage(
'DISCOUNT_COUPON_SAVE_ERROR_COUPON_BASKET_ROW_INACTIVE');
640 $errorList[$couponCode] =
Loc::getMessage(
'DISCOUNT_COUPON_SAVE_ERROR_COUPON_ONE_ORDER_INACTIVE');
643 $errorList[$couponCode] =
Loc::getMessage(
'DISCOUNT_COUPON_SAVE_ERROR_COUPON_INACTIVE');
650 $existCoupon[
'TYPE'] == self::TYPE_BASKET_ROW
651 || $existCoupon[
'TYPE'] == self::TYPE_ONE_ORDER
654 $deactivateCoupons[$couponCode] = $existCoupon[
'ID'];
656 elseif ($existCoupon[
'TYPE'] == self::TYPE_MULTI_ORDER)
658 $existCoupon[
'MAX_USE'] = (int)$existCoupon[
'MAX_USE'];
659 $existCoupon[
'USE_COUNT'] = (int)$existCoupon[
'USE_COUNT'];
661 if ($existCoupon[
'MAX_USE'] > 0 && $existCoupon[
'USE_COUNT'] >= $existCoupon[
'MAX_USE'])
663 $errorList[$couponCode] =
Loc::getMessage(
'DISCOUNT_COUPON_SAVE_ERROR_COUPON_MAX_USE_LIMIT');
666 if ($existCoupon[
'MAX_USE'] > 0 && $existCoupon[
'USE_COUNT'] >= ($existCoupon[
'MAX_USE'] - 1))
668 $limitedCoupons[$existCoupon[
'COUPON']] = $existCoupon[
'ID'];
672 $incrementalCoupons[$existCoupon[
'COUPON']] = $existCoupon[
'ID'];
677 unset($existCoupon, $couponIterator);
678 if (!empty($errorList))
682 'ERROR' => $errorList,
688 if (!empty($deactivateCoupons) || !empty($limitedCoupons) || !empty($incrementalCoupons))
691 $helper = $conn->getSqlHelper();
692 $tableName = $helper->quote(self::getTableName());
693 if (!empty($deactivateCoupons))
696 'update '.$tableName.
' set '.$helper->quote(
'ACTIVE').
' = \'N\', '.$helper->quote(
'DATE_APPLY').
' = '.$helper->getCurrentDateTimeFunction().
697 ' where '.$helper->quote(
'ID').
' in ('.implode(
',', $deactivateCoupons).
')'
700 if (!empty($incrementalCoupons))
703 'update '.$tableName.
' set '.$helper->quote(
'DATE_APPLY').
' = '.$helper->getCurrentDateTimeFunction().
', '.
704 $helper->quote(
'USE_COUNT').
' = '.$helper->quote(
'USE_COUNT').
' + 1'.
705 ' where '.$helper->quote(
'ID').
' in ('.implode(
',', $incrementalCoupons).
')'
708 if (!empty($limitedCoupons))
711 'update '.$tableName.
' set '.$helper->quote(
'DATE_APPLY').
' = '.$helper->getCurrentDateTimeFunction().
', '.
712 $helper->quote(
'ACTIVE').
' = \'N\', '.$helper->quote(
'USE_COUNT').
' = '.$helper->quote(
'USE_COUNT').
' + 1'.
713 ' where '.$helper->quote(
'ID').
' in ('.implode(
',', $limitedCoupons).
')'
716 unset($tableName, $helper);
721 'DEACTIVATE' => $deactivateCoupons,
722 'LIMITED' => $limitedCoupons,
723 'INCREMENT' => $incrementalCoupons
770 $partOne .= mb_substr($allchars, rand(0, $charsLen), 1);
773 $partTwo .= mb_substr($allchars, rand(0, $charsLen), 1);
793 public static function addPacket(array $data, $count, $limit = 0)
795 $result =
new Main\Entity\Result();
796 $result->setData(array(
802 $count = (int)$count;
810 foreach (static::getEntity()->getFields() as $field)
812 if ($field instanceof Main\
Entity\
ScalarField && !array_key_exists($field->getName(), $data))
814 $defaultValue = $field->getDefaultValue();
816 if ($defaultValue !==
null)
817 $data[$field->getName()] = $field->getDefaultValue();
820 $checkResult = static::checkPacket($data,
false);
821 if (!$checkResult->isSuccess())
823 foreach ($checkResult->getErrors() as $checkError)
825 $result->addError($checkError);
831 $discountIterator = DiscountTable::getList(array(
832 'select' => array(
'ID',
'USE_COUPONS'),
833 'filter' => array(
'=ID' => $data[
'DISCOUNT_ID'])
835 if ($discount = $discountIterator->fetch())
837 $useCoupons = ($discount[
'USE_COUPONS'] ==
'Y');
846 if (!$result->isSuccess(
true))
849 self::setDiscountCheckList($data[
'DISCOUNT_ID']);
850 self::disableCheckCouponsUse();
851 $limit = (int)$limit;
858 $data[
'COUPON'] = self::generateCoupon(
true);
859 $couponResult = self::add($data);
860 if ($couponResult->isSuccess())
863 }
while ($resultCount < $count && $all < $limit);
864 $result->setData(array(
865 'result' => $resultCount,
870 if ($resultCount == 0)
875 :
Loc::getMessage(
'DISCOUNT_COUPON_PACKET_NEW_GENERATE_COUPON_ZERO_ERR')
879 self::clearDiscountCheckList();
881 elseif ($resultCount < $count)
885 'DISCOUNT_COUPON_PACKET_GENERATE_COUPON_COUNT_ERR',
887 '#RESULT#' => $resultCount,
895 self::enableCheckCouponsUse();
896 self::updateUseCoupons();
1000 protected static function setUserID(array &$result, array $data, array $keys)
1002 static $currentUserID =
false;
1003 if ($currentUserID ===
false)
1007 $currentUserID = (isset($USER) && $USER instanceof \CUser ? (int)$USER->getID() :
null);
1009 foreach ($keys as $oneKey)
1012 if (array_key_exists($oneKey, $data))
1013 $setField = ($data[$oneKey] !==
null && (
int)$data[$oneKey] <= 0);
1016 $result[$oneKey] = $currentUserID;
1029 protected static function setTimestamp(array &$result, array $data, array $keys)
1031 foreach ($keys as $oneKey)
1034 if (array_key_exists($oneKey, $data))
1035 $setField = ($data[$oneKey] !==
null && !is_object($data[$oneKey]));