2namespace Sale\Handlers\Delivery\Additional;
4use Bitrix\Main\Application;
7use Bitrix\Main\Text\Encoding;
8use Bitrix\Main\Web\HttpClient;
10use Bitrix\Main\IO\File;
11use Bitrix\Main\Localization\Loc;
12use Bitrix\Sale\Delivery\ExternalLocationMap;
14Loc::loadMessages(__FILE__);
27 const ETHALON_LOCATIONS_PATH =
'/bitrix/services/saleservices/locations.zip';
29 public static function compareStepless()
34 $csvFilePath = self::getLocationsFilePath();
36 if($csvFilePath ==
'')
38 $result->addError(
new Error(Loc::getMessage(
'SALE_DLVRS_ADDL_LOCATIONS_ERROR')));
42 $tmpImported = static::saveCsvToTmpTable($csvFilePath);
46 $result->addError(
new Error(Loc::getMessage(
'SALE_DLVRS_ADDL_LOCATIONS_ERROR')));
50 $srvId = self::getExternalServiceId();
52 if(intval($srvId) <=0)
55 self::updateLinksInfo($srvId);
56 self::mapByCodes($srvId);
57 self::fillNormalizedTable();
58 self::mapByNames($srvId);
62 public static function compare($stage, $step =
'', $progress = 0, $timeout = 0)
66 $srvId = self::getExternalServiceId();
68 if(intval($srvId) <=0)
70 $result->addError(
new Error(Loc::getMessage(
'SALE_DLVRS_ADDL_LOCATIONS_ERROR_SID')));
78 $csvFilePath = self::getLocationsFilePath();
80 if($csvFilePath ==
'')
82 $result->addError(
new Error(Loc::getMessage(
'SALE_DLVRS_ADDL_LOCATIONS_ERROR')));
86 $res = \Bitrix\Sale\Location\LocationTable::getList(
array(
87 'runtime' =>
array(
new \Bitrix\Main\Entity\ExpressionField(
'MAX',
'MAX(ID)')),
88 'select' =>
array(
'MAX')
91 if($loc =
$res->fetch())
93 $_SESSION[
'SALE_HNDL_ADD_DLV_LOC_MAX_ID'] = (int)$loc[
'MAX'];
97 $_SESSION[
'SALE_HNDL_ADD_DLV_LOC_MAX_ID'] = 0;
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
109 case 'create_ethalon_loc_tmp_table':
111 $csvFilePath = !empty($step) ? $step :
'';
113 if($csvFilePath ==
'')
115 $result->addError(
new Error(Loc::getMessage(
'SALE_DLVRS_ADDL_LOCATIONS_ERROR_PATH')));
119 $tmpImported = static::saveCsvToTmpTable($csvFilePath);
121 if($tmpImported <= 0)
123 $result->addError(
new Error(Loc::getMessage(
'SALE_DLVRS_ADDL_LOCATIONS_ERROR_TMP_TABLE')));
127 $_SESSION[
'SALE_HNDL_ADD_DLV_ETH_LOC_LAST'] = self::getLastEthalonLoc();
130 'STAGE' =>
'update_links_info',
131 'MESSAGE' => Loc::getMessage(
'SALE_DLVRS_ADDL_LOCATIONS_CHECK_COMPARED'),
132 'PROGRESS' => $progress + 5
137 case 'update_links_info':
139 self::updateLinksInfo($srvId);
141 'STAGE' =>
'map_by_codes',
142 'MESSAGE' => Loc::getMessage(
'SALE_DLVRS_ADDL_LOCATIONS_COMP_BY_CODES'),
143 'PROGRESS' => $progress + 5
149 self::mapByCodes($srvId);
151 'STAGE' =>
'create_normalized_loc_table',
152 'MESSAGE' => Loc::getMessage(
'SALE_DLVRS_ADDL_LOCATIONS_NORM'),
153 'PROGRESS' => $progress + 5
158 case 'create_normalized_loc_table':
160 $lastId = self::fillNormalizedTable((
int)$step, $timeout);
162 if(
$lastId > 0 &&
$lastId < $_SESSION[
'SALE_HNDL_ADD_DLV_LOC_MAX_ID'])
165 'STAGE' =>
'create_normalized_loc_table',
167 'MESSAGE' => Loc::getMessage(
'SALE_DLVRS_ADDL_LOCATIONS_NORM'),
168 'PROGRESS' => $progress <= 25 ? $progress + 1 : $progress
174 'STAGE' =>
'map_by_names',
175 'MESSAGE' => Loc::getMessage(
'SALE_DLVRS_ADDL_LOCATIONS_COMP_BY_NAMES'),
176 'PROGRESS' => $progress + 5
184 $lastProcessedId = self::mapByNames($srvId, $step, $timeout);
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'])
191 $progress = 32 + round(60 * $lastProcessedId / $_SESSION[
'SALE_HNDL_ADD_DLV_ETH_LOC_LAST']);
196 'STAGE' =>
'map_by_names',
197 'STEP' => $lastProcessedId,
198 'MESSAGE' => Loc::getMessage(
'SALE_DLVRS_ADDL_LOCATIONS_COMP_BY_NAMES'),
199 'PROGRESS' => $progress
206 'MESSAGE' => Loc::getMessage(
'SALE_DLVRS_ADDL_LOCATIONS_COMP_COMPLETE'),
214 $result->addError(
new Error(Loc::getMessage(
'SALE_DLVRS_ADDL_LOCATIONS_ERROR_STAGE')));
220 protected static function getLastEthalonLoc()
224 $con = \Bitrix\Main\Application::getConnection();
225 $res =
$con->query(
"SELECT MAX(ID) AS MAX FROM b_sale_hdale");
227 if($loc =
$res->fetch())
233 protected static function getLocationsFilePath()
235 $archiveFileName = self::downloadLocations();
237 if($archiveFileName ==
'')
240 return self::unpackLocations($archiveFileName);
243 protected static function getReplacementClass()
247 $replacementPath = Application::getDocumentRoot().
248 '/bitrix/modules/sale/handlers/delivery/additional/location/'.
249 LANGUAGE_ID.
'/replacement.php';
251 if(file_exists($replacementPath))
253 require_once($replacementPath);
255 if(class_exists(
'\Sale\Handlers\Delivery\Additional\Location\Replacement'))
257 $result =
'\Sale\Handlers\Delivery\Additional\Location\Replacement';
264 protected static function getCountryName()
268 $relpacementClass = static::getReplacementClass();
270 if($relpacementClass)
272 $result = $relpacementClass::getCountryName();
278 protected static function mapByNames($srvId, $startId = 0, $timeout = 0)
280 $countryName = self::getCountryName();
282 if($countryName ==
'')
288 $con = \Bitrix\Main\Application::getConnection();
289 $sqlHelper =
$con->getSqlHelper();
298 TMP.LOCATION_EXT_ID IS NULL
299 AND TMP.PCOUNTRY = '".$sqlHelper->forSql(
300 \Sale\Handlers\Delivery\Additional\Location\Replacement::getNameRussia()
302 AND TMP.LOCATION_EXT_ID IS NULL
305 if(intval($startId) > 0)
307 $query .=
" AND TMP.ID > ".$sqlHelper->forSql(intval($startId));
314 while ($ethLoc =
$dbRes->fetch())
316 $lastLocationId = (int)$ethLoc[
'ID'];
317 $locationId = self::getLocationIdByNames($ethLoc[
'NAME'], $ethLoc[
'PCITY'], $ethLoc[
'PSUBREGION'], $ethLoc[
'PREGION'], $ethLoc[
'PCOUNTRY'],
true);
320 $locationId = self::getLocationIdByNames($ethLoc[
'NAME'], $ethLoc[
'PCITY'], $ethLoc[
'PSUBREGION'], $ethLoc[
'PREGION'], $ethLoc[
'PCOUNTRY'],
false);
322 if(intval($locationId) > 0)
324 $res = self::setExternalLocation($srvId, $locationId, $ethLoc[
'CODE']);
330 if ($timeout > 0 && (mktime(
true)-
$startTime) >= $timeout)
332 return $lastLocationId;
336 return $lastLocationId;
339 protected static function mapByCodes($srvId)
341 $con = \Bitrix\Main\Application::getConnection();
342 $sqlHelper =
$con->getSqlHelper();
346 b_sale_loc_ext (SERVICE_ID, LOCATION_ID, XML_ID)
348 ".$sqlHelper->forSql($srvId).
", TMP.LOCATION_ID, TMP.CODE
352 TMP.LOCATION_ID > 0 AND TMP.LOCATION_EXT_ID IS NULL
356 protected static function updateLinksInfo($srvId)
358 $con = \Bitrix\Main\Application::getConnection();
359 $sqlHelper =
$con->getSqlHelper();
365 b_sale_location AS L ON TMP.CODE = L.CODE
367 TMP.LOCATION_ID = L.ID
374 b_sale_loc_ext AS E ON TMP.CODE = E.XML_ID AND E.SERVICE_ID = ".$sqlHelper->forSql($srvId).
"
376 TMP.LOCATION_EXT_ID = E.ID
380 protected static function saveCsvToTmpTable(
$path)
385 $srvId = static::getExternalServiceId();
390 if(!File::isFileExists(
$path))
401 if(!is_array($lines))
404 $con = \Bitrix\Main\Application::getConnection();
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"));
409 $con->queryExecute(
"DELETE FROM b_sale_hdale");
411 $sqlHelper =
$con->getSqlHelper();
416 foreach($lines as $line)
418 $cols = explode(
';', $line);
420 if(!is_array($cols) ||
count($cols) != 6)
423 if($cols[0] ==
'' || $cols[1] ==
'')
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).
")";
433 $con->queryExecute(
"INSERT INTO b_sale_hdale(CODE, NAME, PCITY, PSUBREGION, PREGION, PCOUNTRY, ID) VALUES ".$values);
443 $con->queryExecute(
"INSERT INTO b_sale_hdale(CODE, NAME, PCITY, PSUBREGION, PREGION, PCOUNTRY, ID) VALUES ".$values);
445 $con->queryExecute(
"CREATE INDEX IX_BSHDALE_LOCATION_ID ON b_sale_hdale(LOCATION_ID)");
450 protected static function unpackLocations($archivePath)
452 $sUnpackDir = \CTempFile::GetDirectoryName(24);
453 $fileUnpackPath = $sUnpackDir.
'locations.csv';
455 $oArchiver = \CBXArchive::GetArchive($archivePath,
"ZIP");
456 $oArchiver->SetOptions(
array(
"STEP_TIME" => 300));
457 $res = $oArchiver->Unpack($sUnpackDir);
458 unlink($archivePath);
460 if(!
$res || !file_exists($fileUnpackPath))
463 return $fileUnpackPath;
466 protected static function downloadLocations()
469 $client = new \Sale\Handlers\Delivery\Additional\RestClient();
470 $host = $client->getServiceHost();
471 $downloadUrl =
$host.self::ETHALON_LOCATIONS_PATH;
472 $tmpDir = \CTempFile::GetDirectoryName(24);
474 $storePath = $tmpDir.
'locations.zip';
475 $httpClient =
new HttpClient();
477 if($httpClient->download($downloadUrl, $storePath))
const EXTERNAL_SERVICE_CODE
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
</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."%"