8namespace Bitrix\Sale\Internals;
11 Bitrix\Main\Application,
13 Bitrix\Main\Localization\Loc,
14 Bitrix\Sale\Discount\Actions,
15 Bitrix\Sale\Discount\Gift,
16 Bitrix\Sale\Discount\Index,
17 Bitrix\Sale\Discount\Analyzer;
19Loc::loadMessages(__FILE__);
97 return 'b_sale_discount';
110 'autocomplete' =>
true,
111 'title' => Loc::getMessage(
'DISCOUNT_ENTITY_ID_FIELD')
114 'validation' =>
array(__CLASS__,
'validateXmlId'),
115 'title' => Loc::getMessage(
'DISCOUNT_ENTITY_XML_ID_FIELD')
119 'validation' =>
array(__CLASS__,
'validateLid'),
120 'title' => Loc::getMessage(
'DISCOUNT_ENTITY_LID_FIELD')
123 'validation' =>
array(__CLASS__,
'validateName'),
124 'title' => Loc::getMessage(
'DISCOUNT_ENTITY_NAME_FIELD')
129 'validation' =>
array(__CLASS__,
'validateCurrency'),
130 'title' => Loc::getMessage(
'DISCOUNT_ENTITY_CURRENCY_FIELD')
134 'default_value' =>
'P',
135 'validation' =>
array(__CLASS__,
'validateDiscountType')
138 'values' =>
array(
'N',
'Y'),
139 'default_value' =>
'Y',
140 'title' => Loc::getMessage(
'DISCOUNT_ENTITY_ACTIVE_FIELD')
143 'title' => Loc::getMessage(
'DISCOUNT_ENTITY_SORT_FIELD')
146 'default_value' =>
null,
147 'title' => Loc::getMessage(
'DISCOUNT_ENTITY_ACTIVE_FROM_FIELD')
150 'default_value' =>
null,
151 'title' => Loc::getMessage(
'DISCOUNT_ENTITY_ACTIVE_TO_FIELD')
155 'title' => Loc::getMessage(
'DISCOUNT_ENTITY_TIMESTAMP_X_FIELD')
157 'MODIFIED_BY' =>
new Main\Entity\IntegerField(
'MODIFIED_BY',
array(
158 'default_value' =>
null,
159 'title' => Loc::getMessage(
'DISCOUNT_ENTITY_MODIFIED_BY_FIELD')
161 'DATE_CREATE' =>
new Main\Entity\DatetimeField(
'DATE_CREATE',
array(
163 'title' => Loc::getMessage(
'DISCOUNT_ENTITY_DATE_CREATE_FIELD')
166 'default_value' =>
null,
167 'title' => Loc::getMessage(
'DISCOUNT_ENTITY_CREATED_BY_FIELD')
169 'PRIORITY' =>
new Main\Entity\IntegerField(
'PRIORITY',
array(
170 'default_value' => 1,
171 'title' => Loc::getMessage(
'DISCOUNT_ENTITY_PRIORITY_FIELD')
173 'LAST_DISCOUNT' =>
new Main\Entity\BooleanField(
'LAST_DISCOUNT',
array(
174 'values' =>
array(
'N',
'Y'),
175 'default_value' =>
'Y',
176 'title' => Loc::getMessage(
'DISCOUNT_ENTITY_LAST_DISCOUNT_FIELD')
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,
185 'title' => Loc::getMessage(
'DISCOUNT_ENTITY_VERSION_FIELD')
187 'CONDITIONS_LIST' =>
new Main\Entity\TextField(
'CONDITIONS_LIST',
array(
188 'serialized' =>
true,
189 'column_name' =>
'CONDITIONS',
190 'title' => Loc::getMessage(
'DISCOUNT_ENTITY_CONDITIONS_LIST_FIELD')
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',
197 'title' => Loc::getMessage(
'DISCOUNT_ENTITY_ACTIONS_LIST_FIELD')
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',
211 'title' => Loc::getMessage(
'DISCOUNT_ENTITY_USE_COUPONS_FIELD')
213 'EXECUTE_MODULE' =>
new Main\Entity\StringField(
'EXECUTE_MODULE',
array(
214 'validation' =>
array(__CLASS__,
'validateExecuteModule'),
215 'title' => Loc::getMessage(
'DISCOUNT_ENTITY_EXECUTE_MODULE_FIELD')
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')
346 $result =
new Main\Entity\EventResult;
349 $modifyFieldList =
array(
350 'DISCOUNT_VALUE' => 0,
351 'DISCOUNT_TYPE' =>
'P',
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');
380 if (isset(
$fields[
'ACTIONS_LIST']))
383 $fields[
'ACTIONS_LIST'] = unserialize(
$fields[
'ACTIONS_LIST'], [
'allowed_classes' =>
false]);
384 if (is_array(
$fields[
'ACTIONS_LIST']))
387 if ($giftManager->isContainGiftAction(
$fields))
389 if (!$giftManager->existsDiscountsWithGift())
390 $giftManager->enableExistenceDiscountsWithGift();
397 $specificFields =
array(
398 'EXECUTE_MODE' => static::resolveExecuteModeByDiscountId($id),
401 if (isset(
$fields[
'CONDITIONS_LIST']))
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 &&
452 $canCalculateSeparately = Analyzer::getInstance()->canCalculateSeparatelyAllDiscount();
453 Config\Option::set(
'sale',
'discount_separately_calculation', $canCalculateSeparately?
'Y' :
'N');
472 $tableName = static::getTableName();
473 $update = $sqlHelper->prepareUpdate($tableName,
$fields);
476 "UPDATE {$tableName} SET {$update[0]} WHERE ID={$id}"
488 $result =
new Main\Entity\EventResult;
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');
520 if (isset(
$fields[
'ACTIONS_LIST']))
523 $fields[
'ACTIONS_LIST'] = unserialize(
$fields[
'ACTIONS_LIST'], [
'allowed_classes' =>
false]);
524 if (is_array(
$fields[
'ACTIONS_LIST']))
528 if ($giftManager->isContainGiftAction(
$fields))
530 if (!$giftManager->existsDiscountsWithGift())
531 $giftManager->enableExistenceDiscountsWithGift();
537 $specificFields =
array(
538 'EXECUTE_MODE' => static::resolveExecuteModeByDiscountId($id),
541 if (isset(
$fields[
'CONDITIONS_LIST']))
546 static::updateSpecificFields($id[
'ID'], $specificFields);
547 static::updateConfigurationIfNeeded(
$fields, $specificFields);
549 self::dropIblockCache();
560 $id =
$event->getParameter(
'primary');
563 'select' =>
array(
'ID',
'USE_COUPONS'),
564 'filter' =>
array(
'=ID' => $id)
568 if ((
string)
$discount[
'USE_COUPONS'] ===
'Y')
582 $id =
$event->getParameter(
'primary');
587 if (self::$deleteCoupons !==
false)
590 self::$deleteCoupons =
false;
595 self::dropIblockCache();
609 if (!is_array($discountList))
610 $discountList =
array($discountList);
612 if ($use !==
'Y' && $use !==
'N')
615 if (empty($discountList))
617 $conn = Application::getConnection();
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);
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']))
686 $actionConfiguration = Actions::getActionConfiguration(
$data);
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 set($moduleId, $name, $value="", $siteId="")
static normalizeArrayValuesByInt(&$map, $sorted=true)
static deleteByDiscount($discount)
static deleteByDiscount($discount)
static changeActiveByDiscount($discount, $active)
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)
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
CheckSerializedData($str, $max_depth=200)