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_Bundle_Model_Resource_Selection extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: 38: 39: 40:
41: protected function _construct()
42: {
43: $this->_init('bundle/selection', 'selection_id');
44: }
45:
46: 47: 48: 49: 50: 51: 52: 53: 54:
55: public function getPriceFromIndex($productId, $qty, $storeId, $groupId)
56: {
57: $adapter = $this->_getReadAdapter();
58: $select = clone $adapter->select();
59: $select->reset();
60:
61: $attrPriceId = Mage::getSingleton('eav/entity_attribute')
62: ->getIdByCode(Mage_Catalog_Model_Product::ENTITY, 'price');
63: $attrTierPriceId = Mage::getSingleton('eav/entity_attribute')
64: ->getIdByCode(Mage_Catalog_Model_Product::ENTITY, 'tier_price');
65:
66: $websiteId = Mage::app()->getStore($storeId)->getWebsiteId();
67:
68: $select->from(array("price_index" => $this->getTable('catalogindex/price')), array('price' => 'SUM(value)'))
69: ->where('entity_id = :product_id')
70: ->where('website_id = :website_id')
71: ->where('customer_group_id = :customer_group')
72: ->where('attribute_id = :price_attribute OR attribute_id = :tier_price_attribute')
73: ->where('qty <= :qty')
74: ->group('entity_id');
75:
76: $bind = array(
77: 'product_id' => $productId,
78: 'website_id' => $websiteId,
79: 'customer_group' => $groupId,
80: 'price_attribute' => $attrPriceId,
81: 'tier_price_attribute' => $attrTierPriceId,
82: 'qty' => $qty
83: );
84:
85: $price = $adapter->fetchCol($select, $bind);
86: if (!empty($price)) {
87: return array_shift($price);
88: } else {
89: return 0;
90: }
91: }
92:
93: 94: 95: 96: 97: 98: 99: 100: 101: 102:
103: public function getChildrenIds($parentId, $required = true)
104: {
105: $childrenIds = array();
106: $notRequired = array();
107: $adapter = $this->_getReadAdapter();
108: $select = $adapter->select()
109: ->from(
110: array('tbl_selection' => $this->getMainTable()),
111: array('product_id', 'parent_product_id', 'option_id')
112: )
113: ->join(
114: array('e' => $this->getTable('catalog/product')),
115: 'e.entity_id = tbl_selection.product_id AND e.required_options=0',
116: array()
117: )
118: ->join(
119: array('tbl_option' => $this->getTable('bundle/option')),
120: 'tbl_option.option_id = tbl_selection.option_id',
121: array('required')
122: )
123: ->where('tbl_selection.parent_product_id = :parent_id');
124: foreach ($adapter->fetchAll($select, array('parent_id' => $parentId)) as $row) {
125: if ($row['required']) {
126: $childrenIds[$row['option_id']][$row['product_id']] = $row['product_id'];
127: } else {
128: $notRequired[$row['option_id']][$row['product_id']] = $row['product_id'];
129: }
130: }
131:
132: if (!$required) {
133: $childrenIds = array_merge($childrenIds, $notRequired);
134: } else {
135: if (!$childrenIds) {
136: foreach ($notRequired as $groupedChildrenIds) {
137: foreach ($groupedChildrenIds as $childId) {
138: $childrenIds[0][$childId] = $childId;
139: }
140: }
141: }
142: if (!$childrenIds) {
143: $childrenIds = array(array());
144: }
145: }
146:
147: return $childrenIds;
148: }
149:
150: 151: 152: 153: 154: 155:
156: public function getParentIdsByChild($childId)
157: {
158: $adapter = $this->_getReadAdapter();
159: $select = $adapter->select()
160: ->distinct(true)
161: ->from($this->getMainTable(), 'parent_product_id')
162: ->where('product_id IN(?)', $childId);
163:
164: return $adapter->fetchCol($select);
165: }
166:
167: 168: 169: 170: 171:
172: public function saveSelectionPrice($item)
173: {
174: $write = $this->_getWriteAdapter();
175: if ($item->getDefaultPriceScope()) {
176: $write->delete($this->getTable('bundle/selection_price'),
177: array(
178: 'selection_id = ?' => $item->getSelectionId(),
179: 'website_id = ?' => $item->getWebsiteId()
180: )
181: );
182: } else {
183: $values = array(
184: 'selection_id' => $item->getSelectionId(),
185: 'website_id' => $item->getWebsiteId(),
186: 'selection_price_type' => $item->getSelectionPriceType(),
187: 'selection_price_value' => $item->getSelectionPriceValue()
188: );
189: $write->insertOnDuplicate(
190: $this->getTable('bundle/selection_price'),
191: $values,
192: array('selection_price_type', 'selection_price_value')
193: );
194: }
195: }
196: }
197: