2namespace Bitrix\Sale\Delivery;
6use Bitrix\Main\Text\Encoding;
7use Bitrix\Main\SystemException;
8use Bitrix\Sale\Location\Comparator;
9use Bitrix\Main\ArgumentNullException;
10use Bitrix\Sale\Location\ExternalTable;
11use Bitrix\Sale\Location\LocationTable;
12use Bitrix\Sale\Location\ExternalServiceTable;
47 if($externalCode ==
'')
50 $srvId = static::getExternalServiceId();
57 '=XML_ID' => $externalCode,
58 '=SERVICE_ID' => $srvId
62 if($loc =
$res->fetch())
78 $srvId = static::getExternalServiceId();
87 '=CODE' => $locationId,
90 '=EXTERNAL.SERVICE_ID' => $srvId
94 'XML_ID' =>
'EXTERNAL.XML_ID'
100 if($loc =
$res->fetch())
117 '=CODE' => $locationId,
122 'ID',
'CODE',
'LEFT_MARGIN',
'RIGHT_MARGIN',
123 'TYPE_CODE' =>
'TYPE.CODE'
127 if(!$loc =
$res->fetch())
130 if($loc[
'TYPE_CODE'] ==
'CITY')
135 '<LEFT_MARGIN' => $loc[
'LEFT_MARGIN'],
136 '>RIGHT_MARGIN' => $loc[
'RIGHT_MARGIN'],
137 'TYPE.CODE' =>
'CITY',
138 '=EXTERNAL.SERVICE_ID' => $srvId
141 'ID',
'CODE',
'LEFT_MARGIN',
'RIGHT_MARGIN',
142 'XML_ID' =>
'EXTERNAL.XML_ID'
146 if($locParent =
$res->fetch())
147 return $locParent[
'XML_ID'];
159 if($locationId ==
'')
166 '=CODE' => $locationId,
167 '=ID' => $locationId,
170 '=TYPE.CODE' =>
'CITY',
171 '=PARENTS.TYPE.CODE' =>
'CITY'
176 'TYPE_CODE' =>
'TYPE.CODE',
177 'PID' =>
'PARENTS.ID',
181 if($loc =
$res->fetch())
198 if(static::isInstalled())
201 $imported = static::importFromCsv(
$_SERVER[
'DOCUMENT_ROOT'].static::CSV_FILE_PATH);
203 if(intval($imported) <= 0)
218 if(!static::isInstalled())
222 $sqlHelper =
$con->getSqlHelper();
223 $srvId = $sqlHelper->forSql(static::getExternalServiceId());
224 $con->queryExecute(
"DELETE FROM b_sale_loc_ext WHERE SERVICE_ID=".$srvId);
225 ExternalServiceTable::delete($srvId);
240 $res = ExternalServiceTable::getList(
array(
242 '=CODE' => static::EXTERNAL_SERVICE_CODE,
243 '!=EXTERNAL.ID' =>
false
263 $res = static::getAllLocations();
265 if(
$res->isSuccess())
267 $locations =
$res->getData();
269 if(is_array($locations) && !empty($locations))
271 $res = static::setMap($locations);
273 if(!
$res->isSuccess())
309 if(intval($srvId) < 0)
314 if(!is_array($lines))
319 foreach($lines as $line)
321 $columns = explode(
';', $line);
323 if(!is_array($columns) ||
count($columns) != 2)
328 '=CODE' => $columns[0],
330 'select' =>
array(
'ID')
333 if($loc =
$res->fetch())
334 if(self::setExternalLocation($srvId, $loc[
'ID'], $columns[1]))
349 $srvId = static::getExternalServiceId();
356 '=EXTERNAL.SERVICE_ID' => $srvId
360 'XML_ID' =>
'EXTERNAL.XML_ID'
366 while($row =
$res->fetch())
367 if($row[
'CODE'] <>
'')
368 $content .= $row[
'CODE'].
";".$row[
'XML_ID'].
"\n";
370 return \Bitrix\Main\IO\File::putFileContents(
$path,
$content);
380 if(static::EXTERNAL_SERVICE_CODE ==
'')
388 $res = ExternalServiceTable::getList(
array(
389 'filter' =>
array(
'=CODE' => static::EXTERNAL_SERVICE_CODE)
392 if($srv =
$res->fetch())
398 $res = ExternalServiceTable::add(
array(
'CODE' => static::EXTERNAL_SERVICE_CODE));
400 if(!
$res->isSuccess())
425 $xmlIdExist =
array();
426 $locationIdExist =
array();
427 $xmlIds = array_keys($cities);
428 $srvId = static::getExternalServiceId();
432 '=SERVICE_ID' => $srvId
438 $xmlIdExist[] =
$map[
'XML_ID'];
439 $locationIdExist[] =
$map[
'LOCATION_ID'];
442 if(in_array(
$map[
'XML_ID'], $xmlIds))
443 unset($cities[
$map[
'XML_ID']]);
450 foreach($cities as $city)
452 $xmlId = $city[self::CITY_XML_ID_IDX];
453 $locId = static::getLocationIdByNames($city[static::CITY_NAME_IDX],
'',
'', $city[static::REGION_NAME_IDX]);
455 if(intval($locId) > 0 && !in_array($xmlId, $xmlIdExist) && !in_array($locId, $locationIdExist))
457 ExternalTable::add(
array(
458 'SERVICE_ID' => $srvId,
459 'LOCATION_ID' => $locId,
463 $xmlIdExist[] = $xmlId;
464 $locationIdExist[] = $locId;
467 unset($cities[$xmlId]);
491 if(intval($locationId) <= 0)
494 static $locCache =
array();
496 if(!isset($locCache[$srvId]))
498 $locCache[$srvId] =
array();
500 $eRes = ExternalTable::getList(
array(
502 '=SERVICE_ID' => $srvId,
504 'select' =>
array(
'ID',
'SERVICE_ID',
'LOCATION_ID',
'XML_ID')
507 while($loc = $eRes->fetch())
508 $locCache[$srvId][$loc[
'LOCATION_ID'].
'##'.$loc[
'XML_ID']] = $loc[
'ID'];
511 if(!empty($locCache[$srvId][$locationId.
'##'.$xmlId]))
515 $res = ExternalTable::update(
516 $locCache[$srvId][$locationId.
'##'.$xmlId],
518 'SERVICE_ID' => $srvId,
520 'LOCATION_ID' => $locationId
527 $result = new \Bitrix\Main\Entity\UpdateResult();
528 $result->addError(
new Error(
'External location already exists',
'EXTERNAL_LOCATION_EXISTS'));
535 'SERVICE_ID' => $srvId,
537 'LOCATION_ID' => $locationId
540 $locCache[$srvId][$locationId.
'##'.$xmlId] =
$res->getId();
565 $res =
$con->query(
"SELECT COUNT(1) AS COUNT FROM b_sale_hdaln");
567 if($row =
$res->fetch())
568 $count = intval($row[
'COUNT']);
584 $lastProcessedId = 0;
586 $sqlHelper =
$con->getSqlHelper();
588 if(intval($startId) <= 0)
589 $con->queryExecute(
"DELETE FROM b_sale_hdaln");
599 INNER JOIN b_sale_loc_name AS N ON L.ID = N.LOCATION_ID
600 INNER JOIN b_sale_loc_type AS T ON L.TYPE_ID = T.ID
603 AND (T.CODE = 'VILLAGE' OR T.CODE = 'CITY')
606 if($startId !==
false)
607 $query .=
" AND L.ID > ".strval(intval($startId));
609 $query .=
" ORDER BY ID ASC";
612 while($loc =
$res->fetch())
616 b_sale_hdaln (LOCATION_ID, LEFT_MARGIN, RIGHT_MARGIN, NAME)
618 ".intval($loc[
'ID']).
",
619 ".intval($loc[
'LEFT_MARGIN']).
",
620 ".intval($loc[
'RIGHT_MARGIN']).
",
621 '".$sqlHelper->forSql(
630 $lastProcessedId = $loc[
'ID'];
632 if($timeout > 0 && (mktime(
true)-
$startTime) >= $timeout)
636 return $lastProcessedId;
652 $nameNorm = Comparator::normalizeEntity(
$name,
'LOCALITY');
653 $subregionNorm =
null;
659 $searchNames = array_merge($searchNames, \
Bitrix\
Sale\
Location\Comparator::getLocalityNamesArray($nameNorm[
'NAME'], $nameNorm[
'TYPE']));
661 $searchNames = array_map(
array(
'\Bitrix\Sale\Location\Comparator',
'flatten'), $searchNames);
662 $searchNames = array_map(
function(
$name){
return "'".$name.
"'";}, $searchNames);
664 if(empty($searchNames))
668 $sqlHelper =
$con->getSqlHelper();
672 N.LOCATION_ID AS LOCATION_ID,
673 N.LEFT_MARGIN AS LEFT_MARGIN,
674 N.RIGHT_MARGIN AS RIGHT_MARGIN,
678 LEFT JOIN b_sale_loc_ext AS E
679 ON N.LOCATION_ID = E.LOCATION_ID AND E.SERVICE_ID = ".$sqlHelper->forSql(self::getExternalServiceId()).
"
681 E.LOCATION_ID IS NULL
682 AND NAME IN (".implode(
', ', $searchNames).
")");
687 while($loc =
$res->fetch())
689 if(Comparator::isEntityEqual($loc[
'NAME'], $nameNorm,
'LOCALITY'))
691 $margins[] =
array($loc[
'LOCATION_ID'], $loc[
'LEFT_MARGIN'], $loc[
'RIGHT_MARGIN'], $loc[
'NAME']);
692 $results[$loc[
'LOCATION_ID']] =
array(
'NAME' =>
true);
694 if($loc[
'NAME'] == $nameNorm[
"NAME"])
695 $exact[] = $loc[
'LOCATION_ID'];
702 $marginFilter =
array(
'LOGIC' =>
'OR');
704 foreach($margins as $v)
705 $marginFilter[] =
array(
'<LEFT_MARGIN' => $v[1],
'>RIGHT_MARGIN' => $v[2]);
709 '=NAME.LANGUAGE_ID' => LANGUAGE_ID,
710 '=TYPE.CODE' =>
array(
'SUBREGION',
'REGION',
'CITY'),
715 'PARENTS_NAME_UPPER' =>
'NAME.NAME_UPPER',
716 'PARENTS_TYPE_CODE' =>
'TYPE.CODE',
717 'LEFT_MARGIN',
'RIGHT_MARGIN'
721 while($loc =
$res->fetch())
727 if(in_array(
false, $results[$id],
true))
732 if($loc[
'PARENTS_TYPE_CODE'] ==
'REGION' &&
$region <> '')
734 if(!is_array($regionNorm))
735 $regionNorm = Comparator::normalizeEntity(
$region,
'REGION');
737 $found = Comparator::isEntityEqual($loc[
'PARENTS_NAME_UPPER'], $regionNorm,
'REGION');
739 elseif($subregion <>
'' && $loc[
'PARENTS_TYPE_CODE'] ==
'SUBREGION')
741 if(!is_array($subregionNorm))
742 $subregionNorm = Comparator::normalizeEntity($subregion,
'SUBREGION');
744 $found = Comparator::isEntityEqual($loc[
'PARENTS_NAME_UPPER'], $subregionNorm,
'SUBREGION');
746 elseif($city <>
'' && $loc[
'PARENTS_TYPE_CODE'] ==
'CITY')
748 if(!is_array($cityNorm))
749 $subregionNorm = Comparator::normalizeEntity($city,
'LOCALITY');
751 $found = Comparator::isEntityEqual($loc[
'PARENTS_NAME_UPPER'], $cityNorm,
'LOCALITY');
756 $isInExact = in_array($id, $exact);
757 $results[$id][$loc[
'PARENTS_TYPE_CODE']] = $found;
759 if($results[$id][
'REGION'] ===
true && $results[$id][
'SUBREGION'] ===
true && $isInExact)
762 if($found ===
false && $isInExact)
764 $key = array_search($id, $exact);
774 foreach($exact as $e)
775 if(!in_array(
false, $results[$e],
true))
778 $resCandidates =
array();
780 foreach($results as $id =>
$result)
782 if(!in_array(
false,
$result,
true))
788 if(empty($resCandidates))
791 if(
count($resCandidates) > 1)
793 arsort($resCandidates);
794 reset($resCandidates);
797 return key($resCandidates);
800 protected static function getIdByMargin($parentLeft, $parentRight, $lMargins)
804 foreach($lMargins as $m)
806 if($m[1] > $parentLeft && $m[2] < $parentRight)
815 foreach($lMargins as $m)
817 if($m[1] > $parentLeft && $m[2] < $parentRight)
static getConnection($name="")
static getFileContents($path)
const EXTERNAL_SERVICE_CODE
static getCityId($locationId)
static getNameByMargin($parentLeft, $parentRight, $lMargins)
static setExternalLocation($srvId, $locationId, $xmlId, $updateExist=false)
static exportToCsv($path)
static importFromCsv($path)
static isNormalizedTableFilled()
static setExternalLocation2($srvId, $locationId, $xmlId, $updateExist=false)
static getIdByMargin($parentLeft, $parentRight, $lMargins)
static getExternalServiceId()
static getInternalId($externalCode)
static getUpperCityExternalId($locationId, $srvId)
static getLocationIdByNames($name, $city, $subregion, $region, $country='', $exactOnly=false)
static fillNormalizedTable($startId=false, $timeout=0)
static getExternalId($locationId)
static setMap(array $cities)
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
$_SERVER["DOCUMENT_ROOT"]
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
if(empty($signedUserToken)) $key
</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."%"