AddRmaToolbarButtonPlugin.php 3.3 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\Plugin\Rma;
  6. use Magento\Backend\Block\Widget\Container;
  7. use Magento\Framework\UrlInterface;
  8. use Magento\Framework\View\LayoutInterface;
  9. use Magento\Rma\Block\Adminhtml\Rma\Edit as RmaEdit;
  10. use Magento\Sales\Api\Data\OrderInterface;
  11. use Temando\Shipping\Model\Config\ModuleConfigInterface;
  12. use Temando\Shipping\Model\ResourceModel\Rma\RmaAccess;
  13. use Temando\Shipping\Model\Shipping\Carrier;
  14. /**
  15. * AddRmaToolbarButtonPlugin
  16. *
  17. * @package Temando\Shipping\Plugin
  18. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  19. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  20. * @link https://www.temando.com/
  21. */
  22. class AddRmaToolbarButtonPlugin
  23. {
  24. /**
  25. * @var ModuleConfigInterface
  26. */
  27. private $config;
  28. /**
  29. * @var RmaAccess
  30. */
  31. private $rmaAccess;
  32. /**
  33. * @var UrlInterface
  34. */
  35. private $urlBuilder;
  36. /**
  37. * AddRmaToolbarButtonPlugin constructor.
  38. * @param ModuleConfigInterface $config
  39. * @param RmaAccess $rmaAccess
  40. * @param UrlInterface $urlBuilder
  41. */
  42. public function __construct(
  43. ModuleConfigInterface $config,
  44. RmaAccess $rmaAccess,
  45. UrlInterface $urlBuilder
  46. ) {
  47. $this->config = $config;
  48. $this->rmaAccess = $rmaAccess;
  49. $this->urlBuilder = $urlBuilder;
  50. }
  51. /**
  52. * Add "Create Return Shipment" button to toolbar if original order was shipped
  53. * with Temando Shipping.
  54. *
  55. * @param RmaEdit|Container $block
  56. * @param LayoutInterface $layout
  57. *
  58. * @return null
  59. */
  60. public function beforeSetLayout(RmaEdit $block, LayoutInterface $layout)
  61. {
  62. // only display button if rma is enabled for temando shipping
  63. if (!$this->config->isRmaEnabled()) {
  64. return null;
  65. }
  66. // only display button if rma is registered
  67. /** @var \Magento\Rma\Model\Rma $rma */
  68. $rma = $this->rmaAccess->getCurrentRma();
  69. if (!$rma) {
  70. return null;
  71. }
  72. /** @var \Magento\Sales\Model\Order $order */
  73. $order = $rma->getOrder();
  74. if (!$order instanceof OrderInterface || !$order->getData('shipping_method')) {
  75. // wrong type, virtual or corrupt order
  76. return null;
  77. }
  78. $shippingMethod = $order->getShippingMethod(true);
  79. if ($shippingMethod->getData('carrier_code') !== Carrier::CODE) {
  80. return null;
  81. }
  82. $isRmaAuthorized = in_array($rma->getStatus(), [
  83. \Magento\Rma\Model\Rma\Source\Status::STATE_AUTHORIZED,
  84. \Magento\Rma\Model\Rma\Source\Status::STATE_PARTIAL_AUTHORIZED
  85. ]);
  86. $createUrl = $this->urlBuilder->getUrl(
  87. 'temando/rma_shipment/create',
  88. ['rma_id' => $rma->getId()]
  89. );
  90. $block->addButton(
  91. 'create_return_shipment',
  92. [
  93. 'label' => __('Create Return Shipment'),
  94. 'class' => $isRmaAuthorized ? '' : 'disabled',
  95. 'onclick' => sprintf("setLocation('%s')", $createUrl)
  96. ],
  97. 6
  98. );
  99. // original method's argument does not get changed.
  100. return null;
  101. }
  102. }