Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete Sources #3412

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Inventory/Model/SourceRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,17 @@ public function getList(SearchCriteriaInterface $searchCriteria = null): SourceS
{
return $this->commandGetList->execute($searchCriteria);
}

/**
* @inheritdoc
*/
public function deleteBySourceCode(SourceInterface $source): bool
{
try {
$this->sourceResource->delete($source);
} catch (\Exception $exception) {
throw new CouldNotDeleteException(__($exception->getMessage()));
}
return true;
}
}
115 changes: 115 additions & 0 deletions InventoryAdminUi/Controller/Adminhtml/Source/MassDelete
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace Magento\InventoryAdminUi\Controller\Adminhtml\Source;

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Exception\CouldNotDeleteException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Inventory\Model\ResourceModel\Source\Collection;
use Magento\Inventory\Model\ResourceModel\Source\CollectionFactory;
use Magento\InventoryApi\Api\Data\SourceInterfaceFactory;
use Magento\InventoryApi\Api\SourceItemsDeleteInterface;
use Magento\InventoryApi\Api\SourceRepositoryInterface;
use Magento\InventoryCatalogApi\Api\DefaultSourceProviderInterface;
use Magento\Ui\Component\MassAction\Filter;

class MassDelete extends Action implements HttpPostActionInterface
{
/**
* @see _isAllowed()
*/
const ADMIN_RESOURCE = 'Magento_InventoryApi::source_delete';

/**
* @var Filter
*/
private $massActionFilter;

/**
* @var CollectionFactory
*/
private $sourceCollectionFactory;

/**
* @var SourceInterfaceFactory
*/
private $sourceFactory;

/**
* @var SourceRepositoryInterface
*/
private $sourceRepository;

/**
* @var DefaultSourceProviderInterface
*/
private $defaultSourceProvider;

private SourceItemsDeleteInterface $sourceItemsDelete;

/**
* @param DefaultSourceProviderInterface $defaultSourceProvider
* @param CollectionFactory $sourceCollectionFactory
* @param Context $context
* @param Filter $massActionFilter
* @param SourceInterfaceFactory $sourceFactory
* @param SourceRepositoryInterface $sourceRepository
*/
public function __construct(
DefaultSourceProviderInterface $defaultSourceProvider,
CollectionFactory $sourceCollectionFactory,
SourceItemsDeleteInterface $sourceItemsDelete,
Context $context,
Filter $massActionFilter,
SourceInterfaceFactory $sourceFactory,
SourceRepositoryInterface $sourceRepository
) {
parent::__construct($context);
$this->massActionFilter = $massActionFilter;
$this->sourceCollectionFactory = $sourceCollectionFactory;
$this->sourceFactory = $sourceFactory;
$this->sourceRepository = $sourceRepository;
$this->defaultSourceProvider = $defaultSourceProvider;
$this->sourceItemsDelete = $sourceItemsDelete;
}


/**
* @throws NoSuchEntityException
* @throws CouldNotDeleteException
* @throws LocalizedException
*/
public function execute(): ResultInterface
{
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('inventory/source/index');
$request = $this->getRequest();
if (!$request->isPost()) {
$this->messageManager->addErrorMessage(__('Wrong request.'));
return $resultRedirect;
}
$sourceCollection = $this->sourceCollectionFactory->create();
$this->massActionFilter->getCollection($sourceCollection);
$this->deleteSources($sourceCollection);

return $resultRedirect;
}


/**
* @throws NoSuchEntityException
* @throws CouldNotDeleteException
*/
public function deleteSources(Collection $sourceCollection): void
{
$size = $sourceCollection->getSize();
foreach ($sourceCollection as $source) {
$this->sourceRepository->deleteBySourceCode($source);
}
$this->messageManager->addSuccessMessage(__('A total of %1 record(s) have been deleted.', $size));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@
<label translate="true">Disable</label>
</settings>
</action>
<action name="delete">
<settings>
<confirm>
<message translate="true">Are you sure you want to delete selected source?</message>
<title translate="true">Delete items</title>
</confirm>
<url path="inventory/block/massDelete"/>
<type>delete</type>
<label translate="true">Delete</label>
</settings>
</action>
</massaction>
<paging name="listing_paging"/>
</listingToolbar>
Expand Down
10 changes: 10 additions & 0 deletions InventoryApi/Api/SourceRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,14 @@ public function get(string $sourceCode): \Magento\InventoryApi\Api\Data\SourceIn
public function getList(
\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria = null
): \Magento\InventoryApi\Api\Data\SourceSearchResultsInterface;

/**
* Delete the Source by Source Code. If Source is not found do nothing
*
* @param SourceInterface $sourceCode
* @return bool
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @throws \Magento\Framework\Exception\CouldNotDeleteException
*/
public function deleteBySourceCode(SourceInterface $source): bool;
}