Cancel.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Controller\Adminhtml\Shipment;
  6. use Magento\Backend\App\Action;
  7. use Magento\Backend\App\Action\Context;
  8. use Magento\Framework\Exception\LocalizedException;
  9. use Temando\Shipping\Model\Sales\Service\ShipmentService;
  10. /**
  11. * Temando Shipment Cancellation Action
  12. *
  13. * @package Temando\Shipping\Controller
  14. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  15. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  16. * @link https://www.temando.com/
  17. */
  18. class Cancel extends Action
  19. {
  20. const ADMIN_RESOURCE = 'Temando_Shipping::shipping';
  21. /**
  22. * @var ShipmentService
  23. */
  24. private $shipmentService;
  25. /**
  26. * Cancel constructor.
  27. *
  28. * @param Context $context
  29. * @param ShipmentService $shipmentService
  30. */
  31. public function __construct(
  32. Context $context,
  33. ShipmentService $shipmentService
  34. ) {
  35. $this->shipmentService = $shipmentService;
  36. parent::__construct($context);
  37. }
  38. /**
  39. * Trigger shipment cancellation at the platform, inform user about result.
  40. *
  41. * @return \Magento\Framework\Controller\ResultInterface
  42. */
  43. public function execute()
  44. {
  45. $salesShipmentId = (int) $this->getRequest()->getParam('sales_shipment_id');
  46. $shipmentId = $this->getRequest()->getParam('shipment_id');
  47. try {
  48. $this->shipmentService->cancel($shipmentId, $salesShipmentId);
  49. $this->messageManager->addSuccessMessage(__('Shipment was successfully cancelled.'));
  50. } catch (LocalizedException $exception) {
  51. $this->messageManager->addErrorMessage($exception->getMessage());
  52. }
  53. /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
  54. $resultRedirect = $this->resultRedirectFactory->create();
  55. $resultRedirect->setPath('sales/shipment/view', ['shipment_id' => $salesShipmentId]);
  56. return $resultRedirect;
  57. }
  58. }