예제 #1
0
 public static function GetList($arFilter = array(), $arGroupBy = false, $arSelectFields = array())
 {
     $params = array();
     if (is_array($arFilter) && !empty($arFilter)) {
         if (isset($arFilter["DELIVERY_ID"]) || $arFilter["DELIVERY_PROFILE_ID"]) {
             $ids = self::convertDeliveryIds(isset($arFilter["DELIVERY_ID"]) ? $arFilter["DELIVERY_ID"] : array(), isset($arFilter["DELIVERY_PROFILE_ID"]) ? $arFilter["DELIVERY_PROFILE_ID"] : array());
             if (!empty($ids)) {
                 $arFilter["=DELIVERY_ID"] = $ids;
             }
             unset($arFilter["DELIVERY_ID"]);
             unset($arFilter["DELIVERY_PROFILE_ID"]);
         }
         if (isset($arFilter["PAYSYSTEM_ID"])) {
             $arFilter["=PAYSYSTEM_ID"] = $arFilter["PAYSYSTEM_ID"];
             unset($arFilter["PAYSYSTEM_ID"]);
         }
         $params['filter'] = $arFilter;
     }
     //todo:
     if (is_array($arGroupBy) && !empty($arGroupBy)) {
         $params['group'] = array_intersect($arGroupBy, array("DELIVERY_ID", "PAYSYSTEM_ID"));
     }
     $params["select"] = array("DELIVERY_ID", "PAYSYSTEM_ID", "DELIVERY_SERVICE_CODE" => "DELIVERY_SERVICE.CODE");
     $params["runtime"] = array(new \Bitrix\Main\Entity\ReferenceField('DELIVERY_SERVICE', 'Bitrix\\Sale\\Delivery\\Services\\Table', array('=this.DELIVERY_ID' => 'ref.ID')));
     $records = array();
     $res = DeliveryPaySystemTable::getList($params);
     while ($record = $res->fetch()) {
         $delivery = CSaleDeliveryHelper::getDeliverySIDAndProfile($record["DELIVERY_SERVICE_CODE"]);
         $record["DELIVERY_ID"] = $delivery["SID"];
         $record["DELIVERY_PROFILE_ID"] = isset($delivery["PROFILE"]) ? $delivery["PROFILE"] : null;
         unset($record["DELIVERY_SERVICE_CODE"]);
         $records[] = $record;
     }
     $result = new \CDBResult();
     $result->InitFromArray($records);
     return $result;
 }
