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_CatalogRule
23: * @copyright Copyright (c) 2012 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:
28: /**
29: * Catalog Rule Product Aggregated Price per date Resource Model
30: *
31: * @category Mage
32: * @package Mage_CatalogRule
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_CatalogRule_Model_Resource_Rule_Product_Price extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: /**
38: * Initialize connection and define main table
39: *
40: */
41: protected function _construct()
42: {
43: $this->_init('catalogrule/rule_product_price', 'rule_product_price_id');
44: }
45:
46: /**
47: * Apply price rule price to price index table
48: *
49: * @param Varien_Db_Select $select
50: * @param array|string $indexTable
51: * @param string $entityId
52: * @param string $customerGroupId
53: * @param string $websiteId
54: * @param array $updateFields the array of fields for compare with rule price and update
55: * @param string $websiteDate
56: * @return Mage_CatalogRule_Model_Resource_Rule_Product_Price
57: */
58: public function applyPriceRuleToIndexTable(Varien_Db_Select $select, $indexTable, $entityId, $customerGroupId,
59: $websiteId, $updateFields, $websiteDate)
60: {
61: if (empty($updateFields)) {
62: return $this;
63: }
64:
65: if (is_array($indexTable)) {
66: foreach ($indexTable as $k => $v) {
67: if (is_string($k)) {
68: $indexAlias = $k;
69: } else {
70: $indexAlias = $v;
71: }
72: break;
73: }
74: } else {
75: $indexAlias = $indexTable;
76: }
77:
78: $select->join(array('rp' => $this->getMainTable()), "rp.rule_date = {$websiteDate}", array())
79: ->where("rp.product_id = {$entityId} AND rp.website_id = {$websiteId} AND rp.customer_group_id = {$customerGroupId}");
80:
81: foreach ($updateFields as $priceField) {
82: $priceCond = $this->_getWriteAdapter()->quoteIdentifier(array($indexAlias, $priceField));
83: $priceExpr = $this->_getWriteAdapter()->getCheckSql("rp.rule_price < {$priceCond}", 'rp.rule_price', $priceCond);
84: $select->columns(array($priceField => $priceExpr));
85: }
86:
87: $query = $select->crossUpdateFromSelect($indexTable);
88: $this->_getWriteAdapter()->query($query);
89:
90: return $this;
91: }
92: }
93: