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