58 lines
2.0 KiB
PHP
58 lines
2.0 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\LegacyOrder\Controller\Adminhtml\Order;
|
|
|
|
use Esun\LegacyOrder\Model\ResourceModel\Order\CollectionFactory;
|
|
use Magento\Backend\App\Action;
|
|
use Magento\Framework\App\Action\HttpPostActionInterface;
|
|
use Magento\Framework\App\ResourceConnection;
|
|
use Magento\Framework\Controller\ResultFactory;
|
|
use Magento\Ui\Component\MassAction\Filter;
|
|
|
|
class MassUnlink extends Action implements HttpPostActionInterface
|
|
{
|
|
public const ADMIN_RESOURCE = 'Esun_LegacyOrder::orders';
|
|
|
|
public function __construct(
|
|
Action\Context $context,
|
|
private readonly Filter $filter,
|
|
private readonly CollectionFactory $collectionFactory,
|
|
private readonly ResourceConnection $resource
|
|
) {
|
|
parent::__construct($context);
|
|
}
|
|
|
|
public function execute()
|
|
{
|
|
try {
|
|
$collection = $this->filter->getCollection($this->collectionFactory->create());
|
|
$ids = array_map('intval', $collection->getAllIds());
|
|
$updated = 0;
|
|
|
|
if ($ids) {
|
|
$updated = $this->resource->getConnection()->update(
|
|
$this->resource->getTableName('esun_legacy_order'),
|
|
['customer_id' => null, 'link_status' => 'unlinked'],
|
|
['legacy_order_id IN (?)' => $ids, 'customer_id IS NOT NULL']
|
|
);
|
|
}
|
|
|
|
$this->messageManager->addSuccessMessage(
|
|
__('%1 legacy order(s) were unbound from customers.', $updated)
|
|
);
|
|
|
|
$alreadyUnlinked = count($ids) - $updated;
|
|
if ($alreadyUnlinked > 0) {
|
|
$this->messageManager->addNoticeMessage(
|
|
__('%1 selected order(s) were already unbound.', $alreadyUnlinked)
|
|
);
|
|
}
|
|
} catch (\Throwable $exception) {
|
|
$this->messageManager->addErrorMessage($exception->getMessage());
|
|
}
|
|
|
|
return $this->resultFactory->create(ResultFactory::TYPE_REDIRECT)->setPath('*/*/index');
|
|
}
|
|
}
|