View.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Controller\Adminhtml\Batch;
  6. use Magento\Backend\App\Action\Context;
  7. use Magento\Backend\Block\Widget\Button;
  8. use Magento\Framework\Controller\ResultFactory;
  9. use Magento\Framework\Exception\LocalizedException;
  10. use Magento\Sales\Api\Data\OrderInterface;
  11. use Magento\Sales\Api\ShipmentRepositoryInterface;
  12. use Magento\Sales\Model\Order\Shipment;
  13. use Temando\Shipping\Model\BatchInterface;
  14. use Temando\Shipping\Model\BatchProviderInterface;
  15. use Temando\Shipping\Model\ResourceModel\Repository\BatchRepositoryInterface;
  16. use Temando\Shipping\Model\ResourceModel\Repository\ShipmentReferenceRepositoryInterface;
  17. use Temando\Shipping\ViewModel\DataProvider\BatchUrl;
  18. /**
  19. * Temando View Batch Action
  20. *
  21. * @package Temando\Shipping\Controller
  22. * @author Rhodri Davies <rhodri.davies@temando.com>
  23. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * @link https://www.temando.com/
  25. */
  26. class View extends AbstractBatchAction
  27. {
  28. /**
  29. * @var BatchUrl
  30. */
  31. private $batchUrl;
  32. /**
  33. * @var BatchProviderInterface
  34. */
  35. private $batchProvider;
  36. /**
  37. * @var ShipmentReferenceRepositoryInterface
  38. */
  39. private $shipmentReferenceRepository;
  40. /**
  41. * @var ShipmentRepositoryInterface
  42. */
  43. private $shipmentRepository;
  44. /**
  45. * @var int
  46. */
  47. private $errorCounter = 0;
  48. /**
  49. * View constructor.
  50. * @param Context $context
  51. * @param BatchRepositoryInterface $batchRepository
  52. * @param BatchProviderInterface $batchProvider
  53. * @param BatchUrl $batchUrl
  54. * @param ShipmentReferenceRepositoryInterface $shipmentReferenceRepository
  55. * @param ShipmentRepositoryInterface $shipmentRepository
  56. */
  57. public function __construct(
  58. Context $context,
  59. BatchRepositoryInterface $batchRepository,
  60. BatchProviderInterface $batchProvider,
  61. BatchUrl $batchUrl,
  62. ShipmentReferenceRepositoryInterface $shipmentReferenceRepository,
  63. ShipmentRepositoryInterface $shipmentRepository
  64. ) {
  65. $this->batchUrl = $batchUrl;
  66. $this->batchProvider = $batchProvider;
  67. $this->shipmentReferenceRepository = $shipmentReferenceRepository;
  68. $this->shipmentRepository = $shipmentRepository;
  69. parent::__construct($context, $batchRepository, $batchProvider);
  70. }
  71. /**
  72. * @return bool
  73. */
  74. protected function _isAllowed()
  75. {
  76. return (
  77. $this->_authorization->isAllowed(static::ADMIN_RESOURCE) &&
  78. $this->_authorization->isAllowed('Magento_Sales::shipment')
  79. );
  80. }
  81. /**
  82. * Render template.
  83. *
  84. * @return \Magento\Backend\Model\View\Result\Page
  85. */
  86. public function execute()
  87. {
  88. $batch = $this->batchProvider->getBatch();
  89. $orders = $this->getOrdersForBatch($batch);
  90. $this->batchProvider->setOrders($orders);
  91. /** @var \Magento\Backend\Model\View\Result\Page $resultPage */
  92. $resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
  93. $resultPage->setActiveMenu('Temando_Shipping::batches');
  94. $resultPage->getConfig()->getTitle()->prepend(__('Batches'));
  95. $resultPage->addBreadcrumb(__('Batches'), __('Batches'), $this->getUrl('temando/batch'));
  96. /** @var \Magento\Backend\Block\Template $toolbar */
  97. $toolbar = $this->_view->getLayout()->getBlock('page.actions.toolbar');
  98. $toolbar->addChild('back', Button::class, [
  99. 'label' => __('Back'),
  100. 'onclick' => sprintf("setLocation('%s')", $this->batchUrl->getListActionUrl()),
  101. 'class' => 'back',
  102. 'level' => 0
  103. ]);
  104. $orderIds = $this->getOrderIds($orders);
  105. $toolbar->addChild('print_packing_slips', Button::class, [
  106. 'label' => __('Print All Packing Slips'),
  107. 'onclick' => sprintf(
  108. "setLocation('%s')",
  109. $this->batchUrl->getPrintAllPackingSlips($orderIds, $batch->getBatchId())
  110. ),
  111. 'class' => 'print',
  112. 'level' => -1
  113. ]);
  114. return $resultPage;
  115. }
  116. /**
  117. * @param BatchInterface $batch
  118. * @return OrderInterface[]
  119. */
  120. private function getOrdersForBatch(BatchInterface $batch)
  121. {
  122. $orders = [];
  123. $batchShipments = $batch->getIncludedShipments();
  124. foreach ($batchShipments as $batchShipment) {
  125. $extShipmentId = $batchShipment->getShipmentId();
  126. try {
  127. $shipmentReference = $this->shipmentReferenceRepository->getByExtShipmentId($extShipmentId);
  128. /** @var Shipment $salesShipment */
  129. $salesShipment = $this->shipmentRepository->get($shipmentReference->getShipmentId());
  130. /** @var OrderInterface[] $orders */
  131. $orders[$extShipmentId] = $salesShipment->getOrder();
  132. } catch (LocalizedException $e) {
  133. $this->errorCounter++;
  134. if ($this->errorCounter === 1) {
  135. $this->messageManager->addWarningMessage(
  136. // @codingStandardsIgnoreLine
  137. __('There are Shipments in this Batch that are still being created. Please check Back soon to view these Shipments.')
  138. );
  139. }
  140. }
  141. }
  142. return $orders;
  143. }
  144. /**
  145. * @param OrderInterface[] $orders
  146. * @return string[]
  147. */
  148. private function getOrderIds(array $orders)
  149. {
  150. $orderIds = [];
  151. foreach ($orders as $order) {
  152. $orderIds[] = $order->getEntityId();
  153. }
  154. return $orderIds;
  155. }
  156. }