Skip to content

Commit 9300183

Browse files
Add possibility To Delete Stocks in Admin Panel
1 parent 2c454bc commit 9300183

File tree

8 files changed

+334
-2
lines changed

8 files changed

+334
-2
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Inventory\Controller\Adminhtml\Stock;
8+
9+
use Magento\Backend\App\Action;
10+
use Magento\Backend\App\Action\Context;
11+
use Magento\Framework\Exception\CouldNotDeleteException;
12+
use Magento\Framework\Exception\NoSuchEntityException;
13+
use Magento\InventoryApi\Api\StockRepositoryInterface;
14+
use Magento\InventoryApi\Api\Data\StockInterface;
15+
16+
/**
17+
* Delete Controller
18+
*/
19+
class Delete extends Action
20+
{
21+
/**
22+
* @see _isAllowed()
23+
*/
24+
const ADMIN_RESOURCE = Index::ADMIN_RESOURCE;
25+
26+
/**
27+
* @var StockRepositoryInterface
28+
*/
29+
private $stockRepository;
30+
31+
/**
32+
* @param Context $context
33+
* @param StockRepositoryInterface $stockRepository
34+
*/
35+
public function __construct(
36+
Context $context,
37+
StockRepositoryInterface $stockRepository
38+
) {
39+
parent::__construct($context);
40+
$this->stockRepository = $stockRepository;
41+
}
42+
43+
/**
44+
* @inheritdoc
45+
*/
46+
public function execute()
47+
{
48+
$resultRedirect = $this->resultRedirectFactory->create();
49+
$stockId = $this->getRequest()->getParam(StockInterface::STOCK_ID);
50+
if ($stockId === null) {
51+
$this->messageManager->addErrorMessage(__('Wrong request.'));
52+
53+
return $resultRedirect->setPath('*/*');
54+
}
55+
try {
56+
$this->stockRepository->deleteById($stockId);
57+
$this->messageManager->addSuccessMessage(__('The Stock has been deleted.'));
58+
$resultRedirect->setPath('*/*');
59+
} catch (NoSuchEntityException $e) {
60+
$this->messageManager->addErrorMessage(__('Stock with id "%1" does not exist.', $stockId));
61+
$resultRedirect->setPath('*/*');
62+
} catch (CouldNotDeleteException $e) {
63+
$this->messageManager->addErrorMessage($e->getMessage());
64+
$resultRedirect->setPath('*/*/edit', [
65+
StockInterface::STOCK_ID => $stockId,
66+
'_current' => true,
67+
]);
68+
}
69+
70+
return $resultRedirect;
71+
}
72+
}

