AddShipmentViewToolbarButtonPlugin.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Plugin\Shipping;
  6. use Magento\Backend\Block\Widget\Container;
  7. use Magento\Framework\UrlInterface;
  8. use Magento\Framework\View\LayoutInterface;
  9. use Magento\Shipping\Block\Adminhtml\View as ShipmentView;
  10. use Temando\Shipping\Model\Config\ModuleConfigInterface;
  11. use Temando\Shipping\Model\Shipment\ShipmentProviderInterface;
  12. /**
  13. * AddShipmentViewToolbarButtonPlugin
  14. *
  15. * @package Temando\Shipping\Plugin
  16. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  17. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  18. * @link https://www.temando.com/
  19. */
  20. class AddShipmentViewToolbarButtonPlugin
  21. {
  22. /**
  23. * @var ModuleConfigInterface
  24. */
  25. private $config;
  26. /**
  27. * @var ShipmentProviderInterface
  28. */
  29. private $shipmentProvider;
  30. /**
  31. * @var UrlInterface
  32. */
  33. private $urlBuilder;
  34. /**
  35. * AddShipmentViewToolbarButtonPlugin constructor.
  36. *
  37. * @param ModuleConfigInterface $config
  38. * @param ShipmentProviderInterface $shipmentProvider
  39. * @param UrlInterface $urlBuilder
  40. */
  41. public function __construct(
  42. ModuleConfigInterface $config,
  43. ShipmentProviderInterface $shipmentProvider,
  44. UrlInterface $urlBuilder
  45. ) {
  46. $this->config = $config;
  47. $this->shipmentProvider = $shipmentProvider;
  48. $this->urlBuilder = $urlBuilder;
  49. }
  50. /**
  51. * @param ShipmentView|Container $block
  52. * @param LayoutInterface $layout
  53. *
  54. * @return null
  55. */
  56. public function beforeSetLayout(ShipmentView $block, LayoutInterface $layout)
  57. {
  58. // only display button if an API shipment was registered
  59. $shipment = $this->shipmentProvider->getShipment();
  60. if (!$shipment) {
  61. return null;
  62. }
  63. // only display button if rma is enabled for temando shipping
  64. if (!$this->config->isRmaEnabled()) {
  65. return null;
  66. }
  67. $salesShipment = $this->shipmentProvider->getSalesShipment();
  68. $shipmentId = $salesShipment->getEntityId();
  69. $extReturnShipmentId = $salesShipment->getExtensionAttributes()->getExtReturnShipmentId();
  70. // only display button if forward-fulfillment return shipment was booked
  71. if (!$extReturnShipmentId) {
  72. return null;
  73. }
  74. $viewUrl = $this->urlBuilder->getUrl(
  75. 'temando/rma_shipment/view',
  76. ['shipment_id' => $shipmentId, 'ext_shipment_id' => $extReturnShipmentId]
  77. );
  78. $block->addButton(
  79. 'view_return_shipment',
  80. [
  81. 'label' => __('View Return Shipment'),
  82. 'onclick' => sprintf("setLocation('%s')", $viewUrl)
  83. ],
  84. 0,
  85. 20
  86. );
  87. // original method's argument does not get changed.
  88. return null;
  89. }
  90. }