Override Create Invoice module - version 1.0.0
0 parents
Showing
10 changed files
with
320 additions
and
0 deletions
.gitignore
0 → 100644
| 1 | <?php | ||
| 2 | /** | ||
| 3 | * Copyright © Magento, Inc. All rights reserved. | ||
| 4 | * See COPYING.txt for license details. | ||
| 5 | */ | ||
| 6 | |||
| 7 | // @codingStandardsIgnoreFile | ||
| 8 | |||
| 9 | namespace FGCT\OverrideCreateInvoice\Block\Adminhtml\Items\Renderer; | ||
| 10 | |||
| 11 | use Magento\Sales\Model\Order\CreditMemo\Item as CreditMemoItem; | ||
| 12 | use Magento\Sales\Model\Order\Invoice\Item as InvoiceItem; | ||
| 13 | use Magento\Sales\Model\Order\Item as OrderItem; | ||
| 14 | |||
| 15 | /** | ||
| 16 | * Order item render block | ||
| 17 | * | ||
| 18 | * @api | ||
| 19 | * @since 100.0.2 | ||
| 20 | */ | ||
| 21 | class DefaultRenderer extends \Magento\Sales\Block\Adminhtml\Items\Renderer\DefaultRenderer { | ||
| 22 | public function __construct( | ||
| 23 | \Magento\Backend\Block\Template\Context $context, | ||
| 24 | \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry, | ||
| 25 | \Magento\CatalogInventory\Api\StockConfigurationInterface $stockConfiguration, | ||
| 26 | \Magento\CatalogInventory\Model\Stock\StockItemRepository $stockItemRepository, | ||
| 27 | \Magento\Framework\Registry $registry, | ||
| 28 | array $data = [] | ||
| 29 | ) { | ||
| 30 | $this->_coreRegistry = $registry; | ||
| 31 | $this->_stockItemRepository = $stockItemRepository; | ||
| 32 | parent::__construct($context, $stockRegistry, $stockConfiguration, $registry, $data); | ||
| 33 | } | ||
| 34 | |||
| 35 | public function getErrorOutStock($productId) { | ||
| 36 | $_productStock = $this->_stockItemRepository->get($productId); | ||
| 37 | $html = ''; | ||
| 38 | if(!$_productStock->getIsInStock()) { | ||
| 39 | $html = '<div class="message message-error error"><div data-ui-id="messages-message-error">This product is out of stock</div></div>'; | ||
| 40 | } | ||
| 41 | return $html; | ||
| 42 | } | ||
| 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 | <?xml version="1.0"?> | ||
| 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" /> | ||
| 4 | <preference for="Magento\Sales\Controller\Adminhtml\Order\Invoice\Save" type="FGCT\OverrideCreateInvoice\Controller\Adminhtml\Order\Invoice\Save" /> | ||
| 5 | </config> |
| 1 | <?xml version="1.0"?> | ||
| 2 | <!-- | ||
| 3 | /** | ||
| 4 | * Copyright © Magento, Inc. All rights reserved. | ||
| 5 | * See COPYING.txt for license details. | ||
| 6 | */ | ||
| 7 | --> | ||
| 8 | <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> | ||
| 9 | <update handle="sales_invoice_item_price"/> | ||
| 10 | <body> | ||
| 11 | <!-- <referenceBlock name="order_items.default" remove="true"/> --> | ||
| 12 | <!-- <referenceContainer name="content"> | ||
| 13 | <block class="FGCT\OverrideCreateInvoice\Block\Adminhtml\Items\Renderer\FgcDefaultRenderer" name="order_items.default" template="FGCT_OverrideCreateInvoice::order/invoice/create/items/renderer/default.phtml"/> | ||
| 14 | </referenceContainer> --> | ||
| 15 | <referenceBlock name="order_items.default"> | ||
| 16 | <action method="setTemplate"> | ||
| 17 | <argument name="template" translate="true" xsi:type="string">FGCT_OverrideCreateInvoice::order/invoice/create/items/renderer/default.phtml</argument> | ||
| 18 | </action> | ||
| 19 | </referenceBlock> | ||
| 20 | </body> | ||
| 21 | </page> |
| 1 | <?php | ||
| 2 | /** | ||
| 3 | * Copyright © Magento, Inc. All rights reserved. | ||
| 4 | * See COPYING.txt for license details. | ||
| 5 | */ | ||
| 6 | |||
| 7 | // @codingStandardsIgnoreFile | ||
| 8 | |||
| 9 | ?> | ||
| 10 | <?php /** @var $block \Magento\Sales\Block\Adminhtml\Items\Renderer\DefaultRenderer */ ?> | ||
| 11 | <?php $_item = $block->getItem() ?> | ||
| 12 | <?php $block->setPriceDataObject($_item)?> | ||
| 13 | <td class="col-product"><?= $block->getColumnHtml($_item, 'name') ?> | ||
| 14 | <?= $block->getErrorOutStock($_item->getProductId()) ?> | ||
| 15 | </td> | ||
| 16 | <td class="col-price"> | ||
| 17 | <?= $block->getColumnHtml($_item, 'price') ?> | ||
| 18 | </td> | ||
| 19 | <td class="col-qty"><?= $block->getColumnHtml($_item, 'qty') ?></td> | ||
| 20 | <td class="col-qty-invoice"> | ||
| 21 | <?php if ($block->canEditQty()) : ?> | ||
| 22 | <input type="text" class="input-text admin__control-text qty-input" | ||
| 23 | name="invoice[items][<?= /* @escapeNotVerified */ $_item->getOrderItemId() ?>]" | ||
| 24 | value="<?= /* @escapeNotVerified */ $_item->getQty()*1 ?>"/> | ||
| 25 | <?php else : ?> | ||
| 26 | <?= /* @escapeNotVerified */ $_item->getQty()*1 ?> | ||
| 27 | <?php endif; ?> | ||
| 28 | </td> | ||
| 29 | <td class="col-subtotal"> | ||
| 30 | <?= $block->getColumnHtml($_item, 'subtotal') ?> | ||
| 31 | </td> | ||
| 32 | <td class="col-tax"><?= /* @escapeNotVerified */ $block->displayPriceAttribute('tax_amount') ?></td> | ||
| 33 | <td class="col-discount"><?= /* @escapeNotVerified */ $block->displayPriceAttribute('discount_amount') ?></td> | ||
| 34 | <td class="col-total last"> | ||
| 35 | <?= $block->getColumnHtml($_item, 'total') ?> | ||
| 36 | </td> |
-
Please register or sign in to post a comment