Skip to content

Commit 8529467

Browse files
committed
Fix #14958 - remove sales sequence data on store view delete
1 parent 07e57ac commit 8529467

File tree

5 files changed

+136
-1
lines changed

5 files changed

+136
-1
lines changed

app/code/Magento/SalesSequence/Model/Builder.php

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
*/
66
namespace Magento\SalesSequence\Model;
77

8+
use Magento\Framework\App\ObjectManager;
89
use Magento\Framework\App\ResourceConnection as AppResource;
910
use Magento\Framework\DB\Ddl\Sequence as DdlSequence;
1011
use Magento\Framework\Webapi\Exception;
1112
use Magento\SalesSequence\Model\ResourceModel\Meta as ResourceMetadata;
13+
use Magento\SalesSequence\Model\ResourceModel\Profile as ResourceProfile;
1214
use Psr\Log\LoggerInterface as Logger;
1315

1416
/**
@@ -83,28 +85,36 @@ class Builder
8385
*/
8486
protected $logger;
8587

88+
/**
89+
* @var ResourceProfile
90+
*/
91+
private $resourceProfile;
92+
8693
/**
8794
* @param ResourceMetadata $resourceMetadata
8895
* @param MetaFactory $metaFactory
8996
* @param ProfileFactory $profileFactory
9097
* @param AppResource $appResource
9198
* @param DdlSequence $ddlSequence
9299
* @param Logger $logger
100+
* @param ResourceProfile|null $resourceProfile
93101
*/
94102
public function __construct(
95103
ResourceMetadata $resourceMetadata,
96104
MetaFactory $metaFactory,
97105
ProfileFactory $profileFactory,
98106
AppResource $appResource,
99107
DdlSequence $ddlSequence,
100-
Logger $logger
108+
Logger $logger,
109+
ResourceProfile $resourceProfile = null
101110
) {
102111
$this->resourceMetadata = $resourceMetadata;
103112
$this->metaFactory = $metaFactory;
104113
$this->profileFactory = $profileFactory;
105114
$this->appResource = $appResource;
106115
$this->ddlSequence = $ddlSequence;
107116
$this->logger = $logger;
117+
$this->resourceProfile = $resourceProfile ?: ObjectManager::getInstance()->get(ResourceProfile::class);
108118
$this->data = array_flip($this->pattern);
109119
}
110120

@@ -264,4 +274,36 @@ public function create()
264274
}
265275
$this->data = array_flip($this->pattern);
266276
}
277+
278+
/**
279+
* Deletes all sequence linked entites
280+
*
281+
* @param $storeId
282+
*
283+
* @return void
284+
* @throws \Magento\Framework\Exception\LocalizedException
285+
*/
286+
public function deleteByStoreId($storeId)
287+
{
288+
$metadataIds = $this->resourceMetadata->getIdsByStore($storeId);
289+
$profileIds = $this->resourceProfile->getProfileIdsByMetadataIds($metadataIds);
290+
291+
$this->appResource->getConnection()->delete(
292+
$this->appResource->getTableName('sales_sequence_profile'),
293+
['profile_id IN (?)' => $profileIds]
294+
);
295+
296+
foreach ($metadataIds as $metadataId) {
297+
$metadata = $this->metaFactory->create();
298+
$this->resourceMetadata->load($metadata, $metadataId);
299+
if (!$metadata->getId()) {
300+
continue;
301+
}
302+
303+
$this->appResource->getConnection()->dropTable(
304+
$metadata->getSequenceTable()
305+
);
306+
$this->resourceMetadata->delete($metadata);
307+
}
308+
}
267309
}

app/code/Magento/SalesSequence/Model/ResourceModel/Meta.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,27 @@ public function loadByEntityTypeAndStore($entityType, $storeId)
9292
return $meta;
9393
}
9494

95+
/**
96+
* Retrieves Metadata Ids by store id
97+
*
98+
* @param int $storeId
99+
* @return int[]
100+
* @throws \Magento\Framework\Exception\LocalizedException
101+
*/
102+
public function getIdsByStore($storeId)
103+
{
104+
$connection = $this->getConnection();
105+
$bind = ['store_id' => $storeId];
106+
$select = $connection->select()->from(
107+
$this->getMainTable(),
108+
[$this->getIdFieldName()]
109+
)->where(
110+
'store_id = :store_id'
111+
);
112+
113+
return $connection->fetchCol($select, $bind);
114+
}
115+
95116
/**
96117
* Using for load sequence profile and setting it into metadata
97118
*

app/code/Magento/SalesSequence/Model/ResourceModel/Profile.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,20 @@ public function loadActiveProfile($metadataId)
7676
}
7777
return $profile;
7878
}
79+
80+
/**
81+
* Get profile ids by metadata ids
82+
* @param int[] $metadataIds
83+
* @return int[]
84+
* @throws \Magento\Framework\Exception\LocalizedException
85+
*/
86+
public function getProfileIdsByMetadataIds(array $metadataIds)
87+
{
88+
$connection = $this->getConnection();
89+
$select = $connection->select()
90+
->from($this->getMainTable(), ['profile_id'])
91+
->where('meta_id IN (?)', $metadataIds);
92+
93+
return $connection->fetchCol($select);
94+
}
7995
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
namespace Magento\SalesSequence\Observer;
8+
9+
use Magento\Framework\Event\ObserverInterface;
10+
use Magento\Framework\Event\Observer as EventObserver;
11+
use Magento\SalesSequence\Model\Builder;
12+
13+
/**
14+
* Class SequenceRemovalObserver
15+
*/
16+
class SequenceRemovalObserver implements ObserverInterface
17+
{
18+
/**
19+
* @var Builder
20+
*/
21+
private $sequenceBuilder;
22+
23+
/**
24+
* @param Builder $sequenceBuilder
25+
*/
26+
public function __construct(
27+
Builder $sequenceBuilder
28+
) {
29+
$this->sequenceBuilder = $sequenceBuilder;
30+
}
31+
32+
/**
33+
* @param EventObserver $observer
34+
* @return $this
35+
* @throws \Magento\Framework\Exception\LocalizedException
36+
*/
37+
public function execute(EventObserver $observer)
38+
{
39+
$storeId = $observer->getData('store')->getId();
40+
$this->sequenceBuilder->deleteByStoreId($storeId);
41+
42+
return $this;
43+
}
44+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
9+
<event name="store_delete">
10+
<observer name="magento_sequence" instance="Magento\SalesSequence\Observer\SequenceRemovalObserver" />
11+
</event>
12+
</config>

0 commit comments

Comments
 (0)