1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
shipment.php
См. документацию.
1<?php
2
3namespace Sale\Handlers\Delivery\Rest\DataProviders;
4
5use Bitrix\Location\Entity\Address;
6use Bitrix\Main\Loader;
7use Bitrix\Sale;
8use Bitrix\Sale\BasketItem;
9
15final class Shipment
16{
21 public static function getData(Sale\Shipment $shipment): array
22 {
23 return [
24 'ID' => $shipment->getId() ?: null,
25 'DELIVERY_SERVICE' => self::getDeliveryService($shipment->getDeliveryId()),
26 'PRICE' => $shipment->getShipmentItemCollection()->getPrice(),
27 'CURRENCY' => $shipment->getCurrency(),
28 'WEIGHT' => $shipment->getWeight(),
29 'PROPERTY_VALUES' => self::getPropertyValues($shipment),
30 'ITEMS' => self::getItems($shipment),
31 'EXTRA_SERVICES_VALUES' => self::getExtraServices($shipment),
32 'RESPONSIBLE_CONTACT' => ResponsibleContact::getData($shipment),
33 'RECIPIENT_CONTACT' => RecipientContact::getData($shipment),
34 ];
35 }
36
41 private static function getDeliveryService(int $deliveryId): ?array
42 {
43 $delivery = Sale\Delivery\Services\Manager::getObjectById($deliveryId);
44 if (!$delivery)
45 {
46 return null;
47 }
48
49 $result = [
50 'ID' => (int)$delivery->getId(),
51 'CONFIG' => self::getConfigValues($delivery->getConfigValues()),
52 ];
53 $parentDelivery = $delivery->getParentService();
54 if ($parentDelivery)
55 {
56 $result['PARENT'] = [
57 'ID' => (int)$parentDelivery->getId(),
58 'CONFIG' => self::getConfigValues($parentDelivery->getConfigValues()),
59 ];
60 }
61
62 return $result;
63 }
64
69 private static function getConfigValues(array $rawConfig): array
70 {
71 $result = [];
72
73 if (isset($rawConfig['MAIN']) && is_array($rawConfig['MAIN']))
74 {
75 foreach ($rawConfig['MAIN'] as $name => $value)
76 {
77 if ($name === 'REST_CODE')
78 {
79 continue;
80 }
81
82 $result[] = [
83 'CODE' => $name,
84 'VALUE' => $value
85 ];
86 }
87 }
88
89 return $result;
90 }
91
96 private static function getItems(Sale\Shipment $shipment): array
97 {
98 $result = [];
99
101 foreach ($shipment->getShipmentItemCollection()->getShippableItems() as $shipmentItem)
102 {
103 if (!$basketItem = $shipmentItem->getBasketItem())
104 {
105 continue;
106 }
107
108 $basketItemWeight = $basketItem->getWeight();
109
110 $result[] = [
111 'NAME' => $basketItem->getField('NAME'),
112 'PRICE' => $basketItem->getPrice(),
113 'WEIGHT' => $basketItemWeight === '' || is_null($basketItemWeight) ? null : (float)$basketItemWeight,
114 'CURRENCY' => $basketItem->getCurrency(),
115 'QUANTITY' => $shipmentItem->getQuantity(),
116 'DIMENSIONS' => self::getDimensions($basketItem),
117 ];
118 }
119
120 return $result;
121 }
122
123 private static function getDimensions(BasketItem $basketItem): ?array
124 {
125 $dimension = $basketItem->getField('DIMENSIONS');
126
127 $dimension =
128 $dimension && is_string($dimension) && \CheckSerializedData($dimension)
129 ? unserialize($dimension, ['allowed_classes' => false])
130 : $dimension
131 ;
132
133 if (
134 isset($dimension['WIDTH'])
135 && isset($dimension['HEIGHT'])
136 && isset($dimension['LENGTH'])
137 )
138 {
139 return [
140 'WIDTH' => (float)$dimension['WIDTH'],
141 'HEIGHT' => (float)$dimension['HEIGHT'],
142 'LENGTH' => (float)$dimension['LENGTH'],
143 ];
144 }
145
146 return null;
147 }
148
153 private static function getExtraServices(Sale\Shipment $shipment): array
154 {
155 $result = [];
156
157 $extraServiceManager = new Sale\Delivery\ExtraServices\Manager($shipment->getDeliveryId());
158 $extraServiceManager->setOperationCurrency($shipment->getField('CURRENCY'));
159 $extraServiceManager->setValues($shipment->getExtraServices());
160
161 foreach ($extraServiceManager->getItems() as $extraService)
162 {
163 $result[] = [
164 'ID' => (int)$extraService->getId(),
165 'CODE' => $extraService->getCode(),
166 'VALUE' => $extraService->getValue(),
167 ];
168 }
169
170 return $result;
171 }
172
177 private static function getPropertyValues(Sale\Shipment $shipment): array
178 {
179 $result = [];
180
181 $propertyCollection = $shipment->getPropertyCollection();
183 foreach ($propertyCollection as $property)
184 {
185 if (!in_array($property->getType(), ['STRING', 'ADDRESS']))
186 {
187 continue;
188 }
189
190 $propertyId = $property->getPropertyId();
191
192 $result[] = [
193 'ID' => (int)$propertyId,
194 'TYPE' => $property->getType(),
195 'VALUE' => self::getPropertyValue($property),
196 ];
197 }
198
199 return $result;
200 }
201
206 private static function getPropertyValue(Sale\EntityPropertyValue $propertyValue)
207 {
208 if ($propertyValue->getType() === 'ADDRESS')
209 {
210 return self::getAddressPropertyValue($propertyValue);
211 }
212
213 return $propertyValue->getValue();
214 }
215
220 private static function getAddressPropertyValue(Sale\EntityPropertyValue $propertyValue): ?array
221 {
222 $value = $propertyValue->getValue();
223 if (!$value || !is_array($value))
224 {
225 return null;
226 }
227
228 $fieldsTypeMap = self::getAddressFieldsTypeMap();
229 $address = Address::fromArray($value);
230
231 $fieldCollection = $address->getFieldCollection();
232
233 $addressFields = [];
235 foreach ($fieldCollection as $field)
236 {
237 $fieldType = $field->getType();
238 if (!isset($fieldsTypeMap[$fieldType]))
239 {
240 continue;
241 }
242
243 $addressFields[$fieldsTypeMap[$fieldType]] = $field->getValue();
244 }
245
246 $latitude = $address->getLatitude();
247 $longitude = $address->getLongitude();
248
249 return [
250 'LATITUDE' => $latitude === '' ? null : (float)$latitude,
251 'LONGITUDE' => $longitude === '' ? null : (float)$longitude,
252 'FIELDS' => $addressFields,
253 ];
254 }
255
259 private static function getAddressFieldsTypeMap(): array
260 {
261 if (!Loader::includeModule('location'))
262 {
263 return [];
264
265 }
266
267 return array_flip((new \ReflectionClass(Address\FieldType::class))->getConstants());
268 }
269}
</td ></tr ></table ></td ></tr ><?endif?><? $propertyIndex=0;foreach( $arGlobalProperties as $propertyCode=> $propertyValue
Определения file_new.php:729
</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
$name
Определения menu_edit.php:35
$value
Определения Param.php:39