Ejemplo n.º 1
0
 protected static function actualizeDeliveriesRestrictionByPS()
 {
     $con = \Bitrix\Main\Application::getConnection();
     $sqlHelper = $con->getSqlHelper();
     $restrictions = array();
     $dbR = Restrictions\Table::getList(array('filter' => array('=CLASS_NAME' => '\\Bitrix\\Sale\\Delivery\\Restrictions\\ByPaySystem'), 'select' => array('DELIVERY_ID')));
     while ($restr = $dbR->fetch()) {
         $restrictions[] = $restr['DELIVERY_ID'];
     }
     $deliveryList = self::getEntityItemsFullList(self::ENTITY_TYPE_DELIVERY);
     $dLinkedToP = array();
     $deliveriesToPs = array();
     $linkedPS = array();
     $dbP2S = DeliveryPaySystemTable::getList();
     while ($d2p = $dbP2S->fetch()) {
         if ($d2p["LINK_DIRECTION"] == self::LINK_DIRECTION_DELIVERY_PAYSYSTEM && !in_array($d2p["DELIVERY_ID"], $dLinkedToP)) {
             $dLinkedToP[] = $d2p["DELIVERY_ID"];
         }
         if ($d2p["LINK_DIRECTION"] == self::LINK_DIRECTION_PAYSYSTEM_DELIVERY) {
             if (!isset($deliveriesToPs[$d2p["DELIVERY_ID"]])) {
                 $deliveriesToPs[$d2p["DELIVERY_ID"]] = array();
             }
             $linkedPS[] = $d2p["PAYSYSTEM_ID"];
             $deliveriesToPs[$d2p["DELIVERY_ID"]][] = $d2p["PAYSYSTEM_ID"];
         }
     }
     $notLinkedToPS = array_diff($deliveryList, $dLinkedToP);
     $existLinkedPs = !empty($linkedPS);
     $notNeedRestriction = array();
     $needRestriction = array();
     foreach ($deliveryList as $id) {
         $need = true;
         //DS not linked to PS and (All PS having links linked to current DS
         if (in_array($id, $notLinkedToPS)) {
             if (isset($deliveriesToPs[$id])) {
                 $diff = array_diff($linkedPS, $deliveriesToPs[$id]);
             } else {
                 $diff = $linkedPS;
             }
             if (!$existLinkedPs || empty($diff)) {
                 $notNeedRestriction[] = $id;
                 $need = false;
             }
         }
         // DS linked to PS or exist linked PS but not linked to current DS
         if ($need) {
             $needRestriction[] = $id;
         }
     }
     $notNeedRestriction = array_intersect($notNeedRestriction, $restrictions);
     if (!empty($notNeedRestriction)) {
         $sql = "";
         foreach ($notNeedRestriction as $deliveryId) {
             $sql .= " " . ($sql == "" ? "WHERE CLASS_NAME='" . $sqlHelper->forSql('\\Bitrix\\Sale\\Delivery\\Restrictions\\ByPaySystem') . "' AND (" : "OR ") . "DELIVERY_ID=" . $sqlHelper->forSql($deliveryId);
         }
         $sql = "DELETE FROM " . Restrictions\Table::getTableName() . $sql . ")";
         $con->queryExecute($sql);
     }
     $needRestriction = array_diff($needRestriction, $restrictions);
     //let's... add missing
     if (!empty($needRestriction)) {
         $sql = "";
         foreach ($needRestriction as $deliveryId) {
             $sql .= ($sql == "" ? " " : ", ") . "(" . $sqlHelper->forSql($deliveryId) . ", '" . $sqlHelper->forSql('\\Bitrix\\Sale\\Delivery\\Restrictions\\ByPaySystem') . "')";
         }
         $sql = "INSERT INTO " . Restrictions\Table::getTableName() . "(DELIVERY_ID, CLASS_NAME) VALUES" . $sql;
         $con->queryExecute($sql);
     }
 }