PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Catalog/Model/Product/Attribute/Backend/Tierprice.php

https://bitbucket.org/andrewjleavitt/magestudy
PHP | 331 lines | 215 code | 36 blank | 80 comment | 56 complexity | 28a08fdebced1338459a45600be6ef2b MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Catalog
  23. * @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Catalog product tier price backend attribute model
  28. *
  29. * @category Mage
  30. * @package Mage_Catalog
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Catalog_Model_Product_Attribute_Backend_Tierprice extends Mage_Catalog_Model_Product_Attribute_Backend_Price
  34. {
  35. /**
  36. * Website currency codes and rates
  37. *
  38. * @var array
  39. */
  40. protected $_rates;
  41. /**
  42. * Retrieve resource instance
  43. *
  44. * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Attribute_Backend_Tierprice
  45. */
  46. protected function _getResource()
  47. {
  48. return Mage::getResourceSingleton('catalog/product_attribute_backend_tierprice');
  49. }
  50. /**
  51. * Retrieve websites rates and base currency codes
  52. *
  53. * @return array
  54. */
  55. public function _getWebsiteRates()
  56. {
  57. if (is_null($this->_rates)) {
  58. $this->_rates = array();
  59. $baseCurrency = Mage::app()->getBaseCurrencyCode();
  60. foreach (Mage::app()->getWebsites() as $website) {
  61. /* @var $website Mage_Core_Model_Website */
  62. if ($website->getBaseCurrencyCode() != $baseCurrency) {
  63. $rate = Mage::getModel('directory/currency')
  64. ->load($baseCurrency)
  65. ->getRate($website->getBaseCurrencyCode());
  66. if (!$rate) {
  67. $rate = 1;
  68. }
  69. $this->_rates[$website->getId()] = array(
  70. 'code' => $website->getBaseCurrencyCode(),
  71. 'rate' => $rate
  72. );
  73. } else {
  74. $this->_rates[$website->getId()] = array(
  75. 'code' => $baseCurrency,
  76. 'rate' => 1
  77. );
  78. }
  79. }
  80. }
  81. return $this->_rates;
  82. }
  83. /**
  84. * Validate tier price data
  85. *
  86. * @param Mage_Catalog_Model_Product $object
  87. * @throws Mage_Core_Exception
  88. * @return bool
  89. */
  90. public function validate($object)
  91. {
  92. $attribute = $this->getAttribute();
  93. $tiers = $object->getData($attribute->getName());
  94. if (empty($tiers)) {
  95. return true;
  96. }
  97. // validate per website
  98. $duplicates = array();
  99. foreach ($tiers as $tier) {
  100. if (!empty($tier['delete'])) {
  101. continue;
  102. }
  103. $compare = join('-', array($tier['website_id'], $tier['cust_group'], $tier['price_qty'] * 1));
  104. if (isset($duplicates[$compare])) {
  105. Mage::throwException(
  106. Mage::helper('catalog')->__('Duplicate website tier price customer group and quantity.')
  107. );
  108. }
  109. $duplicates[$compare] = true;
  110. }
  111. // if attribute scope is website and edit in store view scope
  112. // add global tier prices for duplicates find
  113. if (!$attribute->isScopeGlobal() && $object->getStoreId()) {
  114. $origTierPrices = $object->getOrigData($attribute->getName());
  115. foreach ($origTierPrices as $tier) {
  116. if ($tier['website_id'] == 0) {
  117. $compare = join('-', array($tier['website_id'], $tier['cust_group'], $tier['price_qty'] * 1));
  118. $duplicates[$compare] = true;
  119. }
  120. }
  121. }
  122. // validate currency
  123. $baseCurrency = Mage::app()->getBaseCurrencyCode();
  124. $rates = $this->_getWebsiteRates();
  125. foreach ($tiers as $tier) {
  126. if (!empty($tier['delete'])) {
  127. continue;
  128. }
  129. if ($tier['website_id'] == 0) {
  130. continue;
  131. }
  132. $compare = join('-', array($tier['website_id'], $tier['cust_group'], $tier['price_qty']));
  133. $globalCompare = join('-', array(0, $tier['cust_group'], $tier['price_qty'] * 1));
  134. $websiteCurrency = $rates[$tier['website_id']]['code'];
  135. if ($baseCurrency == $websiteCurrency && isset($duplicates[$globalCompare])) {
  136. Mage::throwException(
  137. Mage::helper('catalog')->__('Duplicate website tier price customer group and quantity.')
  138. );
  139. }
  140. }
  141. return true;
  142. }
  143. /**
  144. * Prepare tier prices data for website
  145. *
  146. * @param array $priceData
  147. * @param string $productTypeId
  148. * @param int $websiteId
  149. * @return array
  150. */
  151. public function preparePriceData(array $priceData, $productTypeId, $websiteId)
  152. {
  153. $rates = $this->_getWebsiteRates();
  154. $data = array();
  155. $price = Mage::getSingleton('catalog/product_type')->priceFactory($productTypeId);
  156. foreach ($priceData as $v) {
  157. $key = join('-', array($v['cust_group'], $v['price_qty']));
  158. if ($v['website_id'] == $websiteId) {
  159. $data[$key] = $v;
  160. $data[$key]['website_price'] = $v['price'];
  161. } else if ($v['website_id'] == 0 && !isset($data[$key])) {
  162. $data[$key] = $v;
  163. $data[$key]['website_id'] = $websiteId;
  164. if ($price->isTierPriceFixed()) {
  165. $data[$key]['price'] = $v['price'] * $rates[$websiteId]['rate'];
  166. $data[$key]['website_price'] = $v['price'] * $rates[$websiteId]['rate'];
  167. }
  168. }
  169. }
  170. return $data;
  171. }
  172. /**
  173. * Assign tier prices to product data
  174. *
  175. * @param Mage_Catalog_Model_Product $object
  176. * @return Mage_Catalog_Model_Product_Attribute_Backend_Tierprice
  177. */
  178. public function afterLoad($object)
  179. {
  180. $storeId = $object->getStoreId();
  181. $websiteId = null;
  182. if ($this->getAttribute()->isScopeGlobal()) {
  183. $websiteId = 0;
  184. } else if ($storeId) {
  185. $websiteId = Mage::app()->getStore($storeId)->getWebsiteId();
  186. }
  187. $data = $this->_getResource()->loadPriceData($object->getId(), $websiteId);
  188. foreach ($data as $k => $v) {
  189. $data[$k]['website_price'] = $v['price'];
  190. if ($v['all_groups']) {
  191. $data[$k]['cust_group'] = Mage_Customer_Model_Group::CUST_GROUP_ALL;
  192. }
  193. }
  194. if (!$object->getData('_edit_mode') && $websiteId) {
  195. $data = $this->preparePriceData($data, $object->getTypeId(), $websiteId);
  196. }
  197. $object->setData($this->getAttribute()->getName(), $data);
  198. $object->setOrigData($this->getAttribute()->getName(), $data);
  199. $valueChangedKey = $this->getAttribute()->getName() . '_changed';
  200. $object->setOrigData($valueChangedKey, 0);
  201. $object->setData($valueChangedKey, 0);
  202. return $this;
  203. }
  204. /**
  205. * After Save Attribute manipulation
  206. *
  207. * @param Mage_Catalog_Model_Product $object
  208. * @return Mage_Catalog_Model_Product_Attribute_Backend_Tierprice
  209. */
  210. public function afterSave($object)
  211. {
  212. $websiteId = Mage::app()->getStore($object->getStoreId())->getWebsiteId();
  213. $isGlobal = $this->getAttribute()->isScopeGlobal() || $websiteId == 0;
  214. $tierPrices = $object->getData($this->getAttribute()->getName());
  215. if (empty($tierPrices)) {
  216. if ($isGlobal) {
  217. $this->_getResource()->deletePriceData($object->getId());
  218. } else {
  219. $this->_getResource()->deletePriceData($object->getId(), $websiteId);
  220. }
  221. return $this;
  222. }
  223. $old = array();
  224. $new = array();
  225. // prepare original data for compare
  226. $origTierPrices = $object->getOrigData($this->getAttribute()->getName());
  227. if (!is_array($origTierPrices)) {
  228. $origTierPrices = array();
  229. }
  230. foreach ($origTierPrices as $data) {
  231. if ($data['website_id'] > 0 || ($data['website_id'] == '0' && $isGlobal)) {
  232. $key = join('-', array($data['website_id'], $data['cust_group'], $data['price_qty'] * 1));
  233. $old[$key] = $data;
  234. }
  235. }
  236. // prepare data for save
  237. foreach ($tierPrices as $data) {
  238. if (empty($data['price_qty']) || !isset($data['cust_group']) || !empty($data['delete'])) {
  239. continue;
  240. }
  241. if ($this->getAttribute()->isScopeGlobal() && $data['website_id'] > 0) {
  242. continue;
  243. }
  244. if (!$isGlobal && (int)$data['website_id'] == 0) {
  245. continue;
  246. }
  247. $key = join('-', array($data['website_id'], $data['cust_group'], $data['price_qty'] * 1));
  248. $useForAllGroups = $data['cust_group'] == Mage_Customer_Model_Group::CUST_GROUP_ALL;
  249. $customerGroupId = !$useForAllGroups ? $data['cust_group'] : 0;
  250. $new[$key] = array(
  251. 'website_id' => $data['website_id'],
  252. 'all_groups' => $useForAllGroups ? 1 : 0,
  253. 'customer_group_id' => $customerGroupId,
  254. 'qty' => $data['price_qty'],
  255. 'value' => $data['price'],
  256. );
  257. }
  258. $delete = array_diff_key($old, $new);
  259. $insert = array_diff_key($new, $old);
  260. $update = array_intersect_key($new, $old);
  261. $isChanged = false;
  262. $productId = $object->getId();
  263. if (!empty($delete)) {
  264. foreach ($delete as $data) {
  265. $this->_getResource()->deletePriceData($productId, null, $data['price_id']);
  266. $isChanged = true;
  267. }
  268. }
  269. if (!empty($insert)) {
  270. foreach ($insert as $data) {
  271. $price = new Varien_Object($data);
  272. $price->setEntityId($productId);
  273. $this->_getResource()->savePriceData($price);
  274. $isChanged = true;
  275. }
  276. }
  277. if (!empty($update)) {
  278. foreach ($update as $k => $v) {
  279. if ($old[$k]['price'] != $v['value']) {
  280. $price = new Varien_Object(array(
  281. 'value_id' => $old[$k]['price_id'],
  282. 'value' => $v['value']
  283. ));
  284. $this->_getResource()->savePriceData($price);
  285. $isChanged = true;
  286. }
  287. }
  288. }
  289. if ($isChanged) {
  290. $valueChangedKey = $this->getAttribute()->getName() . '_changed';
  291. $object->setData($valueChangedKey, 1);
  292. }
  293. return $this;
  294. }
  295. }