Example #1
0
 function ReserveOrder($ID, $val)
 {
     global $APPLICATION;
     $ID = IntVal($ID);
     $val = $val != "Y" ? "N" : "Y";
     $errorMessage = "";
     $isOrderConverted = \Bitrix\Main\Config\Option::get("main", "~sale_converted_15", 'N');
     if ($ID <= 0) {
         $APPLICATION->ThrowException(Loc::getMessage("SKGO_NO_ORDER_ID"), "NO_ORDER_ID");
         return false;
     }
     $arOrder = CSaleOrder::GetByID($ID);
     if (!$arOrder) {
         $APPLICATION->ThrowException(str_replace("#ID#", $ID, Loc::getMessage("SKGO_NO_ORDER")), "NO_ORDER");
         return false;
     }
     if ($arOrder["RESERVED"] == $val) {
         $APPLICATION->ThrowException(str_replace("#ID#", $ID, Loc::getMessage("SKGO_DUB_RESERVATION")), "ALREADY_FLAG");
         return false;
     }
     foreach (GetModuleEvents("sale", "OnSaleBeforeReserveOrder", true) as $arEvent) {
         if (ExecuteModuleEventEx($arEvent, array($ID, $val)) === false) {
             return false;
         }
     }
     unset($GLOBALS["SALE_ORDER"]["SALE_ORDER_CACHE_" . $ID]);
     if ($isOrderConverted == "Y") {
         /** @var \Bitrix\Sale\Result $r */
         $r = \Bitrix\Sale\Compatible\OrderCompatibility::reserve($ID, $val);
         if (!$r->isSuccess(true)) {
             foreach ($r->getErrorMessages() as $error) {
                 $errorMessage .= " " . $error;
             }
             $APPLICATION->ThrowException(Loc::getMessage("SKGB_RESERVE_ERROR", array("#MESSAGE#" => $errorMessage)), "RESERVATION_ERROR");
             return false;
         }
         $res = true;
     } else {
         $res = CSaleOrder::Update($ID, array("RESERVED" => $val), false);
         $arRes = CSaleBasket::OrderReservation($ID, $val == "N" ? true : false);
         if (array_key_exists("ERROR", $arRes)) {
             foreach ($arRes["ERROR"] as $arError) {
                 $errorMessage .= " " . $arError["MESSAGE"];
             }
             CSaleOrder::SetMark($ID, Loc::getMessage("SKGB_RESERVE_ERROR", array("#MESSAGE#" => $errorMessage)));
             $APPLICATION->ThrowException(Loc::getMessage("SKGB_RESERVE_ERROR", array("#MESSAGE#" => $errorMessage)), "RESERVATION_ERROR");
             return false;
         } else {
             if ($arOrder["MARKED"] == "Y") {
                 CSaleOrder::UnsetMark($ID);
             }
         }
     }
     foreach (GetModuleEvents("sale", "OnSaleReserveOrder", true) as $arEvent) {
         ExecuteModuleEventEx($arEvent, array($ID, $val));
     }
     return $res;
 }