1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Product\TierPrice\Observers\ClearTierPriceUpdateObserver |
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 Tim Wagner <[email protected]> |
15
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
16
|
|
|
* @link https://github.com/techdivision/import-product-tier-price |
17
|
|
|
* @link https://www.techdivision.com |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace TechDivision\Import\Product\TierPrice\Observers; |
21
|
|
|
|
22
|
|
|
use TechDivision\Import\Product\TierPrice\Utils\ColumnKeys; |
23
|
|
|
use TechDivision\Import\Product\TierPrice\Utils\MemberNames; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Observer for creating/updating/deleting tier prices from the database. |
27
|
|
|
* |
28
|
|
|
* @author Tim Wagner <[email protected]> |
29
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
30
|
|
|
* @link https://github.com/techdivision/import-product-tier-price |
31
|
|
|
* @link https://www.techdivision.com |
32
|
|
|
*/ |
33
|
|
|
trait PrepareTierPriceTrait |
34
|
|
|
{ |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Prepare the attributes of the entity that has to be persisted. |
38
|
|
|
* |
39
|
|
|
* @return array The prepared attributes |
40
|
|
|
*/ |
41
|
|
|
protected function prepareAttributes() |
42
|
|
|
{ |
43
|
|
|
|
44
|
|
|
// load the SKU |
45
|
|
|
$sku = $this->getValue(ColumnKeys::SKU); |
46
|
|
|
|
47
|
|
|
// load tier price quantity and pricde |
48
|
|
|
$qty = $this->getValue(ColumnKeys::TIER_PRICE_QTY); |
49
|
|
|
$price = $this->getValue(ColumnKeys::TIER_PRICE); |
50
|
|
|
|
51
|
|
|
// validate quantity and price |
52
|
|
|
if ($qty == null || $price == null) { |
53
|
|
|
throw new \Exception( |
54
|
|
|
$this->appendExceptionSuffix( |
55
|
|
|
sprintf('Missing tier price qty or price for product with SKU "%s"', $sku) |
56
|
|
|
) |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// validate the value type |
61
|
|
|
if ($this->getValueTypes()->isValueType($valueType = $this->getValue(ColumnKeys::TIER_PRICE_VALUE_TYPE))) { |
62
|
|
|
// map website and customer group code into their IDs |
63
|
|
|
$websiteId = $this->getStoreWebsiteIdByCode($this->getValue(ColumnKeys::TIER_PRICE_WEBSITE)); |
64
|
|
|
$customerGroupId = $this->getCustomerGroupIdByCode($customerGroupCode = $this->getValue(ColumnKeys::TIER_PRICE_CUSTOMER_GROUP)); |
65
|
|
|
|
66
|
|
|
// load the last entity ID |
67
|
|
|
$entityId = $this->getLastEntityId(); |
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) ? (integer) $price : 0.00; |
74
|
|
|
$percentageValue = $this->getValueTypes()->isDiscount($valueType) ? (integer) $price : 0.00; |
75
|
|
|
|
76
|
|
|
// initialize the tier price with the given values |
77
|
|
|
return $this->initializeEntity( |
78
|
|
|
array( |
79
|
|
|
MemberNames::ENTITY_ID => $entityId, |
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
|
|
|
* Resolve's the value with the passed colum name from the actual row. If a callback will |
100
|
|
|
* be passed, the callback will be invoked with the found value as parameter. If |
101
|
|
|
* the value is NULL or empty, the default value will be returned. |
102
|
|
|
* |
103
|
|
|
* @param string $name The name of the column to return the value for |
104
|
|
|
* @param mixed|null $default The default value, that has to be returned, if the row's value is empty |
105
|
|
|
* @param callable|null $callback The callback that has to be invoked on the value, e. g. to format it |
106
|
|
|
* |
107
|
|
|
* @return mixed|null The, almost formatted, value |
108
|
|
|
*/ |
109
|
|
|
abstract public function getValue($name, $default = null, callable $callback = null); |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Return's the ID of the product that has been created recently. |
113
|
|
|
* |
114
|
|
|
* @return string The entity Id |
115
|
|
|
*/ |
116
|
|
|
abstract protected function getLastEntityId(); |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Append's the exception suffix containing filename and line number to the |
120
|
|
|
* passed message. If no message has been passed, only the suffix will be |
121
|
|
|
* returned |
122
|
|
|
* |
123
|
|
|
* @param string|null $message The message to append the exception suffix to |
124
|
|
|
* @param string|null $filename The filename used to create the suffix |
125
|
|
|
* @param string|null $lineNumber The line number used to create the suffx |
126
|
|
|
* |
127
|
|
|
* @return string The message with the appended exception suffix |
128
|
|
|
*/ |
129
|
|
|
abstract protected function appendExceptionSuffix($message = null, $filename = null, $lineNumber = null); |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Returns the tier price value types. |
133
|
|
|
* |
134
|
|
|
* @return \TechDivision\Import\Product\TierPrice\Utils\ValueTypesInterface The tier price value types |
135
|
|
|
*/ |
136
|
|
|
abstract protected function getValueTypes(); |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Initialize's and return's a new entity with the status 'create'. |
140
|
|
|
* |
141
|
|
|
* @param array $attr The attributes to merge into the new entity |
142
|
|
|
* |
143
|
|
|
* @return array The initialized entity |
144
|
|
|
*/ |
145
|
|
|
abstract protected function initializeEntity(array $attr = array()); |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Returns the customer group ID for the given code, if it exists. |
149
|
|
|
* |
150
|
|
|
* @param string $code The code of the requested customer group |
151
|
|
|
* |
152
|
|
|
* @return integer|null The ID of the customer group |
153
|
|
|
*/ |
154
|
|
|
abstract protected function getCustomerGroupIdByCode($code); |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Return's the store website for the passed code. |
158
|
|
|
* |
159
|
|
|
* @param string $code The code of the store website to return the ID for |
160
|
|
|
* |
161
|
|
|
* @return integer The store website ID |
162
|
|
|
* @throws \Exception Is thrown, if the store website with the requested code is not available |
163
|
|
|
*/ |
164
|
|
|
abstract protected function getStoreWebsiteIdByCode($code); |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Queries whether or not the passed customer group code matches all groups or not. |
168
|
|
|
* |
169
|
|
|
* @param string $code The customer group code to query for |
170
|
|
|
* |
171
|
|
|
* @return boolean TRUE if the customer group code matches, else FALSE |
172
|
|
|
*/ |
173
|
|
|
abstract protected function isAllGroups($code); |
174
|
|
|
} |
175
|
|
|
|