diff --git a/app/code/Magento/SalesSequence/Model/Builder.php b/app/code/Magento/SalesSequence/Model/Builder.php index 443892b420def..b07e687ec25d9 100644 --- a/app/code/Magento/SalesSequence/Model/Builder.php +++ b/app/code/Magento/SalesSequence/Model/Builder.php @@ -5,10 +5,12 @@ */ namespace Magento\SalesSequence\Model; +use Magento\Framework\App\ObjectManager; use Magento\Framework\App\ResourceConnection as AppResource; use Magento\Framework\DB\Ddl\Sequence as DdlSequence; use Magento\Framework\Webapi\Exception; use Magento\SalesSequence\Model\ResourceModel\Meta as ResourceMetadata; +use Magento\SalesSequence\Model\ResourceModel\Profile as ResourceProfile; use Psr\Log\LoggerInterface as Logger; /** @@ -83,6 +85,11 @@ class Builder */ protected $logger; + /** + * @var ResourceProfile + */ + private $resourceProfile; + /** * @param ResourceMetadata $resourceMetadata * @param MetaFactory $metaFactory @@ -90,6 +97,7 @@ class Builder * @param AppResource $appResource * @param DdlSequence $ddlSequence * @param Logger $logger + * @param ResourceProfile|null $resourceProfile */ public function __construct( ResourceMetadata $resourceMetadata, @@ -97,7 +105,8 @@ public function __construct( ProfileFactory $profileFactory, AppResource $appResource, DdlSequence $ddlSequence, - Logger $logger + Logger $logger, + ResourceProfile $resourceProfile = null ) { $this->resourceMetadata = $resourceMetadata; $this->metaFactory = $metaFactory; @@ -105,6 +114,7 @@ public function __construct( $this->appResource = $appResource; $this->ddlSequence = $ddlSequence; $this->logger = $logger; + $this->resourceProfile = $resourceProfile ?: ObjectManager::getInstance()->get(ResourceProfile::class); $this->data = array_flip($this->pattern); } @@ -264,4 +274,36 @@ public function create() } $this->data = array_flip($this->pattern); } + + /** + * Deletes all sequence linked entites + * + * @param $storeId + * + * @return void + * @throws \Magento\Framework\Exception\LocalizedException + */ + public function deleteByStoreId($storeId) + { + $metadataIds = $this->resourceMetadata->getIdsByStore($storeId); + $profileIds = $this->resourceProfile->getProfileIdsByMetadataIds($metadataIds); + + $this->appResource->getConnection()->delete( + $this->appResource->getTableName('sales_sequence_profile'), + ['profile_id IN (?)' => $profileIds] + ); + + foreach ($metadataIds as $metadataId) { + $metadata = $this->metaFactory->create(); + $this->resourceMetadata->load($metadata, $metadataId); + if (!$metadata->getId()) { + continue; + } + + $this->appResource->getConnection()->dropTable( + $metadata->getSequenceTable() + ); + $this->resourceMetadata->delete($metadata); + } + } } diff --git a/app/code/Magento/SalesSequence/Model/ResourceModel/Meta.php b/app/code/Magento/SalesSequence/Model/ResourceModel/Meta.php index 7a4aabea85680..b46844cfab524 100644 --- a/app/code/Magento/SalesSequence/Model/ResourceModel/Meta.php +++ b/app/code/Magento/SalesSequence/Model/ResourceModel/Meta.php @@ -92,6 +92,27 @@ public function loadByEntityTypeAndStore($entityType, $storeId) return $meta; } + /** + * Retrieves Metadata Ids by store id + * + * @param int $storeId + * @return int[] + * @throws \Magento\Framework\Exception\LocalizedException + */ + public function getIdsByStore($storeId) + { + $connection = $this->getConnection(); + $bind = ['store_id' => $storeId]; + $select = $connection->select()->from( + $this->getMainTable(), + [$this->getIdFieldName()] + )->where( + 'store_id = :store_id' + ); + + return $connection->fetchCol($select, $bind); + } + /** * Using for load sequence profile and setting it into metadata * diff --git a/app/code/Magento/SalesSequence/Model/ResourceModel/Profile.php b/app/code/Magento/SalesSequence/Model/ResourceModel/Profile.php index 4f1788b0ca353..bb9b5d19c70eb 100644 --- a/app/code/Magento/SalesSequence/Model/ResourceModel/Profile.php +++ b/app/code/Magento/SalesSequence/Model/ResourceModel/Profile.php @@ -76,4 +76,20 @@ public function loadActiveProfile($metadataId) } return $profile; } + + /** + * Get profile ids by metadata ids + * @param int[] $metadataIds + * @return int[] + * @throws \Magento\Framework\Exception\LocalizedException + */ + public function getProfileIdsByMetadataIds(array $metadataIds) + { + $connection = $this->getConnection(); + $select = $connection->select() + ->from($this->getMainTable(), ['profile_id']) + ->where('meta_id IN (?)', $metadataIds); + + return $connection->fetchCol($select); + } } diff --git a/app/code/Magento/SalesSequence/Observer/SequenceRemovalObserver.php b/app/code/Magento/SalesSequence/Observer/SequenceRemovalObserver.php new file mode 100644 index 0000000000000..4b25bb4c51cee --- /dev/null +++ b/app/code/Magento/SalesSequence/Observer/SequenceRemovalObserver.php @@ -0,0 +1,44 @@ +sequenceBuilder = $sequenceBuilder; + } + + /** + * @param EventObserver $observer + * @return $this + * @throws \Magento\Framework\Exception\LocalizedException + */ + public function execute(EventObserver $observer) + { + $storeId = $observer->getData('store')->getId(); + $this->sequenceBuilder->deleteByStoreId($storeId); + + return $this; + } +} diff --git a/app/code/Magento/SalesSequence/etc/events.xml b/app/code/Magento/SalesSequence/etc/events.xml new file mode 100644 index 0000000000000..5bbde6ab188a5 --- /dev/null +++ b/app/code/Magento/SalesSequence/etc/events.xml @@ -0,0 +1,12 @@ + + + + + + +