1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: abstract class Mage_Catalog_Model_Product_Attribute_Backend_Groupprice_Abstract
36: extends Mage_Catalog_Model_Product_Attribute_Backend_Price
37: {
38: 39: 40: 41: 42:
43: protected $_rates;
44:
45: 46: 47: 48: 49: 50:
51: abstract protected function _getDuplicateErrorMessage();
52:
53: 54: 55: 56: 57:
58: protected function _getWebsiteCurrencyRates()
59: {
60: if (is_null($this->_rates)) {
61: $this->_rates = array();
62: $baseCurrency = Mage::app()->getBaseCurrencyCode();
63: foreach (Mage::app()->getWebsites() as $website) {
64:
65: if ($website->getBaseCurrencyCode() != $baseCurrency) {
66: $rate = Mage::getModel('directory/currency')
67: ->load($baseCurrency)
68: ->getRate($website->getBaseCurrencyCode());
69: if (!$rate) {
70: $rate = 1;
71: }
72: $this->_rates[$website->getId()] = array(
73: 'code' => $website->getBaseCurrencyCode(),
74: 'rate' => $rate
75: );
76: } else {
77: $this->_rates[$website->getId()] = array(
78: 'code' => $baseCurrency,
79: 'rate' => 1
80: );
81: }
82: }
83: }
84: return $this->_rates;
85: }
86:
87: 88: 89: 90: 91: 92:
93: protected function _getAdditionalUniqueFields($objectArray)
94: {
95: return array();
96: }
97:
98: 99: 100: 101: 102: 103:
104: protected function _isPriceFixed($priceObject)
105: {
106: return $priceObject->isGroupPriceFixed();
107: }
108:
109: 110: 111: 112: 113: 114: 115:
116: public function validate($object)
117: {
118: $attribute = $this->getAttribute();
119: $priceRows = $object->getData($attribute->getName());
120: if (empty($priceRows)) {
121: return true;
122: }
123:
124:
125: $duplicates = array();
126: foreach ($priceRows as $priceRow) {
127: if (!empty($priceRow['delete'])) {
128: continue;
129: }
130: $compare = join('-', array_merge(
131: array($priceRow['website_id'], $priceRow['cust_group']),
132: $this->_getAdditionalUniqueFields($priceRow)
133: ));
134: if (isset($duplicates[$compare])) {
135: Mage::throwException($this->_getDuplicateErrorMessage());
136: }
137: $duplicates[$compare] = true;
138: }
139:
140:
141:
142: if (!$attribute->isScopeGlobal() && $object->getStoreId()) {
143: $origGroupPrices = $object->getOrigData($attribute->getName());
144: foreach ($origGroupPrices as $price) {
145: if ($price['website_id'] == 0) {
146: $compare = join('-', array_merge(
147: array($price['website_id'], $price['cust_group']),
148: $this->_getAdditionalUniqueFields($price)
149: ));
150: $duplicates[$compare] = true;
151: }
152: }
153: }
154:
155:
156: $baseCurrency = Mage::app()->getBaseCurrencyCode();
157: $rates = $this->_getWebsiteCurrencyRates();
158: foreach ($priceRows as $priceRow) {
159: if (!empty($priceRow['delete'])) {
160: continue;
161: }
162: if ($priceRow['website_id'] == 0) {
163: continue;
164: }
165:
166: $globalCompare = join('-', array_merge(
167: array(0, $priceRow['cust_group']),
168: $this->_getAdditionalUniqueFields($priceRow)
169: ));
170: $websiteCurrency = $rates[$priceRow['website_id']]['code'];
171:
172: if ($baseCurrency == $websiteCurrency && isset($duplicates[$globalCompare])) {
173: Mage::throwException($this->_getDuplicateErrorMessage());
174: }
175: }
176:
177: return true;
178: }
179:
180: 181: 182: 183: 184: 185: 186: 187:
188: public function preparePriceData(array $priceData, $productTypeId, $websiteId)
189: {
190: $rates = $this->_getWebsiteCurrencyRates();
191: $data = array();
192: $price = Mage::getSingleton('catalog/product_type')->priceFactory($productTypeId);
193: foreach ($priceData as $v) {
194: $key = join('-', array_merge(array($v['cust_group']), $this->_getAdditionalUniqueFields($v)));
195: if ($v['website_id'] == $websiteId) {
196: $data[$key] = $v;
197: $data[$key]['website_price'] = $v['price'];
198: } else if ($v['website_id'] == 0 && !isset($data[$key])) {
199: $data[$key] = $v;
200: $data[$key]['website_id'] = $websiteId;
201: if ($this->_isPriceFixed($price)) {
202: $data[$key]['price'] = $v['price'] * $rates[$websiteId]['rate'];
203: $data[$key]['website_price'] = $v['price'] * $rates[$websiteId]['rate'];
204: }
205: }
206: }
207:
208: return $data;
209: }
210:
211: 212: 213: 214: 215: 216:
217: public function afterLoad($object)
218: {
219: $storeId = $object->getStoreId();
220: $websiteId = null;
221: if ($this->getAttribute()->isScopeGlobal()) {
222: $websiteId = 0;
223: } else if ($storeId) {
224: $websiteId = Mage::app()->getStore($storeId)->getWebsiteId();
225: }
226:
227: $data = $this->_getResource()->loadPriceData($object->getId(), $websiteId);
228: foreach ($data as $k => $v) {
229: $data[$k]['website_price'] = $v['price'];
230: if ($v['all_groups']) {
231: $data[$k]['cust_group'] = Mage_Customer_Model_Group::CUST_GROUP_ALL;
232: }
233: }
234:
235: if (!$object->getData('_edit_mode') && $websiteId) {
236: $data = $this->preparePriceData($data, $object->getTypeId(), $websiteId);
237: }
238:
239: $object->setData($this->getAttribute()->getName(), $data);
240: $object->setOrigData($this->getAttribute()->getName(), $data);
241:
242: $valueChangedKey = $this->getAttribute()->getName() . '_changed';
243: $object->setOrigData($valueChangedKey, 0);
244: $object->setData($valueChangedKey, 0);
245:
246: return $this;
247: }
248:
249: 250: 251: 252: 253: 254:
255: public function afterSave($object)
256: {
257: $websiteId = Mage::app()->getStore($object->getStoreId())->getWebsiteId();
258: $isGlobal = $this->getAttribute()->isScopeGlobal() || $websiteId == 0;
259:
260: $priceRows = $object->getData($this->getAttribute()->getName());
261: if (empty($priceRows)) {
262: if ($isGlobal) {
263: $this->_getResource()->deletePriceData($object->getId());
264: } else {
265: $this->_getResource()->deletePriceData($object->getId(), $websiteId);
266: }
267: return $this;
268: }
269:
270: $old = array();
271: $new = array();
272:
273:
274: $origGroupPrices = $object->getOrigData($this->getAttribute()->getName());
275: if (!is_array($origGroupPrices)) {
276: $origGroupPrices = array();
277: }
278: foreach ($origGroupPrices as $data) {
279: if ($data['website_id'] > 0 || ($data['website_id'] == '0' && $isGlobal)) {
280: $key = join('-', array_merge(
281: array($data['website_id'], $data['cust_group']),
282: $this->_getAdditionalUniqueFields($data)
283: ));
284: $old[$key] = $data;
285: }
286: }
287:
288:
289: foreach ($priceRows as $data) {
290: $hasEmptyData = false;
291: foreach ($this->_getAdditionalUniqueFields($data) as $field) {
292: if (empty($field)) {
293: $hasEmptyData = true;
294: break;
295: }
296: }
297:
298: if ($hasEmptyData || !isset($data['cust_group']) || !empty($data['delete'])) {
299: continue;
300: }
301: if ($this->getAttribute()->isScopeGlobal() && $data['website_id'] > 0) {
302: continue;
303: }
304: if (!$isGlobal && (int)$data['website_id'] == 0) {
305: continue;
306: }
307:
308: $key = join('-', array_merge(
309: array($data['website_id'], $data['cust_group']),
310: $this->_getAdditionalUniqueFields($data)
311: ));
312:
313: $useForAllGroups = $data['cust_group'] == Mage_Customer_Model_Group::CUST_GROUP_ALL;
314: $customerGroupId = !$useForAllGroups ? $data['cust_group'] : 0;
315:
316: $new[$key] = array_merge(array(
317: 'website_id' => $data['website_id'],
318: 'all_groups' => $useForAllGroups ? 1 : 0,
319: 'customer_group_id' => $customerGroupId,
320: 'value' => $data['price'],
321: ), $this->_getAdditionalUniqueFields($data));
322: }
323:
324: $delete = array_diff_key($old, $new);
325: $insert = array_diff_key($new, $old);
326: $update = array_intersect_key($new, $old);
327:
328: $isChanged = false;
329: $productId = $object->getId();
330:
331: if (!empty($delete)) {
332: foreach ($delete as $data) {
333: $this->_getResource()->deletePriceData($productId, null, $data['price_id']);
334: $isChanged = true;
335: }
336: }
337:
338: if (!empty($insert)) {
339: foreach ($insert as $data) {
340: $price = new Varien_Object($data);
341: $price->setEntityId($productId);
342: $this->_getResource()->savePriceData($price);
343:
344: $isChanged = true;
345: }
346: }
347:
348: if (!empty($update)) {
349: foreach ($update as $k => $v) {
350: if ($old[$k]['price'] != $v['value']) {
351: $price = new Varien_Object(array(
352: 'value_id' => $old[$k]['price_id'],
353: 'value' => $v['value']
354: ));
355: $this->_getResource()->savePriceData($price);
356:
357: $isChanged = true;
358: }
359: }
360: }
361:
362: if ($isChanged) {
363: $valueChangedKey = $this->getAttribute()->getName() . '_changed';
364: $object->setData($valueChangedKey, 1);
365: }
366:
367: return $this;
368: }
369: }
370: