Shipment.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Block\Order\PrintOrder;
  7. use Magento\Framework\View\Element\AbstractBlock;
  8. /**
  9. * Sales order details block
  10. *
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class Shipment extends \Magento\Sales\Block\Items\AbstractItems
  15. {
  16. /**
  17. * Tracks for Shippings
  18. *
  19. * @var array
  20. */
  21. protected $tracks = [];
  22. /**
  23. * Order shipments collection
  24. *
  25. * @var array|\Magento\Sales\Model\ResourceModel\Order\Shipment\Collection
  26. */
  27. protected $shipmentsCollection;
  28. /**
  29. * Core registry
  30. *
  31. * @var \Magento\Framework\Registry
  32. */
  33. protected $coreRegistry = null;
  34. /**
  35. * @var \Magento\Payment\Helper\Data
  36. */
  37. protected $paymentHelper;
  38. /**
  39. * @var \Magento\Sales\Model\Order\Address\Renderer
  40. */
  41. protected $addressRenderer;
  42. /**
  43. * @param \Magento\Framework\View\Element\Template\Context $context
  44. * @param \Magento\Framework\Registry $registry
  45. * @param \Magento\Payment\Helper\Data $paymentHelper
  46. * @param \Magento\Sales\Model\Order\Address\Renderer $addressRenderer
  47. * @param array $data
  48. */
  49. public function __construct(
  50. \Magento\Framework\View\Element\Template\Context $context,
  51. \Magento\Framework\Registry $registry,
  52. \Magento\Payment\Helper\Data $paymentHelper,
  53. \Magento\Sales\Model\Order\Address\Renderer $addressRenderer,
  54. array $data = []
  55. ) {
  56. $this->paymentHelper = $paymentHelper;
  57. $this->coreRegistry = $registry;
  58. $this->addressRenderer = $addressRenderer;
  59. parent::__construct($context, $data);
  60. }
  61. /**
  62. * Load all tracks and save it to local cache by shipments
  63. *
  64. * @return $this
  65. */
  66. protected function _beforeToHtml()
  67. {
  68. $tracksCollection = $this->getOrder()->getTracksCollection();
  69. foreach ($tracksCollection->getItems() as $track) {
  70. $shipmentId = $track->getParentId();
  71. $this->tracks[$shipmentId][] = $track;
  72. }
  73. $shipment = $this->coreRegistry->registry('current_shipment');
  74. if ($shipment) {
  75. $this->shipmentsCollection = [$shipment];
  76. } else {
  77. $this->shipmentsCollection = $this->getOrder()->getShipmentsCollection();
  78. }
  79. return parent::_beforeToHtml();
  80. }
  81. /**
  82. * @return void
  83. */
  84. protected function _prepareLayout()
  85. {
  86. $this->pageConfig->getTitle()->set(__('Order # %1', $this->getOrder()->getRealOrderId()));
  87. $infoBlock = $this->paymentHelper->getInfoBlock($this->getOrder()->getPayment(), $this->getLayout());
  88. $this->setChild('payment_info', $infoBlock);
  89. }
  90. /**
  91. * @return string
  92. */
  93. public function getBackUrl()
  94. {
  95. return $this->getUrl('*/*/history');
  96. }
  97. /**
  98. * @return string
  99. */
  100. public function getPrintUrl()
  101. {
  102. return $this->getUrl('*/*/print');
  103. }
  104. /**
  105. * @return string
  106. */
  107. public function getPaymentInfoHtml()
  108. {
  109. return $this->getChildHtml('payment_info');
  110. }
  111. /**
  112. * @return array|null
  113. */
  114. public function getOrder()
  115. {
  116. return $this->coreRegistry->registry('current_order');
  117. }
  118. /**
  119. * @return array|null
  120. */
  121. public function getShipment()
  122. {
  123. return $this->coreRegistry->registry('current_shipment');
  124. }
  125. /**
  126. * @param AbstractBlock $renderer
  127. * @return $this
  128. */
  129. protected function _prepareItem(AbstractBlock $renderer)
  130. {
  131. $renderer->setPrintStatus(true);
  132. return parent::_prepareItem($renderer);
  133. }
  134. /**
  135. * Retrieve order shipments collection
  136. *
  137. * @return array|\Magento\Sales\Model\ResourceModel\Order\Shipment\Collection
  138. */
  139. public function getShipmentsCollection()
  140. {
  141. return $this->shipmentsCollection;
  142. }
  143. /**
  144. * Getter for order tracking numbers collection per shipment
  145. *
  146. * @param \Magento\Sales\Model\Order\Shipment $shipment
  147. * @return array
  148. */
  149. public function getShipmentTracks($shipment)
  150. {
  151. $tracks = [];
  152. if (!empty($this->tracks[$shipment->getId()])) {
  153. $tracks = $this->tracks[$shipment->getId()];
  154. }
  155. return $tracks;
  156. }
  157. /**
  158. * Getter for shipment address by format
  159. *
  160. * @param \Magento\Sales\Model\Order\Shipment $shipment
  161. * @return string
  162. */
  163. public function getShipmentAddressFormattedHtml($shipment)
  164. {
  165. $shippingAddress = $shipment->getShippingAddress();
  166. if (!$shippingAddress instanceof \Magento\Sales\Model\Order\Address) {
  167. return '';
  168. }
  169. return $this->addressRenderer->format($shippingAddress, 'html');
  170. }
  171. /**
  172. * Getter for billing address of order by format
  173. *
  174. * @param \Magento\Sales\Model\Order $order
  175. * @return string
  176. */
  177. public function getBillingAddressFormattedHtml($order)
  178. {
  179. $billingAddress = $order->getBillingAddress();
  180. if (!$billingAddress instanceof \Magento\Sales\Model\Order\Address) {
  181. return '';
  182. }
  183. return $this->addressRenderer->format($billingAddress, 'html');
  184. }
  185. /**
  186. * Getter for billing address of order by format
  187. *
  188. * @param \Magento\Sales\Model\Order\Shipment $shipment
  189. * @return array
  190. */
  191. public function getShipmentItems($shipment)
  192. {
  193. $res = [];
  194. foreach ($shipment->getItemsCollection() as $item) {
  195. if (!$item->getOrderItem()->getParentItem()) {
  196. $res[] = $item;
  197. }
  198. }
  199. return $res;
  200. }
  201. }