PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/blingbang2016/shop
PHP | 369 lines | 237 code | 39 blank | 93 comment | 57 complexity | d55be51c8b1b3df3c3ad6a40b450eadb MD5 | raw file
  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@magento.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.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Catalog
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Catalog product abstract group price backend attribute model
  28. *
  29. * @category Mage
  30. * @package Mage_Catalog
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. abstract class Mage_Catalog_Model_Product_Attribute_Backend_Groupprice_Abstract
  34. extends Mage_Catalog_Model_Product_Attribute_Backend_Price
  35. {
  36. /**
  37. * Website currency codes and rates
  38. *
  39. * @var array
  40. */
  41. protected $_rates;
  42. /**
  43. * Error message when duplicates
  44. *
  45. * @abstract
  46. * @return string
  47. */
  48. abstract protected function _getDuplicateErrorMessage();
  49. /**
  50. * Retrieve websites currency rates and base currency codes
  51. *
  52. * @return array
  53. */
  54. protected function _getWebsiteCurrencyRates()
  55. {
  56. if (is_null($this->_rates)) {
  57. $this->_rates = array();
  58. $baseCurrency = Mage::app()->getBaseCurrencyCode();
  59. foreach (Mage::app()->getWebsites() as $website) {
  60. /* @var $website Mage_Core_Model_Website */
  61. if ($website->getBaseCurrencyCode() != $baseCurrency) {
  62. $rate = Mage::getModel('directory/currency')
  63. ->load($baseCurrency)
  64. ->getRate($website->getBaseCurrencyCode());
  65. if (!$rate) {
  66. $rate = 1;
  67. }
  68. $this->_rates[$website->getId()] = array(
  69. 'code' => $website->getBaseCurrencyCode(),
  70. 'rate' => $rate
  71. );
  72. } else {
  73. $this->_rates[$website->getId()] = array(
  74. 'code' => $baseCurrency,
  75. 'rate' => 1
  76. );
  77. }
  78. }
  79. }
  80. return $this->_rates;
  81. }
  82. /**
  83. * Get additional unique fields
  84. *
  85. * @param array $objectArray
  86. * @return array
  87. */
  88. protected function _getAdditionalUniqueFields($objectArray)
  89. {
  90. return array();
  91. }
  92. /**
  93. * Whether group price value fixed or percent of original price
  94. *
  95. * @param Mage_Catalog_Model_Product_Type_Price $priceObject
  96. * @return bool
  97. */
  98. protected function _isPriceFixed($priceObject)
  99. {
  100. return $priceObject->isGroupPriceFixed();
  101. }
  102. /**
  103. * Validate group price data
  104. *
  105. * @param Mage_Catalog_Model_Product $object
  106. * @throws Mage_Core_Exception
  107. * @return bool
  108. */
  109. public function validate($object)
  110. {
  111. $attribute = $this->getAttribute();
  112. $priceRows = $object->getData($attribute->getName());
  113. if (empty($priceRows)) {
  114. return true;
  115. }
  116. // validate per website
  117. $duplicates = array();
  118. foreach ($priceRows as $priceRow) {
  119. if (!empty($priceRow['delete'])) {
  120. continue;
  121. }
  122. $compare = join('-', array_merge(
  123. array($priceRow['website_id'], $priceRow['cust_group']),
  124. $this->_getAdditionalUniqueFields($priceRow)
  125. ));
  126. if (isset($duplicates[$compare])) {
  127. Mage::throwException($this->_getDuplicateErrorMessage());
  128. }
  129. $duplicates[$compare] = true;
  130. }
  131. // if attribute scope is website and edit in store view scope
  132. // add global group prices for duplicates find
  133. if (!$attribute->isScopeGlobal() && $object->getStoreId()) {
  134. $origGroupPrices = $object->getOrigData($attribute->getName());
  135. foreach ($origGroupPrices as $price) {
  136. if ($price['website_id'] == 0) {
  137. $compare = join('-', array_merge(
  138. array($price['website_id'], $price['cust_group']),
  139. $this->_getAdditionalUniqueFields($price)
  140. ));
  141. $duplicates[$compare] = true;
  142. }
  143. }
  144. }
  145. // validate currency
  146. $baseCurrency = Mage::app()->getBaseCurrencyCode();
  147. $rates = $this->_getWebsiteCurrencyRates();
  148. foreach ($priceRows as $priceRow) {
  149. if (!empty($priceRow['delete'])) {
  150. continue;
  151. }
  152. if ($priceRow['website_id'] == 0) {
  153. continue;
  154. }
  155. $globalCompare = join('-', array_merge(
  156. array(0, $priceRow['cust_group']),
  157. $this->_getAdditionalUniqueFields($priceRow)
  158. ));
  159. $websiteCurrency = $rates[$priceRow['website_id']]['code'];
  160. if ($baseCurrency == $websiteCurrency && isset($duplicates[$globalCompare])) {
  161. Mage::throwException($this->_getDuplicateErrorMessage());
  162. }
  163. }
  164. return true;
  165. }
  166. /**
  167. * Prepare group prices data for website
  168. *
  169. * @param array $priceData
  170. * @param string $productTypeId
  171. * @param int $websiteId
  172. * @return array
  173. */
  174. public function preparePriceData(array $priceData, $productTypeId, $websiteId)
  175. {
  176. $rates = $this->_getWebsiteCurrencyRates();
  177. $data = array();
  178. $price = Mage::getSingleton('catalog/product_type')->priceFactory($productTypeId);
  179. foreach ($priceData as $v) {
  180. $key = join('-', array_merge(array($v['cust_group']), $this->_getAdditionalUniqueFields($v)));
  181. if ($v['website_id'] == $websiteId) {
  182. $data[$key] = $v;
  183. $data[$key]['website_price'] = $v['price'];
  184. } else if ($v['website_id'] == 0 && !isset($data[$key])) {
  185. $data[$key] = $v;
  186. $data[$key]['website_id'] = $websiteId;
  187. if ($this->_isPriceFixed($price)) {
  188. $data[$key]['price'] = $v['price'] * $rates[$websiteId]['rate'];
  189. $data[$key]['website_price'] = $v['price'] * $rates[$websiteId]['rate'];
  190. }
  191. }
  192. }
  193. return $data;
  194. }
  195. /**
  196. * Assign group prices to product data
  197. *
  198. * @param Mage_Catalog_Model_Product $object
  199. * @return Mage_Catalog_Model_Product_Attribute_Backend_Groupprice_Abstract
  200. */
  201. public function afterLoad($object)
  202. {
  203. $storeId = $object->getStoreId();
  204. $websiteId = null;
  205. if ($this->getAttribute()->isScopeGlobal()) {
  206. $websiteId = 0;
  207. } else if ($storeId) {
  208. $websiteId = Mage::app()->getStore($storeId)->getWebsiteId();
  209. }
  210. $data = $this->_getResource()->loadPriceData($object->getId(), $websiteId);
  211. foreach ($data as $k => $v) {
  212. $data[$k]['website_price'] = $v['price'];
  213. if ($v['all_groups']) {
  214. $data[$k]['cust_group'] = Mage_Customer_Model_Group::CUST_GROUP_ALL;
  215. }
  216. }
  217. if (!$object->getData('_edit_mode') && $websiteId) {
  218. $data = $this->preparePriceData($data, $object->getTypeId(), $websiteId);
  219. }
  220. $object->setData($this->getAttribute()->getName(), $data);
  221. $object->setOrigData($this->getAttribute()->getName(), $data);
  222. $valueChangedKey = $this->getAttribute()->getName() . '_changed';
  223. $object->setOrigData($valueChangedKey, 0);
  224. $object->setData($valueChangedKey, 0);
  225. return $this;
  226. }
  227. /**
  228. * After Save Attribute manipulation
  229. *
  230. * @param Mage_Catalog_Model_Product $object
  231. * @return Mage_Catalog_Model_Product_Attribute_Backend_Groupprice_Abstract
  232. */
  233. public function afterSave($object)
  234. {
  235. $websiteId = Mage::app()->getStore($object->getStoreId())->getWebsiteId();
  236. $isGlobal = $this->getAttribute()->isScopeGlobal() || $websiteId == 0;
  237. $priceRows = $object->getData($this->getAttribute()->getName());
  238. if (empty($priceRows)) {
  239. if ($isGlobal) {
  240. $this->_getResource()->deletePriceData($object->getId());
  241. } else {
  242. $this->_getResource()->deletePriceData($object->getId(), $websiteId);
  243. }
  244. return $this;
  245. }
  246. $old = array();
  247. $new = array();
  248. // prepare original data for compare
  249. $origGroupPrices = $object->getOrigData($this->getAttribute()->getName());
  250. if (!is_array($origGroupPrices)) {
  251. $origGroupPrices = array();
  252. }
  253. foreach ($origGroupPrices as $data) {
  254. if ($data['website_id'] > 0 || ($data['website_id'] == '0' && $isGlobal)) {
  255. $key = join('-', array_merge(
  256. array($data['website_id'], $data['cust_group']),
  257. $this->_getAdditionalUniqueFields($data)
  258. ));
  259. $old[$key] = $data;
  260. }
  261. }
  262. // prepare data for save
  263. foreach ($priceRows as $data) {
  264. $hasEmptyData = false;
  265. foreach ($this->_getAdditionalUniqueFields($data) as $field) {
  266. if (empty($field)) {
  267. $hasEmptyData = true;
  268. break;
  269. }
  270. }
  271. if ($hasEmptyData || !isset($data['cust_group']) || !empty($data['delete'])) {
  272. continue;
  273. }
  274. if ($this->getAttribute()->isScopeGlobal() && $data['website_id'] > 0) {
  275. continue;
  276. }
  277. if (!$isGlobal && (int)$data['website_id'] == 0) {
  278. continue;
  279. }
  280. $key = join('-', array_merge(
  281. array($data['website_id'], $data['cust_group']),
  282. $this->_getAdditionalUniqueFields($data)
  283. ));
  284. $useForAllGroups = $data['cust_group'] == Mage_Customer_Model_Group::CUST_GROUP_ALL;
  285. $customerGroupId = !$useForAllGroups ? $data['cust_group'] : 0;
  286. $new[$key] = array_merge(array(
  287. 'website_id' => $data['website_id'],
  288. 'all_groups' => $useForAllGroups ? 1 : 0,
  289. 'customer_group_id' => $customerGroupId,
  290. 'value' => $data['price'],
  291. ), $this->_getAdditionalUniqueFields($data));
  292. }
  293. $delete = array_diff_key($old, $new);
  294. $insert = array_diff_key($new, $old);
  295. $update = array_intersect_key($new, $old);
  296. $isChanged = false;
  297. $productId = $object->getId();
  298. if (!empty($delete)) {
  299. foreach ($delete as $data) {
  300. $this->_getResource()->deletePriceData($productId, null, $data['price_id']);
  301. $isChanged = true;
  302. }
  303. }
  304. if (!empty($insert)) {
  305. foreach ($insert as $data) {
  306. $price = new Varien_Object($data);
  307. $price->setEntityId($productId);
  308. $this->_getResource()->savePriceData($price);
  309. $isChanged = true;
  310. }
  311. }
  312. if (!empty($update)) {
  313. foreach ($update as $k => $v) {
  314. if ($old[$k]['price'] != $v['value']) {
  315. $price = new Varien_Object(array(
  316. 'value_id' => $old[$k]['price_id'],
  317. 'value' => $v['value']
  318. ));
  319. $this->_getResource()->savePriceData($price);
  320. $isChanged = true;
  321. }
  322. }
  323. }
  324. if ($isChanged) {
  325. $valueChangedKey = $this->getAttribute()->getName() . '_changed';
  326. $object->setData($valueChangedKey, 1);
  327. }
  328. return $this;
  329. }
  330. }