View.php 3.9 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\Rma\Shipment;
  6. use Magento\Backend\App\Action;
  7. use Magento\Backend\App\Action\Context;
  8. use Magento\Framework\Controller\ResultFactory;
  9. use Magento\Sales\Api\ShipmentRepositoryInterface as SalesShipmentRepositoryInterface;
  10. use Temando\Shipping\Model\ResourceModel\Repository\ShipmentRepositoryInterface;
  11. use Temando\Shipping\Model\ResourceModel\Rma\RmaAccess;
  12. /**
  13. * Temando View Return Shipment Action
  14. *
  15. * A return shipment can be either accessed
  16. * - from the RMA Edit page with an `rma_id` request parameter available ("Added Shipment") OR
  17. * - from the Shipment View page with a `shipment_id` request parameter available ("Available Shipment")
  18. *
  19. * @package Temando\Shipping\Controller
  20. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  21. * @author Max Melzer <max.melzer@netresearch.de>
  22. * @author Rhodri Davies <rhodri.davies@temando.com>
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * @link http://www.temando.com/
  25. */
  26. class View extends Action
  27. {
  28. const ADMIN_RESOURCE = 'Magento_Rma::magento_rma';
  29. /**
  30. * @var RmaAccess
  31. */
  32. private $rmaAccess;
  33. /**
  34. * @var ShipmentRepositoryInterface
  35. */
  36. private $shipmentRepository;
  37. /**
  38. * @var SalesShipmentRepositoryInterface
  39. */
  40. private $salesShipmentRepository;
  41. /**
  42. * View constructor.
  43. *
  44. * @param Context $context
  45. * @param RmaAccess $rmaAccess
  46. * @param ShipmentRepositoryInterface $shipmentRepository
  47. * @param SalesShipmentRepositoryInterface $salesShipmentRepository
  48. */
  49. public function __construct(
  50. Context $context,
  51. RmaAccess $rmaAccess,
  52. ShipmentRepositoryInterface $shipmentRepository,
  53. SalesShipmentRepositoryInterface $salesShipmentRepository
  54. ) {
  55. $this->rmaAccess = $rmaAccess;
  56. $this->shipmentRepository = $shipmentRepository;
  57. $this->salesShipmentRepository = $salesShipmentRepository;
  58. parent::__construct($context);
  59. }
  60. /**
  61. * @return \Magento\Framework\Controller\ResultInterface
  62. */
  63. public function execute()
  64. {
  65. $rmaId = $this->getRequest()->getParam('rma_id');
  66. $shipmentId = $this->getRequest()->getParam('shipment_id');
  67. if (!$rmaId && !$shipmentId) {
  68. /** @var \Magento\Framework\Controller\Result\Forward $resultPage */
  69. $resultPage = $this->resultFactory->create(ResultFactory::TYPE_FORWARD);
  70. $resultPage->forward('noroute');
  71. return $resultPage;
  72. }
  73. /** @var \Magento\Backend\Model\View\Result\Page $resultPage */
  74. $resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
  75. if ($rmaId) {
  76. // load existing RMA
  77. $rma = $this->rmaAccess->getById($rmaId);
  78. $resultPage->addHandle('temando_rma_shipment_view_added');
  79. } else {
  80. // create dummy RMA
  81. $salesShipment = $this->salesShipmentRepository->get($shipmentId);
  82. $rma = $this->rmaAccess->create(['data' => [
  83. 'store_id' => $salesShipment->getStoreId(),
  84. 'customer_id' => $salesShipment->getCustomerId(),
  85. 'order_id' => $salesShipment->getOrderId(),
  86. ]]);
  87. $resultPage->addHandle('temando_rma_shipment_view_available');
  88. }
  89. // register current RMA
  90. $this->rmaAccess->setCurrentRma($rma);
  91. // load and register current RMA shipment
  92. $extShipmentId = $this->getRequest()->getParam('ext_shipment_id');
  93. /** @var \Temando\Shipping\Model\ShipmentInterface $extShipment */
  94. $extShipment = $this->shipmentRepository->getById($extShipmentId);
  95. $this->rmaAccess->setCurrentRmaShipment($extShipment);
  96. $resultPage->getConfig()->getTitle()->prepend(__('Return Shipment'));
  97. return $resultPage;
  98. }
  99. }