14use Bitrix\Main\Entity\EntityError;
21Loc::loadMessages(__FILE__);
41 parent::__construct($initParams);
43 if(isset($this->config[
"MAIN"][
"SID"]) && $this->config[
"MAIN"][
"SID"] <>
'')
47 if(!isset($initedHandlers[$this->config[
"MAIN"][
"SID"]]))
49 throw new SystemException(
"Can't initialize service with code\"".$this->config[
"MAIN"][
"SID"].
"\"");
52 $this->sid = $this->code = $this->config[
"MAIN"][
"SID"];
55 if(!empty($this->handlerInitParams[
"TRACKING_CLASS_NAME"]))
56 $this->setTrackingClass($this->handlerInitParams[
"TRACKING_CLASS_NAME"]);
58 if($this->handlerInitParams ==
false)
59 throw new SystemException(
"Can't get delivery services init params. Delivery id: ".$this->
id.
", sid: ".$this->sid);
61 if($this->currency ==
'' && !empty($this->handlerInitParams[
"BASE_CURRENCY"]))
62 $this->currency = $this->handlerInitParams[
"BASE_CURRENCY"];
67 if(isset($initParams[
"CONFIG"]))
68 $this->oldConfig = $initParams[
"CONFIG"];
76 return 'BITRIX_' . (string)$this->sid;
81 return Loc::getMessage(
"SALE_DLVR_HANDL_AUT_NAME");
86 return Loc::getMessage(
"SALE_DLVR_HANDL_AUT_DESCRIPTION");
91 static $handlers =
null;
92 static $jsData =
array();
98 if($handlers ===
null)
100 $handlers =
array(
"" =>
"");
102 foreach($initedHandlers as $handler)
104 if (isset($handler[
"DEPRECATED"]) && $handler[
"DEPRECATED"] =
"Y")
109 if (!self::isAutomaticHandlerCompatible($handler))
114 $handlers[$handler[
"SID"]] = $handler[
"NAME"].
" [".$handler[
"SID"].
"]";
115 $jsData[$handler[
"SID"]] =
array(
123 if (($this->handlerInitParams[
"SID"] ??
'') ==
'' || $this->
id <=0)
127 "TITLE" => Loc::getMessage(
"SALE_DLVR_HANDL_AUT_HANDLER_SETTINGS"),
128 "DESCRIPTION" => Loc::getMessage(
"SALE_DLVR_HANDL_AUT_HANDLER_SETTINGS_DSCR"),
132 "NAME" => Loc::getMessage(
"SALE_DLVR_HANDL_AUT_HANDLER_CHOOSE"),
133 "OPTIONS" => $handlers,
134 "ONCHANGE" =>
"var data=".\CUtil::PhpToJSObject($jsData).
"; BX.onCustomEvent('onDeliveryServiceNameChange',[{name: data[this.value][0], description: data[this.value][1]}]); BX('adm-sale-delivery-auto-description_inner_view').innerHTML=data[this.value][2]; //this.form.submit();"
136 "DESCRIPTION_INNER" =>
array(
137 "TYPE" =>
"DELIVERY_READ_ONLY",
138 "NAME" => Loc::getMessage(
"SALE_DLVR_HANDL_AUT_DESCRIPTION_INNER"),
139 "ID" =>
"adm-sale-delivery-auto-description_inner",
148 $handler = $this->handlerInitParams[
"SID"];
152 "TITLE" => Loc::getMessage(
"SALE_DLVR_HANDL_AUT_HANDLER_SETTINGS"),
153 "DESCRIPTION" => Loc::getMessage(
"SALE_DLVR_HANDL_AUT_HANDLER_SETTINGS_DSCR"),
156 "TYPE" =>
"DELIVERY_READ_ONLY",
157 "NAME" => Loc::getMessage(
"SALE_DLVR_HANDL_AUT_HANDLER_CHOOSE"),
159 "VALUE_VIEW" => $handlers[$handler]
161 "DESCRIPTION_INNER" =>
array(
162 "TYPE" =>
"DELIVERY_READ_ONLY",
163 "NAME" => Loc::getMessage(
"SALE_DLVR_HANDL_AUT_DESCRIPTION_INNER"),
164 "VALUE" => $this->handlerInitParams[
"DESCRIPTION_INNER"]
173 if(\
Bitrix\
Main\Loader::includeModule(
'currency'))
175 $currencyList = CurrencyManager::getCurrencyList();
177 if (isset($currencyList[$this->currency]))
180 unset($currencyList);
183 $marginTypes =
array(
185 "CURRENCY" => $serviceCurrency
190 "NAME" => Loc::getMessage(
"SALE_DLVR_HANDL_AUT_MARGIN_VALUE"),
196 "NAME" => Loc::getMessage(
"SALE_DLVR_HANDL_AUT_MARGIN_TYPE"),
198 "OPTIONS" => $marginTypes
203 $configProfileIds = array_keys($this->handlerInitParams[
"PROFILES"]);
207 $configProfileIds =
array();
210 if(isset($this->oldConfig[
"CONFIG_GROUPS"]))
212 $groupProfileIds = array_keys($this->oldConfig[
"CONFIG_GROUPS"]);
213 $intersect = array_intersect($groupProfileIds, $configProfileIds);
215 foreach($intersect as $pid)
216 unset($this->oldConfig[
"CONFIG_GROUPS"][$pid]);
223 if(isset(
$oldConfig[
"CONFIG_GROUPS"][
"MAIN"]))
242 if(!isset(
$fields[
"CONFIG"][
"MAIN"][
"SID"]) ||
$fields[
"CONFIG"][
"MAIN"][
"SID"] ==
'')
243 throw new SystemException(Loc::getMessage(
"SALE_DLVR_HANDL_AUT_ERROR_HANDLER"));
250 $configMain =
$fields[
"CONFIG"][
"MAIN"];
252 if (isset($this->handlerInitParams[
"DBSETSETTINGS"]) && is_callable($this->handlerInitParams[
"DBSETSETTINGS"]))
254 $oldSettings =
$fields[
"CONFIG"];
255 unset($oldSettings[
"MAIN"]);
259 if (!$strOldSettings = call_user_func($this->handlerInitParams[
"DBSETSETTINGS"], $oldSettings))
260 throw new SystemException(
"Can't save delivery services's old settings");
264 $strOldSettings =
"";
267 $strOldSettings = serialize($strOldSettings);
269 "MAIN" => $configMain
272 $fields[
"CONFIG"][
"MAIN"][
"OLD_SETTINGS"] = $strOldSettings;
274 if(isset($this->handlerInitParams[
"CURRENCY"]) && $this->handlerInitParams[
"CURRENCY"] <>
'')
275 $fields[
"CURRENCY"] = $this->handlerInitParams[
"CURRENCY"];
284 foreach($newSettings as
$key => $value)
287 $result = array_merge(
$result, self::convertNewSettingsToOld($value));
306 foreach(
$oldConfig[
"CONFIG_GROUPS"] as $groupId => $groupName)
308 $handlerConfig =
array(
309 "TITLE" => $groupName,
310 "DESCRIPTION" => $groupName,
316 if($param[
"GROUP"] == $groupId)
320 if(isset($param[
"MCS_ID"]))
322 if($newParam[
"TYPE"] ==
'DELIVERY_MULTI_CONTROL_STRING')
326 $handlerConfig[
"ITEMS"][$mc->getKey()] = $mc->getParams();
330 $mc->setParams(
$key, $newParam);
334 $mc->addItem(
$key, $newParam);
338 $handlerConfig[
"ITEMS"][
$key] = $newParam;
343 $handlerConfig[
"ITEMS"][$mc->getKey()] = $mc->getParams();
345 $handlerConfig[
"ITEMS"][
$key] = $newParam;
349 $handlerConfig[
"ITEMS"][
$key] = $newParam;
356 $handlerConfig[
"ITEMS"][$mc->getKey()] = $mc->getParams();
360 $result[$groupId] = $handlerConfig;
370 if(isset($oldParam[
"TYPE"]))
372 switch($oldParam[
"TYPE"])
393 $result[
"TYPE"] =
'DELIVERY_SECTION';
396 case 'MULTI_CONTROL_STRING':
397 $result[
"TYPE"] =
'DELIVERY_MULTI_CONTROL_STRING';
401 $result[
"TYPE"] =
'DELIVERY_READ_ONLY';
410 if(isset($oldParam[
"TITLE"]))
411 $result[
"NAME"] = $oldParam[
"TITLE"];
413 if(isset($oldParam[
"DEFAULT"]))
414 $result[
"DEFAULT"] = $oldParam[
"DEFAULT"];
416 if(isset($oldParam[
"VALUE"]))
417 $result[
"VALUE"] = $oldParam[
"VALUE"];
419 if(isset($oldParam[
"VALUES"]))
420 $result[
"OPTIONS"] = $oldParam[
"VALUES"];
427 return \CSaleDelivery::convertOrderNewToOld($shipment);
437 return isset($handlers[
$sid]) ? $handlers[
$sid] :
false;
442 static $isHandlerInited =
false;
448 \
Bitrix\
Main\
Config\Option::get(
'sale',
'delivery_handles_custom_path', BX_PERSONAL_ROOT.
'/php_interface/include/sale_delivery/'),
449 "/bitrix/modules/sale/delivery/",
452 $arLoadedHandlers =
array();
477 $isHandlerInited =
true;
483 static $arHandlersList =
array();
485 if(isset($arHandlersList[$indexBy]) && is_array($arHandlersList[$indexBy]))
486 return $arHandlersList[$indexBy];
490 $arHandlersList[$indexBy] =
array();
492 foreach(
GetModuleEvents(
"sale",
"onSaleDeliveryHandlersBuildList",
true) as $arHandler)
497 if($indexBy <>
'' && isset($initParams[$indexBy]))
498 $arHandlersList[$indexBy][$initParams[$indexBy]] = $initParams;
500 $arHandlersList[$indexBy][] = $initParams;
503 return $arHandlersList[$indexBy];
519 $handlerExists = isset($handlers[
$service[
"SID"]]) && is_array($handlers[
$service[
"SID"]]);
540 $serviceId = (int)(
$service[
'ID'] ?? 0);
545 foreach($restrictions as $restriction)
547 if($restriction[
"CLASS_NAME"] ==
'\Bitrix\Sale\Delivery\Restrictions\BySite' && !empty($restriction[
"PARAMS"][
"SITE_ID"]))
549 if(is_array($restriction[
"PARAMS"][
"SITE_ID"]))
551 reset($restriction[
"PARAMS"][
"SITE_ID"]);
552 $siteId = current($restriction[
"PARAMS"][
"SITE_ID"]);
556 $siteId = $restriction[
"PARAMS"][
"SITE_ID"];
565 $siteId = Helper::getDefaultSiteId();
572 $service[
"SETTINGS"] = unserialize(
$service[
"SETTINGS"], [
'allowed_classes' =>
false]);
579 $profileId = $profile[
"CONFIG"][
"MAIN"][
"PROFILE_ID"];
580 $profileParams =
array(
581 "TITLE" => $profile[
"NAME"],
582 "DESCRIPTION" => $profile[
"DESCRIPTION"],
583 "TAX_RATE" =>
$service[
"CONFIG"][
"MAIN"][
"MARGIN_VALUE"] ??
'',
584 "ACTIVE" => $profile[
"ACTIVE"]
589 foreach($restrictions as $restriction)
591 switch($restriction[
"CLASS_NAME"])
593 case '\Bitrix\Sale\Delivery\Restrictions\ByWeight':
594 $profileParams[
"RESTRICTIONS_WEIGHT"] =
array($restriction[
"PARAMS"][
"MIN_WEIGHT"], $restriction[
"PARAMS"][
"MAX_WEIGHT"]);
597 case '\Bitrix\Sale\Delivery\Restrictions\ByPrice':
598 $profileParams[
"RESTRICTIONS_SUM"] =
array($restriction[
"PARAMS"][
"MIN_PRICE"], $restriction[
"PARAMS"][
"MAX_PRICE"]);
601 case '\Bitrix\Sale\Delivery\Restrictions\ByDimensions':
602 $profileParams[
"RESTRICTIONS_DIMENSIONS"] =
array(
603 $restriction[
"PARAMS"][
"LENGTH"],
604 $restriction[
"PARAMS"][
"WIDTH"],
605 $restriction[
"PARAMS"][
"HEIGHT"]
608 $profileParams[
"RESTRICTIONS_MAX_SIZE"] = $restriction[
"PARAMS"][
"MAX_DIMENSION"];
609 $profileParams[
"RESTRICTIONS_DIMENSIONS_SUM"] = $restriction[
"PARAMS"][
"MAX_DIMENSIONS_SUM"];
618 $service[
"PROFILES"][$profileId] = $profileParams;
641 if(!is_callable($this->handlerInitParams[
"DBGETSETTINGS"]))
644 return call_user_func($this->handlerInitParams[
"DBGETSETTINGS"],
$settings);
650 $hitCacheId = md5(serialize($initHandlerParams)).
"_".md5(serialize(
$settings)).
"_".strval(
$siteId);
652 if(!isset(
$result[$hitCacheId]))
655 "CONFIG_GROUPS" =>
array(),
659 if (is_callable($initHandlerParams[
"GETCONFIG"]))
661 $conf = call_user_func($initHandlerParams[
"GETCONFIG"],
$siteId);
663 if(isset($conf[
"CONFIG_GROUPS"]))
664 $config[
"CONFIG_GROUPS"] = $conf[
"CONFIG_GROUPS"];
666 if (
$settings <> '' && is_callable($initHandlerParams[
"DBGETSETTINGS"]))
669 $arConfigValues = call_user_func($initHandlerParams[
"DBGETSETTINGS"],
$settings);
673 $arConfigValues =
array();
676 foreach ($conf[
"CONFIG"] as
$key => $arConfig)
678 if (is_array($conf[
"CONFIG"][
$key]))
682 if(isset($arConfigValues[
$key]))
684 elseif(isset($conf[
"CONFIG"][
$key][
"DEFAULT"]))
702 if(isset($this->handlerInitParams[
"CALCULATOR"]) && is_callable($this->handlerInitParams[
"CALCULATOR"]))
703 $result = $this->handlerInitParams[
"CALCULATOR"];
712 if(isset($this->handlerInitParams[
"COMPABILITY"]) && is_callable($this->handlerInitParams[
"COMPABILITY"]))
713 $result = $this->handlerInitParams[
"COMPABILITY"];
726 if(!empty($oldOrder[
"ITEMS"]) && is_array($oldOrder[
"ITEMS"]))
728 $maxDimensions =
array();
730 foreach($oldOrder[
"ITEMS"] as $item)
732 if (!isset($item[
'DIMENSIONS']))
736 if (is_string($item[
'DIMENSIONS']) && $item[
'DIMENSIONS'] !==
'')
738 $item[
'DIMENSIONS'] = unserialize($item[
'DIMENSIONS'], [
'allowed_classes' =>
false]);
741 if(!is_array($item[
"DIMENSIONS"]) || empty($item[
"DIMENSIONS"]))
746 $item[
"DIMENSIONS"][
"WIDTH"],
747 $item[
"DIMENSIONS"][
"HEIGHT"],
748 $item[
"DIMENSIONS"][
"LENGTH"]
754 if(!empty($maxDimensions))
755 $oldOrder[
"MAX_DIMENSIONS"] = $maxDimensions;
758 $hitCacheId =
$sid.
'_'.md5(serialize($oldOrder)).
'_'.md5(serialize(
$config[
"CONFIG"]));
760 if(!isset(
$result[$hitCacheId]))
762 $result[$hitCacheId] = call_user_func($compatibilityFunc, $oldOrder,
$config[
"CONFIG"]);
772 if($compatibilityFunc ===
false)
776 return is_array(
$res) && in_array($profileId,
$res);
801 $hitCacheId = $this->
id.
'_'.$profileId.
'_'.md5(serialize($profileConfig)).
'_'.md5(serialize($oldOrder));
803 if(isset(
$result[$hitCacheId]))
804 return clone
$result[$hitCacheId];
811 $shipmentCollection = $shipment->getCollection();
813 $order = $shipmentCollection->getOrder();
814 $shipmentCurrency =
$order->getCurrency();
816 if(!Loader::includeModule(
'currency'))
821 if ($calculator!==
false)
823 if (
$res = call_user_func(
826 $profileConfig[
"CONFIG"],
833 if(
$res[
"RESULT"] ==
"OK" )
835 if(isset(
$res[
"TEXT"]))
836 $calcRes->setDescription(
$res[
"TEXT"]);
838 if(isset(
$res[
"VALUE"]))
839 $calcRes->setDeliveryPrice(floatval(
$res[
"VALUE"]));
841 if(isset(
$res[
"TRANSIT"]))
842 $calcRes->setPeriodDescription(
$res[
"TRANSIT"]);
844 if(isset(
$res[
"PERIOD_FROM"]))
845 $calcRes->setPeriodFrom(
$res[
"PERIOD_FROM"]);
847 if(isset(
$res[
"PERIOD_TO"]))
848 $calcRes->setPeriodTo(
$res[
"PERIOD_TO"]);
850 if(isset(
$res[
"PERIOD_TYPE"]))
851 $calcRes->setPeriodType(
$res[
"PERIOD_TYPE"]);
855 if(isset(
$res[
"TEXT"]) &&
$res[
"TEXT"] <>
'')
859 'DELIVERY_CALCULATION'
865 Loc::getMessage(
'SALE_DLVR_HANDL_AUT_ERROR_CALCULATION'),
866 'DELIVERY_CALCULATION'
873 $calcRes->setDeliveryPrice(floatval(
$res));
880 $calcRes->addError(
new EntityError(
882 'DELIVERY_CALCULATION'
887 $calcRes->setDeliveryPrice(0);
891 if ($calcRes->isSuccess() && $this->currency != $shipmentCurrency)
893 $calcRes->setDeliveryPrice(
894 \CCurrencyRates::convertCurrency(
895 $calcRes->getPrice(),
902 $price = $calcRes->getPrice();
904 $calcRes->setDeliveryPrice(
908 $result[$hitCacheId] = $calcRes;
909 return clone
$result[$hitCacheId];
915 '\Bitrix\Sale\Delivery\Services\AutomaticProfile'
926 if($this->config[
"MAIN"][
"MARGIN_TYPE"] ==
"%")
928 $marginPrice = $price * floatval($this->config[
"MAIN"][
"MARGIN_VALUE"]) / 100;
932 $marginPrice = floatval($this->config[
"MAIN"][
"MARGIN_VALUE"]);
934 if($marginPrice && $shipmentCurrency !=
'' && $this->currency != $shipmentCurrency)
936 if(Loader::includeModule(
'currency'))
938 $marginPrice = \CCurrencyRates::convertCurrency(
952 if(empty($this->handlerInitParams[
"PROFILES"]) || !is_array($this->handlerInitParams[
"PROFILES"]))
957 foreach($this->handlerInitParams[
"PROFILES"] as $profId =>
$params)
963 "CODE" => $this->handlerInitParams[
"SID"].
":".$profId,
964 "PARENT_ID" => $this->
id,
966 "ACTIVE" => $this->active ?
"Y" :
"N",
967 "SORT" => $this->sort,
968 "DESCRIPTION" => isset(
$params[
"DESCRIPTION"]) ?
$params[
"DESCRIPTION"] :
"",
969 "CLASS_NAME" =>
'\Bitrix\Sale\Delivery\Services\AutomaticProfile',
970 "CURRENCY" => $this->currency,
974 "PROFILE_ID" => $profId,
992 $profiles = $srv->getProfilesDefaultParams(
$fields);
994 if(!is_array($profiles))
999 foreach($profiles as $profile)
1001 $res = Manager::add($profile);
1016 foreach($this->handlerInitParams[
"PROFILES"] as $profId =>
$params)
1026 return self::$canHasProfiles;
1033 if(isset($this->handlerInitParams[
"GET_ADMIN_MESSAGE"]) && is_callable($this->handlerInitParams[
"GET_ADMIN_MESSAGE"]))
1034 $result = call_user_func($this->handlerInitParams[
"GET_ADMIN_MESSAGE"]);
1043 if(isset($this->handlerInitParams[
"EXEC_ADMIN_ACTION"]) && is_callable($this->handlerInitParams[
"EXEC_ADMIN_ACTION"]))
1044 $result = call_user_func($this->handlerInitParams[
"EXEC_ADMIN_ACTION"]);
1053 if(isset($this->handlerInitParams[
"GET_ADD_INFO_SHIPMENT_EDIT"]) && is_callable($this->handlerInitParams[
"GET_ADD_INFO_SHIPMENT_EDIT"]))
1056 $this->handlerInitParams[
"GET_ADD_INFO_SHIPMENT_EDIT"],
1061 throw new SystemException(
'GET_ADD_INFO_SHIPMENT_EDIT return value must be array!');
1071 if(isset($this->handlerInitParams[
"PROCESS_ADD_INFO_SHIPMENT_EDIT"]) && is_callable($this->handlerInitParams[
"PROCESS_ADD_INFO_SHIPMENT_EDIT"]))
1074 $this->handlerInitParams[
"PROCESS_ADD_INFO_SHIPMENT_EDIT"],
1080 throw new SystemException(
'PROCESS_ADD_INFO_SHIPMENT_EDIT return value myst be of type "Bitrix\Sale\Result" !');
1090 if(isset($this->handlerInitParams[
"GET_ADD_INFO_SHIPMENT_VIEW"]) && is_callable($this->handlerInitParams[
"GET_ADD_INFO_SHIPMENT_VIEW"]))
1093 $this->handlerInitParams[
"GET_ADD_INFO_SHIPMENT_VIEW"],
1098 throw new SystemException(
'GET_ADD_INFO_SHIPMENT_VIEW return value must be array!');
1114 if (isset($handler[
"IS_HANDLER_COMPATIBLE"])
1115 && is_callable($handler[
"IS_HANDLER_COMPATIBLE"]))
1117 $result = call_user_func($handler[
"IS_HANDLER_COMPATIBLE"]);
isProfileCompatible($profileId, $config, Shipment $shipment)
static convertNewSettingsToOld(array $newSettings=array())
static convertOldConfigToNew($oldConfig)
static convertNewOrderToOld(\Bitrix\Sale\Shipment $shipment)
static getHandlerInitParams($sid)
static createConfig($initHandlerParams, $settings, $siteId=false)
static getRegisteredHandlers($indexBy="")
static convertOldConfigParamToNew(array $oldParam)
static getClassDescription()
__construct(array $initParams)
static getCompatibleProfiles($sid, $compatibilityFunc, array $config, Shipment $shipment)
static getChildrenClassNames()
prepareFieldsForSaving(array $fields)
getOldDbSettings($settings)
getProfilesDefaultParams(array $fields=array())
getMarginPrice($price, $shipmentCurrency='')
static onAfterAdd($serviceId, array $fields=array())
getAdditionalInfoShipmentEdit(Shipment $shipment)
processAdditionalInfoShipmentEdit(Shipment $shipment, array $requestData)
static isAutomaticHandlerCompatible($handler)
static convertNewServiceToOld($service, $sid=false)
getAdditionalInfoShipmentView(Shipment $shipment)
static getByParentId($parentId)
static getRestrictionsList($serviceId)
static getMaxDimensions($arDim1, $arDim2)
if(!is_array($prop["VALUES"])) $tmp
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
$_SERVER["DOCUMENT_ROOT"]
ExecuteModuleEventEx($arEvent, $arParams=[])
htmlspecialcharsbx($string, $flags=ENT_COMPAT, $doubleEncode=true)
GetModuleEvents($MODULE_ID, $MESSAGE_ID, $bReturnArray=false)
sortByColumn(array &$array, $columns, $callbacks='', $defaultValueIfNotSetValue=null, $preserveKeys=false)
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
if(empty($signedUserToken)) $key
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']