Server IP : 80.87.202.40 / Your IP : 216.73.216.169 Web Server : Apache System : Linux rospirotorg.ru 5.14.0-539.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Dec 5 22:26:13 UTC 2024 x86_64 User : bitrix ( 600) PHP Version : 8.2.27 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /home/bitrix/ext_www/rospirotorg.ru/bitrix/modules/lpost.delivery/lib/main/ |
Upload File : |
<?php namespace Lpost\Delivery\Main; use Bitrix\Main\Localization\Loc, Bitrix\Main\Web\Json; Loc::loadMessages(__FILE__); class Zone extends Base { use \Lpost\Delivery\Base; protected static $tableName = 'ZoneTable'; public static function getZone($filter = []) { return \Lpost\Delivery\Entity\ZoneTable::getList([ 'select' => ["id", "id_pickup", "zone", "is_cash", "is_card", "params"], 'filter' => $filter, "order" => [ "day_logistic" => "desc" ] ])->fetch(); } public static function getZoneCityById($cityId, $json = true) { if(!$cityData = City::getCityData($cityId)) { return false; } /*\Bitrix\Main\Diag\Debug::writeToFile( ['cityId' => $cityId, 'cityData' => $cityData], date("d.m.Y H:i:s"). ' getZoneCityById', "/bitrix/php_interface/lpost.delivery.all.txt" );*/ $arResult = [ "city" => [ "name" => $cityData['name'], "full_name" => $cityData['full_name'], "lat" => $cityData['coord_lat'], "long" => $cityData['coord_long'] ] ]; //if(isset(self::CITY_REGION_MAIN[$regionName])) //$arCityData['MAIN_CITY_REGION'] = self::CITY_REGION_MAIN[$regionName]; $moscow = Loc::getMessage('Moscow'); if($arZone = self::getNearbyZoneByCityCode($cityData, false)) { $arZone['zone'] = Json::decode($arZone['zone']); if ($cityData['name'] == $moscow && isset($arZone['zone'][2])) { unset($arZone['zone'][2]); } $arResult = array_merge($arResult, $arZone); } /*\Bitrix\Main\Diag\Debug::writeToFile( ['id_pickup' => $arResult['id_pickup'], 'zone' => $arResult['zone']], date("d.m.Y H:i:s"). ' getZoneCityById', "/bitrix/php_interface/lpost.delivery.all.txt" );*/ if(empty($arResult['id_pickup']) || empty($arResult['zone'][0])){ // есть города где, 'IsCourier' => 1, но нет зоны $arCityData = $cityData['arCityData']; $arLocationParentZoneMsk = [ Loc::getMessage('MoscowRegion'), $moscow, ]; $arLocationParentZoneSpb = [ Loc::getMessage('MoscowRegion'), Loc::getMessage('StPetersburg') ]; // остальные города $arLocationParentZoneOther = !empty(City::CITY_REGION_MAIN) ? City::CITY_REGION_MAIN : []; $arLocationParentZone = array_merge($arLocationParentZoneMsk, $arLocationParentZoneSpb, array_keys($arLocationParentZoneOther)); if(isset($arCityData['REGION_NAME']) && in_array($arCityData['REGION_NAME'], $arLocationParentZone)){ $cityName = $arLocationParentZoneOther[$arCityData['REGION_NAME']] ?? (in_array($arCityData['REGION_NAME'], $arLocationParentZoneMsk) ? $moscow : Loc::getMessage('StPetersburg')); if(is_array($cityName)){ // проверим к какому городу/столице относится введеный город foreach($cityName as $city){ $arZoneParent = \Lpost\Delivery\Main\Zone::getZone(["city_name" => $city]); $coordsJson = Json::decode($arZoneParent['zone']); $coords = Json::decode("{" . $coordsJson[0]['WKT'] . "}")['Coordinates']; $inPoly = self::inPoly($cityData['coord_long'], $cityData['coord_lat'], $coords); if($inPoly){ self::getZoneParentData($city, $arZoneParent, $arResult); break; } } } else { $arZoneParent = \Lpost\Delivery\Main\Zone::getZone(["city_name" => $cityName]); self::getZoneParentData($cityName, $arZoneParent, $arResult); } } else{ // for paren Leningradskaya oblast $cityName = $cityData['name']; if(Loc::getMessage('Moscow') == $cityName || Loc::getMessage('StPetersburg') == $cityName){ $arZoneParent = \Lpost\Delivery\Main\Zone::getZone(["city_name" => $cityName]); self::getZoneParentData($cityName, $arZoneParent, $arResult); } } } /*\Bitrix\Main\Diag\Debug::writeToFile( ['cityId' => $arResult], date("d.m.Y H:i:s"). ' getZoneCityById', "/bitrix/php_interface/lpost.delivery.all.txt" );*/ if($json) { return Json::encode($arResult); } return $arResult; } private static function getZoneParentData($cityName, $arZoneParent, &$arResult){ if(!empty($arZoneParent)){ if(isset($arZoneParent['id_pickup'])) $arResult['id_pickup'] = $arZoneParent['id_pickup']; if(empty($arResult['is_card']) && isset($arZoneParent['is_card'])) $arResult['is_card'] = $arZoneParent['is_card']; if(empty($arResult['is_cash']) && isset($arZoneParent['is_cash'])) $arResult['is_cash'] = $arZoneParent['is_cash']; if(empty($arResult['params']) && isset($arZoneParent['params'])) $arResult['params'] = $arZoneParent['params']; if(empty($arResult['zone']) && !empty($arZoneParent['zone'])){ $arResult['zone'] = Json::decode($arZoneParent['zone']); if ($cityName == Loc::getMessage('Moscow') && isset($arResult['zone'][2])) { unset($arResult['zone'][2]); } } elseif(empty($arResult['zone'][0]) && !empty($arZoneParent['zone'])){ $arResult['zone'] = Json::decode($arZoneParent['zone']); } } } private static function getNearbyZoneByCityCode($cityData, $json = true) { if(isset($cityData['params']['zone_id'])) if($arZone = self::getZone([ "LOGIC" => "OR", [ "city_name" => $cityData['name'] ], [ "id" => $cityData['params']['zone_id'] ] ])) { return $arZone; } if($arZone = self::getCityByCoords($cityData)) return $arZone; if($nearbyZone = self::getNearbyCityByCoords($cityData)) { $coords = Json::decode($nearbyZone['zone']); $coords = Json::decode("{" . $coords[0]['WKT'] . "}"); $coords = $coords['Coordinates']; $res = self::inPoly( $cityData['coord_long'], $cityData['coord_lat'], $coords ); if($res) { self::saveNearbyZoneId($nearbyZone['id'], $cityData); return $nearbyZone; } } return false; } /** * @param $zoneId * @param $cityData */ private static function saveNearbyZoneId($zoneId, $cityData) { if(empty($cityData['params'])) $cityData['params'] = []; $cityData['params']['zone_id'] = $zoneId; \Lpost\Delivery\Entity\CityCoordTable::update( [ "id" => $cityData['id'], "city_id" => $cityData['city_id'] ], [ "params" => $cityData['params'] ] ); } /** * @param array $arCoords * @return */ private static function getNearbyCityByCoords(array $arCoords = []) { return isset($arCoords['coord_lat']) && $arCoords['coord_long'] ? \Lpost\Delivery\Entity\ZoneTable::getList([ "select" => [ "*", new \Bitrix\Main\Entity\ExpressionField('track', "abs({$arCoords['coord_lat']}-%s)+abs({$arCoords['coord_long']}-%s)", [ "coord_lat", "coord_long" ]), ], "limit" => 1 ])->fetch() : false; } /** * @param array $arCoords * @return */ private static function getCityByCoords(array $arCoords = []) { return isset($arCoords['coord_lat']) && $arCoords['coord_long'] ? \Lpost\Delivery\Entity\ZoneTable::getList([ "select" => [ "*" ], "filter" => [ 'coord_lat' => $arCoords['coord_lat'], 'coord_long' => $arCoords['coord_long'], ], "limit" => 1 ])->fetch() : false; } /** * @param $x * @param $y * @param $polygons * @return bool */ private static function inPolyV1($x, $y, $polygons) { foreach($polygons as $polygon){ $npol = count($polygon); $j = $npol - 1; for($i = 0; $i < $npol; $i++) { if( ((($polygon[$i][1] <= $y) && ($y < $polygon[$j][1])) || (($polygon[$j][1] <= $y) && ($y < $polygon[$i][1]))) && ($x > ($polygon[$j][0] - $polygon[$i][0]) * ($y - $polygon[$i][1]) / ($polygon[$j][1] - $polygon[$i][1]) + $polygon[$i][0]) ){ return true; } $j = $i; } } return false; } private static function inPoly($lon, $lat, $polygons) { foreach($polygons as $polygon){ $sbPolygonEngine = new \Lpost\Delivery\Main\sbPolygonEngine($polygon); $isCrosses = $sbPolygonEngine->isCrossesWith($lon, $lat); if($isCrosses) return true; } return false; } }