1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
tariffschecker.php
См. документацию.
1<?php
2
3namespace Sale\Handlers\Delivery\YandexTaxi;
4
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;
10
16final class TariffsChecker
17{
19 protected $api;
20
22 private $results = [];
23
28 public function __construct(Api $api)
29 {
30 $this->api = $api;
31 }
32
37 private function getTariffsResult(array $coordinates): TariffsResult
38 {
39 $resultHash = $this->getCoordinatesHash($coordinates);
40 if (isset($this->results[$resultHash]))
41 {
42 return $this->results[$resultHash];
43 }
44
45 $this->results[$resultHash] = $this->api->getTariffs(
46 (new TariffsOptions)->setStartPoint(
47 array_map('floatval', $coordinates)
48 )
49 );
50
51 return $this->results[$resultHash];
52 }
53
58 public function getAvailableTariffs(array $coordinates): ?array
59 {
60 $tariffsResult = $this->getTariffsResult($coordinates);
61 if (!$tariffsResult->isSuccess())
62 {
63 return null;
64 }
65
66 return array_map(
67 function ($tariff)
68 {
69 return $tariff->getCode();
70 },
71 $tariffsResult->getTariffs()
72 );
73 }
74
80 public function getSupportedRequirementsByTariff(string $tariffCode, Shipment $shipment): array
81 {
82 $result = [];
83
84 $coordinates = $this->getSourceCoordinatesByShipment($shipment);
85 if (!$coordinates)
86 {
87 return $result;
88 }
89
90 $tariffsResult = $this->getTariffsResult($coordinates);
91 if (!$tariffsResult->isSuccess())
92 {
93 return $result;
94 }
95
96 $tariffs = $tariffsResult->getTariffs();
97 foreach ($tariffs as $tariff)
98 {
99 if ($tariff->getCode() === $tariffCode)
100 {
101 $supportedRequirements = $tariff->getSupportedRequirements();
102 foreach ($supportedRequirements as $supportedRequirement)
103 {
104 $result[] = $supportedRequirement;
105 }
106 break;
107 }
108 }
109
110 return $result;
111 }
112
118 public function isTariffAvailableByShipment(string $tariff, Shipment $shipment): ?bool
119 {
120 $coordinates = $this->getSourceCoordinatesByShipment($shipment);
121 if (!$coordinates)
122 {
123 return null;
124 }
125
126 $tariffs = $this->getAvailableTariffs($coordinates);
127 if (is_null($tariffs))
128 {
129 return null;
130 }
131
132 return in_array($tariff, $tariffs, true);
133 }
134
139 private function getSourceCoordinatesByShipment(Shipment $shipment): ?array
140 {
141 $addressFrom = $shipment->getPropertyCollection()->getAddressFrom();
142 if (!$addressFrom)
143 {
144 return null;
145 }
146
147 $addressFromValue = $addressFrom->getValue();
148 if (!$addressFromValue)
149 {
150 return null;
151 }
152
153 $address = Address::fromArray($addressFromValue);
154 if (!$address->getLatitude() || !$address->getLongitude())
155 {
156 return null;
157 }
158
159 return [
160 $address->getLongitude(),
161 $address->getLatitude(),
162 ];
163 }
164
169 private function getCoordinatesHash(array $coordinates): string
170 {
171 return md5(implode(';', $coordinates));
172 }
173}
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14