예제 #2
0
 /**
  * @return \Bitrix\Sale\Result|bool
  * @throws Exception
  * @throws \Bitrix\Main\SystemException
  */
 public static function convertToNew($renameTable = false)
 {
     $result = new \Bitrix\Sale\Result();
     $con = \Bitrix\Main\Application::getConnection();
     if (!$con->isTableExists("b_sale_delivery_handler")) {
         return true;
     }
     $sqlHelper = $con->getSqlHelper();
     $deliveryRes = $con->query('SELECT * FROM b_sale_delivery_handler WHERE CONVERTED != \'Y\'');
     $tablesToUpdate = array('b_sale_order', 'b_sale_order_history');
     \CSaleDeliveryHandler::Initialize();
     $handlers = \CSaleDeliveryHandler::__getRegisteredHandlers();
     while ($delivery = $deliveryRes->fetch()) {
         if (strlen($delivery["PROFILES"]) > 0) {
             $delivery["PROFILES"] = unserialize($delivery["PROFILES"]);
         } else {
             $delivery["PROFILES"] = $handlers[$delivery["HID"]]["PROFILES"];
             foreach ($delivery["PROFILES"] as $id => $params) {
                 $delivery["PROFILES"][$id]["ACTIVE"] = $delivery["ACTIVE"];
             }
         }
         // Something strange it probably not used
         if ($delivery["PROFILES"] == false || !is_array($delivery["PROFILES"]) || empty($delivery["PROFILES"])) {
             $result->addError(new \Bitrix\Main\Entity\EntityError("Can't receive info about profiles. Delivery HID: \"" . $delivery["HID"] . "\""));
             continue;
         }
         unset($delivery["ID"]);
         $delivery["CONFIG"] = array();
         if (strlen($delivery["SETTINGS"]) > 0) {
             if (isset($handlers[$delivery["HID"]]["DBGETSETTINGS"]) && is_callable($handlers[$delivery["HID"]]["DBGETSETTINGS"])) {
                 $delivery["CONFIG"] = call_user_func($handlers[$delivery["HID"]]["DBGETSETTINGS"], $delivery["SETTINGS"]);
             } else {
                 $delivery["CONFIG"] = $delivery["SETTINGS"];
             }
         } elseif (is_callable($handlers[$delivery["HID"]]["GETCONFIG"])) {
             $config = call_user_func($handlers[$delivery["HID"]]["GETCONFIG"], strlen($delivery["LID"]) > 0 ? $delivery["LID"] : false);
             foreach ($config["CONFIG"] as $key => $arConfig) {
                 if (!empty($arConfig["DEFAULT"])) {
                     $delivery["CONFIG"][$key] = $arConfig["DEFAULT"];
                 }
             }
         }
         $delivery["SID"] = $handlers[$delivery["HID"]]["SID"];
         $id = \CSaleDeliveryHandler::Set($delivery["HID"], $delivery, strlen($delivery["LID"]) > 0 ? $delivery["LID"] : false);
         if (intval($id) <= 0) {
             $result->addError(new \Bitrix\Main\Entity\EntityError("Can't convert delivery handler with hid: " . $delivery["HID"] . (strlen($delivery["LID"]) > 0 ? " for site: " . $delivery["LID"] : "")));
             continue;
         }
         $con->queryExecute('UPDATE b_sale_delivery_handler SET CONVERTED="Y" WHERE HID="' . $sqlHelper->forSql($delivery["HID"]) . '"');
         $ids = array($id);
         foreach ($delivery["PROFILES"] as $profileName => $profileData) {
             $fullSid = $delivery["HID"] . ":" . $profileName;
             $profileId = \Bitrix\Sale\Delivery\Services\Table::getIdByCode($fullSid);
             $ids[] = $profileId;
             if (intval($profileId) > 0) {
                 foreach ($tablesToUpdate as $table) {
                     $con->queryExecute("UPDATE " . $table . " SET DELIVERY_ID=" . $sqlHelper->forSql($profileId) . " WHERE DELIVERY_ID = '" . $sqlHelper->forSql($fullSid) . "'");
                 }
                 $con->queryExecute("UPDATE b_sale_delivery2paysystem SET DELIVERY_ID=" . $sqlHelper->forSql($profileId) . ", DELIVERY_PROFILE_ID='' WHERE DELIVERY_ID = '" . $sqlHelper->forSql($delivery["HID"]) . "' AND DELIVERY_PROFILE_ID='" . $profileName . "'");
             } else {
                 $result->addError(new \Bitrix\Main\Entity\EntityError("Cant determine id for profile code: " . $fullSid));
             }
         }
         $con->queryExecute("UPDATE b_sale_delivery2paysystem SET DELIVERY_ID=" . $sqlHelper->forSql($id) . ", DELIVERY_PROFILE_ID='' WHERE DELIVERY_ID = '" . $sqlHelper->forSql($delivery["HID"]) . "' AND (DELIVERY_PROFILE_ID='' OR DELIVERY_PROFILE_ID IS NULL)");
         $d2pRes = \Bitrix\Sale\Internals\DeliveryPaySystemTable::getList(array('filter' => array('DELIVERY_ID' => $ids), 'select' => array("DELIVERY_ID"), 'group' => array("DELIVERY_ID")));
         while ($d2p = $d2pRes->fetch()) {
             $res = \Bitrix\Sale\Delivery\Restrictions\Table::add(array("DELIVERY_ID" => $d2p["DELIVERY_ID"], "CLASS_NAME" => '\\Bitrix\\Sale\\Delivery\\Restrictions\\ByPaySystem', "SORT" => 100));
             if (!$res->isSuccess()) {
                 $result->addErrors($res->getErrors());
             }
         }
     }
     if ($renameTable && $result->isSuccess()) {
         $con->queryExecute("ALTER TABLE b_sale_delivery_handler RENAME b_sale_delivery_handler_old");
     }
     return $result;
 }
