-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathSources.php
More file actions
186 lines (166 loc) · 5.67 KB
/
Sources.php
File metadata and controls
186 lines (166 loc) · 5.67 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
/**
* Copyright 2017 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);
namespace Magento\InventoryImportExport\Model\Import;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\LocalizedException;
use Magento\ImportExport\Helper\Data as DataHelper;
use Magento\ImportExport\Model\Import\Entity\AbstractEntity;
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
use Magento\ImportExport\Model\ResourceModel\Helper as ResourceHelper;
use Magento\ImportExport\Model\ResourceModel\Import\Data as ImportData;
use Magento\InventoryApi\Api\Data\SourceItemInterface;
use Magento\InventoryApi\Model\GetSourceCodesBySkusInterface;
use Magento\InventoryImportExport\Model\Import\Command\CommandInterface;
use Magento\InventoryImportExport\Model\Import\Serializer\Json;
use Magento\InventoryImportExport\Model\Import\Validator\ValidatorInterface;
/**
* @inheritdoc
*/
class Sources extends AbstractEntity
{
/**
* Column names for import file
*/
public const COL_SKU = SourceItemInterface::SKU;
public const COL_SOURCE_CODE = SourceItemInterface::SOURCE_CODE;
public const COL_QTY = SourceItemInterface::QUANTITY;
public const COL_STATUS = SourceItemInterface::STATUS;
protected $validColumnNames = [
self::COL_SKU,
self::COL_SOURCE_CODE,
self::COL_QTY,
self::COL_STATUS
];
/**
* @var Json
*/
protected $jsonHelper;
/**
* @var ValidatorInterface
*/
private $validator;
/**
* @var CommandInterface[]
*/
private $commands = [];
/**
* @var GetSourceCodesBySkusInterface
*/
private $getSourceCodesBySkus;
/**
* @param Json $jsonHelper
* @param ProcessingErrorAggregatorInterface $errorAggregator
* @param ResourceHelper $resourceHelper
* @param DataHelper $dataHelper
* @param ImportData $importData
* @param ValidatorInterface $validator
* @param CommandInterface[] $commands
* @param GetSourceCodesBySkusInterface|null $getSourceCodesBySkus
* @throws LocalizedException
*/
public function __construct(
Json $jsonHelper,
ProcessingErrorAggregatorInterface $errorAggregator,
ResourceHelper $resourceHelper,
DataHelper $dataHelper,
ImportData $importData,
ValidatorInterface $validator,
array $commands = [],
?GetSourceCodesBySkusInterface $getSourceCodesBySkus = null,
) {
$this->jsonHelper = $jsonHelper;
$this->errorAggregator = $errorAggregator;
$this->_resourceHelper = $resourceHelper;
$this->_importExportData = $dataHelper;
$this->_dataSourceModel = $importData;
$this->validator = $validator;
foreach ($commands as $command) {
if (!$command instanceof CommandInterface) {
throw new LocalizedException(
__('Source Import Commands must implement %interface.', ['interface' => CommandInterface::class])
);
}
}
$this->commands = $commands;
$this->getSourceCodesBySkus = $getSourceCodesBySkus ?: ObjectManager::getInstance()
->get(\Magento\InventoryApi\Model\GetSourceCodesBySkusInterface::class);
foreach ($this->errorMessageTemplates as $errorCode => $message) {
$this->getErrorAggregator()->addErrorMessageTemplate($errorCode, $message);
}
}
/**
* Import data rows.
*
* @return boolean
* @throws LocalizedException
*/
protected function _importData()
{
$command = $this->getCommandByBehavior($this->getBehavior());
while ($bunch = $this->_dataSourceModel->getNextUniqueBunch($this->getIds())) {
foreach ($bunch as $rowData) {
if ($this->getBehavior() == \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND) {
$sourceCodes = $this->getSourceCodesBySkus->execute([$rowData['sku']]);
if (in_array($rowData['source_code'], $sourceCodes)) {
$this->countItemsUpdated ++;
} else {
$this->countItemsCreated ++;
}
} elseif ($this->getBehavior() == \Magento\ImportExport\Model\Import::BEHAVIOR_REPLACE) {
$this->countItemsUpdated ++;
} elseif ($this->getBehavior() == \Magento\ImportExport\Model\Import::BEHAVIOR_DELETE) {
$this->countItemsDeleted ++;
}
}
$command->execute($bunch);
}
return true;
}
/**
* To fetch command by behavior
*
* @param string $behavior
* @return CommandInterface
* @throws LocalizedException
*/
private function getCommandByBehavior($behavior)
{
if (!isset($this->commands[$behavior])) {
throw new LocalizedException(
__('There is no command registered for behavior "%behavior".', ['behavior' => $behavior])
);
}
return $this->commands[$behavior];
}
/**
* EAV entity type code getter.
*
* @return string
*/
public function getEntityTypeCode()
{
return 'stock_sources';
}
/**
* Validate data row.
*
* @param array $rowData
* @param int $rowNum
* @return boolean
*/
public function validateRow(array $rowData, $rowNum)
{
$result = $this->validator->validate($rowData, $rowNum);
if ($result->isValid()) {
return true;
}
foreach ($result->getErrors() as $error) {
$this->addRowError($error, $rowNum);
}
return false;
}
}