123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- <?php
- /**
- *
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Shipping\Controller\Adminhtml\Order\Shipment;
- use Magento\Backend\App\Action;
- use Magento\Framework\App\ResponseInterface;
- use Magento\Framework\App\Filesystem\DirectoryList;
- use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
- use Magento\Ui\Component\MassAction\Filter;
- use Magento\Backend\App\Action\Context;
- use Magento\Shipping\Model\Shipping\LabelGenerator;
- use Magento\Framework\App\Response\Http\FileFactory;
- use Magento\Sales\Model\ResourceModel\Order\Shipment\CollectionFactory as ShipmentCollectionFactory;
- use Magento\Framework\Controller\ResultInterface;
- use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class MassPrintShippingLabel extends \Magento\Sales\Controller\Adminhtml\Order\AbstractMassAction
- {
- /**
- * Authorization level of a basic admin session
- *
- * @see _isAllowed()
- */
- const ADMIN_RESOURCE = 'Magento_Sales::shipment';
- /**
- * @var LabelGenerator
- */
- protected $labelGenerator;
- /**
- * @var FileFactory
- */
- protected $fileFactory;
- /**
- * @var CollectionFactory
- */
- protected $collectionFactory;
- /**
- * @var ShipmentCollectionFactory
- */
- protected $shipmentCollectionFactory;
- /**
- * @param Context $context
- * @param Filter $filter
- * @param CollectionFactory $collectionFactory
- * @param FileFactory $fileFactory
- * @param LabelGenerator $labelGenerator
- * @param ShipmentCollectionFactory $shipmentCollectionFactory
- */
- public function __construct(
- Context $context,
- Filter $filter,
- CollectionFactory $collectionFactory,
- FileFactory $fileFactory,
- LabelGenerator $labelGenerator,
- ShipmentCollectionFactory $shipmentCollectionFactory
- ) {
- $this->fileFactory = $fileFactory;
- $this->collectionFactory = $collectionFactory;
- $this->shipmentCollectionFactory = $shipmentCollectionFactory;
- $this->labelGenerator = $labelGenerator;
- parent::__construct($context, $filter);
- }
- /**
- * Batch print shipping labels for whole shipments.
- * Push pdf document with shipping labels to user browser
- *
- * @param AbstractCollection $collection
- * @return ResponseInterface|ResultInterface
- */
- protected function massAction(AbstractCollection $collection)
- {
- $labelsContent = [];
- $shipments = $this->shipmentCollectionFactory->create()->setOrderFilter(['in' => $collection->getAllIds()]);
- if ($shipments->getSize()) {
- /** @var \Magento\Sales\Model\Order\Shipment $shipment */
- foreach ($shipments as $shipment) {
- $labelContent = $shipment->getShippingLabel();
- if ($labelContent) {
- $labelsContent[] = $labelContent;
- }
- }
- }
- if (!empty($labelsContent)) {
- $outputPdf = $this->labelGenerator->combineLabelsPdf($labelsContent);
- return $this->fileFactory->create(
- 'ShippingLabels.pdf',
- $outputPdf->render(),
- DirectoryList::VAR_DIR,
- 'application/pdf'
- );
- }
- $this->messageManager->addError(__('There are no shipping labels related to selected orders.'));
- return $this->resultRedirectFactory->create()->setPath('sales/order/');
- }
- }
|