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
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Shipping\Controller\Adminhtml\Order\Shipment;
use Magento\Backend\App\Action;
class RemoveTrack extends \Magento\Backend\App\Action
{
/**
* Authorization level of a basic admin session
*
* @see _isAllowed()
*/
const ADMIN_RESOURCE = 'Magento_Sales::shipment';
/**
* @var \Magento\Shipping\Controller\Adminhtml\Order\ShipmentLoader
*/
protected $shipmentLoader;
/**
* @param Action\Context $context
* @param \Magento\Shipping\Controller\Adminhtml\Order\ShipmentLoader $shipmentLoader
*/
public function __construct(
Action\Context $context,
\Magento\Shipping\Controller\Adminhtml\Order\ShipmentLoader $shipmentLoader
) {
$this->shipmentLoader = $shipmentLoader;
parent::__construct($context);
}
/**
* Remove tracking number from shipment
*
* @return void
*/
public function execute()
{
$trackId = $this->getRequest()->getParam('track_id');
/** @var \Magento\Sales\Model\Order\Shipment\Track $track */
$track = $this->_objectManager->create(\Magento\Sales\Model\Order\Shipment\Track::class)->load($trackId);
if ($track->getId()) {
try {
$this->shipmentLoader->setOrderId($this->getRequest()->getParam('order_id'));
$this->shipmentLoader->setShipmentId($this->getRequest()->getParam('shipment_id'));
$this->shipmentLoader->setShipment($this->getRequest()->getParam('shipment'));
$this->shipmentLoader->setTracking($this->getRequest()->getParam('tracking'));
$shipment = $this->shipmentLoader->load();
if ($shipment) {
$track->delete();
$this->_view->loadLayout();
$this->_view->getPage()->getConfig()->getTitle()->prepend(__('Shipments'));
$response = $this->_view->getLayout()->getBlock('shipment_tracking')->toHtml();
} else {
$response = [
'error' => true,
'message' => __('We can\'t initialize shipment for delete tracking number.'),
];
}
} catch (\Exception $e) {
$response = ['error' => true, 'message' => __('We can\'t delete tracking number.')];
}
} else {
$response = [
'error' => true,
'message' => __('We can\'t load track with retrieving identifier right now.')
];
}
if (is_array($response)) {
$response = $this->_objectManager->get(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($response);
$this->getResponse()->representJson($response);
} else {
$this->getResponse()->setBody($response);
}
}
}