1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_Weee_Model_Resource_Tax extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: 38: 39: 40:
41: protected function _construct()
42: {
43: $this->_init('weee/tax', 'value_id');
44: }
45:
46: 47: 48: 49: 50: 51:
52: public function fetchOne($select)
53: {
54: return $this->_getReadAdapter()->fetchOne($select);
55: }
56:
57: 58: 59: 60: 61: 62:
63: public function fetchCol($select)
64: {
65: return $this->_getReadAdapter()->fetchCol($select);
66: }
67:
68: 69: 70: 71: 72:
73: public function updateDiscountPercents()
74: {
75: return $this->_updateDiscountPercents();
76: }
77:
78: 79: 80: 81: 82: 83:
84: public function updateProductsDiscountPercent($condition)
85: {
86: return $this->_updateDiscountPercents($condition);
87: }
88:
89: 90: 91: 92: 93: 94:
95: protected function _updateDiscountPercents($productCondition = null)
96: {
97: $now = Varien_Date::toTimestamp(Varien_Date::now());
98: $adapter = $this->_getWriteAdapter();
99:
100: $select = $this->_getReadAdapter()->select();
101: $select->from(array('data' => $this->getTable('catalogrule/rule_product')));
102:
103: $deleteCondition = '';
104: if ($productCondition) {
105: if ($productCondition instanceof Mage_Catalog_Model_Product) {
106: $select->where('product_id = ?', (int)$productCondition->getId());
107: $deleteCondition = $adapter->quoteInto('entity_id=?', (int)$productCondition->getId());
108: } elseif ($productCondition instanceof Mage_Catalog_Model_Product_Condition_Interface) {
109: $productCondition = $productCondition->getIdsSelect($adapter)->__toString();
110: $select->where("product_id IN ({$productCondition})");
111: $deleteCondition = "entity_id IN ({$productCondition})";
112: } else {
113: $select->where('product_id = ?', (int)$productCondition);
114: $deleteCondition = $adapter->quoteInto('entity_id = ?', (int)$productCondition);
115: }
116: } else {
117: $select->where('(from_time <= ? OR from_time = 0)', $now)
118: ->where('(to_time >= ? OR to_time = 0)', $now);
119: }
120: $adapter->delete($this->getTable('weee/discount'), $deleteCondition);
121:
122: $select->order(array('data.website_id', 'data.customer_group_id', 'data.product_id', 'data.sort_order'));
123:
124: $data = $this->_getReadAdapter()->query($select);
125:
126: $productData = array();
127: $stops = array();
128: $prevKey = false;
129: while ($row = $data->fetch()) {
130: $key = "{$row['product_id']}-{$row['website_id']}-{$row['customer_group_id']}";
131: if (isset($stops[$key]) && $stops[$key]) {
132: continue;
133: }
134:
135: if ($prevKey && ($prevKey != $key)) {
136: foreach ($productData as $product) {
137: $adapter->insert($this->getTable('weee/discount'), $product);
138: }
139: $productData = array();
140: }
141: if ($row['action_operator'] == 'by_percent') {
142: if (isset($productData[$key])) {
143: $productData[$key]['value'] -= $productData[$key]['value']/100*$row['action_amount'];
144: } else {
145: $productData[$key] = array(
146: 'entity_id' => $row['product_id'],
147: 'customer_group_id' => $row['customer_group_id'],
148: 'website_id' => $row['website_id'],
149: 'value' => 100-max(0, min(100, $row['action_amount'])),
150: );
151: }
152: }
153:
154: if ($row['action_stop']) {
155: $stops[$key] = true;
156: }
157: $prevKey = $key;
158: }
159: foreach ($productData as $product) {
160: $adapter->insert($this->getTable('weee/discount'), $product);
161: }
162:
163: return $this;
164: }
165:
166: 167: 168: 169: 170: 171: 172: 173:
174: public function getProductDiscountPercent($productId, $websiteId, $customerGroupId)
175: {
176: $select = $this->_getReadAdapter()->select();
177: $select->from($this->getTable('weee/discount'), 'value')
178: ->where('website_id = ?', (int)$websiteId)
179: ->where('entity_id = ?', (int)$productId)
180: ->where('customer_group_id = ?', (int)$customerGroupId);
181:
182: return $this->_getReadAdapter()->fetchOne($select);
183: }
184: }
185:
186: