Mage_Catalog_Model_Product_Attribute_Backend_Tierprice Class Reference

Inheritance diagram for Mage_Catalog_Model_Product_Attribute_Backend_Tierprice:

Mage_Catalog_Model_Product_Attribute_Backend_Price Mage_Eav_Model_Entity_Attribute_Backend_Abstract Mage_Eav_Model_Entity_Attribute_Backend_Interface

List of all members.

Public Member Functions

 _getWebsiteRates ()
 validate ($object)
 afterLoad ($object)
 afterSave ($object)
 afterDelete ($object)

Protected Member Functions

 _getResource ()

Protected Attributes

 $_rates


Detailed Description

Definition at line 35 of file Tierprice.php.


Member Function Documentation

_getResource (  )  [protected]

Retrieve resource model

Returns:
Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Attribute_Backend_Tierprice

Definition at line 49 of file Tierprice.php.

00050     {
00051         return Mage::getResourceSingleton('catalog/product_attribute_backend_tierprice');
00052     }

_getWebsiteRates (  ) 

Retrieve websites rates and base currency codes

Returns:
array

Definition at line 59 of file Tierprice.php.

00060     {
00061         if (is_null($this->_rates)) {
00062             $this->_rates = array();
00063             $baseCurrency = Mage::app()->getBaseCurrencyCode();
00064             foreach (Mage::app()->getWebsites() as $website) {
00065                 /* @var $website Mage_Core_Model_Website */
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     }

afterDelete ( object  ) 

After delete object remove additional data

Parameters:
Mage_Catalog_Model_Product $object
Returns:
Mage_Catalog_Model_Product_Attribute_Backend_Tierprice

Reimplemented from Mage_Eav_Model_Entity_Attribute_Backend_Abstract.

Definition at line 292 of file Tierprice.php.

00293     {
00294         $this->_getResource()->deleteProductPrices($object, $this->getAttribute());
00295         return $this;
00296     }

afterLoad ( object  ) 

Assign tier prices to product data

Parameters:
Mage_Catalog_Model_Product $object
Returns:
Mage_Catalog_Model_Product_Attribute_Backend_Tierprice

Remove tier price if rate not available

Reimplemented from Mage_Eav_Model_Entity_Attribute_Backend_Abstract.

Definition at line 147 of file Tierprice.php.

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 //        foreach ($data as $i => $row) {
00183 //            if (!empty($row['all_groups'])) {
00184 //                $data[$i]['cust_group'] = Mage_Customer_Model_Group::CUST_GROUP_ALL;
00185 //            }
00186 //            if ($data[$i]['website_id'] == 0) {
00187 //                $rate = Mage::app()->getStore()->getBaseCurrency()
00188 //                    ->getRate(Mage::app()->getBaseCurrencyCode());
00189 //                if ($rate) {
00190 //                    $data[$i]['website_price'] = $data[$i]['price']/$rate;
00191 //                }
00192 //                else {
00193 //                    /**
00194 //                     * Remove tier price if rate not available
00195 //                     */
00196 //                    unset($data[$i]);
00197 //                }
00198 //            }
00199 //            else {
00200 //                $data[$i]['website_price'] = $data[$i]['price'];
00201 //            }
00202 //        }
00203         $object->setData($this->getAttribute()->getName(), $data);
00204         return $this;
00205     }

afterSave ( object  ) 

After Save Attribute manipulation

Parameters:
Mage_Catalog_Model_Product $object
Returns:
Mage_Catalog_Model_Product_Attribute_Backend_Tierprice

Reimplemented from Mage_Catalog_Model_Product_Attribute_Backend_Price.

Definition at line 213 of file Tierprice.php.

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                 /* @var $website Mage_Core_Model_Website */
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     }

validate ( object  ) 

Validate data

Parameters:
Mage_Catalog_Model_Product $object
Returns:
this

Reimplemented from Mage_Eav_Model_Entity_Attribute_Backend_Abstract.

Definition at line 95 of file Tierprice.php.

00096     {
00097         $tiers = $object->getData($this->getAttribute()->getName());
00098         if (empty($tiers)) {
00099             return $this;
00100         }
00101 
00102         // validate per website
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         // validate currency
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     }


Member Data Documentation

$_rates [protected]

Definition at line 42 of file Tierprice.php.


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:23:40 2009 for Magento by  doxygen 1.5.8