Pdfinvoices.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Controller\Adminhtml\Order;
  7. use Magento\Framework\App\ResponseInterface;
  8. use Magento\Framework\App\Filesystem\DirectoryList;
  9. use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
  10. use Magento\Ui\Component\MassAction\Filter;
  11. use Magento\Sales\Model\Order\Pdf\Invoice;
  12. use Magento\Framework\Stdlib\DateTime\DateTime;
  13. use Magento\Framework\App\Response\Http\FileFactory;
  14. use Magento\Backend\App\Action\Context;
  15. use Magento\Framework\Controller\ResultInterface;
  16. use Magento\Sales\Model\ResourceModel\Order\Invoice\CollectionFactory;
  17. use Magento\Sales\Model\ResourceModel\Order\Collection as OrderCollection;
  18. /**
  19. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  20. */
  21. class Pdfinvoices extends \Magento\Sales\Controller\Adminhtml\Order\PdfDocumentsMassAction
  22. {
  23. /**
  24. * Authorization level of a basic admin session
  25. */
  26. const ADMIN_RESOURCE = 'Magento_Sales::invoice';
  27. /**
  28. * @var FileFactory
  29. */
  30. protected $fileFactory;
  31. /**
  32. * @var DateTime
  33. */
  34. protected $dateTime;
  35. /**
  36. * @var Invoice
  37. */
  38. protected $pdfInvoice;
  39. /**
  40. * @var CollectionFactory
  41. */
  42. protected $collectionFactory;
  43. /**
  44. * @param Context $context
  45. * @param Filter $filter
  46. * @param CollectionFactory $collectionFactory
  47. * @param DateTime $dateTime
  48. * @param FileFactory $fileFactory
  49. * @param Invoice $pdfInvoice
  50. */
  51. public function __construct(
  52. Context $context,
  53. Filter $filter,
  54. CollectionFactory $collectionFactory,
  55. DateTime $dateTime,
  56. FileFactory $fileFactory,
  57. Invoice $pdfInvoice
  58. ) {
  59. $this->fileFactory = $fileFactory;
  60. $this->dateTime = $dateTime;
  61. $this->pdfInvoice = $pdfInvoice;
  62. $this->collectionFactory = $collectionFactory;
  63. parent::__construct($context, $filter);
  64. }
  65. /**
  66. * Print invoices for selected orders
  67. *
  68. * @param AbstractCollection $collection
  69. * @return ResponseInterface|ResultInterface
  70. * @throws \Exception
  71. */
  72. protected function massAction(AbstractCollection $collection)
  73. {
  74. $invoicesCollection = $this->collectionFactory->create()->setOrderFilter(['in' => $collection->getAllIds()]);
  75. if (!$invoicesCollection->getSize()) {
  76. $this->messageManager->addErrorMessage(__('There are no printable documents related to selected orders.'));
  77. return $this->resultRedirectFactory->create()->setPath($this->getComponentRefererUrl());
  78. }
  79. $pdf = $this->pdfInvoice->getPdf($invoicesCollection->getItems());
  80. $fileContent = ['type' => 'string', 'value' => $pdf->render(), 'rm' => true];
  81. return $this->fileFactory->create(
  82. sprintf('invoice%s.pdf', $this->dateTime->date('Y-m-d_H-i-s')),
  83. $fileContent,
  84. DirectoryList::VAR_DIR,
  85. 'application/pdf'
  86. );
  87. }
  88. }