|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Product\TierPrice\Observers\ClearTierPriceUpdateObserver |
|
5
|
|
|
* |
|
6
|
|
|
* PHP version 7 |
|
7
|
|
|
* |
|
8
|
|
|
* @author Tim Wagner <[email protected]> |
|
9
|
|
|
* @license https://opensource.org/licenses/MIT |
|
10
|
|
|
* @link https://github.com/techdivision/import-product-tier-price |
|
11
|
|
|
* @link https://www.techdivision.com |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace TechDivision\Import\Product\TierPrice\Observers; |
|
15
|
|
|
|
|
16
|
|
|
use TechDivision\Import\Product\TierPrice\Utils\ColumnKeys; |
|
17
|
|
|
use TechDivision\Import\Product\TierPrice\Utils\MemberNames; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Observer for creating/updating/deleting tier prices from the database. |
|
21
|
|
|
* |
|
22
|
|
|
* @author Tim Wagner <[email protected]> |
|
23
|
|
|
* @license https://opensource.org/licenses/MIT |
|
24
|
|
|
* @link https://github.com/techdivision/import-product-tier-price |
|
25
|
|
|
* @link https://www.techdivision.com |
|
26
|
|
|
*/ |
|
27
|
|
|
trait PrepareTierPriceTrait |
|
28
|
|
|
{ |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Prepare the attributes of the entity that has to be persisted. |
|
32
|
|
|
* |
|
33
|
|
|
* @return array The prepared attributes |
|
34
|
|
|
* @throws \Exception Is thrown, if the product with the given SKU can not be loaded |
|
35
|
|
|
*/ |
|
36
|
|
|
protected function prepareAttributes() |
|
37
|
|
|
{ |
|
38
|
|
|
|
|
39
|
|
|
// try to load the entity ID for the product with the passed SKU |
|
40
|
|
|
if ($product = $this->loadProduct($sku = $this->getValue(ColumnKeys::SKU))) { |
|
41
|
|
|
$this->setLastPk($pk = $product[$this->getPrimaryKeyMemberName()]); |
|
42
|
|
|
} else { |
|
43
|
|
|
throw new \Exception( |
|
44
|
|
|
$this->appendExceptionSuffix( |
|
45
|
|
|
sprintf('Product with SKU "%s" can\'t be loaded to create tier price for', $sku) |
|
46
|
|
|
) |
|
47
|
|
|
); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
// load tier price quantity and pricde |
|
51
|
|
|
$qty = $this->getValue(ColumnKeys::TIER_PRICE_QTY); |
|
52
|
|
|
$price = $this->getValue(ColumnKeys::TIER_PRICE); |
|
53
|
|
|
|
|
54
|
|
|
// validate quantity and price |
|
55
|
|
|
if ($qty == null || $price == null) { |
|
56
|
|
|
throw new \Exception( |
|
57
|
|
|
$this->appendExceptionSuffix( |
|
58
|
|
|
sprintf('Missing tier price qty or price for product with SKU "%s"', $sku) |
|
59
|
|
|
) |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
// validate the value type |
|
64
|
|
|
if ($this->getValueTypes()->isValueType($valueType = $this->getValue(ColumnKeys::TIER_PRICE_VALUE_TYPE))) { |
|
65
|
|
|
// map website and customer group code into their IDs |
|
66
|
|
|
$websiteId = $this->getStoreWebsiteIdByCode($this->getValue(ColumnKeys::TIER_PRICE_WEBSITE)); |
|
67
|
|
|
$customerGroupId = $this->getCustomerGroupIdByCode($customerGroupCode = $this->getValue(ColumnKeys::TIER_PRICE_CUSTOMER_GROUP)); |
|
68
|
|
|
|
|
69
|
|
|
// query whether or not the tier price is valid for ALL GROUPS |
|
70
|
|
|
$allGroups = (integer) $this->isAllGroups($customerGroupCode); |
|
71
|
|
|
|
|
72
|
|
|
// initialize the (percentage) value |
|
73
|
|
|
$value = $this->getValueTypes()->isFixed($valueType) ? (double) $price : 0.00; |
|
74
|
|
|
$percentageValue = $this->getValueTypes()->isDiscount($valueType) ? (integer) $price : null; |
|
75
|
|
|
|
|
76
|
|
|
// initialize the tier price with the given values |
|
77
|
|
|
return $this->initializeEntity( |
|
78
|
|
|
array( |
|
79
|
|
|
$this->getPrimaryKeyMemberName() => $pk, |
|
80
|
|
|
MemberNames::ALL_GROUPS => $allGroups, |
|
81
|
|
|
MemberNames::CUSTOMER_GROUP_ID => $customerGroupId, |
|
82
|
|
|
MemberNames::QTY => $qty, |
|
83
|
|
|
MemberNames::VALUE => $value, |
|
84
|
|
|
MemberNames::WEBSITE_ID => $websiteId, |
|
85
|
|
|
MemberNames::PERCENTAGE_VALUE => $percentageValue |
|
86
|
|
|
) |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// throw an exception for invalid tier price value types |
|
91
|
|
|
throw new \Exception( |
|
92
|
|
|
$this->appendExceptionSuffix( |
|
93
|
|
|
sprintf('Invalid value type "%s" for product with SKU "%s"', $valueType, $sku) |
|
94
|
|
|
) |
|
95
|
|
|
); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Load's and return's the product with the passed SKU. |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $sku The SKU of the product to load |
|
102
|
|
|
* |
|
103
|
|
|
* @return array The product |
|
104
|
|
|
*/ |
|
105
|
|
|
abstract protected function loadProduct($sku); |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Resolve's the value with the passed colum name from the actual row. If a callback will |
|
109
|
|
|
* be passed, the callback will be invoked with the found value as parameter. If |
|
110
|
|
|
* the value is NULL or empty, the default value will be returned. |
|
111
|
|
|
* |
|
112
|
|
|
* @param string $name The name of the column to return the value for |
|
113
|
|
|
* @param mixed|null $default The default value, that has to be returned, if the row's value is empty |
|
114
|
|
|
* @param callable|null $callback The callback that has to be invoked on the value, e. g. to format it |
|
115
|
|
|
* |
|
116
|
|
|
* @return mixed|null The, almost formatted, value |
|
117
|
|
|
*/ |
|
118
|
|
|
abstract public function getValue($name, $default = null, callable $callback = null); |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Append's the exception suffix containing filename and line number to the |
|
122
|
|
|
* passed message. If no message has been passed, only the suffix will be |
|
123
|
|
|
* returned |
|
124
|
|
|
* |
|
125
|
|
|
* @param string|null $message The message to append the exception suffix to |
|
126
|
|
|
* @param string|null $filename The filename used to create the suffix |
|
127
|
|
|
* @param string|null $lineNumber The line number used to create the suffx |
|
128
|
|
|
* |
|
129
|
|
|
* @return string The message with the appended exception suffix |
|
130
|
|
|
*/ |
|
131
|
|
|
abstract protected function appendExceptionSuffix($message = null, $filename = null, $lineNumber = null); |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Returns the tier price value types. |
|
135
|
|
|
* |
|
136
|
|
|
* @return \TechDivision\Import\Product\TierPrice\Utils\ValueTypesInterface The tier price value types |
|
137
|
|
|
*/ |
|
138
|
|
|
abstract protected function getValueTypes(); |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Initialize's and return's a new entity with the status 'create'. |
|
142
|
|
|
* |
|
143
|
|
|
* @param array $attr The attributes to merge into the new entity |
|
144
|
|
|
* |
|
145
|
|
|
* @return array The initialized entity |
|
146
|
|
|
*/ |
|
147
|
|
|
abstract protected function initializeEntity(array $attr = array()); |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Returns the customer group ID for the given code, if it exists. |
|
151
|
|
|
* |
|
152
|
|
|
* @param string $code The code of the requested customer group |
|
153
|
|
|
* |
|
154
|
|
|
* @return integer|null The ID of the customer group |
|
155
|
|
|
*/ |
|
156
|
|
|
abstract protected function getCustomerGroupIdByCode($code); |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Return's the store website for the passed code. |
|
160
|
|
|
* |
|
161
|
|
|
* @param string $code The code of the store website to return the ID for |
|
162
|
|
|
* |
|
163
|
|
|
* @return integer The store website ID |
|
164
|
|
|
* @throws \Exception Is thrown, if the store website with the requested code is not available |
|
165
|
|
|
*/ |
|
166
|
|
|
abstract protected function getStoreWebsiteIdByCode($code); |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Queries whether or not the passed customer group code matches all groups or not. |
|
170
|
|
|
* |
|
171
|
|
|
* @param string $code The customer group code to query for |
|
172
|
|
|
* |
|
173
|
|
|
* @return boolean TRUE if the customer group code matches, else FALSE |
|
174
|
|
|
*/ |
|
175
|
|
|
abstract protected function isAllGroups($code); |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Set's the ID of the last PK used. |
|
179
|
|
|
* |
|
180
|
|
|
* @param string $pk The PK |
|
181
|
|
|
* |
|
182
|
|
|
* @return void |
|
183
|
|
|
*/ |
|
184
|
|
|
abstract protected function setLastPk($pk); |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Returns the primary key member name for the actual Magento edition. |
|
188
|
|
|
* |
|
189
|
|
|
* @return string The primary key member name |
|
190
|
|
|
* @see \TechDivision\Import\Product\TierPrice\Services\TierPriceProcessorInterface::getPrimaryKeyMemberName() |
|
191
|
|
|
*/ |
|
192
|
|
|
abstract protected function getPrimaryKeyMemberName(); |
|
193
|
|
|
} |
|
194
|
|
|
|