2namespace Sale\Handlers\Delivery\Additional\DeliveryRequests\RusPost\Requests;
5use Bitrix\Main\Web\HttpClient;
6use Bitrix\Sale\Delivery\Requests;
7use Bitrix\Main\Localization\Loc;
8use Bitrix\Sale\Delivery\Packing\Packer;
9use Sale\Handlers\Delivery\AdditionalHandler;
10use Sale\Handlers\Delivery\Additional\DeliveryRequests\RusPost\Reference;
11use Sale\Handlers\Delivery\Additional\DeliveryRequests\RusPost\Handler;
13Loc::loadMessages(__FILE__);
21class OrderCreate
extends Base
23 protected $path =
"/1.0/user/backlog";
24 protected $type = HttpClient::HTTP_PUT;
26 protected $deliveryService =
null;
33 protected function convertResponse($rawData, $requestData)
35 $result =
new Requests\Result();
36 $errorPositions =
array();
38 $createResults =
array();
40 if(is_array($rawData[
'errors']))
42 foreach($rawData[
'errors'] as
$error)
44 if(!isset($requestData[
$error[
'position']][
'order-num']))
47 $internalId = $requestData[
$error[
'position']][
'order-num'];
49 if(!isset($createResults[$internalId]))
50 $createResults[$internalId] =
new Requests\ShipmentResult($internalId,
'');
52 $errorPositions[] =
$error[
'position'];
54 if(is_array(
$error[
'error-codes']))
56 foreach(
$error[
'error-codes'] as $errorcode)
58 $message = Reference::getErrorDescription($errorcode[
'code'],
'PUT /1.0/user/backlog');
60 if(!empty($errorcode[
'details']))
61 $details = $errorcode[
'details'];
62 elseif(!empty($errorcode[
'description']))
63 $details = $errorcode[
'description'];
69 if($errorcode[
'code'] ==
'ILLEGAL_MAIL_CATEGORY')
81 $idsMap = $this->getShipmentsMap($rawData[
'result-ids'], $errorPositions, $requestData);
82 $idsMapFlipped = array_flip($idsMap);
84 if(is_array($rawData[
'result-ids']))
86 foreach($rawData[
'result-ids'] as $externalId)
88 if(!isset($idsMapFlipped[$externalId]))
91 $internalId = $idsMapFlipped[$externalId];
93 if(!isset($createResults[$internalId]))
94 $createResults[$internalId] =
new Requests\ShipmentResult($internalId, $externalId);
98 $result->setResults($createResults);
108 protected function getShipmentsMap($successIds, $errorPositions, $requestData)
112 if(empty($successIds) || !is_array($successIds))
117 foreach($requestData as $position =>
$request)
119 if(!empty($errorPositions) && in_array($position, $errorPositions))
125 if(!empty($requestData[$position][
'order-num']) && !empty($successIds[$position - $shift]))
136 protected function normalizeAddresses(
array $addresses)
138 $requestData =
array();
140 foreach($addresses as $id => $address)
142 $address = str_replace([
"\n",
"\t",
"\r"],
" ", $address);
144 $requestData[] =
array(
146 'original-address' => $address
151 $deliveryRequest = $this->deliveryService->getDeliveryRequestHandler();
152 return $deliveryRequest->send(
'CLEAN_ADDRESS', $requestData);
159 protected function normalizeFios(
array $fios)
161 $requestData =
array();
163 foreach($fios as $id => $fio)
165 $requestData[] =
array(
167 'original-fio' => $fio
172 $deliveryRequest = $this->deliveryService->getDeliveryRequestHandler();
173 return $deliveryRequest->send(
'NORMALIZE_FIO', $requestData);
180 protected function isAddressGood(
array $address)
182 $quality = $address[
'quality-code'] ==
'GOOD'
183 || $address[
'quality-code'] ==
'POSTAL_BOX'
184 || $address[
'quality-code'] ==
'ON_DEMAND'
185 || $address[
'quality-code'] ==
'UNDEF_05';
187 $valid = $address[
'validation-code'] ==
'VALIDATED'
188 || $address[
'validation-code'] ==
'OVERRIDDEN'
189 || $address[
'validation-code'] ==
'CONFIRMED_MANUALLY';
191 return $quality && $valid;
199 public function createBody(
array $shipmentIds,
array $additional =
array())
201 $result =
new Requests\Result();
202 $resultData =
array();
203 $shipments = Requests\Helper::getShipmentsByIds($shipmentIds);
204 $rpoCategory = Reference::getRpoCategoriesMap();
205 $qualityCodes = Reference::getQualityCodesList();
206 $addresses =
array();
208 $notValidShipmentIds =
array();
211 foreach($shipments as $shipment)
213 $shipmentParams = AdditionalHandler::getShipmentParams($shipment, $this->deliveryService->getServiceType());
214 $deliveryConfig = $shipment->getDelivery() ? $shipment->getDelivery()->getConfig() : [];
215 $shipmentId = $shipment->getId();
217 $mailType = $deliveryConfig[
'MAIN'][
'ITEMS'][
'OTPRAVKA_RPO'][
'VALUE'];
221 $shpResult =
new Requests\ShipmentResult($shipmentId);
222 $shpResult->addError(
225 'SALE_DLVRS_ADD_DREQ_ROC_NOT_SUPPORTED',
227 '#DELIVERY_PROFILE_LINK#' =>
228 Requests\Helper::getDeliveryEditLink($shipment->getDeliveryId(), $shipmentParams[
'DELIVERY_SERVICE_CONFIG'][
'MAIN'][
'NAME']),
232 $result->addResult($shpResult);
238 foreach($shipmentParams[
'ITEMS'] as $item)
240 if(!empty($item[
'DIMENSIONS']) && is_array($item[
'DIMENSIONS']))
242 if(intval($item[
'DIMENSIONS'][
'HEIGHT']) > 0
243 && intval($item[
'DIMENSIONS'][
'WIDTH']) > 0
244 && intval($item[
'DIMENSIONS'][
'LENGTH']) > 0)
246 for(
$i=0;
$i < $item[
'QUANTITY'];
$i++)
249 intval($item[
'DIMENSIONS'][
'LENGTH']),
250 intval($item[
'DIMENSIONS'][
'WIDTH']),
251 intval($item[
'DIMENSIONS'][
'HEIGHT'])
258 $mailCategory = $rpoCategory[$shipmentParams[
'DELIVERY_SERVICE_CONFIG'][
'MAIN'][
'CATEGORY']];
260 if($mailCategory ==
'')
261 $mailCategory =
'ORDINARY';
264 'address-type-to' =>
'DEFAULT',
267 'fragile' => isset($shipmentParams[
'EXTRA_SERVICES'][4]) && $shipmentParams[
'EXTRA_SERVICES'][4] ==
'Y',
268 'mail-category' => $mailCategory,
269 'order-num' => strval($shipmentParams[
'SHIPMENT_ID']),
270 'sms-notice-recipient' => isset($shipmentParams[
'EXTRA_SERVICES'][42]) && $shipmentParams[
'EXTRA_SERVICES'][4] ==
'Y' ? 1 : 0,
271 'mail-direct' => 643,
272 'mail-type' => $mailType
275 $item[
'courier'] = isset($shipmentParams[
'EXTRA_SERVICES'][26]) && $shipmentParams[
'EXTRA_SERVICES'][26] ==
'Y';
277 if(isset($shipmentParams[
'EXTRA_SERVICES'][1]))
279 if($shipmentParams[
'EXTRA_SERVICES'][1] ==
"1")
280 $item[
'with-simple-notice'] =
true;
281 elseif($shipmentParams[
'EXTRA_SERVICES'][1] ==
"2")
282 $item['with-order-of-notice'] =
true;
285 if(intval($shipmentParams['WEIGHT']) > 0)
287 $item[
'mass'] = intval($shipmentParams[
'WEIGHT']);
291 $dlvConfig = $this->deliveryService->getConfigValues();
293 if(intval($dlvConfig[
'MAIN'][
'WEIGHT_DEFAULT']) > 0)
294 $item[
'mass'] = intval($dlvConfig[
'MAIN'][
'WEIGHT_DEFAULT']);
301 $item[
'dimension'] =
array(
302 'length' => $dimensions[0]/10,
303 'width' => $dimensions[1]/10,
304 'height' => $dimensions[2]/10
309 $dlvConfig = $this->deliveryService->getConfigValues();
311 if(intval($dlvConfig[
'MAIN'][
'LENGTH_DEFAULT']) > 0
312 && intval($dlvConfig[
'MAIN'][
'WIDTH_DEFAULT']) > 0
313 && intval($dlvConfig[
'MAIN'][
'HEIGHT_DEFAULT']) > 0)
315 $item[
'dimension'] =
array(
316 'length' => intval($dlvConfig[
'MAIN'][
'LENGTH_DEFAULT']/10),
317 'width' => intval($dlvConfig[
'MAIN'][
'WIDTH_DEFAULT']/10),
318 'height' => intval($dlvConfig[
'MAIN'][
'HEIGHT_DEFAULT']/10)
324 $OVERSIZED_DIM_SUM = 120;
326 if($item[
'dimension'][
'length'] < $OVERSIZED_DIM
327 && $item[
'dimension'][
'width'] < $OVERSIZED_DIM
328 && $item[
'dimension'][
'height'] < $OVERSIZED_DIM
329 && array_sum($item[
'dimension']) < $OVERSIZED_DIM_SUM
332 unset($item[
'dimension']);
335 if(!empty($additional[
'OPS']))
336 $item[
'postoffice-code'] = $additional[
'OPS'];
338 if(!empty($shipmentParams[
'PHONE']))
339 $item[
'tel-address'] = preg_replace(
'/[^\d]/',
'',$shipmentParams[
'PHONE']);
341 $price = ($shipmentParams[
'PRICE'] + $shipmentParams[
'PRICE_DELIVERY']) * 100;
343 if($shipmentParams[
'DELIVERY_SERVICE_CONFIG'][
'MAIN'][
'CATEGORY'] == 2 || $shipmentParams[
'DELIVERY_SERVICE_CONFIG'][
'MAIN'][
'CATEGORY'] == 4)
344 $item[
'insr-value'] = $price;
346 if($shipmentParams[
'DELIVERY_SERVICE_CONFIG'][
'MAIN'][
'CATEGORY'] == 4)
347 $item[
'payment'] = $price;
349 if (!empty($shipmentParams[
'LOCATION_TO_TYPES'][
'REGION']))
351 $item[
'region-to'] = $this->convertLocationTypeToString($shipmentParams[
'LOCATION_TO_TYPES'][
'REGION']);
354 if (!empty($shipmentParams[
'LOCATION_TO_TYPES'][
'SUBREGION']))
356 $item[
'area-to'] = $this->convertLocationTypeToString($shipmentParams[
'LOCATION_TO_TYPES'][
'SUBREGION']);
359 if (!empty($shipmentParams[
'LOCATION_TO_TYPES'][
'STREET']))
361 $item[
'street-to'] = $this->convertLocationTypeToString($shipmentParams[
'LOCATION_TO_TYPES'][
'STREET']);
364 if (!empty($shipmentParams[
'LOCATION_TO_TYPES'][
'VILLAGE']))
366 $item[
'place-to'] = $this->convertLocationTypeToString($shipmentParams[
'LOCATION_TO_TYPES'][
'VILLAGE']);
368 elseif (!empty($shipmentParams[
'LOCATION_TO_TYPES'][
'CITY']))
370 $item[
'place-to'] = $this->convertLocationTypeToString($shipmentParams[
'LOCATION_TO_TYPES'][
'CITY']);
373 if (!empty($shipmentParams[
'ZIP_TO']))
375 $item[
'index-to'] = $shipmentParams[
'ZIP_TO'];
379 $types = [
'COUNTRY',
'REGION',
'SUBREGION',
'CITY',
'VILLAGE',
'STREET'];
381 foreach($types as $type)
383 if (empty($shipmentParams[
'LOCATION_TO_TYPES'][$type]))
393 $address .= $this->convertLocationTypeToString($shipmentParams[
'LOCATION_TO_TYPES'][$type]);
396 if(!empty($shipmentParams[
'ADDRESS']))
401 $address .= $shipmentParams[
'ADDRESS'];
404 $addresses[$shipment->getId()] = $address;
405 $fios[$shipment->getId()] = $shipmentParams[
'PAYER_NAME'];
406 $resultData[$shipment->getId()] = $item;
409 if(!empty($resultData))
411 $normalizeResult = $this->normalizeAddresses($addresses);
413 if(!$normalizeResult->isSuccess())
415 $result->addErrors($normalizeResult->getErrors());
420 foreach($normalizeResult->getData() as $address)
422 $shipmentId = $address[
'id'];
424 if(!isset($resultData[$shipmentId]))
427 if(!$this->isAddressGood($address))
429 $shpResult =
new Requests\ShipmentResult($shipmentId);
430 $shpResult->addError(
new Error(
431 $qualityCodes[$address[
'quality-code']].
'. '.Loc::getMessage(
'SALE_DLVRS_ADD_DREQ_ROC_02').
'. "'.
432 print_r($address[
'original-address'],
true).
'"',
435 $result->addResult($shpResult);
436 $notValidShipmentIds[$shipmentId] =
true;
440 if(!empty($address[
'area']) && empty($resultData[$shipmentId][
'area-to']))
441 $resultData[$shipmentId][
'area-to'] = $address[
'area'];
443 if(!empty($address[
'house']))
444 $resultData[$shipmentId][
'house-to'] = $address[
'house'];
446 if(!empty($address[
'place']))
447 $resultData[$shipmentId][
'place-to'] = $address[
'place'];
449 if(!empty($address[
'region']) && empty($resultData[$shipmentId][
'region-to']))
450 $resultData[$shipmentId][
'region-to'] = $address[
'region'];
452 if(!empty($address[
'room']))
453 $resultData[$shipmentId][
'room-to'] = $address[
'room'];
455 if(!empty($address[
'slash']))
456 $resultData[$shipmentId][
'slash-to'] = $address[
'slash'];
458 if(!empty($address[
'building']))
459 $resultData[$shipmentId][
'building-to'] = $address[
'building'];
461 if(!empty($address[
'corpus']))
462 $resultData[$shipmentId][
'corpus-to'] = $address[
'corpus'];
464 if(!empty($address[
'hotel']))
465 $resultData[$shipmentId][
'hotel-to'] = $address[
'hotel'];
467 if(!empty($address[
'letter']))
468 $resultData[$shipmentId][
'letter-to'] = $address[
'letter'];
470 if(!empty($address[
'location']))
471 $resultData[$shipmentId][
'location-to'] = $address[
'location'];
473 if(!empty($address[
'street']) && empty($resultData[$shipmentId][
'street-to']))
474 $resultData[$shipmentId][
'street-to'] = $address[
'street'];
476 if(!empty($address[
'place']) && empty($resultData[$shipmentId][
'place-to']))
477 $resultData[$shipmentId][
'place-to'] = $address[
'place'];
479 if(!empty($address[
'index']) && empty($resultData[$shipmentId][
'index-to']))
480 $resultData[$shipmentId][
'index-to'] = $address[
'index'];
482 if(!empty($address[
'num-address-type']))
483 $resultData[$shipmentId][
'num-address-type-to'] = $address[
'num-address-type'];
487 $normalizeResult = $this->normalizeFios($fios);
489 if(!$normalizeResult->isSuccess())
491 $result->addErrors($normalizeResult->getErrors());
495 foreach($normalizeResult->getData() as $fio)
497 $shipmentId = $fio[
'id'];
499 if(!isset($resultData[$shipmentId]) || $notValidShipmentIds[$shipmentId])
502 if((!isset($fio[
"valid"]) || $fio[
"valid"] !==
false) && $fio[
"quality-code"] !=
'NOT_SURE' )
504 if(!empty($fio[
'middle-name']))
505 $resultData[$shipmentId][
'middle-name'] = $fio[
'middle-name'];
507 if(!empty($fio[
'surname']))
508 $resultData[$shipmentId][
'surname'] = $fio[
'surname'];
510 if(!empty($fio[
'name']))
511 $resultData[$shipmentId][
'given-name'] = $fio[
'name'];
514 $resultData[$shipmentId][
'recipient-name'] = $fio[
'original-fio'];
518 if(!empty($notValidShipmentIds))
519 foreach($notValidShipmentIds as $shipmentId => $t)
520 unset($resultData[$shipmentId]);
522 if(!empty($resultData))
523 $result->setData(array_values($resultData));
525 $result->addError(
new Error(Loc::getMessage(
'SALE_DLVRS_ADD_DREQ_ROC_DATA_EMPTY')));
534 private function convertLocationTypeToString(
array $locationTypeComponent): string
536 return implode(
', ', $locationTypeComponent);
if(!Loader::includeModule('catalog')) if(!AccessController::getCurrent() ->check(ActionDictionary::ACTION_PRICE_EDIT)) if(!check_bitrix_sessid()) $request
static countMinContainerSize(array $boxesSizes)
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
if(!function_exists(__NAMESPACE__.'\\___972068685'))
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)