Untitled

mail@pastecode.io avatar
unknown
php
5 months ago
3.8 kB
4
Indexable
<?php

declare(strict_types=1);

namespace Creativestyle\CustomizationRoecklOrder\Controller\Order;


class PrintInvoice extends \Magento\Sales\Controller\Order\PrintInvoice implements \Magento\Framework\App\Action\HttpGetActionInterface
{
    // phpcs:disable
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Sales\Controller\AbstractController\OrderViewAuthorizationInterface $orderAuthorization,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        protected \Magento\Framework\App\RequestInterface $requestInterface,
        protected \Magento\Sales\Api\OrderRepositoryInterfaceFactory $orderRepositoryInterfaceFactory,
        protected \Magetrend\PdfTemplates\Helper\Data $moduleHelper,
        protected \Magento\Framework\Stdlib\DateTime\DateTime $dateTime,
        protected \Magento\Framework\App\Response\Http\FileFactory $fileFactory,
        protected \Magento\Framework\Filesystem $filesystem,
        protected \Magetrend\PdfTemplates\Model\Template $template,
        protected \Magento\Sales\Api\InvoiceRepositoryInterface $invoiceRepository
    ) {
        parent::__construct($context, $orderAuthorization, $registry, $resultPageFactory);
    }
    // phpcs:enable

    public function execute()
    {
        $orderId = (int)$this->requestInterface->getParam('order_id');
        if (!$orderId) {
            $invoiceId = (int)$this->requestInterface->getParam('invoice_id');
            $invoiceRepository = $this->invoiceRepository->get($invoiceId);
            $orderId = $invoiceRepository->getOrderId();
        }

        $orderRepository = $this->orderRepositoryInterfaceFactory->create();
        $order = $orderRepository->get($orderId);
        $storeId = $order->getStoreId();

        if (!$this->orderAuthorization->canView($order)) {
            return  $this->getRedirect($order);
        }

        $invoices = $order->getInvoiceCollection();
        $pdf = $this->template->getPdf($invoices->getItems());
        $fileName = $this->getFileName($order, (int) $storeId);

        if ($pdf) {
            $this->createInvoicePdf($fileName, $pdf);
        }
    }

    protected function getRedirect(\Magento\Sales\Api\Data\OrderInterface $order): \Magento\Framework\Controller\Result\Redirect|\Magento\Framework\Controller\ResultInterface
    {
        $redirect = $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT);
        $redirect->setUrl('*/*/history/');

        $this->messageManager->addErrorMessage(
            __(
                'You can not download invoice for order #%1. Please contact store administrator.',
                $order->getIncrementId()
            )
        );

        return $redirect;
    }

    protected function getFileName(\Magento\Sales\Api\Data\OrderInterface $order, ?int $storeId): string
    {
        return $this->moduleHelper->getFileName(
            \Magetrend\PdfTemplates\Helper\Data::FILENAME_ORDER,
            [
                'increment_id' => $this->moduleHelper->prepareFileName($order->getIncrementId()),
                'date' => $this->dateTime->date('Y-m-d_H-i-s')
            ],
            $storeId
        );
    }

    protected function createInvoicePdf(string $fileName, \Zend_Pdf $pdf): void
    {
        $path = $this->filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::TMP)->getAbsolutePath($fileName);
        $pdf->save($path);
        $this->fileFactory->create(
            $fileName,
            ['value' => $fileName,
                'type' => 'filename',
                'rm' => true],
            \Magento\Framework\App\Filesystem\DirectoryList::TMP
        );
    }
}
Leave a Comment