DispatchCreate.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\ViewModel\Rma;
  6. use Magento\Framework\UrlInterface;
  7. use Magento\Framework\View\Element\Block\ArgumentInterface;
  8. use Temando\Shipping\Model\ResourceModel\Repository\ShipmentReferenceRepositoryInterface;
  9. use Temando\Shipping\Model\ResourceModel\Rma\RmaAccess;
  10. use Temando\Shipping\ViewModel\DataProvider\ShippingApiAccess;
  11. use Temando\Shipping\ViewModel\DataProvider\ShippingApiAccessInterface;
  12. use Temando\Shipping\ViewModel\PageActionsInterface;
  13. use Temando\Shipping\ViewModel\ShippingApiInterface;
  14. /**
  15. * View model for RMA shipment dispatch creation.
  16. *
  17. * @package Temando\Shipping\ViewModel
  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 DispatchCreate implements ArgumentInterface, PageActionsInterface, ShippingApiInterface
  23. {
  24. /**
  25. * @var ShippingApiAccess
  26. */
  27. private $apiAccess;
  28. /**
  29. * @var RmaAccess
  30. */
  31. private $rmaAccess;
  32. /**
  33. * @var ShipmentReferenceRepositoryInterface
  34. */
  35. private $shipmentReferenceRepository;
  36. /**
  37. * @var UrlInterface
  38. */
  39. private $urlBuilder;
  40. /**
  41. * DispatchCreate constructor.
  42. * @param ShippingApiAccess $apiAccess
  43. * @param RmaAccess $rmaAccess
  44. * @param ShipmentReferenceRepositoryInterface $shipmentReferenceRepository
  45. * @param UrlInterface $urlBuilder
  46. */
  47. public function __construct(
  48. ShippingApiAccess $apiAccess,
  49. RmaAccess $rmaAccess,
  50. ShipmentReferenceRepositoryInterface $shipmentReferenceRepository,
  51. UrlInterface $urlBuilder
  52. ) {
  53. $this->apiAccess = $apiAccess;
  54. $this->rmaAccess = $rmaAccess;
  55. $this->shipmentReferenceRepository = $shipmentReferenceRepository;
  56. $this->urlBuilder = $urlBuilder;
  57. }
  58. /**
  59. * Obtain array of button data.
  60. *
  61. * @see \Temando\Shipping\Block\Adminhtml\ComponentContainer::_prepareLayout
  62. * @see \Magento\Backend\Block\Widget\Button\ButtonList::add
  63. *
  64. * @return mixed[][]
  65. */
  66. public function getMainActions(): array
  67. {
  68. $buttonId = 'back';
  69. $buttonData = [
  70. 'label' => __('Back'),
  71. 'onclick' => sprintf("window.location.href = '%s';", $this->getRmaShipmentPageUrl()),
  72. 'class' => 'back',
  73. 'sort_order' => 10
  74. ];
  75. $mainActions = [
  76. $buttonId => $buttonData,
  77. ];
  78. return $mainActions;
  79. }
  80. /**
  81. * @return ShippingApiAccessInterface
  82. */
  83. public function getShippingApiAccess(): ShippingApiAccessInterface
  84. {
  85. return $this->apiAccess;
  86. }
  87. /**
  88. * @return string
  89. */
  90. public function getRmaShipmentId(): string
  91. {
  92. $rmaShipment = $this->rmaAccess->getCurrentRmaShipment();
  93. return $rmaShipment->getShipmentId();
  94. }
  95. /**
  96. * @return string
  97. */
  98. public function getRmaShipmentPageUrl(): string
  99. {
  100. $rmaShipmentId = $this->getRmaShipmentId();
  101. $routeParams = ['ext_shipment_id' => $rmaShipmentId];
  102. $rma = $this->rmaAccess->getCurrentRma();
  103. if (!$rma->getEntityId()) {
  104. // forward-fulfillment return shipment
  105. $shipment = $this->shipmentReferenceRepository->getByExtReturnShipmentId($rmaShipmentId);
  106. $routeParams['shipment_id'] = $shipment->getShipmentId();
  107. } else {
  108. // ad-hoc return shipment
  109. $routeParams['rma_id'] = $rma->getEntityId();
  110. }
  111. return $this->urlBuilder->getUrl('temando/rma_shipment/view', $routeParams);
  112. }
  113. /**
  114. * @return string
  115. */
  116. public function getDispatchViewPageUrlTpl(): string
  117. {
  118. return $this->urlBuilder->getUrl('temando/dispatch/view/dispatch_id/--id--/');
  119. }
  120. /**
  121. * @return string
  122. */
  123. public function getDispatchGridPageUrl(): string
  124. {
  125. return $this->urlBuilder->getUrl('temando/dispatch/index');
  126. }
  127. }