50 'DISCOUNT_ID' =>
'ID',
51 'CONDITIONS' =>
'CONDITIONS_LIST',
52 'ACTIONS' =>
'ACTIONS_LIST',
53 'MODULE_ID' =>
'MODULE'
56 'CONDITIONS' =>
'CONDITIONS_LIST',
57 'ACTIONS' =>
'ACTIONS_LIST'
67 return 'b_sale_order_discount';
80 'autocomplete' =>
true,
85 'validation' => array(__CLASS__,
'validateModuleId'),
93 'validation' => array(__CLASS__,
'validateName'),
98 'validation' => array(__CLASS__,
'validateDiscountHash'),
102 'serialized' =>
true,
109 'serialized' =>
true,
116 'values' => array(
'N',
'Y'),
126 'values' => array(
'N',
'Y'),
127 'title' =>
Loc::getMessage(
'ORDER_DISCOUNT_ENTITY_LAST_DISCOUNT_FIELD')
130 'serialized' =>
true,
131 'title' =>
Loc::getMessage(
'ORDER_DISCOUNT_ENTITY_ACTIONS_DESCR_FIELD')
143 new Main\
Entity\Validator\Length(
null, 50),
154 new Main\
Entity\Validator\Length(
null, 255),
165 new Main\
Entity\Validator\Length(32, 32),
177 $hash = (string)$hash;
181 $discountIterator = self::getList(array(
182 'select' => array(
'ID',
'DISCOUNT_HASH'),
183 'filter' => array(
'=DISCOUNT_HASH' => $hash)
185 if ($discount = $discountIterator->fetch())
186 $result = (int)$discount[
'ID'];
187 unset($discount, $discountIterator);
200 if (!empty($discount) && is_array($discount))
203 foreach (self::$requiredFields as $fieldName)
205 if (!isset($discount[$fieldName]))
207 $fields[$fieldName] = (
208 is_array($discount[$fieldName])
209 ? $discount[$fieldName]
210 : trim((
string)$discount[$fieldName])
214 $hash = md5(serialize($fields));
228 if (!empty($discount) && is_array($discount))
230 if (isset($discount[
'CONDITIONS_LIST']))
231 $discount[
'CONDITIONS'] = $discount[
'CONDITIONS_LIST'];
232 if (isset($discount[
'ACTIONS_LIST']))
233 $discount[
'ACTIONS'] = $discount[
'ACTIONS_LIST'];
234 if (!isset($discount[
'CONDITIONS']) || !isset($discount[
'ACTIONS']))
237 'CONDITIONS' => $discount[
'CONDITIONS'],
238 'ACTIONS' => $discount[
'ACTIONS']
240 $hash = md5(serialize($fields));
254 if (!empty($discount) && is_array($discount))
256 foreach (self::$replaceFields as $dest => $src)
258 if (!isset($discount[$dest]) && isset($discount[$src]))
259 $discount[$dest] = $discount[$src];
264 foreach (self::$requiredFields as $fieldName)
266 if (!isset($discount[$fieldName]))
268 $fields[$fieldName] = $discount[$fieldName];
283 if (!is_array($discount))
285 if (empty($discount))
289 foreach (self::$replaceFields as $dest => $src)
291 if (!isset($discount[$dest]) && isset($discount[$src]))
292 $discount[$dest] = $discount[$src];
295 foreach (self::$requiredFields as $fieldName)
297 if (!isset($discount[$fieldName]))
298 $fields[] = (isset(self::$revertFields[$fieldName]) ? self::$revertFields[$fieldName] :$fieldName);
315 $needDiscountModules = array();
316 if (!empty($discount[
'MODULES']))
318 $needDiscountModules = (
319 !is_array($discount[
'MODULES'])
320 ? array($discount[
'MODULES'])
321 : $discount[
'MODULES']
324 elseif (!empty($discount[
'HANDLERS']))
326 if (!empty($discount[
'HANDLERS'][
'MODULES']))
328 $needDiscountModules = (
329 !is_array($discount[
'HANDLERS'][
'MODULES'])
330 ? array($discount[
'HANDLERS'][
'MODULES'])
331 : $discount[
'HANDLERS'][
'MODULES']
335 if (!empty($needDiscountModules))
337 foreach ($needDiscountModules as &$module)
339 $module = trim((
string)$module);
356 if (!is_array($discount))
357 $discount = array($discount);
358 Main\Type\Collection::normalizeArrayValuesByInt($discount,
true);
359 if (empty($discount))
361 $conn = Main\Application::getConnection();
362 $helper = $conn->getSqlHelper();
363 $discountRows = array_chunk($discount, 500);
364 $query =
'delete from '.$helper->quote(self::getTableName()).
' where '.$helper->quote(
'ID');
365 foreach ($discountRows as &$row)
367 $conn->queryExecute($query.
' in ('.implode(
', ', $row).
')');
370 unset($row, $query, $discountRows, $helper, $conn);
399 return 'b_sale_order_coupons';
412 'autocomplete' =>
true,
421 'title' =>
Loc::getMessage(
'ORDER_COUPONS_ENTITY_ORDER_DISCOUNT_ID_FIELD')
425 'validation' => array(__CLASS__,
'validateCoupon'),
430 'validation' => array(__CLASS__,
'validateType'),
435 'serialized' =>
true,
442 'ORDER_DISCOUNT' =>
new Main\
Entity\ReferenceField(
444 'Bitrix\Sale\Internals\OrderDiscount',
445 array(
'=this.ORDER_DISCOUNT_ID' =>
'ref.ID'),
446 array(
'join_type' =>
'LEFT')
459 new Main\
Entity\Validator\Length(
null, 32),
471 array(__CLASS__,
'checkType')
500 $order = (int)$order;
504 $conn = Main\Application::getConnection();
505 $helper = $conn->getSqlHelper();
506 $conn->queryExecute(
'delete from '.$helper->quote(self::getTableName()).
' where '.$helper->quote(
'ORDER_ID').
' = '.$order);
507 unset($helper, $conn);
518 if (!is_array($coupon))
519 $coupon = array($coupon);
520 Main\Type\Collection::normalizeArrayValuesByInt($coupon,
true);
523 $conn = Main\Application::getConnection();
524 $helper = $conn->getSqlHelper();
525 $couponRows = array_chunk($coupon, 500);
526 $query =
'delete from '.$helper->quote(self::getTableName()).
' where '.$helper->quote(
'ID');
527 foreach ($couponRows as &$row)
529 $conn->queryExecute($query.
' in ('.implode(
', ', $row).
')');
531 unset($row, $query, $couponRows, $helper, $conn);
543 $order = (int)$order;
544 if ($order <= 0 || empty($coupons))
546 if (!is_array($coupons))
547 $coupons = array($coupons);
548 Main\Type\Collection::normalizeArrayValuesByInt($coupons,
true);
551 $conn = Main\Application::getConnection();
552 $helper = $conn->getSqlHelper();
554 'update '.$helper->quote(self::getTableName()).
555 ' set '.$helper->quote(
'ORDER_ID').
' = '.$order.
' where '.
556 $helper->quote(
'ID').
' in ('.implode(
',', $coupons).
') and '.
557 $helper->quote(
'ORDER_ID').
' = 0'
584 return 'b_sale_order_modules';
597 'autocomplete' =>
true,
602 'title' =>
Loc::getMessage(
'ORDER_MODULES_ENTITY_ORDER_DISCOUNT_ID_FIELD')
606 'validation' => array(__CLASS__,
'validateModuleId'),
619 new Main\
Entity\Validator\Length(
null, 50),
632 $discountId = (int)$discountId;
633 if ($discountId <= 0)
635 if (!is_array($moduleList))
636 $moduleList = array($moduleList);
640 $conn = Main\Application::getConnection();
641 $helper = $conn->getSqlHelper();
642 $query =
'delete from '.$helper->quote(self::getTableName()).
' where '.$helper->quote(
'ORDER_DISCOUNT_ID').
' = '.$discountId;
643 $conn->queryExecute($query);
644 foreach ($moduleList as &$module)
646 $module = (string)$module;
650 'ORDER_DISCOUNT_ID' => $discountId,
651 'MODULE_ID' => $module
653 $result = self::add($fields);
654 if (!$result->isSuccess())
660 unset($result, $module);
662 $conn->queryExecute($query);
664 unset($query, $helper, $conn);
676 if (!is_array($discount))
677 $discount = array($discount);
678 Main\Type\Collection::normalizeArrayValuesByInt($discount,
true);
679 if (empty($discount))
681 $conn = Main\Application::getConnection();
682 $helper = $conn->getSqlHelper();
683 $discountRows = array_chunk($discount, 500);
684 $query =
'delete from '.$helper->quote(self::getTableName()).
' where '.$helper->quote(
'ORDER_DISCOUNT_ID');
685 foreach ($discountRows as &$row)
687 $conn->queryExecute($query.
' in ('.implode(
', ', $row).
')');
689 unset($row, $query, $discountRows, $helper, $conn);
728 return 'b_sale_order_discount_data';
741 'autocomplete' =>
true,
746 'title' =>
Loc::getMessage(
'ORDER_DISCOUNT_DATA_ENTITY_ORDER_ID_FIELD')
751 self::ENTITY_TYPE_BASKET_ITEM,
752 self::ENTITY_TYPE_DELIVERY,
753 self::ENTITY_TYPE_SHIPMENT,
754 self::ENTITY_TYPE_DISCOUNT,
755 self::ENTITY_TYPE_ORDER,
756 self::ENTITY_TYPE_ROUND,
757 self::ENTITY_TYPE_DISCOUNT_STORED_DATA
759 'title' =>
Loc::getMessage(
'ORDER_DISCOUNT_DATA_ENTITY_ENTITY_TYPE_FIELD')
763 'title' =>
Loc::getMessage(
'ORDER_DISCOUNT_DATA_ENTITY_ENTITY_ID_FIELD')
766 'validation' => array(__CLASS__,
'validateEntityValue'),
767 'title' =>
Loc::getMessage(
'ORDER_DISCOUNT_DATA_ENTITY_ENTITY_VALUE_FIELD')
771 'serialized' =>
true,
772 'title' =>
Loc::getMessage(
'ORDER_DISCOUNT_DATA_ENTITY_ENTITY_DATA_FIELD')
784 new Main\
Entity\Validator\Length(
null, 255),
799 $order = (int)$order;
800 $basket = (int)$basket;
801 if ($order < 0 || $basket <= 0 || empty($data) || !is_array($data))
803 $clear = ($clear ===
true);
806 'ENTITY_DATA' => $data
808 $dataIterator = self::getList(array(
809 'select' => array(
'ID',
'ENTITY_DATA'),
810 'filter' => array(
'=ORDER_ID' => $order,
'=ENTITY_TYPE' => self::ENTITY_TYPE_BASKET_ITEM,
'=ENTITY_ID' => $basket)
812 if ($oldData = $dataIterator->fetch())
814 if (!$clear && !empty($oldData[
'ENTITY_DATA']))
815 $fields[
'ENTITY_DATA'] = array_merge($oldData[
'ENTITY_DATA'], $fields[
'ENTITY_DATA']);
816 $id = (int)$oldData[
'ID'];
818 unset($oldData, $dataIterator);
821 $result = self::update($id, $fields);
825 $fields[
'ORDER_ID'] = $order;
827 $fields[
'ENTITY_ID'] = $basket;
828 $fields[
'ENTITY_VALUE'] = $basket;
829 $result = self::add($fields);
830 if ($result->isSuccess())
831 $id = (int)$result->getId();
834 return $result->isSuccess();
845 $basket = (int)$basket;
849 $conn = Main\Application::getConnection();
850 $helper = $conn->getSqlHelper();
852 'delete from '.$helper->quote(self::getTableName()).
853 ' where '.$helper->quote(
'ENTITY_TYPE').
' = '.self::ENTITY_TYPE_BASKET_ITEM.
854 ' and '.$helper->quote(
'ENTITY_ID').
' = '.$basket
856 unset($helper, $conn);
868 $order = (int)$order;
872 $conn = Main\Application::getConnection();
873 $helper = $conn->getSqlHelper();
874 $conn->queryExecute(
'delete from '.$helper->quote(self::getTableName()).
' where '.$helper->quote(
'ORDER_ID').
' = '.$order);
875 unset($helper, $conn);
888 if (!is_array($discountList))
889 $discountList = array($discountList);
890 if (empty($discountList))
892 Main\Type\Collection::normalizeArrayValuesByInt($discountList,
true);
893 if (empty($discountList))
896 $conn = Main\Application::getConnection();
897 $helper = $conn->getSqlHelper();
899 'delete from '.$helper->quote(self::getTableName()).
900 ' where '.$helper->quote(
'ENTITY_TYPE').
' = '.self::ENTITY_TYPE_DISCOUNT.
901 ' and '.$helper->quote(
'ENTITY_ID').
' in ('.implode(
',', $discountList).
')'
903 unset($helper, $conn);
943 return 'b_sale_order_rules';
956 'autocomplete' =>
true,
961 'validation' => array(__CLASS__,
'validateModuleId'),
966 'title' =>
Loc::getMessage(
'ORDER_RULES_ENTITY_ORDER_DISCOUNT_ID_FIELD')
974 'values' => array(self::ENTITY_TYPE_BASKET_ITEM, self::ENTITY_TYPE_DELIVERY),
982 'validation' => array(__CLASS__,
'validateEntityValue'),
990 'values' => array(
'N',
'Y'),
993 'ACTION_BLOCK_LIST' =>
new Main\
Entity\
TextField(
'ACTION_BLOCK_LIST', array(
994 'serialized' =>
true,
999 'ORDER_DISCOUNT' =>
new Main\
Entity\ReferenceField(
1001 'Bitrix\Sale\Internals\OrderDiscount',
1002 array(
'=this.ORDER_DISCOUNT_ID' =>
'ref.ID'),
1003 array(
'join_type' =>
'INNER')
1005 'DESCR' =>
new Main\
Entity\ReferenceField(
1007 'Bitrix\Sale\Internals\OrderRulesDescr',
1008 array(
'=this.ID' =>
'ref.RULE_ID'),
1009 array(
'join_type' =>
'LEFT')
1022 new Main\
Entity\Validator\Length(
null, 50),
1033 new Main\
Entity\Validator\Length(
null, 255),
1045 $basket = (int)$basket;
1049 self::clear(array(
'=ENTITY_TYPE' => self::ENTITY_TYPE_BASKET_ITEM,
'=ENTITY_ID' => $basket,
'=ORDER_ID' => 0));
1060 if (empty($basketList) || !is_array($basketList))
1062 Main\Type\Collection::normalizeArrayValuesByInt($basketList,
true);
1063 if (empty($basketList))
1066 self::clear(array(
'=MODULE_ID' =>
'sale',
'=ENTITY_TYPE' => self::ENTITY_TYPE_BASKET_ITEM,
'@ENTITY_ID' => $basketList,
'=ORDER_ID' => 0));
1077 $order = (int)$order;
1093 if (empty($orderDiscountList) || empty($ruleList))
1096 $discountIterator = self::getList(array(
1098 'filter' => array(
'!@ID' => $ruleList,
'@ORDER_DISCOUNT_ID' => $orderDiscountList),
1099 'group' => array(
'DISCOUNT_ID')
1101 while ($discount = $discountIterator->fetch())
1103 $discount[
'CNT'] = (int)$discount[
'CNT'];
1104 if ($discount[
'CNT'] > 0)
1106 $discount[
'ORDER_DISCOUNT_ID'] = (int)$discount[
'ORDER_DISCOUNT_ID'];
1107 unset($orderDiscountList[$discount[
'ORDER_DISCOUNT_ID']]);
1110 unset($discount, $discountIterator);
1122 if (empty($orderCouponList) || empty($ruleList))
1125 $couponIterator = self::getList(array(
1127 'filter' => array(
'!@ID' => $ruleList,
'@COUPON_ID' => $orderCouponList),
1128 'group' => array(
'COUPON_ID')
1130 while ($coupon = $couponIterator->fetch())
1132 $coupon[
'CNT'] = (int)$coupon[
'CNT'];
1133 if ($coupon[
'CNT'] > 0)
1135 $coupon[
'COUPON_ID'] = (int)$coupon[
'COUPON_ID'];
1136 unset($orderCouponList[$coupon[
'COUPON_ID']]);
1139 unset($coupon, $couponIterator);
1148 protected static function clear($filter)
1150 if (empty($filter) || !is_array($filter))
1153 $ruleList = array();
1154 $orderDiscountList = array();
1155 $orderCouponList = array();
1156 $ruleIterator = self::getList(array(
1157 'select' => array(
'ID',
'ORDER_DISCOUNT_ID',
'COUPON_ID'),
1160 while ($rule = $ruleIterator->fetch())
1162 $rule[
'ID'] = (int)$rule[
'ID'];
1163 $rule[
'ORDER_DISCOUNT_ID'] = (int)$rule[
'ORDER_DISCOUNT_ID'];
1164 $rule[
'COUPON_ID'] = (int)$rule[
'COUPON_ID'];
1165 $ruleList[] = $rule[
'ID'];
1167 unset($rule, $ruleIterator);
1168 if (empty($ruleList))
1171 $conn = Main\Application::getConnection();
1172 $helper = $conn->getSqlHelper();
1173 $ruleRows = array_chunk($ruleList, 500);
1174 $mainQuery =
'delete from '.$helper->quote(self::getTableName()).
' where '.$helper->quote(
'ID');
1176 foreach ($ruleRows as &$row)
1178 $conn->queryExecute($mainQuery.
' in ('.implode(
', ', $row).
')');
1179 $conn->queryExecute($descrQuery.
' in ('.implode(
', ', $row).
')');
1181 unset($row, $descrQuery, $mainQuery, $ruleRows, $ruleList);
1182 unset($helper, $conn);
1184 if (!empty($orderDiscountList))
1186 unset($orderDiscountList);
1187 if (!empty($orderCouponList))
1189 unset($orderCouponList);
1231 return 'b_sale_order_rules_descr';
1244 'autocomplete' =>
true,
1249 'validation' => array(__CLASS__,
'validateModuleId'),
1250 'title' =>
Loc::getMessage(
'ORDER_RULES_DESCR_ENTITY_MODULE_ID_FIELD')
1254 'title' =>
Loc::getMessage(
'ORDER_RULES_DESCR_ENTITY_ORDER_DISCOUNT_ID_FIELD')
1266 'serialized' =>
true,
1280 new Main\
Entity\Validator\Length(
null, 50),
static loadMessages($file)
static getMessage($code, $replace=null, $language=null)
static isValidCouponType($couponType)
static clearByOrder($order)
static applyOrder($coupons, $order)
static clearList($coupon)
static checkType($value, $primary, array $row, Main\Entity\Field $field)
static clearByOrder($order)
const ENTITY_TYPE_BASKET_ITEM
static clearByDiscount($discountList)
const ENTITY_TYPE_SHIPMENT
const ENTITY_TYPE_DISCOUNT_STORED_DATA
static clearByBasketItem($basket)
static saveBasketItemData($order, $basket, $data, $clear=false)
const ENTITY_TYPE_DISCOUNT
static validateEntityValue()
const ENTITY_TYPE_DELIVERY
static prepareDiscountData($discount)
static getDiscountByHash($hash)
static clearList($discount)
static validateDiscountHash()
static getDiscountModules($discount)
static getEmptyFields($discount)
static calculateRuleHash($discount)
static validateModuleId()
static calculateHash($discount)
static saveOrderDiscountModules($discountId, $moduleList)
static validateModuleId()
static clearByDiscount($discount)
static validateModuleId()
static checkUseOrderDiscounts(&$orderDiscountList, &$ruleList)
static clearByOrder($order)
const ENTITY_TYPE_BASKET_ITEM
static checkUseOrderCoupons(&$orderCouponList, &$ruleList)
static clearByBasketItem($basket)
static validateEntityValue()
static clearBasketSaleDiscount($basketList)
const ENTITY_TYPE_DELIVERY
static validateModuleId()