예제 #3
0
 /**
  * @return \Bitrix\Sale\Result
  * @throws Exception
  * @throws \Bitrix\Main\ArgumentException
  * @internal
  */
 public static function convertPSRelations()
 {
     $result = new \Bitrix\Sale\Result();
     $con = \Bitrix\Main\Application::getConnection();
     if (!$con->isTableExists("b_sale_delivery2paysystem")) {
         return $result;
     }
     $query = new \Bitrix\Main\Entity\Query(DeliveryPaySystemTable::getEntity());
     $query->setSelect(array('DELIVERY_ID'));
     $query->addFilter('LINK_DIRECTION', NULL);
     $query->setLimit(1);
     $res = $query->exec();
     if (!$res->fetch()) {
         return $result;
     }
     $con->queryExecute('UPDATE b_sale_delivery2paysystem SET LINK_DIRECTION=\'' . DeliveryPaySystemTable::LINK_DIRECTION_DELIVERY_PAYSYSTEM . '\'');
     $res = DeliveryPaySystemTable::getList(array());
     while ($rec = $res->fetch()) {
         unset($rec["ID"]);
         $rec["LINK_DIRECTION"] = DeliveryPaySystemTable::LINK_DIRECTION_PAYSYSTEM_DELIVERY;
         DeliveryPaySystemTable::Add($rec);
     }
     return $result;
 }
