1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
delivery_rus_post_first.php
См. документацию.
1<?
2/********************************************************************************
3Delivery services for Russian Post Service (http://www.russianpost.ru/)
4"First class" service.
5Calculations based on RP rates:
6http://www.russianpost.ru/rp/servise/ru/home/postuslug/1class/1class_tariffs
7********************************************************************************/
9
10IncludeModuleLangFile($_SERVER["DOCUMENT_ROOT"].'/bitrix/modules/sale/delivery/delivery_rus_post_first.php');
11
12define('DELIVERY_RPF_CSV_PATH', $_SERVER['DOCUMENT_ROOT'].BX_ROOT.'/modules/sale/ru/delivery/rus_post_first'); //where we can found csv files
13
15{
16 private static $MAX_WEIGHT = 2500; // (g)
17 private static $MAX_SUMM = 20000; // RUB
18 private static $MAX_SIZE = 360; //milimeters
19 private static $MAX_DIMENSIONS_SUMM = 700; //milimeters
20 private static $MAX_DIMENSIONS = array("165", "100", "190"); //milimeters
21
22 private static $BASE_WEIGHT = 100; // Base weight gramm
23
24 private static $TARIFS = array();
25 private static $SERVICES = array();
26
27 private static $TARIF_IDX = 0;
28 private static $TARIF_DESCR = 1;
29
30 /* Standard mandatory delivery services functions */
31 public static function Init()
32 {
33 self::$TARIFS = array(
34 'WEIGHT_LESS_100' => array(6, GetMessage('SALE_DH_RPF_WRP_LESS_100')),
35 'WEIGHT_LESS_100_DECLARED_VALUE' => array(11, GetMessage('SALE_DH_RPF_WRP_LESS_100_DECLARED_VALUE')),
36 'WEIGHT_MORE_100' => array(7, GetMessage('SALE_DH_RPF_WRP_MORE_100'))
37 );
38
39 self::$SERVICES = array(
40 'NOTIFICATION_SIMPLE' => array(8, GetMessage('SALE_DH_RPF_SMPL_NTF')),
41 'NOTIFICATION_REG' => array(9, GetMessage('SALE_DH_RPF_RGST_NTF')),
42 'DECLARED_VALUE' => array(10, GetMessage('SALE_DH_RPF_DCL_VAL'))
43 );
44
45 return array(
46 /* Basic description */
47 'SID' => 'rus_post_first',
48 'NAME' => GetMessage('SALE_DH_RPF_NAME'),
49 'DESCRIPTION' => GetMessage('SALE_DH_RPF_DESCR').' <a href="http://www.russianpost.ru/rp/servise/ru/home/postuslug/1class">http://www.russianpost.ru/rp/servise/ru/home/postuslug/1class</a>',
50 'DESCRIPTION_INNER' => GetMessage('SALE_DH_RPF_DESCR').' <a href="http://www.russianpost.ru/rp/servise/ru/home/postuslug/1class">http://www.russianpost.ru/rp/servise/ru/home/postuslug/1class</a>',
51 'BASE_CURRENCY' => 'RUB',
52 'HANDLER' => __FILE__,
53
54 /* Handler methods */
55 'DBGETSETTINGS' => array('CDeliveryRusPostFirst', 'GetSettings'),
56 'DBSETSETTINGS' => array('CDeliveryRusPostFirst', 'SetSettings'),
57 'GETCONFIG' => array('CDeliveryRusPostFirst', 'GetConfig'),
58 'GETFEATURES' => array('CDeliveryRusPostFirst', 'GetFeatures'),
59 'COMPABILITY' => array('CDeliveryRusPostFirst', 'Compability'),
60 'CALCULATOR' => array('CDeliveryRusPostFirst', 'Calculate'),
61 'DEPRECATED' => 'Y',
62 "GET_ADMIN_MESSAGE" => array("CDeliveryRUSSIANPOST", "getAdminMessage"),
63 "TRACKING_CLASS_NAME" => '\Bitrix\Sale\Delivery\Tracking\RusPost',
64
65 /* List of delivery profiles */
66 'PROFILES' => array(
67 'wrapper' => array(
68 'TITLE' => GetMessage('SALE_DH_RPF_WRP_TITLE'),
69 'DESCRIPTION' => GetMessage('SALE_DH_RPF_WRP_DESCR'),
70 'RESTRICTIONS_WEIGHT' => array(0, self::$MAX_WEIGHT),
71 'RESTRICTIONS_SUM' => array(0, self::$MAX_SUMM),
72 'TAX_RATE' => 0,
73 'RESTRICTIONS_MAX_SIZE' => self::$MAX_SIZE,
74 'RESTRICTIONS_DIMENSIONS_SUM' => self::$MAX_DIMENSIONS_SUMM,
75 'RESTRICTIONS_DIMENSIONS' => self::$MAX_DIMENSIONS
76 )
77 )
78 );
79 }
80
81 public static function GetConfig($siteId = false)
82 {
83 $shopLocationId = CSaleHelper::getShopLocationId($siteId);
84 $arShopLocation = CSaleHelper::getLocationByIdHitCached($shopLocationId);
85
86 if(!$arShopLocation)
87 $arShopLocation = array();
88
89 $shopPrevLocationId = COption::GetOptionString('sale', 'delivery_rus_post_first_prev_loc', 0);
90
91 /* if shop's location was changed */
92 if($shopPrevLocationId != $shopLocationId)
93 {
94 COption::SetOptionString('sale', 'delivery_rus_post_first_prev_loc', $shopLocationId);
95 COption::RemoveOption('sale', 'delivery_rus_post_first_tarifs');
96 }
97
98 $arConfig = array(
99 'CONFIG_GROUPS' => array(
100 'wrapper' => GetMessage('SALE_DH_RPF_WRP_TITLE'),
101 ),
102 );
103
104 $aviableBoxes = self::getAviableBoxes();
105
106 foreach ($aviableBoxes as $boxId => $arBox)
107 CSaleDeliveryHelper::makeBoxConfig($boxId, $arBox, 'wrapper', $arConfig);
108
109 $arConfig['CONFIG']['tarif_section_1'] = array(
110 'TYPE' => 'SECTION',
111 'TITLE' => GetMessage('SALE_DH_RPF_TARIFS'),
112 'GROUP' => 'wrapper',
113 );
114
115 $arConfig['CONFIG']['RESET_TARIF_SETTINGS'] = array(
116 'TYPE' => 'CUSTOM',
117 'TITLE' => GetMessage('SALE_DH_RPF_SET_DEFAULT_TARIF'),
118 'GROUP' => 'wrapper',
119 'DEFAULT' => '<a href="javascript:void(0);" onclick="BX.Sale.Delivery.resetRusPostTarifSettings();">'.GetMessage('SALE_DH_RPF_SET_DEFAULT_TARIF_SET').'</a>'
120 );
121
123 'delivery_rus_post_first_tarifs',
124 array('CDeliveryRusPostFirst', 'getTarifsByRegionFromCsv'),
125 array($arShopLocation)
126 );
127
128 foreach (self::$TARIFS as $arTarif)
129 {
130 $tarifId = $arTarif[self::$TARIF_IDX];
131
132 $arConfig['CONFIG']['TARIF_'.$tarifId] = array(
133 'TYPE' => 'STRING',
134 'DEFAULT' => isset($arTarifs[$tarifId]) ? $arTarifs[$tarifId] : '0',
135 'TITLE' => $arTarif[self::$TARIF_DESCR],
136 'GROUP' => 'wrapper',
137 );
138 }
139
140 /* Additional services */
141 foreach (self::$SERVICES as $serviceId => $arService)
142 {
143 $tarifId = $arService[self::$TARIF_IDX];
144
145 $arConfig['CONFIG']['service_'.$tarifId.'_section'] = array(
146 'TYPE' => 'SECTION',
147 'TITLE' => $arService[self::$TARIF_DESCR],
148 'GROUP' => 'wrapper',
149 );
150
151 $arConfig['CONFIG']['service_'.$tarifId.'_enabled'] = array(
152 'TYPE' => 'CHECKBOX',
153 'TITLE' => GetMessage('SALE_DH_RPF_SRV_ALLOW'),
154 'GROUP' => 'wrapper',
155 'DEFAULT' => $serviceId == 'NOTIFICATION_REG' ? 'N' : 'Y',
156 'HIDE_BY_NAMES' => array('service_'.$tarifId.'_value')
157 );
158
159 $arConfig['CONFIG']['service_'.$tarifId.'_value'] = array(
160 'TYPE' => 'STRING',
161 'TITLE' => GetMessage('SALE_DH_RPF_SRV_PRICE'),
162 'GROUP' => 'wrapper',
163 'DEFAULT' => isset($arTarifs[$tarifId]) ? $arTarifs[$tarifId] : '0',
164 );
165 }
166
167 return $arConfig;
168 }
169
170 public static function GetSettings($strSettings)
171 {
172 $result = unserialize($strSettings, ['allowed_classes' => false]);
173
174 if(isset($result['RESET_TARIF_SETTINGS']))
175 unset($result['RESET_TARIF_SETTINGS']);
176
177 if(isset($_REQUEST["RESET_TARIF_SETTINGS"]) && $_REQUEST["RESET_TARIF_SETTINGS"] == "Y" && !isset($_REQUEST["apply"]))
178 {
179 COption::RemoveOption('sale', 'delivery_rus_post_first_tarifs');
180
181 foreach($result as $key => $value)
182 if(mb_substr($key, 0, 6) == 'TARIF_' || mb_substr($key, 0, 8) == 'service_')
183 unset($result[$key]);
184 }
185
186 return $result;
187 }
188
189 public static function SetSettings($arSettings)
190 {
191 if(isset($arSettings['RESET_TARIF_SETTINGS']))
192 unset($arSettings['RESET_TARIF_SETTINGS']);
193
194 foreach ($arSettings as $key => $value)
195 {
196 if ($value <> '')
197 $arSettings[$key] = $value;
198 else
199 unset($arSettings[$key]);
200 }
201
202 return serialize($arSettings);
203 }
204
205 public static function GetFeatures($arConfig)
206 {
207 $arResult = array();
208
209 if ($arConfig["service_".array_shift(array_values(self::$SERVICES["NOTIFICATION_SIMPLE"]))."_enabled"]["VALUE"] == "Y")
210 $arResult[GetMessage("SALE_DH_RPF_SMPL_NTF")] = GetMessage("SALE_DH_RPF_FEATURE_ENABLED");
211
212 if ($arConfig["service_".array_shift(array_values(self::$SERVICES["NOTIFICATION_REG"]))."_enabled"]["VALUE"] == "Y")
213 $arResult[GetMessage("SALE_DH_RPF_RGST_NTF")] = GetMessage("SALE_DH_RPF_FEATURE_ENABLED");
214
215 if ($arConfig["service_".array_shift(array_values(self::$SERVICES["DECLARED_VALUE"]))."_enabled"]["VALUE"] == "Y")
216 $arResult[GetMessage("SALE_DH_RPF_FEATURE_VALUE")] = GetMessage("SALE_DH_RPF_FEATURE_ENABLED");
217
218 return $arResult;
219 }
220
221 public static function Calculate($profile, $arConfig, $arOrder, $STEP, $TEMP = false)
222 {
223 $arPacks = CSaleDeliveryHelper::getBoxesFromConfig($profile, $arConfig);
224
225 $arPackagesParams = CSaleDeliveryHelper::getRequiredPacks(
226 $arOrder["ITEMS"],
227 $arPacks,
228 self::$MAX_WEIGHT);
229
230 $packageCount = count($arPackagesParams);
231
232 if(intval($packageCount) <= 0)
233 {
234 return array(
235 "RESULT" => "ERROR",
236 "TEXT" => GetMessage("SALE_DH_RPF_OVERLOAD"),
237 );
238 }
239
240 $totalPrice = 0;
241 $arLocationTo = CSaleHelper::getLocationByIdHitCached($arOrder['LOCATION_TO']);
242
243 foreach ($arPackagesParams as $arPackage)
244 $totalPrice += self::calculatePackPrice($arPackage, $profile, $arConfig, $arLocationTo);
245
247 'RESULT' => 'OK',
248 'VALUE' => $totalPrice,
249 'PACKS_COUNT' => $packageCount
250 );
251 return $arResult;
252 }
253
254 public static function Compability($arOrder, $arConfig)
255 {
256 $result = array();
257
258 $aviableBoxes = CSaleDeliveryHelper::getBoxesFromConfig('wrapper', $arConfig);
259
260 foreach ($aviableBoxes as $arBox)
261 {
262 if (CSaleDeliveryHandler::checkDimensions($arOrder["MAX_DIMENSIONS"], $arBox["DIMENSIONS"]))
263 {
264 $result = array('wrapper');
265 break;
266 }
267 }
268
269 return $result;
270 }
271
272 /* Particular services helper functions*/
273
274 public static function getTarifNumFromCsv(array $arShopLocation)
275 {
276 if(empty($arShopLocation) || !isset($arShopLocation["REGION_ID"]) || !isset($arShopLocation['REGION_NAME_LANG']))
277 return false;
278
279 $regionCodeFromCode = $regionCodeFromName = "";
280
281 $dbRes = \Bitrix\Sale\Location\LocationTable::getById($arShopLocation["REGION_ID"]);
282
283 if($locReg = $dbRes->fetch())
284 $regionCodeFromCode = $locReg["CODE"];
285
286 $regionCodeFromName = self::getRegionCodeByOldName($arShopLocation['REGION_NAME_LANG']);
287
288 $csvFile = CSaleHelper::getCsvObject(DELIVERY_RP_CSV_PATH.'/tarif_regions.csv');
289 $tarifNumber = false;
290 $COL_TARIF_NUM = 0;
291
292 while ($arRes = $csvFile->Fetch())
293 {
294 if(
295 ($regionCodeFromCode <> '' && in_array($regionCodeFromCode, $arRes))
296 || ($regionCodeFromName <> '' && in_array($regionCodeFromName, $arRes))
297 )
298 {
299 $tarifNumber = $arRes[$COL_TARIF_NUM];
300 break;
301 }
302 }
303 return $tarifNumber;
304 }
305
306 public static function getTarifsByRegionFromCsv(array $arShopLocation)
307 {
308 if(empty($arShopLocation))
309 return false;
310
311 $tarifNumber = self::getTarifNumFromCsv($arShopLocation);
312
313 if($tarifNumber === false)
314 return false;
315
316 $csvFile = CSaleHelper::getCsvObject(DELIVERY_RPF_CSV_PATH.'/tarif_data.csv');
317 $COL_TARIF_ITEMS = 0;
318 $arTarifs = array();
319 $arRes = $csvFile->Fetch();
320
321 while ($arRes = $csvFile->Fetch())
322 {
323 if(!isset($arRes[$tarifNumber]))
324 break;
325
326 $arTarifs[$arRes[$COL_TARIF_ITEMS]] = $arRes[$tarifNumber];
327 }
328
329 return $arTarifs;
330 }
331
332 private static function getConfValue(&$arConfig, $key)
333 {
334 return CSaleDeliveryHelper::getConfValue($arConfig[$key]);
335 }
336
337 private static function isConfCheckedVal(&$arConfig, $key)
338 {
339 return $arConfig[$key]['VALUE'] == 'Y'
340 ||(
341 !isset($arConfig[$key]['VALUE'])
342 && $arConfig[$key]['DEFAULT'] == 'Y'
343 );
344 }
345
346 private static function getAviableBoxes()
347 {
348 return array(
349 array(
350 "NAME" => GetMessage("SALE_DH_RPF_STNRD_BOX"),
351 "DIMENSIONS" => array("165", "100", "190")
352 )
353 );
354 }
355
356 private static function calculatePackPrice($arPackage, $profile, $arConfig, $arLocationTo)
357 {
358 $arDebug = array();
359 $totalPrice = 0;
360 $declaredValue = self::isConfCheckedVal($arConfig, 'service_'.self::$SERVICES['DECLARED_VALUE'][self::$TARIF_IDX].'_enabled');
361
362 //2. Wrapper
363 //2.1, 2.2 declared value, weight less 100 gramm
364
365 if($declaredValue && floatval($arConfig['TARIF_'.self::$TARIFS['WEIGHT_LESS_100_DECLARED_VALUE'][self::$TARIF_IDX]]['VALUE']) > 0)
366 $basePrice = floatval(self::getConfValue($arConfig, 'TARIF_'.self::$TARIFS['WEIGHT_LESS_100_DECLARED_VALUE'][self::$TARIF_IDX]));
367 else
368 $basePrice = floatval(self::getConfValue($arConfig, 'TARIF_'.self::$TARIFS['WEIGHT_LESS_100'][self::$TARIF_IDX]));
369
370 $arDebug[] = 'Base Price less 100 g: '.$basePrice;
371
372 // 2.3 weight more than 100 g
373 if($arPackage['WEIGHT'] > self::$BASE_WEIGHT)
374 {
375 $addWeight = ceil($arPackage['WEIGHT'] / self::$BASE_WEIGHT - 1);
376 $addPrice = floatval(self::getConfValue($arConfig, 'TARIF_'.self::$TARIFS['WEIGHT_MORE_100'][self::$TARIF_IDX]));
377 $arDebug[] = 'Price for additional weight more than 100 g: '.$addPrice;
378 $basePrice += $addWeight * $addPrice;
379 }
380
381 $totalPrice = $basePrice;
382
383 // 3.1 simple notification
384 $snPrice = 0;
385 if(self::isConfCheckedVal($arConfig, 'service_'.self::$SERVICES['NOTIFICATION_SIMPLE'][self::$TARIF_IDX].'_enabled'))
386 {
387 $snPrice = floatval(self::getConfValue($arConfig, 'service_'.self::$SERVICES['NOTIFICATION_SIMPLE'][self::$TARIF_IDX].'_value'));
388 $arDebug[] = 'Simple notification: '.$snPrice;
389 $totalPrice += $snPrice;
390 }
391
392 // 3.2. registered notification
393 $rnPrice = 0;
394 if(self::isConfCheckedVal($arConfig, 'service_'.self::$SERVICES['NOTIFICATION_REG'][self::$TARIF_IDX].'_enabled'))
395 {
396 $rnPrice = floatval(self::getConfValue($arConfig, 'service_'.self::$SERVICES['NOTIFICATION_REG'][self::$TARIF_IDX].'_value'));
397 $arDebug[] = 'Registered notification: '.$rnPrice;
398 $totalPrice += $rnPrice;
399 }
400
401 // 4. Service "declared value"
402 $dvPrice = 0;
403 if($declaredValue)
404 {
405 $dvTarif = floatval(self::getConfValue($arConfig, 'service_'.self::$SERVICES['DECLARED_VALUE'][self::$TARIF_IDX].'_value'));
406 $dvPrice += ($arPackage['PRICE'])*$dvTarif;
407 $arDebug[] = 'Declared value: '.$dvPrice;
408 $totalPrice += $dvPrice;
409 }
410
411 $arDebug[] = 'Total value: '.$totalPrice;
412 return $totalPrice;
413 }
414
415 protected static function getRegionCodeByOldName($regionLangName)
416 {
417 if($regionLangName == '')
418 return "";
419
420 static $data = array();
421
422 if(empty($data))
423 {
424 require_once(__DIR__.'/rus_post/old_loc_to_codes.php');
426 }
427
428 return isset($data[$regionLangName]) ? $data[$regionLangName] : "";
429 }
430
431 public static function getAdminMessage()
432 {
433 return array(
434 'MESSAGE' => GetMessage(
435 'SALE_DH_RPF_DEPRECATED_MESSAGE',
436 array(
437 '#A1#' => '<a href="/bitrix/admin/sale_delivery_service_edit.php?lang='.LANGUAGE_ID.'&PARENT_ID=0&CLASS_NAME=%5CSale%5CHandlers%5CDelivery%5CAdditionalHandler&SERVICE_TYPE=RUSPOST">',
438 '#A2#' => '</a>'
439 )
440 ),
441 "TYPE" => "ERROR",
442 "HTML" => true
443 );
444 }
445}
446
447AddEventHandler('sale', 'onSaleDeliveryHandlersBuildList', array('CDeliveryRusPostFirst', 'Init'));
448
449?>
const BX_ROOT
Определения bx_root.php:3
$arResult
Определения generate_coupon.php:16
static checkDimensions($arOrderDimensions, $arRestrictDimensions)
Определения delivery_handler.php:1360
Определения delivery_rus_post_first.php:15
static Compability($arOrder, $arConfig)
Определения delivery_rus_post_first.php:254
static GetConfig($siteId=false)
Определения delivery_rus_post_first.php:81
static Calculate($profile, $arConfig, $arOrder, $STEP, $TEMP=false)
Определения delivery_rus_post_first.php:221
static getAdminMessage()
Определения delivery_rus_post_first.php:431
static SetSettings($arSettings)
Определения delivery_rus_post_first.php:189
static getTarifsByRegionFromCsv(array $arShopLocation)
Определения delivery_rus_post_first.php:306
static getRegionCodeByOldName($regionLangName)
Определения delivery_rus_post_first.php:415
static GetSettings($strSettings)
Определения delivery_rus_post_first.php:170
static Init()
Определения delivery_rus_post_first.php:31
static getTarifNumFromCsv(array $arShopLocation)
Определения delivery_rus_post_first.php:274
static GetFeatures($arConfig)
Определения delivery_rus_post_first.php:205
static IncludeModule($module_name)
Определения module.php:151
static makeBoxConfig($boxId, $arBox, $group, &$arConfig)
Определения delivery_helper.php:390
static getRequiredPacks(&$arItems, $arPacks, $maxWeight, $reservedSpace=0.15)
Определения delivery_helper.php:204
static getConfValue($arField)
Определения delivery_helper.php:148
static getBoxesFromConfig($profile, &$arConfig)
Определения delivery_helper.php:447
static getCsvObject($filePath)
Определения helper.php:379
static getLocationByIdHitCached($id)
Определения helper.php:462
static getShopLocationId($siteId)
Определения helper.php:334
static getOptionOrImportValues($optName, $importFuncName=false, $arFuncParams=array(), $siteId="")
Определения helper.php:261
$data['IS_AVAILABLE']
Определения .description.php:13
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$_REQUEST["admin_mnu_menu_id"]
Определения get_menu.php:8
$result
Определения get_property_values.php:14
$_SERVER["DOCUMENT_ROOT"]
Определения cron_frame.php:9
$STEP
Определения csv_new_setup.php:23
$siteId
Определения ajax.php:8
AddEventHandler($FROM_MODULE_ID, $MESSAGE_ID, $CALLBACK, $SORT=100, $FULL_PATH=false)
Определения tools.php:5165
IncludeModuleLangFile($filepath, $lang=false, $bReturnArray=false)
Определения tools.php:3778
GetMessage($name, $aReplace=null)
Определения tools.php:3397
if(empty($signedUserToken)) $key
Определения quickway.php:257
const DELIVERY_RP_CSV_PATH
Определения delivery_rus_post.php:11
const DELIVERY_RPF_CSV_PATH
Определения delivery_rus_post_first.php:12
</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."%"
Определения waybill.php:936
$locToCode
Определения old_loc_to_codes.php:2
$arRes
Определения options.php:104
$dbRes
Определения yandex_detail.php:168