PrintInvoice.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Sales\Controller\AbstractController;
  8. use Magento\Framework\App\Action\Context;
  9. use Magento\Framework\View\Result\PageFactory;
  10. abstract class PrintInvoice extends \Magento\Framework\App\Action\Action
  11. {
  12. /**
  13. * @var OrderViewAuthorizationInterface
  14. */
  15. protected $orderAuthorization;
  16. /**
  17. * @var \Magento\Framework\Registry
  18. */
  19. protected $_coreRegistry;
  20. /**
  21. * @var PageFactory
  22. */
  23. protected $resultPageFactory;
  24. /**
  25. * @param Context $context
  26. * @param OrderViewAuthorizationInterface $orderAuthorization
  27. * @param \Magento\Framework\Registry $registry
  28. * @param PageFactory $resultPageFactory
  29. */
  30. public function __construct(
  31. Context $context,
  32. OrderViewAuthorizationInterface $orderAuthorization,
  33. \Magento\Framework\Registry $registry,
  34. PageFactory $resultPageFactory
  35. ) {
  36. $this->orderAuthorization = $orderAuthorization;
  37. $this->_coreRegistry = $registry;
  38. $this->resultPageFactory = $resultPageFactory;
  39. parent::__construct($context);
  40. }
  41. /**
  42. * Print Invoice Action
  43. *
  44. * @return \Magento\Framework\Controller\Result\Redirect|\Magento\Framework\View\Result\Page
  45. */
  46. public function execute()
  47. {
  48. $invoiceId = (int)$this->getRequest()->getParam('invoice_id');
  49. if ($invoiceId) {
  50. $invoice = $this->_objectManager->create(
  51. \Magento\Sales\Api\InvoiceRepositoryInterface::class
  52. )->get($invoiceId);
  53. $order = $invoice->getOrder();
  54. } else {
  55. $orderId = (int)$this->getRequest()->getParam('order_id');
  56. $order = $this->_objectManager->create(\Magento\Sales\Model\Order::class)->load($orderId);
  57. }
  58. if ($this->orderAuthorization->canView($order)) {
  59. $this->_coreRegistry->register('current_order', $order);
  60. if (isset($invoice)) {
  61. $this->_coreRegistry->register('current_invoice', $invoice);
  62. }
  63. /** @var \Magento\Framework\View\Result\Page $resultPage */
  64. $resultPage = $this->resultPageFactory->create();
  65. $resultPage->addHandle('print');
  66. return $resultPage;
  67. } else {
  68. /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
  69. $resultRedirect = $this->resultRedirectFactory->create();
  70. if ($this->_objectManager->get(\Magento\Customer\Model\Session::class)->isLoggedIn()) {
  71. $resultRedirect->setPath('*/*/history');
  72. } else {
  73. $resultRedirect->setPath('sales/guest/form');
  74. }
  75. return $resultRedirect;
  76. }
  77. }
  78. }