123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Shipping\Controller\Adminhtml\Order\Shipment;
- use Magento\Framework\App\Action\HttpPostActionInterface;
- use Magento\Framework\Controller\ResultFactory;
- use Magento\Sales\Model\Order\Shipment\Validation\QuantityValidator;
- /**
- * Class Save
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class Save extends \Magento\Backend\App\Action implements HttpPostActionInterface
- {
- /**
- * Authorization level of a basic admin session
- *
- * @see _isAllowed()
- */
- const ADMIN_RESOURCE = 'Magento_Sales::shipment';
- /**
- * @var \Magento\Shipping\Controller\Adminhtml\Order\ShipmentLoader
- */
- protected $shipmentLoader;
- /**
- * @var \Magento\Shipping\Model\Shipping\LabelGenerator
- */
- protected $labelGenerator;
- /**
- * @var \Magento\Sales\Model\Order\Email\Sender\ShipmentSender
- */
- protected $shipmentSender;
- /**
- * @var \Magento\Sales\Model\Order\Shipment\ShipmentValidatorInterface
- */
- private $shipmentValidator;
- /**
- * @param \Magento\Backend\App\Action\Context $context
- * @param \Magento\Shipping\Controller\Adminhtml\Order\ShipmentLoader $shipmentLoader
- * @param \Magento\Shipping\Model\Shipping\LabelGenerator $labelGenerator
- * @param \Magento\Sales\Model\Order\Email\Sender\ShipmentSender $shipmentSender
- * @param \Magento\Sales\Model\Order\Shipment\ShipmentValidatorInterface|null $shipmentValidator
- */
- public function __construct(
- \Magento\Backend\App\Action\Context $context,
- \Magento\Shipping\Controller\Adminhtml\Order\ShipmentLoader $shipmentLoader,
- \Magento\Shipping\Model\Shipping\LabelGenerator $labelGenerator,
- \Magento\Sales\Model\Order\Email\Sender\ShipmentSender $shipmentSender,
- \Magento\Sales\Model\Order\Shipment\ShipmentValidatorInterface $shipmentValidator = null
- ) {
- parent::__construct($context);
- $this->shipmentLoader = $shipmentLoader;
- $this->labelGenerator = $labelGenerator;
- $this->shipmentSender = $shipmentSender;
- $this->shipmentValidator = $shipmentValidator ?: \Magento\Framework\App\ObjectManager::getInstance()
- ->get(\Magento\Sales\Model\Order\Shipment\ShipmentValidatorInterface::class);
- }
- /**
- * Save shipment and order in one transaction
- *
- * @param \Magento\Sales\Model\Order\Shipment $shipment
- * @return $this
- */
- protected function _saveShipment($shipment)
- {
- $shipment->getOrder()->setIsInProcess(true);
- $transaction = $this->_objectManager->create(
- \Magento\Framework\DB\Transaction::class
- );
- $transaction->addObject(
- $shipment
- )->addObject(
- $shipment->getOrder()
- )->save();
- return $this;
- }
- /**
- * Save shipment
- *
- * We can save only new shipment. Existing shipments are not editable
- *
- * @return \Magento\Framework\Controller\ResultInterface
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
- * @SuppressWarnings(PHPMD.NPathComplexity)
- */
- public function execute()
- {
- /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
- $resultRedirect = $this->resultRedirectFactory->create();
- $formKeyIsValid = $this->_formKeyValidator->validate($this->getRequest());
- $isPost = $this->getRequest()->isPost();
- if (!$formKeyIsValid || !$isPost) {
- $this->messageManager->addErrorMessage(__('We can\'t save the shipment right now.'));
- return $resultRedirect->setPath('sales/order/index');
- }
- $data = $this->getRequest()->getParam('shipment');
- if (!empty($data['comment_text'])) {
- $this->_objectManager->get(\Magento\Backend\Model\Session::class)->setCommentText($data['comment_text']);
- }
- $isNeedCreateLabel = isset($data['create_shipping_label']) && $data['create_shipping_label'];
- $responseAjax = new \Magento\Framework\DataObject();
- try {
- $this->shipmentLoader->setOrderId($this->getRequest()->getParam('order_id'));
- $this->shipmentLoader->setShipmentId($this->getRequest()->getParam('shipment_id'));
- $this->shipmentLoader->setShipment($data);
- $this->shipmentLoader->setTracking($this->getRequest()->getParam('tracking'));
- $shipment = $this->shipmentLoader->load();
- if (!$shipment) {
- return $this->resultFactory->create(ResultFactory::TYPE_FORWARD)->forward('noroute');
- }
- if (!empty($data['comment_text'])) {
- $shipment->addComment(
- $data['comment_text'],
- isset($data['comment_customer_notify']),
- isset($data['is_visible_on_front'])
- );
- $shipment->setCustomerNote($data['comment_text']);
- $shipment->setCustomerNoteNotify(isset($data['comment_customer_notify']));
- }
- $validationResult = $this->shipmentValidator->validate($shipment, [QuantityValidator::class]);
- if ($validationResult->hasMessages()) {
- $this->messageManager->addErrorMessage(
- __("Shipment Document Validation Error(s):\n" . implode("\n", $validationResult->getMessages()))
- );
- return $resultRedirect->setPath('*/*/new', ['order_id' => $this->getRequest()->getParam('order_id')]);
- }
- $shipment->register();
- $shipment->getOrder()->setCustomerNoteNotify(!empty($data['send_email']));
- if ($isNeedCreateLabel) {
- $this->labelGenerator->create($shipment, $this->_request);
- $responseAjax->setOk(true);
- }
- $this->_saveShipment($shipment);
- if (!empty($data['send_email'])) {
- $this->shipmentSender->send($shipment);
- }
- $shipmentCreatedMessage = __('The shipment has been created.');
- $labelCreatedMessage = __('You created the shipping label.');
- $this->messageManager->addSuccessMessage(
- $isNeedCreateLabel ? $shipmentCreatedMessage . ' ' . $labelCreatedMessage : $shipmentCreatedMessage
- );
- $this->_objectManager->get(\Magento\Backend\Model\Session::class)->getCommentText(true);
- } catch (\Magento\Framework\Exception\LocalizedException $e) {
- if ($isNeedCreateLabel) {
- $responseAjax->setError(true);
- $responseAjax->setMessage($e->getMessage());
- } else {
- $this->messageManager->addErrorMessage($e->getMessage());
- return $resultRedirect->setPath('*/*/new', ['order_id' => $this->getRequest()->getParam('order_id')]);
- }
- } catch (\Exception $e) {
- $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
- if ($isNeedCreateLabel) {
- $responseAjax->setError(true);
- $responseAjax->setMessage(__('An error occurred while creating shipping label.'));
- } else {
- $this->messageManager->addErrorMessage(__('Cannot save shipment.'));
- return $resultRedirect->setPath('*/*/new', ['order_id' => $this->getRequest()->getParam('order_id')]);
- }
- }
- if ($isNeedCreateLabel) {
- return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setJsonData($responseAjax->toJson());
- }
- return $resultRedirect->setPath('sales/order/view', ['order_id' => $shipment->getOrderId()]);
- }
- }
|