Cancel.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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\Pickup;
  6. use Magento\Backend\App\Action;
  7. use Magento\Backend\App\Action\Context;
  8. use Magento\Framework\Controller\ResultFactory;
  9. use Magento\Framework\Controller\ResultInterface;
  10. use Magento\Framework\Exception\CouldNotSaveException;
  11. use Temando\Shipping\Model\Pickup\Email\Sender\PickupSender;
  12. use Temando\Shipping\Model\Pickup\PickupLoader;
  13. use Temando\Shipping\Model\PickupInterface;
  14. use Temando\Shipping\Model\PickupProviderInterface;
  15. use Temando\Shipping\Model\ResourceModel\Repository\PickupRepositoryInterface;
  16. /**
  17. * Cancel Pickups Action
  18. *
  19. * @package Temando\Shipping\Controller
  20. * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
  21. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  22. * @link https://www.temando.com/
  23. */
  24. class Cancel extends Action
  25. {
  26. const ADMIN_RESOURCE = 'Temando_Shipping::pickups';
  27. /**
  28. * @var PickupLoader
  29. */
  30. private $pickupLoader;
  31. /**
  32. * @var PickupProviderInterface
  33. */
  34. private $pickupProvider;
  35. /**
  36. * @var PickupRepositoryInterface
  37. */
  38. private $pickupRepository;
  39. /**
  40. * @var PickupSender
  41. */
  42. private $pickupSender;
  43. /**
  44. * Cancel constructor.
  45. * @param Context $context
  46. * @param PickupLoader $pickupLoader
  47. * @param PickupProviderInterface $pickupProvider
  48. * @param PickupRepositoryInterface $pickupRepository
  49. * @param PickupSender $pickupSender
  50. */
  51. public function __construct(
  52. Context $context,
  53. PickupLoader $pickupLoader,
  54. PickupProviderInterface $pickupProvider,
  55. PickupRepositoryInterface $pickupRepository,
  56. PickupSender $pickupSender
  57. ) {
  58. $this->pickupLoader = $pickupLoader;
  59. $this->pickupProvider = $pickupProvider;
  60. $this->pickupRepository = $pickupRepository;
  61. $this->pickupSender = $pickupSender;
  62. parent::__construct($context);
  63. }
  64. /**
  65. * Execute action.
  66. *
  67. * @return ResultInterface
  68. */
  69. public function execute()
  70. {
  71. $orderId = $this->getRequest()->getParam('sales_order_id', 0);
  72. $pickupId = $this->getRequest()->getParam('pickup_id', '');
  73. $pickups = $this->pickupLoader->load($orderId, $pickupId);
  74. $this->pickupLoader->register($pickups, $orderId, $pickupId);
  75. /** @var \Temando\Shipping\Model\Pickup $pickup */
  76. $pickup = $this->pickupProvider->getPickup();
  77. /** @var \Magento\Sales\Model\Order $order */
  78. $order = $this->pickupProvider->getOrder();
  79. /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
  80. $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
  81. $resultRedirect->setUrl($this->_redirect->getRefererUrl());
  82. try {
  83. $pickup->setData(PickupInterface::STATE, PickupInterface::STATE_CANCELLED);
  84. $this->pickupRepository->save($pickup);
  85. $this->messageManager->addSuccessMessage('Pickup was cancelled.');
  86. } catch (CouldNotSaveException $exception) {
  87. $this->messageManager->addErrorMessage('Failed to update pickup status.');
  88. return $resultRedirect;
  89. }
  90. try {
  91. $this->pickupSender->setPickupCancelled();
  92. $this->pickupSender->send($order);
  93. $this->messageManager->addSuccessMessage('Email confirmation was sent.');
  94. } catch (\Exception $exception) {
  95. $this->messageManager->addSuccessMessage('Email confirmation could not be sent.');
  96. }
  97. return $resultRedirect;
  98. }
  99. }