/** * @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; }
/** * @param bool|false $renameTable * @return \Bitrix\Sale\Result * @throws Exception * @throws \Bitrix\Main\ArgumentException * @internal */ public static function convertToNew($renameTable = false) { $result = new \Bitrix\Sale\Result(); $con = \Bitrix\Main\Application::getConnection(); if (!$con->isTableExists("b_sale_delivery")) { return $result; } if (!$con->isTableExists("b_sale_delivery2location_tmp")) { $res = self::createD2LTable(); if (!$res->isSuccess()) { $result->addErrors($res->getErrors()); return $result; } $con->queryExecute(' INSERT INTO b_sale_delivery2location_tmp(DELIVERY_ID, LOCATION_CODE, LOCATION_TYPE) SELECT DELIVERY_ID, LOCATION_CODE, LOCATION_TYPE FROM b_sale_delivery2location '); $con->queryExecute('DELETE FROM b_sale_delivery2location'); } $sqlHelper = $con->getSqlHelper(); $deliveryRes = $con->query('SELECT * FROM b_sale_delivery WHERE CONVERTED != \'Y\''); while ($delivery = $deliveryRes->fetch()) { $delivery["CODE"] = $delivery["ID"]; unset($delivery["ID"]); $newId = \CSaleDelivery::Add($delivery); if (intval($newId) <= 0) { $result->addError(new \Bitrix\Main\Entity\EntityError("Can't convert old delivery id: " . $delivery["CODE"])); continue; } $res = \Bitrix\Sale\Internals\ServiceRestrictionTable::add(array("SERVICE_ID" => $newId, "SERVICE_TYPE" => \Bitrix\Sale\Services\Base\RestrictionManager::SERVICE_TYPE_SHIPMENT, "CLASS_NAME" => '\\Bitrix\\Sale\\Delivery\\Restrictions\\ByLocation', "SORT" => 100)); if (!$res->isSuccess()) { $result->addErrors($res->getErrors()); } $con->queryExecute('UPDATE b_sale_delivery SET CONVERTED=\'Y\' WHERE ID=' . $sqlHelper->forSql($delivery["CODE"])); $con->queryExecute("UPDATE b_sale_order SET DELIVERY_ID='" . $sqlHelper->forSql($newId) . "' WHERE DELIVERY_ID = '" . $sqlHelper->forSql($delivery["CODE"]) . "'"); $con->queryExecute("UPDATE b_sale_order_history SET DELIVERY_ID='" . $sqlHelper->forSql($newId) . "' WHERE DELIVERY_ID = '" . $sqlHelper->forSql($delivery["CODE"]) . "'"); $con->queryExecute("UPDATE b_sale_delivery2paysystem SET DELIVERY_ID='" . $sqlHelper->forSql($newId) . "', DELIVERY_PROFILE_ID='##CONVERTED##' WHERE DELIVERY_ID = '" . $sqlHelper->forSql($delivery["CODE"]) . "'"); $con->queryExecute(' INSERT INTO b_sale_delivery2location(DELIVERY_ID, LOCATION_CODE, LOCATION_TYPE) SELECT ' . $sqlHelper->forSql($newId) . ', LOCATION_CODE, LOCATION_TYPE FROM b_sale_delivery2location_tmp WHERE DELIVERY_ID = ' . $sqlHelper->forSql($delivery["CODE"]) . ' '); $con->queryExecute('DELETE FROM b_sale_delivery2location_tmp WHERE DELIVERY_ID = ' . $sqlHelper->forSql($delivery["CODE"])); $d2pRes = \Bitrix\Sale\Internals\DeliveryPaySystemTable::getList(array('filter' => array('DELIVERY_ID' => $newId), 'select' => array("DELIVERY_ID"), 'group' => array("DELIVERY_ID"))); if ($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 ($result->isSuccess()) { $con->dropTable('b_sale_delivery2location_tmp'); if ($renameTable) { $con->renameTable("b_sale_delivery", "b_sale_delivery_old"); } } return $result; }