Your IP : 127.0.0.1


Current Path : /home/dev2.destoffenstraat.com/app/code/Mrgig/MV/Controller/Adminhtml/Invoice/
Upload File :
Current File : /home/dev2.destoffenstraat.com/app/code/Mrgig/MV/Controller/Adminhtml/Invoice/Send.php

<?php

namespace Mrgig\MV\Controller\Adminhtml\Invoice;
use Mrgig\MV\Model\Config\Source\Status;

class Send extends \Magento\Backend\App\Action
{
    protected $_resultPageFactory;

    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Sales\Api\InvoiceRepositoryInterface $invoiceRepository,
        \Mrgig\MV\Model\CustomerInvoice $customerInvoice
    ) 
    {
        parent::__construct($context);
        $this->invoiceRepository = $invoiceRepository;
        $this->customerInvoice = $customerInvoice;
    }

    public function getInvoiceData($invoiceId)
    {
        try {
            $invoice = $this->invoiceRepository->get($invoiceId);
        } catch (\Exception $exception)  {
            $invoice = null;
        }

        return $invoice;
    }

    public function execute()
    {
        $resultRedirect = $this->resultRedirectFactory->create();

        $invoiceId = $this->getRequest()->getParam('invoice_id');
        $invoice = $this->getInvoiceData($invoiceId);
        if($invoice && $invoice->getId()) {
            try {
                $isSend = $this->customerInvoice->createInvoice($invoice);
                if($isSend) {
                    $this->messageManager->addSuccessMessage(__('Import invoice successfully. Invoice Id: %1 ', $invoice->getIncrementId()));
                }
            } catch(\Exception $exception) {
                $this->messageManager->addError($exception->getMessage());
            }
        }

        return $this->resultRedirectFactory->create()->setPath(
            'sales/invoice/view',
            [
                'invoice_id' => $invoiceId
            ]
        );
    }
}