114 public static function setLinks($entityId, $entityType, array $linkedIds = array(), $actualizeRestrictions =
true)
116 if(intval($entityId) <= 0)
119 if($entityType != self::ENTITY_TYPE_DELIVERY && $entityType != self::ENTITY_TYPE_PAYSYSTEM)
122 $result = new \Bitrix\Main\Entity\Result();
123 $con = \Bitrix\Main\Application::getConnection();
124 $sqlHelper = $con->getSqlHelper();
125 $entityId = $sqlHelper->forSql($entityId);
126 $reverseParentLinks = array();
128 if($entityType == self::ENTITY_TYPE_DELIVERY)
142 if(!empty($linkedIds))
146 foreach($linkedIds as $id)
147 if(isset($reverseFieldsList[$id][
'PARENT_ID']) && self::isValidParent($reverseFieldsList[$id][
'PARENT_ID'], $reverseEntityType))
148 if(!in_array($reverseFieldsList[$id][
'PARENT_ID'], $linkedIds))
149 $reverseParentLinks[] = $reverseFieldsList[$id][
'PARENT_ID'];
151 if(!empty($reverseParentLinks))
153 $linkedIds = array_unique(array_merge($linkedIds, $reverseParentLinks));
160 "DELETE FROM ".self::getTableName().
161 " WHERE ".$entityType.
"=".$entityId .
" AND LINK_DIRECTION='".$linkDirection.
"'"
165 if(!empty($linkedIds))
170 '=LINK_DIRECTION' => $reverseLinkDirection,
174 if(!empty($linkedIds))
175 $glParams[
'filter'][$reverseEntityType] = $linkedIds;
177 $res = self::getList($glParams);
179 $linkedToEntity = array();
180 $linkedToOther = array();
182 while($rec = $res->fetch())
184 if($rec[$entityType] == $entityId)
186 if(!in_array($rec[$reverseEntityType], $linkedToEntity))
187 $linkedToEntity[] = $rec[$reverseEntityType];
191 if(!in_array($rec[$reverseEntityType], $linkedToOther))
192 $linkedToOther[] = $rec[$reverseEntityType];
196 $reverseIdsToAdd = array_diff($linkedToOther, $linkedToEntity);
199 if(!empty($reverseIdsToAdd))
200 self::insertLinks($entityId, $reverseLinkDirection, $entityType, $reverseIdsToAdd);
205 '=LINK_DIRECTION' => $reverseLinkDirection,
206 '='.$entityType => $entityId,
210 if(!empty($linkedIds))
211 $glParams[
'filter'][
'!='.$reverseEntityType] = $linkedIds;
213 $res = self::getList($glParams);
215 while($rec = $res->fetch())
218 'DELIVERY_ID' => $rec[
'DELIVERY_ID'],
219 'PAYSYSTEM_ID' => $rec[
'PAYSYSTEM_ID'],
220 'LINK_DIRECTION' => $rec[
'LINK_DIRECTION']
224 self::$unLinked =
null;
227 if(!empty($linkedIds))
230 if($entityType == self::ENTITY_TYPE_DELIVERY && self::isValidParent($parentId, $entityType) && !in_array($parentId, $unlinked))
232 $parentLinks =
self::getLinks($parentId, $entityType, array(),
false);
237 array_unique(array_merge($parentLinks, $linkedIds)),
241 elseif($entityType == self::ENTITY_TYPE_PAYSYSTEM)
246 $entityList = array_diff($entityList, array($entityId));
248 foreach($reverseFieldsList as $id => $fields)
250 if(intval($fields[
'PARENT_ID']) > 0 && in_array($fields[
'PARENT_ID'], $reverseParentLinks) && in_array($id, $unlinkedReverse))
263 if($actualizeRestrictions)
295 $con = \Bitrix\Main\Application::getConnection();
296 $sqlHelper = $con->getSqlHelper();
298 $restrictions = array();
299 $dbR = \Bitrix\Sale\Internals\ServiceRestrictionTable::getList(array(
301 '=CLASS_NAME' =>
'\Bitrix\Sale\Delivery\Restrictions\ByPaySystem'
303 'select' => array(
'SERVICE_ID')
306 while($restr = $dbR->fetch())
307 $restrictions[] = $restr[
'SERVICE_ID'];
310 $dLinkedToP = array();
311 $deliveriesToPs = array();
314 $dbP2S = DeliveryPaySystemTable::getList();
316 while($d2p = $dbP2S->fetch())
318 if($d2p[
"LINK_DIRECTION"] == self::LINK_DIRECTION_DELIVERY_PAYSYSTEM && !in_array($d2p[
"DELIVERY_ID"], $dLinkedToP))
319 $dLinkedToP[] = $d2p[
"DELIVERY_ID"];
321 if($d2p[
"LINK_DIRECTION"] == self::LINK_DIRECTION_PAYSYSTEM_DELIVERY )
323 if(!isset($deliveriesToPs[$d2p[
"DELIVERY_ID"]]))
324 $deliveriesToPs[$d2p[
"DELIVERY_ID"]] = array();
326 $linkedPS[] = $d2p[
"PAYSYSTEM_ID"];
327 $deliveriesToPs[$d2p[
"DELIVERY_ID"]][] = $d2p[
"PAYSYSTEM_ID"];
331 $notLinkedToPS = array_diff($deliveryList, $dLinkedToP);
332 $existLinkedPs = !empty($linkedPS);
333 $notNeedRestriction = array();
334 $needRestriction = array();
336 foreach($deliveryList as $id)
341 if(in_array($id, $notLinkedToPS))
343 if(isset($deliveriesToPs[$id]))
344 $diff = array_diff($linkedPS, $deliveriesToPs[$id]);
348 if(!$existLinkedPs || empty($diff))
350 $notNeedRestriction[] = $id;
357 $needRestriction[] = $id;
360 $notNeedRestriction = array_intersect($notNeedRestriction, $restrictions);
362 if(!empty($notNeedRestriction))
366 foreach($notNeedRestriction as $deliveryId)
367 $sql .=
" ".($sql ==
"" ?
"WHERE CLASS_NAME='".$sqlHelper->forSql(
'\Bitrix\Sale\Delivery\Restrictions\ByPaySystem').
"' AND (" :
"OR " ).
"SERVICE_ID=".$sqlHelper->forSql($deliveryId).
" AND SERVICE_TYPE=".Restrictions\Manager::SERVICE_TYPE_SHIPMENT;
369 $sql =
"DELETE FROM ".\Bitrix\Sale\Internals\ServiceRestrictionTable::getTableName().$sql.
")";
370 $con->queryExecute($sql);
373 $needRestriction = array_diff($needRestriction, $restrictions);
376 if(!empty($needRestriction))
380 foreach($needRestriction as $deliveryId)
381 $sql .= ($sql ==
"" ?
" " :
", ").
"(".$sqlHelper->forSql($deliveryId).
", '".$sqlHelper->forSql(
'\Bitrix\Sale\Delivery\Restrictions\ByPaySystem').
"', ".Restrictions\Manager::SERVICE_TYPE_SHIPMENT.
")";
383 $sql =
"INSERT INTO ".\Bitrix\Sale\Internals\ServiceRestrictionTable::getTableName().
"(SERVICE_ID, CLASS_NAME, SERVICE_TYPE) VALUES".$sql;
384 $con->queryExecute($sql);
390 $con = \Bitrix\Main\Application::getConnection();
391 $sqlHelper = $con->getSqlHelper();
393 $restrictions = array();
394 $dbR = \Bitrix\Sale\Internals\ServiceRestrictionTable::getList(array(
398 'select' => array(
'SERVICE_ID')
401 while($restr = $dbR->fetch())
402 $restrictions[] = $restr[
'SERVICE_ID'];
405 $dLinkedToP = array();
406 $deliveriesToPs = array();
409 $dbP2S = DeliveryPaySystemTable::getList();
411 while($d2p = $dbP2S->fetch())
413 if($d2p[
"LINK_DIRECTION"] == self::LINK_DIRECTION_PAYSYSTEM_DELIVERY && !in_array($d2p[
"PAYSYSTEM_ID"], $dLinkedToP))
414 $dLinkedToP[] = $d2p[
"PAYSYSTEM_ID"];
416 if($d2p[
"LINK_DIRECTION"] == self::LINK_DIRECTION_DELIVERY_PAYSYSTEM)
418 if(!isset($deliveriesToPs[$d2p[
"PAYSYSTEM_ID"]]))
419 $deliveriesToPs[$d2p[
"PAYSYSTEM_ID"]] = array();
421 $linkedPS[] = $d2p[
"DELIVERY_ID"];
422 $deliveriesToPs[$d2p[
"PAYSYSTEM_ID"]][] = $d2p[
"DELIVERY_ID"];
426 $notLinkedToPS = array_diff($deliveryList, $dLinkedToP);
427 $existLinkedPs = !empty($linkedPS);
428 $notNeedRestriction = array();
429 $needRestriction = array();
431 foreach($deliveryList as $id)
436 if(in_array($id, $notLinkedToPS))
438 if(isset($deliveriesToPs[$id]))
439 $diff = array_diff($linkedPS, $deliveriesToPs[$id]);
443 if(!$existLinkedPs || empty($diff))
445 $notNeedRestriction[] = $id;
452 $needRestriction[] = $id;
455 $notNeedRestriction = array_intersect($notNeedRestriction, $restrictions);
457 if(!empty($notNeedRestriction))
461 foreach($notNeedRestriction as $deliveryId)
462 $sql .=
" ".($sql ==
"" ?
"WHERE CLASS_NAME='".$sqlHelper->forSql(
'\Bitrix\Sale\Services\PaySystem\Restrictions\Delivery').
"' AND (" :
"OR " ).
"SERVICE_ID=".$sqlHelper->forSql($deliveryId).
" AND SERVICE_TYPE=".Manager::SERVICE_TYPE_PAYMENT;
464 $sql =
"DELETE FROM ".\Bitrix\Sale\Internals\ServiceRestrictionTable::getTableName().$sql.
")";
465 $con->queryExecute($sql);
468 $needRestriction = array_diff($needRestriction, $restrictions);
471 if(!empty($needRestriction))
475 foreach($needRestriction as $deliveryId)
476 $sql .= ($sql ==
"" ?
" " :
", ").
"(".$sqlHelper->forSql($deliveryId).
", '".$sqlHelper->forSql(
'\\'.\
Bitrix\
Sale\Services\PaySystem\Restrictions\
Delivery::class).
"', ".Manager::SERVICE_TYPE_PAYMENT.
")";
478 $sql =
"INSERT INTO ".\Bitrix\Sale\Internals\ServiceRestrictionTable::getTableName().
"(SERVICE_ID, CLASS_NAME, SERVICE_TYPE) VALUES".$sql;
479 $con->queryExecute($sql);
483 protected static function insertLinks($entityId, $linkDirection, $entityType, $linkedIds)
485 $con = \Bitrix\Main\Application::getConnection();
486 $sqlHelper = $con->getSqlHelper();
487 $entityId = (int)$entityId;
488 $linkDirection = $sqlHelper->forSql($linkDirection);
490 $sql =
"INSERT INTO ".
492 "(DELIVERY_ID, PAYSYSTEM_ID, LINK_DIRECTION) ".
497 foreach($linkedIds as $id)
506 if($entityType == self::ENTITY_TYPE_DELIVERY)
507 $sql .=
" (".$entityId.
", ".$id;
509 $sql .=
" (".$id.
", ".$entityId;
511 $sql .=
", '".$linkDirection.
"')";
514 $con->queryExecute($sql);
525 public static function getLinks($entityId, $entityType, array $preparedData = array(), $considerParent =
true)
529 if(intval($entityId) <= 0)
532 if($entityType != self::ENTITY_TYPE_DELIVERY && $entityType != self::ENTITY_TYPE_PAYSYSTEM)
535 if($entityType == self::ENTITY_TYPE_DELIVERY)
550 if(isset($preparedData[$entityId][
"DIRECT"]))
552 $result = $preparedData[$entityId][
"DIRECT"];
558 "=".$entityType => $entityId,
559 "=LINK_DIRECTION" => $linkDirection
561 'select' => array($reverseEntityType)
564 $res = self::getList($glParams);
566 while($rec = $res->fetch())
567 $result[] = $rec[$reverseEntityType];
575 if($entityType == self::ENTITY_TYPE_PAYSYSTEM)
580 elseif($entityType == self::ENTITY_TYPE_DELIVERY && $parentId > 0)
589 if(isset($preparedData[$entityId][
"REVERSE"]))
591 $result = $preparedData[$entityId][
"REVERSE"];
597 "=".$entityType => $entityId,
598 "=LINK_DIRECTION" => $reverseLinkDirection
600 'select' => array($reverseEntityType)
603 $res = self::getList($glParams);
605 while($rec = $res->fetch())
606 $result[] = $rec[$reverseEntityType];
609 $result = array_merge($result, self::getUnlinkedEnityItems($reverseEntityType, $reverseLinkDirection));
613 if($entityType == self::ENTITY_TYPE_DELIVERY && $parentId > 0)
617 elseif($entityType == self::ENTITY_TYPE_PAYSYSTEM && !empty($result))
677 if(intval($parentId) <= 0)
678 return $profilePsIds;
680 $result = $profilePsIds;
682 $parentPSIds =
self::getLinks($parentId, self::ENTITY_TYPE_DELIVERY, $preparedData);
684 if(!in_array($parentId, $unlinkedIds) && in_array($profileId, $unlinkedIds))
685 $result = $parentPSIds;
686 elseif(!in_array($parentId, $unlinkedIds) && !in_array($profileId, $unlinkedIds))
687 $result = array_intersect($profilePsIds, $parentPSIds);
711 if($entityType != self::ENTITY_TYPE_DELIVERY && $entityType != self::ENTITY_TYPE_PAYSYSTEM)
714 if($linkDirection !=
null)
715 if($linkDirection != self::LINK_DIRECTION_DELIVERY_PAYSYSTEM && $linkDirection != self::LINK_DIRECTION_PAYSYSTEM_DELIVERY)
718 if(!isset(self::$unLinked[$entityType]))
720 $entityList = array_flip(self::getEntityItemsFullList($entityType));
722 self::$unLinked[$entityType] = array(
723 self::LINK_DIRECTION_DELIVERY_PAYSYSTEM => $entityList,
724 self::LINK_DIRECTION_PAYSYSTEM_DELIVERY => $entityList
728 'group' => array($entityType,
'LINK_DIRECTION'),
729 'select' => array($entityType,
'LINK_DIRECTION')
732 $res = DeliveryPaySystemTable::getList($glParams);
734 while($row = $res->fetch())
736 if(isset(self::$unLinked[$entityType][$row[
'LINK_DIRECTION']][$row[$entityType]]))
738 unset(self::$unLinked[$entityType][$row[
'LINK_DIRECTION']][$row[$entityType]]);
746 if($linkDirection ==
null)
748 $result = array_intersect(self::$unLinked[$entityType][self::LINK_DIRECTION_DELIVERY_PAYSYSTEM], self::$unLinked[$entityType][self::LINK_DIRECTION_PAYSYSTEM_DELIVERY]);
752 $result = self::$unLinked[$entityType][$linkDirection];
766 if(isset(self::$entityItemsFullList[$entityType]))
767 return self::$entityItemsFullList[$entityType];
769 self::$entityItemsFullList[$entityType] = array();
771 if($entityType == self::ENTITY_TYPE_DELIVERY)
773 \Bitrix\Sale\Delivery\Services\Manager::getActiveList();
774 $res = Services\Table::getList(array(
778 'select' => array(
'*',
'PARENT_CLASS_NAME' =>
'PARENT.CLASS_NAME')
781 while($dsrv = $res->fetch())
783 $obj = Services\Manager::createObject($dsrv);
785 if ($obj && $obj->canHasChildren())
788 self::$entityItemsFullList[$entityType][] = $dsrv[
"ID"];
789 self::$entityItemsFieldsList[$entityType][$dsrv[
"ID"]] = $dsrv;
794 $dbRes = PaySystem\Manager::getList(array(
795 'filter' => array(
"ACTIVE" =>
"Y"),
796 'select' => array(
"ID")
799 while($ps = $dbRes->fetch())
801 self::$entityItemsFullList[$entityType][] = $ps[
"ID"];
802 self::$entityItemsFieldsList[$entityType][$ps[
"ID"]] = $ps;
806 return self::$entityItemsFullList[$entityType];
811 static $preparedData = array();
813 if(!isset($preparedData[$entityType]))
814 $preparedData[$entityType] = array();
816 if($entityType == self::ENTITY_TYPE_DELIVERY)
829 if(empty($entityIds))
832 $arrdif = array_diff($entityIds, array_keys($preparedData[$entityType]));
834 if(is_array($arrdif) && empty($arrdif))
835 return array_intersect_key($preparedData[$entityType], $entityIds);
839 "=".$entityType => $arrdif
843 $res = DeliveryPaySystemTable::getList($glParams);
845 foreach($arrdif as $id)
847 $preparedData[$entityType][$id] = array(
853 while($rec = $res->fetch())
855 if($rec[
"LINK_DIRECTION"] == $linkDirection)
856 $preparedData[$entityType][$rec[$entityType]][
"DIRECT"][] = $rec[$reverseEntityType];
857 elseif($rec[
"LINK_DIRECTION"] == $reverseLinkDirection)
858 $preparedData[$entityType][$rec[$entityType]][
"REVERSE"][] = $rec[$reverseEntityType];
861 return $preparedData[$entityType];