Component.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Block\Adminhtml\Shipping\Create;
  6. use Magento\Backend\Block\Widget\Context as WidgetContext;
  7. use Magento\Backend\Model\Auth\StorageInterface;
  8. use Magento\Directory\Helper\Data as DirectoryHelper;
  9. use Magento\Framework\Exception\LocalizedException;
  10. use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
  11. use Magento\Framework\Stdlib\DateTime\DateTime;
  12. use Magento\Integration\Model\Oauth\Token;
  13. use Magento\Security\Model\Config;
  14. use Magento\Store\Model\ScopeInterface;
  15. use Temando\Shipping\Model\ResourceModel\Repository\OrderRepositoryInterface;
  16. use Temando\Shipping\Block\Adminhtml\Template\AbstractComponent;
  17. use Temando\Shipping\Model\Shipment\ShipmentProviderInterface;
  18. use Temando\Shipping\Rest\AuthenticationInterface;
  19. use Temando\Shipping\Webservice\Config\WsConfigInterface;
  20. /**
  21. * Temando OrderShip Component Layout Block
  22. *
  23. * @package Temando\Shipping\Block
  24. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  25. * @author Rhodri Davies <rhodri.davies@temando.com>
  26. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  27. * @link http://www.temando.com/
  28. *
  29. * @api
  30. * @deprecated since 1.0.5 | Block data is provided by view model
  31. * @see \Temando\Shipping\ViewModel\Order\OrderShip
  32. */
  33. class Component extends AbstractComponent
  34. {
  35. /**
  36. * @var OrderRepositoryInterface
  37. */
  38. private $orderRepository;
  39. /**
  40. * @var ShipmentProviderInterface
  41. */
  42. private $shipmentProvider;
  43. /**
  44. * Component constructor.
  45. *
  46. * @param WidgetContext $context
  47. * @param WsConfigInterface $config
  48. * @param StorageInterface $session
  49. * @param AuthenticationInterface $auth
  50. * @param Token $token
  51. * @param DateTime $dateTime
  52. * @param RemoteAddress $remoteAddress
  53. * @param Config $securityConfig
  54. * @param OrderRepositoryInterface $orderRepository
  55. * @param ShipmentProviderInterface $shipmentProvider
  56. * @param mixed[] $data
  57. */
  58. public function __construct(
  59. WidgetContext $context,
  60. WsConfigInterface $config,
  61. StorageInterface $session,
  62. AuthenticationInterface $auth,
  63. Token $token,
  64. DateTime $dateTime,
  65. RemoteAddress $remoteAddress,
  66. Config $securityConfig,
  67. OrderRepositoryInterface $orderRepository,
  68. ShipmentProviderInterface $shipmentProvider,
  69. array $data = []
  70. ) {
  71. $this->orderRepository = $orderRepository;
  72. $this->shipmentProvider = $shipmentProvider;
  73. parent::__construct(
  74. $context,
  75. $config,
  76. $session,
  77. $auth,
  78. $token,
  79. $dateTime,
  80. $remoteAddress,
  81. $securityConfig,
  82. $data
  83. );
  84. }
  85. /**
  86. * Obtain order properties for JSON serialization.
  87. *
  88. * @return string[]
  89. */
  90. private function getOrderProperties()
  91. {
  92. return [
  93. 'entity_id',
  94. 'is_virtual',
  95. 'store_id',
  96. 'customer_id',
  97. 'base_shipping_amount',
  98. 'customer_is_guest',
  99. 'billing_address_id',
  100. 'shipping_address_id',
  101. 'weight',
  102. 'total_qty_ordered',
  103. 'base_currency_code',
  104. ];
  105. }
  106. /**
  107. * Obtain order item properties for JSON serialization.
  108. *
  109. * @return string[]
  110. */
  111. private function getOrderItemProperties()
  112. {
  113. return [
  114. 'item_id',
  115. 'order_id',
  116. 'store_id',
  117. 'product_id',
  118. 'weight',
  119. 'is_virtual',
  120. 'sku',
  121. 'name',
  122. 'qty_ordered',
  123. 'qty_shipped',
  124. 'base_price',
  125. 'base_row_total'
  126. ];
  127. }
  128. /**
  129. * Obtain order address properties for JSON serialization.
  130. *
  131. * @return string[]
  132. */
  133. private function getOrderAddressProperties()
  134. {
  135. return [
  136. 'entity_id',
  137. 'postcode',
  138. 'lastname',
  139. 'street',
  140. 'city',
  141. 'email',
  142. 'telephone',
  143. 'country_id',
  144. 'firstname',
  145. 'address_type',
  146. 'prefix',
  147. 'middlename',
  148. 'suffix',
  149. 'company'
  150. ];
  151. }
  152. /**
  153. * @return \Magento\Sales\Model\Order
  154. */
  155. private function getOrder()
  156. {
  157. /** @var \Magento\Sales\Model\Order\Shipment $shipment */
  158. $shipment = $this->shipmentProvider->getSalesShipment();
  159. $order = $shipment->getOrder();
  160. return $order;
  161. }
  162. /**
  163. * @return string
  164. */
  165. public function getLocale()
  166. {
  167. $order = $this->getOrder();
  168. $localeCode = $this->_scopeConfig->getValue(
  169. DirectoryHelper::XML_PATH_DEFAULT_LOCALE,
  170. ScopeInterface::SCOPE_STORE,
  171. $order->getStore()->getCode()
  172. );
  173. return strtolower(str_replace('_', '-', $localeCode));
  174. }
  175. /**
  176. * @return null|string
  177. */
  178. public function getDefaultCurrency()
  179. {
  180. $order = $this->getOrder();
  181. return $order->getBaseCurrencyCode();
  182. }
  183. /**
  184. * @return string
  185. */
  186. public function getDefaultDimensionsUnit()
  187. {
  188. $weightUnit = $this->getDefaultWeightUnit();
  189. if ($weightUnit === 'lbs') {
  190. return 'in';
  191. }
  192. return 'cm';
  193. }
  194. /**
  195. * @return string
  196. */
  197. public function getDefaultWeightUnit()
  198. {
  199. $order = $this->getOrder();
  200. $weightUnit = $this->_scopeConfig->getValue(
  201. DirectoryHelper::XML_PATH_WEIGHT_UNIT,
  202. ScopeInterface::SCOPE_STORE,
  203. $order->getStore()->getCode()
  204. );
  205. return $weightUnit;
  206. }
  207. /**
  208. * Obtain Magento REST API endpoint for shipment creation.
  209. *
  210. * @return string
  211. */
  212. public function getShipEndpoint()
  213. {
  214. $orderId = $this->getOrder()->getId();
  215. $endpoint = $this->_urlBuilder->getDirectUrl("rest/V1/order/$orderId/ship", ['_secure' => true]);
  216. // core bug workaround, route parameter "_direct" does not get reset
  217. $this->_urlBuilder->getUrl("rest/V1/order/$orderId/ship", ['_direct' => null]);
  218. return $endpoint;
  219. }
  220. /**
  221. * Obtain a JSON representation of relevant order data for usage in the
  222. * OrderShip UI component.
  223. *
  224. * @return string
  225. */
  226. public function getOrderData()
  227. {
  228. $order = $this->getOrder();
  229. $orderItems = [];
  230. foreach ($order->getAllItems() as $orderItem) {
  231. if (!$orderItem->getIsVirtual() && !$orderItem->getParentItem()) {
  232. // skip virtual and child items
  233. $orderItems[$orderItem->getId()] = $orderItem->toArray($this->getOrderItemProperties());
  234. }
  235. }
  236. $orderAddresses = [];
  237. /** @var \Magento\Sales\Model\Order\Address $orderAddress */
  238. foreach ($order->getAddresses() as $orderAddress) {
  239. $orderAddresses[$orderAddress->getId()] = $orderAddress->toArray($this->getOrderAddressProperties());
  240. $orderAddresses[$orderAddress->getId()]['region'] = $orderAddress->getRegionCode();
  241. }
  242. $orderData = $order->toArray($this->getOrderProperties());
  243. $orderData['items'] = $orderItems;
  244. $orderData['addresses'] = $orderAddresses;
  245. return json_encode($orderData);
  246. }
  247. /**
  248. * @return string
  249. */
  250. public function getSelectedExperience()
  251. {
  252. $order = $this->getOrder();
  253. $shippingMethod = $order->getShippingMethod(true);
  254. $experienceCode = $shippingMethod->getData('method');
  255. return $experienceCode;
  256. }
  257. /**
  258. * @return string
  259. */
  260. public function getExtOrderId()
  261. {
  262. $order = $this->getOrder();
  263. try {
  264. $orderReference = $this->orderRepository->getReferenceByOrderId($order->getId());
  265. $extOrderId = $orderReference->getExtOrderId();
  266. } catch (LocalizedException $e) {
  267. $this->_logger->critical($e->getMessage(), ['exception' => $e]);
  268. $extOrderId = '';
  269. }
  270. return $extOrderId;
  271. }
  272. /**
  273. * Obtain shipmentViewPageUrl
  274. *
  275. * @return string
  276. */
  277. public function getShipmentViewPageUrl()
  278. {
  279. return $this->_urlBuilder->getUrl('sales/shipment/view', ['shipment_id' => '--id--']);
  280. }
  281. /**
  282. * @return string
  283. */
  284. public function getConfigUrl()
  285. {
  286. return $this->getUrl('adminhtml/system_config/edit', [
  287. 'section' => 'carriers',
  288. '_fragment' => 'carriers_temando-link',
  289. ]);
  290. }
  291. /**
  292. * Check if a shipment was registered for component rendering.
  293. * @see \Temando\Shipping\Plugin\Shipping\Order\ShipmentLoaderPlugin::afterLoad
  294. *
  295. * @return bool
  296. */
  297. public function canShow()
  298. {
  299. /** @var \Magento\Sales\Model\Order\Shipment $shipment */
  300. return (bool) $this->shipmentProvider->getSalesShipment();
  301. }
  302. }