Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
manager.php
1<?php
2
4
14
15Loc::loadMessages(__FILE__);
16
22final class Manager
23{
24 public const STATUS_PREPARED = 0;
25 public const STATUS_SENT = 10;
26 public const STATUS_PROCESSED = 20;
27
31
32 public const REQUEST_CREATED_EVENT_CODE = 'OnDeliveryRequestCreated';
33 public const REQUEST_DELETED_EVENT_CODE = 'OnDeliveryRequestDeleted';
34 public const REQUEST_UPDATED_EVENT_CODE = 'OnDeliveryRequestUpdated';
35 public const REQUEST_ACTION_EXECUTED_EVENT_CODE = 'OnDeliveryRequestActionExecuted';
36
37 public const MESSAGE_RECEIVED_EVENT_CODE = 'OnDeliveryRequestMessageReceived';
38 public const MESSAGE_MANAGER_ADDRESSEE = 'MANAGER';
39 public const MESSAGE_RECIPIENT_ADDRESSEE = 'RECIPIENT';
40
41 public const EXTERNAL_STATUS_SEMANTIC_SUCCESS = 'success';
42 public const EXTERNAL_STATUS_SEMANTIC_PROCESS = 'process';
43
44 protected static $isChangedShipmentNeedsMark = true;
45
52 public static function getDeliveryRequestShipmentContent($requestId, $shipmentId)
53 {
54 $result = new Result();
55
56 if (intval($shipmentId) <= 0)
57 {
58 $result->addError(new Main\Error(Loc::getMessage('SALE_DLVR_REQ_MNGR_ERROR_SHIPMENT_ID')));
59 return $result;
60 }
61
62 $res = ShipmentTable::getList(array(
63 'filter' => array(
64 '=SHIPMENT_ID' => $shipmentId
65 ),
66 'select' => array(
67 '*',
68 'DELIVERY_ID' => 'SHIPMENT.DELIVERY_ID'
69 )
70 ));
71
72 if (!($row = $res->fetch()))
73 {
74 $result->addError(
75 new Main\Error(
77 'SALE_DLVR_REQ_MNGR_ERROR_SHIPMENT_NOT_IN_REQUEST',
78 array('#SHIPMENT_ID#' => $shipmentId)
79 )));
80
81 return $result;
82 }
83
84 $deliveryId = intval($row['DELIVERY_ID']);
85
86 if ($deliveryId <= 0)
87 {
88 $result->addError(
89 new Main\Error(
91 'SALE_DLVR_REQ_MNGR_ERROR_DELIVERY_NOT_FOUND',
92 array('#SHIPMENT_LINK#' => Helper::getShipmentEditLink($shipmentId)))));
93
94 return $result;
95 }
96
97 $deliveryRequestHandler = self::getDeliveryRequestHandlerByDeliveryId($deliveryId);
98
99 if (!$deliveryRequestHandler)
100 {
101 $result->addError(
102 new Main\Error(
104 'SALE_DLVR_REQ_MNGR_ERROR_DELIVERY_NOT_SUPPORT',
105 array('#DELIVERY_LINK#' => Helper::getDeliveryEditLink($deliveryId)))));
106
107 return $result;
108 }
109
110 return $deliveryRequestHandler->getShipmentContent($requestId, $shipmentId);
111 }
112
118 public static function getDeliveryRequestShipmentActions(Shipment $shipment)
119 {
120 $deliveryId = $shipment->getDeliveryId();
121
122 if ($deliveryId <= 0)
123 return array();
124
125 if (!($delivery = Services\Manager::getObjectById($deliveryId)))
126 return array();
127
128 if (!($deliveryRequestHandler = $delivery->getDeliveryRequestHandler()))
129 return array();
130
131 return $deliveryRequestHandler->getShipmentActions($shipment);
132 }
133
139 public static function getDeliveryRequestActions($requestId)
140 {
141 $result = array();
142 $deliveryRequestHandler = self::getDeliveryRequestHandlerByRequestId($requestId);
143
144 if ($deliveryRequestHandler)
145 $result = $deliveryRequestHandler->getActions($requestId);
146
147 return $result;
148 }
149
155 protected static function getDeliveryRequestHandlerByRequestId($requestId)
156 {
157 if (intval($requestId) <= 0)
158 return null;
159
160 if (!($requestFields = RequestTable::getById($requestId)->fetch()))
161 return null;
162
163 if (intval($requestFields['DELIVERY_ID']) <= 0)
164 return null;
165
166 return self::getDeliveryRequestHandlerByDeliveryId($requestFields['DELIVERY_ID']);
167 }
168
176 public static function getDeliveryRequestHandlerByDeliveryId($deliveryId)
177 {
178 if (intval($deliveryId) <= 0)
179 return null;
180
181 if (!($delivery = Services\Manager::getObjectById($deliveryId)))
182 return null;
183
184 return $delivery->getDeliveryRequestHandler();
185 }
186
198 public static function createDeliveryRequest($deliveryId, array $shipmentIds, array $additional = array())
199 {
200 $result = new Result();
201
202 if (!($deliveryRequestHandler = self::getDeliveryRequestHandlerByDeliveryId($deliveryId)))
203 {
204 $result->addError(
205 new Main\Error(
207 'SALE_DLVR_REQ_MNGR_ERROR_NOT_SUPPORT',
208 array('#DELIVERY_LINK#' => Helper::getDeliveryEditLink($deliveryId))
209 )));
210 self::sendOnCreateDeliveryRequestEvent($result, $deliveryId, $shipmentIds, $additional);
211
212 return $result;
213 }
214
216 $checkResults = self::checkShipmentIdsBeforeAdd($shipmentIds);
217
218 foreach ($checkResults as $res)
219 {
220 if ($res->isSuccess())
221 continue;
222
223 $result->addResult(
224 self::processShipmentResult(
225 $res
226 ));
227
228 unset($shipmentIds[array_search($res->getInternalId(), $shipmentIds)]);
229 }
230
231 if (empty($shipmentIds))
232 {
233 $result->addError(new Main\Error(Loc::getMessage('SALE_DLVR_REQ_MNGR_ERROR_SHP_ABSENT')));
234 self::sendOnCreateDeliveryRequestEvent($result, $deliveryId, $shipmentIds, $additional);
235
236 return $result;
237 }
238
239 $handlerResult = $deliveryRequestHandler->create($shipmentIds, $additional);
240 if ($handlerResult->isSuccess())
241 {
242 $result->addResults($handlerResult->getResults());
243 }
244 else
245 {
246 $result->addErrors($handlerResult->getErrors());
247
248 foreach ($handlerResult->getShipmentResults() as $sRes)
249 {
250 $result->addResult(self::processShipmentResult($sRes));
251 }
252 self::sendOnCreateDeliveryRequestEvent($result, $deliveryId, $shipmentIds, $additional);
253
254 return $result;
255 }
256
257 $results = $result->getResults();
258
259 if (!is_array($results) || empty($results))
260 {
261 $result->addError(new Main\Error(Loc::getMessage('SALE_DLVR_REQ_MNGR_ERROR_RES_UNKNOWN')));
262
263 self::sendOnCreateDeliveryRequestEvent($result, $deliveryId, $shipmentIds, $additional);
264
265 return $result;
266 }
267
269 foreach ($results as $resId => $requestResult)
270 {
271 if ($requestResult instanceof ShipmentResult)
272 {
273 $requestResult = self::processShipmentResult($requestResult);
274 }
275 elseif ($requestResult instanceof RequestResult)
276 {
277 $requestId = 0;
279 if ($requestResult->isSuccess())
280 {
281 $handlerResultData = $handlerResult->getData();
282
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)
287 ? (int)$GLOBALS['USER']->GetID()
288 : null,
289 'STATUS' => Manager::STATUS_SENT,
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']
294 : [],
295 ));
296
297 if (!$res->isSuccess())
298 {
299 $requestResult->addErrors($res->getErrors());
300 continue;
301 }
302
303 $requestId = $res->getId();
304
305 if ($requestId > 0)
306 {
307 $requestResult->setInternalId($requestId);
308 }
309 }
310
311 $shipmentsResults = $requestResult->getShipmentResults();
312
313 if (empty($shipmentsResults))
314 continue;
315
316 foreach ($shipmentsResults as $sResIdx => $shipmentResult)
317 {
318 $shipmentsResults[$sResIdx] = self::processShipmentResult($shipmentResult, $requestId);
319 }
320
321 $requestResult->setResults($shipmentsResults);
322 }
323 else
324 {
325 $result->addError(new Main\Error(Loc::getMessage('SALE_DLVR_REQ_MNGR_ERROR_ANSW_TYPE').' "'.get_class($requestResult).'"'));
326 continue;
327 }
328
329 $results[$resId] = $requestResult;
330 }
331
332 $result->setResults($results);
333 self::sendOnCreateDeliveryRequestEvent($result, $deliveryId, $shipmentIds, $additional);
334
335 return $result;
336 }
337
338 private static function sendOnCreateDeliveryRequestEvent(
339 Result $result,
340 int $deliveryId,
341 array $shipmentIds,
342 array $additional
343 )
344 {
345 (new Main\Event(
346 'sale',
347 self::REQUEST_CREATED_EVENT_CODE,
348 [
349 'DELIVERY_ID' => $deliveryId,
350 'SHIPMENT_IDS' => $shipmentIds,
351 'ADDITIONAL' => $additional,
352 'RESULT' => $result,
353 ]
354 ))->send();
355 }
356
362 protected static function processShipmentResult($result, $requestId = 0)
363 {
364 if (!($result instanceof ShipmentResult))
365 return $result;
366
367 $shipmentId = $result->getInternalId();
368
369 if (intval($shipmentId) <= 0)
370 {
371 $result->addError(new Main\Error(Loc::getMessage('SALE_DLVR_REQ_MNGR_ERROR_SAVE_SHIPMENT_ID')));
372 return $result;
373 }
374
375 $extShipmentId = $result->getExternalId();
376
377 if ($result->isSuccess())
378 {
379 if (intval($requestId) > 0)
380 {
381 $res = ShipmentTable::setShipment(array(
382 'REQUEST_ID' => $requestId,
383 'SHIPMENT_ID' => $shipmentId,
384 'EXTERNAL_ID' => $extShipmentId,
385 'ERROR_DESCRIPTION' => ''
386 ));
387
388 if (!$res->isSuccess())
389 $result->addErrors($res->getErrors());
390
391 $res = self::saveShipmentResult($shipmentId, $result);
392
393 if (!$res->isSuccess())
394 $result->addErrors($res->getErrors());
395 }
396 }
397 else
398 {
400 'SHIPMENT_ID' => $shipmentId,
401 'ERROR_DESCRIPTION' => implode("\n", $result->getErrorMessages())
402 ));
403 }
404
405 return $result;
406 }
407
416 public static function getDeliveryRequestFormFields($deliveryId, $formFieldsType, array $shipmentIds, array $additional = array())
417 {
418 if (!$deliveryRequestHandler = self::getDeliveryRequestHandlerByDeliveryId($deliveryId))
419 return array();
420
421 return $deliveryRequestHandler->getFormFields($formFieldsType, $shipmentIds, $additional);
422 }
423
429 public static function deleteDeliveryRequest($requestId)
430 {
431 $result = new Result();
432 $shipmentIds = [];
433
434 if (empty($requestId))
435 {
436 $result->addError(new Main\Error(Loc::getMessage('SALE_DLVR_REQ_MNGR_ERROR_REQ_ID')));
437 return $result;
438 }
439
440 if ($deliveryRequestHandler = self::getDeliveryRequestHandlerByRequestId($requestId))
441 {
442 $res = $deliveryRequestHandler->delete($requestId);
443
444 if (!$res->isSuccess())
445 $result->addErrors($res->getErrors());
446 }
447
448 if ($result->isSuccess())
449 {
450 $requestShipmentList = ShipmentTable::getList(array('filter' => array('=REQUEST_ID' => $requestId)));
451 while ($requestShipment = $requestShipmentList->fetch())
452 {
453 $shipmentIds[] = $requestShipment['SHIPMENT_ID'];
454 }
455
456 $con = Main\Application::getConnection();
457 $con->queryExecute("DELETE FROM ".ShipmentTable::getTableName()." WHERE REQUEST_ID=".intval($requestId));
458 $res = RequestTable::delete($requestId);
459
460 if (!$res->isSuccess())
461 $result->addErrors($res->getErrors());
462 }
463
464 (new Main\Event(
465 'sale',
466 self::REQUEST_DELETED_EVENT_CODE,
467 [
468 'REQUEST_ID' => $requestId,
469 'SHIPMENT_IDS' => $shipmentIds,
470 'RESULT' => $result
471 ]
472 ))->send();
473
474 if ($result->isSuccess() && Loader::includeModule('pull'))
475 {
476 \CPullWatch::AddToStack(
477 'SALE_DELIVERY_REQUEST',
478 [
479 'module_id' => 'sale',
480 'command' => 'onDeliveryRequestDelete',
481 'params' => [
482 'ID' => $requestId,
483 ]
484 ]
485 );
486 }
487
488 return $result;
489 }
490
497 public static function updateDeliveryRequest($requestId, array $fields, bool $overwriteProperties = false): Result
498 {
499 $result = new Result();
500
501 if (empty($requestId))
502 {
503 $result->addError(new Main\Error(Loc::getMessage('SALE_DLVR_REQ_MNGR_ERROR_REQ_ID')));
504 return $result;
505 }
506
507 if (isset($fields['EXTERNAL_PROPERTIES']) && !is_array($fields['EXTERNAL_PROPERTIES']))
508 {
509 unset($fields['EXTERNAL_PROPERTIES']);
510 }
511
512 if (
513 $overwriteProperties === false
514 && isset($fields['EXTERNAL_PROPERTIES'])
515 && ($existingRequestFields = RequestTable::getById($requestId)->fetch())
516 )
517 {
518 $existingProperties = array_column(
519 is_null($existingRequestFields['EXTERNAL_PROPERTIES'])
520 ? []
521 : $existingRequestFields['EXTERNAL_PROPERTIES'],
522 null,
523 'NAME'
524 );
525 $newProperties = array_column(
526 $fields['EXTERNAL_PROPERTIES'],
527 null,
528 'NAME'
529 );
530
531 $fields['EXTERNAL_PROPERTIES'] = array_values(array_merge($existingProperties, $newProperties));
532 }
533
534 $updateResult = RequestTable::update(
535 $requestId,
536 array_intersect_key(
537 $fields,
538 array_flip([
539 'STATUS',
540 'EXTERNAL_STATUS',
541 'EXTERNAL_STATUS_SEMANTIC',
542 'EXTERNAL_PROPERTIES',
543 ])
544 )
545 );
546 if (!$updateResult->isSuccess())
547 {
548 $result->addErrors($updateResult->getErrors());
549 }
550
551 (new Main\Event(
552 'sale',
553 self::REQUEST_UPDATED_EVENT_CODE,
554 [
555 'REQUEST_ID' => $requestId,
556 'FIELDS' => $fields,
557 'RESULT' => $result,
558 ]
559 ))->send();
560
561 if ($result->isSuccess() && Loader::includeModule('pull'))
562 {
563 \CPullWatch::AddToStack(
564 'SALE_DELIVERY_REQUEST',
565 [
566 'module_id' => 'sale',
567 'command' => 'onDeliveryRequestUpdate',
568 'params' => [
569 'ID' => $requestId,
570 ]
571 ]
572 );
573 }
574
575 return $result;
576 }
577
583 public static function deleteShipmentsFromDeliveryRequest($requestId, array $shipmentIds)
584 {
585 $result = new Result();
586
587 if (empty($requestId))
588 {
589 $result->addError(new Main\Error(Loc::getMessage('SALE_DLVR_REQ_MNGR_ERROR_REQ_ID')));
590 return $result;
591 }
592
593 if (empty($shipmentIds))
594 {
595 $result->addError(new Main\Error(Loc::getMessage('SALE_DLVR_REQ_MNGR_ERROR_SHP_ID_LIST_EMPTY')));
596 return $result;
597 }
598
599 if (!($deliveryRequestHandler = self::getDeliveryRequestHandlerByRequestId($requestId)))
600 {
601 $result->addError(
602 new Main\Error(
604 'SALE_DLVR_REQ_MNGR_ERROR_REQ_OBJ'
605 )));
606
607 return $result;
608 }
609
610 $checkResults = self::checkShipmentIdsBeforeDelete($shipmentIds);
611
612 foreach ($checkResults as $res)
613 {
614 if (!$res->isSuccess())
615 {
616 $result->addErrors($res->getErrors());
617 unset($shipmentIds[array_search($res->getInternalId(), $shipmentIds)]);
618 }
619 }
620
621 if (empty($shipmentIds))
622 return $result;
623
624 $res = $deliveryRequestHandler->deleteShipments($requestId, $shipmentIds);
625 $result->setResults($res->getResults());
626
627 if (!$res->isSuccess())
628 {
629 $result->addErrors($res->getErrors());
630 return $result;
631 }
632
633 $results = $result->getResults();
634
635 if (!is_array($results) || empty($results))
636 {
637 $result->addError(new Main\Error(Loc::getMessage('SALE_DLVR_REQ_MNGR_ERROR_RES_UNKNOWN')));
638 return $result;
639 }
640
641 $con = Main\Application::getConnection();
642
644 foreach ($results as $resId => $shpRes)
645 {
646 if (!($shpRes instanceof ShipmentResult))
647 {
648 $result->addError(new Main\Error(Loc::getMessage('SALE_DLVR_REQ_MNGR_ERROR_ANSW_TYPE').' "'.get_class($shpRes).'"'));
649 continue;
650 }
651
652 if ($shpRes->isSuccess())
653 {
654 $shpId = intval($shpRes->getInternalId());
655
656 if ($shpId <= 0)
657 {
658 $result->addError(new Main\Error(Loc::getMessage('SALE_DLVR_REQ_MNGR_SHP_ID_LESS_ZERO')));
659 continue;
660 }
661
662 $con->queryExecute("DELETE FROM ".ShipmentTable::getTableName()." WHERE REQUEST_ID=".intval($requestId)." AND SHIPMENT_ID=".intval($shpId));
663 //Unset mark about changed shipment
664 self::unSetMarkerShipmentChanged($shpId);
665
666 //If there is no more shipments in this request
667 if (!ShipmentTable::getList(array('filter' => array('=REQUEST_ID' => $requestId)))->fetch())
668 {
669 $res = RequestTable::delete($requestId);
670
671 if ($res->isSuccess())
672 {
673 $result->addMessage(
674 new Message(
676 'SALE_DLVR_REQ_MNGR_EMPTY_REQ_DELETED',
677 array('#REQUEST_ID#' => $requestId)
678 )));
679 }
680 else
681 {
682 $result->addError(
683 new Main\Error(
685 'SALE_DLVR_REQ_MNGR_EMPTY_REQ_NOT_DELETED',
686 array('#REQUEST_LINK#' => Helper::getRequestViewLink($requestId))
687 ).implode('; ',$result->getErrorMessages())
688 ));
689 }
690 }
691 }
692
693 $results[$resId] = $shpRes;
694 }
695
696 $result->setResults($results);
697 return $result;
698 }
699
704 public static function getDeliveryRequestContent($requestId)
705 {
706 $deliveryRequestHandler = self::getDeliveryRequestHandlerByRequestId($requestId);
707
708 if (!$deliveryRequestHandler)
709 {
710 $result= new Result();
711 $result->addError(new Main\Error(Loc::getMessage('SALE_DLVR_REQ_MNGR_ERROR_REQ_OBJ')));
712 return $result;
713 }
714
715 return $deliveryRequestHandler->getContent($requestId);
716 }
717
724 public static function executeDeliveryRequestAction($requestId, $actionType, array $additional = array())
725 {
726 $deliveryRequestHandler = self::getDeliveryRequestHandlerByRequestId($requestId);
727
728 if (!$deliveryRequestHandler)
729 {
730 $result = new Result();
731 $result->addError(new Main\Error(Loc::getMessage('SALE_DLVR_REQ_MNGR_ERROR_ACTION_EXEC').'. '.Loc::getMessage('SALE_DLVR_REQ_MNGR_ERROR_REQ_OBJ')));
732 return $result;
733 }
734
735 $result = $deliveryRequestHandler->executeAction($requestId, $actionType, $additional);
736
737 (new Main\Event(
738 'sale',
739 self::REQUEST_ACTION_EXECUTED_EVENT_CODE,
740 [
741 'REQUEST_ID' => $requestId,
742 'ACTION_TYPE' => $actionType,
743 'ADDITIONAL' => $additional,
744 'RESULT' => $result,
745 'DELIVERY_REQUEST_HANDLER' => $deliveryRequestHandler,
746 ]
747 ))->send();
748
749 return $result;
750 }
751
759 public static function executeDeliveryRequestShipmentAction($requestId, $shipmentId, $actionType, array $additional = array())
760 {
761 $deliveryRequestHandler = self::getDeliveryRequestHandlerByRequestId($requestId);
762
763 if (!$deliveryRequestHandler)
764 {
765 $result = new Result();
766 $result->addError(new Main\Error(Loc::getMessage('SALE_DLVR_REQ_MNGR_ERROR_ACTION_EXEC').'. '.Loc::getMessage('SALE_DLVR_REQ_MNGR_ERROR_REQ_OBJ')));
767 return $result;
768 }
769
770 return $deliveryRequestHandler->executeShipmentAction($requestId, $shipmentId, $actionType, $additional);
771 }
772
778 protected static function checkShipmentIdsBeforeAdd(array $shipmentIds)
779 {
780 $result = array();
781 $positiveIds = self::filterPositiveIds($shipmentIds);
782
783 foreach (array_diff($shipmentIds, $positiveIds) as $id)
784 {
785 $shpRes = new ShipmentResult($id);
786 $shpRes->addError(
787 new Main\Error(
788 Loc::getMessage('SALE_DLVR_REQ_MNGR_ERROR_SHIPMENT_ID').' "'.$id.'"',
789 $id
790 ));
791 $result[] = $shpRes;
792 }
793
794 $addedIds = self::filterAddedIds($positiveIds);
795
796 foreach (array_intersect($positiveIds, $addedIds) as $id)
797 {
798 $shpRes = new ShipmentResult($id);
799 $shpRes->addError(
800 new Main\Error(
801 Loc::getMessage('SALE_DLVR_REQ_MNGR_ALREADY_ADDED')
802 ));
803 $result[] = $shpRes;
804 }
805
806 $existingIds = self::filterExistIds($addedIds);
807
808 foreach (array_diff($addedIds, $existingIds) as $id)
809 {
810 $shpRes = new ShipmentResult($id);
811 $shpRes->addError(
812 new Main\Error(
813 Loc::getMessage('SALE_DLVR_REQ_MNGR_SHP_NOT_FOUND', array('#SHIPMENT_ID#' => $id)
814 )));
815 $result[] = $shpRes;
816 }
817
818 return $result;
819 }
820
825 protected static function filterExistIds(array $shipmentIds)
826 {
827 $result = array();
828
829 $res = Internals\ShipmentTable::getList(array(
830 'filter' => array(
831 '=ID' => $shipmentIds,
832 )
833 ));
834
835 while ($row = $res->fetch())
836 $result[] = $row['ID'];
837
838 return $result;
839 }
840
845 protected static function filterAddedIds(array $shipmentIds)
846 {
847 $result = array();
848
849 $res = ShipmentTable::getList(array(
850 'filter' => array(
851 '=SHIPMENT_ID' => $shipmentIds,
852 '!=REQUEST_ID' => false
853 )
854 ));
855
856 while ($row = $res->fetch())
857 $result[] = $row['SHIPMENT_ID'];
858
859 return $result;
860 }
861
866 protected static function filterPositiveIds(array $shipmentIds)
867 {
868 $result = array();
869
870 foreach ($shipmentIds as $id)
871 if (intval($id) > 0)
872 $result[] = $id;
873
874 return $result;
875 }
876
881 protected static function checkShipmentIdsBeforeDelete(array $shipmentIds)
882 {
883 return self::checkShipmentIdsBeforeUpdate($shipmentIds);
884 }
885
891 protected static function checkShipmentIdsBeforeUpdate(array $shipmentIds)
892 {
893 $result = array();
894 $positiveIds = self::filterPositiveIds($shipmentIds);
895
896 foreach (array_diff($shipmentIds, $positiveIds) as $id)
897 {
898 $shpRes = new ShipmentResult($id);
899 $shpRes->addError(
900 new Main\Error(
901 Loc::getMessage('SALE_DLVR_REQ_MNGR_ERROR_SHIPMENT_ID').' "'.$id.'"')
902 );
903 $result[] = $shpRes;
904 }
905
906 $addedIds = self::filterAddedIds($positiveIds);
907
908 foreach (array_diff($positiveIds, $addedIds) as $id)
909 {
910 $shpRes = new ShipmentResult($id);
911 $shpRes->addError(
912 new Main\Error(
913 Loc::getMessage('SALE_DLVR_REQ_MNGR_ERROR_SHIPMENT_NOT_IN_REQUEST', array('#SHIPMENT_ID#' => $id)
914 )));
915 $result[] = $shpRes;
916 }
917
918 $existingIds = self::filterExistIds($addedIds);
919
920 foreach (array_diff($addedIds, $existingIds) as $id)
921 {
922 $shpRes = new ShipmentResult($id);
923 $shpRes->addError(
924 new Main\Error(
925 Loc::getMessage('SALE_DLVR_REQ_MNGR_SHP_NOT_FOUND', array('#SHIPMENT_ID#' => $id)
926 )));
927 $result[] = $shpRes;
928 }
929
930 return $result;
931 }
932
937 public static function isShipmentSent($shipmentId)
938 {
939 return intval(self::getRequestIdByShipmentId($shipmentId)) > 0;
940 }
941
946 public static function getRequestIdByShipmentId($shipmentId)
947 {
948 $result = 0;
949
950 $res = ShipmentTable::getList(array(
951 'filter' => array(
952 '=SHIPMENT_ID' => $shipmentId,
953 )
954 ));
955
956 if ($row = $res->fetch())
957 $result = $row['REQUEST_ID'];
958
959 return $result;
960 }
961
968 public static function addShipmentsToDeliveryRequest($requestId, array $shipmentIds, array $additional = [])
969 {
970 $result = new Result();
971
972 if (empty($requestId))
973 {
974 $result->addError(new Main\Error(Loc::getMessage('SALE_DLVR_REQ_MNGR_ERROR_REQ_ID')));
975 return $result;
976 }
977
978 if (empty($shipmentIds))
979 {
980 $result->addError(new Main\Error(Loc::getMessage('SALE_DLVR_REQ_MNGR_ERROR_SHP_ID_LIST_EMPTY')));
981 return $result;
982 }
983
984 if (!($deliveryRequestHandler = self::getDeliveryRequestHandlerByRequestId($requestId)))
985 {
986 $result->addError(
987 new Main\Error(
988 Loc::getMessage('SALE_DLVR_REQ_MNGR_ERROR_SHP_ADD2').'. '.
989 Loc::getMessage('SALE_DLVR_REQ_MNGR_ERROR_REQ_OBJ',
990 "",
991 $requestId
992 )));
993 return $result;
994 }
995
996 $checkResults = self::checkShipmentIdsBeforeAdd($shipmentIds);
997
998 foreach ($checkResults as $res)
999 {
1000 if ($res->isSuccess())
1001 continue;
1002
1003 $result->addResult(
1004 self::processShipmentResult(
1005 $res
1006 ));
1007
1008 unset($shipmentIds[array_search($res->getInternalId(), $shipmentIds)]);
1009 }
1010
1011 if (empty($shipmentIds))
1012 {
1013 $result->addError(new Main\Error(Loc::getMessage('SALE_DLVR_REQ_MNGR_ERROR_SHP_ABSENT')));
1014 return $result;
1015 }
1016
1017 $res = $deliveryRequestHandler->addShipments($requestId, $shipmentIds, $additional);
1018
1019 if ($res->isSuccess())
1020 {
1021 $result->addResults($res->getResults());
1022 }
1023 else
1024 {
1025 $result->addErrors($res->getErrors());
1026
1027 foreach ($res->getShipmentResults() as $sRes)
1028 $result->addResult(self::processShipmentResult($sRes));
1029
1030 return $result;
1031 }
1032
1033 $results = $result->getResults();
1034
1035 if (!is_array($results) || empty($results))
1036 {
1037 $result->addError(new Main\Error(Loc::getMessage('SALE_DLVR_REQ_MNGR_ERROR_RES_UNKNOWN')));
1038 return $result;
1039 }
1040
1041 $successResCount = 0;
1042
1044 foreach ($results as $resId => $reqRes)
1045 {
1046 if ($reqRes instanceof ShipmentResult)
1047 {
1048 $results[$resId] = self::processShipmentResult($reqRes, $requestId);
1049 }
1050 elseif ($reqRes instanceof RequestResult)
1051 {
1052 $reqShpResults = $reqRes->getShipmentResults();
1053
1054 foreach ($reqShpResults as $id => $shpRes)
1055 {
1056 $reqShpResults[$id] = self::processShipmentResult($shpRes, $requestId);
1057
1058 if ($shpRes->isSuccess())
1059 $successResCount++;
1060 }
1061
1062 $reqRes->setResults($reqShpResults);
1063 $results[$resId] = $reqRes;
1064 }
1065 else
1066 {
1067 $result->addError(
1068 new Main\Error(
1070 'SALE_DLVR_REQ_MNGR_RES_WRONG',
1071 array(
1072 '#CLASS_NAME#' => get_class($reqRes),
1073 '#REQUEST_ID#' => $requestId
1074 )
1075 )
1076 )
1077 );
1078
1079 continue;
1080 }
1081 }
1082
1083 if ($successResCount <= 0)
1084 $result->addError(new Main\Error(Loc::getMessage('SALE_DLVR_REQ_MNGR_ERROR_SHP_ABSENT2')));
1085
1086 $result->setResults($results);
1087 return $result;
1088 }
1089
1096 public static function updateShipmentsFromDeliveryRequest($requestId, array $shipmentIds)
1097 {
1098 $result = new Result();
1099
1100 if (empty($requestId))
1101 {
1102 $result->addError(new Main\Error(Loc::getMessage('SALE_DLVR_REQ_MNGR_ERROR_REQ_ID')));
1103 return $result;
1104 }
1105
1106 if (empty($shipmentIds))
1107 {
1108 $result->addError(new Main\Error(Loc::getMessage('SALE_DLVR_REQ_MNGR_ERROR_SHP_ID_LIST_EMPTY')));
1109 return $result;
1110 }
1111
1112 $checkResults = self::checkShipmentIdsBeforeUpdate($shipmentIds);
1113
1114 foreach ($checkResults as $res)
1115 {
1116 if ($res->isSuccess())
1117 continue;
1118
1119 $result->addResult(
1120 self::processShipmentResult($res, $requestId)
1121 );
1122
1123 unset($shipmentIds[array_search($res->getInternalId(), $shipmentIds)]);
1124 }
1125
1126 if (empty($shipmentIds))
1127 {
1128 $result->addError(
1129 new Main\Error(
1131 'SALE_DLVR_REQ_MNGR_ERROR_SHP_UPD',
1132 array('#REQUEST_LINK#' => Helper::getRequestViewLink($requestId))
1133 )));
1134 return $result;
1135 }
1136
1137 $deliveryRequestHandler = self::getDeliveryRequestHandlerByRequestId($requestId);
1138
1139 if (!$deliveryRequestHandler)
1140 {
1141 $result->addError(
1142 new Main\Error(
1144 'SALE_DLVR_REQ_MNGR_ERROR_SHP_UPD',
1145 array('#REQUEST_LINK#' => Helper::getRequestViewLink($requestId))
1146 ).'. '.
1147 Loc::getMessage('SALE_DLVR_REQ_MNGR_ERROR_REQ_OBJ')
1148 ));
1149
1150 return $result;
1151 }
1152
1153 $res = $deliveryRequestHandler->updateShipments($requestId, $shipmentIds);
1154 $result->addResults($res->getResults());
1155
1156 if (!$res->isSuccess())
1157 {
1158 $result->addErrors($res->getErrors());
1159 return $result;
1160 }
1161
1162 $results = $res->getResults();
1163
1164 if (empty($results))
1165 {
1166 $result->addError(new Main\Error(Loc::getMessage('SALE_DLVR_REQ_MNGR_ERROR_RES_EMPTY')));
1167 return $result;
1168 }
1169
1170 $resultsFinal = array();
1171
1172 foreach ($results as $res)
1173 {
1174 if ($res instanceof ShipmentResult)
1175 {
1176 $resultsFinal[] = self::processShipmentResult($res, $requestId);
1177 }
1178 elseif ($res instanceof RequestResult)
1179 {
1180 $reqShpResults = $res->getShipmentResults();
1181
1182 foreach ($reqShpResults as $id => $shpRes)
1183 {
1184 $shpRes = self::processShipmentResult($shpRes, $requestId);
1185
1186 if ($shpRes->isSuccess())
1187 {
1188 $shpInternalId = intval($shpRes->getInternalId());
1189 //Unset mark about changed shipments
1190 self::unSetMarkerShipmentChanged($shpInternalId);
1191
1192 $dbRes = self::saveShipmentResult($shpRes->getInternalId(), $shpRes);
1193
1194 if (!$dbRes->isSuccess())
1195 $shpRes->addErrors($dbRes->getErrors());
1196 }
1197
1198 $resultsFinal[] = $shpRes;
1199 }
1200 }
1201 else
1202 {
1203 $result->addError(
1204 new Main\Error(
1205 Loc::getMessage('SALE_DLVR_REQ_MNGR_RES_WRONG_UPD')
1206 ));
1207
1208 continue;
1209 }
1210 }
1211
1212 $result->setResults($resultsFinal);
1213 return $result;
1214 }
1215
1227 protected static function saveShipmentResult($shipmentId, ShipmentResult $shipmentResult)
1228 {
1229 $result = new Result();
1230 $shipments = Helper::getShipmentsByIds(array($shipmentId));
1231
1232 if ($shipments[$shipmentId])
1233 {
1234 $shipments[$shipmentId]->setFields(array(
1235 'TRACKING_NUMBER' => $shipmentResult->getTrackingNumber(),
1236 'DELIVERY_DOC_NUM' => $shipmentResult->getDeliveryDocNum(),
1237 'DELIVERY_DOC_DATE' => $shipmentResult->getDeliveryDocDate()
1238 ));
1239
1240 static::$isChangedShipmentNeedsMark = false;
1241 $res = $shipments[$shipmentId]->getOrder()->save();
1242 static::$isChangedShipmentNeedsMark = true;
1243
1244 if (!$res->isSuccess())
1245 $result->addError(new Main\Error(Loc::getMessage('SALE_DLVR_REQ_MNGR_ERROR_SAVE_SHIPMENT').'"'.$shipmentId.'"'));
1246 }
1247 else
1248 {
1249 $result->addError(
1250 new Main\Error(
1252 'SALE_DLVR_REQ_MNGR_SHP_NOT_FOUND',
1253 array('#SHIPMENT_ID#' => $shipmentId)
1254 )));
1255 }
1256
1257 return $result;
1258 }
1259
1264 public static function onBeforeShipmentSave(&$order, &$shipment)
1265 {
1266 if (static::$isChangedShipmentNeedsMark && self::isShipmentSent($shipment->getId()))
1267 {
1268 self::setMarkerShipmentChanged($order, $shipment);
1269 }
1270 }
1271
1275 public static function onBeforeShipmentDelete(&$shipment)
1276 {
1277 $shipmentId = $shipment->getId();
1278
1279 if (self::isShipmentSent($shipmentId))
1280 {
1281 self::deleteShipmentsFromDeliveryRequest(
1282 self::getRequestIdByShipmentId($shipmentId),
1283 array($shipmentId)
1284 );
1285 }
1286 }
1287
1292 protected static function setMarkerShipmentChanged(&$order, &$shipment)
1293 {
1294 $r = new \Bitrix\Sale\Result();
1295
1296 $r->addWarning(
1297 new ResultWarning(
1299 'SALE_DLVR_REQ_MNGR_NOT_UPDATED'
1300 ),
1301 'DELIVERY_REQUEST_NOT_UPDATED'
1302 ));
1303
1304 EntityMarker::addMarker($order, $shipment, $r);
1305 $shipment->setField('MARKED', 'Y');
1306 }
1307
1311 protected static function unSetMarkerShipmentChanged($shipmentId)
1312 {
1314 '=ENTITY_TYPE' => EntityMarker::ENTITY_TYPE_SHIPMENT,
1315 '=ENTITY_ID' => $shipmentId,
1316 '=CODE' => 'DELIVERY_REQUEST_NOT_UPDATED'
1317 ));
1318 }
1319
1326 public static function sendMessage(
1327 string $addressee,
1328 Message\Message $message,
1329 int $requestId,
1330 int $shipmentId
1331 ): void
1332 {
1333 (new Main\Event(
1334 'sale',
1335 self::MESSAGE_RECEIVED_EVENT_CODE,
1336 [
1337 'ADDRESSEE' => $addressee,
1338 'REQUEST_ID' => $requestId,
1339 'SHIPMENT_ID' => $shipmentId,
1340 'MESSAGE' => $message,
1341 ]
1342 ))->send();
1343 }
1344
1348 public static function getMessageAddressees(): array
1349 {
1350 return [
1351 self::MESSAGE_MANAGER_ADDRESSEE,
1352 self::MESSAGE_RECIPIENT_ADDRESSEE,
1353 ];
1354 }
1355
1359 public static function getRequestStatusSemantics(): array
1360 {
1361 return [
1362 self::EXTERNAL_STATUS_SEMANTIC_SUCCESS,
1363 self::EXTERNAL_STATUS_SEMANTIC_PROCESS,
1364 ];
1365 }
1366
1367 public static function initJs()
1368 {
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')
1373 ));
1374
1375 \CUtil::InitJSCore(array('sale_delivery_requests'));
1376 }
1377}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getDeliveryEditLink($deliveryId, $deliveryName='', $languageId=LANGUAGE_ID)
Definition helper.php:58
static getShipmentEditLink($shipmentId, $text='', $orderId=0, $languageId=LANGUAGE_ID)
Definition helper.php:25
static getRequestViewLink($requestId, $text='', $languageId=LANGUAGE_ID)
Definition helper.php:80
static onBeforeShipmentDelete(&$shipment)
Definition manager.php:1275
static getDeliveryRequestHandlerByRequestId($requestId)
Definition manager.php:155
static sendMessage(string $addressee, Message\Message $message, int $requestId, int $shipmentId)
Definition manager.php:1326
static executeDeliveryRequestAction($requestId, $actionType, array $additional=array())
Definition manager.php:724
static filterAddedIds(array $shipmentIds)
Definition manager.php:845
static unSetMarkerShipmentChanged($shipmentId)
Definition manager.php:1311
static filterPositiveIds(array $shipmentIds)
Definition manager.php:866
static checkShipmentIdsBeforeUpdate(array $shipmentIds)
Definition manager.php:891
static saveShipmentResult($shipmentId, ShipmentResult $shipmentResult)
Definition manager.php:1227
static getDeliveryRequestShipmentContent($requestId, $shipmentId)
Definition manager.php:52
static onBeforeShipmentSave(&$order, &$shipment)
Definition manager.php:1264
static getDeliveryRequestFormFields($deliveryId, $formFieldsType, array $shipmentIds, array $additional=array())
Definition manager.php:416
static executeDeliveryRequestShipmentAction($requestId, $shipmentId, $actionType, array $additional=array())
Definition manager.php:759
static getDeliveryRequestContent($requestId)
Definition manager.php:704
static checkShipmentIdsBeforeDelete(array $shipmentIds)
Definition manager.php:881
static deleteDeliveryRequest($requestId)
Definition manager.php:429
static updateDeliveryRequest($requestId, array $fields, bool $overwriteProperties=false)
Definition manager.php:497
static getRequestIdByShipmentId($shipmentId)
Definition manager.php:946
static processShipmentResult($result, $requestId=0)
Definition manager.php:362
static setMarkerShipmentChanged(&$order, &$shipment)
Definition manager.php:1292
static checkShipmentIdsBeforeAdd(array $shipmentIds)
Definition manager.php:778
static filterExistIds(array $shipmentIds)
Definition manager.php:825
static getDeliveryRequestShipmentActions(Shipment $shipment)
Definition manager.php:118
static updateShipmentsFromDeliveryRequest($requestId, array $shipmentIds)
Definition manager.php:1096
static getDeliveryRequestActions($requestId)
Definition manager.php:139
static isShipmentSent($shipmentId)
Definition manager.php:937
static getDeliveryRequestHandlerByDeliveryId($deliveryId)
Definition manager.php:176
static deleteByFilter(array $values)
$GLOBALS['____1444769544']
Definition license.php:1