97 return 'b_sale_discount';
110 'autocomplete' =>
true,
114 'validation' => array(__CLASS__,
'validateXmlId'),
119 'validation' => array(__CLASS__,
'validateLid'),
123 'validation' => array(__CLASS__,
'validateName'),
129 'validation' => array(__CLASS__,
'validateCurrency'),
134 'default_value' =>
'P',
135 'validation' => array(__CLASS__,
'validateDiscountType')
138 'values' => array(
'N',
'Y'),
139 'default_value' =>
'Y',
146 'default_value' =>
null,
150 'default_value' =>
null,
157 'MODIFIED_BY' =>
new Main\Entity\IntegerField(
'MODIFIED_BY', array(
158 'default_value' =>
null,
161 'DATE_CREATE' =>
new Main\Entity\DatetimeField(
'DATE_CREATE', array(
165 'CREATED_BY' =>
new Main\Entity\IntegerField(
'CREATED_BY', array(
166 'default_value' =>
null,
169 'PRIORITY' =>
new Main\Entity\IntegerField(
'PRIORITY', array(
170 'default_value' => 1,
173 'LAST_DISCOUNT' =>
new Main\Entity\BooleanField(
'LAST_DISCOUNT', array(
174 'values' => array(
'N',
'Y'),
175 'default_value' =>
'Y',
178 'LAST_LEVEL_DISCOUNT' =>
new Main\Entity\BooleanField(
'LAST_LEVEL_DISCOUNT', array(
179 'values' => array(
'N',
'Y'),
180 'default_value' =>
'N',
182 'VERSION' =>
new Main\Entity\EnumField(
'VERSION', array(
183 'values' => array(self::VERSION_OLD, self::VERSION_NEW, self::VERSION_15),
184 'default_value' => self::VERSION_15,
187 'CONDITIONS_LIST' =>
new Main\Entity\TextField(
'CONDITIONS_LIST', array(
188 'serialized' =>
true,
189 'column_name' =>
'CONDITIONS',
192 'CONDITIONS' =>
new Main\Entity\ExpressionField(
'CONDITIONS',
'%s',
'CONDITIONS_LIST'),
193 'UNPACK' =>
new Main\Entity\TextField(
'UNPACK', array()),
194 'ACTIONS_LIST' =>
new Main\Entity\TextField(
'ACTIONS_LIST', array(
195 'serialized' =>
true,
196 'column_name' =>
'ACTIONS',
199 'ACTIONS' =>
new Main\Entity\ExpressionField(
'ACTIONS',
'%s',
'ACTIONS_LIST'),
200 'APPLICATION' =>
new Main\Entity\TextField(
'APPLICATION', array()),
201 'PREDICTION_TEXT' =>
new Main\Entity\TextField(
'PREDICTION_TEXT', array()),
202 'PREDICTIONS_APP' =>
new Main\Entity\TextField(
'PREDICTIONS_APP', array()),
203 'PREDICTIONS_LIST' =>
new Main\Entity\TextField(
'PREDICTIONS_LIST', array(
204 'serialized' =>
true,
205 'column_name' =>
'PREDICTIONS',
207 'PREDICTIONS' =>
new Main\Entity\ExpressionField(
'PREDICTIONS',
'%s',
'PREDICTIONS_LIST'),
208 'USE_COUPONS' =>
new Main\Entity\BooleanField(
'USE_COUPONS', array(
209 'values' => array(
'N',
'Y'),
210 'default_value' =>
'N',
213 'EXECUTE_MODULE' =>
new Main\Entity\StringField(
'EXECUTE_MODULE', array(
214 'validation' => array(__CLASS__,
'validateExecuteModule'),
217 'EXECUTE_MODE' =>
new Main\Entity\IntegerField(
'EXECUTE_MODE', array(
218 'default_value' => self::EXECUTE_MODE_GENERAL,
220 'HAS_INDEX' =>
new Main\Entity\BooleanField(
'HAS_INDEX', array(
221 'values' => array(
'N',
'Y'),
222 'default_value' =>
'N',
224 'PRESET_ID' =>
new Main\Entity\StringField(
'PRESET_ID', array(
225 'validation' => array(__CLASS__,
'validatePresetId'),
227 'SHORT_DESCRIPTION_STRUCTURE' =>
new Main\Entity\TextField(
'SHORT_DESCRIPTION_STRUCTURE', array(
228 'serialized' =>
true,
229 'column_name' =>
'SHORT_DESCRIPTION',
231 'SHORT_DESCRIPTION' =>
new Main\Entity\ExpressionField(
'SHORT_DESCRIPTION',
'%s',
'SHORT_DESCRIPTION_STRUCTURE'),
232 'CREATED_BY_USER' =>
new Main\Entity\ReferenceField(
235 array(
'=this.CREATED_BY' =>
'ref.ID'),
236 array(
'join_type' =>
'LEFT')
238 'MODIFIED_BY_USER' =>
new Main\Entity\ReferenceField(
241 array(
'=this.MODIFIED_BY' =>
'ref.ID'),
242 array(
'join_type' =>
'LEFT')
244 'COUPON' =>
new Main\Entity\ReferenceField(
246 'Bitrix\Sale\Internals\DiscountCoupon',
247 array(
'=this.ID' =>
'ref.DISCOUNT_ID'),
248 array(
'join_type' =>
'LEFT')
250 'DISCOUNT_ENTITY' =>
new Main\Entity\ReferenceField(
252 'Bitrix\Sale\Internals\DiscountEntities',
253 array(
'=this.ID' =>
'ref.DISCOUNT_ID'),
254 array(
'join_type' =>
'LEFT')
266 new Main\
Entity\Validator\Length(
null, 255),
277 new Main\
Entity\Validator\Length(
null, 2),
288 new Main\
Entity\Validator\Length(
null, 255),
299 new Main\
Entity\Validator\Length(
null, 3),
310 new Main\
Entity\Validator\Length(
null, 1),
322 new Main\
Entity\Validator\Length(
null, 50),
334 new Main\
Entity\Validator\Length(
null, 255),
346 $result =
new Main\Entity\EventResult;
347 $fields = $event->getParameter(
'fields');
349 $modifyFieldList = array(
350 'DISCOUNT_VALUE' => 0,
351 'DISCOUNT_TYPE' =>
'P',
353 if (isset($fields[
'LID']))
355 self::setUserID($modifyFieldList, $fields, array(
'CREATED_BY',
'MODIFIED_BY'));
356 self::setTimestamp($modifyFieldList, $fields, array(
'DATE_CREATE',
'TIMESTAMP_X'));
357 self::setShortDescription($modifyFieldList, $fields);
359 self::copyOldFields($modifyFieldList, $fields);
360 $result->unsetField(
'CONDITIONS');
361 $result->unsetField(
'ACTIONS');
363 if (!empty($modifyFieldList))
364 $result->modifyFields($modifyFieldList);
365 unset($modifyFieldList);
378 $id = $event->getParameter(
'primary');
379 $fields = $event->getParameter(
'fields');
380 if (isset($fields[
'ACTIONS_LIST']))
382 if (!is_array($fields[
'ACTIONS_LIST']) && \CheckSerializedData($fields[
'ACTIONS_LIST']))
383 $fields[
'ACTIONS_LIST'] = unserialize($fields[
'ACTIONS_LIST'], [
'allowed_classes' =>
false]);
384 if (is_array($fields[
'ACTIONS_LIST']))
386 $giftManager = Gift\Manager::getInstance();
387 if ($giftManager->isContainGiftAction($fields))
389 if (!$giftManager->existsDiscountsWithGift())
390 $giftManager->enableExistenceDiscountsWithGift();
391 Gift\RelatedDataTable::fillByDiscount($fields + $id);
397 $specificFields = array(
398 'EXECUTE_MODE' => static::resolveExecuteModeByDiscountId($id),
401 if (isset($fields[
'CONDITIONS_LIST']))
403 $specificFields[
'HAS_INDEX'] = Index\Manager::getInstance()->indexDiscount($fields + $id) ?
'Y' :
'N';
406 static::updateSpecificFields($id[
'ID'], $specificFields);
407 static::updateConfigurationIfNeeded($fields, $specificFields);
409 self::dropIblockCache();
421 $fields = static::getRowById($discountId);
425 return self::EXECUTE_MODE_GENERAL;
428 return Analyzer::getInstance()->canCalculateSeparately($fields) ?
429 self::EXECUTE_MODE_SEPARATELY : self::EXECUTE_MODE_GENERAL;
442 isset($specificFields[
'EXECUTE_MODE']) &&
443 $specificFields[
'EXECUTE_MODE'] == self::EXECUTE_MODE_GENERAL &&
444 isset($fields[
'ACTIVE']) &&
445 $fields[
'ACTIVE'] ===
'Y'
448 Config\Option::set(
'sale',
'discount_separately_calculation',
'N');
452 $canCalculateSeparately = Analyzer::getInstance()->canCalculateSeparatelyAllDiscount();
453 Config\Option::set(
'sale',
'discount_separately_calculation', $canCalculateSeparately?
'Y' :
'N');
469 $connection = Main\Application::getConnection();
470 $sqlHelper = $connection->getSqlHelper();
472 $tableName = static::getTableName();
473 $update = $sqlHelper->prepareUpdate($tableName, $fields);
475 $connection->queryExecute(
476 "UPDATE {$tableName} SET {$update[0]} WHERE ID={$id}"
488 $result =
new Main\Entity\EventResult;
489 $fields = $event->getParameter(
'fields');
491 $modifyFieldList = array();
492 self::setUserID($modifyFieldList, $fields, array(
'MODIFIED_BY'));
493 self::setTimestamp($modifyFieldList, $fields, array(
'TIMESTAMP_X'));
494 self::setShortDescription($modifyFieldList, $fields);
496 self::copyOldFields($modifyFieldList, $fields);
497 $result->unsetField(
'CONDITIONS');
498 $result->unsetField(
'ACTIONS');
500 if (!empty($modifyFieldList))
501 $result->modifyFields($modifyFieldList);
502 unset($modifyFieldList);
515 $id = $event->getParameter(
'primary');
516 $fields = $event->getParameter(
'fields');
517 if (isset($fields[
'ACTIVE']))
518 DiscountGroupTable::changeActiveByDiscount($id[
'ID'], $fields[
'ACTIVE']);
520 if (isset($fields[
'ACTIONS_LIST']))
522 if (!is_array($fields[
'ACTIONS_LIST']) && \CheckSerializedData($fields[
'ACTIONS_LIST']))
523 $fields[
'ACTIONS_LIST'] = unserialize($fields[
'ACTIONS_LIST'], [
'allowed_classes' =>
false]);
524 if (is_array($fields[
'ACTIONS_LIST']))
526 Gift\RelatedDataTable::deleteByDiscount($id[
'ID']);
527 $giftManager = Gift\Manager::getInstance();
528 if ($giftManager->isContainGiftAction($fields))
530 if (!$giftManager->existsDiscountsWithGift())
531 $giftManager->enableExistenceDiscountsWithGift();
532 Gift\RelatedDataTable::fillByDiscount($fields + $id);
537 $specificFields = array(
538 'EXECUTE_MODE' => static::resolveExecuteModeByDiscountId($id),
541 if (isset($fields[
'CONDITIONS_LIST']))
543 $specificFields[
'HAS_INDEX'] = Index\Manager::getInstance()->indexDiscount($fields + $id) ?
'Y' :
'N';
546 static::updateSpecificFields($id[
'ID'], $specificFields);
547 static::updateConfigurationIfNeeded($fields, $specificFields);
549 self::dropIblockCache();
560 $id = $event->getParameter(
'primary');
562 $discount = self::getList(array(
563 'select' => array(
'ID',
'USE_COUPONS'),
564 'filter' => array(
'=ID' => $id)
566 if (!empty($discount))
568 if ((
string)$discount[
'USE_COUPONS'] ===
'Y')
569 self::$deleteCoupons = $discount[
'ID'];
571 unset($discount, $id);
582 $id = $event->getParameter(
'primary');
586 DiscountGroupTable::deleteByDiscount($id);
587 if (self::$deleteCoupons !==
false)
590 self::$deleteCoupons =
false;
592 Gift\RelatedDataTable::deleteByDiscount($id);
593 Index\Manager::getInstance()->dropIndex($id);
595 self::dropIblockCache();
609 if (!is_array($discountList))
610 $discountList = array($discountList);
612 if ($use !==
'Y' && $use !==
'N')
614 Main\Type\Collection::normalizeArrayValuesByInt($discountList);
615 if (empty($discountList))
618 $helper = $conn->getSqlHelper();
620 'update '.$helper->quote(self::getTableName()).
621 ' set '.$helper->quote(
'USE_COUPONS').
' = \''.$use.
'\' where
'.
622 $helper->quote('ID
').' in (
'.implode(',
', $discountList).')
'
624 unset($helper, $conn);
628 Gift\RelatedDataTable::deleteByDiscounts($discountList);
638 public static function setAllUseCoupons($use)
641 if ($use !== 'Y
' && $use !== 'N
')
643 $conn = Application::getConnection();
644 $helper = $conn->getSqlHelper();
646 'update '.$helper->quote(self::getTableName()).' set '.$helper->quote('USE_COUPONS
').' = \
''.$use.
'\''
648 unset($helper, $conn);
659 protected static function setUserID(&$result, $data, $keys)
661 static $currentUserID =
false;
662 if ($currentUserID ===
false)
665 $currentUserID = (isset($USER) && $USER instanceof \CUser ? (int)$USER->getID() :
null);
667 foreach ($keys as $oneKey)
670 if (array_key_exists($oneKey, $data))
671 $setField = ($data[$oneKey] !==
null && (
int)$data[$oneKey] <= 0);
674 $result[$oneKey] = $currentUserID;
681 if (!empty($data[
'SHORT_DESCRIPTION_STRUCTURE']))
683 if (empty($data[
'ACTIONS']) && empty($data[
'ACTIONS_LIST']))
687 if (!$actionConfiguration)
690 $result[
'SHORT_DESCRIPTION_STRUCTURE'] = $actionConfiguration;
703 foreach ($keys as $oneKey)
706 if (array_key_exists($oneKey, $data))
707 $setField = ($data[$oneKey] !==
null && !is_object($data[$oneKey]));
724 if (!isset($data[
'CONDITIONS_LIST']) && isset($data[
'CONDITIONS']))
725 $result[
'CONDITIONS_LIST'] = (is_array($data[
'CONDITIONS']) ? $data[
'CONDITIONS'] : unserialize($data[
'CONDITIONS'], [
'allowed_classes' =>
false]));
727 if (!isset($data[
'ACTIONS_LIST']) && isset($data[
'ACTIONS']))
728 $result[
'ACTIONS_LIST'] = (is_array($data[
'ACTIONS']) ? $data[
'ACTIONS'] : unserialize($data[
'ACTIONS'], [
'allowed_classes' =>
false]));
737 private static function dropIblockCache()
740 !Main\ModuleManager::isModuleInstalled(
'bitrix24')
741 || !Main\Loader::includeModule(
'crm')
742 || !Main\Loader::includeModule(
'iblock')
746 $iblockId = \CCrmCatalog::GetDefaultID();
748 \CIBlock::clearIblockTagCache($iblockId);
static deleteByDiscount($discount)
static deleteByDiscount($discount)
static update($primary, array $data)
static getConnection($name="")
static loadMessages($file)
static getMessage($code, $replace=null, $language=null)
static getActionConfiguration(array $discount)
static deleteByDiscount($discount)
static updateSpecificFields($id, array $fields)
static validatePresetId()
static setUseCoupons($discountList, $use)
static setUserID(&$result, $data, $keys)
static onBeforeAdd(Main\Entity\Event $event)
static validateDiscountType()
static onAfterDelete(Main\Entity\Event $event)
const EXECUTE_MODE_SEPARATELY
static copyOldFields(&$result, $data)
static onDelete(Main\Entity\Event $event)
static setTimestamp(&$result, $data, $keys)
static validateCurrency()
static onAfterUpdate(Main\Entity\Event $event)
static updateConfigurationIfNeeded(array $fields, array $specificFields)
static onBeforeUpdate(Main\Entity\Event $event)
static validateExecuteModule()
static onAfterAdd(Main\Entity\Event $event)
static resolveExecuteModeByDiscountId($discountId)
const EXECUTE_MODE_GENERAL
static setShortDescription(&$result, array $data)
static getSiteCurrency($siteId)