28 protected static $pool = array();
37 if (!$result->hasWarnings())
42 $entityType = static::getEntityType($entity);
43 if ($entityType ===
null)
53 if ($order->getId() > 0)
55 $fields[
'ORDER_ID'] = $order->getId();
58 if ($entity->getId() > 0)
60 $fields[
'ENTITY_ID'] = $entity->getId();
63 $fields[
'ENTITY_TYPE'] = $entityType;
65 foreach ($result->getWarnings() as $resultWarning)
67 $code = $resultWarning->getCode();
68 $message = $resultWarning->getMessage();
73 $isAutoFix = $entity->canAutoFixError($code);
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);
82 $lastWarning = end($result->getWarnings());
83 $order->
setField(
'REASON_MARKED', $lastWarning->getMessage());
86 $entity instanceof Payment
87 || $entity instanceof Shipment
90 $entity->setField(
'MARKED',
'Y');
105 $entityType = static::getEntityType($entity);
106 if ($entityType !==
null)
108 $r = static::updateItem($id, $values, $order, $entityType);
109 if (!$r->isSuccess())
111 $result->addErrors($r->getErrors());
129 if (!empty(static::$pool[$orderCode]) && !empty(static::$pool[$orderCode][$entityType]) && is_array(static::$pool[$orderCode][$entityType]))
131 foreach (static::$pool[$orderCode][$entityType] as $index => $fields)
135 foreach (static::getFieldsDuplicateCheck() as $checkField)
137 if (!empty($fields[$checkField]) && !empty($values[$checkField]) && $fields[$checkField] == $values[$checkField])
149 if (!empty($values[
'SUCCESS']))
151 static::$pool[$orderCode][$entityType][$index][
'SUCCESS'] = $values[
'SUCCESS'];
158 static::$pool[$orderCode][$entityType][] = $values;
172 $orderCode = $order->getInternalId();
175 if (!empty(static::$pool[$orderCode]) && !empty(static::$pool[$orderCode][$entityType]) && is_array(static::$pool[$orderCode][$entityType]))
177 foreach (static::$pool[$orderCode][$entityType] as $index => $fields)
180 if ((isset($fields[
'ID']) && $id > 0 && intval($fields[
'ID']) == $id))
187 foreach (static::getFieldsDuplicateCheck() as $checkField)
189 if (!empty($fields[$checkField]) && !empty($values[$checkField]) && $fields[$checkField] == $values[$checkField])
202 static::$pool[$orderCode][$entityType][$index] = array_merge($fields, $values);
210 if (empty($values[
'ORDER']))
212 $values[
'ORDER'] = $order;
215 if ($order->getId() > 0)
217 $values[
'ORDER_ID'] = $order->getId();
220 static::$pool[$orderCode][$entityType][] = $values;
233 if (empty(static::$pool[$orderCode]))
238 if ($entity !==
null)
240 $entityType = static::getEntityType($entity);
241 if ($entityType !==
null && array_key_exists($entityType, static::$pool[$orderCode]))
243 return static::$pool[$orderCode][$entityType];
248 return static::$pool[$orderCode];
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',
275 $typeList = static::getEntityTypeList();
277 foreach ($typeList as $type => $entityClass)
279 if ($entity instanceof $entityClass)
302 $oldMarkerDataList = array();
306 $newOrderList = array();
308 if ($order instanceof
Order && $order->getId() > 0)
313 foreach (static::$pool as $orderIndex => $entityList)
315 foreach ($entityList as $entityType => $fieldsList)
317 foreach ($fieldsList as $fieldIndex => $values)
319 if ($values[
'ORDER'] instanceof
Order)
321 if (empty($values[
'ORDER_ID']) && $values[
'ORDER']->getId() > 0)
323 $values[
'ORDER_ID'] = $values[
'ORDER']->getId();
324 $newOrderList[] = $values[
'ORDER_ID'];
327 if ($order instanceof
Order && $values[
'ORDER']->getInternalId() != $order->
getInternalId())
333 if (!empty($values[
'ENTITY']) && $values[
'ENTITY'] instanceof Internals\
Entity)
335 if (empty($values[
'ENTITY_TYPE']))
337 $entityType = static::getEntityType($values[
'ENTITY']);
339 if (strval($entityType) !=
'')
341 $values[
'ENTITY_TYPE'] = $entityType;
345 if (!isset($values[
'ENTITY_ID']) || intval($values[
'ENTITY_ID']) <= 0)
347 $values[
'ENTITY_ID'] = $values[
'ENTITY']->getId();
353 if (empty($values[
'ID']))
355 if (intval($values[
'ENTITY_ID']) <= 0)
360 if (empty($values[
'ENTITY_TYPE']))
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'] ??
'',
374 if (is_object($USER) && $USER->IsAuthorized())
376 $fields[
'USER_ID'] = $USER->GetID();
380 if (intval($values[
'ORDER_ID']) >= 0)
382 $fields[
'ORDER_ID'] = intval($values[
'ORDER_ID']);
385 if (empty($fields[
'ENTITY_ID']) && intval($values[
'ENTITY_ID']) >= 0)
387 $fields[
'ENTITY_ID'] = intval($values[
'ENTITY_ID']);
390 if (empty($fields[
'ENTITY_TYPE']) && !empty($values[
'ENTITY_TYPE']))
392 $fields[
'ENTITY_TYPE'] = $values[
'ENTITY_TYPE'];
395 if (!empty($values[
'ID']))
397 $fields[
'ID'] = $values[
'ID'];
400 if (!empty($values[
'SUCCESS']))
402 $fields[
'SUCCESS'] = $values[
'SUCCESS'];
405 if (!empty($values[
'DATE_CREATE']) && $values[
'DATE_CREATE'] instanceof Main\
Type\
Date)
407 $fields[
'DATE_CREATE'] = $values[
'DATE_CREATE'];
410 if (!empty($values[
'DATE_UPDATE']) && $values[
'DATE_UPDATE'] instanceof Main\
Type\
Date)
412 $fields[
'DATE_UPDATE'] = $values[
'DATE_UPDATE'];
415 if ($values[
'ORDER'] instanceof
Order)
417 unset(static::$pool[$values[
'ORDER']->getInternalId()][$entityType][$fieldIndex]);
423 $markerOrderId =
null;
425 if (!empty($values[
'ORDER_ID']))
427 $markerOrderId = $values[
'ORDER_ID'];
430 $saveList[$markerOrderId][] = $fields;
435 if (!empty($saveList) && is_array($saveList))
439 'ID',
'ORDER_ID',
'ENTITY_TYPE',
'ENTITY_ID',
'CODE',
'SUCCESS',
'MESSAGE'
442 '!=SUCCESS' => static::ENTITY_SUCCESS_CODE_DONE
444 'order' => array(
'ID' =>
'ASC')
447 foreach ($saveList as $fieldsList)
450 foreach ($fieldsList as $fields)
452 if (!empty($fields[
'ORDER_ID']) && in_array($fields[
'ORDER_ID'], $newOrderList))
457 if (!empty($fields[
'ORDER_ID']) && (empty($filter[
'filter'][
'=ORDER_ID']) || !in_array($fields[
'ORDER_ID'], $filter[
'filter'][
'=ORDER_ID'])))
459 $filter[
'filter'][
'=ORDER_ID'][] = $fields[
'ORDER_ID'];
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']))))
466 $filter[
'filter'][
'=ENTITY_TYPE'][] = $fields[
'ENTITY_TYPE'];
472 if (!empty($filter[
'filter'][
'=ENTITY_TYPE']))
474 $res = static::getList($filter);
475 while($data = $res->fetch())
477 if (isset($saveList[$data[
'ORDER_ID']]) && is_array($saveList[$data[
'ORDER_ID']]))
479 foreach($saveList[$data[
'ORDER_ID']] as $key => $values)
481 if (!empty($values[
'ID']) && $data[
'ID'] == $values[
'ID'])
483 $oldMarkerDataList[$data[
'ID']] = $data;
485 $values = array_merge($data, $values);
486 $saveList[$data[
'ORDER_ID']][$key] = $values;
493 foreach (static::getFieldsDuplicateCheck() as $checkField)
495 if (!empty($data[$checkField]) && !empty($values[$checkField]) && $data[$checkField] == $values[$checkField])
508 foreach($saveList[$data[
'ORDER_ID']] as $doubleKey => $doubleValues)
510 if ($doubleKey == $key)
513 if (!empty($doubleValues[
'ID']) && $data[
'ID'] == $doubleValues[
'ID'])
515 if (empty($values[
'SUCCESS']))
517 unset($doubleValues[
'SUCCESS']);
520 $values = array_merge($doubleValues, $values);
521 unset($saveList[$data[
'ORDER_ID']][$doubleKey]);
529 if (!empty($values[
'SUCCESS']) && $data[
'SUCCESS'] != $values[
'SUCCESS'])
531 $fields[
'SUCCESS'] = $values[
'SUCCESS'];
534 $saveList[$data[
'ORDER_ID']][$key] = $fields;
541 foreach ($saveList as $orderId => $fieldsList)
543 foreach ($fieldsList as $fields)
545 if (!empty($fields[
'ID']))
547 $elementId = intval($fields[
'ID']);
548 unset($fields[
'ID']);
553 if (!empty($oldMarkerDataList) && !empty($oldMarkerDataList[$elementId]))
555 foreach($fields as $fieldName => $fieldValue)
557 if (array_key_exists($fieldName, $oldMarkerDataList[$elementId])
558 && $oldMarkerDataList[$elementId][$fieldName] == $fieldValue)
560 unset($fields[$fieldName]);
568 if (empty($fields[
'DATE_UPDATE']))
573 if (!empty($fields[
'SUCCESS']) && $fields[
'SUCCESS'] == static::ENTITY_SUCCESS_CODE_DONE
574 && !empty($oldMarkerDataList) && !empty($oldMarkerDataList[$elementId]))
577 $oldMarkerDataList[$elementId][
'ENTITY_TYPE'],
578 $oldMarkerDataList[$elementId][
'ORDER_ID'],
580 $oldMarkerDataList[$elementId][
'ENTITY_ID'],
583 "ENTITY_ID" => $oldMarkerDataList[$elementId][
'ENTITY_ID'],
584 "MESSAGE" => $oldMarkerDataList[$elementId][
'MESSAGE'],
585 "ENTITY_TYPE" => $oldMarkerDataList[$elementId][
'ENTITY_TYPE'],
590 $r = static::delete($elementId);
591 if (!$r->isSuccess())
593 $result->addErrors($r->getErrors());
599 $r = static::updateInternal($elementId, $fields);
600 if (!$r->isSuccess())
602 $result->addErrors($r->getErrors());
607 if (empty($fields[
'DATE_CREATE']))
612 $r = static::addInternal($fields);
613 if (!$r->isSuccess())
615 $result->addErrors($r->getErrors());
622 static::resetMarkers($orderCode);
630 if (intval($orderCode) > 0)
632 unset(static::$pool[$orderCode]);
636 static::$pool = array();
650 if ($order->getId() <=0)
662 '=ORDER_ID' => $order->getId(),
663 '=TYPE' => static::ENTITY_MARKED_TYPE_AUTO,
665 'select' => array(
'ID',
'ENTITY_TYPE',
'ENTITY_ID',
'CODE',
'SUCCESS'),
666 'order' => array(
'ID' =>
'DESC')
669 if (intval($markerId) > 0)
671 $filter[
'filter'][
'=ID'] = intval($markerId);
675 $filter[
'filter'][
'!=SUCCESS'] = static::ENTITY_SUCCESS_CODE_DONE;
678 $res = static::getList($filter);
679 while($markerData = $res->fetch())
681 if ($markerData[
'SUCCESS'] == static::ENTITY_SUCCESS_CODE_DONE)
683 $resultList[
'LIST'][$markerData[
'ID']] = static::ENTITY_SUCCESS_CODE_DONE;
687 if (!$entity = static::getEntity($order, $markerData[
'ENTITY_TYPE'], $markerData[
'ENTITY_ID']))
689 $result->addError(
new ResultError(Main\Localization\
Loc::getMessage(
'SALE_ENTITY_MARKER_ENTITY_NOT_FOUND'),
'SALE_ENTITY_MARKER_ENTITY_NOT_FOUND'));
698 $r = $entity->tryFixError($markerData[
'CODE']);
699 if ($r->isSuccess() && !$r->hasWarnings())
701 $markerData[
'SUCCESS'] = static::ENTITY_SUCCESS_CODE_DONE;
705 $markerData[
'SUCCESS'] = static::ENTITY_SUCCESS_CODE_FAIL;
706 if (!isset($resultList[
'ERRORS'][$markerData[
'ID']]))
708 $resultList[
'ERRORS'][$markerData[
'ID']] = array();
710 $resultList[
'ERRORS'][$markerData[
'ID']] = array_merge($resultList[
'ERRORS'][$markerData[
'ID']], $r->getWarningMessages());
711 if (!$r->isSuccess())
713 $result->addErrors($r->getErrors());
717 static::updateMarker($markerData[
'ID'], $markerData, $order, $entity);
718 $resultList[
'LIST'][$markerData[
'ID']] = ($markerData[
'SUCCESS'] == static::ENTITY_SUCCESS_CODE_DONE);
722 if (!empty($resultList) && is_array($resultList))
724 $result->setData($resultList);
736 public static function tryFixErrors()
738 static $orderList = array();
739 $orderSaveList = array();
744 $orderClass = $registry->getOrderClassName();
747 $res = static::getList(array(
749 '=TYPE' => static::ENTITY_MARKED_TYPE_AUTO,
750 '!=SUCCESS' => static::ENTITY_SUCCESS_CODE_DONE
752 'select' => array(
'ID',
'ENTITY_TYPE',
'ENTITY_ID',
'CODE',
'ORDER_ID'),
753 'order' => array(
'ORDER_ID' =>
'ASC',
'ID' =>
'DESC')
755 while($data = $res->fetch())
757 if (array_key_exists($data[
'ORDER_ID'], $orderList))
759 $order = $orderList[$data[
'ORDER_ID']];
763 $order = $orderClass::load($data[
'ORDER_ID']);
764 $orderList[$data[
'ORDER_ID']] = $order;
767 if (!$entity = static::getEntity($order, $data[
'ENTITY_TYPE'], $data[
'ENTITY_ID']))
772 if ($lastOrderId !==
null && $lastOrderId !== $order->getId())
774 if (isset($orderSaveList[$lastOrderId]))
776 $r = $orderSaveList[$lastOrderId]->save();
777 unset($orderSaveList[$lastOrderId]);
786 $r = $entity->tryFixError($data[
'CODE']);
789 $data[
'SUCCESS'] = static::ENTITY_SUCCESS_CODE_DONE;
791 if (!array_key_exists($data[
'ORDER_ID'], $orderSaveList))
793 $orderSaveList[$order->getId()] = $order;
798 $data[
'SUCCESS'] = static::ENTITY_SUCCESS_CODE_FAIL;
801 static::updateMarker($data[
'ID'], $data, $order, $entity);
803 $lastOrderId = $order->getId();
806 if (!empty($orderSaveList))
808 foreach ($orderSaveList as $order)
814 foreach ($orderList as $order)
816 static::saveMarkers($order);
824 $entityDat = static::getList($filter)->fetch();
841 public static function getEntity(
Order $order, $entityType, $entityId)
843 static $entityList = array();
845 $hash = md5($order->getId().
'|'. $entityType .
'|' . $entityId);
847 if (!empty($entityList[$hash]))
849 return $entityList[$hash];
853 $entityCollection =
null;
855 if ($entityType == static::ENTITY_TYPE_ORDER)
857 if ($order->getId() == $entityId)
863 elseif($entityType == static::ENTITY_TYPE_SHIPMENT)
866 $entityCollection = $order->getShipmentCollection();
868 elseif($entityType == static::ENTITY_TYPE_PAYMENT)
871 $entityCollection = $order->getPaymentCollection();
873 elseif($entityType == static::ENTITY_TYPE_BASKET_ITEM)
876 $entityCollection = $order->getBasket();
879 if ($entity ===
null && !$entityCollection)
882 if ($entity ===
null)
885 if (!$entity = $entityCollection->getItemById($entityId))
891 if ($entity !==
null)
893 $entityList[$hash] = $entity;
905 public static function getList(array $parameters = array())
907 return Internals\EntityMarkerTable::getList($parameters);
917 public static function delete($id)
919 if (intval($id) <= 0)
924 return Internals\EntityMarkerTable::delete($id);
929 return Internals\EntityMarkerTable::add($data);
934 return Internals\EntityMarkerTable::update($primary, $data);
948 $orderCode = $order->getInternalId();
950 if (!empty(static::$pool[$orderCode]))
952 foreach (static::$pool[$orderCode] as $poolEntityType => $fieldsList)
954 foreach ($fieldsList as $fieldIndex => $values)
956 if ($values[
'ORDER'] instanceof
Order)
958 if ($order instanceof
Order && $values[
'ORDER']->getInternalId() != $order->getInternalId())
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)
970 if ((!empty($values[
'ID']) && $values[
'ID'] == $id) || !isset($values[
'ID']))
972 return $values[
'SUCCESS'];
984 $orderCode = $order->getInternalId();
985 if (!empty(static::$pool[$orderCode]))
987 foreach (static::$pool[$orderCode] as $poolEntityType => $fieldsList)
989 foreach ($fieldsList as $fieldIndex => $values)
991 if ($values[
'ORDER'] instanceof
Order)
993 if ($order instanceof
Order && $values[
'ORDER']->getInternalId() != $order->getInternalId())
999 if(empty($values[
'SUCCESS']) || ($values[
'SUCCESS'] != static::ENTITY_SUCCESS_CODE_DONE))
1010 private static function getFieldsDuplicateCheck()
1026 if(intval($id) <= 0)
1029 $res = static::getList(array(
1033 'select' => array(
'ID')
1035 while($data = $res->fetch())
1037 static::delete($data[
'ID']);
1048 if($entity->getId() <= 0)
1051 if ($entityType = static::getEntityType($entity))
1053 $res = static::getList(array(
1055 '=ENTITY_ID' => $entity->getId(),
1056 '=ENTITY_TYPE' => $entityType
1058 'select' => array(
'ID')
1060 while($data = $res->fetch())
1062 static::delete($data[
'ID']);
1069 $res = static::getList(array(
1070 'filter' => $values,
1071 'select' => array(
'ID')
1073 while($data = $res->fetch())
1075 static::delete($data[
'ID']);
1089 public static function refreshMarkers(
Order $order)
1091 if ($order->getId() == 0)
1100 '=ORDER_ID' => $order->getId(),
1101 '!=SUCCESS' => static::ENTITY_SUCCESS_CODE_DONE
1103 'select' => [
'ID',
'ENTITY_TYPE',
'ENTITY_ID',
'CODE',
'SUCCESS'],
1104 'order' => [
'ID' =>
'DESC']
1107 $res = static::getList($filter);
1108 while($markerData = $res->fetch())
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']]
1118 if ($markerData[
'SUCCESS'] != static::ENTITY_SUCCESS_CODE_DONE)
1120 $markList[$markerData[
'ENTITY_TYPE']][$markerData[
'ENTITY_ID']][] = $markerData[
'CODE'];
1123 $poolItemSuccess = static::getPoolItemSuccess(
1126 $markerData[
'ENTITY_TYPE'],
1127 $markerData[
'ENTITY_ID'],
1131 if ($poolItemSuccess && $poolItemSuccess == static::ENTITY_SUCCESS_CODE_DONE)
1133 foreach ($markList[$markerData[
'ENTITY_TYPE']][$markerData[
'ENTITY_ID']] as $markerIndex => $markerCode)
1135 if ($markerData[
'CODE'] == $markerCode)
1137 unset($markList[$markerData[
'ENTITY_TYPE']][$markerData[
'ENTITY_ID']][$markerIndex]);
1141 if (empty($markList[$markerData[
'ENTITY_TYPE']][$markerData[
'ENTITY_ID']]))
1143 unset($markList[$markerData[
'ENTITY_TYPE']][$markerData[
'ENTITY_ID']]);
1147 if (empty($markList[$markerData[
'ENTITY_TYPE']]))
1149 unset($markList[$markerData[
'ENTITY_TYPE']]);
1153 if (!empty($markList))
1155 foreach ($markList as $markEntityType => $markEntityList)
1157 foreach ($markEntityList as $markEntityId => $markEntityCodeList)
1159 if (empty($markEntityCodeList))
1161 if (($entity = static::getEntity($order, $markEntityType, $markEntityId)) && ($entity instanceof \
IEntityMarker))
1163 if ($entity->canMarked())
1165 $markedField = $entity->getMarkField();
1166 $entity->setField($markedField,
'N');
1174 if (empty($markList) && !static::hasErrors($order))
1176 $shipmentCollection = $order->getShipmentCollection();
1177 if ($shipmentCollection->isMarked())
1180 foreach ($shipmentCollection as $shipment)
1182 if ($shipment->isMarked())
1184 $shipment->setField(
'MARKED',
'N');
1189 $paymentCollection = $order->getPaymentCollection();
1190 if ($paymentCollection->isMarked())
1193 foreach ($paymentCollection as $payment)
1195 if ($payment->isMarked())
1197 $payment->setField(
'MARKED',
'N');
1202 $order->setField(
'MARKED',
'N');
static addAction($entityName, $orderId, $type, $id=null, $entity=null, array $fields=array(), $level=null)