app/code/Magento/Inventory/Controller/Adminhtml/Stock/Edit.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ public function execute()
4949
$stockId = $this->getRequest()->getParam(StockInterface::STOCK_ID);
5050
try {
5151
$stock = $this->stockRepository->get($stockId);
52-
5352
/** @var Page $result */
5453
$result = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
5554
$result->setActiveMenu('Magento_Inventory::stock')
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Inventory\Controller\Adminhtml\Stock;
8+
9+
use Magento\Backend\App\Action;
10+
use Magento\Backend\App\Action\Context;
11+
use Magento\Inventory\Ui\Component\MassAction\Filter;
12+
use Magento\Framework\Exception\CouldNotDeleteException;
13+
use Magento\InventoryApi\Api\StockRepositoryInterface;
14+
15+
/**
16+
* Delete Controller
17+
*/
18+
class MassDelete extends Action
19+
{
20+
/**
21+
* @see _isAllowed()
22+
*/
23+
const ADMIN_RESOURCE = Index::ADMIN_RESOURCE;
24+
25+
/**
26+
* @var StockRepositoryInterface
27+
*/
28+
private $stockRepository;
29+
30+
/**
31+
* @var Filter
32+
*/
33+
private $massActionFilter;
34+
35+
/**
36+
* @param Context $context
37+
* @param StockRepositoryInterface $stockRepository
38+
* @param Filter $massActionFilter
39+
*/
40+
public function __construct(
41+
Context $context,
42+
StockRepositoryInterface $stockRepository,
43+
Filter $massActionFilter
44+
) {
45+
parent::__construct($context);
46+
$this->stockRepository = $stockRepository;
47+
$this->massActionFilter = $massActionFilter;
48+
}
49+
50+
/**
51+
* @inheritdoc
52+
*/
53+
public function execute()
54+
{
55+
if ($this->getRequest()->isPost() !== true) {
56+
$this->messageManager->addErrorMessage(__('Wrong request.'));
57+
58+
return $this->resultRedirectFactory->create()->setPath('*/*');
59+
}
60+
$deletedItemsCount = 0;
61+
foreach ($this->massActionFilter->getIds() as $id) {
62+
try {
63+
$this->stockRepository->deleteById($id);
64+
$deletedItemsCount++;
65+
} catch (CouldNotDeleteException $e) {
66+
$errorMessage = __('[ID: %1] ', $id) . $e->getMessage();
67+
$this->messageManager->addErrorMessage($errorMessage);
68+
}
69+
}
70+
$this->messageManager->addSuccessMessage(__('You deleted %1 Stock(s).', $deletedItemsCount));
71+
72+
return $this->resultRedirectFactory->create()->setPath('*/*');
73+
}
74+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Inventory\Ui\Component\Control\Stock;
8+
9+
use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;
10+
use Magento\InventoryApi\Api\Data\StockInterface;
11+
12+
/**
13+
* Class DeleteButton
14+
*/
15+
class DeleteButton implements ButtonProviderInterface
16+
{
17+
/**
18+
* @var GenericButton
19+
*/
20+
private $button;
21+
22+
/**
23+
* DeleteButton constructor.
24+
*
25+
* @param GenericButton $button
26+
*/
27+
public function __construct(GenericButton $button)
28+
{
29+
$this->button = $button;
30+
}
31+
32+
/**
33+
* Get stock delete button data such as label, css class, on click action and sort order
34+
*
35+
* @return array
36+
*/
37+
public function getButtonData()
38+
{
39+
$data = [];
40+
if ($this->button->getId()) {
41+
$data = [
42+
'label' => __('Delete Stock'),
43+
'class' => 'delete',
44+
'on_click' => 'deleteConfirm(\'' . __('Are you sure you want to delete this stock ?') . '\', \'' . $this->getDeleteUrl() . '\')',
45+
'sort_order' => 20,
46+
];
47+
}
48+
49+
return $data;
50+
}
51+
52+
/**
53+
* Get stock delete url
54+
*
55+
* @return string
56+
*/
57+
public function getDeleteUrl()
58+
{
59+
return $this->button->getUrl('*/*/delete', [StockInterface::STOCK_ID => $this->button->getId()]);
60+
}
61+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Inventory\Ui\Component\Control\Stock;
8+
9+
use Magento\InventoryApi\Api\Data\StockInterface;
10+
use Magento\Framework\UrlInterface;
11+
use Magento\Framework\App\Request\Http as HttpRequest;
12+
13+
/**
14+
* Class GenericButton
15+
*/
16+
class GenericButton
17+
{
18+
/**
19+
* @var \Magento\Framework\UrlInterface
20+
*/
21+
private $urlBuilder;
22+
23+
/**
24+
* @var HttpRequest
25+
*/
26+
private $request;
27+
28+
/**
29+
* @param UrlInterface $urlBuilder
30+
* @param HttpRequest $request
31+
*/
32+
public function __construct(UrlInterface $urlBuilder, HttpRequest $request)
33+
{
34+
$this->urlBuilder = $urlBuilder;
35+
$this->request = $request;
36+
}
37+
38+
/**
39+
* Get stock id
40+
*
41+
* @return int|null
42+
*/
43+
public function getId()
44+
{
45+
return $this->request->getParam(StockInterface::STOCK_ID) ?? null;
46+
}
47+
48+
/**
49+
* Get button url base on route and parameters
50+
*
51+
* @param string $route
52+
* @param array $params
53+
*
54+
* @return string
55+
*/
56+
public function getUrl($route = '', $params = [])
57+
{
58+
return $this->urlBuilder->getUrl($route, $params);
59+
}
60+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Inventory\Ui\Component\MassAction;
8+
9+
use Magento\Ui\Component\MassAction\Filter as BaseFilter;
10+
use Magento\Framework\Api\Search\DocumentInterface;
11+
12+
/**
13+
* Class Filter
14+
* @todo remove this class after code from Engine\MagentoFix\Ui\Component\MassAction\Filter would be deploy to
15+
* production
16+
*/
17+
class Filter
18+
{
19+
/**
20+
* @var BaseFilter
21+
*/
22+
private $filter;
23+
24+
/**
25+
* @param BaseFilter $filter
26+
*/
27+
public function __construct(
28+
BaseFilter $filter
29+
) {
30+
$this->filter = $filter;
31+
}
32+
33+
/**
34+
* Get ids from search filter
35+
*
36+
* @return array
37+
*/
38+
public function getIds()
39+
{
40+
$this->filter->applySelectionOnTargetProvider();
41+
42+
return array_map(function(DocumentInterface $item) {
43+
return $item->getId();
44+
}, $this->filter->getComponent()->getContext()->getDataProvider()->getSearchResult()->getItems());
45+
}
46+
}

app/code/Magento/Inventory/view/adminhtml/ui_component/inventory_stock_form.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<class>reset</class>
3030
<label translate="true">Reset</label>
3131
</button>
32+
<button name="delete" class="Magento\Inventory\Ui\Component\Control\Stock\DeleteButton"/>
3233
<button name="save" class="Magento\Inventory\Ui\Component\Control\Stock\SaveSplitButton"/>
3334
</buttons>
3435
</settings>

app/code/Magento/Inventory/view/adminhtml/ui_component/inventory_stock_listing.xml

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,26 @@
5757
</templates>
5858
</settings>
5959
</filters>
60-
<massaction/>
60+
<massaction name="listing_massaction">
61+
<argument name="data" xsi:type="array">
62+
<item name="config" xsi:type="array">
63+
<item name="component" xsi:type="string">Magento_Ui/js/grid/tree-massactions</item>
64+
</item>
65+
</argument>
66+
<action name="delete">
67+
<argument name="data" xsi:type="array">
68+
<item name="config" xsi:type="array">
69+
<item name="type" xsi:type="string">delete</item>
70+
<item name="label" xsi:type="string" translate="true">Delete</item>
71+
<item name="url" xsi:type="url" path="inventory/stock/massDelete"/>
72+
<item name="confirm" xsi:type="array">
73+
<item name="title" xsi:type="string" translate="true">Delete items</item>
74+
<item name="message" xsi:type="string" translate="true">Are you sure you want to delete selected items?</item>
75+
</item>
76+
</item>
77+
</argument>
78+
</action>
79+
</massaction>
6180
<paging name="listing_paging"/>
6281
</listingToolbar>
6382
<columns name="inventory_stock_listing_columns">

0 commit comments

Comments
 (0)