Pdfinvoices.php 2.4 KB

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