|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Product\TierPrice\Observers\ClearTierPriceObserver |
|
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\MemberNames; |
|
23
|
|
|
use TechDivision\Import\Product\TierPrice\Utils\ValueTypesInterface; |
|
24
|
|
|
use TechDivision\Import\Product\TierPrice\Services\TierPriceProcessorInterface; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Observer for deleting tier prices from the database. |
|
28
|
|
|
* |
|
29
|
|
|
* @author Tim Wagner <[email protected]> |
|
30
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
31
|
|
|
* @link https://github.com/techdivision/import-product-tier-price |
|
32
|
|
|
* @link https://www.techdivision.com |
|
33
|
|
|
*/ |
|
34
|
|
|
class ClearTierPriceObserver extends AbstractProductTierPriceObserver |
|
35
|
|
|
{ |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The trait that prepares the tier price data. |
|
39
|
|
|
* |
|
40
|
|
|
* @var \TechDivision\Import\Product\TierPrice\Observers\PrepareTierPriceTrait |
|
41
|
|
|
*/ |
|
42
|
|
|
use PrepareTierPriceTrait; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* The available tier price value types. |
|
46
|
|
|
* |
|
47
|
|
|
* @var \TechDivision\Import\Product\TierPrice\Utils\ValueTypesInterface |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $valueTypes; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Initialize the observer with the passed product tier price processor instance. |
|
53
|
|
|
* |
|
54
|
|
|
* @param \TechDivision\Import\Product\TierPrice\Services\TierPriceProcessorInterface $tierPriceProcessor The processor instance |
|
55
|
|
|
* @param \TechDivision\Import\Product\TierPrice\Utils\ValueTypesInterface $valueTypes The tier price value types |
|
56
|
|
|
*/ |
|
57
|
|
|
public function __construct(TierPriceProcessorInterface $tierPriceProcessor, ValueTypesInterface $valueTypes) |
|
58
|
|
|
{ |
|
59
|
|
|
|
|
60
|
|
|
// pass the tier price processor through to the parent instance |
|
61
|
|
|
parent::__construct($tierPriceProcessor); |
|
62
|
|
|
|
|
63
|
|
|
// set the value types |
|
64
|
|
|
$this->valueTypes = $valueTypes; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Returns the tier price value types. |
|
69
|
|
|
* |
|
70
|
|
|
* @return \TechDivision\Import\Product\TierPrice\Utils\ValueTypesInterface The tier price value types |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function getValueTypes() |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->valueTypes; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Process the observer's business logic. |
|
79
|
|
|
* |
|
80
|
|
|
* @return void |
|
81
|
|
|
* @throws \Exception |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function process() |
|
84
|
|
|
{ |
|
85
|
|
|
|
|
86
|
|
|
// prepare the tier price attributes of the actual row |
|
87
|
|
|
$tierPrice = $this->prepareAttributes(); |
|
88
|
|
|
|
|
89
|
|
|
// load the values for loading the actual tier price |
|
90
|
|
|
$pk = $tierPrice[$this->getPrimaryKeyMemberName()]; |
|
91
|
|
|
$qty = $tierPrice[MemberNames::QTY]; |
|
92
|
|
|
$allGroups = $tierPrice[MemberNames::ALL_GROUPS]; |
|
93
|
|
|
$websiteId = $tierPrice[MemberNames::WEBSITE_ID]; |
|
94
|
|
|
$customerGroupId = $tierPrice[MemberNames::CUSTOMER_GROUP_ID]; |
|
95
|
|
|
|
|
96
|
|
|
// delete the tier price if available |
|
97
|
|
|
if ($tierPrice = $this->loadTierPriceByPkAndAllGroupsAndCustomerGroupIdAndQtyAndWebsiteId($pk, $allGroups, $customerGroupId, $qty, $websiteId)) { |
|
98
|
|
|
$this->deleteTierPrice(array(MemberNames::VALUE_ID => $tierPrice[MemberNames::VALUE_ID])); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Loads and returns the product with the passed SKU. |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $sku The SKU of the product to load |
|
106
|
|
|
* |
|
107
|
|
|
* @return array The product |
|
108
|
|
|
*/ |
|
109
|
|
|
protected function loadProduct($sku) |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->getTierPriceProcessor()->loadProduct($sku); |
|
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Returns the tier price with the given parameters. |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $pk The PK of the product relation |
|
118
|
|
|
* @param integer $allGroups The flag if all groups are affected or not |
|
119
|
|
|
* @param integer $customerGroupId The customer group ID |
|
120
|
|
|
* @param integer $qty The tier price quantity |
|
121
|
|
|
* @param integer $websiteId The website ID the tier price is related to |
|
122
|
|
|
* |
|
123
|
|
|
* @return array The tier price |
|
124
|
|
|
*/ |
|
125
|
|
|
protected function loadTierPriceByPkAndAllGroupsAndCustomerGroupIdAndQtyAndWebsiteId($pk, $allGroups, $customerGroupId, $qty, $websiteId) |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->getTierPriceProcessor()->loadTierPriceByPkAndAllGroupsAndCustomerGroupIdAndQtyAndWebsiteId($pk, $allGroups, $customerGroupId, $qty, $websiteId); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Deletes the tierprice with the passed attributes. |
|
132
|
|
|
* |
|
133
|
|
|
* @param array $row The attributes of the entity to delete |
|
134
|
|
|
* @param string|null $name The name of the prepared statement that has to be executed |
|
135
|
|
|
* |
|
136
|
|
|
* @return void |
|
137
|
|
|
*/ |
|
138
|
|
|
protected function deleteTierPrice($row, $name = null) |
|
139
|
|
|
{ |
|
140
|
|
|
$this->getTierPriceProcessor()->deleteTierPrice($row, $name); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Returns the customer group ID for the given code, if it exists. |
|
145
|
|
|
* |
|
146
|
|
|
* @param string $code The code of the requested customer group |
|
147
|
|
|
* |
|
148
|
|
|
* @return integer|null The ID of the customer group |
|
149
|
|
|
*/ |
|
150
|
|
|
protected function getCustomerGroupIdByCode($code) |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->getSubject()->getCustomerGroupIdByCode($code); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Return's the store website for the passed code. |
|
157
|
|
|
* |
|
158
|
|
|
* @param string $code The code of the store website to return the ID for |
|
159
|
|
|
* |
|
160
|
|
|
* @return integer The store website ID |
|
161
|
|
|
* @throws \Exception Is thrown, if the store website with the requested code is not available |
|
162
|
|
|
*/ |
|
163
|
|
|
protected function getStoreWebsiteIdByCode($code) |
|
164
|
|
|
{ |
|
165
|
|
|
return $this->getSubject()->getStoreWebsiteIdByCode($code); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Query whether or not the passed value type is valid. |
|
170
|
|
|
* |
|
171
|
|
|
* @param string $valueType The value type to query for |
|
172
|
|
|
* |
|
173
|
|
|
* @return boolean TRUE if the value type is valid, else FALSE |
|
174
|
|
|
*/ |
|
175
|
|
|
protected function isValueType($valueType) |
|
176
|
|
|
{ |
|
177
|
|
|
return $this->getValueTypes()->isValueType($valueType); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Queries whether or not the passed customer group code matches all groups or not. |
|
182
|
|
|
* |
|
183
|
|
|
* @param string $code The customer group code to query for |
|
184
|
|
|
* |
|
185
|
|
|
* @return boolean TRUE if the customer group code matches, else FALSE |
|
186
|
|
|
*/ |
|
187
|
|
|
protected function isAllGroups($code) |
|
188
|
|
|
{ |
|
189
|
|
|
return $this->getSubject()->isAllGroups($code); |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|