11define(
'DELIVERY_RP_CSV_PATH',
$_SERVER[
'DOCUMENT_ROOT'].
BX_ROOT.
'/modules/sale/ru/delivery/rus_post');
18 private static $MAX_WEIGHT_HEAVY = 20000;
19 private static $MAX_WEIGHT = 10000;
20 private static $ZONES_COUNT = 5;
21 private static $BASE_WEIGHT = 500;
25 private static $TARIF_LESS_500 =
array(
33 private static $TARIF_MORE_500 =
array(
41 private static $TARIF_HEAVY_WEIGHT = 11;
42 private static $TARIF_FRAGILE = 14;
43 private static $TARIF_DECLARED_VAL = 20;
44 private static $TARIF_AVIA_STANDART = 15;
45 private static $TARIF_AVIA_HEAVY = 16;
47 private static $MAX_DIMENSIONS =
array(
"425",
"265",
"380");
51 public static function Init()
56 'MULTISITE_CONFIG' =>
"Y",
58 'DESCRIPTION' =>
GetMessage(
'SALE_DH_RP_DESCRIPTION').
' <a href="http://www.russianpost.ru">http://www.russianpost.ru</a>',
59 'DESCRIPTION_INNER' =>
GetMessage(
'SALE_DH_RP_DESCRIPTION_INNER'),
60 'BASE_CURRENCY' =>
'RUB',
62 'HANDLER' => __FILE__,
65 'DBGETSETTINGS' =>
array(
'CDeliveryRusPost',
'GetSettings'),
66 'DBSETSETTINGS' =>
array(
'CDeliveryRusPost',
'SetSettings'),
67 'GETCONFIG' =>
array(
'CDeliveryRusPost',
'GetConfig'),
68 'GETFEATURES' =>
array(
'CDeliveryRusPost',
'GetFeatures'),
70 'COMPABILITY' =>
array(
'CDeliveryRusPost',
'Compability'),
71 'CALCULATOR' =>
array(
'CDeliveryRusPost',
'Calculate'),
72 'TRACKING_CLASS_NAME' =>
'\Bitrix\Sale\Delivery\Tracking\RusPost',
73 'IS_HANDLER_COMPATIBLE' =>
array(
'CDeliveryRusPost',
'isHandlerCompatible'),
78 'TITLE' =>
GetMessage(
'SALE_DH_RP_LAND_TITLE'),
79 'DESCRIPTION' =>
GetMessage(
'SALE_H_RP_LAND_DESCRIPTION'),
80 'RESTRICTIONS_WEIGHT' =>
array(0, self::$MAX_WEIGHT_HEAVY),
81 'RESTRICTIONS_SUM' =>
array(0),
83 'RESTRICTIONS_DIMENSIONS' => self::$MAX_DIMENSIONS
87 'TITLE' =>
GetMessage(
'SALE_DH_RP_AVIA_TITLE'),
88 'DESCRIPTION' =>
GetMessage(
'SALE_DH_RP_AVIA_DESCRIPTION'),
89 'RESTRICTIONS_WEIGHT' =>
array(0, self::$MAX_WEIGHT_HEAVY),
90 'RESTRICTIONS_SUM' =>
array(0),
92 'RESTRICTIONS_DIMENSIONS' => self::$MAX_DIMENSIONS
104 $arShopLocation =
array();
121 if($shopPrevLocationId ==
'' || $shopPrevLocationId != $shopLocationId)
129 'CONFIG_GROUPS' =>
array(
130 'zones' =>
GetMessage(
'SALE_DH_RP_CONFIG_GROUP_ZONES'),
131 'tarifs' =>
GetMessage(
'SALE_DH_RP_CONFIG_GROUP_TARIFS'),
132 'land' =>
GetMessage(
'SALE_DH_RP_CONFIG_GROUP_LAND'),
133 'avia' =>
GetMessage(
'SALE_DH_RP_CONFIG_GROUP_AVIA'),
140 $arZones[0] =
GetMessage(
'SALE_DH_RP_CONFIG_ZONES_EMPTY');
142 for (
$i = 1;
$i <= self::$ZONES_COUNT;
$i++)
146 'delivery_regs_to_zones',
147 array(
'CDeliveryRusPost',
'importZonesFromCsv'),
148 array($arShopLocation),
152 $arConfig[
'CONFIG'][
'RESET_HANDLER_SETTINGS'] =
array(
154 'TITLE' =>
GetMessage(
'SALE_DH_RP_SET_DEFAULT_TARIF_ZONES'),
156 'DEFAULT' =>
'<a href="javascript:void(0);" onclick="BX.Sale.Delivery.resetRusPostSettings();">'.
GetMessage(
'SALE_DH_RP_SET_DEFAULT_TARIF_ZONES_SET').
'</a>'
159 foreach ($arRegions as $regId => $regName)
161 $codeByName = self::getRegionCodeByOldName($regName);
162 $code = $codeByName <>
'' ? $codeByName : $regId;
164 if(isset($arRegsToZones[
$code]))
166 $arConfig[
'CONFIG'][
'REG_'.$code] =
array(
167 'TYPE' =>
'DROPDOWN',
168 'DEFAULT' => isset($arRegsToZones[
$code]) ? $arRegsToZones[
$code] :
'0',
182 $arConfig[
'CONFIG'][
'RESET_TARIF_SETTINGS'] =
array(
184 'TITLE' =>
GetMessage(
'SALE_DH_RP_SET_DEFAULT_TARIF'),
186 'DEFAULT' =>
'<a href="javascript:void(0);" onclick="BX.Sale.Delivery.resetRusPostTarifSettings();">'.
GetMessage(
'SALE_DH_RP_SET_DEFAULT_TARIF_SET').
'</a>'
189 $arConfig[
'CONFIG'][
'tarif_section_1'] =
array(
191 'TITLE' =>
GetMessage(
'SALE_DH_RP_WEIGHT_LESS'),
196 'delivery_rus_post_tarifs',
197 array(
'CDeliveryRusPost',
'getTarifsByRegionFromCsv'),
198 array($arShopLocation),
202 foreach ($arZones as $zoneId => $zoneName)
207 $tarifId = self::$TARIF_LESS_500[$zoneId];
208 $arConfig[
'CONFIG'][
'ZONE_RATE_MAIN_'.$zoneId] =
array(
210 'DEFAULT' => isset($arTarifs[$tarifId]) ? $arTarifs[$tarifId] :
'0',
211 'TITLE' => $zoneName,
216 $arConfig[
'CONFIG'][
'tarif_section_2'] =
array(
218 'TITLE' =>
GetMessage(
'SALE_DH_RP_WEIGHT_MORE'),
222 foreach ($arZones as $zoneId => $zoneName)
227 $tarifId = self::$TARIF_MORE_500[$zoneId];
229 $arConfig[
'CONFIG'][
'ZONE_RATE_ADD_'.$zoneId] =
array(
231 'DEFAULT' => isset($arTarifs[$tarifId]) ? $arTarifs[$tarifId] :
'0',
232 'TITLE' => $zoneName,
238 $arConfig[
'CONFIG'][
'tarif_add_services'] =
array(
245 $tarifId = self::$TARIF_HEAVY_WEIGHT;
246 $arConfig[
'CONFIG'][
'service_'.$tarifId.
'_enabled'] =
array(
247 'TYPE' =>
'CHECKBOX',
248 'TITLE' =>
GetMessage(
'SALE_DH_RP_SRV_HEAVY'),
251 'HIDE_BY_NAMES' =>
array(
'service_'.$tarifId.
'_value')
254 $arConfig[
'CONFIG'][
'service_'.$tarifId.
'_value'] =
array(
256 'TITLE' =>
GetMessage(
'SALE_DH_RP_SRV_HEAVY_VAL').
' %',
258 'DEFAULT' => isset($arTarifs[$tarifId]) ? $arTarifs[$tarifId] :
'0',
262 $tarifId = self::$TARIF_FRAGILE;
263 $arConfig[
'CONFIG'][
'service_'.$tarifId.
'_enabled'] =
array(
264 'TYPE' =>
'CHECKBOX',
268 'HIDE_BY_NAMES' =>
array(
'service_'.$tarifId.
'_value'),
272 $arConfig[
'CONFIG'][
'service_'.$tarifId.
'_value'] =
array(
274 'TITLE' =>
GetMessage(
'SALE_DH_RP_SRV_FRGL_VAL').
' %',
276 'DEFAULT' => isset($arTarifs[$tarifId]) ? $arTarifs[$tarifId] :
'0'
280 $tarifId = self::$TARIF_DECLARED_VAL;
281 $arConfig[
'CONFIG'][
'service_'.$tarifId.
'_enabled'] =
array(
282 'TYPE' =>
'CHECKBOX',
286 'HIDE_BY_NAMES' =>
array(
'service_'.$tarifId.
'_value'),
290 $arConfig[
'CONFIG'][
'service_'.$tarifId.
'_value'] =
array(
292 'TITLE' =>
GetMessage(
'SALE_DH_RP_SRV_DECL_VAL'),
294 'DEFAULT' => isset($arTarifs[$tarifId]) ? $arTarifs[$tarifId] :
'0',
298 $aviableBoxes = self::getAviableBoxes();
300 foreach ($aviableBoxes as $boxId => $arBox)
305 foreach ($aviableBoxes as $boxId => $arBox)
308 $tarifId = self::$TARIF_AVIA_STANDART;
309 $arConfig[
'CONFIG'][
'tarif_avia_services'] =
array(
311 'TITLE' =>
GetMessage(
'SALE_DH_RP_TARIFS_AVIA'),
315 $arConfig[
'CONFIG'][
'tarif_avia_'.$tarifId.
'_value'] =
array(
317 'TITLE' =>
GetMessage(
'SALE_DH_RP_TARIF_AVIA_STNDRT'),
319 'DEFAULT' => isset($arTarifs[$tarifId]) ? $arTarifs[$tarifId] :
'0',
322 $tarifId = self::$TARIF_AVIA_HEAVY;
323 $arConfig[
'CONFIG'][
'tarif_avia_'.$tarifId.
'_value'] =
array(
325 'TITLE' =>
GetMessage(
'SALE_DH_RP_TARIF_AVIA_HEAVY'),
327 'DEFAULT' => isset($arTarifs[$tarifId]) ? $arTarifs[$tarifId] :
'0',
335 $result = unserialize($strSettings, [
'allowed_classes' =>
false]);
337 if(isset(
$result[
'RESET_HANDLER_SETTINGS']))
338 unset(
$result[
'RESET_HANDLER_SETTINGS']);
340 if(isset(
$result[
'SET_DEFAULT_TARIF_ZONES']))
341 unset(
$result[
'SET_DEFAULT_TARIF_ZONES']);
343 if(isset(
$result[
'RESET_TARIF_SETTINGS']))
344 unset(
$result[
'RESET_TARIF_SETTINGS']);
349 if(mb_substr(
$key, 0, 4) ==
'REG_')
356 if(mb_substr(
$key, 0, 5) ==
'ZONE_' || mb_substr(
$key, 0, 6) ==
'tarif_' || mb_substr(
$key, 0, 8) ==
'service_')
365 if(isset($arSettings[
'RESET_HANDLER_SETTINGS']))
366 unset($arSettings[
'RESET_HANDLER_SETTINGS']);
368 if(isset($arSettings[
'SET_DEFAULT_TARIF_ZONES']))
369 unset($arSettings[
'SET_DEFAULT_TARIF_ZONES']);
371 if(isset($arSettings[
'RESET_TARIF_SETTINGS']))
372 unset($arSettings[
'RESET_TARIF_SETTINGS']);
375 foreach ($arSettings as
$key => $value)
377 if ($value <>
'' && (mb_substr(
$key, 0, 4) !=
'REG_' || $value !=
'0'))
378 $arSettings[
$key] = $value;
380 unset($arSettings[
$key]);
383 return serialize($arSettings);
390 if ($arConfig[
"service_".self::$TARIF_FRAGILE.
"_enabled"][
"VALUE"] ==
"Y")
393 if ($arConfig[
"service_".self::$TARIF_DECLARED_VAL.
"_enabled"][
"VALUE"] ==
"Y")
399 public static function Calculate($profile, $arConfig, $arOrder,
$STEP, $TEMP =
false)
401 $maxWeight = self::isHeavyEnabled($arConfig) ? self::$MAX_WEIGHT_HEAVY : self::$MAX_WEIGHT;
410 $packageCount =
count($arPackagesParams);
412 if(intval($packageCount) <= 0)
425 foreach ($arPackagesParams as $arPackage)
426 $totalPrice += self::calculatePackPrice($arPackage, $profile, $arConfig, $arLocationTo);
432 "TEXT" => $e->getMessage()
438 'VALUE' => $totalPrice,
439 'PACKS_COUNT' => $packageCount
446 $profiles =
array(
'land',
'avia');
448 $bHevyWeightEnabled = self::isConfCheckedVal($arConfig,
'service_'.self::$TARIF_HEAVY_WEIGHT.
'_enabled');
450 $maxWeight = $bHevyWeightEnabled ? self::$MAX_WEIGHT_HEAVY : self::$MAX_WEIGHT;
452 if (!empty($arOrder[
"ITEMS"]) && is_array($arOrder[
"ITEMS"]))
454 foreach ($arOrder[
"ITEMS"] as $arItem)
456 if ((
float)($arItem[
'WEIGHT'] ?? 0) > $maxWeight)
466 if(self::getLocationToCode($locationToCode) ==
'')
471 foreach ($profiles as $profile)
475 foreach ($aviableBoxes as $arBox)
492 if(empty($arShopLocation) || !isset($arShopLocation[
"REGION_ID"]) || !isset($arShopLocation[
'REGION_NAME_LANG']))
495 $regionCodeFromCode = $regionCodeFromName =
"";
499 '=TYPE.CODE' =>
'REGION',
500 '=REGION_ID' => intval($arShopLocation[
"REGION_ID"]),
508 if($locReg =
$dbRes->fetch())
509 $regionCodeFromCode = $locReg[
"CODE"];
511 $regionCodeFromName = self::getRegionCodeByOldName($arShopLocation[
'REGION_NAME_LANG']);
515 $arRegsTo = $csvFile->Fetch();
516 $arRegionsZones =
array();
518 while (
$arRes = $csvFile->Fetch())
520 if(isset(
$arRes[$COL_REG_CODE])
522 $regionCodeFromCode ==
$arRes[$COL_REG_CODE]
523 || $regionCodeFromName ==
$arRes[$COL_REG_CODE]
529 if(isset($arRegsTo[
$i])
542 return $arRegionsZones;
555 $arRes = $csvFile->Fetch();
558 $arRegionsZones =
array();
560 while (
$arRes = $csvFile->Fetch())
567 if(isset($arRegions[
$location[
'REGION_ID']]))
570 unset($arRegions[
$location[
'REGION_ID']]);
572 if(empty($arRegions))
576 return $arRegionsZones;
581 if(empty($arShopLocation) || !isset($arShopLocation[
'REGION_NAME_LANG'], $arShopLocation[
"ID"]))
584 $regionCodeFromCode = $regionCodeFromName =
"";
588 $res = \Bitrix\Sale\Location\LocationTable::getList(
array(
589 'filter' =>
array(
'=ID' => $loc[
"REGION_ID"]),
590 'select' =>
array(
'CODE')
593 if($locReg =
$res->fetch())
595 $regionCodeFromCode = $locReg[
"CODE"];
598 $regionCodeFromName = self::getRegionCodeByOldName($arShopLocation[
'REGION_NAME_LANG']);
600 $tarifNumber =
false;
603 while (
$arRes = $csvFile->Fetch())
606 ($regionCodeFromCode <>
'' && in_array($regionCodeFromCode,
$arRes))
607 || ($regionCodeFromName <>
'' && in_array($regionCodeFromName,
$arRes))
610 $tarifNumber =
$arRes[$COL_TARIF_NUM];
621 if($tarifNumber ===
false)
625 $COL_TARIF_ITEMS = 0;
627 $arRes = $csvFile->Fetch();
628 while (
$arRes = $csvFile->Fetch())
630 if(!isset(
$arRes[$tarifNumber]))
633 $arTarifs[
$arRes[$COL_TARIF_ITEMS]] =
$arRes[$tarifNumber];
639 private static function getConfValue(&$arConfig,
$key)
644 private static function isConfCheckedVal(&$arConfig,
$key)
646 return $arConfig[
$key][
'VALUE'] ==
'Y'
648 !isset($arConfig[
$key][
'VALUE'])
649 && $arConfig[
$key][
'DEFAULT'] ==
'Y'
653 private static function isHeavyEnabled(&$arConfig)
655 return self::isConfCheckedVal($arConfig,
'service_'.self::$TARIF_HEAVY_WEIGHT.
'_enabled');
658 private static function getAviableBoxes()
662 "NAME" =>
GetMessage(
"SALE_DH_RP_STNDRD_BOX"),
663 "DIMENSIONS" =>
array(
"425",
"265",
"380")
668 private static function getLocationToCode($arLocationTo)
670 if (!is_array($arLocationTo))
674 $code = (string)self::getRegionCodeByOldName($arLocationTo[
'REGION_NAME_LANG'] ??
'');
678 $dbRes = Location\LocationTable::getList([
680 '=TYPE.CODE' =>
'REGION',
681 '=REGION_ID' => (
int)($arLocationTo[
"REGION_ID"] ?? 0),
691 $locReg =
$dbRes->fetch();
693 $code = $locReg[
"CODE"] ??
'';
699 private static function calculatePackPrice($arPackage, $profile, $arConfig, $arLocationTo)
706 $code = self::getLocationToCode($arLocationTo);
709 throw new \Bitrix\Main\SystemException(
GetMessage(
"SALE_DH_RP_ERROR_LOCATION_NOT_FOUND"));
711 $zoneTo = self::getConfValue($arConfig,
'REG_'.
$code);
712 $basePrice = floatval(self::getConfValue($arConfig,
'ZONE_RATE_MAIN_'.$zoneTo));
715 throw new \Bitrix\Main\SystemException(
GetMessage(
"SALE_DH_RP_CALCULATE_ERROR"));
717 $arDebug[] =
'Base Price less 500 g: '.$basePrice;
719 if($arPackage[
'WEIGHT'] > self::$BASE_WEIGHT)
721 $addWeight = ceil($arPackage[
'WEIGHT'] / self::$BASE_WEIGHT - 1);
722 $addPrice = floatval(self::getConfValue($arConfig,
'ZONE_RATE_ADD_'.$zoneTo));
723 $arDebug[] =
'Price for additional weight more than 500 g: '.$addWeight * $addPrice;
724 $basePrice += $addWeight * $addPrice;
727 $totalPrice = $basePrice;
731 if($arPackage[
'WEIGHT'] >= self::$MAX_WEIGHT)
733 $hwTarif = floatval(self::getConfValue($arConfig,
'service_'.self::$TARIF_HEAVY_WEIGHT.
'_value'));
734 $hwPrice += $totalPrice*$hwTarif/100;
735 $arDebug[] =
'Heavy weight: '.$hwPrice;
736 $totalPrice += $hwPrice;
741 if(self::isConfCheckedVal($arConfig,
'service_'.self::$TARIF_FRAGILE.
'_enabled'))
743 $fTarif = floatval(self::getConfValue($arConfig,
'service_'.self::$TARIF_FRAGILE.
'_value'));
744 $fPrice += $totalPrice*$fTarif/100;
745 $arDebug[] =
'Fragile: '.$fPrice;
746 $totalPrice += $fPrice;
751 if(self::isConfCheckedVal($arConfig,
'service_'.self::$TARIF_DECLARED_VAL.
'_enabled'))
753 $dvTarif = floatval(self::getConfValue($arConfig,
'service_'.self::$TARIF_DECLARED_VAL.
'_value'));
754 $dvPrice += ($arPackage[
'PRICE'])*$dvTarif;
755 $arDebug[] =
'Declared value: '.$dvPrice;
756 $totalPrice += $dvPrice;
759 if($profile ==
'avia')
762 $aviaPrice = floatval(self::getConfValue($arConfig,
'tarif_avia_'.self::$TARIF_AVIA_STANDART.
'_value'));
763 $arDebug[] =
'avia price: '.$aviaPrice;
764 $totalPrice += $aviaPrice;
767 if($arPackage[
'WEIGHT'] > self::$MAX_WEIGHT)
769 $aviaHeavyPrice = floatval(self::getConfValue($arConfig,
'tarif_avia_'.self::$TARIF_AVIA_HEAVY.
'_value'));
770 $arDebug[] =
'avia heavy price: '.$aviaHeavyPrice;
771 $totalPrice += $aviaHeavyPrice;
778 protected static function getRegionCodeByOldName($regionLangName)
780 if($regionLangName ==
'')
787 require_once(__DIR__.
'/rus_post/old_loc_to_codes.php');
792 return $data[$regionLangName] ??
"";
803 return \Bitrix\Sale\Delivery\Helper::getPortalZone() !==
'ua';
static delete($moduleId, array $filter=array())
static get($moduleId, $name, $default="", $siteId=false)
static set($moduleId, $name, $value="", $siteId="")
static checkDimensions($arOrderDimensions, $arRestrictDimensions)
static isLocationProMigrated()
static Compability($arOrder, $arConfig)
static GetConfig($siteId=false)
static Calculate($profile, $arConfig, $arOrder, $STEP, $TEMP=false)
static importZonesFromZipCsv()
static importZonesFromCsv(array $arShopLocation)
static SetSettings($arSettings)
static getTarifsByRegionFromCsv(array $arShopLocation)
const LOCATION_CODE_RUSSIA
static GetSettings($strSettings)
static isHandlerCompatible()
static getTarifNumFromCsv(array $arShopLocation)
static GetFeatures($arConfig)
static IncludeModule($module_name)
static makeBoxConfig($boxId, $arBox, $group, &$arConfig)
static getRequiredPacks(&$arItems, $arPacks, $maxWeight, $reservedSpace=0.15)
static getRegionsList($countryId=0, $bFlip=false)
static getConfValue($arField)
static getBoxesFromConfig($profile, &$arConfig)
static getCsvObject($filePath)
static getLocationByIdHitCached($id)
static getShopLocationId($siteId)
static getOptionOrImportValues($optName, $importFuncName=false, $arFuncParams=array(), $siteId="")
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
$_SERVER["DOCUMENT_ROOT"]
if(!is_null($config))($config as $configItem)(! $configItem->isVisible()) $code
AddEventHandler($FROM_MODULE_ID, $MESSAGE_ID, $CALLBACK, $SORT=100, $FULL_PATH=false)
IncludeModuleLangFile($filepath, $lang=false, $bReturnArray=false)
GetMessage($name, $aReplace=null)
if(empty($signedUserToken)) $key
const DELIVERY_RP_CSV_PATH
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"