Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
orderdiscount.php
1<?php
3
7
8Loc::loadMessages(__FILE__);
9
33class OrderDiscountTable extends Main\Entity\DataManager
34{
35 protected static $requiredFields = array(
36 'MODULE_ID',
37 'DISCOUNT_ID',
38 'NAME',
39 'CONDITIONS',
40 'UNPACK',
41 'ACTIONS',
42 'APPLICATION',
43 'USE_COUPONS',
44 'SORT',
45 'PRIORITY',
46 'LAST_DISCOUNT',
47 'ACTIONS_DESCR'
48 );
49 protected static $replaceFields = array(
50 'DISCOUNT_ID' => 'ID',
51 'CONDITIONS' => 'CONDITIONS_LIST',
52 'ACTIONS' => 'ACTIONS_LIST',
53 'MODULE_ID' => 'MODULE'
54 );
55 protected static $revertFields = array(
56 'CONDITIONS' => 'CONDITIONS_LIST',
57 'ACTIONS' => 'ACTIONS_LIST'
58 );
59
65 public static function getTableName()
66 {
67 return 'b_sale_order_discount';
68 }
69
75 public static function getMap()
76 {
77 return array(
78 'ID' => new Main\Entity\IntegerField('ID', array(
79 'primary' => true,
80 'autocomplete' => true,
81 'title' => Loc::getMessage('ORDER_DISCOUNT_ENTITY_ID_FIELD')
82 )),
83 'MODULE_ID' => new Main\Entity\StringField('MODULE_ID', array(
84 'required' => true,
85 'validation' => array(__CLASS__, 'validateModuleId'),
86 'title' => Loc::getMessage('ORDER_DISCOUNT_ENTITY_MODULE_ID_FIELD')
87 )),
88 'DISCOUNT_ID' => new Main\Entity\IntegerField('DISCOUNT_ID', array(
89 'required' => true,
90 'title' => Loc::getMessage('ORDER_DISCOUNT_ENTITY_DISCOUNT_ID_FIELD')
91 )),
92 'NAME' => new Main\Entity\StringField('NAME', array(
93 'validation' => array(__CLASS__, 'validateName'),
94 'title' => Loc::getMessage('ORDER_DISCOUNT_ENTITY_NAME_FIELD')
95 )),
96 'DISCOUNT_HASH' => new Main\Entity\StringField('DISCOUNT_HASH', array(
97 'required' => true,
98 'validation' => array(__CLASS__, 'validateDiscountHash'),
99 'title' => Loc::getMessage('ORDER_DISCOUNT_ENTITY_DISCOUNT_HASH_FIELD')
100 )),
101 'CONDITIONS' => new Main\Entity\TextField('CONDITIONS', array(
102 'serialized' => true,
103 'title' => Loc::getMessage('ORDER_DISCOUNT_ENTITY_CONDITIONS_FIELD')
104 )),
105 'UNPACK' => new Main\Entity\TextField('UNPACK', array(
106 'title' => Loc::getMessage('ORDER_DISCOUNT_ENTITY_UNPACK_FIELD')
107 )),
108 'ACTIONS' => new Main\Entity\TextField('ACTIONS', array(
109 'serialized' => true,
110 'title' => Loc::getMessage('ORDER_DISCOUNT_ENTITY_ACTIONS_FIELD')
111 )),
112 'APPLICATION' => new Main\Entity\TextField('APPLICATION', array(
113 'title' => Loc::getMessage('ORDER_DISCOUNT_ENTITY_APPLICATION_FIELD')
114 )),
115 'USE_COUPONS' => new Main\Entity\BooleanField('USE_COUPONS', array(
116 'values' => array('N', 'Y'),
117 'title' => Loc::getMessage('ORDER_DISCOUNT_ENTITY_USE_COUPONS_FIELD')
118 )),
119 'SORT' => new Main\Entity\IntegerField('SORT', array(
120 'title' => Loc::getMessage('ORDER_DISCOUNT_ENTITY_SORT_FIELD')
121 )),
122 'PRIORITY' => new Main\Entity\IntegerField('PRIORITY', array(
123 'title' => Loc::getMessage('ORDER_DISCOUNT_ENTITY_PRIORITY_FIELD')
124 )),
125 'LAST_DISCOUNT' => new Main\Entity\BooleanField('LAST_DISCOUNT', array(
126 'values' => array('N', 'Y'),
127 'title' => Loc::getMessage('ORDER_DISCOUNT_ENTITY_LAST_DISCOUNT_FIELD')
128 )),
129 'ACTIONS_DESCR' => new Main\Entity\TextField('ACTIONS_DESCR',array(
130 'serialized' => true,
131 'title' => Loc::getMessage('ORDER_DISCOUNT_ENTITY_ACTIONS_DESCR_FIELD')
132 ))
133 );
134 }
140 public static function validateModuleId()
141 {
142 return array(
143 new Main\Entity\Validator\Length(null, 50),
144 );
145 }
151 public static function validateName()
152 {
153 return array(
154 new Main\Entity\Validator\Length(null, 255),
155 );
156 }
162 public static function validateDiscountHash()
163 {
164 return array(
165 new Main\Entity\Validator\Length(32, 32),
166 );
167 }
168
175 public static function getDiscountByHash($hash)
176 {
177 $hash = (string)$hash;
178 if ($hash == '')
179 return false;
180 $result = 0;
181 $discountIterator = self::getList(array(
182 'select' => array('ID', 'DISCOUNT_HASH'),
183 'filter' => array('=DISCOUNT_HASH' => $hash)
184 ));
185 if ($discount = $discountIterator->fetch())
186 $result = (int)$discount['ID'];
187 unset($discount, $discountIterator);
188 return $result;
189 }
190
197 public static function calculateHash($discount)
198 {
199 $hash = false;
200 if (!empty($discount) && is_array($discount))
201 {
202 $fields = array();
203 foreach (self::$requiredFields as $fieldName)
204 {
205 if (!isset($discount[$fieldName]))
206 return $hash;
207 $fields[$fieldName] = (
208 is_array($discount[$fieldName])
209 ? $discount[$fieldName]
210 : trim((string)$discount[$fieldName])
211 );
212 }
213 if (!empty($fields))
214 $hash = md5(serialize($fields));
215 }
216 return $hash;
217 }
218
225 public static function calculateRuleHash($discount)
226 {
227 $hash = false;
228 if (!empty($discount) && is_array($discount))
229 {
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']))
235 return $hash;
236 $fields = array(
237 'CONDITIONS' => $discount['CONDITIONS'],
238 'ACTIONS' => $discount['ACTIONS']
239 );
240 $hash = md5(serialize($fields));
241 }
242 return $hash;
243 }
244
251 public static function prepareDiscountData($discount)
252 {
253 $fields = false;
254 if (!empty($discount) && is_array($discount))
255 {
256 foreach (self::$replaceFields as $dest => $src)
257 {
258 if (!isset($discount[$dest]) && isset($discount[$src]))
259 $discount[$dest] = $discount[$src];
260 }
261 unset($dest, $src);
262
263 $fields = array();
264 foreach (self::$requiredFields as $fieldName)
265 {
266 if (!isset($discount[$fieldName]))
267 return false;
268 $fields[$fieldName] = $discount[$fieldName];
269 }
270 unset($fieldName);
271 }
272 return $fields;
273 }
274
281 public static function getEmptyFields($discount)
282 {
283 if (!is_array($discount))
284 return false;
285 if (empty($discount))
287
288 $fields = array();
289 foreach (self::$replaceFields as $dest => $src)
290 {
291 if (!isset($discount[$dest]) && isset($discount[$src]))
292 $discount[$dest] = $discount[$src];
293 }
294 unset($dest, $src);
295 foreach (self::$requiredFields as $fieldName)
296 {
297 if (!isset($discount[$fieldName]))
298 $fields[] = (isset(self::$revertFields[$fieldName]) ? self::$revertFields[$fieldName] :$fieldName);
299 }
300 unset($fieldName);
301
302 return $fields;
303 }
304
312 public static function getDiscountModules($discount)
313 {
314 $result = array();
315 $needDiscountModules = array();
316 if (!empty($discount['MODULES']))
317 {
318 $needDiscountModules = (
319 !is_array($discount['MODULES'])
320 ? array($discount['MODULES'])
321 : $discount['MODULES']
322 );
323 }
324 elseif (!empty($discount['HANDLERS']))
325 {
326 if (!empty($discount['HANDLERS']['MODULES']))
327 {
328 $needDiscountModules = (
329 !is_array($discount['HANDLERS']['MODULES'])
330 ? array($discount['HANDLERS']['MODULES'])
331 : $discount['HANDLERS']['MODULES']
332 );
333 }
334 }
335 if (!empty($needDiscountModules))
336 {
337 foreach ($needDiscountModules as &$module)
338 {
339 $module = trim((string)$module);
340 if (!empty($module))
341 $result[] = $module;
342 }
343 unset($module);
344 }
345 return $result;
346 }
347
354 public static function clearList($discount)
355 {
356 if (!is_array($discount))
357 $discount = array($discount);
358 Main\Type\Collection::normalizeArrayValuesByInt($discount, true);
359 if (empty($discount))
360 return;
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)
366 {
367 $conn->queryExecute($query.' in ('.implode(', ', $row).')');
369 }
370 unset($row, $query, $discountRows, $helper, $conn);
371 }
372}
373
390class OrderCouponsTable extends Main\Entity\DataManager
391{
397 public static function getTableName()
398 {
399 return 'b_sale_order_coupons';
400 }
401
407 public static function getMap()
408 {
409 return array(
410 'ID' => new Main\Entity\IntegerField('ID', array(
411 'primary' => true,
412 'autocomplete' => true,
413 'title' => Loc::getMessage('ORDER_COUPONS_ENTITY_ID_FIELD')
414 )),
415 'ORDER_ID' => new Main\Entity\IntegerField('ORDER_ID', array(
416 'required' => true,
417 'title' => Loc::getMessage('ORDER_COUPONS_ENTITY_ORDER_ID_FIELD')
418 )),
419 'ORDER_DISCOUNT_ID' => new Main\Entity\IntegerField('ORDER_DISCOUNT_ID', array(
420 'required' => true,
421 'title' => Loc::getMessage('ORDER_COUPONS_ENTITY_ORDER_DISCOUNT_ID_FIELD')
422 )),
423 'COUPON' => new Main\Entity\StringField('COUPON', array(
424 'required' => true,
425 'validation' => array(__CLASS__, 'validateCoupon'),
426 'title' => Loc::getMessage('ORDER_COUPONS_ENTITY_COUPON_FIELD')
427 )),
428 'TYPE' => new Main\Entity\IntegerField('TYPE', array(
429 'required' => true,
430 'validation' => array(__CLASS__, 'validateType'),
431 'title' => Loc::getMessage('ORDER_COUPONS_ENTITY_TYPE_FIELD')
432 )),
433 'DATA' => new Main\Entity\TextField('DATA', array(
434 'required' => true,
435 'serialized' => true,
436 'title' => Loc::getMessage('ORDER_COUPONS_ENTITY_DATA_FIELD')
437 )),
438 'COUPON_ID' => new Main\Entity\IntegerField('COUPON_ID', array(
439 'required' => true,
440 'title' => Loc::getMessage('ORDER_COUPONS_ENTITY_COUPON_ID_FIELD')
441 )),
442 'ORDER_DISCOUNT' => new Main\Entity\ReferenceField(
443 'ORDER_DISCOUNT',
444 'Bitrix\Sale\Internals\OrderDiscount',
445 array('=this.ORDER_DISCOUNT_ID' => 'ref.ID'),
446 array('join_type' => 'LEFT')
447 )
448 );
449 }
450
456 public static function validateCoupon()
457 {
458 return array(
459 new Main\Entity\Validator\Length(null, 32),
460 );
461 }
462
468 public static function validateType()
469 {
470 return array(
471 array(__CLASS__, 'checkType')
472 );
473 }
474
484 public static function checkType($value, $primary, array $row, Main\Entity\Field $field)
485 {
486 if (Internals\DiscountCouponTable::isValidCouponType($value) || $value == Internals\DiscountCouponTable::TYPE_ARCHIVED)
487 return true;
488
489 return Loc::getMessage('ORDER_COUPONS_VALIDATOR_TYPE');
490 }
491
498 public static function clearByOrder($order)
499 {
500 $order = (int)$order;
501 if ($order <= 0)
502 return;
503
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);
508 }
509
516 public static function clearList($coupon)
517 {
518 if (!is_array($coupon))
519 $coupon = array($coupon);
520 Main\Type\Collection::normalizeArrayValuesByInt($coupon, true);
521 if (empty($coupon))
522 return;
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)
528 {
529 $conn->queryExecute($query.' in ('.implode(', ', $row).')');
530 }
531 unset($row, $query, $couponRows, $helper, $conn);
532 }
533
541 public static function applyOrder($coupons, $order)
542 {
543 $order = (int)$order;
544 if ($order <= 0 || empty($coupons))
545 return;
546 if (!is_array($coupons))
547 $coupons = array($coupons);
548 Main\Type\Collection::normalizeArrayValuesByInt($coupons, true);
549 if (empty($coupons))
550 return;
551 $conn = Main\Application::getConnection();
552 $helper = $conn->getSqlHelper();
553 $conn->queryExecute(
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'
558 );
559 }
560}
561
575class OrderModulesTable extends Main\Entity\DataManager
576{
582 public static function getTableName()
583 {
584 return 'b_sale_order_modules';
585 }
586
592 public static function getMap()
593 {
594 return array(
595 'ID' => new Main\Entity\IntegerField('ID', array(
596 'primary' => true,
597 'autocomplete' => true,
598 'title' => Loc::getMessage('ORDER_MODULES_ENTITY_ID_FIELD')
599 )),
600 'ORDER_DISCOUNT_ID' => new Main\Entity\IntegerField('ORDER_DISCOUNT_ID', array(
601 'required' => true,
602 'title' => Loc::getMessage('ORDER_MODULES_ENTITY_ORDER_DISCOUNT_ID_FIELD')
603 )),
604 'MODULE_ID' => new Main\Entity\StringField('MODULE_ID', array(
605 'required' => true,
606 'validation' => array(__CLASS__, 'validateModuleId'),
607 'title' => Loc::getMessage('ORDER_MODULES_ENTITY_MODULE_ID_FIELD')
608 ))
609 );
610 }
616 public static function validateModuleId()
617 {
618 return array(
619 new Main\Entity\Validator\Length(null, 50),
620 );
621 }
622
630 public static function saveOrderDiscountModules($discountId, $moduleList)
631 {
632 $discountId = (int)$discountId;
633 if ($discountId <= 0)
634 return false;
635 if (!is_array($moduleList))
636 $moduleList = array($moduleList);
637
638 $error = false;
639
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)
645 {
646 $module = (string)$module;
647 if (empty($module))
648 continue;
649 $fields = array(
650 'ORDER_DISCOUNT_ID' => $discountId,
651 'MODULE_ID' => $module
652 );
653 $result = self::add($fields);
654 if (!$result->isSuccess())
655 {
656 $error = true;
657 break;
658 }
659 }
660 unset($result, $module);
661 if ($error)
662 $conn->queryExecute($query);
663
664 unset($query, $helper, $conn);
665 return !$error;
666 }
667
674 public static function clearByDiscount($discount)
675 {
676 if (!is_array($discount))
677 $discount = array($discount);
678 Main\Type\Collection::normalizeArrayValuesByInt($discount, true);
679 if (empty($discount))
680 return;
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)
686 {
687 $conn->queryExecute($query.' in ('.implode(', ', $row).')');
688 }
689 unset($row, $query, $discountRows, $helper, $conn);
690 }
691}
692
709class OrderDiscountDataTable extends Main\Entity\DataManager
710{
714 const ENTITY_TYPE_DELIVERY = 0x0002;
715 const ENTITY_TYPE_SHIPMENT = 0x0004;
716 const ENTITY_TYPE_DISCOUNT = 0x0008;
717 const ENTITY_TYPE_ORDER = 0x0010;
718 const ENTITY_TYPE_ROUND = 0x0020;
720
726 public static function getTableName()
727 {
728 return 'b_sale_order_discount_data';
729 }
730
736 public static function getMap()
737 {
738 return array(
739 'ID' => new Main\Entity\IntegerField('ID', array(
740 'primary' => true,
741 'autocomplete' => true,
742 'title' => Loc::getMessage('ORDER_DISCOUNT_DATA_ENTITY_ID_FIELD')
743 )),
744 'ORDER_ID' => new Main\Entity\IntegerField('ORDER_ID', array(
745 'required' => true,
746 'title' => Loc::getMessage('ORDER_DISCOUNT_DATA_ENTITY_ORDER_ID_FIELD')
747 )),
748 'ENTITY_TYPE' => new Main\Entity\EnumField('ENTITY_TYPE', array(
749 'required' => true,
750 'values' => array(
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
758 ),
759 'title' => Loc::getMessage('ORDER_DISCOUNT_DATA_ENTITY_ENTITY_TYPE_FIELD')
760 )),
761 'ENTITY_ID' => new Main\Entity\IntegerField('ENTITY_ID', array(
762 'required' => true,
763 'title' => Loc::getMessage('ORDER_DISCOUNT_DATA_ENTITY_ENTITY_ID_FIELD')
764 )),
765 'ENTITY_VALUE' => new Main\Entity\StringField('ENTITY_VALUE', array(
766 'validation' => array(__CLASS__, 'validateEntityValue'),
767 'title' => Loc::getMessage('ORDER_DISCOUNT_DATA_ENTITY_ENTITY_VALUE_FIELD')
768 )),
769 'ENTITY_DATA' => new Main\Entity\TextField('ENTITY_DATA', array(
770 'required' => true,
771 'serialized' => true,
772 'title' => Loc::getMessage('ORDER_DISCOUNT_DATA_ENTITY_ENTITY_DATA_FIELD')
773 ))
774 );
775 }
781 public static function validateEntityValue()
782 {
783 return array(
784 new Main\Entity\Validator\Length(null, 255),
785 );
786 }
787
797 public static function saveBasketItemData($order, $basket, $data, $clear = false)
798 {
799 $order = (int)$order;
800 $basket = (int)$basket;
801 if ($order < 0 || $basket <= 0 || empty($data) || !is_array($data))
802 return false;
803 $clear = ($clear === true);
804 $id = 0;
805 $fields = array(
806 'ENTITY_DATA' => $data
807 );
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)
811 ));
812 if ($oldData = $dataIterator->fetch())
813 {
814 if (!$clear && !empty($oldData['ENTITY_DATA']))
815 $fields['ENTITY_DATA'] = array_merge($oldData['ENTITY_DATA'], $fields['ENTITY_DATA']);
816 $id = (int)$oldData['ID'];
817 }
818 unset($oldData, $dataIterator);
819 if ($id > 0)
820 {
821 $result = self::update($id, $fields);
822 }
823 else
824 {
825 $fields['ORDER_ID'] = $order;
826 $fields['ENTITY_TYPE'] = self::ENTITY_TYPE_BASKET_ITEM;
827 $fields['ENTITY_ID'] = $basket;
828 $fields['ENTITY_VALUE'] = $basket;
829 $result = self::add($fields);
830 if ($result->isSuccess())
831 $id = (int)$result->getId();
832 }
833 unset($fields, $id);
834 return $result->isSuccess();
835 }
836
843 public static function clearByBasketItem($basket)
844 {
845 $basket = (int)$basket;
846 if ($basket <= 0)
847 return false;
848
849 $conn = Main\Application::getConnection();
850 $helper = $conn->getSqlHelper();
851 $conn->queryExecute(
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
855 );
856 unset($helper, $conn);
857 return true;
858 }
859
866 public static function clearByOrder($order)
867 {
868 $order = (int)$order;
869 if ($order <= 0)
870 return false;
871
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);
876
877 return true;
878 }
879
886 public static function clearByDiscount($discountList)
887 {
888 if (!is_array($discountList))
889 $discountList = array($discountList);
890 if (empty($discountList))
891 return false;
892 Main\Type\Collection::normalizeArrayValuesByInt($discountList, true);
893 if (empty($discountList))
894 return false;
895
896 $conn = Main\Application::getConnection();
897 $helper = $conn->getSqlHelper();
898 $conn->queryExecute(
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).')'
902 );
903 unset($helper, $conn);
904
905 return true;
906 }
907}
908
929class OrderRulesTable extends Main\Entity\DataManager
930{
934 const ENTITY_TYPE_DELIVERY = 0x0002;
935
941 public static function getTableName()
942 {
943 return 'b_sale_order_rules';
944 }
945
951 public static function getMap()
952 {
953 return array(
954 'ID' => new Main\Entity\IntegerField('ID', array(
955 'primary' => true,
956 'autocomplete' => true,
957 'title' => Loc::getMessage('ORDER_RULES_ENTITY_ID_FIELD')
958 )),
959 'MODULE_ID' => new Main\Entity\StringField('MODULE_ID', array(
960 'required' => true,
961 'validation' => array(__CLASS__, 'validateModuleId'),
962 'title' => Loc::getMessage('ORDER_RULES_ENTITY_MODULE_ID_FIELD')
963 )),
964 'ORDER_DISCOUNT_ID' => new Main\Entity\IntegerField('ORDER_DISCOUNT_ID', array(
965 'required' => true,
966 'title' => Loc::getMessage('ORDER_RULES_ENTITY_ORDER_DISCOUNT_ID_FIELD')
967 )),
968 'ORDER_ID' => new Main\Entity\IntegerField('ORDER_ID', array(
969 'required' => true,
970 'title' => Loc::getMessage('ORDER_RULES_ENTITY_ORDER_ID_FIELD')
971 )),
972 'ENTITY_TYPE' => new Main\Entity\EnumField('ENTITY_TYPE', array(
973 'required' => true,
974 'values' => array(self::ENTITY_TYPE_BASKET_ITEM, self::ENTITY_TYPE_DELIVERY),
975 'title' => Loc::getMessage('ORDER_RULES_ENTITY_ENTITY_TYPE_FIELD')
976 )),
977 'ENTITY_ID' => new Main\Entity\IntegerField('ENTITY_ID', array(
978 'required' => true,
979 'title' => Loc::getMessage('ORDER_RULES_ENTITY_ENTITY_ID_FIELD')
980 )),
981 'ENTITY_VALUE' => new Main\Entity\StringField('ENTITY_VALUE', array(
982 'validation' => array(__CLASS__, 'validateEntityValue'),
983 'title' => Loc::getMessage('ORDER_RULES_ENTITY_ENTITY_VALUE_FIELD')
984 )),
985 'COUPON_ID' => new Main\Entity\IntegerField('COUPON_ID', array(
986 'required' => true,
987 'title' => Loc::getMessage('ORDER_RULES_ENTITY_COUPON_ID_FIELD')
988 )),
989 'APPLY' => new Main\Entity\BooleanField('APPLY', array(
990 'values' => array('N', 'Y'),
991 'title' => Loc::getMessage('ORDER_RULES_ENTITY_APPLY_FIELD')
992 )),
993 'ACTION_BLOCK_LIST' => new Main\Entity\TextField('ACTION_BLOCK_LIST', array(
994 'serialized' => true,
995 )),
996 'APPLY_BLOCK_COUNTER' => new Main\Entity\IntegerField('APPLY_BLOCK_COUNTER', array(
997 'default_value' => 0
998 )),
999 'ORDER_DISCOUNT' => new Main\Entity\ReferenceField(
1000 'ORDER_DISCOUNT',
1001 'Bitrix\Sale\Internals\OrderDiscount',
1002 array('=this.ORDER_DISCOUNT_ID' => 'ref.ID'),
1003 array('join_type' => 'INNER')
1004 ),
1005 'DESCR' => new Main\Entity\ReferenceField(
1006 'DESCR',
1007 'Bitrix\Sale\Internals\OrderRulesDescr',
1008 array('=this.ID' => 'ref.RULE_ID'),
1009 array('join_type' => 'LEFT')
1010 )
1011 );
1012 }
1013
1019 public static function validateModuleId()
1020 {
1021 return array(
1022 new Main\Entity\Validator\Length(null, 50),
1023 );
1024 }
1030 public static function validateEntityValue()
1031 {
1032 return array(
1033 new Main\Entity\Validator\Length(null, 255),
1034 );
1035 }
1036
1043 public static function clearByBasketItem($basket)
1044 {
1045 $basket = (int)$basket;
1046 if ($basket <= 0)
1047 return;
1048
1049 self::clear(array('=ENTITY_TYPE' => self::ENTITY_TYPE_BASKET_ITEM, '=ENTITY_ID' => $basket, '=ORDER_ID' => 0));
1050 }
1051
1058 public static function clearBasketSaleDiscount($basketList)
1059 {
1060 if (empty($basketList) || !is_array($basketList))
1061 return;
1062 Main\Type\Collection::normalizeArrayValuesByInt($basketList, true);
1063 if (empty($basketList))
1064 return;
1065
1066 self::clear(array('=MODULE_ID' => 'sale', '=ENTITY_TYPE' => self::ENTITY_TYPE_BASKET_ITEM, '@ENTITY_ID' => $basketList, '=ORDER_ID' => 0));
1067 }
1068
1075 public static function clearByOrder($order)
1076 {
1077 $order = (int)$order;
1078 if ($order <= 0)
1079 return;
1080
1081 self::clear(array('=ORDER_ID' => $order));
1082 }
1083
1091 protected static function checkUseOrderDiscounts(&$orderDiscountList, &$ruleList)
1092 {
1093 if (empty($orderDiscountList) || empty($ruleList))
1094 return;
1095
1096 $discountIterator = self::getList(array(
1097 'select' => array('ORDER_DISCOUNT_ID', new Main\Entity\ExpressionField('CNT', 'COUNT(*)')),
1098 'filter' => array('!@ID' => $ruleList, '@ORDER_DISCOUNT_ID' => $orderDiscountList),
1099 'group' => array('DISCOUNT_ID')
1100 ));
1101 while ($discount = $discountIterator->fetch())
1102 {
1103 $discount['CNT'] = (int)$discount['CNT'];
1104 if ($discount['CNT'] > 0)
1105 {
1106 $discount['ORDER_DISCOUNT_ID'] = (int)$discount['ORDER_DISCOUNT_ID'];
1107 unset($orderDiscountList[$discount['ORDER_DISCOUNT_ID']]);
1108 }
1109 }
1110 unset($discount, $discountIterator);
1111 }
1112
1120 protected static function checkUseOrderCoupons(&$orderCouponList, &$ruleList)
1121 {
1122 if (empty($orderCouponList) || empty($ruleList))
1123 return;
1124
1125 $couponIterator = self::getList(array(
1126 'select' => array('COUPON_ID', new Main\Entity\ExpressionField('CNT', 'COUNT(*)')),
1127 'filter' => array('!@ID' => $ruleList, '@COUPON_ID' => $orderCouponList),
1128 'group' => array('COUPON_ID')
1129 ));
1130 while ($coupon = $couponIterator->fetch())
1131 {
1132 $coupon['CNT'] = (int)$coupon['CNT'];
1133 if ($coupon['CNT'] > 0)
1134 {
1135 $coupon['COUPON_ID'] = (int)$coupon['COUPON_ID'];
1136 unset($orderCouponList[$coupon['COUPON_ID']]);
1137 }
1138 }
1139 unset($coupon, $couponIterator);
1140 }
1141
1148 protected static function clear($filter)
1149 {
1150 if (empty($filter) || !is_array($filter))
1151 return;
1152
1153 $ruleList = array();
1154 $orderDiscountList = array();
1155 $orderCouponList = array();
1156 $ruleIterator = self::getList(array(
1157 'select' => array('ID', 'ORDER_DISCOUNT_ID', 'COUPON_ID'),
1158 'filter' => $filter
1159 ));
1160 while ($rule = $ruleIterator->fetch())
1161 {
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'];
1166 }
1167 unset($rule, $ruleIterator);
1168 if (empty($ruleList))
1169 return;
1170
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');
1175 $descrQuery = 'delete from '.$helper->quote(OrderRulesDescrTable::getTableName()).' where '.$helper->quote('RULE_ID');
1176 foreach ($ruleRows as &$row)
1177 {
1178 $conn->queryExecute($mainQuery.' in ('.implode(', ', $row).')');
1179 $conn->queryExecute($descrQuery.' in ('.implode(', ', $row).')');
1180 }
1181 unset($row, $descrQuery, $mainQuery, $ruleRows, $ruleList);
1182 unset($helper, $conn);
1183
1184 if (!empty($orderDiscountList))
1185 OrderDiscountTable::clearList($orderDiscountList);
1186 unset($orderDiscountList);
1187 if (!empty($orderCouponList))
1188 OrderCouponsTable::clearList($orderCouponList);
1189 unset($orderCouponList);
1190 }
1191}
1192
1222class OrderRulesDescrTable extends Main\Entity\DataManager
1223{
1229 public static function getTableName()
1230 {
1231 return 'b_sale_order_rules_descr';
1232 }
1233
1239 public static function getMap()
1240 {
1241 return array(
1242 'ID' => new Main\Entity\IntegerField('ID', array(
1243 'primary' => true,
1244 'autocomplete' => true,
1245 'title' => Loc::getMessage('ORDER_RULES_DESCR_ENTITY_ID_FIELD')
1246 )),
1247 'MODULE_ID' => new Main\Entity\StringField('MODULE_ID', array(
1248 'required' => true,
1249 'validation' => array(__CLASS__, 'validateModuleId'),
1250 'title' => Loc::getMessage('ORDER_RULES_DESCR_ENTITY_MODULE_ID_FIELD')
1251 )),
1252 'ORDER_DISCOUNT_ID' => new Main\Entity\IntegerField('ORDER_DISCOUNT_ID', array(
1253 'required' => true,
1254 'title' => Loc::getMessage('ORDER_RULES_DESCR_ENTITY_ORDER_DISCOUNT_ID_FIELD')
1255 )),
1256 'ORDER_ID' => new Main\Entity\IntegerField('ORDER_ID', array(
1257 'required' => true,
1258 'title' => Loc::getMessage('ORDER_RULES_DESCR_ENTITY_ORDER_ID_FIELD')
1259 )),
1260 'RULE_ID' => new Main\Entity\IntegerField('RULE_ID', array(
1261 'required' => true,
1262 'title' => Loc::getMessage('ORDER_RULES_DESCR_ENTITY_RULE_ID_FIELD')
1263 )),
1264 'DESCR' => new Main\Entity\TextField('DESCR', array(
1265 'required' => true,
1266 'serialized' => true,
1267 'title' => Loc::getMessage('ORDER_RULES_DESCR_ENTITY_DESCR_FIELD')
1268 ))
1269 );
1270 }
1271
1277 public static function validateModuleId()
1278 {
1279 return array(
1280 new Main\Entity\Validator\Length(null, 50),
1281 );
1282 }
1283}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static checkType($value, $primary, array $row, Main\Entity\Field $field)
static saveBasketItemData($order, $basket, $data, $clear=false)
static saveOrderDiscountModules($discountId, $moduleList)
static checkUseOrderDiscounts(&$orderDiscountList, &$ruleList)
static checkUseOrderCoupons(&$orderCouponList, &$ruleList)