1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\InventoryCatalog\Model;
use Magento\Framework\Validation\ValidationException;
use Magento\InventoryCatalog\Model\ResourceModel\BulkInventoryTransfer as BulkInventoryTransferResource;
use Magento\InventoryCatalogApi\Api\BulkInventoryTransferInterface;
use Magento\InventoryCatalogApi\Api\DefaultSourceProviderInterface;
use Magento\InventoryCatalogApi\Model\BulkInventoryTransferValidatorInterface;
use Magento\InventoryCatalogApi\Model\GetProductIdsBySkusInterface;
use Magento\CatalogInventory\Model\Indexer\Stock as LegacyIndexer;
use Magento\InventoryIndexer\Indexer\Source\SourceIndexer;
/**
* @inheritdoc
*/
class BulkInventoryTransfer implements BulkInventoryTransferInterface
{
/**
* @var BulkInventoryTransferValidatorInterface
*/
private $bulkInventoryTransferValidator;
/**
* @var BulkInventoryTransfer
*/
private $bulkInventoryTransfer;
/**
* @var GetProductIdsBySkusInterface
*/
private $getProductIdsBySkus;
/**
* @var LegacyIndexer
*/
private $legacyIndexer;
/**
* @var DefaultSourceProviderInterface
*/
private $defaultSourceProvider;
/**
* @var SourceIndexer
*/
private $sourceIndexer;
/**
* MassProductSourceAssign constructor.
* @param BulkInventoryTransferValidatorInterface $inventoryTransferValidator
* @param BulkInventoryTransferResource $bulkInventoryTransfer
* @param SourceIndexer $sourceIndexer
* @param DefaultSourceProviderInterface $defaultSourceProvider
* @param GetProductIdsBySkusInterface $getProductIdsBySkus
* @param LegacyIndexer $legacyIndexer
* @SuppressWarnings(PHPMD.LongVariable)
*/
public function __construct(
BulkInventoryTransferValidatorInterface $inventoryTransferValidator,
BulkInventoryTransferResource $bulkInventoryTransfer,
SourceIndexer $sourceIndexer,
DefaultSourceProviderInterface $defaultSourceProvider,
GetProductIdsBySkusInterface $getProductIdsBySkus,
LegacyIndexer $legacyIndexer
) {
$this->bulkInventoryTransferValidator = $inventoryTransferValidator;
$this->bulkInventoryTransfer = $bulkInventoryTransfer;
$this->getProductIdsBySkus = $getProductIdsBySkus;
$this->legacyIndexer = $legacyIndexer;
$this->defaultSourceProvider = $defaultSourceProvider;
$this->sourceIndexer = $sourceIndexer;
}
/**
* Reindex legacy stock (for default source)
* @param array $productIds
*/
private function reindexLegacy(array $productIds): void
{
$this->legacyIndexer->executeList($productIds);
}
/**
* @inheritdoc
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function execute(
array $skus,
string $originSource,
string $destinationSource,
bool $unassignFromOrigin
): bool {
$validationResult = $this->bulkInventoryTransferValidator->validate(
$skus,
$originSource,
$destinationSource
);
if (!$validationResult->isValid()) {
throw new ValidationException(__('Validation Failed'), null, 0, $validationResult);
}
$this->bulkInventoryTransfer->execute(
$skus,
$originSource,
$destinationSource,
$unassignFromOrigin
);
$this->sourceIndexer->executeList([$originSource, $destinationSource]);
if (($this->defaultSourceProvider->getCode() === $originSource) ||
($this->defaultSourceProvider->getCode() === $destinationSource)) {
$productIds = array_values($this->getProductIdsBySkus->execute($skus));
$this->reindexLegacy($productIds);
}
return true;
}
}