3namespace Sale\Handlers\Delivery\YandexTaxi;
5use Bitrix\Location\Entity\Address;
6use Bitrix\Sale\Shipment;
7use Sale\Handlers\Delivery\YandexTaxi\Api\Api;
8use Sale\Handlers\Delivery\YandexTaxi\Api\ApiResult\TariffsResult;
9use Sale\Handlers\Delivery\YandexTaxi\Api\RequestEntity\TariffsOptions;
16final class TariffsChecker
22 private $results = [];
28 public function __construct(Api $api)
37 private function getTariffsResult(
array $coordinates): TariffsResult
39 $resultHash = $this->getCoordinatesHash($coordinates);
40 if (isset($this->results[$resultHash]))
42 return $this->results[$resultHash];
45 $this->results[$resultHash] = $this->api->getTariffs(
46 (
new TariffsOptions)->setStartPoint(
47 array_map(
'floatval', $coordinates)
51 return $this->results[$resultHash];
58 public function getAvailableTariffs(
array $coordinates): ?
array
60 $tariffsResult = $this->getTariffsResult($coordinates);
61 if (!$tariffsResult->isSuccess())
69 return $tariff->getCode();
71 $tariffsResult->getTariffs()
80 public function getSupportedRequirementsByTariff(
string $tariffCode, Shipment $shipment):
array
84 $coordinates = $this->getSourceCoordinatesByShipment($shipment);
90 $tariffsResult = $this->getTariffsResult($coordinates);
91 if (!$tariffsResult->isSuccess())
96 $tariffs = $tariffsResult->getTariffs();
97 foreach ($tariffs as $tariff)
99 if ($tariff->getCode() === $tariffCode)
101 $supportedRequirements = $tariff->getSupportedRequirements();
102 foreach ($supportedRequirements as $supportedRequirement)
104 $result[] = $supportedRequirement;
118 public function isTariffAvailableByShipment(
string $tariff, Shipment $shipment): ?bool
120 $coordinates = $this->getSourceCoordinatesByShipment($shipment);
126 $tariffs = $this->getAvailableTariffs($coordinates);
127 if (is_null($tariffs))
132 return in_array($tariff, $tariffs,
true);
139 private function getSourceCoordinatesByShipment(Shipment $shipment): ?
array
141 $addressFrom = $shipment->getPropertyCollection()->getAddressFrom();
147 $addressFromValue = $addressFrom->getValue();
148 if (!$addressFromValue)
153 $address = Address::fromArray($addressFromValue);
154 if (!$address->getLatitude() || !$address->getLongitude())
160 $address->getLongitude(),
161 $address->getLatitude(),
169 private function getCoordinatesHash(
array $coordinates): string
171 return md5(implode(
';', $coordinates));
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)