1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
location.php
См. документацию.
1<?
2namespace Sale\Handlers\Delivery\Additional;
3
4use Bitrix\Main\Application;
5use Bitrix\Main\Error;
6use Bitrix\Main\Loader;
7use Bitrix\Main\Text\Encoding;
8use Bitrix\Main\Web\HttpClient;
9use Bitrix\Sale\Result;
10use Bitrix\Main\IO\File;
11use Bitrix\Main\Localization\Loc;
12use Bitrix\Sale\Delivery\ExternalLocationMap;
13
14Loc::loadMessages(__FILE__);
15
23class Location extends ExternalLocationMap
24{
25 const EXTERNAL_SERVICE_CODE = 'ADD_DLV';
26 //public path
27 const ETHALON_LOCATIONS_PATH = '/bitrix/services/saleservices/locations.zip';
28
29 public static function compareStepless()
30 {
31 set_time_limit(0);
32 $result = new Result();
33
34 $csvFilePath = self::getLocationsFilePath();
35
36 if($csvFilePath == '')
37 {
38 $result->addError(new Error(Loc::getMessage('SALE_DLVRS_ADDL_LOCATIONS_ERROR')));
39 return $result;
40 }
41
42 $tmpImported = static::saveCsvToTmpTable($csvFilePath);
43
44 if($tmpImported <= 0)
45 {
46 $result->addError(new Error(Loc::getMessage('SALE_DLVRS_ADDL_LOCATIONS_ERROR')));
47 return $result;
48 }
49
50 $srvId = self::getExternalServiceId();
51
52 if(intval($srvId) <=0)
53 return $result;
54
55 self::updateLinksInfo($srvId);
56 self::mapByCodes($srvId);
57 self::fillNormalizedTable();
58 self::mapByNames($srvId);
59 return $result;
60 }
61
62 public static function compare($stage, $step = '', $progress = 0, $timeout = 0)
63 {
64 $result = new Result();
65 set_time_limit(0);
66 $srvId = self::getExternalServiceId();
67
68 if(intval($srvId) <=0)
69 {
70 $result->addError(new Error(Loc::getMessage('SALE_DLVRS_ADDL_LOCATIONS_ERROR_SID')));
71 return $result;
72 }
73
74 switch($stage)
75 {
76 case 'start':
77
78 $csvFilePath = self::getLocationsFilePath();
79
80 if($csvFilePath == '')
81 {
82 $result->addError(new Error(Loc::getMessage('SALE_DLVRS_ADDL_LOCATIONS_ERROR')));
83 return $result;
84 }
85
86 $res = \Bitrix\Sale\Location\LocationTable::getList(array(
87 'runtime' => array(new \Bitrix\Main\Entity\ExpressionField('MAX', 'MAX(ID)')),
88 'select' => array('MAX')
89 ));
90
91 if($loc = $res->fetch())
92 {
93 $_SESSION['SALE_HNDL_ADD_DLV_LOC_MAX_ID'] = (int)$loc['MAX'];
94 }
95 else
96 {
97 $_SESSION['SALE_HNDL_ADD_DLV_LOC_MAX_ID'] = 0;
98 }
99
100 $result->setData(array(
101 'STAGE' => 'create_ethalon_loc_tmp_table',
102 'MESSAGE' => Loc::getMessage('SALE_DLVRS_ADDL_LOCATIONS_CREATE_TMP_TABLE'),
103 'STEP' => $csvFilePath,
104 'PROGRESS' => $progress + 5
105 ));
106
107 break;
108
109 case 'create_ethalon_loc_tmp_table':
110
111 $csvFilePath = !empty($step) ? $step : '';
112
113 if($csvFilePath == '')
114 {
115 $result->addError(new Error(Loc::getMessage('SALE_DLVRS_ADDL_LOCATIONS_ERROR_PATH')));
116 return $result;
117 }
118
119 $tmpImported = static::saveCsvToTmpTable($csvFilePath);
120
121 if($tmpImported <= 0)
122 {
123 $result->addError(new Error(Loc::getMessage('SALE_DLVRS_ADDL_LOCATIONS_ERROR_TMP_TABLE')));
124 return $result;
125 }
126
127 $_SESSION['SALE_HNDL_ADD_DLV_ETH_LOC_LAST'] = self::getLastEthalonLoc();
128
129 $result->setData(array(
130 'STAGE' => 'update_links_info',
131 'MESSAGE' => Loc::getMessage('SALE_DLVRS_ADDL_LOCATIONS_CHECK_COMPARED'),
132 'PROGRESS' => $progress + 5
133 ));
134
135 break;
136
137 case 'update_links_info':
138
139 self::updateLinksInfo($srvId);
140 $result->setData(array(
141 'STAGE' => 'map_by_codes',
142 'MESSAGE' => Loc::getMessage('SALE_DLVRS_ADDL_LOCATIONS_COMP_BY_CODES'),
143 'PROGRESS' => $progress + 5
144 ));
145 break;
146
147 case 'map_by_codes':
148
149 self::mapByCodes($srvId);
150 $result->setData(array(
151 'STAGE' => 'create_normalized_loc_table',
152 'MESSAGE' => Loc::getMessage('SALE_DLVRS_ADDL_LOCATIONS_NORM'),
153 'PROGRESS' => $progress + 5
154 ));
155
156 break;
157
158 case 'create_normalized_loc_table':
159
160 $lastId = self::fillNormalizedTable((int)$step, $timeout);
161
162 if($lastId > 0 && $lastId < $_SESSION['SALE_HNDL_ADD_DLV_LOC_MAX_ID'])
163 {
164 $result->setData(array(
165 'STAGE' => 'create_normalized_loc_table',
166 'STEP' => $lastId,
167 'MESSAGE' => Loc::getMessage('SALE_DLVRS_ADDL_LOCATIONS_NORM'),
168 'PROGRESS' => $progress <= 25 ? $progress + 1 : $progress
169 ));
170 }
171 else
172 {
173 $result->setData(array(
174 'STAGE' => 'map_by_names',
175 'MESSAGE' => Loc::getMessage('SALE_DLVRS_ADDL_LOCATIONS_COMP_BY_NAMES'),
176 'PROGRESS' => $progress + 5
177 ));
178 }
179
180 break;
181
182 case 'map_by_names':
183
184 $lastProcessedId = self::mapByNames($srvId, $step, $timeout);
185
186 if($_SESSION['SALE_HNDL_ADD_DLV_ETH_LOC_LAST'] <= 0)
187 $progress = $progress <= 90 ? $progress + 1 : 90;
188 elseif($lastProcessedId <= 0 || $lastProcessedId == $_SESSION['SALE_HNDL_ADD_DLV_ETH_LOC_LAST'])
189 $progress = 100;
190 else
191 $progress = 32 + round(60 * $lastProcessedId / $_SESSION['SALE_HNDL_ADD_DLV_ETH_LOC_LAST']);
192
193 if($progress < 100)
194 {
195 $result->setData(array(
196 'STAGE' => 'map_by_names',
197 'STEP' => $lastProcessedId,
198 'MESSAGE' => Loc::getMessage('SALE_DLVRS_ADDL_LOCATIONS_COMP_BY_NAMES'),
199 'PROGRESS' => $progress
200 ));
201 }
202 else
203 {
204 $result->setData(array(
205 'STAGE' => 'finish',
206 'MESSAGE' => Loc::getMessage('SALE_DLVRS_ADDL_LOCATIONS_COMP_COMPLETE'),
207 'PROGRESS' => 100
208 ));
209 }
210
211 break;
212
213 default:
214 $result->addError(new Error(Loc::getMessage('SALE_DLVRS_ADDL_LOCATIONS_ERROR_STAGE')));
215 }
216
217 return $result;
218 }
219
220 protected static function getLastEthalonLoc()
221 {
222 $result = 0;
223
224 $con = \Bitrix\Main\Application::getConnection();
225 $res = $con->query("SELECT MAX(ID) AS MAX FROM b_sale_hdale");
226
227 if($loc = $res->fetch())
228 $result = $loc['MAX'];
229
230 return $result;
231 }
232
233 protected static function getLocationsFilePath()
234 {
235 $archiveFileName = self::downloadLocations();
236
237 if($archiveFileName == '')
238 return '';
239
240 return self::unpackLocations($archiveFileName);
241 }
242
243 protected static function getReplacementClass()
244 {
245 $result = null;
246
247 $replacementPath = Application::getDocumentRoot().
248 '/bitrix/modules/sale/handlers/delivery/additional/location/'.
249 LANGUAGE_ID.'/replacement.php';
250
251 if(file_exists($replacementPath))
252 {
253 require_once($replacementPath);
254
255 if(class_exists('\Sale\Handlers\Delivery\Additional\Location\Replacement'))
256 {
257 $result = '\Sale\Handlers\Delivery\Additional\Location\Replacement';
258 }
259 }
260
261 return $result;
262 }
263
264 protected static function getCountryName()
265 {
266 $result = '';
268 $relpacementClass = static::getReplacementClass();
269
270 if($relpacementClass)
271 {
272 $result = $relpacementClass::getCountryName();
273 }
274
275 return $result;
276 }
277
278 protected static function mapByNames($srvId, $startId = 0, $timeout = 0)
279 {
280 $countryName = self::getCountryName();
281
282 if($countryName == '')
283 {
284 return 0;
285 }
286
287 $startTime = mktime(true);
288 $con = \Bitrix\Main\Application::getConnection();
289 $sqlHelper = $con->getSqlHelper();
290 $imported = 0;
291
292 $query = "
293 SELECT
294 TMP.*
295 FROM
296 b_sale_hdale AS TMP
297 WHERE
298 TMP.LOCATION_EXT_ID IS NULL
299 AND TMP.PCOUNTRY = '".$sqlHelper->forSql(
300 \Sale\Handlers\Delivery\Additional\Location\Replacement::getNameRussia()
301 )."'
302 AND TMP.LOCATION_EXT_ID IS NULL
303 ";
304
305 if(intval($startId) > 0)
306 {
307 $query .= " AND TMP.ID > ".$sqlHelper->forSql(intval($startId));
308 }
309
310 $dbRes = $con->query($query);
311
312 $lastLocationId = 0;
313
314 while ($ethLoc = $dbRes->fetch())
315 {
316 $lastLocationId = (int)$ethLoc['ID'];
317 $locationId = self::getLocationIdByNames($ethLoc['NAME'], $ethLoc['PCITY'], $ethLoc['PSUBREGION'], $ethLoc['PREGION'], $ethLoc['PCOUNTRY'], true);
318
319 if(!$locationId)
320 $locationId = self::getLocationIdByNames($ethLoc['NAME'], $ethLoc['PCITY'], $ethLoc['PSUBREGION'], $ethLoc['PREGION'], $ethLoc['PCOUNTRY'], false);
321
322 if(intval($locationId) > 0)
323 {
324 $res = self::setExternalLocation($srvId, $locationId, $ethLoc['CODE']);
325
326 if($res)
327 $imported++;
328 }
329
330 if ($timeout > 0 && (mktime(true)-$startTime) >= $timeout)
331 {
332 return $lastLocationId;
333 }
334 }
335
336 return $lastLocationId;
337 }
338
339 protected static function mapByCodes($srvId)
340 {
341 $con = \Bitrix\Main\Application::getConnection();
342 $sqlHelper = $con->getSqlHelper();
343
344 $con->queryExecute("
345 INSERT INTO
346 b_sale_loc_ext (SERVICE_ID, LOCATION_ID, XML_ID)
347 SELECT
348 ".$sqlHelper->forSql($srvId).", TMP.LOCATION_ID, TMP.CODE
349 FROM
350 b_sale_hdale AS TMP
351 WHERE
352 TMP.LOCATION_ID > 0 AND TMP.LOCATION_EXT_ID IS NULL
353 ");
354 }
355
356 protected static function updateLinksInfo($srvId)
357 {
358 $con = \Bitrix\Main\Application::getConnection();
359 $sqlHelper = $con->getSqlHelper();
360
361 $con->queryExecute("
362 UPDATE
363 b_sale_hdale AS TMP
364 INNER JOIN
365 b_sale_location AS L ON TMP.CODE = L.CODE
366 SET
367 TMP.LOCATION_ID = L.ID
368 ");
369
370 $con->queryExecute("
371 UPDATE
372 b_sale_hdale AS TMP
373 INNER JOIN
374 b_sale_loc_ext AS E ON TMP.CODE = E.XML_ID AND E.SERVICE_ID = ".$sqlHelper->forSql($srvId)."
375 SET
376 TMP.LOCATION_EXT_ID = E.ID
377 ");
378 }
379
380 protected static function saveCsvToTmpTable($path)
381 {
382 if($path == '')
383 return false;
384
385 $srvId = static::getExternalServiceId();
386
387 if($srvId <= 0)
388 return false;
389
390 if(!File::isFileExists($path))
391 return 0;
392
393 set_time_limit(0);
394 $content = File::getFileContents($path);
395
396 if($content === false)
397 return false;
398
399 $lines = explode("\n", $content);
400
401 if(!is_array($lines))
402 return false;
403
404 $con = \Bitrix\Main\Application::getConnection();
405
406 if($con->isIndexExists('b_sale_hdale', array('LOCATION_ID')))
407 $con->queryExecute("DROP INDEX IX_BSHDALE_LOCATION_ID".($con->getType() == "oracle" ? "" : " ON b_sale_hdale"));
408
409 $con->queryExecute("DELETE FROM b_sale_hdale");
410
411 $sqlHelper = $con->getSqlHelper();
412 $imported = 0;
413 $i = 0;
414 $values = '';
415
416 foreach($lines as $line)
417 {
418 $cols = explode(';', $line);
419
420 if(!is_array($cols) || count($cols) != 6)
421 continue;
422
423 if($cols[0] == '' || $cols[1] == '')
424 continue;
425
426 if($values <> '')
427 $values .= ', ';
428
429 $values .= "('".$sqlHelper->forSql($cols[0])."', '".$sqlHelper->forSql($cols[1])."', '".$sqlHelper->forSql($cols[2])."', '".$sqlHelper->forSql($cols[3])."', '".$sqlHelper->forSql($cols[4])."', '".$sqlHelper->forSql($cols[5])."', ".($imported+1).")";
430
431 if($i >= 100)
432 {
433 $con->queryExecute("INSERT INTO b_sale_hdale(CODE, NAME, PCITY, PSUBREGION, PREGION, PCOUNTRY, ID) VALUES ".$values);
434 $i = 0;
435 $values = '';
436 }
437
438 $imported++;
439 $i++;
440 }
441
442 if($values <> '')
443 $con->queryExecute("INSERT INTO b_sale_hdale(CODE, NAME, PCITY, PSUBREGION, PREGION, PCOUNTRY, ID) VALUES ".$values);
444
445 $con->queryExecute("CREATE INDEX IX_BSHDALE_LOCATION_ID ON b_sale_hdale(LOCATION_ID)");
446
447 return $imported;
448 }
449
450 protected static function unpackLocations($archivePath)
451 {
452 $sUnpackDir = \CTempFile::GetDirectoryName(24);
453 $fileUnpackPath = $sUnpackDir.'locations.csv';
454 CheckDirPath($sUnpackDir);
455 $oArchiver = \CBXArchive::GetArchive($archivePath, "ZIP");
456 $oArchiver->SetOptions(array("STEP_TIME" => 300));
457 $res = $oArchiver->Unpack($sUnpackDir);
458 unlink($archivePath);
459
460 if(!$res || !file_exists($fileUnpackPath))
461 return '';
462
463 return $fileUnpackPath;
464 }
465
466 protected static function downloadLocations()
467 {
468 $result = '';
469 $client = new \Sale\Handlers\Delivery\Additional\RestClient();
470 $host = $client->getServiceHost();
471 $downloadUrl = $host.self::ETHALON_LOCATIONS_PATH;
472 $tmpDir = \CTempFile::GetDirectoryName(24);
473 CheckDirPath($tmpDir);
474 $storePath = $tmpDir.'locations.zip';
475 $httpClient = new HttpClient();
476
477 if($httpClient->download($downloadUrl, $storePath))
478 $result = $storePath;
479
480 return $result;
481 }
482}
$path
Определения access_edit.php:21
$con
Определения admin_tab.php:7
$startTime
Определения sync.php:69
if(!\Bitrix\Main\Loader::includeModule('clouds')) $lastId
Определения sync.php:68
$content
Определения commerceml.php:144
</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
$query
Определения get_search.php:11
CheckDirPath($path)
Определения tools.php:2707
$host
Определения mysql_to_pgsql.php:32
trait Error
Определения error.php:11
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$i
Определения factura.php:643
</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
$dbRes
Определения yandex_detail.php:168