function GetBySID($SID, $SITE_ID = false) { global $DB; if (!defined('SALE_DH_INITIALIZED')) { CSaleDeliveryHandler::Initialize(); } $arHandlersList = CSaleDeliveryHandler::__getRegisteredHandlers(); $cnt = count($arHandlersList); $arResult = array(); for ($i = 0; $i < $cnt; $i++) { if ($arHandlersList[$i]["SID"] == $SID) { $arResult[] = $arHandlersList[$i]; break; } } if (count($arResult) > 0) { $arResult = CSaleDeliveryHandler::__getHandlersData($arResult, $SITE_ID); } $dbResult = new CDBResult(); reset($arResult); $dbResult->InitFromArray($arResult); return $dbResult; }
function GetBySID($SID, $SITE_ID = false) { if (!defined('SALE_DH_INITIALIZED')) CSaleDeliveryHandler::Initialize(); static $cache = array(); if (!isset($cache[$SITE_ID])) $cache[$SITE_ID] = array(); if (!isset($cache[$SITE_ID][$SID])) { $cache[$SITE_ID][$SID] = array(); $arHandlersList = CSaleDeliveryHandler::__getRegisteredHandlers(); foreach ($arHandlersList as $handler) { if ($handler["SID"] == $SID) { $cache[$SITE_ID][$SID][] = $handler; break; } } if ($cache[$SITE_ID][$SID]) { $cache[$SITE_ID][$SID] = CSaleDeliveryHandler::__getHandlersData($cache[$SITE_ID][$SID], $SITE_ID); } } $dbResult = new CDBResult(); $dbResult->InitFromArray($cache[$SITE_ID][$SID]); return $dbResult; }
/** * @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; }
/** * @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; }