56 if (intval($shipmentId) <= 0)
62 $res = ShipmentTable::getList(array(
64 '=SHIPMENT_ID' => $shipmentId
68 'DELIVERY_ID' =>
'SHIPMENT.DELIVERY_ID'
72 if (!($row = $res->fetch()))
77 'SALE_DLVR_REQ_MNGR_ERROR_SHIPMENT_NOT_IN_REQUEST',
78 array(
'#SHIPMENT_ID#' => $shipmentId)
84 $deliveryId = intval($row[
'DELIVERY_ID']);
91 'SALE_DLVR_REQ_MNGR_ERROR_DELIVERY_NOT_FOUND',
97 $deliveryRequestHandler = self::getDeliveryRequestHandlerByDeliveryId($deliveryId);
99 if (!$deliveryRequestHandler)
104 'SALE_DLVR_REQ_MNGR_ERROR_DELIVERY_NOT_SUPPORT',
110 return $deliveryRequestHandler->getShipmentContent($requestId, $shipmentId);
122 if ($deliveryId <= 0)
128 if (!($deliveryRequestHandler = $delivery->getDeliveryRequestHandler()))
131 return $deliveryRequestHandler->getShipmentActions($shipment);
142 $deliveryRequestHandler = self::getDeliveryRequestHandlerByRequestId($requestId);
144 if ($deliveryRequestHandler)
145 $result = $deliveryRequestHandler->getActions($requestId);
157 if (intval($requestId) <= 0)
160 if (!($requestFields = RequestTable::getById($requestId)->fetch()))
163 if (intval($requestFields[
'DELIVERY_ID']) <= 0)
166 return self::getDeliveryRequestHandlerByDeliveryId($requestFields[
'DELIVERY_ID']);
178 if (intval($deliveryId) <= 0)
184 return $delivery->getDeliveryRequestHandler();
198 public static function createDeliveryRequest($deliveryId, array $shipmentIds, array $additional = array())
202 if (!($deliveryRequestHandler = self::getDeliveryRequestHandlerByDeliveryId($deliveryId)))
207 'SALE_DLVR_REQ_MNGR_ERROR_NOT_SUPPORT',
210 self::sendOnCreateDeliveryRequestEvent($result, $deliveryId, $shipmentIds, $additional);
216 $checkResults = self::checkShipmentIdsBeforeAdd($shipmentIds);
218 foreach ($checkResults as $res)
220 if ($res->isSuccess())
224 self::processShipmentResult(
228 unset($shipmentIds[array_search($res->getInternalId(), $shipmentIds)]);
231 if (empty($shipmentIds))
234 self::sendOnCreateDeliveryRequestEvent($result, $deliveryId, $shipmentIds, $additional);
239 $handlerResult = $deliveryRequestHandler->create($shipmentIds, $additional);
240 if ($handlerResult->isSuccess())
242 $result->addResults($handlerResult->getResults());
246 $result->addErrors($handlerResult->getErrors());
248 foreach ($handlerResult->getShipmentResults() as $sRes)
250 $result->addResult(self::processShipmentResult($sRes));
252 self::sendOnCreateDeliveryRequestEvent($result, $deliveryId, $shipmentIds, $additional);
257 $results = $result->getResults();
259 if (!is_array($results) || empty($results))
263 self::sendOnCreateDeliveryRequestEvent($result, $deliveryId, $shipmentIds, $additional);
269 foreach ($results as $resId => $requestResult)
271 if ($requestResult instanceof ShipmentResult)
273 $requestResult = self::processShipmentResult($requestResult);
275 elseif ($requestResult instanceof RequestResult)
279 if ($requestResult->isSuccess())
281 $handlerResultData = $handlerResult->getData();
283 $res = RequestTable::add(array(
284 'DELIVERY_ID' => $deliveryRequestHandler->getHandlingDeliveryServiceId(),
285 'EXTERNAL_ID' => $requestResult->getExternalId(),
286 'CREATED_BY' => (
$GLOBALS[
'USER'] instanceof \CUser && (
int)
$GLOBALS[
'USER']->GetID() > 0)
290 'EXTERNAL_STATUS' => $handlerResultData[
'STATUS'] ?? null,
291 'EXTERNAL_STATUS_SEMANTIC' => $handlerResultData[
'STATUS_SEMANTIC'] ?? null,
292 'EXTERNAL_PROPERTIES' => (isset($handlerResultData[
'PROPERTIES']) && is_array($handlerResultData[
'PROPERTIES']))
293 ? $handlerResultData[
'PROPERTIES']
297 if (!$res->isSuccess())
299 $requestResult->addErrors($res->getErrors());
303 $requestId = $res->getId();
307 $requestResult->setInternalId($requestId);
311 $shipmentsResults = $requestResult->getShipmentResults();
313 if (empty($shipmentsResults))
316 foreach ($shipmentsResults as $sResIdx => $shipmentResult)
318 $shipmentsResults[$sResIdx] = self::processShipmentResult($shipmentResult, $requestId);
321 $requestResult->setResults($shipmentsResults);
325 $result->addError(
new Main\
Error(
Loc::getMessage(
'SALE_DLVR_REQ_MNGR_ERROR_ANSW_TYPE').
' "'.get_class($requestResult).
'"'));
329 $results[$resId] = $requestResult;
332 $result->setResults($results);
333 self::sendOnCreateDeliveryRequestEvent($result, $deliveryId, $shipmentIds, $additional);
338 private static function sendOnCreateDeliveryRequestEvent(
347 self::REQUEST_CREATED_EVENT_CODE,
349 'DELIVERY_ID' => $deliveryId,
350 'SHIPMENT_IDS' => $shipmentIds,
351 'ADDITIONAL' => $additional,
367 $shipmentId = $result->getInternalId();
369 if (intval($shipmentId) <= 0)
371 $result->addError(
new Main\
Error(
Loc::getMessage(
'SALE_DLVR_REQ_MNGR_ERROR_SAVE_SHIPMENT_ID')));
375 $extShipmentId = $result->getExternalId();
377 if ($result->isSuccess())
379 if (intval($requestId) > 0)
382 'REQUEST_ID' => $requestId,
383 'SHIPMENT_ID' => $shipmentId,
384 'EXTERNAL_ID' => $extShipmentId,
385 'ERROR_DESCRIPTION' =>
''
388 if (!$res->isSuccess())
389 $result->addErrors($res->getErrors());
391 $res = self::saveShipmentResult($shipmentId, $result);
393 if (!$res->isSuccess())
394 $result->addErrors($res->getErrors());
400 'SHIPMENT_ID' => $shipmentId,
401 'ERROR_DESCRIPTION' => implode(
"\n", $result->getErrorMessages())
418 if (!$deliveryRequestHandler = self::getDeliveryRequestHandlerByDeliveryId($deliveryId))
421 return $deliveryRequestHandler->getFormFields($formFieldsType, $shipmentIds, $additional);
434 if (empty($requestId))
440 if ($deliveryRequestHandler = self::getDeliveryRequestHandlerByRequestId($requestId))
442 $res = $deliveryRequestHandler->delete($requestId);
444 if (!$res->isSuccess())
445 $result->addErrors($res->getErrors());
448 if ($result->isSuccess())
450 $requestShipmentList = ShipmentTable::getList(array(
'filter' => array(
'=REQUEST_ID' => $requestId)));
451 while ($requestShipment = $requestShipmentList->fetch())
453 $shipmentIds[] = $requestShipment[
'SHIPMENT_ID'];
456 $con = Main\Application::getConnection();
458 $res = RequestTable::delete($requestId);
460 if (!$res->isSuccess())
461 $result->addErrors($res->getErrors());
466 self::REQUEST_DELETED_EVENT_CODE,
468 'REQUEST_ID' => $requestId,
469 'SHIPMENT_IDS' => $shipmentIds,
474 if ($result->isSuccess() && Loader::includeModule(
'pull'))
476 \CPullWatch::AddToStack(
477 'SALE_DELIVERY_REQUEST',
479 'module_id' =>
'sale',
480 'command' =>
'onDeliveryRequestDelete',
501 if (empty($requestId))
507 if (isset($fields[
'EXTERNAL_PROPERTIES']) && !is_array($fields[
'EXTERNAL_PROPERTIES']))
509 unset($fields[
'EXTERNAL_PROPERTIES']);
513 $overwriteProperties ===
false
514 && isset($fields[
'EXTERNAL_PROPERTIES'])
515 && ($existingRequestFields = RequestTable::getById($requestId)->fetch())
518 $existingProperties = array_column(
519 is_null($existingRequestFields[
'EXTERNAL_PROPERTIES'])
521 : $existingRequestFields[
'EXTERNAL_PROPERTIES'],
525 $newProperties = array_column(
526 $fields[
'EXTERNAL_PROPERTIES'],
531 $fields[
'EXTERNAL_PROPERTIES'] = array_values(array_merge($existingProperties, $newProperties));
534 $updateResult = RequestTable::update(
541 'EXTERNAL_STATUS_SEMANTIC',
542 'EXTERNAL_PROPERTIES',
546 if (!$updateResult->isSuccess())
548 $result->addErrors($updateResult->getErrors());
553 self::REQUEST_UPDATED_EVENT_CODE,
555 'REQUEST_ID' => $requestId,
561 if ($result->isSuccess() && Loader::includeModule(
'pull'))
563 \CPullWatch::AddToStack(
564 'SALE_DELIVERY_REQUEST',
566 'module_id' =>
'sale',
567 'command' =>
'onDeliveryRequestUpdate',
583 public static function deleteShipmentsFromDeliveryRequest($requestId, array $shipmentIds)
587 if (empty($requestId))
593 if (empty($shipmentIds))
595 $result->addError(
new Main\
Error(
Loc::getMessage(
'SALE_DLVR_REQ_MNGR_ERROR_SHP_ID_LIST_EMPTY')));
599 if (!($deliveryRequestHandler = self::getDeliveryRequestHandlerByRequestId($requestId)))
604 'SALE_DLVR_REQ_MNGR_ERROR_REQ_OBJ'
610 $checkResults = self::checkShipmentIdsBeforeDelete($shipmentIds);
612 foreach ($checkResults as $res)
614 if (!$res->isSuccess())
616 $result->addErrors($res->getErrors());
617 unset($shipmentIds[array_search($res->getInternalId(), $shipmentIds)]);
621 if (empty($shipmentIds))
624 $res = $deliveryRequestHandler->deleteShipments($requestId, $shipmentIds);
625 $result->setResults($res->getResults());
627 if (!$res->isSuccess())
629 $result->addErrors($res->getErrors());
633 $results = $result->getResults();
635 if (!is_array($results) || empty($results))
641 $con = Main\Application::getConnection();
644 foreach ($results as $resId => $shpRes)
646 if (!($shpRes instanceof ShipmentResult))
648 $result->addError(
new Main\
Error(
Loc::getMessage(
'SALE_DLVR_REQ_MNGR_ERROR_ANSW_TYPE').
' "'.get_class($shpRes).
'"'));
652 if ($shpRes->isSuccess())
654 $shpId = intval($shpRes->getInternalId());
662 $con->queryExecute(
"DELETE FROM ".
ShipmentTable::getTableName().
" WHERE REQUEST_ID=".intval($requestId).
" AND SHIPMENT_ID=".intval($shpId));
664 self::unSetMarkerShipmentChanged($shpId);
667 if (!ShipmentTable::getList(array(
'filter' => array(
'=REQUEST_ID' => $requestId)))->fetch())
669 $res = RequestTable::delete($requestId);
671 if ($res->isSuccess())
676 'SALE_DLVR_REQ_MNGR_EMPTY_REQ_DELETED',
677 array(
'#REQUEST_ID#' => $requestId)
685 'SALE_DLVR_REQ_MNGR_EMPTY_REQ_NOT_DELETED',
687 ).implode(
'; ',$result->getErrorMessages())
693 $results[$resId] = $shpRes;
696 $result->setResults($results);
706 $deliveryRequestHandler = self::getDeliveryRequestHandlerByRequestId($requestId);
708 if (!$deliveryRequestHandler)
715 return $deliveryRequestHandler->getContent($requestId);
726 $deliveryRequestHandler = self::getDeliveryRequestHandlerByRequestId($requestId);
728 if (!$deliveryRequestHandler)
735 $result = $deliveryRequestHandler->executeAction($requestId, $actionType, $additional);
739 self::REQUEST_ACTION_EXECUTED_EVENT_CODE,
741 'REQUEST_ID' => $requestId,
742 'ACTION_TYPE' => $actionType,
743 'ADDITIONAL' => $additional,
745 'DELIVERY_REQUEST_HANDLER' => $deliveryRequestHandler,
761 $deliveryRequestHandler = self::getDeliveryRequestHandlerByRequestId($requestId);
763 if (!$deliveryRequestHandler)
770 return $deliveryRequestHandler->executeShipmentAction($requestId, $shipmentId, $actionType, $additional);
781 $positiveIds = self::filterPositiveIds($shipmentIds);
783 foreach (array_diff($shipmentIds, $positiveIds) as $id)
794 $addedIds = self::filterAddedIds($positiveIds);
796 foreach (array_intersect($positiveIds, $addedIds) as $id)
806 $existingIds = self::filterExistIds($addedIds);
808 foreach (array_diff($addedIds, $existingIds) as $id)
813 Loc::getMessage(
'SALE_DLVR_REQ_MNGR_SHP_NOT_FOUND', array(
'#SHIPMENT_ID#' => $id)
829 $res = Internals\ShipmentTable::getList(array(
831 '=ID' => $shipmentIds,
835 while ($row = $res->fetch())
836 $result[] = $row[
'ID'];
849 $res = ShipmentTable::getList(array(
851 '=SHIPMENT_ID' => $shipmentIds,
852 '!=REQUEST_ID' =>
false
856 while ($row = $res->fetch())
857 $result[] = $row[
'SHIPMENT_ID'];
870 foreach ($shipmentIds as $id)
883 return self::checkShipmentIdsBeforeUpdate($shipmentIds);
894 $positiveIds = self::filterPositiveIds($shipmentIds);
896 foreach (array_diff($shipmentIds, $positiveIds) as $id)
906 $addedIds = self::filterAddedIds($positiveIds);
908 foreach (array_diff($positiveIds, $addedIds) as $id)
913 Loc::getMessage(
'SALE_DLVR_REQ_MNGR_ERROR_SHIPMENT_NOT_IN_REQUEST', array(
'#SHIPMENT_ID#' => $id)
918 $existingIds = self::filterExistIds($addedIds);
920 foreach (array_diff($addedIds, $existingIds) as $id)
925 Loc::getMessage(
'SALE_DLVR_REQ_MNGR_SHP_NOT_FOUND', array(
'#SHIPMENT_ID#' => $id)
939 return intval(self::getRequestIdByShipmentId($shipmentId)) > 0;
950 $res = ShipmentTable::getList(array(
952 '=SHIPMENT_ID' => $shipmentId,
956 if ($row = $res->fetch())
957 $result = $row[
'REQUEST_ID'];
968 public static function addShipmentsToDeliveryRequest($requestId, array $shipmentIds, array $additional = [])
972 if (empty($requestId))
978 if (empty($shipmentIds))
980 $result->addError(
new Main\
Error(
Loc::getMessage(
'SALE_DLVR_REQ_MNGR_ERROR_SHP_ID_LIST_EMPTY')));
984 if (!($deliveryRequestHandler = self::getDeliveryRequestHandlerByRequestId($requestId)))
996 $checkResults = self::checkShipmentIdsBeforeAdd($shipmentIds);
998 foreach ($checkResults as $res)
1000 if ($res->isSuccess())
1004 self::processShipmentResult(
1008 unset($shipmentIds[array_search($res->getInternalId(), $shipmentIds)]);
1011 if (empty($shipmentIds))
1013 $result->addError(
new Main\Error(
Loc::getMessage(
'SALE_DLVR_REQ_MNGR_ERROR_SHP_ABSENT')));
1017 $res = $deliveryRequestHandler->addShipments($requestId, $shipmentIds, $additional);
1019 if ($res->isSuccess())
1021 $result->addResults($res->getResults());
1025 $result->addErrors($res->getErrors());
1027 foreach ($res->getShipmentResults() as $sRes)
1028 $result->addResult(self::processShipmentResult($sRes));
1033 $results = $result->getResults();
1035 if (!is_array($results) || empty($results))
1041 $successResCount = 0;
1044 foreach ($results as $resId => $reqRes)
1046 if ($reqRes instanceof ShipmentResult)
1048 $results[$resId] = self::processShipmentResult($reqRes, $requestId);
1050 elseif ($reqRes instanceof RequestResult)
1052 $reqShpResults = $reqRes->getShipmentResults();
1054 foreach ($reqShpResults as $id => $shpRes)
1056 $reqShpResults[$id] = self::processShipmentResult($shpRes, $requestId);
1058 if ($shpRes->isSuccess())
1062 $reqRes->setResults($reqShpResults);
1063 $results[$resId] = $reqRes;
1070 'SALE_DLVR_REQ_MNGR_RES_WRONG',
1072 '#CLASS_NAME#' => get_class($reqRes),
1073 '#REQUEST_ID#' => $requestId
1083 if ($successResCount <= 0)
1086 $result->setResults($results);
1100 if (empty($requestId))
1106 if (empty($shipmentIds))
1108 $result->addError(
new Main\
Error(
Loc::getMessage(
'SALE_DLVR_REQ_MNGR_ERROR_SHP_ID_LIST_EMPTY')));
1112 $checkResults = self::checkShipmentIdsBeforeUpdate($shipmentIds);
1114 foreach ($checkResults as $res)
1116 if ($res->isSuccess())
1120 self::processShipmentResult($res, $requestId)
1123 unset($shipmentIds[array_search($res->getInternalId(), $shipmentIds)]);
1126 if (empty($shipmentIds))
1131 'SALE_DLVR_REQ_MNGR_ERROR_SHP_UPD',
1137 $deliveryRequestHandler = self::getDeliveryRequestHandlerByRequestId($requestId);
1139 if (!$deliveryRequestHandler)
1144 'SALE_DLVR_REQ_MNGR_ERROR_SHP_UPD',
1153 $res = $deliveryRequestHandler->updateShipments($requestId, $shipmentIds);
1154 $result->addResults($res->getResults());
1156 if (!$res->isSuccess())
1158 $result->addErrors($res->getErrors());
1162 $results = $res->getResults();
1164 if (empty($results))
1170 $resultsFinal = array();
1172 foreach ($results as $res)
1176 $resultsFinal[] = self::processShipmentResult($res, $requestId);
1180 $reqShpResults = $res->getShipmentResults();
1182 foreach ($reqShpResults as $id => $shpRes)
1184 $shpRes = self::processShipmentResult($shpRes, $requestId);
1186 if ($shpRes->isSuccess())
1188 $shpInternalId = intval($shpRes->getInternalId());
1190 self::unSetMarkerShipmentChanged($shpInternalId);
1192 $dbRes = self::saveShipmentResult($shpRes->getInternalId(), $shpRes);
1194 if (!$dbRes->isSuccess())
1195 $shpRes->addErrors($dbRes->getErrors());
1198 $resultsFinal[] = $shpRes;
1212 $result->setResults($resultsFinal);
1230 $shipments = Helper::getShipmentsByIds(array($shipmentId));
1232 if ($shipments[$shipmentId])
1234 $shipments[$shipmentId]->setFields(array(
1240 static::$isChangedShipmentNeedsMark =
false;
1241 $res = $shipments[$shipmentId]->getOrder()->save();
1242 static::$isChangedShipmentNeedsMark =
true;
1244 if (!$res->isSuccess())
1245 $result->addError(
new Main\
Error(
Loc::getMessage(
'SALE_DLVR_REQ_MNGR_ERROR_SAVE_SHIPMENT').
'"'.$shipmentId.
'"'));
1252 'SALE_DLVR_REQ_MNGR_SHP_NOT_FOUND',
1253 array(
'#SHIPMENT_ID#' => $shipmentId)
1266 if (static::$isChangedShipmentNeedsMark && self::isShipmentSent($shipment->getId()))
1268 self::setMarkerShipmentChanged($order, $shipment);
1277 $shipmentId = $shipment->getId();
1279 if (self::isShipmentSent($shipmentId))
1281 self::deleteShipmentsFromDeliveryRequest(
1282 self::getRequestIdByShipmentId($shipmentId),
1294 $r = new \Bitrix\Sale\Result();
1299 'SALE_DLVR_REQ_MNGR_NOT_UPDATED'
1301 'DELIVERY_REQUEST_NOT_UPDATED'
1304 EntityMarker::addMarker($order, $shipment, $r);
1305 $shipment->setField(
'MARKED',
'Y');
1315 '=ENTITY_ID' => $shipmentId,
1316 '=CODE' =>
'DELIVERY_REQUEST_NOT_UPDATED'
1335 self::MESSAGE_RECEIVED_EVENT_CODE,
1337 'ADDRESSEE' => $addressee,
1338 'REQUEST_ID' => $requestId,
1339 'SHIPMENT_ID' => $shipmentId,
1340 'MESSAGE' => $message,
1351 self::MESSAGE_MANAGER_ADDRESSEE,
1352 self::MESSAGE_RECIPIENT_ADDRESSEE,
1362 self::EXTERNAL_STATUS_SEMANTIC_SUCCESS,
1363 self::EXTERNAL_STATUS_SEMANTIC_PROCESS,
1369 \CJSCore::RegisterExt(
'sale_delivery_requests', array(
1370 'js' =>
'/bitrix/js/sale/delivery_request.js',
1371 'lang' =>
'/bitrix/modules/sale/lang/' . LANGUAGE_ID .
'/admin/js/sale_delivery_requests.php',
1372 'rel' => array(
'core',
'ajax')
1375 \CUtil::InitJSCore(array(
'sale_delivery_requests'));