BatchNew.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\ViewModel\Batch;
  6. use Magento\Directory\Helper\Data as DirectoryHelper;
  7. use Magento\Framework\App\Config\ScopeConfigInterface;
  8. use Magento\Framework\Exception\NoSuchEntityException;
  9. use Magento\Framework\Serialize\Serializer\Json;
  10. use Magento\Framework\UrlInterface;
  11. use Magento\Framework\View\Element\Block\ArgumentInterface;
  12. use Magento\Store\Model\ScopeInterface;
  13. use Magento\Store\Model\StoreManagerInterface;
  14. use Temando\Shipping\Model\BatchProviderInterface;
  15. use Temando\Shipping\ViewModel\CoreApiInterface;
  16. use Temando\Shipping\ViewModel\DataProvider\BatchUrl;
  17. use Temando\Shipping\ViewModel\DataProvider\CoreApiAccess;
  18. use Temando\Shipping\ViewModel\DataProvider\CoreApiAccessInterface;
  19. use Temando\Shipping\ViewModel\DataProvider\EntityUrlInterface;
  20. use Temando\Shipping\ViewModel\DataProvider\ShippingApiAccess;
  21. use Temando\Shipping\ViewModel\DataProvider\ShippingApiAccessInterface;
  22. use Temando\Shipping\ViewModel\ShippingApiInterface;
  23. /**
  24. * View model for batch list JS component.
  25. *
  26. * @package Temando\Shipping\ViewModel
  27. * @author Rhodri Davies <rhodri.davies@temando.com>
  28. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  29. * @link http://www.temando.com/
  30. */
  31. class BatchNew implements ArgumentInterface, CoreApiInterface, ShippingApiInterface
  32. {
  33. /**
  34. * @var CoreApiAccess
  35. */
  36. private $coreApiAccess;
  37. /**
  38. * @var ShippingApiAccess
  39. */
  40. private $shippingApiAccess;
  41. /**
  42. * @var UrlInterface
  43. */
  44. private $urlBuilder;
  45. /**
  46. * @var BatchUrl
  47. */
  48. private $batchUrl;
  49. /**
  50. * @var BatchProviderInterface
  51. */
  52. private $batchProvider;
  53. /**
  54. * @var StoreManagerInterface
  55. */
  56. private $storeManager;
  57. /**
  58. * @var ScopeConfigInterface
  59. */
  60. private $scopeConfig;
  61. /**
  62. * @var Json
  63. */
  64. private $serializer;
  65. /**
  66. * BatchNew constructor.
  67. * @param CoreApiAccess $coreApiAccess
  68. * @param ShippingApiAccess $shippingApiAccess
  69. * @param UrlInterface $urlBuilder
  70. * @param BatchUrl $batchUrl
  71. * @param BatchProviderInterface $batchProvider
  72. * @param StoreManagerInterface $storeManager
  73. * @param ScopeConfigInterface $scopeConfig
  74. * @param Json $serializer
  75. */
  76. public function __construct(
  77. CoreApiAccess $coreApiAccess,
  78. ShippingApiAccess $shippingApiAccess,
  79. UrlInterface $urlBuilder,
  80. BatchUrl $batchUrl,
  81. BatchProviderInterface $batchProvider,
  82. StoreManagerInterface $storeManager,
  83. ScopeConfigInterface $scopeConfig,
  84. Json $serializer
  85. ) {
  86. $this->coreApiAccess = $coreApiAccess;
  87. $this->shippingApiAccess = $shippingApiAccess;
  88. $this->urlBuilder = $urlBuilder;
  89. $this->batchUrl = $batchUrl;
  90. $this->batchProvider = $batchProvider;
  91. $this->storeManager = $storeManager;
  92. $this->scopeConfig = $scopeConfig;
  93. $this->serializer = $serializer;
  94. }
  95. /**
  96. * @return CoreApiAccessInterface
  97. */
  98. public function getCoreApiAccess(): CoreApiAccessInterface
  99. {
  100. return $this->coreApiAccess;
  101. }
  102. /**
  103. * @return ShippingApiAccessInterface
  104. */
  105. public function getShippingApiAccess(): ShippingApiAccessInterface
  106. {
  107. return $this->shippingApiAccess;
  108. }
  109. /**
  110. * @return string
  111. */
  112. public function getOrderListEndpoint(): string
  113. {
  114. $endpoint = $this->urlBuilder->getDirectUrl("rest/V1/orders", ['_secure' => true]);
  115. return $endpoint;
  116. }
  117. /**
  118. * @return EntityUrlInterface|BatchUrl
  119. */
  120. public function getBatchUrl(): EntityUrlInterface
  121. {
  122. return $this->batchUrl;
  123. }
  124. /**
  125. * Prepare component init order data.
  126. *
  127. * The component only needs the IDs, more details will be fetched via the
  128. * salesOrderRepositoryV1 endpoint. Only the weight unit is not available
  129. * there so we pass it right in here.
  130. *
  131. * @return string
  132. */
  133. public function getOrderData(): string
  134. {
  135. $data = [];
  136. $weightUnits = [];
  137. $orders = $this->batchProvider->getOrders();
  138. foreach ($orders as $order) {
  139. $data[$order->getEntityId()] = [];
  140. $data[$order->getEntityId()]['id'] = $order->getEntityId();
  141. try {
  142. $storeCode = $this->storeManager->getStore($order->getStoreId())->getCode();
  143. if (!isset($weightUnits[$storeCode])) {
  144. $weightUnit = $this->scopeConfig->getValue(
  145. DirectoryHelper::XML_PATH_WEIGHT_UNIT,
  146. ScopeInterface::SCOPE_STORE,
  147. $storeCode
  148. );
  149. $weightUnits[$storeCode] = $weightUnit;
  150. }
  151. $data[$order->getEntityId()]['weight_unit'] = $weightUnits[$storeCode];
  152. } catch (NoSuchEntityException $exception) {
  153. $weightUnit = $this->scopeConfig->getValue(DirectoryHelper::XML_PATH_WEIGHT_UNIT);
  154. $data[$order->getEntityId()]['weight_unit'] = $weightUnit;
  155. }
  156. }
  157. return $this->serializer->serialize($data);
  158. }
  159. }