49 lines
1.6 KiB
PHP
49 lines
1.6 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\VatInvoice\Controller\Adminhtml\Invoice;
|
|
|
|
use Esun\VatInvoice\Model\Pdf\VatInvoice;
|
|
use Magento\Backend\App\Action;
|
|
use Magento\Framework\App\Response\Http\FileFactory;
|
|
use Magento\Framework\Controller\Result\Redirect;
|
|
use Magento\Framework\Exception\NoSuchEntityException;
|
|
use Magento\Sales\Api\InvoiceRepositoryInterface;
|
|
|
|
class PrintAction extends Action
|
|
{
|
|
public const ADMIN_RESOURCE = 'Magento_Sales::sales_invoice';
|
|
|
|
public function __construct(
|
|
Action\Context $context,
|
|
private readonly InvoiceRepositoryInterface $invoiceRepository,
|
|
private readonly VatInvoice $pdf,
|
|
private readonly FileFactory $fileFactory
|
|
) {
|
|
parent::__construct($context);
|
|
}
|
|
|
|
public function execute()
|
|
{
|
|
try {
|
|
$invoice = $this->invoiceRepository->get((int)$this->getRequest()->getParam('invoice_id'));
|
|
$content = $this->pdf->getPdf($invoice)->render();
|
|
|
|
return $this->fileFactory->create(
|
|
'vat-invoice-' . $invoice->getIncrementId() . '.pdf',
|
|
$content,
|
|
'var',
|
|
'application/pdf'
|
|
);
|
|
} catch (NoSuchEntityException $exception) {
|
|
$this->messageManager->addErrorMessage(__('The invoice no longer exists.'));
|
|
} catch (\Throwable $exception) {
|
|
$this->messageManager->addErrorMessage(__('The VAT invoice PDF could not be generated.'));
|
|
}
|
|
|
|
/** @var Redirect $result */
|
|
$result = $this->resultRedirectFactory->create();
|
|
return $result->setPath('sales/order/index');
|
|
}
|
|
}
|