예제 #4
0
 /**
  * @return \Bitrix\Sale\Result
  * @throws Exception
  * @throws \Bitrix\Main\SystemException
  */
 public static function convertToNew($renameTable = false)
 {
     $result = new \Bitrix\Sale\Result();
     $con = \Bitrix\Main\Application::getConnection();
     if (!$con->isTableExists("b_sale_delivery_handler")) {
         return $result;
     }
     $sqlHelper = $con->getSqlHelper();
     $deliveryRes = $con->query('SELECT * FROM b_sale_delivery_handler WHERE CONVERTED != \'Y\'');
     $tablesToUpdate = array('b_sale_order', 'b_sale_order_history');
     \CSaleDeliveryHandler::Initialize();
     $handlers = \CSaleDeliveryHandler::__getRegisteredHandlers();
     while ($delivery = $deliveryRes->fetch()) {
         if (strlen($delivery["HID"]) <= 0) {
             $result->addError(new \Bitrix\Main\Entity\EntityError("Can't find delivery HID. ID: \"" . $delivery["ID"] . "\""));
             continue;
         }
         if (!isset($handlers[$delivery["HID"]])) {
             \CEventLog::Add(array("SEVERITY" => "ERROR", "AUDIT_TYPE_ID" => "SALE_CONVERTER_ERROR", "MODULE_ID" => "sale", "ITEM_ID" => "CAllSaleDeliveryHandler::convertToNew()", "DESCRIPTION" => "Can't find delivery handler for registered HID: \"" . $delivery["HID"] . "\""));
             //$result->addError( new \Bitrix\Main\Entity\EntityError("Can't find delivery handler for registered HID: \"".$delivery["HID"]."\""));
             continue;
         }
         if (strlen($delivery["PROFILES"]) > 0) {
             //get from base
             $delivery["PROFILES"] = unserialize($delivery["PROFILES"]);
         } else {
             //or default.
             $delivery["PROFILES"] = $handlers[$delivery["HID"]]["PROFILES"];
         }
         // Something strange it probably not used
         if ($delivery["PROFILES"] == false || !is_array($delivery["PROFILES"]) || empty($delivery["PROFILES"])) {
             $result->addError(new \Bitrix\Main\Entity\EntityError("Can't receive info about profiles. Delivery HID: \"" . $delivery["HID"] . "\""));
             continue;
         }
         //Set profiles activity
         foreach ($delivery["PROFILES"] as $id => $params) {
             if (!isset($delivery["PROFILES"][$id]["ACTIVE"]) || $delivery["ACTIVE"] == "N") {
                 $delivery["PROFILES"][$id]["ACTIVE"] = $delivery["ACTIVE"];
             }
         }
         unset($delivery["ID"]);
         $delivery["CONFIG"] = array();
         if (strlen($delivery["SETTINGS"]) > 0) {
             if (isset($handlers[$delivery["HID"]]["DBGETSETTINGS"]) && is_callable($handlers[$delivery["HID"]]["DBGETSETTINGS"])) {
                 $delivery["CONFIG"] = call_user_func($handlers[$delivery["HID"]]["DBGETSETTINGS"], $delivery["SETTINGS"]);
             } else {
                 $delivery["CONFIG"] = $delivery["SETTINGS"];
             }
         } elseif (isset($handlers[$delivery["HID"]]["GETCONFIG"]) && is_callable($handlers[$delivery["HID"]]["GETCONFIG"])) {
             $config = call_user_func($handlers[$delivery["HID"]]["GETCONFIG"], strlen($delivery["LID"]) > 0 ? $delivery["LID"] : false);
             foreach ($config["CONFIG"] as $key => $arConfig) {
                 if (!empty($arConfig["DEFAULT"])) {
                     $delivery["CONFIG"][$key] = $arConfig["DEFAULT"];
                 }
             }
         }
         if (empty($delivery["NAME"])) {
             if (!empty($handlers[$delivery["HID"]]["NAME"])) {
                 $delivery["NAME"] = $handlers[$delivery["HID"]]["NAME"];
             } else {
                 $delivery["NAME"] = "-";
             }
         }
         $delivery["SID"] = $handlers[$delivery["HID"]]["SID"];
         $id = \CSaleDeliveryHandler::Set($delivery["HID"], $delivery, strlen($delivery["LID"]) > 0 ? $delivery["LID"] : false);
         if (intval($id) <= 0) {
             $result->addError(new \Bitrix\Main\Entity\EntityError("Can't convert delivery handler with hid: " . $delivery["HID"] . (strlen($delivery["LID"]) > 0 ? " for site: " . $delivery["LID"] : "")));
             continue;
         }
         $con->queryExecute("UPDATE b_sale_delivery_handler SET CONVERTED='Y' WHERE HID LIKE '" . $sqlHelper->forSql($delivery["HID"]) . "'");
         $ids = array($id);
         foreach ($delivery["PROFILES"] as $profileName => $profileData) {
             $fullSid = $delivery["HID"] . ":" . $profileName;
             $profileId = \CSaleDelivery::getIdByCode($fullSid);
             $ids[] = $profileId;
             if (intval($profileId) > 0) {
                 foreach ($tablesToUpdate as $table) {
                     $con->queryExecute("UPDATE " . $table . " SET DELIVERY_ID='" . $sqlHelper->forSql($profileId) . "' WHERE DELIVERY_ID = '" . $sqlHelper->forSql($fullSid) . "'");
                 }
                 $con->queryExecute("UPDATE b_sale_delivery2paysystem SET DELIVERY_ID='" . $sqlHelper->forSql($profileId) . "', DELIVERY_PROFILE_ID='##CONVERTED##' WHERE DELIVERY_ID = '" . $sqlHelper->forSql($delivery["HID"]) . "' AND DELIVERY_PROFILE_ID='" . $profileName . "'");
             } else {
                 $result->addError(new \Bitrix\Main\Entity\EntityError("Cant determine id for profile code: " . $fullSid));
             }
         }
         $con->queryExecute("UPDATE b_sale_delivery2paysystem SET DELIVERY_ID='" . $sqlHelper->forSql($id) . "', DELIVERY_PROFILE_ID='##CONVERTED##' WHERE DELIVERY_ID = '" . $sqlHelper->forSql($delivery["HID"]) . "' AND (DELIVERY_PROFILE_ID='' OR DELIVERY_PROFILE_ID IS NULL)");
         $d2pRes = \Bitrix\Sale\Internals\DeliveryPaySystemTable::getList(array('filter' => array('DELIVERY_ID' => $ids), 'select' => array("DELIVERY_ID"), 'group' => array("DELIVERY_ID")));
         while ($d2p = $d2pRes->fetch()) {
             $res = \Bitrix\Sale\Internals\ServiceRestrictionTable::add(array("SERVICE_ID" => $d2p["DELIVERY_ID"], "SERVICE_TYPE" => \Bitrix\Sale\Services\Base\RestrictionManager::SERVICE_TYPE_SHIPMENT, "CLASS_NAME" => '\\Bitrix\\Sale\\Delivery\\Restrictions\\ByPaySystem', "SORT" => 100));
             if (!$res->isSuccess()) {
                 $result->addErrors($res->getErrors());
             }
         }
     }
     if ($renameTable && $result->isSuccess()) {
         $con->renameTable('b_sale_delivery_handler', 'b_sale_delivery_handler_old');
     }
     return $result;
 }
