Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
entitymarker.php
1<?php
2
3
4namespace Bitrix\Sale;
5
6
10
11Main\Localization\Loc::loadMessages(__FILE__);
12
14{
16 const ENTITY_MARKED_TYPE_MANUAL = 'MANUAL';
17
18 const ENTITY_TYPE_ORDER = 'ORDER';
19 const ENTITY_TYPE_BASKET_ITEM = 'BASKET_ITEM';
20 const ENTITY_TYPE_SHIPMENT = 'SHIPMENT';
21 const ENTITY_TYPE_PAYMENT = 'PAYMENT';
22 const ENTITY_TYPE_PROPERTY_VALUE = 'PROPERTY_VALUE';
23
26
28 protected static $pool = array();
29
35 public static function addMarker(OrderBase $order, Internals\Entity $entity, Result $result)
36 {
37 if (!$result->hasWarnings())
38 {
39 return;
40 }
41
42 $entityType = static::getEntityType($entity);
43 if ($entityType === null)
44 {
45 return;
46 }
47
48 $fields = array(
49 'ENTITY' => $entity,
50 'ORDER' => $order,
51 );
52
53 if ($order->getId() > 0)
54 {
55 $fields['ORDER_ID'] = $order->getId();
56 }
57
58 if ($entity->getId() > 0)
59 {
60 $fields['ENTITY_ID'] = $entity->getId();
61 }
62
63 $fields['ENTITY_TYPE'] = $entityType;
65 foreach ($result->getWarnings() as $resultWarning)
66 {
67 $code = $resultWarning->getCode();
68 $message = $resultWarning->getMessage();
69 $isAutoFix = false;
70
71 if ($entity instanceof \IEntityMarker)
72 {
73 $isAutoFix = $entity->canAutoFixError($code);
74 }
75
76 $fields['CODE'] = $code;
77 $fields['MESSAGE'] = $message;
78 $fields['TYPE'] = $isAutoFix ? static::ENTITY_MARKED_TYPE_AUTO : static::ENTITY_MARKED_TYPE_MANUAL;
79 $fields['SUCCESS'] = static::ENTITY_SUCCESS_CODE_FAIL;
80 static::addItem($order, $entityType, $fields);
81 }
82 $lastWarning = end($result->getWarnings());
83 $order->setField('REASON_MARKED', $lastWarning->getMessage());
84
85 if (
86 $entity instanceof Payment
87 || $entity instanceof Shipment
88 )
89 {
90 $entity->setField('MARKED', 'Y');
91 }
92 }
93
102 public static function updateMarker($id, array $values, Order $order, Internals\Entity $entity)
103 {
104 $result = new Result();
105 $entityType = static::getEntityType($entity);
106 if ($entityType !== null)
107 {
108 $r = static::updateItem($id, $values, $order, $entityType);
109 if (!$r->isSuccess())
110 {
111 $result->addErrors($r->getErrors());
112 }
113 }
114
115 return $result;
116 }
117
125 protected static function addItem(OrderBase $order, $entityType, array $values)
126 {
127 $orderCode = $order->getInternalId();
128
129 if (!empty(static::$pool[$orderCode]) && !empty(static::$pool[$orderCode][$entityType]) && is_array(static::$pool[$orderCode][$entityType]))
130 {
131 foreach (static::$pool[$orderCode][$entityType] as $index => $fields)
132 {
133 $foundItem = false;
134
135 foreach (static::getFieldsDuplicateCheck() as $checkField)
136 {
137 if (!empty($fields[$checkField]) && !empty($values[$checkField]) && $fields[$checkField] == $values[$checkField])
138 {
139 $foundItem = true;
140 continue;
141 }
142
143 $foundItem = false;
144 break;
145 }
146
147 if ($foundItem)
148 {
149 if (!empty($values['SUCCESS']))
150 {
151 static::$pool[$orderCode][$entityType][$index]['SUCCESS'] = $values['SUCCESS'];
152 return true;
153 }
154 }
155 }
156 }
157
158 static::$pool[$orderCode][$entityType][] = $values;
159 return true;
160 }
161
170 protected static function updateItem($id, $values, Order $order, $entityType)
171 {
172 $orderCode = $order->getInternalId();
173 $result = new Result();
174
175 if (!empty(static::$pool[$orderCode]) && !empty(static::$pool[$orderCode][$entityType]) && is_array(static::$pool[$orderCode][$entityType]))
176 {
177 foreach (static::$pool[$orderCode][$entityType] as $index => $fields)
178 {
179 $foundItem = false;
180 if ((isset($fields['ID']) && $id > 0 && intval($fields['ID']) == $id))
181 {
182 $foundItem = true;
183 }
184
185 if (!$foundItem)
186 {
187 foreach (static::getFieldsDuplicateCheck() as $checkField)
188 {
189 if (!empty($fields[$checkField]) && !empty($values[$checkField]) && $fields[$checkField] == $values[$checkField])
190 {
191 $foundItem = true;
192 continue;
193 }
194
195 $foundItem = false;
196 break;
197 }
198 }
199
200 if ($foundItem)
201 {
202 static::$pool[$orderCode][$entityType][$index] = array_merge($fields, $values);
203 return $result;
204 }
205 }
206 }
207
208 $values['ID'] = $id;
209
210 if (empty($values['ORDER']))
211 {
212 $values['ORDER'] = $order;
213 }
214
215 if ($order->getId() > 0)
216 {
217 $values['ORDER_ID'] = $order->getId();
218 }
219
220 static::$pool[$orderCode][$entityType][] = $values;
221
222 return $result;
223 }
224
231 public static function getMarker($orderCode, Internals\Entity $entity = null)
232 {
233 if (empty(static::$pool[$orderCode]))
234 {
235 return null;
236 }
237
238 if ($entity !== null)
239 {
240 $entityType = static::getEntityType($entity);
241 if ($entityType !== null && array_key_exists($entityType, static::$pool[$orderCode]))
242 {
243 return static::$pool[$orderCode][$entityType];
244 }
245 }
246 else
247 {
248 return static::$pool[$orderCode];
249 }
250
251 return null;
252 }
253
257 protected static function getEntityTypeList()
258 {
259 return array(
260 static::ENTITY_TYPE_ORDER => '\Bitrix\Sale\OrderBase',
261 static::ENTITY_TYPE_BASKET_ITEM => '\Bitrix\Sale\BasketItemBase',
262 static::ENTITY_TYPE_SHIPMENT => '\Bitrix\Sale\Shipment',
263 static::ENTITY_TYPE_PAYMENT => '\Bitrix\Sale\Payment',
264 static::ENTITY_TYPE_PROPERTY_VALUE => '\Bitrix\Sale\PropertyValue',
265 );
266 }
267
273 protected static function getEntityType(Internals\Entity $entity)
274 {
275 $typeList = static::getEntityTypeList();
276
277 foreach ($typeList as $type => $entityClass)
278 {
279 if ($entity instanceof $entityClass)
280 {
281 return $type;
282 }
283 }
284
285 return null;
286 }
287
295 public static function saveMarkers(Order $order = null)
296 {
297 global $USER;
298 $result = new Result();
299
300 $saveList = array();
301
302 $oldMarkerDataList = array();
303
304 $orderCode = null;
305
306 $newOrderList = array();
307
308 if ($order instanceof Order && $order->getId() > 0)
309 {
310 $orderCode = $order->getInternalId();
311 }
312
313 foreach (static::$pool as $orderIndex => $entityList)
314 {
315 foreach ($entityList as $entityType => $fieldsList)
316 {
317 foreach ($fieldsList as $fieldIndex => $values)
318 {
319 if ($values['ORDER'] instanceof Order)
320 {
321 if (empty($values['ORDER_ID']) && $values['ORDER']->getId() > 0)
322 {
323 $values['ORDER_ID'] = $values['ORDER']->getId();
324 $newOrderList[] = $values['ORDER_ID'];
325 }
326
327 if ($order instanceof Order && $values['ORDER']->getInternalId() != $order->getInternalId())
328 {
329 continue 3;
330 }
331 }
332
333 if (!empty($values['ENTITY']) && $values['ENTITY'] instanceof Internals\Entity)
334 {
335 if (empty($values['ENTITY_TYPE']))
336 {
337 $entityType = static::getEntityType($values['ENTITY']);
338
339 if (strval($entityType) != '')
340 {
341 $values['ENTITY_TYPE'] = $entityType;
342 }
343 }
344
345 if (!isset($values['ENTITY_ID']) || intval($values['ENTITY_ID']) <= 0)
346 {
347 $values['ENTITY_ID'] = $values['ENTITY']->getId();
348 }
349 }
350
351 $fields = array();
352
353 if (empty($values['ID']))
354 {
355 if (intval($values['ENTITY_ID']) <= 0)
356 {
357 continue;
358 }
359
360 if (empty($values['ENTITY_TYPE']))
361 {
362 throw new Main\ArgumentNullException('ENTITY_TYPE');
363 }
364
365 $fields = array(
366 'ENTITY_TYPE' => $values['ENTITY_TYPE'],
367 'ENTITY_ID' => intval($values['ENTITY_ID']),
368 'TYPE' => $values['TYPE'] ?? '',
369 'CODE' => $values['CODE'] ?? '',
370 'MESSAGE' => $values['MESSAGE'] ?? '',
371 'COMMENT' => $values['COMMENT'] ?? '',
372 );
373
374 if (is_object($USER) && $USER->IsAuthorized())
375 {
376 $fields['USER_ID'] = $USER->GetID();
377 }
378 }
379
380 if (intval($values['ORDER_ID']) >= 0)
381 {
382 $fields['ORDER_ID'] = intval($values['ORDER_ID']);
383 }
384
385 if (empty($fields['ENTITY_ID']) && intval($values['ENTITY_ID']) >= 0)
386 {
387 $fields['ENTITY_ID'] = intval($values['ENTITY_ID']);
388 }
389
390 if (empty($fields['ENTITY_TYPE']) && !empty($values['ENTITY_TYPE']))
391 {
392 $fields['ENTITY_TYPE'] = $values['ENTITY_TYPE'];
393 }
394
395 if (!empty($values['ID']))
396 {
397 $fields['ID'] = $values['ID'];
398 }
399
400 if (!empty($values['SUCCESS']))
401 {
402 $fields['SUCCESS'] = $values['SUCCESS'];
403 }
404
405 if (!empty($values['DATE_CREATE']) && $values['DATE_CREATE'] instanceof Main\Type\Date)
406 {
407 $fields['DATE_CREATE'] = $values['DATE_CREATE'];
408 }
409
410 if (!empty($values['DATE_UPDATE']) && $values['DATE_UPDATE'] instanceof Main\Type\Date)
411 {
412 $fields['DATE_UPDATE'] = $values['DATE_UPDATE'];
413 }
414
415 if ($values['ORDER'] instanceof Order)
416 {
417 unset(static::$pool[$values['ORDER']->getInternalId()][$entityType][$fieldIndex]);
418 }
419
420 if (empty($fields))
421 continue;
422
423 $markerOrderId = null;
424
425 if (!empty($values['ORDER_ID']))
426 {
427 $markerOrderId = $values['ORDER_ID'];
428 }
429
430 $saveList[$markerOrderId][] = $fields;
431 }
432 }
433 }
434
435 if (!empty($saveList) && is_array($saveList))
436 {
437 $filter = array(
438 'select' => array(
439 'ID', 'ORDER_ID', 'ENTITY_TYPE', 'ENTITY_ID', 'CODE', 'SUCCESS', 'MESSAGE'
440 ),
441 'filter' => array(
442 '!=SUCCESS' => static::ENTITY_SUCCESS_CODE_DONE
443 ),
444 'order' => array('ID' => 'ASC')
445 );
446
447 foreach ($saveList as $fieldsList)
448 {
449
450 foreach ($fieldsList as $fields)
451 {
452 if (!empty($fields['ORDER_ID']) && in_array($fields['ORDER_ID'], $newOrderList))
453 {
454 continue;
455 }
456
457 if (!empty($fields['ORDER_ID']) && (empty($filter['filter']['=ORDER_ID']) || !in_array($fields['ORDER_ID'], $filter['filter']['=ORDER_ID'])))
458 {
459 $filter['filter']['=ORDER_ID'][] = $fields['ORDER_ID'];
460 }
461
462 if (!empty($fields['ENTITY_TYPE'])
463 && (empty($filter['filter']['=ENTITY_TYPE'])
464 || (is_array($filter['filter']['=ENTITY_TYPE']) && !in_array($fields['ENTITY_TYPE'], $filter['filter']['=ENTITY_TYPE']))))
465 {
466 $filter['filter']['=ENTITY_TYPE'][] = $fields['ENTITY_TYPE'];
467 }
468 }
469 }
470
471
472 if (!empty($filter['filter']['=ENTITY_TYPE']))
473 {
474 $res = static::getList($filter);
475 while($data = $res->fetch())
476 {
477 if (isset($saveList[$data['ORDER_ID']]) && is_array($saveList[$data['ORDER_ID']]))
478 {
479 foreach($saveList[$data['ORDER_ID']] as $key => $values)
480 {
481 if (!empty($values['ID']) && $data['ID'] == $values['ID'])
482 {
483 $oldMarkerDataList[$data['ID']] = $data;
484
485 $values = array_merge($data, $values);
486 $saveList[$data['ORDER_ID']][$key] = $values;
487 continue;
488 }
489 $foundItem = false;
490
491 if (!$foundItem)
492 {
493 foreach (static::getFieldsDuplicateCheck() as $checkField)
494 {
495 if (!empty($data[$checkField]) && !empty($values[$checkField]) && $data[$checkField] == $values[$checkField])
496 {
497 $foundItem = true;
498 continue;
499 }
500
501 $foundItem = false;
502 break;
503 }
504 }
505
506 if ($foundItem)
507 {
508 foreach($saveList[$data['ORDER_ID']] as $doubleKey => $doubleValues)
509 {
510 if ($doubleKey == $key)
511 continue;
512
513 if (!empty($doubleValues['ID']) && $data['ID'] == $doubleValues['ID'])
514 {
515 if (empty($values['SUCCESS']))
516 {
517 unset($doubleValues['SUCCESS']);
518 }
519
520 $values = array_merge($doubleValues, $values);
521 unset($saveList[$data['ORDER_ID']][$doubleKey]);
522 }
523 }
524
525 $fields = array(
526 'ID' => $data['ID'],
527 );
528
529 if (!empty($values['SUCCESS']) && $data['SUCCESS'] != $values['SUCCESS'])
530 {
531 $fields['SUCCESS'] = $values['SUCCESS'];
532 }
533
534 $saveList[$data['ORDER_ID']][$key] = $fields;
535 }
536 }
537 }
538 }
539 }
540
541 foreach ($saveList as $orderId => $fieldsList)
542 {
543 foreach ($fieldsList as $fields)
544 {
545 if (!empty($fields['ID']))
546 {
547 $elementId = intval($fields['ID']);
548 unset($fields['ID']);
549
550 if (empty($fields))
551 continue;
552
553 if (!empty($oldMarkerDataList) && !empty($oldMarkerDataList[$elementId]))
554 {
555 foreach($fields as $fieldName => $fieldValue)
556 {
557 if (array_key_exists($fieldName, $oldMarkerDataList[$elementId])
558 && $oldMarkerDataList[$elementId][$fieldName] == $fieldValue)
559 {
560 unset($fields[$fieldName]);
561 }
562 }
563 }
564
565 if (empty($fields))
566 continue;
567
568 if (empty($fields['DATE_UPDATE']))
569 {
570 $fields['DATE_UPDATE'] = new Main\Type\DateTime();
571 }
572
573 if (!empty($fields['SUCCESS']) && $fields['SUCCESS'] == static::ENTITY_SUCCESS_CODE_DONE
574 && !empty($oldMarkerDataList) && !empty($oldMarkerDataList[$elementId]))
575 {
577 $oldMarkerDataList[$elementId]['ENTITY_TYPE'],
578 $oldMarkerDataList[$elementId]['ORDER_ID'],
579 'MARKER_SUCCESS',
580 $oldMarkerDataList[$elementId]['ENTITY_ID'],
581 null,
582 array(
583 "ENTITY_ID" => $oldMarkerDataList[$elementId]['ENTITY_ID'],
584 "MESSAGE" => $oldMarkerDataList[$elementId]['MESSAGE'],
585 "ENTITY_TYPE" => $oldMarkerDataList[$elementId]['ENTITY_TYPE'],
586 ),
588 );
589
590 $r = static::delete($elementId);
591 if (!$r->isSuccess())
592 {
593 $result->addErrors($r->getErrors());
594 }
595
596 continue;
597 }
598
599 $r = static::updateInternal($elementId, $fields);
600 if (!$r->isSuccess())
601 {
602 $result->addErrors($r->getErrors());
603 }
604 }
605 else
606 {
607 if (empty($fields['DATE_CREATE']))
608 {
609 $fields['DATE_CREATE'] = new Main\Type\DateTime();
610 }
611
612 $r = static::addInternal($fields);
613 if (!$r->isSuccess())
614 {
615 $result->addErrors($r->getErrors());
616 }
617 }
618 }
619 }
620 }
621
622 static::resetMarkers($orderCode);
623
624 return $result;
625 }
626
627
628 protected static function resetMarkers($orderCode = null)
629 {
630 if (intval($orderCode) > 0)
631 {
632 unset(static::$pool[$orderCode]);
633 }
634 else
635 {
636 static::$pool = array();
637 }
638 }
647 public static function tryFixErrorsByOrder(Order $order, $markerId = null)
648 {
649 $result = new Result();
650 if ($order->getId() <=0)
651 {
652 return $result;
653 }
654
655 $resultList = array(
656 'LIST' => array(),
657 'ERRORS' => array(),
658 );
659
660 $filter = array(
661 'filter' => array(
662 '=ORDER_ID' => $order->getId(),
663 '=TYPE' => static::ENTITY_MARKED_TYPE_AUTO,
664 ),
665 'select' => array('ID', 'ENTITY_TYPE', 'ENTITY_ID', 'CODE', 'SUCCESS'),
666 'order' => array('ID' => 'DESC')
667 );
668
669 if (intval($markerId) > 0)
670 {
671 $filter['filter']['=ID'] = intval($markerId);
672 }
673 else
674 {
675 $filter['filter']['!=SUCCESS'] = static::ENTITY_SUCCESS_CODE_DONE;
676 }
677
678 $res = static::getList($filter);
679 while($markerData = $res->fetch())
680 {
681 if ($markerData['SUCCESS'] == static::ENTITY_SUCCESS_CODE_DONE)
682 {
683 $resultList['LIST'][$markerData['ID']] = static::ENTITY_SUCCESS_CODE_DONE;
684 }
685 else
686 {
687 if (!$entity = static::getEntity($order, $markerData['ENTITY_TYPE'], $markerData['ENTITY_ID']))
688 {
689 $result->addError(new ResultError(Main\Localization\Loc::getMessage('SALE_ENTITY_MARKER_ENTITY_NOT_FOUND'), 'SALE_ENTITY_MARKER_ENTITY_NOT_FOUND'));
690 return $result;
691 }
692
693 if (!($entity instanceof \IEntityMarker))
694 {
695 return $result;
696 }
697
698 $r = $entity->tryFixError($markerData['CODE']);
699 if ($r->isSuccess() && !$r->hasWarnings())
700 {
701 $markerData['SUCCESS'] = static::ENTITY_SUCCESS_CODE_DONE;
702 }
703 else
704 {
705 $markerData['SUCCESS'] = static::ENTITY_SUCCESS_CODE_FAIL;
706 if (!isset($resultList['ERRORS'][$markerData['ID']]))
707 {
708 $resultList['ERRORS'][$markerData['ID']] = array();
709 }
710 $resultList['ERRORS'][$markerData['ID']] = array_merge($resultList['ERRORS'][$markerData['ID']], $r->getWarningMessages());
711 if (!$r->isSuccess())
712 {
713 $result->addErrors($r->getErrors());
714 }
715 }
716
717 static::updateMarker($markerData['ID'], $markerData, $order, $entity);
718 $resultList['LIST'][$markerData['ID']] = ($markerData['SUCCESS'] == static::ENTITY_SUCCESS_CODE_DONE);
719 }
720 }
721
722 if (!empty($resultList) && is_array($resultList))
723 {
724 $result->setData($resultList);
725 }
726
727 return $result;
728 }
729
730
736 public static function tryFixErrors()
737 {
738 static $orderList = array();
739 $orderSaveList = array();
740 $lastOrderId = null;
741
742 $registry = Sale\Registry::getInstance(Sale\Registry::REGISTRY_TYPE_ORDER);
744 $orderClass = $registry->getOrderClassName();
745
746 $result = new Result();
747 $res = static::getList(array(
748 'filter' => array(
749 '=TYPE' => static::ENTITY_MARKED_TYPE_AUTO,
750 '!=SUCCESS' => static::ENTITY_SUCCESS_CODE_DONE
751 ),
752 'select' => array('ID', 'ENTITY_TYPE', 'ENTITY_ID', 'CODE', 'ORDER_ID'),
753 'order' => array('ORDER_ID' => 'ASC', 'ID' => 'DESC')
754 ));
755 while($data = $res->fetch())
756 {
757 if (array_key_exists($data['ORDER_ID'], $orderList))
758 {
759 $order = $orderList[$data['ORDER_ID']];
760 }
761 else
762 {
763 $order = $orderClass::load($data['ORDER_ID']);
764 $orderList[$data['ORDER_ID']] = $order;
765 }
766
767 if (!$entity = static::getEntity($order, $data['ENTITY_TYPE'], $data['ENTITY_ID']))
768 {
769 continue;
770 }
771
772 if ($lastOrderId !== null && $lastOrderId !== $order->getId())
773 {
774 if (isset($orderSaveList[$lastOrderId]))
775 {
776 $r = $orderSaveList[$lastOrderId]->save();
777 unset($orderSaveList[$lastOrderId]);
778 }
779 }
780
781 if (!($entity instanceof \IEntityMarker))
782 {
783 continue;
784 }
785
786 $r = $entity->tryFixError($data['CODE']);
787 if ($r->isSuccess())
788 {
789 $data['SUCCESS'] = static::ENTITY_SUCCESS_CODE_DONE;
790
791 if (!array_key_exists($data['ORDER_ID'], $orderSaveList))
792 {
793 $orderSaveList[$order->getId()] = $order;
794 }
795 }
796 else
797 {
798 $data['SUCCESS'] = static::ENTITY_SUCCESS_CODE_FAIL;
799 }
800
801 static::updateMarker($data['ID'], $data, $order, $entity);
802
803 $lastOrderId = $order->getId();
804 }
805
806 if (!empty($orderSaveList))
807 {
808 foreach ($orderSaveList as $order)
809 {
810 $order->save();
811 }
812 }
813
814 foreach ($orderList as $order)
815 {
816 static::saveMarkers($order);
817 }
818
819 return $result;
820 }
821
822 public static function loadFromDb(array $filter)
823 {
824 $entityDat = static::getList($filter)->fetch();
825 if ($entityDat)
826 {
827 return $entityDat;
828 }
829
830 return false;
831 }
832
841 public static function getEntity(Order $order, $entityType, $entityId)
842 {
843 static $entityList = array();
844
845 $hash = md5($order->getId(). '|'. $entityType . '|' . $entityId);
846
847 if (!empty($entityList[$hash]))
848 {
849 return $entityList[$hash];
850 }
851
852 $entity = null;
853 $entityCollection = null;
854
855 if ($entityType == static::ENTITY_TYPE_ORDER)
856 {
857 if ($order->getId() == $entityId)
858 {
859 return $order;
860 }
861 return null;
862 }
863 elseif($entityType == static::ENTITY_TYPE_SHIPMENT)
864 {
866 $entityCollection = $order->getShipmentCollection();
867 }
868 elseif($entityType == static::ENTITY_TYPE_PAYMENT)
869 {
871 $entityCollection = $order->getPaymentCollection();
872 }
873 elseif($entityType == static::ENTITY_TYPE_BASKET_ITEM)
874 {
876 $entityCollection = $order->getBasket();
877 }
878
879 if ($entity === null && !$entityCollection)
880 return null;
881
882 if ($entity === null)
883 {
885 if (!$entity = $entityCollection->getItemById($entityId))
886 {
887 return null;
888 }
889 }
890
891 if ($entity !== null)
892 {
893 $entityList[$hash] = $entity;
894 }
895
896 return $entity;
897 }
898
905 public static function getList(array $parameters = array())
906 {
907 return Internals\EntityMarkerTable::getList($parameters);
908 }
909
917 public static function delete($id)
918 {
919 if (intval($id) <= 0)
920 {
921 throw new Main\ArgumentNullException('ID');
922 }
923
924 return Internals\EntityMarkerTable::delete($id);
925 }
926
927 protected static function addInternal(array $data)
928 {
929 return Internals\EntityMarkerTable::add($data);
930 }
931
932 protected static function updateInternal($primary, array $data)
933 {
934 return Internals\EntityMarkerTable::update($primary, $data);
935 }
936
946 public static function getPoolItemSuccess(Order $order, $id, $entityType, $entityId, $code)
947 {
948 $orderCode = $order->getInternalId();
949
950 if (!empty(static::$pool[$orderCode]))
951 {
952 foreach (static::$pool[$orderCode] as $poolEntityType => $fieldsList)
953 {
954 foreach ($fieldsList as $fieldIndex => $values)
955 {
956 if ($values['ORDER'] instanceof Order)
957 {
958 if ($order instanceof Order && $values['ORDER']->getInternalId() != $order->getInternalId())
959 {
960 continue 2;
961 }
962 }
963
964 if (!empty($values['SUCCESS'])
965 && (isset($values['ENTITY_ID']) && intval($values['ENTITY_ID']) == intval($entityId))
966 && (isset($values['ENTITY_TYPE']) && $values['ENTITY_TYPE'] == $entityType)
967 && (isset($values['CODE']) && $values['CODE'] == $code)
968 )
969 {
970 if ((!empty($values['ID']) && $values['ID'] == $id) || !isset($values['ID']))
971 {
972 return $values['SUCCESS'];
973 }
974 }
975 }
976 }
977 }
978
979 return null;
980 }
981
982 public static function hasErrors(Order $order)
983 {
984 $orderCode = $order->getInternalId();
985 if (!empty(static::$pool[$orderCode]))
986 {
987 foreach (static::$pool[$orderCode] as $poolEntityType => $fieldsList)
988 {
989 foreach ($fieldsList as $fieldIndex => $values)
990 {
991 if ($values['ORDER'] instanceof Order)
992 {
993 if ($order instanceof Order && $values['ORDER']->getInternalId() != $order->getInternalId())
994 {
995 continue 2;
996 }
997 }
998
999 if(empty($values['SUCCESS']) || ($values['SUCCESS'] != static::ENTITY_SUCCESS_CODE_DONE))
1000 {
1001 return true;
1002 }
1003 }
1004 }
1005 }
1006
1007 return false;
1008 }
1009
1010 private static function getFieldsDuplicateCheck()
1011 {
1012 return array(
1013 'ENTITY_ID',
1014 'ENTITY_TYPE',
1015 'CODE',
1016 );
1017 }
1018
1024 public static function deleteByOrderId($id)
1025 {
1026 if(intval($id) <= 0)
1027 return false;
1028
1029 $res = static::getList(array(
1030 'filter' => array(
1031 '=ORDER_ID' => $id
1032 ),
1033 'select' => array('ID')
1034 ));
1035 while($data = $res->fetch())
1036 {
1037 static::delete($data['ID']);
1038 }
1039 }
1040
1046 public static function deleteByEntity(Internals\Entity $entity)
1047 {
1048 if($entity->getId() <= 0)
1049 return false;
1050
1051 if ($entityType = static::getEntityType($entity))
1052 {
1053 $res = static::getList(array(
1054 'filter' => array(
1055 '=ENTITY_ID' => $entity->getId(),
1056 '=ENTITY_TYPE' => $entityType
1057 ),
1058 'select' => array('ID')
1059 ));
1060 while($data = $res->fetch())
1061 {
1062 static::delete($data['ID']);
1063 }
1064 }
1065 }
1066
1067 public static function deleteByFilter(array $values)
1068 {
1069 $res = static::getList(array(
1070 'filter' => $values,
1071 'select' => array('ID')
1072 ));
1073 while($data = $res->fetch())
1074 {
1075 static::delete($data['ID']);
1076 }
1077 }
1078
1089 public static function refreshMarkers(Order $order)
1090 {
1091 if ($order->getId() == 0)
1092 {
1093 return;
1094 }
1095
1096 $markList = [];
1097
1098 $filter = [
1099 'filter' => [
1100 '=ORDER_ID' => $order->getId(),
1101 '!=SUCCESS' => static::ENTITY_SUCCESS_CODE_DONE
1102 ],
1103 'select' => ['ID', 'ENTITY_TYPE', 'ENTITY_ID', 'CODE', 'SUCCESS'],
1104 'order' => ['ID' => 'DESC']
1105 ];
1106
1107 $res = static::getList($filter);
1108 while($markerData = $res->fetch())
1109 {
1110 if (!empty($markList[$markerData['ENTITY_TYPE']])
1111 && !empty($markList[$markerData['ENTITY_TYPE']][$markerData['ENTITY_ID']])
1112 && $markerData['CODE'] == $markList[$markerData['ENTITY_TYPE']][$markerData['ENTITY_ID']]
1113 )
1114 {
1115 continue;
1116 }
1117
1118 if ($markerData['SUCCESS'] != static::ENTITY_SUCCESS_CODE_DONE)
1119 {
1120 $markList[$markerData['ENTITY_TYPE']][$markerData['ENTITY_ID']][] = $markerData['CODE'];
1121 }
1122
1123 $poolItemSuccess = static::getPoolItemSuccess(
1124 $order,
1125 $markerData['ID'],
1126 $markerData['ENTITY_TYPE'],
1127 $markerData['ENTITY_ID'],
1128 $markerData['CODE']
1129 );
1130
1131 if ($poolItemSuccess && $poolItemSuccess == static::ENTITY_SUCCESS_CODE_DONE)
1132 {
1133 foreach ($markList[$markerData['ENTITY_TYPE']][$markerData['ENTITY_ID']] as $markerIndex => $markerCode)
1134 {
1135 if ($markerData['CODE'] == $markerCode)
1136 {
1137 unset($markList[$markerData['ENTITY_TYPE']][$markerData['ENTITY_ID']][$markerIndex]);
1138 }
1139 }
1140
1141 if (empty($markList[$markerData['ENTITY_TYPE']][$markerData['ENTITY_ID']]))
1142 {
1143 unset($markList[$markerData['ENTITY_TYPE']][$markerData['ENTITY_ID']]);
1144 }
1145 }
1146
1147 if (empty($markList[$markerData['ENTITY_TYPE']]))
1148 {
1149 unset($markList[$markerData['ENTITY_TYPE']]);
1150 }
1151 }
1152
1153 if (!empty($markList))
1154 {
1155 foreach ($markList as $markEntityType => $markEntityList)
1156 {
1157 foreach ($markEntityList as $markEntityId => $markEntityCodeList)
1158 {
1159 if (empty($markEntityCodeList))
1160 {
1161 if (($entity = static::getEntity($order, $markEntityType, $markEntityId)) && ($entity instanceof \IEntityMarker))
1162 {
1163 if ($entity->canMarked())
1164 {
1165 $markedField = $entity->getMarkField();
1166 $entity->setField($markedField, 'N');
1167 }
1168 }
1169 }
1170 }
1171 }
1172 }
1173
1174 if (empty($markList) && !static::hasErrors($order))
1175 {
1176 $shipmentCollection = $order->getShipmentCollection();
1177 if ($shipmentCollection->isMarked())
1178 {
1180 foreach ($shipmentCollection as $shipment)
1181 {
1182 if ($shipment->isMarked())
1183 {
1184 $shipment->setField('MARKED', 'N');
1185 }
1186 }
1187 }
1188
1189 $paymentCollection = $order->getPaymentCollection();
1190 if ($paymentCollection->isMarked())
1191 {
1193 foreach ($paymentCollection as $payment)
1194 {
1195 if ($payment->isMarked())
1196 {
1197 $payment->setField('MARKED', 'N');
1198 }
1199 }
1200 }
1201
1202 $order->setField('MARKED', 'N');
1203 }
1204 }
1205}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static addItem(OrderBase $order, $entityType, array $values)
static loadFromDb(array $filter)
static hasErrors(Order $order)
static getEntityType(Internals\Entity $entity)
static getPoolItemSuccess(Order $order, $id, $entityType, $entityId, $code)
static getList(array $parameters=array())
static updateInternal($primary, array $data)
static getMarker($orderCode, Internals\Entity $entity=null)
static resetMarkers($orderCode=null)
static saveMarkers(Order $order=null)
static addInternal(array $data)
static deleteByFilter(array $values)
static deleteByEntity(Internals\Entity $entity)
static updateMarker($id, array $values, Order $order, Internals\Entity $entity)
static updateItem($id, $values, Order $order, $entityType)
static tryFixErrorsByOrder(Order $order, $markerId=null)
setField($name, $value)
const SALE_ORDER_HISTORY_ACTION_LOG_LEVEL_1
static addAction($entityName, $orderId, $type, $id=null, $entity=null, array $fields=array(), $level=null)