MassPrintShippingLabel.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Shipping\Controller\Adminhtml\Order\Shipment;
  8. use Magento\Backend\App\Action;
  9. use Magento\Framework\App\ResponseInterface;
  10. use Magento\Framework\App\Filesystem\DirectoryList;
  11. use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
  12. use Magento\Ui\Component\MassAction\Filter;
  13. use Magento\Backend\App\Action\Context;
  14. use Magento\Shipping\Model\Shipping\LabelGenerator;
  15. use Magento\Framework\App\Response\Http\FileFactory;
  16. use Magento\Sales\Model\ResourceModel\Order\Shipment\CollectionFactory as ShipmentCollectionFactory;
  17. use Magento\Framework\Controller\ResultInterface;
  18. use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
  19. /**
  20. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  21. */
  22. class MassPrintShippingLabel extends \Magento\Sales\Controller\Adminhtml\Order\AbstractMassAction
  23. {
  24. /**
  25. * Authorization level of a basic admin session
  26. *
  27. * @see _isAllowed()
  28. */
  29. const ADMIN_RESOURCE = 'Magento_Sales::shipment';
  30. /**
  31. * @var LabelGenerator
  32. */
  33. protected $labelGenerator;
  34. /**
  35. * @var FileFactory
  36. */
  37. protected $fileFactory;
  38. /**
  39. * @var CollectionFactory
  40. */
  41. protected $collectionFactory;
  42. /**
  43. * @var ShipmentCollectionFactory
  44. */
  45. protected $shipmentCollectionFactory;
  46. /**
  47. * @param Context $context
  48. * @param Filter $filter
  49. * @param CollectionFactory $collectionFactory
  50. * @param FileFactory $fileFactory
  51. * @param LabelGenerator $labelGenerator
  52. * @param ShipmentCollectionFactory $shipmentCollectionFactory
  53. */
  54. public function __construct(
  55. Context $context,
  56. Filter $filter,
  57. CollectionFactory $collectionFactory,
  58. FileFactory $fileFactory,
  59. LabelGenerator $labelGenerator,
  60. ShipmentCollectionFactory $shipmentCollectionFactory
  61. ) {
  62. $this->fileFactory = $fileFactory;
  63. $this->collectionFactory = $collectionFactory;
  64. $this->shipmentCollectionFactory = $shipmentCollectionFactory;
  65. $this->labelGenerator = $labelGenerator;
  66. parent::__construct($context, $filter);
  67. }
  68. /**
  69. * Batch print shipping labels for whole shipments.
  70. * Push pdf document with shipping labels to user browser
  71. *
  72. * @param AbstractCollection $collection
  73. * @return ResponseInterface|ResultInterface
  74. */
  75. protected function massAction(AbstractCollection $collection)
  76. {
  77. $labelsContent = [];
  78. $shipments = $this->shipmentCollectionFactory->create()->setOrderFilter(['in' => $collection->getAllIds()]);
  79. if ($shipments->getSize()) {
  80. /** @var \Magento\Sales\Model\Order\Shipment $shipment */
  81. foreach ($shipments as $shipment) {
  82. $labelContent = $shipment->getShippingLabel();
  83. if ($labelContent) {
  84. $labelsContent[] = $labelContent;
  85. }
  86. }
  87. }
  88. if (!empty($labelsContent)) {
  89. $outputPdf = $this->labelGenerator->combineLabelsPdf($labelsContent);
  90. return $this->fileFactory->create(
  91. 'ShippingLabels.pdf',
  92. $outputPdf->render(),
  93. DirectoryList::VAR_DIR,
  94. 'application/pdf'
  95. );
  96. }
  97. $this->messageManager->addError(__('There are no shipping labels related to selected orders.'));
  98. return $this->resultRedirectFactory->create()->setPath('sales/order/');
  99. }
  100. }