Info.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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\Sales\Order\View;
  6. use Magento\Framework\Exception\LocalizedException;
  7. use Magento\Sales\Api\Data\OrderAddressInterfaceFactory;
  8. use Magento\Sales\Block\Adminhtml\Order\View\Info as SalesOrderInfo;
  9. use Temando\Shipping\Model\ResourceModel\Order\OrderRepository;
  10. use Temando\Shipping\Model\Shipment\ShipmentProviderInterface;
  11. /**
  12. * Temando Shipment Info Layout Block
  13. *
  14. * @deprecated since 1.2.0 | Block data is provided by view model
  15. * @see \Temando\Shipping\ViewModel\Shipment\Location
  16. *
  17. * @package Temando\Shipping\Block
  18. * @author Benjamin Heuer <benjamin.heuer@netresearch.de>
  19. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  20. * @link http://www.temando.com/
  21. *
  22. * @api
  23. */
  24. class Info extends SalesOrderInfo
  25. {
  26. /**
  27. * @var \Temando\Shipping\Model\ShipmentInterface
  28. */
  29. private $extShipment;
  30. /**
  31. * @var ShipmentProviderInterface
  32. */
  33. private $shipmentProvider;
  34. /**
  35. * @var OrderAddressInterfaceFactory
  36. */
  37. private $addressFactory;
  38. /**
  39. * @var OrderRepository
  40. */
  41. private $orderRepository;
  42. /**
  43. * Constructor
  44. *
  45. * @param \Magento\Backend\Block\Template\Context $context
  46. * @param \Magento\Framework\Registry $registry
  47. * @param \Magento\Sales\Helper\Admin $adminHelper
  48. * @param \Magento\Customer\Api\GroupRepositoryInterface $groupRepository
  49. * @param \Magento\Customer\Api\CustomerMetadataInterface $metadata
  50. * @param \Magento\Customer\Model\Metadata\ElementFactory $elementFactory
  51. * @param \Magento\Sales\Model\Order\Address\Renderer $addressRenderer
  52. * @param ShipmentProviderInterface $shipmentProvider
  53. * @param OrderAddressInterfaceFactory $addressFactory
  54. * @param OrderRepository $orderRepository
  55. * @param mixed[] $data
  56. */
  57. public function __construct(
  58. \Magento\Backend\Block\Template\Context $context,
  59. \Magento\Framework\Registry $registry,
  60. \Magento\Sales\Helper\Admin $adminHelper,
  61. \Magento\Customer\Api\GroupRepositoryInterface $groupRepository,
  62. \Magento\Customer\Api\CustomerMetadataInterface $metadata,
  63. \Magento\Customer\Model\Metadata\ElementFactory $elementFactory,
  64. \Magento\Sales\Model\Order\Address\Renderer $addressRenderer,
  65. ShipmentProviderInterface $shipmentProvider,
  66. OrderAddressInterfaceFactory $addressFactory,
  67. OrderRepository $orderRepository,
  68. array $data = []
  69. ) {
  70. $this->shipmentProvider = $shipmentProvider;
  71. $this->addressFactory = $addressFactory;
  72. $this->orderRepository = $orderRepository;
  73. parent::__construct(
  74. $context,
  75. $registry,
  76. $adminHelper,
  77. $groupRepository,
  78. $metadata,
  79. $elementFactory,
  80. $addressRenderer,
  81. $data
  82. );
  83. }
  84. /**
  85. * Declare External Shipment instance
  86. *
  87. * @return \Temando\Shipping\Model\ShipmentInterface
  88. */
  89. public function getExtShipment()
  90. {
  91. /** @var \Temando\Shipping\Model\ShipmentInterface $shipment */
  92. if ($this->extShipment === null) {
  93. if ($extShipment = $this->shipmentProvider->getShipment()) {
  94. $this->extShipment = $extShipment;
  95. } else {
  96. $this->extShipment = $this->_coreRegistry->registry('ext_shipment');
  97. }
  98. }
  99. return $this->extShipment;
  100. }
  101. /**
  102. * Check if the current order has a shipment at the Temando platform.
  103. * If so, it qualifies for some additional data to be displayed.
  104. *
  105. * @return bool
  106. */
  107. public function hasExtShipment()
  108. {
  109. /** @var \Temando\Shipping\Model\ShipmentInterface $shipment */
  110. $shipment = $this->getExtShipment();
  111. return isset($shipment);
  112. }
  113. /**
  114. * @return string
  115. */
  116. public function getExtShipmentId()
  117. {
  118. /** @var \Temando\Shipping\Model\ShipmentInterface $shipment */
  119. if ($shipment = $this->getExtShipment()) {
  120. return $shipment->getShipmentId();
  121. }
  122. return '';
  123. }
  124. /**
  125. * @return string
  126. */
  127. public function getExtOrderId()
  128. {
  129. try {
  130. /** @var \Temando\Shipping\Api\Data\Order\OrderReferenceInterface $orderReference */
  131. $orderReference = $this->orderRepository->getReferenceByOrderId($this->getOrder()->getId());
  132. return $orderReference->getExtOrderId();
  133. } catch (LocalizedException $exception) {
  134. return '';
  135. }
  136. }
  137. /**
  138. * @return string
  139. */
  140. public function getFormattedDestinationAddress()
  141. {
  142. /** @var \Temando\Shipping\Model\ShipmentInterface $shipment */
  143. $shipment = $this->getExtShipment();
  144. if (!$shipment) {
  145. return '';
  146. }
  147. $destinationLocation = $shipment->getDestinationLocation();
  148. $addressData = [
  149. 'region' => $destinationLocation->getRegionCode(),
  150. 'postcode' => $destinationLocation->getPostalCode(),
  151. 'lastname' => $destinationLocation->getPersonLastName(),
  152. 'street' => $destinationLocation->getStreet(),
  153. 'city' => $destinationLocation->getCity(),
  154. 'email' => $destinationLocation->getEmail(),
  155. 'telephone' => $destinationLocation->getPhoneNumber(),
  156. 'country_id' => $destinationLocation->getCountryCode(),
  157. 'firstname' => $destinationLocation->getPersonFirstName(),
  158. 'company' => $destinationLocation->getCompany()
  159. ];
  160. /** @var \Magento\Sales\Api\Data\OrderAddressInterface|\Magento\Sales\Model\Order\Address $address */
  161. $address = $this->addressFactory->create(['data' => $addressData]);
  162. $formattedAddress = $this->addressRenderer->format($address, 'html');
  163. return (string) $formattedAddress;
  164. }
  165. }