Пример #1
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;
 }
Пример #2
0
 function Calculate($STEP, $SID, $profile, $arOrder, $currency, $TMP = false, $SITE_ID = false)
 {
     global $APPLICATION;
     if (!defined('SALE_DH_INITIALIZED')) {
         CSaleDeliveryHandler::Initialize();
     }
     if (!$SITE_ID) {
         $SITE_ID = SITE_ID;
     }
     $rsDeliveryHandler = CSaleDeliveryHandler::GetBySID($SID, $SITE_ID);
     if (!($arHandler = $rsDeliveryHandler->Fetch())) {
         return array("RESULT" => "ERROR", "TEXT" => GetMessage("SALE_DH_ERROR_HANDLER_NOT_INSTALLED"));
     }
     if (is_callable($arHandler["CALCULATOR"])) {
         $arConfig = $arHandler["CONFIG"]["CONFIG"];
         $arOrder["PRICE"] = CCurrencyRates::ConvertCurrency($arOrder["PRICE"], $currency, $arHandler["BASE_CURRENCY"]);
         if ($res = call_user_func($arHandler["CALCULATOR"], $profile, $arConfig, $arOrder, $STEP, $TMP)) {
             if (is_array($res)) {
                 $arReturn = $res;
             } elseif (is_numeric($res)) {
                 $arReturn = array("RESULT" => "OK", "VALUE" => doubleval($res));
             }
         } else {
             if ($ex = $APPLICATION->GetException()) {
                 return array("RESULT" => "ERROR", "TEXT" => $ex->GetString());
             } else {
                 return array("RESULT" => "OK", "VALUE" => 0);
             }
         }
         if (is_array($arReturn) && $arReturn["RESULT"] == "OK" && $currency != $arHandler["BASE_CURRENCY"] && CModule::IncludeModule('currency')) {
             $arReturn["VALUE"] = CCurrencyRates::ConvertCurrency($arReturn["VALUE"], $arHandler["BASE_CURRENCY"], $currency);
         }
         $arReturn["VALUE"] *= 1 + $arHandler["TAX_RATE"] / 100;
         $arReturn = CSaleDeliveryHandler::__executeCalculateEvents($SID, $profile, $arOrder, $arReturn);
         return $arReturn;
     } else {
         return array("RESULT" => "ERROR", "TEXT" => GetMessage("SALE_DH_ERROR_WRONG_HANDLER_FILE"));
     }
 }
Пример #3
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;
 }