Skip to content

Commit 4ffcd9f

Browse files
committed
SE upload delete order module all files.
1 parent db1bc9b commit 4ffcd9f

File tree

10 files changed

+570
-0
lines changed

10 files changed

+570
-0
lines changed
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
<?php
2+
/**
3+
* Copyright © 2018 SolutionExcel. All rights reserved.
4+
* See COPYING.txt for license details.
5+
**/
6+
7+
namespace Solutionexcel\DeleteOrders\Controller\Adminhtml\Order;
8+
9+
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
10+
use Magento\Backend\App\Action\Context;
11+
use Magento\Framework\App\ResourceConnection;
12+
use Magento\Framework\App\DeploymentConfig;
13+
use Magento\Framework\Config\ConfigOptionsListConstants;
14+
15+
class DeleteOrder extends \Magento\Backend\App\Action
16+
{
17+
public $_resource;
18+
private $deploymentConfig;
19+
protected $orderRepository;
20+
21+
/**
22+
* @param \Magento\Backend\App\Action\Context $context
23+
* @param \Magento\Framework\Registry $coreRegistry
24+
*/
25+
public function __construct(
26+
Context $context,
27+
ResourceConnection $resource,
28+
DeploymentConfig $deploymentConfig,
29+
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository
30+
){
31+
32+
$this->_resource = $resource;
33+
parent::__construct($context);
34+
$this->deploymentConfig = $deploymentConfig;
35+
$this->orderRepository = $orderRepository;
36+
}
37+
38+
public function getOrder($id)
39+
{
40+
return $this->orderRepository->get($id);
41+
}
42+
43+
public function execute()
44+
{
45+
$orderId = $this->getRequest()->getParam('id');
46+
if($orderId){
47+
try {
48+
$order = $this->getOrder($orderId);
49+
$countCancelOrder = 0;
50+
$connection = $this->_resource->getConnection(\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION);
51+
$showTables = $connection->fetchCol('show tables');
52+
53+
$tblPrefix = (string)$this->deploymentConfig->get(
54+
ConfigOptionsListConstants::CONFIG_PATH_DB_PREFIX
55+
);
56+
57+
$tblSalesOrder = $connection->getTableName($tblPrefix . 'sales_order');
58+
$tblSalesCreditmemoComment = $connection->getTableName($tblPrefix . 'sales_creditmemo_comment');
59+
$tblSalesCreditmemoItem = $connection->getTableName($tblPrefix . 'sales_creditmemo_item');
60+
$tblSalesCreditmemo = $connection->getTableName($tblPrefix . 'sales_creditmemo');
61+
$tblSalesCreditmemoGrid = $connection->getTableName($tblPrefix . 'sales_creditmemo_grid');
62+
$tblSalesInvoiceComment = $connection->getTableName($tblPrefix . 'sales_invoice_comment');
63+
$tblSalesInvoiceItem = $connection->getTableName($tblPrefix . 'sales_invoice_item');
64+
$tblSalesInvoice = $connection->getTableName($tblPrefix . 'sales_invoice');
65+
$tblSalesInvoiceGrid = $connection->getTableName($tblPrefix . 'sales_invoice_grid');
66+
$tblQuoteAddressItem = $connection->getTableName($tblPrefix . 'quote_address_item');
67+
$tblQuoteItemOption = $connection->getTableName($tblPrefix . 'quote_item_option');
68+
$tblQuote = $connection->getTableName($tblPrefix . 'quote');
69+
$tblQuoteAddress = $connection->getTableName($tblPrefix . 'quote_address');
70+
$tblQuoteItem = $connection->getTableName($tblPrefix . 'quote_item');
71+
$tblQuotePayment = $connection->getTableName($tblPrefix . 'quote_payment');
72+
$tblQuoteShippingRate = $connection->getTableName($tblPrefix . 'quote_shipping_rate');
73+
$tblQuoteIDMask = $connection->getTableName($tblPrefix . 'quote_id_mask');
74+
$tblSalesShipmentComment = $connection->getTableName($tblPrefix . 'sales_shipment_comment');
75+
$tblSalesShipmentItem = $connection->getTableName($tblPrefix . 'sales_shipment_item');
76+
$tblSalesShipmentTrack = $connection->getTableName($tblPrefix . 'sales_shipment_track');
77+
$tblSalesShipment = $connection->getTableName($tblPrefix . 'sales_shipment');
78+
$tblSalesShipmentGrid = $connection->getTableName($tblPrefix . 'sales_shipment_grid');
79+
$tblSalesOrderAddress = $connection->getTableName($tblPrefix . 'sales_order_address');
80+
$tblSalesOrderItem = $connection->getTableName($tblPrefix . 'sales_order_item');
81+
$tblSalesOrderPayment = $connection->getTableName($tblPrefix . 'sales_order_payment');
82+
$tblSalesOrderStatusHistory = $connection->getTableName($tblPrefix . 'sales_order_status_history');
83+
$tblSalesOrderGrid = $connection->getTableName($tblPrefix . 'sales_order_grid');
84+
$tblLogQuote = $connection->getTableName($tblPrefix . 'log_quote');
85+
$showTablesLog = $connection->fetchCol('SHOW TABLES LIKE ?', '%'.$tblLogQuote);
86+
$tblSalesOrderTax = $connection->getTableName($tblPrefix . 'sales_order_tax');
87+
88+
if ($order->getIncrementId()) {
89+
$incId = $order->getIncrementId();
90+
if (in_array($tblSalesOrder, $showTables)) {
91+
$result1 = $connection->fetchAll('SELECT quote_id FROM `'.$tblSalesOrder.'` WHERE entity_id='.$orderId);
92+
$quoteId = (int) $result1[0]['quote_id'];
93+
}
94+
$connection->rawQuery('SET FOREIGN_KEY_CHECKS=1');
95+
if (in_array($tblSalesCreditmemoComment, $showTables)) {
96+
$connection->rawQuery('DELETE FROM `'.$tblSalesCreditmemoComment.'` WHERE parent_id IN (SELECT entity_id FROM `'.$tblSalesCreditmemo.'` WHERE order_id='.$orderId.')');
97+
}
98+
if (in_array('sales__creditmemo_item', $showTables)) {
99+
$connection->rawQuery('DELETE FROM `'.$tblSalesCreditmemoItem.'` WHERE parent_id IN (SELECT entity_id FROM `'.$tblSalesCreditmemo.'` WHERE order_id='.$orderId.')');
100+
}
101+
if (in_array($tblSalesCreditmemo, $showTables)) {
102+
$connection->rawQuery('DELETE FROM `'.$tblSalesCreditmemo.'` WHERE order_id='.$orderId);
103+
}
104+
if (in_array($tblSalesCreditmemoGrid, $showTables)) {
105+
$connection->rawQuery('DELETE FROM `'.$tblSalesCreditmemoGrid.'` WHERE order_id='.$orderId);
106+
}
107+
if (in_array($tblSalesInvoiceComment, $showTables)) {
108+
$connection->rawQuery('DELETE FROM `'.$tblSalesInvoiceComment.'` WHERE parent_id IN (SELECT entity_id FROM `'.$tblSalesInvoice.'` WHERE order_id='.$orderId.')');
109+
}
110+
if (in_array($tblSalesInvoiceItem, $showTables)) {
111+
$connection->rawQuery('DELETE FROM `'.$tblSalesInvoiceItem.'` WHERE parent_id IN (SELECT entity_id FROM `'.$tblSalesInvoice.'` WHERE order_id='.$orderId.')');
112+
}
113+
if (in_array($tblSalesInvoice, $showTables)) {
114+
$connection->rawQuery('DELETE FROM `'.$tblSalesInvoice.'` WHERE order_id='.$orderId);
115+
}
116+
if (in_array($tblSalesInvoiceGrid, $showTables)) {
117+
$connection->rawQuery('DELETE FROM `'.$tblSalesInvoiceGrid.'` WHERE order_id='.$orderId);
118+
}
119+
if ($quoteId) {
120+
if (in_array($tblQuoteAddressItem, $showTables)) {
121+
$connection->rawQuery('DELETE FROM `'.$tblQuoteAddressItem.'` WHERE parent_item_id IN (SELECT address_id FROM `'.$tblQuoteAddress.'` WHERE quote_id='.$quoteId.')');
122+
}
123+
if (in_array($tblQuoteShippingRate, $showTables)) {
124+
$connection->rawQuery('DELETE FROM `'.$tblQuoteShippingRate.'` WHERE address_id IN (SELECT address_id FROM `'.$tblQuoteAddress.'` WHERE quote_id='.$quoteId.')');
125+
}
126+
if (in_array($tblQuoteIDMask, $showTables)) {
127+
$connection->rawQuery('DELETE FROM `'.$tblQuoteIDMask.'` where quote_id='.$quoteId);
128+
}
129+
if (in_array($tblQuoteItemOption, $showTables)) {
130+
$connection->rawQuery('DELETE FROM `'.$tblQuoteItemOption.'` WHERE item_id IN (SELECT item_id FROM `'.$tblQuoteItem.'` WHERE quote_id='.$quoteId.')');
131+
}
132+
if (in_array($tblQuote, $showTables)) {
133+
$connection->rawQuery('DELETE FROM `'.$tblQuote.'` WHERE entity_id='.$quoteId);
134+
}
135+
if (in_array($tblQuoteAddress, $showTables)) {
136+
$connection->rawQuery('DELETE FROM `'.$tblQuoteAddress.'` WHERE quote_id='.$quoteId);
137+
}
138+
if (in_array($tblQuoteItem, $showTables)) {
139+
$connection->rawQuery('DELETE FROM `'.$tblQuoteItem.'` WHERE quote_id='.$quoteId);
140+
}
141+
if (in_array('sales__quotePayment', $showTables)) {
142+
$connection->rawQuery('DELETE FROM `'.$tblQuotePayment.'` WHERE quote_id='.$quoteId);
143+
}
144+
}
145+
if (in_array($tblSalesShipmentComment, $showTables)) {
146+
$connection->rawQuery('DELETE FROM `'.$tblSalesShipmentComment.'` WHERE parent_id IN (SELECT entity_id FROM `'.$tblSalesShipment.'` WHERE order_id='.$orderId.')');
147+
}
148+
if (in_array($tblSalesShipmentItem, $showTables)) {
149+
$connection->rawQuery('DELETE FROM `'.$tblSalesShipmentItem.'` WHERE parent_id IN (SELECT entity_id FROM `'.$tblSalesShipment.'` WHERE order_id='.$orderId.')');
150+
}
151+
if (in_array($tblSalesShipmentTrack, $showTables)) {
152+
$connection->rawQuery('DELETE FROM `'.$tblSalesShipmentTrack.'` WHERE order_id IN (SELECT entity_id FROM `'.$tblSalesShipment.'` WHERE parent_id='.$orderId.')');
153+
}
154+
if (in_array($tblSalesShipment, $showTables)) {
155+
$connection->rawQuery('DELETE FROM `'.$tblSalesShipment.'` WHERE order_id='.$orderId);
156+
}
157+
if (in_array($tblSalesShipmentGrid, $showTables)) {
158+
$connection->rawQuery('DELETE FROM `'.$tblSalesShipmentGrid.'` WHERE order_id='.$orderId);
159+
}
160+
if (in_array($tblSalesOrder, $showTables)) {
161+
$connection->rawQuery('DELETE FROM `'.$tblSalesOrder.'` WHERE entity_id='.$orderId);
162+
}
163+
if (in_array($tblSalesOrderAddress, $showTables)) {
164+
$connection->rawQuery('DELETE FROM `'.$tblSalesOrderAddress.'` WHERE parent_id='.$orderId);
165+
}
166+
if (in_array($tblSalesOrderItem, $showTables)) {
167+
$connection->rawQuery('DELETE FROM `'.$tblSalesOrderItem.'` WHERE order_id='.$orderId);
168+
}
169+
if (in_array($tblSalesOrderPayment, $showTables)) {
170+
$connection->rawQuery('DELETE FROM `'.$tblSalesOrderPayment.'` WHERE parent_id='.$orderId);
171+
}
172+
if (in_array($tblSalesOrderStatusHistory, $showTables)) {
173+
$connection->rawQuery('DELETE FROM `'.$tblSalesOrderStatusHistory.'` WHERE parent_id='.$orderId);
174+
}
175+
if ($incId && in_array($tblSalesOrderGrid, $showTables)) {
176+
$connection->rawQuery('DELETE FROM `'.$tblSalesOrderGrid.'` WHERE increment_id='.$incId);
177+
}
178+
if (in_array($tblSalesOrderTax, $showTables)) {
179+
$connection->rawQuery('DELETE FROM `'.$tblSalesOrderTax.'` WHERE order_id='.$orderId);
180+
}
181+
if ($quoteId && $showTablesLog) {
182+
$connection->rawQuery('DELETE FROM `'.$tblLogQuote.'` WHERE quote_id='.$quoteId);
183+
}
184+
$connection->rawQuery('SET FOREIGN_KEY_CHECKS=1');
185+
}
186+
187+
$this->messageManager->addSuccess(__('Successfully deleted 1 order.'));
188+
189+
} catch (\Exception $e) {
190+
$this->messageManager->addError($e->getMessage());
191+
}
192+
193+
} else {
194+
$this->messageManager->addError(__('Unable to delete this order.'));
195+
}
196+
197+
$resultRedirect = $this->resultRedirectFactory->create();
198+
$resultRedirect->setPath('sales/*/');
199+
return $resultRedirect;
200+
201+
}
202+
203+
}

0 commit comments

Comments
 (0)