Skip to content

33800: Fixed issue with print invoice on customer account #34211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Sales\Controller\AbstractController;

use Magento\Customer\Model\Session;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Registry;
use Magento\Framework\View\Result\Page;
use Magento\Framework\View\Result\PageFactory;
use Magento\Sales\Api\InvoiceRepositoryInterface;
use Magento\Sales\Api\OrderRepositoryInterface;

abstract class PrintInvoice extends \Magento\Framework\App\Action\Action
{
Expand All @@ -17,7 +25,7 @@ abstract class PrintInvoice extends \Magento\Framework\App\Action\Action
protected $orderAuthorization;

/**
* @var \Magento\Framework\Registry
* @var Registry
*/
protected $_coreRegistry;

Expand All @@ -26,55 +34,89 @@ abstract class PrintInvoice extends \Magento\Framework\App\Action\Action
*/
protected $resultPageFactory;

/**
* @var InvoiceRepositoryInterface
*/
protected $invoiceRepository;

/**
* @var Session
*/
protected $session;

/**
* @var OrderRepositoryInterface
*/
protected $order;

/**
* @param Context $context
* @param OrderViewAuthorizationInterface $orderAuthorization
* @param \Magento\Framework\Registry $registry
* @param Registry $registry
* @param PageFactory $resultPageFactory
* @param InvoiceRepositoryInterface $invoiceRepository
* @param Session $session
* @param OrderRepositoryInterface $order
*/
public function __construct(
Context $context,
OrderViewAuthorizationInterface $orderAuthorization,
\Magento\Framework\Registry $registry,
PageFactory $resultPageFactory
Registry $registry,
PageFactory $resultPageFactory,
InvoiceRepositoryInterface $invoiceRepository,
Session $session,
OrderRepositoryInterface $order
Comment on lines +66 to +68
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

) {
$this->orderAuthorization = $orderAuthorization;
$this->_coreRegistry = $registry;
$this->resultPageFactory = $resultPageFactory;
$this->invoiceRepository = $invoiceRepository;
$this->session = $session;
$this->order = $order;
parent::__construct($context);
}

/**
* Print Invoice Action
*
* @return \Magento\Framework\Controller\Result\Redirect|\Magento\Framework\View\Result\Page
* @return Redirect|Page
*/
public function execute()
{
$invoiceId = (int)$this->getRequest()->getParam('invoice_id');
if ($invoiceId) {
$invoice = $this->_objectManager->create(
\Magento\Sales\Api\InvoiceRepositoryInterface::class
)->get($invoiceId);
try {
$invoice = $this->invoiceRepository->get($invoiceId);
} catch (NoSuchEntityException $e) {
$this->messageManager->addErrorMessage(__($e->getMessage()));
/** @var Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
if ($this->session->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);
$order = $this->order->get($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 */
/** @var Page $resultPage */
$resultPage = $this->resultPageFactory->create();
$resultPage->addHandle('print');
return $resultPage;
} else {
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
/** @var Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
if ($this->_objectManager->get(\Magento\Customer\Model\Session::class)->isLoggedIn()) {
if ($this->session->isLoggedIn()) {
$resultRedirect->setPath('*/*/history');
} else {
$resultRedirect->setPath('sales/guest/form');
Expand Down