1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
location.php
См. документацию.
1<?
2namespace Bitrix\Sale\Delivery\Pecom;
3
4use Bitrix\Main\Error;
5use Bitrix\Main\Loader;
6use Bitrix\Sale\Result;
7use Bitrix\Main\Text\Encoding;
8use Bitrix\Sale\Location\Comparator;
9use Bitrix\Main\ArgumentNullException;
10use Bitrix\Sale\Location\ExternalTable;
11use Bitrix\Sale\Location\LocationTable;
12use Bitrix\Sale\Delivery\ExternalLocationMap;
13
14Loader::registerAutoLoadClasses(
15 'sale',
16 array(
17 'Bitrix\\Sale\\Delivery\\Pecom\\Replacement' => 'ru/delivery/pecom/replacement.php'
18 )
19);
20
26{
27 const EXTERNAL_SERVICE_CODE = 'PECOM';
28 const CSV_FILE_PATH = '/bitrix/modules/sale/ru/delivery/pecom/location.csv';
29
30 public static function compare($startKey = 0, $timeout = 0, $updateExist = false)
31 {
32 set_time_limit(0);
33 $result = new Result();
35
36 if(intval($srvId) <= 0)
37 return $result;
38
40 $res = static::getAllLocations();
41
42 if($res->isSuccess())
43 {
44 $locations = $res->getData();
45
46 if(is_array($locations) && !empty($locations))
47 {
48 $lastKey = static::mapByNames($locations, $srvId, $startKey, $timeout, $updateExist);
49 $result->addData(array(
50 'LAST_KEY' => $lastKey
51 ));
52 }
53 }
54 else
55 {
56 $result->addErrors($res->getErrors());
57 }
58
59 return $result;
60 }
61
72 protected static function mapByNames($locations, $srvId, $startKey = 0, $timeout = 0, $updateExist = false)
73 {
74 if(empty($locations))
75 throw new ArgumentNullException('locations');
76
77 $startTime = mktime(true);
78 $imported = 0;
79 $xmlIdExist = array();
80 $locationIdExist = array();
81
82 if(!$updateExist)
83 {
84 $res = ExternalTable::getList(array(
85 'filter' => array(
86 '=SERVICE_ID' => $srvId
87 )
88 ));
89
90 while($map = $res->fetch())
91 {
92 $xmlIdExist[] = $map['XML_ID'];
93 $locationIdExist[] = $map['LOCATION_ID'];
94 }
95 }
96
97 $key = 0;
98
99 foreach($locations as $key => $location)
100 {
101 $xmlId = $location[self::CITY_XML_ID_IDX];
102
103 if($startKey <= 0 || $key >= $startKey)
104 {
105 if(!in_array($xmlId, $xmlIdExist))
106 {
107 $cityName = $location[static::CITY_NAME_IDX];
108 $districtName = self::extractDistrict($cityName);
109
110 $locationId = static::getLocationIdByNames($cityName, '', $districtName, $location[static::REGION_NAME_IDX], '', true);
111
112 if(!$locationId)
113 $locationId = static::getLocationIdByNames($cityName, '', $districtName,$location[static::REGION_NAME_IDX], '', false);
114
115 if(intval($locationId) > 0 && !in_array($locationId, $locationIdExist))
116 {
117 $res = self::setExternalLocation($srvId, $locationId, $xmlId, $updateExist);
118
119 if($res)
120 $imported++;
121 }
122 }
123 }
124
125 unset($locations[$key]);
126
127 if($timeout > 0 && (mktime(true)-$startTime) >= $timeout)
128 return intval($key);
129 }
130
131 return intval($key) > 0 ? intval($key) : 0;
132 }
133
134 protected static function setMap(array $cities)
135 {
136 self::mapByNames($cities, static::getExternalServiceId());
137 return new Result();
138 }
139
142 protected static function getAllLocations()
143 {
144 $result = new Result();
145 $http = new \Bitrix\Main\Web\HttpClient(array(
146 "version" => "1.1",
147 "socketTimeout" => 30,
148 "streamTimeout" => 30,
149 "redirect" => true,
150 "redirectMax" => 5,
151 "disableSslVerification" => true
152 ));
153
154 $jsnData = $http->get("https://www.pecom.ru/ru/calc/towns.php");
155 $errors = $http->getError();
156
157 if (!$jsnData && !empty($errors))
158 {
159 foreach($errors as $errorCode => $errMes)
160 $result->addError(new Error($errMes, $errorCode));
161
162 return $result;
163 }
164
165 $data = json_decode($jsnData, true);
166
167 if(is_array($data))
168 {
169 // City MOSKVA Region MOSKVA
170 $cityRegionSame = array();
171 //City name contains (temeryazevskiy r-n)
172 $precised = array();
173 $emptyRegions = array();
174 $other = array();
175
176 $regions = self::getParents(array_keys($data));
177
178 foreach($data as $regionCity => $city)
179 {
180 $regionCity = mb_strtoupper($regionCity);
181 $regionName = !empty($regions[$regionCity]['REGION']) ? $regions[$regionCity]['REGION'] : '';
182
183 if($regionName == '')
184 {
185 foreach(Replacement::getRegionExceptions() as $cName => $rName)
186 {
187 if($regionCity == $cName)
188 {
189 $regionName = $rName;
190 break;
191 }
192 }
193 }
194
195 foreach($city as $cityId => $cityName)
196 {
197 $cityName = mb_strtoupper($cityName);
198 $location = array($cityName, $regionName, $cityId);
199
200 if(mb_strpos($cityName, '(') !== false && mb_strpos($cityName, ')') !== false)
201 $precised[] = $location;
202 elseif($cityName == $regionCity)
203 $cityRegionSame[] = $location;
204 elseif($regionCity == '' || $regionCity == '-' || $regionCity == '--')
205 $emptyRegions = $location;
206 else
207 $other[] = $location;
208 }
209 }
210
211 $result->addData(
212 array_merge(
213 $precised,
214 $other,
215 $cityRegionSame,
216 $emptyRegions
217 )
218 );
219 }
220 else
221 {
222 $result->addError(new Error("Can decode pecom cities data!"));
223 }
224
225 return $result;
226 }
227
234 protected static function getParents(array $cityNames)
235 {
236 if(empty($cityNames))
237 return array();
238
239 $result = array();
240
241 foreach($cityNames as $key => $name)
242 $cityNames[$key] = mb_strtoupper($name);
243
244 $res = LocationTable::getList(array(
245 'filter' => array(
246 '=NAME.NAME_UPPER' => $cityNames,
247 '=NAME.LANGUAGE_ID' => LANGUAGE_ID,
248 '=PARENTS.NAME.LANGUAGE_ID' => LANGUAGE_ID
249 ),
250 'select' => array(
251 'NAME_UPPER' => 'NAME.NAME_UPPER',
252 'PARENTS_TYPE_CODE' => 'PARENTS.TYPE.CODE' ,
253 'PARENTS_NAME_UPPER' => 'PARENTS.NAME.NAME_UPPER'
254 )
255 ));
256
257 while($loc = $res->fetch())
258 {
259 if(!isset($result[$loc['NAME_UPPER']]))
260 $result[$loc['NAME_UPPER']] = array();
261
262 $result[$loc['NAME_UPPER']][$loc['PARENTS_TYPE_CODE']] = $loc['PARENTS_NAME_UPPER'];
263 }
264
265 return $result;
266 }
267
273 protected static function extractDistrict(&$cityName)
274 {
275 $result = '';
276 $matches = array();
277
278 if(preg_match('/(.*)\s*\‍((.*)\‍)/iu', $cityName, $matches))
279 {
280 if(!empty($matches[2]))
281 {
282 $result = trim($matches[2]);
284
285 if(!preg_match('/('.$mark.'){1}/iu', $result))
286 $result = '';
287
288 if(!empty($matches[1]))
289 $cityName = trim($matches[1]);
290 }
291 }
292
293 return $result;
294 }
295}
Определения error.php:15
static setExternalLocation($srvId, $locationId, $xmlId, $updateExist=false)
Определения externallocationmap.php:555
static fillNormalizedTable($startId=false, $timeout=0)
Определения externallocationmap.php:580
const EXTERNAL_SERVICE_CODE
Определения location.php:27
static getAllLocations()
Определения location.php:142
static extractDistrict(&$cityName)
Определения location.php:273
static compare($startKey=0, $timeout=0, $updateExist=false)
Определения location.php:30
static getParents(array $cityNames)
Определения location.php:234
static mapByNames($locations, $srvId, $startKey=0, $timeout=0, $updateExist=false)
Определения location.php:72
static setMap(array $cities)
Определения location.php:134
static getRegionExceptions()
Определения replacement.php:6
$startTime
Определения sync.php:69
$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
$res
Определения filter_act.php:7
$result
Определения get_property_values.php:14
$errors
Определения iblock_catalog_edit.php:74
$name
Определения menu_edit.php:35
$map
Определения config.php:5
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
if(empty($signedUserToken)) $key
Определения quickway.php:257
$location
Определения options.php:2729
$matches
Определения index.php:22