Exemple #1
0
 function DeliverOrder($ID, $val, $recurringID = 0, $arAdditionalFields = array())
 {
     global $DB, $USER, $APPLICATION;
     $ID = IntVal($ID);
     $val = $val != "Y" ? "N" : "Y";
     $recurringID = IntVal($recurringID);
     $isOrderConverted = \Bitrix\Main\Config\Option::get("main", "~sale_converted_15", 'N');
     $NO_CHANGE_STATUS = "N";
     if (is_set($arAdditionalFields["NOT_CHANGE_STATUS"]) && $arAdditionalFields["NOT_CHANGE_STATUS"] == "Y") {
         $NO_CHANGE_STATUS = "Y";
         unset($arAdditionalFields["NOT_CHANGE_STATUS"]);
     }
     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["ALLOW_DELIVERY"] == $val) {
         $APPLICATION->ThrowException(str_replace("#ID#", $ID, Loc::getMessage("SKGO_DUB_DELIVERY")), "ALREADY_FLAG");
         return False;
     }
     foreach (GetModuleEvents("sale", "OnSaleBeforeDeliveryOrder", true) as $arEvent) {
         if (ExecuteModuleEventEx($arEvent, array($ID, $val, $recurringID, $arAdditionalFields)) === false) {
             return false;
         }
     }
     if ($isOrderConverted != "Y") {
         $arFields = array("ALLOW_DELIVERY" => $val, "=DATE_ALLOW_DELIVERY" => $DB->GetNowFunction(), "EMP_ALLOW_DELIVERY_ID" => IntVal($USER->GetID()) > 0 ? IntVal($USER->GetID()) : false);
         if (count($arAdditionalFields) > 0) {
             foreach ($arAdditionalFields as $addKey => $addValue) {
                 if (!array_key_exists($addKey, $arFields)) {
                     $arFields[$addKey] = $addValue;
                 }
             }
         }
         $res = CSaleOrder::Update($ID, $arFields);
     } else {
         $errorMessage = '';
         /** @var \Bitrix\Sale\Result $r */
         $r = \Bitrix\Sale\Compatible\OrderCompatibility::allowDelivery($ID, $val == "Y" ? true : false);
         if (!$r->isSuccess(true)) {
             foreach ($r->getErrorMessages() as $error) {
                 $errorMessage .= " " . $error;
             }
             $APPLICATION->ThrowException(Loc::getMessage("SKGO_DELIVER_ERROR", array("#MESSAGE#" => $errorMessage)), "DELIVER_ERROR");
             return false;
         }
         $res = true;
     }
     unset($GLOBALS["SALE_ORDER"]["SALE_ORDER_CACHE_" . $ID]);
     if ($recurringID <= 0) {
         if (IntVal($arOrder["RECURRING_ID"]) > 0) {
             $recurringID = IntVal($arOrder["RECURRING_ID"]);
         }
     }
     CSaleBasket::OrderDelivery($ID, $val == "Y" ? True : False, $recurringID);
     if ($isOrderConverted != "Y") {
         foreach (GetModuleEvents("sale", "OnSaleDeliveryOrder", true) as $arEvent) {
             ExecuteModuleEventEx($arEvent, array($ID, $val));
         }
     }
     if ($val == "Y") {
         CTimeZone::Disable();
         $arOrder = CSaleOrder::GetByID($ID);
         CTimeZone::Enable();
         if ($NO_CHANGE_STATUS != "Y") {
             $orderStatus = COption::GetOptionString("sale", "status_on_allow_delivery", "");
             if (strlen($orderStatus) > 0 && $orderStatus != $arOrder["STATUS_ID"]) {
                 $dbStatus = CSaleStatus::GetList(array("SORT" => "ASC"), array("LID" => LANGUAGE_ID), false, false, array("ID", "SORT"));
                 while ($arStatus = $dbStatus->GetNext()) {
                     $arStatuses[$arStatus["ID"]] = $arStatus["SORT"];
                 }
                 if ($arStatuses[$orderStatus] >= $arStatuses[$arOrder["STATUS_ID"]]) {
                     CSaleOrder::StatusOrder($ID, $orderStatus);
                 }
             }
         }
         $userEMail = "";
         $dbOrderProp = CSaleOrderPropsValue::GetList(array(), array("ORDER_ID" => $arOrder["ID"], "PROP_IS_EMAIL" => "Y"));
         if ($arOrderProp = $dbOrderProp->Fetch()) {
             $userEMail = $arOrderProp["VALUE"];
         }
         if (strlen($userEMail) <= 0) {
             $dbUser = CUser::GetByID($arOrder["USER_ID"]);
             if ($arUser = $dbUser->Fetch()) {
                 $userEMail = $arUser["EMAIL"];
             }
         }
         $eventName = "SALE_ORDER_DELIVERY";
         $arFields = array("ORDER_ID" => $arOrder["ACCOUNT_NUMBER"], "ORDER_DATE" => $arOrder["DATE_INSERT_FORMAT"], "EMAIL" => $userEMail, "SALE_EMAIL" => COption::GetOptionString("sale", "order_email", "order@" . $_SERVER["SERVER_NAME"]));
         $bSend = true;
         foreach (GetModuleEvents("sale", "OnOrderDeliverSendEmail", true) as $arEvent) {
             if (ExecuteModuleEventEx($arEvent, array($ID, &$eventName, &$arFields)) === false) {
                 $bSend = false;
             }
         }
         if ($bSend) {
             $event = new CEvent();
             $event->Send($eventName, $arOrder["LID"], $arFields, "N");
         }
         CSaleMobileOrderPush::send("ORDER_DELIVERY_ALLOWED", array("ORDER" => $arOrder));
     }
     //reservation
     if (COption::GetOptionString("sale", "product_reserve_condition", "O") == "D" && $arOrder["RESERVED"] != $val) {
         if (!CSaleOrder::ReserveOrder($ID, $val)) {
             return false;
         }
     }
     //proceed to deduction
     if ($val == "Y") {
         $allowDeduction = COption::GetOptionString("sale", "allow_deduction_on_delivery", "");
         if ($allowDeduction == "Y" && $arOrder["DEDUCTED"] == "N") {
             CSaleOrder::DeductOrder($ID, "Y");
         }
     }
     return $res;
 }