12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Sales\Controller\Adminhtml\Order;
- 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\Sales\Model\Order\Pdf\Invoice;
- use Magento\Framework\Stdlib\DateTime\DateTime;
- use Magento\Framework\App\Response\Http\FileFactory;
- use Magento\Backend\App\Action\Context;
- use Magento\Framework\Controller\ResultInterface;
- use Magento\Sales\Model\ResourceModel\Order\Invoice\CollectionFactory;
- use Magento\Sales\Model\ResourceModel\Order\Collection as OrderCollection;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class Pdfinvoices extends \Magento\Sales\Controller\Adminhtml\Order\PdfDocumentsMassAction
- {
- /**
- * Authorization level of a basic admin session
- */
- const ADMIN_RESOURCE = 'Magento_Sales::invoice';
- /**
- * @var FileFactory
- */
- protected $fileFactory;
- /**
- * @var DateTime
- */
- protected $dateTime;
- /**
- * @var Invoice
- */
- protected $pdfInvoice;
- /**
- * @var CollectionFactory
- */
- protected $collectionFactory;
- /**
- * @param Context $context
- * @param Filter $filter
- * @param CollectionFactory $collectionFactory
- * @param DateTime $dateTime
- * @param FileFactory $fileFactory
- * @param Invoice $pdfInvoice
- */
- public function __construct(
- Context $context,
- Filter $filter,
- CollectionFactory $collectionFactory,
- DateTime $dateTime,
- FileFactory $fileFactory,
- Invoice $pdfInvoice
- ) {
- $this->fileFactory = $fileFactory;
- $this->dateTime = $dateTime;
- $this->pdfInvoice = $pdfInvoice;
- $this->collectionFactory = $collectionFactory;
- parent::__construct($context, $filter);
- }
- /**
- * Print invoices for selected orders
- *
- * @param AbstractCollection $collection
- * @return ResponseInterface|ResultInterface
- * @throws \Exception
- */
- protected function massAction(AbstractCollection $collection)
- {
- $invoicesCollection = $this->collectionFactory->create()->setOrderFilter(['in' => $collection->getAllIds()]);
- if (!$invoicesCollection->getSize()) {
- $this->messageManager->addErrorMessage(__('There are no printable documents related to selected orders.'));
- return $this->resultRedirectFactory->create()->setPath($this->getComponentRefererUrl());
- }
- $pdf = $this->pdfInvoice->getPdf($invoicesCollection->getItems());
- $fileContent = ['type' => 'string', 'value' => $pdf->render(), 'rm' => true];
- return $this->fileFactory->create(
- sprintf('invoice%s.pdf', $this->dateTime->date('Y-m-d_H-i-s')),
- $fileContent,
- DirectoryList::VAR_DIR,
- 'application/pdf'
- );
- }
- }
|