Remove event
Showing
6 changed files
with
199 additions
and
94 deletions
| ... | @@ -23,18 +23,21 @@ class DefaultRenderer extends \Magento\Sales\Block\Adminhtml\Items\Renderer\Defa | ... | @@ -23,18 +23,21 @@ class DefaultRenderer extends \Magento\Sales\Block\Adminhtml\Items\Renderer\Defa |
| 23 | \Magento\Backend\Block\Template\Context $context, | 23 | \Magento\Backend\Block\Template\Context $context, |
| 24 | \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry, | 24 | \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry, |
| 25 | \Magento\CatalogInventory\Api\StockConfigurationInterface $stockConfiguration, | 25 | \Magento\CatalogInventory\Api\StockConfigurationInterface $stockConfiguration, |
| 26 | \Magento\CatalogInventory\Model\Stock\StockItemRepository $stockItemRepository, | ||
| 26 | \Magento\Framework\Registry $registry, | 27 | \Magento\Framework\Registry $registry, |
| 27 | array $data = [] | 28 | array $data = [] |
| 28 | ) { | 29 | ) { |
| 29 | $this->_coreRegistry = $registry; | 30 | $this->_coreRegistry = $registry; |
| 31 | $this->_stockItemRepository = $stockItemRepository; | ||
| 30 | parent::__construct($context, $stockRegistry, $stockConfiguration, $registry, $data); | 32 | parent::__construct($context, $stockRegistry, $stockConfiguration, $registry, $data); |
| 31 | } | 33 | } |
| 32 | 34 | ||
| 33 | public function getErrorOutStock($productId) { | 35 | public function getErrorOutStock($productId) { |
| 34 | $productsId = explode(',',$this->getRequest()->getParam('productsId')); | 36 | $_productStock = $this->_stockItemRepository->get($productId); |
| 35 | if(in_array($productId, $productsId)) { | 37 | $html = ''; |
| 36 | $html = '<div id="messages"><div class="messages"><div class="message message-error error"><div data-ui-id="messages-message-error">This product is out of stock</div></div></div></div>'; | 38 | if(!$_productStock->getIsInStock()) { |
| 37 | return $html; | 39 | $html = '<div class="message message-error error"><div data-ui-id="messages-message-error">This product is out of stock</div></div>'; |
| 38 | } | 40 | } |
| 41 | return $html; | ||
| 39 | } | 42 | } |
| 40 | } | 43 | } | ... | ... |
| 1 | <?php | ||
| 2 | namespace FGCT\OverrideCreateInvoice\Controller\Adminhtml\Order\Invoice; | ||
| 3 | |||
| 4 | use Magento\Backend\App\Action; | ||
| 5 | use Magento\Framework\Exception\LocalizedException; | ||
| 6 | use Magento\Framework\Registry; | ||
| 7 | use Magento\Sales\Model\Order\Email\Sender\InvoiceSender; | ||
| 8 | use Magento\Sales\Model\Order\Email\Sender\ShipmentSender; | ||
| 9 | use Magento\Sales\Model\Order\ShipmentFactory; | ||
| 10 | use Magento\Sales\Model\Order\Invoice; | ||
| 11 | use Magento\Sales\Model\Service\InvoiceService; | ||
| 12 | |||
| 13 | /** | ||
| 14 | * @SuppressWarnings(PHPMD.CouplingBetweenObjects) | ||
| 15 | */ | ||
| 16 | class Save extends \Magento\Sales\Controller\Adminhtml\Order\Invoice\Save { | ||
| 17 | /** | ||
| 18 | * @var InvoiceSender | ||
| 19 | */ | ||
| 20 | protected $invoiceSender; | ||
| 21 | |||
| 22 | /** | ||
| 23 | * @var ShipmentSender | ||
| 24 | */ | ||
| 25 | protected $shipmentSender; | ||
| 26 | |||
| 27 | /** | ||
| 28 | * @var ShipmentFactory | ||
| 29 | */ | ||
| 30 | protected $shipmentFactory; | ||
| 31 | |||
| 32 | /** | ||
| 33 | * @var Registry | ||
| 34 | */ | ||
| 35 | protected $registry; | ||
| 36 | |||
| 37 | /** | ||
| 38 | * @var InvoiceService | ||
| 39 | */ | ||
| 40 | private $invoiceService; | ||
| 41 | |||
| 42 | /** | ||
| 43 | * @param Action\Context $context | ||
| 44 | * @param Registry $registry | ||
| 45 | * @param InvoiceSender $invoiceSender | ||
| 46 | * @param ShipmentSender $shipmentSender | ||
| 47 | * @param ShipmentFactory $shipmentFactory | ||
| 48 | * @param InvoiceService $invoiceService | ||
| 49 | */ | ||
| 50 | public function __construct( | ||
| 51 | Action\Context $context, | ||
| 52 | Registry $registry, | ||
| 53 | InvoiceSender $invoiceSender, | ||
| 54 | ShipmentSender $shipmentSender, | ||
| 55 | ShipmentFactory $shipmentFactory, | ||
| 56 | InvoiceService $invoiceService, | ||
| 57 | \Magento\CatalogInventory\Model\Stock\StockItemRepository $stockItemRepository | ||
| 58 | ) { | ||
| 59 | $this->registry = $registry; | ||
| 60 | $this->invoiceSender = $invoiceSender; | ||
| 61 | $this->shipmentSender = $shipmentSender; | ||
| 62 | $this->shipmentFactory = $shipmentFactory; | ||
| 63 | $this->invoiceService = $invoiceService; | ||
| 64 | $this->_stockItemRepository = $stockItemRepository; | ||
| 65 | parent::__construct($context, $registry, $invoiceSender, $shipmentSender, $shipmentFactory, $invoiceService); | ||
| 66 | } | ||
| 67 | |||
| 68 | public function execute() { | ||
| 69 | /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */ | ||
| 70 | $resultRedirect = $this->resultRedirectFactory->create(); | ||
| 71 | |||
| 72 | $formKeyIsValid = $this->_formKeyValidator->validate($this->getRequest()); | ||
| 73 | $isPost = $this->getRequest()->isPost(); | ||
| 74 | if (!$formKeyIsValid || !$isPost) { | ||
| 75 | $this->messageManager->addError(__('We can\'t save the invoice right now.')); | ||
| 76 | return $resultRedirect->setPath('sales/order/index'); | ||
| 77 | } | ||
| 78 | |||
| 79 | $data = $this->getRequest()->getPost('invoice'); | ||
| 80 | $orderId = $this->getRequest()->getParam('order_id'); | ||
| 81 | |||
| 82 | if (!empty($data['comment_text'])) { | ||
| 83 | $this->_objectManager->get(\Magento\Backend\Model\Session::class)->setCommentText($data['comment_text']); | ||
| 84 | } | ||
| 85 | |||
| 86 | try { | ||
| 87 | $invoiceData = $this->getRequest()->getParam('invoice', []); | ||
| 88 | $invoiceItems = isset($invoiceData['items']) ? $invoiceData['items'] : []; | ||
| 89 | /** @var \Magento\Sales\Model\Order $order */ | ||
| 90 | $order = $this->_objectManager->create(\Magento\Sales\Model\Order::class)->load($orderId); | ||
| 91 | if (!$order->getId()) { | ||
| 92 | throw new \Magento\Framework\Exception\LocalizedException(__('The order no longer exists.')); | ||
| 93 | } | ||
| 94 | |||
| 95 | if (!$order->canInvoice()) { | ||
| 96 | throw new \Magento\Framework\Exception\LocalizedException( | ||
| 97 | __('The order does not allow an invoice to be created.') | ||
| 98 | ); | ||
| 99 | } | ||
| 100 | |||
| 101 | $invoice = $this->invoiceService->prepareInvoice($order, $invoiceItems); | ||
| 102 | foreach ($invoice->getAllItems() as $item) { | ||
| 103 | $_productStock = $this->_stockItemRepository->get($item->getProductId()); | ||
| 104 | if(!$_productStock->getIsInStock()) { | ||
| 105 | $this->messageManager->addError(__('Some of the products are out of stock!')); | ||
| 106 | return $resultRedirect->setPath('sales/order_invoice/new/*', ['order_id' => $order->getId()]); | ||
| 107 | } | ||
| 108 | } | ||
| 109 | $invoice = false; | ||
| 110 | if (!$invoice) { | ||
| 111 | throw new LocalizedException(__('We can\'t save the invoice right now.')); | ||
| 112 | } | ||
| 113 | |||
| 114 | if (!$invoice->getTotalQty()) { | ||
| 115 | throw new \Magento\Framework\Exception\LocalizedException( | ||
| 116 | __('You can\'t create an invoice without products.') | ||
| 117 | ); | ||
| 118 | } | ||
| 119 | $this->registry->register('current_invoice', $invoice); | ||
| 120 | if (!empty($data['capture_case'])) { | ||
| 121 | $invoice->setRequestedCaptureCase($data['capture_case']); | ||
| 122 | } | ||
| 123 | |||
| 124 | if (!empty($data['comment_text'])) { | ||
| 125 | $invoice->addComment( | ||
| 126 | $data['comment_text'], | ||
| 127 | isset($data['comment_customer_notify']), | ||
| 128 | isset($data['is_visible_on_front']) | ||
| 129 | ); | ||
| 130 | |||
| 131 | $invoice->setCustomerNote($data['comment_text']); | ||
| 132 | $invoice->setCustomerNoteNotify(isset($data['comment_customer_notify'])); | ||
| 133 | } | ||
| 134 | |||
| 135 | $invoice->register(); | ||
| 136 | |||
| 137 | $invoice->getOrder()->setCustomerNoteNotify(!empty($data['send_email'])); | ||
| 138 | $invoice->getOrder()->setIsInProcess(true); | ||
| 139 | |||
| 140 | $transactionSave = $this->_objectManager->create( | ||
| 141 | \Magento\Framework\DB\Transaction::class | ||
| 142 | )->addObject( | ||
| 143 | $invoice | ||
| 144 | )->addObject( | ||
| 145 | $invoice->getOrder() | ||
| 146 | ); | ||
| 147 | $shipment = false; | ||
| 148 | if (!empty($data['do_shipment']) || (int)$invoice->getOrder()->getForcedShipmentWithInvoice()) { | ||
| 149 | $shipment = $this->_prepareShipment($invoice); | ||
| 150 | if ($shipment) { | ||
| 151 | $transactionSave->addObject($shipment); | ||
| 152 | } | ||
| 153 | } | ||
| 154 | $transactionSave->save(); | ||
| 155 | |||
| 156 | if (!empty($data['do_shipment'])) { | ||
| 157 | $this->messageManager->addSuccess(__('You created the invoice and shipment.')); | ||
| 158 | } else { | ||
| 159 | $this->messageManager->addSuccess(__('The invoice has been created.')); | ||
| 160 | } | ||
| 161 | |||
| 162 | // send invoice/shipment emails | ||
| 163 | try { | ||
| 164 | if (!empty($data['send_email'])) { | ||
| 165 | $this->invoiceSender->send($invoice); | ||
| 166 | } | ||
| 167 | } catch (\Exception $e) { | ||
| 168 | $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e); | ||
| 169 | $this->messageManager->addError(__('We can\'t send the invoice email right now.')); | ||
| 170 | } | ||
| 171 | if ($shipment) { | ||
| 172 | try { | ||
| 173 | if (!empty($data['send_email'])) { | ||
| 174 | $this->shipmentSender->send($shipment); | ||
| 175 | } | ||
| 176 | } catch (\Exception $e) { | ||
| 177 | $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e); | ||
| 178 | $this->messageManager->addError(__('We can\'t send the shipment right now.')); | ||
| 179 | } | ||
| 180 | } | ||
| 181 | $this->_objectManager->get(\Magento\Backend\Model\Session::class)->getCommentText(true); | ||
| 182 | return $resultRedirect->setPath('sales/order/view', ['order_id' => $orderId]); | ||
| 183 | } catch (LocalizedException $e) { | ||
| 184 | $this->messageManager->addError($e->getMessage()); | ||
| 185 | } catch (\Exception $e) { | ||
| 186 | $this->messageManager->addError(__('We can\'t save the invoice right now.')); | ||
| 187 | $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e); | ||
| 188 | } | ||
| 189 | return $resultRedirect->setPath('sales/*/new', ['order_id' => $orderId]); | ||
| 190 | } | ||
| 191 | } |
| 1 | <?php | ||
| 2 | namespace FGCT\OverrideCreateInvoice\Observer; | ||
| 3 | use Magento\Framework\Event\ObserverInterface; | ||
| 4 | |||
| 5 | class OrderInvoiceSaveAfter implements ObserverInterface { | ||
| 6 | public $_coreRegistry; | ||
| 7 | protected $_helper; | ||
| 8 | public function __construct( | ||
| 9 | \Magento\Framework\Registry $coreRegistry | ||
| 10 | ) { | ||
| 11 | $this->_coreRegistry = $coreRegistry; | ||
| 12 | } | ||
| 13 | public function execute(\Magento\Framework\Event\Observer $observer) { | ||
| 14 | $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); | ||
| 15 | $productsId = $this->_coreRegistry->registry('fgct_overridecreateinvoice_products_out_stock') ?: []; | ||
| 16 | if(count($productsId)) { | ||
| 17 | $msg = "Some of the products are out of stock"; | ||
| 18 | //throw new \Exception($msg); | ||
| 19 | exit('OrderInvoiceSaveAfter'); | ||
| 20 | } | ||
| 21 | } | ||
| 22 | } |
| 1 | <?php | ||
| 2 | namespace FGCT\OverrideCreateInvoice\Observer; | ||
| 3 | |||
| 4 | use Magento\Framework\Event\Observer; | ||
| 5 | use Magento\Framework\Event\ObserverInterface; | ||
| 6 | |||
| 7 | use Magento\Framework\Controller\ResultFactory; | ||
| 8 | |||
| 9 | class OrderInvoiceSaveBefore implements ObserverInterface { | ||
| 10 | public $_coreRegistry; | ||
| 11 | protected $_helper; | ||
| 12 | public function __construct( | ||
| 13 | \Magento\Framework\Registry $coreRegistry, | ||
| 14 | \Magento\Framework\App\RequestInterface $request, | ||
| 15 | \Magento\Framework\App\ResponseFactory $responseFactory, | ||
| 16 | \Magento\Framework\Message\ManagerInterface $messageManager, | ||
| 17 | \Magento\CatalogInventory\Model\Stock\StockItemRepository $stockItemRepository, | ||
| 18 | \Magento\Framework\UrlInterface $url | ||
| 19 | ) { | ||
| 20 | $this->_coreRegistry = $coreRegistry; | ||
| 21 | $this->_request = $request; | ||
| 22 | $this->responseFactory = $responseFactory; | ||
| 23 | $this->url = $url; | ||
| 24 | $this->_messageManager = $messageManager; | ||
| 25 | $this->_stockItemRepository = $stockItemRepository; | ||
| 26 | } | ||
| 27 | public function execute(\Magento\Framework\Event\Observer $observer) { | ||
| 28 | $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); | ||
| 29 | $orderId = $this->_request->getParam('order_id'); | ||
| 30 | $invoice = $observer->getEvent()->getInvoice(); | ||
| 31 | // $order = $invoice->getOrder(); | ||
| 32 | $productManager = $objectManager->create('Magento\Catalog\Model\Product'); | ||
| 33 | $productsOutStock = $productsNotEnoughQty = []; | ||
| 34 | foreach ($invoice->getAllItems() as $item) { | ||
| 35 | $name = $item->getName(); | ||
| 36 | // $type = $item->getSku(); | ||
| 37 | $productId = $item->getProductId(); | ||
| 38 | $qty = $item->getQty(); | ||
| 39 | // $product = $productManager->load($productId); | ||
| 40 | $_productStock = $this->_stockItemRepository->get($productId); | ||
| 41 | // $_productStock->getQty(); | ||
| 42 | if(!$_productStock->getIsInStock()) { | ||
| 43 | $productsOutStock[$productId] = $name; | ||
| 44 | } | ||
| 45 | if($qty > $_productStock->getQty()) { | ||
| 46 | $productsNotEnoughQty[$productId] = $_productStock->getQty(); | ||
| 47 | } | ||
| 48 | } | ||
| 49 | if(count($productsOutStock)) { | ||
| 50 | $productsId = array_keys($productsOutStock); | ||
| 51 | $this->_coreRegistry->register('fgct_overridecreateinvoice_products_out_stock', $productsId, true); | ||
| 52 | $this->_messageManager->addError(__('Some of the products are out of stock')); | ||
| 53 | |||
| 54 | $redirectionUrl = $this->url->getUrl('sales/order_invoice/new/*/product/*/', ['order_id' => $orderId, 'productsId' => implode(',', $productsId)]); | ||
| 55 | $this->responseFactory->create()->setRedirect($redirectionUrl)->sendResponse(); | ||
| 56 | return $this; | ||
| 57 | } | ||
| 58 | } | ||
| 59 | } |
| 1 | <?xml version="1.0"?> | ||
| 2 | <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> | ||
| 3 | <event name="sales_order_invoice_save_before"> | ||
| 4 | <observer name="fgc_sales_order_invoice_save_before" instance="FGCT\OverrideCreateInvoice\Observer\OrderInvoiceSaveBefore" /> | ||
| 5 | </event> | ||
| 6 | <event name="sales_order_invoice_save_after"> | ||
| 7 | <observer name="fgc_sales_order_invoice_save_after" instance="FGCT\OverrideCreateInvoice\Observer\OrderInvoiceSaveAfter" /> | ||
| 8 | </event> | ||
| 9 | </config> |
| 1 | <?xml version="1.0"?> | 1 | <?xml version="1.0"?> |
| 2 | <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | 2 | <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> |
| 3 | <preference for="Magento\Sales\Block\Adminhtml\Items\Renderer\DefaultRenderer" type="FGCT\OverrideCreateInvoice\Block\Adminhtml\Items\Renderer\DefaultRenderer" /> | 3 | <preference for="Magento\Sales\Block\Adminhtml\Items\Renderer\DefaultRenderer" type="FGCT\OverrideCreateInvoice\Block\Adminhtml\Items\Renderer\DefaultRenderer" /> |
| 4 | <preference for="Magento\Sales\Controller\Adminhtml\Order\Invoice\Save" type="FGCT\OverrideCreateInvoice\Controller\Adminhtml\Order\Invoice\Save" /> | ||
| 4 | </config> | 5 | </config> | ... | ... |
-
Please register or sign in to post a comment