Save.php
7.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
namespace FGCT\OverrideCreateInvoice\Controller\Adminhtml\Order\Invoice;
use Magento\Backend\App\Action;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Registry;
use Magento\Sales\Model\Order\Email\Sender\InvoiceSender;
use Magento\Sales\Model\Order\Email\Sender\ShipmentSender;
use Magento\Sales\Model\Order\ShipmentFactory;
use Magento\Sales\Model\Order\Invoice;
use Magento\Sales\Model\Service\InvoiceService;
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Save extends \Magento\Sales\Controller\Adminhtml\Order\Invoice\Save {
/**
* @var InvoiceSender
*/
protected $invoiceSender;
/**
* @var ShipmentSender
*/
protected $shipmentSender;
/**
* @var ShipmentFactory
*/
protected $shipmentFactory;
/**
* @var Registry
*/
protected $registry;
/**
* @var InvoiceService
*/
private $invoiceService;
/**
* @param Action\Context $context
* @param Registry $registry
* @param InvoiceSender $invoiceSender
* @param ShipmentSender $shipmentSender
* @param ShipmentFactory $shipmentFactory
* @param InvoiceService $invoiceService
*/
public function __construct(
Action\Context $context,
Registry $registry,
InvoiceSender $invoiceSender,
ShipmentSender $shipmentSender,
ShipmentFactory $shipmentFactory,
InvoiceService $invoiceService,
\Magento\Catalog\Model\Product $product,
\Magento\CatalogInventory\Model\Stock\StockItemRepository $stockItemRepository
) {
$this->registry = $registry;
$this->invoiceSender = $invoiceSender;
$this->shipmentSender = $shipmentSender;
$this->shipmentFactory = $shipmentFactory;
$this->invoiceService = $invoiceService;
$this->_product = $product;
$this->_stockItemRepository = $stockItemRepository;
parent::__construct($context, $registry, $invoiceSender, $shipmentSender, $shipmentFactory, $invoiceService);
}
public function execute() {
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
$formKeyIsValid = $this->_formKeyValidator->validate($this->getRequest());
$isPost = $this->getRequest()->isPost();
if (!$formKeyIsValid || !$isPost) {
$this->messageManager->addError(__('We can\'t save the invoice right now.'));
return $resultRedirect->setPath('sales/order/index');
}
$data = $this->getRequest()->getPost('invoice');
$orderId = $this->getRequest()->getParam('order_id');
if (!empty($data['comment_text'])) {
$this->_objectManager->get(\Magento\Backend\Model\Session::class)->setCommentText($data['comment_text']);
}
try {
$invoiceData = $this->getRequest()->getParam('invoice', []);
$invoiceItems = isset($invoiceData['items']) ? $invoiceData['items'] : [];
/** @var \Magento\Sales\Model\Order $order */
$order = $this->_objectManager->create(\Magento\Sales\Model\Order::class)->load($orderId);
if (!$order->getId()) {
throw new \Magento\Framework\Exception\LocalizedException(__('The order no longer exists.'));
}
if (!$order->canInvoice()) {
throw new \Magento\Framework\Exception\LocalizedException(
__('The order does not allow an invoice to be created.')
);
}
$invoice = $this->invoiceService->prepareInvoice($order, $invoiceItems);
foreach ($invoice->getAllItems() as $item) {
$sku = $item->getSku();
//$productId = $item->getProductId(); // parent
$productId = $this->_product->getIdBySku($sku);
$_productStock = $this->_stockItemRepository->get($productId);
$qtySelected = intval($item->getQty());
$qtyProduct = $_productStock->getQty();
if(!$_productStock->getIsInStock() || ($qtySelected > $qtyProduct)) {
//$this->messageManager->addError(__('Some of the products are out of stock!'));
$this->messageManager->addError(__('Some products do not have enough quantity to create invoice!'));
return $resultRedirect->setPath('sales/order_invoice/new/*', ['order_id' => $order->getId()]);
}
}
//$invoice = false;
if (!$invoice) {
throw new LocalizedException(__('We can\'t save the invoice right now.'));
}
if (!$invoice->getTotalQty()) {
throw new \Magento\Framework\Exception\LocalizedException(
__('You can\'t create an invoice without products.')
);
}
$this->registry->register('current_invoice', $invoice);
if (!empty($data['capture_case'])) {
$invoice->setRequestedCaptureCase($data['capture_case']);
}
if (!empty($data['comment_text'])) {
$invoice->addComment(
$data['comment_text'],
isset($data['comment_customer_notify']),
isset($data['is_visible_on_front'])
);
$invoice->setCustomerNote($data['comment_text']);
$invoice->setCustomerNoteNotify(isset($data['comment_customer_notify']));
}
$invoice->register();
$invoice->getOrder()->setCustomerNoteNotify(!empty($data['send_email']));
$invoice->getOrder()->setIsInProcess(true);
$transactionSave = $this->_objectManager->create(
\Magento\Framework\DB\Transaction::class
)->addObject(
$invoice
)->addObject(
$invoice->getOrder()
);
$shipment = false;
if (!empty($data['do_shipment']) || (int)$invoice->getOrder()->getForcedShipmentWithInvoice()) {
$shipment = $this->_prepareShipment($invoice);
if ($shipment) {
$transactionSave->addObject($shipment);
}
}
$transactionSave->save();
if (!empty($data['do_shipment'])) {
$this->messageManager->addSuccess(__('You created the invoice and shipment.'));
} else {
$this->messageManager->addSuccess(__('The invoice has been created.'));
}
// send invoice/shipment emails
try {
if (!empty($data['send_email'])) {
$this->invoiceSender->send($invoice);
}
} catch (\Exception $e) {
$this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
$this->messageManager->addError(__('We can\'t send the invoice email right now.'));
}
if ($shipment) {
try {
if (!empty($data['send_email'])) {
$this->shipmentSender->send($shipment);
}
} catch (\Exception $e) {
$this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
$this->messageManager->addError(__('We can\'t send the shipment right now.'));
}
}
$this->_objectManager->get(\Magento\Backend\Model\Session::class)->getCommentText(true);
return $resultRedirect->setPath('sales/order/view', ['order_id' => $orderId]);
} catch (LocalizedException $e) {
$this->messageManager->addError($e->getMessage());
} catch (\Exception $e) {
$this->messageManager->addError(__('We can\'t save the invoice right now.'));
$this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
}
return $resultRedirect->setPath('sales/*/new', ['order_id' => $orderId]);
}
}