Skip to content

Commit c271c24

Browse files
committed
updated module files
0 parents  commit c271c24

File tree

15 files changed

+1437
-0
lines changed

15 files changed

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

0 commit comments

Comments
 (0)