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