00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_Catalog_Model_Product_Attribute_Backend_Tierprice extends Mage_Catalog_Model_Product_Attribute_Backend_Price
00036 {
00037
00038
00039
00040
00041
00042 protected $_rates;
00043
00044
00045
00046
00047
00048
00049 protected function _getResource()
00050 {
00051 return Mage::getResourceSingleton('catalog/product_attribute_backend_tierprice');
00052 }
00053
00054
00055
00056
00057
00058
00059 public function _getWebsiteRates()
00060 {
00061 if (is_null($this->_rates)) {
00062 $this->_rates = array();
00063 $baseCurrency = Mage::app()->getBaseCurrencyCode();
00064 foreach (Mage::app()->getWebsites() as $website) {
00065
00066 if ($website->getBaseCurrencyCode() != $baseCurrency) {
00067 $rate = Mage::getModel('directory/currency')
00068 ->load($baseCurrency)
00069 ->getRate($website->getBaseCurrencyCode());
00070 if (!$rate) {
00071 $rate = 1;
00072 }
00073 $this->_rates[$website->getId()] = array(
00074 'code' => $website->getBaseCurrencyCode(),
00075 'rate' => $rate
00076 );
00077 }
00078 else {
00079 $this->_rates[$website->getId()] = array(
00080 'code' => $baseCurrency,
00081 'rate' => 1
00082 );
00083 }
00084 }
00085 }
00086 return $this->_rates;
00087 }
00088
00089
00090
00091
00092
00093
00094
00095 public function validate($object)
00096 {
00097 $tiers = $object->getData($this->getAttribute()->getName());
00098 if (empty($tiers)) {
00099 return $this;
00100 }
00101
00102
00103 $duplicates = array();
00104 foreach ($tiers as $tier) {
00105 if (!empty($tier['delete'])) {
00106 continue;
00107 }
00108 $compare = join('-', array($tier['website_id'], $tier['cust_group'], $tier['price_qty']));
00109 if (isset($duplicates[$compare])) {
00110 Mage::throwException(
00111 Mage::helper('catalog')->__('Duplicate website tier price customer group and quantity.')
00112 );
00113 }
00114 $duplicates[$compare] = true;
00115 }
00116
00117
00118 $baseCurrency = Mage::app()->getBaseCurrencyCode();
00119 $rates = $this->_getWebsiteRates();
00120 foreach ($tiers as $tier) {
00121 if (!empty($tier['delete'])) {
00122 continue;
00123 }
00124 if ($tier['website_id'] == 0) {
00125 continue;
00126 }
00127
00128 $compare = join('-', array($tier['website_id'], $tier['cust_group'], $tier['price_qty']));
00129 $globalCompare = join('-', array(0, $tier['cust_group'], $tier['price_qty']));
00130 $websiteCurrency = $rates[$tier['website_id']]['code'];
00131
00132 if ($baseCurrency == $websiteCurrency && isset($duplicates[$globalCompare])) {
00133 Mage::throwException(
00134 Mage::helper('catalog')->__('Duplicate website tier price customer group and quantity.')
00135 );
00136 }
00137 }
00138 return $this;
00139 }
00140
00141
00142
00143
00144
00145
00146
00147 public function afterLoad($object)
00148 {
00149 $data = $this->_getResource()
00150 ->loadProductPrices($object, $this->getAttribute());
00151 foreach ($data as $k => $v) {
00152 if ($v['all_groups']) {
00153 $data[$k]['cust_group'] = Mage_Customer_Model_Group::CUST_GROUP_ALL;
00154 $data[$k]['website_price'] = $v['price'];
00155 }
00156 }
00157
00158 if (!$object->getData('_edit_mode')
00159 && $this->getAttribute()->getIsGlobal() == Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_WEBSITE
00160 && ($storeId = $object->getStoreId()))
00161 {
00162 $websiteId = Mage::app()->getStore($storeId)->getWebsiteId();
00163 $rates = $this->_getWebsiteRates();
00164
00165 $full = $data;
00166 $data = array();
00167 foreach ($full as $v) {
00168 $key = join('-', array($v['cust_group'], $v['price_qty']));
00169 if ($v['website_id'] == $websiteId) {
00170 $data[$key] = $v;
00171 $data[$key]['website_price'] = $v['price'];
00172 }
00173 elseif ($v['website_id'] == 0 && !isset($data[$key])) {
00174 $data[$key] = $v;
00175 $data[$key]['website_id'] = $websiteId;
00176 $data[$key]['price'] = $v['price'] * $rates[$websiteId]['rate'];
00177 $data[$key]['website_price'] = $v['price'] * $rates[$websiteId]['rate'];
00178 }
00179 }
00180 }
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203 $object->setData($this->getAttribute()->getName(), $data);
00204 return $this;
00205 }
00206
00207
00208
00209
00210
00211
00212
00213 public function afterSave($object)
00214 {
00215 $this->_getResource()->deleteProductPrices($object, $this->getAttribute());
00216 $tierPrices = $object->getData($this->getAttribute()->getName());
00217
00218 if (!is_array($tierPrices)) {
00219 return $this;
00220 }
00221
00222 $prices = array();
00223 foreach ($tierPrices as $tierPrice) {
00224 if (empty($tierPrice['price_qty']) || !isset($tierPrice['price']) || !empty($tierPrice['delete'])) {
00225 continue;
00226 }
00227
00228 $useForAllGroups = $tierPrice['cust_group'] == Mage_Customer_Model_Group::CUST_GROUP_ALL;
00229 $customerGroupId = !$useForAllGroups ? $tierPrice['cust_group'] : 0;
00230 $priceKey = join('-', array(
00231 $tierPrice['website_id'],
00232 intval($useForAllGroups),
00233 $customerGroupId,
00234 $tierPrice['price_qty']
00235 ));
00236
00237 $prices[$priceKey] = array(
00238 'website_id' => $tierPrice['website_id'],
00239 'all_groups' => intval($useForAllGroups),
00240 'customer_group_id' => $customerGroupId,
00241 'qty' => $tierPrice['price_qty'],
00242 'value' => $tierPrice['price'],
00243 );
00244 }
00245
00246 if ($this->getAttribute()->getIsGlobal() == Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_WEBSITE) {
00247 if ($storeId = $object->getStoreId()) {
00248 $websites = array(Mage::app()->getStore($storeId)->getWebsite());
00249 }
00250 else {
00251 $websites = Mage::app()->getWebsites();
00252 }
00253
00254 $baseCurrency = Mage::app()->getBaseCurrencyCode();
00255 $rates = $this->_getWebsiteRates();
00256 foreach ($websites as $website) {
00257
00258 if (!is_array($object->getWebsiteIds()) || !in_array($website->getId(), $object->getWebsiteIds())) {
00259 continue;
00260 }
00261 if ($rates[$website->getId()]['code'] != $baseCurrency) {
00262 foreach ($prices as $data) {
00263 $priceKey = join('-', array(
00264 $website->getId(),
00265 $data['all_groups'],
00266 $data['customer_group_id'],
00267 $data['qty']
00268 ));
00269 if (!isset($prices[$priceKey])) {
00270 $prices[$priceKey] = $data;
00271 $prices[$priceKey]['website_id'] = $website->getId();
00272 $prices[$priceKey]['value'] = $data['value'] * $rates[$website->getId()]['rate'];
00273 }
00274 }
00275 }
00276 }
00277 }
00278
00279 foreach ($prices as $data) {
00280 $this->_getResource()->insertProductPrice($object, $data);
00281 }
00282
00283 return $this;
00284 }
00285
00286
00287
00288
00289
00290
00291
00292 public function afterDelete($object)
00293 {
00294 $this->_getResource()->deleteProductPrices($object, $this->getAttribute());
00295 return $this;
00296 }
00297 }