1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
delivery_ups.php
См. документацию.
1<?php
2/******************************************************************************/
3/* UPS Delivery Handler. Tarifification files can be found at http://ups.com */
4/* Delete ups/*.php files if you change tarification csv files */
5/******************************************************************************/
7
8IncludeModuleLangFile($_SERVER["DOCUMENT_ROOT"].'/bitrix/modules/sale/delivery/delivery_ups.php');
9
10const DELIVERY_UPS_ZONES_PHP_FILE = 'ups/zones.php';
11const DELIVERY_UPS_EXPORT_PHP_FILE = 'ups/export.php';
12
14{
15 public static function Init()
16 {
17 if (\Bitrix\Main\Loader::includeModule('currency') && $arCurrency = CCurrency::GetByID('RUR'))
18 {
19 $base_currency = 'RUR';
20 }
21 else
22 {
23 $base_currency = 'RUB';
24 }
25
26 return array(
27 /* Basic description */
28 "SID" => "ups",
29 "NAME" => GetMessage('SALE_DH_UPS_NAME'),
30 "DESCRIPTION" => GetMessage('SALE_DH_UPS_DESCRIPTION'),
31 "DESCRIPTION_INNER" => GetMessage('SALE_DH_UPS_DESCRIPTION_INNER'),
32 "BASE_CURRENCY" => $base_currency,
33
34 "HANDLER" => __FILE__,
35
36 /* Handler methods */
37 "DBGETSETTINGS" => array("CDeliveryUPS", "GetSettings"),
38 "DBSETSETTINGS" => array("CDeliveryUPS", "SetSettings"),
39 "GETCONFIG" => array("CDeliveryUPS", "GetConfig"),
40
41 "COMPABILITY" => array("CDeliveryUPS", "Compability"),
42 "CALCULATOR" => array("CDeliveryUPS", "Calculate"),
43
44 /* List of delivery profiles */
45 "PROFILES" => array(
46 "express" => array(
47 "TITLE" => GetMessage("SALE_DH_UPS_EXPRESS_TITLE"),
48 "DESCRIPTION" => GetMessage("SALE_DH_UPS_EXPRESS_DESCRIPTION"),
49
50 "RESTRICTIONS_WEIGHT" => array(0, CSaleMeasure::Convert(150, "LBS", "G")),
51 "RESTRICTIONS_SUM" => array(0),
52 ),
53
54 "express_saver" => array(
55 "TITLE" => GetMessage("SALE_DH_UPS_EXPRESS_SAVER_TITLE"),
56 "DESCRIPTION" => GetMessage("SALE_DH_UPS_EXPRESS_SAVER_DESCRIPTION"),
57
58 "RESTRICTIONS_WEIGHT" => array(0, CSaleMeasure::Convert(150, "LBS", "G")),
59 "RESTRICTIONS_SUM" => array(0),
60 ),
61 )
62 );
63 }
64
65 public static function GetConfig()
66 {
67 $arConfig = array(
68 "CONFIG_GROUPS" => array(
69 "tariff_tables" => GetMessage('SALE_DH_UPS_TARIFF_TITLE'),
70 ),
71
72 "CONFIG" => array(
73 "zones_csv" => array(
74 "TYPE" => "TEXT",
75 "TITLE" => GetMessage('SALE_DH_UPS_CONFIG_zones_csv'),
76 "DEFAULT" => "/bitrix/modules/sale/delivery/ups/ru_csv_zones.csv",
77 "GROUP" => "tariff_tables",
78 ),
79
80 "export_csv" => array(
81 "TYPE" => "TEXT",
82 "TITLE" => GetMessage('SALE_DH_UPS_CONFIG_export_csv'),
83 "DEFAULT" => "/bitrix/modules/sale/delivery/ups/ru_csv_export.csv",
84 "GROUP" => "tariff_tables",
85 ),
86 ),
87 );
88 return $arConfig;
89 }
90
91 public static function GetSettings($strSettings)
92 {
93 [$zones_path, $export_path] = explode(";", $strSettings);
94
95 return array(
96 "zones_csv" => $zones_path,
97 "export_csv" => $export_path
98 );
99 }
100
101 public static function SetSettings($arSettings)
102 {
103 return
104 ($arSettings['zones_csv'] ?? '')
105 . ';'
106 . ($arSettings['export_csv'] ?? '')
107 ;
108 }
109
110 public static function __parseZonesFile($file)
111 {
112 $arResult = array();
113
114 $fp = fopen($_SERVER["DOCUMENT_ROOT"].$file, "r");
115 while ($data = fgetcsv($fp, 1000, ","))
116 {
117 if (count($data >= 9) && mb_strlen($data[1]) == 2)
118 {
119 if (mb_substr($data[2], -3) == " EU") $data[2] = mb_substr($data[2], 0, -3);
120
121 $arResult[$data[1]] = array(
122 $data[2],
123 intval($data[6]),
124 intval($data[8])
125 );
126 }
127 }
128
129 fclose($fp);
130
131 $data_file = __DIR__."/".DELIVERY_UPS_ZONES_PHP_FILE;
132
133 if ($fp = fopen($data_file, "w"))
134 {
135 fwrite($fp, '<'.'?'."\r\n");
136 fwrite($fp, '$arUPSZones = array('."\r\n");
137
138 foreach ($arResult as $key => $arRow)
139 {
140 fwrite($fp, '"'.$key.'" => array("'.$arRow[0].'", '.intval($arRow[1]).', '.intval($arRow[2]).'),'."\r\n");
141 }
142
143 fwrite($fp, ');'."\r\n");
144 fwrite($fp, '?'.'>');
145 fclose($fp);
146 }
147
148 return $arResult;
149 }
150
151 public static function __parseExportFile($file)
152 {
153 $arResult = array();
154
155 $fp = fopen($_SERVER["DOCUMENT_ROOT"].$file, "r");
156
157 $check = 0;
158 $bSkip = true;
159 $arResult = array();
160 while ($data = fgetcsv($fp, 1000, ","))
161 {
162 if(mb_stristr($data[0], "service option"))
163 {
164 if(mb_stristr($data[1], "express saver"))
165 {
166 $current_profile = "express_saver";
167 }
168 else
169 {
170 $current_profile = "express";
171 }
172
173 $arResult[$current_profile] = array();
174 }
175 elseif(mb_stristr($data[1], 'weight'))
176 {
177 if($check == 0)
178 {
179 $bSkip = true;
180 $check++;
181 }
182 elseif($check == 1)
183 {
184 $bSkip = false;
185 $check = 0;
186 }
187 }
188 elseif(count($data) == 10)
189 {
190 if($bSkip)
191 {
192 continue;
193 }
194 else
195 {
196 foreach($data as $key => $value)
197 {
198 $value = trim($value);
199 $value = str_replace(".", '', $value);
200 $value = str_replace(",", '.', $value);
201 $data[$key] = $value;
202 }
203
204 if(doubleval($data[1]) <= 0)
205 {
206 $bSkip = true;
207 continue;
208 }
209
210 $arResult[$current_profile][] = $data;
211 }
212 }
213 }
214
215 fclose($fp);
216
217 $arFinalResult = array();
218
219 foreach ($arResult as $profile_id => $arProfileResult)
220 {
221 $arFinalResult[$profile_id] = array();
222
223 foreach ($arProfileResult as $key => $arWeightValues)
224 {
225 array_shift($arWeightValues);
226 $weight_value = $arWeightValues[0];
227 unset($arWeightValues[0]);
228
229 $arFinalResult[$profile_id][$weight_value] = $arWeightValues;
230 }
231 }
232
233 $data_file = __DIR__."/".DELIVERY_UPS_EXPORT_PHP_FILE;
234
235 if ($fp = fopen($data_file, "w"))
236 {
237 fwrite($fp, '<'.'?'."\r\n");
238 fwrite($fp, '$arUPSExport = array('."\r\n");
239
240 foreach ($arFinalResult as $profile => $arWeightValues)
241 {
242 fwrite($fp, '"'.$profile.'" => array('."\r\n");
243
244 foreach ($arWeightValues as $weight => $arZoneValues)
245 {
246 fwrite($fp, '"'.$weight.'" => array(');
247
248 foreach ($arZoneValues as $zone => $value)
249 {
250 fwrite($fp, $zone.' => '.$value.', ');
251 }
252
253 fwrite($fp, '),'."\r\n");
254 }
255
256 fwrite($fp, '),'."\r\n");
257 }
258
259 fwrite($fp, ');'."\r\n");
260 fwrite($fp, '?'.'>');
261 fclose($fp);
262 }
263
264 return $arFinalResult;
265 }
266
267 public static function __GetZones($file)
268 {
269 static $arUPSZones;
270
271 if (is_array($arUPSZones)) return $arUPSZones;
272
273 if (file_exists(__DIR__."/".DELIVERY_UPS_ZONES_PHP_FILE))
275
276 if (!is_array($arUPSZones) || count($arUPSZones) <= 0)
278
279 return $arUPSZones;
280 }
281
282 public static function __GetExport($file)
283 {
284 static $arUPSExport;
285
286 if (is_array($arUPSExport)) return $arUPSExport;
287
288 if (file_exists(__DIR__."/".DELIVERY_UPS_EXPORT_PHP_FILE))
290
291 if (!is_array($arUPSExport) || count($arUPSExport) <= 0)
293
294 return $arUPSExport;
295 }
296
297
298 public static function __GetLocation(&$arLocation, $arConfig)
299 {
300 $zones_file = $arConfig["zones_csv"]["VALUE"];
301 $arZones = CDeliveryUPS::__GetZones($zones_file);
302
303 foreach ($arZones as $country_id => $arZone)
304 {
305 if (
306 ($arLocation["COUNTRY_NAME_ORIG"] && mb_stristr($arZone[0], $arLocation["COUNTRY_NAME_ORIG"]) !== false)
307 || ($arLocation["COUNTRY_SHORT_NAME"] && mb_stristr($arZone[0], $arLocation["COUNTRY_SHORT_NAME"]) !== false)
308 || ($arLocation["COUNTRY_NAME_LANG"] && mb_stristr($arZone[0], $arLocation["COUNTRY_NAME_LANG"]) !== false)
309 || ($arLocation["COUNTRY_NAME_ORIG"] && mb_stristr($arLocation["COUNTRY_NAME_ORIG"], $arZone[0]) !== false)
310 || ($arLocation["COUNTRY_SHORT_NAME"] && mb_stristr($arLocation["COUNTRY_SHORT_NAME"], $arZone[0]) !== false)
311 || ($arLocation["COUNTRY_NAME_LANG"] && mb_stristr($arLocation["COUNTRY_NAME_LANG"], $arZone[0]) !== false)
312 )
313 {
314 $arLocation["COUNTRY_SID"] = $country_id;
315 break;
316 }
317 }
318 }
319
320
321 public static function Calculate($profile, $arConfig, $arOrder, $STEP, $TEMP = false)
322 {
323 $arOrder["WEIGHT"] = CSaleMeasure::Convert($arOrder["WEIGHT"], "G", "KG");
324
325 $arLocationTo = CSaleLocation::GetByID($arOrder["LOCATION_TO"]);
326
327 if (LANGUAGE_ID !== 'en')
328 {
329 $arCountry = CSaleLocation::GetCountryLangByID($arLocationTo['COUNTRY_ID'], 'en');
330 if (false !== $arCountry)
331 $arLocationTo['COUNTRY_NAME_LANG'] = $arCountry['NAME'];
332 }
333
334 CDeliveryUPS::__GetLocation($arLocationTo, $arConfig);
335
336 $arPriceTable = CDeliveryUPS::__GetExport($arConfig["export_csv"]["VALUE"]);
337 $zones_file = $arConfig["zones_csv"]["VALUE"];
338 $arZones = CDeliveryUPS::__GetZones($zones_file);
339
340 reset($arPriceTable);
341 do
342 {
343 $key = key($arPriceTable[$profile]);
344 next($arPriceTable[$profile]);
345 }
346 while ($key && (doubleval($arOrder["WEIGHT"]) > doubleval($key)));
347
348 $zone = $arZones[$arLocationTo["COUNTRY_SID"]][$profile == "express_saver" ? 1 : 2];
349
350 $sum = $arPriceTable[$profile][$key][$zone];
351
352 return array(
353 "RESULT" => "OK",
354 "VALUE" => $sum
355 );
356 }
357
358
359 public static function Compability($arOrder, $arConfig)
360 {
361 if (intval($arOrder["LOCATION_FROM"]) <= 0)
362 return array();
363
364 $arLocationFrom = CSaleLocation::GetByID($arOrder["LOCATION_FROM"]);
365 $arLocationTo = CSaleLocation::GetByID($arOrder["LOCATION_TO"]);
366
367 if ($arLocationFrom === false || $arLocationTo === false)
368 {
369 return [];
370 }
371
372 if ($arLocationFrom["COUNTRY_ID"] == $arLocationTo["COUNTRY_ID"])
373 return array();
374
375 if (LANGUAGE_ID !== 'en')
376 {
377 $arCountry = CSaleLocation::GetCountryLangByID($arLocationTo['COUNTRY_ID'], 'en');
378 if (false !== $arCountry)
379 $arLocationTo['COUNTRY_NAME_LANG'] = $arCountry['NAME'];
380 }
381
382 CDeliveryUPS::__GetLocation($arLocationTo, $arConfig);
383
384 if ($arLocationTo["COUNTRY_SID"] == '')
385 return array();
386
387 $zones_file = $arConfig["zones_csv"]["VALUE"];
388 $arZones = CDeliveryUPS::__GetZones($zones_file);
389
390 $arZoneTo = $arZones[$arLocationTo["COUNTRY_SID"]];
391
392 if (intval($arZoneTo[1]) > 0)
393 return array("express", "express_saver");
394 else
395 return array("express");
396 }
397}
398
399AddEventHandler("sale", "onSaleDeliveryHandlersBuildList", array('CDeliveryUPS', 'Init'));
$sum
Определения checkout.php:6
$arResult
Определения generate_coupon.php:16
static GetByID($currency)
Определения currency.php:453
static GetCountryLangByID($ID, $strLang=LANGUAGE_ID)
Определения location.php:1172
Определения delivery_ups.php:14
static __GetZones($file)
Определения delivery_ups.php:267
static Compability($arOrder, $arConfig)
Определения delivery_ups.php:359
static Calculate($profile, $arConfig, $arOrder, $STEP, $TEMP=false)
Определения delivery_ups.php:321
static SetSettings($arSettings)
Определения delivery_ups.php:101
static GetConfig()
Определения delivery_ups.php:65
static __GetExport($file)
Определения delivery_ups.php:282
static GetSettings($strSettings)
Определения delivery_ups.php:91
static Init()
Определения delivery_ups.php:15
static __GetLocation(&$arLocation, $arConfig)
Определения delivery_ups.php:298
static __parseZonesFile($file)
Определения delivery_ups.php:110
static __parseExportFile($file)
Определения delivery_ups.php:151
static IncludeModule($module_name)
Определения module.php:151
static GetByID($primary, $strLang=LANGUAGE_ID)
Определения location.php:118
static Convert($value, $measureFrom, $measureTo="G")
Определения measurement.php:37
$base_currency
Определения commerceml_g_run_cur.php:2
$data['IS_AVAILABLE']
Определения .description.php:13
const DELIVERY_UPS_EXPORT_PHP_FILE
Определения delivery_ups.php:11
const DELIVERY_UPS_ZONES_PHP_FILE
Определения delivery_ups.php:10
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$_SERVER["DOCUMENT_ROOT"]
Определения cron_frame.php:9
if(!\Bitrix\Main\Loader::includeModule('catalog')) $profile_id
Определения cron_frame.php:43
$STEP
Определения csv_new_setup.php:23
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( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
if(empty($signedUserToken)) $key
Определения quickway.php:257
</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
$arUPSExport
Определения export.php:2
$arLocation['REGION_NAME']
Определения options.php:2800
$arUPSZones
Определения zones.php:2