1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Product\TierPrice\Observers\ProductTierPriceObserver |
5
|
|
|
* |
6
|
|
|
* PHP version 7 |
7
|
|
|
* |
8
|
|
|
* @author Klaas-Tido Rühl <[email protected]> |
9
|
|
|
* @author Tim Wagner <[email protected]> |
10
|
|
|
* @copyright 2019 REFUSiON GmbH <[email protected]> |
11
|
|
|
* @license https://opensource.org/licenses/MIT |
12
|
|
|
* @link https://github.com/techdivision/import-product-tier-price |
13
|
|
|
* @link https://www.techdivision.com |
14
|
|
|
* @link https://www.refusion.com |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace TechDivision\Import\Product\TierPrice\Observers; |
18
|
|
|
|
19
|
|
|
use TechDivision\Import\Utils\StoreViewCodes; |
20
|
|
|
use TechDivision\Import\Product\TierPrice\Utils\ColumnKeys; |
21
|
|
|
use TechDivision\Import\Product\TierPrice\Utils\DefaultCodes; |
22
|
|
|
use TechDivision\Import\Product\TierPrice\Utils\ValueTypesInterface; |
23
|
|
|
use TechDivision\Import\Product\Observers\AbstractProductImportObserver; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Observer for creating a separate import file for the tier price data. |
27
|
|
|
* |
28
|
|
|
* @author Klaas-Tido Rühl <[email protected]> |
29
|
|
|
* @author Tim Wagner <[email protected]> |
30
|
|
|
* @copyright 2019 REFUSiON GmbH <[email protected]> |
31
|
|
|
* @license https://opensource.org/licenses/MIT |
32
|
|
|
* @link https://github.com/techdivision/import-product-tier-price |
33
|
|
|
* @link https://www.techdivision.com |
34
|
|
|
* @link https://www.refusion.com |
35
|
|
|
*/ |
36
|
|
|
class ProductTierPriceObserver extends AbstractProductImportObserver |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The artefact type. |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
const ARTEFACT_TYPE = 'tier-price'; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The available tier price value types. |
48
|
|
|
* |
49
|
|
|
* @var \TechDivision\Import\Product\TierPrice\Utils\ValueTypesInterface |
50
|
|
|
*/ |
51
|
|
|
protected $valueTypes; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Initializes the observer with the tier price value types. |
55
|
|
|
* |
56
|
|
|
* @param \TechDivision\Import\Product\TierPrice\Utils\ValueTypesInterface $valueTypes The tier price value types |
57
|
|
|
*/ |
58
|
|
|
public function __construct(ValueTypesInterface $valueTypes) |
59
|
|
|
{ |
60
|
|
|
$this->valueTypes = $valueTypes; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Returns the tier price value types. |
65
|
|
|
* |
66
|
|
|
* @return \TechDivision\Import\Product\TierPrice\Utils\ValueTypesInterface The tier price value types |
67
|
|
|
*/ |
68
|
|
|
protected function getValueTypes() |
69
|
|
|
{ |
70
|
|
|
return $this->valueTypes; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Process the observer's business logic. |
75
|
|
|
* |
76
|
|
|
* @return void |
77
|
|
|
* @throws \Exception |
78
|
|
|
*/ |
79
|
|
|
protected function process() |
80
|
|
|
{ |
81
|
|
|
|
82
|
|
|
// initialize the array for the artefacts |
83
|
|
|
$artefacts = array(); |
84
|
|
|
|
85
|
|
|
// load the SKU from the row |
86
|
|
|
$sku = $this->getValue(ColumnKeys::SKU); |
87
|
|
|
|
88
|
|
|
// prepare the store view code |
89
|
|
|
$this->getSubject()->prepareStoreViewCode(); |
90
|
|
|
|
91
|
|
|
// try to load the store view code |
92
|
|
|
$storeViewCode = $this->getSubject()->getStoreViewCode(StoreViewCodes::ADMIN); |
93
|
|
|
|
94
|
|
|
// mapping only possible for admin store |
95
|
|
|
if ($storeViewCode != StoreViewCodes::ADMIN) { |
96
|
|
|
return; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// load the serialized tier price information from the column |
100
|
|
|
if ($tierPrices = $this->getValue(ColumnKeys::TIER_PRICES)) { |
101
|
|
|
// iterate over the found tier prices |
102
|
|
|
foreach ($this->explode($tierPrices, '|') as $tierPrice) { |
103
|
|
|
// explode the tier prices |
104
|
|
|
$explodedTierPrice = $this->getSubject()->explode($tierPrice); |
105
|
|
|
// build the dictionary |
106
|
|
|
$tierPriceImportData = array(); |
107
|
|
|
|
108
|
|
|
// explode the tier price details |
109
|
|
|
foreach ($explodedTierPrice as $assignmentString) { |
110
|
|
|
list($key, $value) = $this->explode($assignmentString, '='); |
111
|
|
|
$tierPriceImportData[$key] = trim($value); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
// load the quantity and the price for the tier price |
115
|
|
|
$qty = isset($tierPriceImportData[ColumnKeys::QTY]) ? $tierPriceImportData[ColumnKeys::QTY] : null; |
116
|
|
|
$price = isset($tierPriceImportData[ColumnKeys::PRICE]) ? $tierPriceImportData[ColumnKeys::PRICE] : null; |
117
|
|
|
|
118
|
|
|
// validate quantity and price |
119
|
|
|
if ($qty == null || $price == null) { |
120
|
|
|
throw new \Exception( |
121
|
|
|
$this->appendExceptionSuffix( |
122
|
|
|
sprintf('Missing qty or price for tier price for product "%s"', $sku) |
123
|
|
|
) |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
// load the other values from the extracted data |
128
|
|
|
$valueType = $this->getValueType($tierPriceImportData); |
129
|
|
|
$website = $this->getWebsiteCode($tierPriceImportData); |
130
|
|
|
$customerGroup = $this->getCustomerGroupCode($tierPriceImportData); |
131
|
|
|
$tierpriceWebsites = $this->explode($this->getValue(ColumnKeys::PRODUCT_WEBSITES)); |
132
|
|
|
|
133
|
|
|
if ($website === DefaultCodes::ALL_WEBSITES || in_array($website, $tierpriceWebsites)) { |
134
|
|
|
// prepare the artefact we want to export |
135
|
|
|
$artefacts = $this->createArtefact($sku, $qty, $price, $valueType, $website, $customerGroup, $artefacts); |
|
|
|
|
136
|
|
|
} else { |
137
|
|
|
$this->getSubject()->getSystemLogger()->warning( |
138
|
|
|
sprintf( |
139
|
|
|
"The Product with the SKU %s has not assigned to the Website %s for tier prices", |
140
|
|
|
$sku, |
141
|
|
|
$website |
142
|
|
|
) |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
// add the artefact to the observer to be exported later |
147
|
|
|
$this->addArtefacts($artefacts); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Returns the tier price value type from the passed data, or the default one (fixed), if not set. |
153
|
|
|
* |
154
|
|
|
* @param array $data The data to load the tier price value type from |
155
|
|
|
* |
156
|
|
|
* @return string The tier price value type |
157
|
|
|
*/ |
158
|
|
|
protected function getValueType(array $data) |
159
|
|
|
{ |
160
|
|
|
return isset($data[ColumnKeys::VALUE_TYPE]) ? $data[ColumnKeys::VALUE_TYPE] : $this->getValueTypes()->getFixed(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Returns the website code from the passed data, or 'All Websites', if not set. |
165
|
|
|
* |
166
|
|
|
* @param array $data The data to load the website codefrom |
167
|
|
|
* |
168
|
|
|
* @return string The website code |
169
|
|
|
*/ |
170
|
|
|
protected function getWebsiteCode(array $data) |
171
|
|
|
{ |
172
|
|
|
return $data[ColumnKeys::WEBSITE] ? $data[ColumnKeys::WEBSITE] : DefaultCodes::ALL_WEBSITES; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Returns the customer group code from the passed data, or 'ALL GROUPS', if not set. |
177
|
|
|
* |
178
|
|
|
* @param array $data The data to load the customer group code from |
179
|
|
|
* |
180
|
|
|
* @return string The customer group code |
181
|
|
|
*/ |
182
|
|
|
protected function getCustomerGroupCode(array $data) |
183
|
|
|
{ |
184
|
|
|
return $data[ColumnKeys::CUSTOMER_GROUP] ? $data[ColumnKeys::CUSTOMER_GROUP] : DefaultCodes::ALL_GROUPS; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Create's and return's a new empty artefact entity. |
189
|
|
|
* |
190
|
|
|
* @param array $columns The array with the column data |
191
|
|
|
* @param array $originalColumnNames The array with a mapping from the old to the new column names |
192
|
|
|
* |
193
|
|
|
* @return array The new artefact entity |
194
|
|
|
*/ |
195
|
|
|
protected function newArtefact(array $columns, array $originalColumnNames) |
196
|
|
|
{ |
197
|
|
|
return $this->getSubject()->newArtefact($columns, $originalColumnNames); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Add the passed product type artefacts to the product with the |
202
|
|
|
* last entity ID. |
203
|
|
|
* |
204
|
|
|
* @param array $artefacts The product type artefacts |
205
|
|
|
* |
206
|
|
|
* @return void |
207
|
|
|
* @uses \TechDivision\Import\Product\TierPrice\Subjects\TierPriceSubject::getLastEntityId() |
208
|
|
|
*/ |
209
|
|
|
protected function addArtefacts(array $artefacts) |
210
|
|
|
{ |
211
|
|
|
$this->getSubject()->addArtefacts(ProductTierPriceObserver::ARTEFACT_TYPE, $artefacts); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @param string $sku Sku |
216
|
|
|
* @param string $qty Quantity |
217
|
|
|
* @param string $price Price |
218
|
|
|
* @param string $valueType valueType |
219
|
|
|
* @param string $website Website |
220
|
|
|
* @param string $customerGroup Customer Group |
221
|
|
|
* @param array $artefacts Artefact |
222
|
|
|
* @return array |
223
|
|
|
*/ |
224
|
|
|
protected function createArtefact( |
225
|
|
|
string $sku, |
226
|
|
|
string $qty, |
227
|
|
|
string $price, |
228
|
|
|
string $valueType, |
229
|
|
|
string $website, |
230
|
|
|
string $customerGroup, |
231
|
|
|
array $artefacts |
232
|
|
|
) { |
233
|
|
|
// prepare the artefact we want to export |
234
|
|
|
$artefacts[] = $this->newArtefact( |
235
|
|
|
array( |
236
|
|
|
ColumnKeys::SKU => $sku, |
237
|
|
|
ColumnKeys::TIER_PRICE_QTY => $qty, |
238
|
|
|
ColumnKeys::TIER_PRICE => $price, |
239
|
|
|
ColumnKeys::TIER_PRICE_VALUE_TYPE => $valueType, |
240
|
|
|
ColumnKeys::TIER_PRICE_WEBSITE => $website, |
241
|
|
|
ColumnKeys::TIER_PRICE_CUSTOMER_GROUP => $customerGroup |
242
|
|
|
), |
243
|
|
|
array( |
244
|
|
|
ColumnKeys::SKU => ColumnKeys::SKU, |
245
|
|
|
ColumnKeys::TIER_PRICE_QTY => ColumnKeys::TIER_PRICES, |
246
|
|
|
ColumnKeys::TIER_PRICE => ColumnKeys::TIER_PRICES, |
247
|
|
|
ColumnKeys::TIER_PRICE_VALUE_TYPE => ColumnKeys::TIER_PRICES, |
248
|
|
|
ColumnKeys::TIER_PRICE_WEBSITE => ColumnKeys::TIER_PRICES, |
249
|
|
|
ColumnKeys::TIER_PRICE_CUSTOMER_GROUP => ColumnKeys::TIER_PRICES |
250
|
|
|
) |
251
|
|
|
); |
252
|
|
|
return $artefacts; |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|