예제 #5
0
 public static function prepareData(array $entityIds, $entityType)
 {
     static $preparedData = array();
     if (!isset($preparedData[$entityType])) {
         $preparedData[$entityType] = array();
     }
     if ($entityType == self::ENTITY_TYPE_DELIVERY) {
         $linkDirection = self::LINK_DIRECTION_DELIVERY_PAYSYSTEM;
         $reverseLinkDirection = self::LINK_DIRECTION_PAYSYSTEM_DELIVERY;
         $reverseEntityType = self::ENTITY_TYPE_PAYSYSTEM;
     } else {
         $linkDirection = self::LINK_DIRECTION_PAYSYSTEM_DELIVERY;
         $reverseLinkDirection = self::LINK_DIRECTION_DELIVERY_PAYSYSTEM;
         $reverseEntityType = self::ENTITY_TYPE_DELIVERY;
     }
     if (empty($entityIds)) {
         $entityIds = self::getEntityItemsFullList($entityType);
     }
     $arrdif = array_diff($entityIds, array_keys($preparedData[$entityType]));
     if (is_array($arrdif) && empty($arrdif)) {
         return array_intersect_key($preparedData[$entityType], $entityIds);
     }
     $glParams = array('filter' => array("=" . $entityType => $arrdif));
     $res = DeliveryPaySystemTable::getList($glParams);
     foreach ($arrdif as $id) {
         $preparedData[$entityType][$id] = array("DIRECT" => array(), "REVERSE" => array());
     }
     while ($rec = $res->fetch()) {
         if ($rec["LINK_DIRECTION"] == $linkDirection) {
             $preparedData[$entityType][$rec[$entityType]]["DIRECT"][] = $rec[$reverseEntityType];
         } elseif ($rec["LINK_DIRECTION"] == $reverseLinkDirection) {
             $preparedData[$entityType][$rec[$entityType]]["REVERSE"][] = $rec[$reverseEntityType];
         }
     }
     return array_intersect_key($preparedData[$entityType], $entityIds);
 }
 public static function GetList($arFilter = array(), $arGroupBy = false, $arSelectFields = array())
 {
     $params = array();
     if (is_array($arFilter) && !empty($arFilter)) {
         if (isset($arFilter["DELIVERY_ID"]) || $arFilter["DELIVERY_PROFILE_ID"]) {
             $ids = self::convertDeliveryIds(isset($arFilter["DELIVERY_ID"]) ? $arFilter["DELIVERY_ID"] : array(), isset($arFilter["DELIVERY_PROFILE_ID"]) ? $arFilter["DELIVERY_PROFILE_ID"] : array());
             if (!empty($ids)) {
                 $arFilter["=DELIVERY_ID"] = $ids;
             }
             unset($arFilter["DELIVERY_ID"]);
             unset($arFilter["DELIVERY_PROFILE_ID"]);
         }
         if (isset($arFilter["PAYSYSTEM_ID"])) {
             $arFilter["=PAYSYSTEM_ID"] = $arFilter["PAYSYSTEM_ID"];
             unset($arFilter["PAYSYSTEM_ID"]);
         }
         $params['filter'] = $arFilter;
     }
     //todo:
     if (is_array($arGroupBy) && !empty($arGroupBy)) {
         $params['group'] = array_intersect($arGroupBy, array("DELIVERY_ID", "PAYSYSTEM_ID"));
     }
     $params["select"] = array("DELIVERY_ID", "PAYSYSTEM_ID", "LINK_DIRECTION");
     $deliveryChildrenList = self::getDeliveryChildrenList();
     $records = array();
     $res = DeliveryPaySystemTable::getList($params);
     $restricted = array('D' => array(), 'P' => array());
     while ($record = $res->fetch()) {
         if ($record['LINK_DIRECTION'] == 'D') {
             if (!in_array($record["DELIVERY_ID"], $restricted['D'])) {
                 $restricted['D'][] = $record["DELIVERY_ID"];
             }
         } elseif ($record['LINK_DIRECTION'] == 'P') {
             if (!in_array($record["DELIVERY_ID"], $restricted['P'])) {
                 $restricted['P'][] = $record["PAYSYSTEM_ID"];
             }
         }
         $deliveryId = $record["DELIVERY_ID"];
         $linkDirection = $record["LINK_DIRECTION"];
         unset($record["LINK_DIRECTION"]);
         $deliveryCode = \CSaleDelivery::getCodeById($record["DELIVERY_ID"]);
         $delivery = CSaleDeliveryHelper::getDeliverySIDAndProfile($deliveryCode);
         $record["DELIVERY_ID"] = $delivery["SID"];
         $record["DELIVERY_PROFILE_ID"] = isset($delivery["PROFILE"]) ? $delivery["PROFILE"] : null;
         $records[] = $record;
         if (!empty($deliveryChildrenList[$deliveryId])) {
             foreach ($deliveryChildrenList[$deliveryId] as $childrenId) {
                 if ($linkDirection == 'D' && !in_array($childrenId, $restricted['D'])) {
                     $restricted['D'][] = $childrenId;
                 }
                 $deliveryCode = \CSaleDelivery::getCodeById($childrenId);
                 $delivery = CSaleDeliveryHelper::getDeliverySIDAndProfile($deliveryCode);
                 $record["DELIVERY_ID"] = $delivery["SID"];
                 $record["DELIVERY_PROFILE_ID"] = isset($delivery["PROFILE"]) ? $delivery["PROFILE"] : null;
                 $records[] = $record;
             }
         }
     }
     foreach (self::getFullDeliveryList() as $dlvId) {
         if (in_array($dlvId, $restricted['D'])) {
             continue;
         }
         $deliveryCode = \CSaleDelivery::getCodeById($dlvId);
         $delivery = CSaleDeliveryHelper::getDeliverySIDAndProfile($deliveryCode);
         $record = array("DELIVERY_ID" => $delivery["SID"], "DELIVERY_PROFILE_ID" => isset($delivery["PROFILE"]) ? $delivery["PROFILE"] : null);
         foreach (self::getFullPaySystemList() as $psId) {
             if (in_array($psId, $restricted['P'])) {
                 continue;
             }
             if (self::isRecordExists($record["DELIVERY_ID"], $record["DELIVERY_PROFILE_ID"], $psId, $records)) {
                 continue;
             }
             $record["PAYSYSTEM_ID"] = $psId;
             $records[] = $record;
         }
     }
     $result = new \CDBResult();
     $result->InitFromArray($records);
     return $result;
 }