1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Product\TierPrice\Observers\TierPriceUpdateObserver |
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\Product\Services\ProductBunchProcessorInterface; |
20
|
|
|
use TechDivision\Import\Product\TierPrice\Utils\MemberNames; |
21
|
|
|
use TechDivision\Import\Product\TierPrice\Utils\ValueTypesInterface; |
22
|
|
|
use TechDivision\Import\Product\TierPrice\Services\TierPriceProcessorInterface; |
23
|
|
|
use TechDivision\Import\Product\TierPrice\Utils\ColumnKeys; |
24
|
|
|
use TechDivision\Import\Product\TierPrice\Utils\DefaultCodes; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Observer for creating/updating/deleting tier prices from the database. |
28
|
|
|
* |
29
|
|
|
* @author Klaas-Tido Rühl <[email protected]> |
30
|
|
|
* @author Tim Wagner <[email protected]> |
31
|
|
|
* @copyright 2019 REFUSiON GmbH <[email protected]> |
32
|
|
|
* @license https://opensource.org/licenses/MIT |
33
|
|
|
* @link https://github.com/techdivision/import-product-tier-price |
34
|
|
|
* @link https://www.techdivision.com |
35
|
|
|
* @link https://www.refusion.com |
36
|
|
|
*/ |
37
|
|
|
class TierPriceObserver extends AbstractProductTierPriceObserver |
38
|
|
|
{ |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The trait that prepares the tier price data. |
42
|
|
|
* |
43
|
|
|
* @var \TechDivision\Import\Product\TierPrice\Observers\PrepareTierPriceTrait |
44
|
|
|
*/ |
45
|
|
|
use PrepareTierPriceTrait; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The available tier price value types. |
49
|
|
|
* |
50
|
|
|
* @var \TechDivision\Import\Product\TierPrice\Utils\ValueTypesInterface |
51
|
|
|
*/ |
52
|
|
|
protected $valueTypes; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var ProductBunchProcessorInterface |
56
|
|
|
*/ |
57
|
|
|
protected $productBunchProcessor; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Initialize the observer with the passed product tier price processor instance. |
61
|
|
|
* |
62
|
|
|
* @param \TechDivision\Import\Product\TierPrice\Services\TierPriceProcessorInterface $tierPriceProcessor The processor instance |
63
|
|
|
* @param \TechDivision\Import\Product\TierPrice\Utils\ValueTypesInterface $valueTypes The tier price value types |
64
|
|
|
* @param \TechDivision\Import\Product\Services\ProductBunchProcessorInterface $productBunchProcessor The product processor instance |
65
|
|
|
*/ |
66
|
|
|
public function __construct( |
67
|
|
|
TierPriceProcessorInterface $tierPriceProcessor, |
68
|
|
|
ValueTypesInterface $valueTypes, |
69
|
|
|
ProductBunchProcessorInterface $productBunchProcessor |
70
|
|
|
) { |
71
|
|
|
// set the value types |
72
|
|
|
$this->valueTypes = $valueTypes; |
73
|
|
|
$this->productBunchProcessor = $productBunchProcessor; |
74
|
|
|
|
75
|
|
|
// pass the tier price processor through to the parent instance |
76
|
|
|
parent::__construct($tierPriceProcessor); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Returns the tier price value types. |
81
|
|
|
* |
82
|
|
|
* @return \TechDivision\Import\Product\TierPrice\Utils\ValueTypesInterface The tier price value types |
83
|
|
|
*/ |
84
|
|
|
protected function getValueTypes() |
85
|
|
|
{ |
86
|
|
|
return $this->valueTypes; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Return's the product bunch processor instance. |
91
|
|
|
* |
92
|
|
|
* @return ProductBunchProcessorInterface The product bunch processor instance |
93
|
|
|
*/ |
94
|
|
|
protected function getProductBunchProcessor() |
95
|
|
|
{ |
96
|
|
|
return $this->productBunchProcessor; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Process the observer's business logic. |
101
|
|
|
* |
102
|
|
|
* @return void |
103
|
|
|
* @throws \Exception |
104
|
|
|
*/ |
105
|
|
|
protected function process() |
106
|
|
|
{ |
107
|
|
|
|
108
|
|
|
try { |
109
|
|
|
// intialize the tier price data |
110
|
|
|
$tierPriceData = $this->initializeTierPrice($this->prepareAttributes()); |
111
|
|
|
|
112
|
|
|
if ($tierPriceData['website_id'] === 0) { |
113
|
|
|
$this->addTierPriceDataToPkMapping($tierPriceData); |
114
|
|
|
} else { |
115
|
|
|
$productWebsiteData = $this->getProductBunchProcessor()->loadProductWebsitesBySku( |
116
|
|
|
$this->getValue(ColumnKeys::SKU) |
117
|
|
|
); |
118
|
|
|
$found = false; |
119
|
|
|
foreach ($productWebsiteData as $productWebsite) { |
120
|
|
|
if ($tierPriceData['website_id'] == $productWebsite['website_id']) { |
121
|
|
|
$found = true; |
122
|
|
|
// persist the tier price and mark it as processed |
123
|
|
|
$this->addTierPriceDataToPkMapping($tierPriceData); |
124
|
|
|
break; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
if (!$found) { |
128
|
|
|
$this->getSubject()->getSystemLogger()->warning( |
129
|
|
|
sprintf( |
130
|
|
|
"The Product with the SKU %s has not assigned to the Website %s", |
131
|
|
|
$this->getValue(ColumnKeys::SKU), |
132
|
|
|
$tierPriceData['website_id'] |
133
|
|
|
) |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} catch (\Exception $e) { |
138
|
|
|
// query whether or not we're in debug mode |
139
|
|
|
if ($this->getSubject()->isDebugMode()) { |
140
|
|
|
$this->getSubject()->getSystemLogger()->warning($e->getMessage()); |
141
|
|
|
$this->skipRow(); |
142
|
|
|
} else { |
143
|
|
|
// throw the exception agatin in strict mode |
144
|
|
|
throw $e; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/**s |
150
|
|
|
* Initialize the product website with the passed attributes and returns an instance. |
151
|
|
|
* |
152
|
|
|
* @param array $attr The product website attributes |
153
|
|
|
* |
154
|
|
|
* @return array The initialized product website |
155
|
|
|
* @throws \RuntimeException Is thrown, if the attributes can not be initialized |
156
|
|
|
*/ |
157
|
|
|
protected function initializeTierPrice(array $attr) |
158
|
|
|
{ |
159
|
|
|
return $attr; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Loads and returns the product with the passed SKU. |
164
|
|
|
* |
165
|
|
|
* @param string $sku The SKU of the product to load |
166
|
|
|
* |
167
|
|
|
* @return array The product |
168
|
|
|
*/ |
169
|
|
|
protected function loadProduct($sku) |
170
|
|
|
{ |
171
|
|
|
return $this->getTierPriceProcessor()->loadProduct($sku); |
|
|
|
|
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Persists the tier price with the passed data. |
176
|
|
|
* |
177
|
|
|
* @param array $row The tier price to persist |
178
|
|
|
* @param string|null $name The name of the prepared statement that has to be executed |
179
|
|
|
* |
180
|
|
|
* @return string The ID of the persisted entity |
181
|
|
|
*/ |
182
|
|
|
protected function persistTierPrice(array $row, $name = null) |
183
|
|
|
{ |
184
|
|
|
return $this->getTierPriceProcessor()->persistTierPrice($row, $name); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Add the ID of the processed tier price. |
189
|
|
|
* |
190
|
|
|
* @param integer $valueId The ID of the processed tier price |
191
|
|
|
* @param integer $entityId The entity ID of the related product |
192
|
|
|
* |
193
|
|
|
* @return void |
194
|
|
|
*/ |
195
|
|
|
protected function addProcessedTierPrice($valueId, $entityId) |
196
|
|
|
{ |
197
|
|
|
$this->getSubject()->addProcessedTierPrice($valueId, $entityId); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Returns the customer group ID for the given code, if it exists. |
202
|
|
|
* |
203
|
|
|
* @param string $code The code of the requested customer group |
204
|
|
|
* |
205
|
|
|
* @return integer|null The ID of the customer group |
206
|
|
|
*/ |
207
|
|
|
protected function getCustomerGroupIdByCode($code) |
208
|
|
|
{ |
209
|
|
|
return $this->getSubject()->getCustomerGroupIdByCode($code); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Return's the store website for the passed code. |
214
|
|
|
* |
215
|
|
|
* @param string $code The code of the store website to return the ID for |
216
|
|
|
* |
217
|
|
|
* @return integer The store website ID |
218
|
|
|
* @throws \Exception Is thrown, if the store website with the requested code is not available |
219
|
|
|
*/ |
220
|
|
|
protected function getStoreWebsiteIdByCode($code) |
221
|
|
|
{ |
222
|
|
|
return $this->getSubject()->getStoreWebsiteIdByCode($code); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Query whether or not the passed value type is valid. |
227
|
|
|
* |
228
|
|
|
* @param string $valueType The value type to query for |
229
|
|
|
* |
230
|
|
|
* @return boolean TRUE if the value type is valid, else FALSE |
231
|
|
|
*/ |
232
|
|
|
protected function isValueType($valueType) |
233
|
|
|
{ |
234
|
|
|
return $this->getValueTypes()->isValueType($valueType); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Queries whether or not the passed customer group code matches all groups or not. |
239
|
|
|
* |
240
|
|
|
* @param string $code The customer group code to query for |
241
|
|
|
* |
242
|
|
|
* @return boolean TRUE if the customer group code matches, else FALSE |
243
|
|
|
*/ |
244
|
|
|
protected function isAllGroups($code) |
245
|
|
|
{ |
246
|
|
|
return $this->getSubject()->isAllGroups($code); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Persist the tier price and mark it as processed |
251
|
|
|
* |
252
|
|
|
* @param array $tierPriceData TierPriceData |
253
|
|
|
* |
254
|
|
|
* @return void |
255
|
|
|
*/ |
256
|
|
|
protected function addTierPriceDataToPkMapping(array $tierPriceData) |
257
|
|
|
{ |
258
|
|
|
$this->addProcessedTierPrice( |
259
|
|
|
$this->persistTierPrice($tierPriceData), |
|
|
|
|
260
|
|
|
$pk = $tierPriceData[$this->getPrimaryKeyMemberName()] |
261
|
|
|
); |
262
|
|
|
$this->addSkuToPkMapping($this->getValue(ColumnKeys::SKU), $pk); |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|