MassPrintShippingLabel.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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\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\Framework\Controller\ResultInterface;
  17. use Magento\Sales\Model\ResourceModel\Order\Shipment\CollectionFactory;
  18. class MassPrintShippingLabel extends \Magento\Sales\Controller\Adminhtml\Order\AbstractMassAction
  19. {
  20. /**
  21. * Authorization level of a basic admin session
  22. *
  23. * @see _isAllowed()
  24. */
  25. const ADMIN_RESOURCE = 'Magento_Sales::shipment';
  26. /**
  27. * @var LabelGenerator
  28. */
  29. protected $labelGenerator;
  30. /**
  31. * @var FileFactory
  32. */
  33. protected $fileFactory;
  34. /**
  35. * @param Context $context
  36. * @param Filter $filter
  37. * @param FileFactory $fileFactory
  38. * @param LabelGenerator $labelGenerator
  39. * @param CollectionFactory $collectionFactory
  40. */
  41. public function __construct(
  42. Context $context,
  43. Filter $filter,
  44. FileFactory $fileFactory,
  45. LabelGenerator $labelGenerator,
  46. CollectionFactory $collectionFactory
  47. ) {
  48. $this->collectionFactory = $collectionFactory;
  49. $this->fileFactory = $fileFactory;
  50. $this->labelGenerator = $labelGenerator;
  51. parent::__construct($context, $filter);
  52. }
  53. /**
  54. * Batch print shipping labels for whole shipments.
  55. * Push pdf document with shipping labels to user browser
  56. *
  57. * @param AbstractCollection $collection
  58. * @return ResponseInterface|ResultInterface
  59. */
  60. protected function massAction(AbstractCollection $collection)
  61. {
  62. $labelsContent = [];
  63. if ($collection->getSize()) {
  64. /** @var \Magento\Sales\Model\Order\Shipment $shipment */
  65. foreach ($collection as $shipment) {
  66. $labelContent = $shipment->getShippingLabel();
  67. if ($labelContent) {
  68. $labelsContent[] = $labelContent;
  69. }
  70. }
  71. }
  72. if (!empty($labelsContent)) {
  73. $outputPdf = $this->labelGenerator->combineLabelsPdf($labelsContent);
  74. return $this->fileFactory->create(
  75. 'ShippingLabels.pdf',
  76. $outputPdf->render(),
  77. DirectoryList::VAR_DIR,
  78. 'application/pdf'
  79. );
  80. }
  81. $this->messageManager->addError(__('There are no shipping labels related to selected shipments.'));
  82. return $this->resultRedirectFactory->create()->setPath('sales/shipment/');
  83. }
  84. }