PageRenderTime 29ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Catalog/Model/Resource/Product/Indexer/Price/Configurable.php

https://bitbucket.org/acidel/buykoala
PHP | 222 lines | 132 code | 22 blank | 68 comment | 2 complexity | 670a914c1cc8dfa05b61df2f82efa181 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@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) 2011 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. * Configurable Products Price Indexer Resource model
  28. *
  29. * @category Mage
  30. * @package Mage_Catalog
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Catalog_Model_Resource_Product_Indexer_Price_Configurable
  34. extends Mage_Catalog_Model_Resource_Product_Indexer_Price_Default
  35. {
  36. /**
  37. * Reindex temporary (price result data) for all products
  38. *
  39. * @return Mage_Catalog_Model_Resource_Product_Indexer_Price_Configurable
  40. */
  41. public function reindexAll()
  42. {
  43. $this->useIdxTable(true);
  44. $this->beginTransaction();
  45. try {
  46. $this->_prepareFinalPriceData();
  47. $this->_applyCustomOption();
  48. $this->_applyConfigurableOption();
  49. $this->_movePriceDataToIndexTable();
  50. $this->commit();
  51. } catch (Exception $e) {
  52. $this->rollBack();
  53. throw $e;
  54. }
  55. return $this;
  56. }
  57. /**
  58. * Reindex temporary (price result data) for defined product(s)
  59. *
  60. * @param int|array $entityIds
  61. * @return Mage_Catalog_Model_Resource_Product_Indexer_Price_Configurable
  62. */
  63. public function reindexEntity($entityIds)
  64. {
  65. $this->_prepareFinalPriceData($entityIds);
  66. $this->_applyCustomOption();
  67. $this->_applyConfigurableOption();
  68. $this->_movePriceDataToIndexTable();
  69. return $this;
  70. }
  71. /**
  72. * Retrieve table name for custom option temporary aggregation data
  73. *
  74. * @return string
  75. */
  76. protected function _getConfigurableOptionAggregateTable()
  77. {
  78. if ($this->useIdxTable()) {
  79. return $this->getTable('catalog/product_price_indexer_cfg_option_aggregate_idx');
  80. }
  81. return $this->getTable('catalog/product_price_indexer_cfg_option_aggregate_tmp');
  82. }
  83. /**
  84. * Retrieve table name for custom option prices data
  85. *
  86. * @return string
  87. */
  88. protected function _getConfigurableOptionPriceTable()
  89. {
  90. if ($this->useIdxTable()) {
  91. return $this->getTable('catalog/product_price_indexer_cfg_option_idx');
  92. }
  93. return $this->getTable('catalog/product_price_indexer_cfg_option_tmp');
  94. }
  95. /**
  96. * Prepare table structure for custom option temporary aggregation data
  97. *
  98. * @return Mage_Catalog_Model_Resource_Product_Indexer_Price_Configurable
  99. */
  100. protected function _prepareConfigurableOptionAggregateTable()
  101. {
  102. $this->_getWriteAdapter()->delete($this->_getConfigurableOptionAggregateTable());
  103. return $this;
  104. }
  105. /**
  106. * Prepare table structure for custom option prices data
  107. *
  108. * @return Mage_Catalog_Model_Resource_Product_Indexer_Price_Configurable
  109. */
  110. protected function _prepareConfigurableOptionPriceTable()
  111. {
  112. $this->_getWriteAdapter()->delete($this->_getConfigurableOptionPriceTable());
  113. return $this;
  114. }
  115. /**
  116. * Calculate minimal and maximal prices for configurable product options
  117. * and apply it to final price
  118. *
  119. * @return Mage_Catalog_Model_Resource_Product_Indexer_Price_Configurable
  120. */
  121. protected function _applyConfigurableOption()
  122. {
  123. $write = $this->_getWriteAdapter();
  124. $coaTable = $this->_getConfigurableOptionAggregateTable();
  125. $copTable = $this->_getConfigurableOptionPriceTable();
  126. $this->_prepareConfigurableOptionAggregateTable();
  127. $this->_prepareConfigurableOptionPriceTable();
  128. $select = $write->select()
  129. ->from(array('i' => $this->_getDefaultFinalPriceTable()), array())
  130. ->join(
  131. array('l' => $this->getTable('catalog/product_super_link')),
  132. 'l.parent_id = i.entity_id',
  133. array('parent_id', 'product_id'))
  134. ->columns(array('customer_group_id', 'website_id'), 'i')
  135. ->join(
  136. array('a' => $this->getTable('catalog/product_super_attribute')),
  137. 'l.parent_id = a.product_id',
  138. array())
  139. ->join(
  140. array('cp' => $this->getValueTable('catalog/product', 'int')),
  141. 'l.product_id = cp.entity_id AND cp.attribute_id = a.attribute_id AND cp.store_id = 0',
  142. array())
  143. ->joinLeft(
  144. array('apd' => $this->getTable('catalog/product_super_attribute_pricing')),
  145. 'a.product_super_attribute_id = apd.product_super_attribute_id'
  146. . ' AND apd.website_id = 0 AND cp.value = apd.value_index',
  147. array())
  148. ->joinLeft(
  149. array('apw' => $this->getTable('catalog/product_super_attribute_pricing')),
  150. 'a.product_super_attribute_id = apw.product_super_attribute_id'
  151. . ' AND apw.website_id = i.website_id AND cp.value = apw.value_index',
  152. array())
  153. ->join(
  154. array('le' => $this->getTable('catalog/product')),
  155. 'le.entity_id = l.product_id',
  156. array())
  157. ->where('le.required_options=0')
  158. ->group(array('l.parent_id', 'i.customer_group_id', 'i.website_id', 'l.product_id'));
  159. $priceExpression = $write->getCheckSql('apw.value_id IS NOT NULL', 'apw.pricing_value', 'apd.pricing_value');
  160. $percenExpr = $write->getCheckSql('apw.value_id IS NOT NULL', 'apw.is_percent', 'apd.is_percent');
  161. $roundExpr = "ROUND(i.price * ({$priceExpression} / 100), 4)";
  162. $roundPriceExpr = $write->getCheckSql("{$percenExpr} = 1", $roundExpr, $priceExpression);
  163. $priceColumn = $write->getCheckSql("{$priceExpression} IS NULL", '0', $roundPriceExpr);
  164. $priceColumn = new Zend_Db_Expr("SUM({$priceColumn})");
  165. $tierPrice = $priceExpression;
  166. $tierRoundPriceExp = $write->getCheckSql("{$percenExpr} = 1", $roundExpr, $tierPrice);
  167. $tierPriceExp = $write->getCheckSql("{$tierPrice} IS NULL", '0', $tierRoundPriceExp);
  168. $tierPriceColumn = $write->getCheckSql("MIN(i.tier_price) IS NOT NULL", "SUM({$tierPriceExp})", 'NULL');
  169. $select->columns(array(
  170. 'price' => $priceColumn,
  171. 'tier_price' => $tierPriceColumn
  172. ));
  173. $query = $select->insertFromSelect($coaTable);
  174. $write->query($query);
  175. $select = $write->select()
  176. ->from(
  177. array($coaTable),
  178. array('parent_id', 'customer_group_id', 'website_id', 'MIN(price)', 'MAX(price)', 'MIN(tier_price)'))
  179. ->group(array('parent_id', 'customer_group_id', 'website_id'));
  180. $query = $select->insertFromSelect($copTable);
  181. $write->query($query);
  182. $table = array('i' => $this->_getDefaultFinalPriceTable());
  183. $select = $write->select()
  184. ->join(
  185. array('io' => $copTable),
  186. 'i.entity_id = io.entity_id AND i.customer_group_id = io.customer_group_id'
  187. .' AND i.website_id = io.website_id',
  188. array());
  189. $select->columns(array(
  190. 'min_price' => new Zend_Db_Expr('i.min_price + io.min_price'),
  191. 'max_price' => new Zend_Db_Expr('i.max_price + io.max_price'),
  192. 'tier_price' => $write->getCheckSql('i.tier_price IS NOT NULL', 'i.tier_price + io.tier_price', 'NULL'),
  193. ));
  194. $query = $select->crossUpdateFromSelect($table);
  195. $write->query($query);
  196. $write->delete($coaTable);
  197. $write->delete($copTable);
  198. return $this;
  199. }
  200. }