-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathPrintInvoice.php
97 lines (90 loc) · 3.38 KB
/
PrintInvoice.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
/**
*
* Copyright 2014 Adobe
* All Rights Reserved.
*/
namespace Magento\Sales\Controller\AbstractController;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
abstract class PrintInvoice extends \Magento\Framework\App\Action\Action
{
/**
* @var OrderViewAuthorizationInterface
*/
protected $orderAuthorization;
/**
* @var \Magento\Framework\Registry
*/
protected $_coreRegistry;
/**
* @var PageFactory
*/
protected $resultPageFactory;
/**
* @param Context $context
* @param OrderViewAuthorizationInterface $orderAuthorization
* @param \Magento\Framework\Registry $registry
* @param PageFactory $resultPageFactory
*/
public function __construct(
Context $context,
OrderViewAuthorizationInterface $orderAuthorization,
\Magento\Framework\Registry $registry,
PageFactory $resultPageFactory
) {
$this->orderAuthorization = $orderAuthorization;
$this->_coreRegistry = $registry;
$this->resultPageFactory = $resultPageFactory;
parent::__construct($context);
}
/**
* Print Invoice Action
*
* @return \Magento\Framework\Controller\Result\Redirect|\Magento\Framework\View\Result\Page
*/
public function execute()
{
$invoiceId = (int)$this->getRequest()->getParam('invoice_id');
if ($invoiceId) {
try {
$invoice = $this->_objectManager->create(
\Magento\Sales\Api\InvoiceRepositoryInterface::class
)->get($invoiceId);
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
$this->messageManager->addError(__($e->getMessage()));
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
if ($this->_objectManager->get(\Magento\Customer\Model\Session::class)->isLoggedIn()) {
$resultRedirect->setPath('*/*/history');
} else {
$resultRedirect->setPath('sales/guest/form');
}
return $resultRedirect;
}
$order = $invoice->getOrder();
} else {
$orderId = (int)$this->getRequest()->getParam('order_id');
$order = $this->_objectManager->create(\Magento\Sales\Model\Order::class)->load($orderId);
}
if ($this->orderAuthorization->canView($order)) {
$this->_coreRegistry->register('current_order', $order);
if (isset($invoice)) {
$this->_coreRegistry->register('current_invoice', $invoice);
}
/** @var \Magento\Framework\View\Result\Page $resultPage */
$resultPage = $this->resultPageFactory->create();
$resultPage->addHandle('print');
return $resultPage;
} else {
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
if ($this->_objectManager->get(\Magento\Customer\Model\Session::class)->isLoggedIn()) {
$resultRedirect->setPath('*/*/history');
} else {
$resultRedirect->setPath('sales/guest/form');
}
return $resultRedirect;
}
}
}