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_CatalogInventory_Model_Resource_Stock_Status extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: 38: 39: 40:
41: protected function _construct()
42: {
43: $this->_init('cataloginventory/stock_status', 'product_id');
44: }
45:
46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56:
57: public function saveProductStatus(Mage_CatalogInventory_Model_Stock_Status $object, $productId, $status, $qty = 0,
58: $stockId = 1, $websiteId = null)
59: {
60: $websites = array_keys($object->getWebsites($websiteId));
61: $adapter = $this->_getWriteAdapter();
62: foreach ($websites as $websiteId) {
63: $select = $adapter->select()
64: ->from($this->getMainTable())
65: ->where('product_id = :product_id')
66: ->where('website_id = :website_id')
67: ->where('stock_id = :stock_id');
68: $bind = array(
69: ':product_id' => $productId,
70: ':website_id' => $websiteId,
71: ':stock_id' => $stockId
72: );
73: if ($row = $adapter->fetchRow($select, $bind)) {
74: $bind = array(
75: 'qty' => $qty,
76: 'stock_status' => $status
77: );
78: $where = array(
79: $adapter->quoteInto('product_id=?', (int)$row['product_id']),
80: $adapter->quoteInto('website_id=?', (int)$row['website_id']),
81: $adapter->quoteInto('stock_id=?', (int)$row['stock_id']),
82: );
83: $adapter->update($this->getMainTable(), $bind, $where);
84: } else {
85: $bind = array(
86: 'product_id' => $productId,
87: 'website_id' => $websiteId,
88: 'stock_id' => $stockId,
89: 'qty' => $qty,
90: 'stock_status' => $status
91: );
92: $adapter->insert($this->getMainTable(), $bind);
93: }
94: }
95:
96: return $this;
97: }
98:
99: 100: 101: 102: 103: 104: 105: 106: 107:
108: public function getProductStatus($productIds, $websiteId, $stockId = 1)
109: {
110: if (!is_array($productIds)) {
111: $productIds = array($productIds);
112: }
113:
114: $select = $this->_getReadAdapter()->select()
115: ->from($this->getMainTable(), array('product_id', 'stock_status'))
116: ->where('product_id IN(?)', $productIds)
117: ->where('stock_id=?', (int)$stockId)
118: ->where('website_id=?', (int)$websiteId);
119: return $this->_getReadAdapter()->fetchPairs($select);
120: }
121:
122: 123: 124: 125: 126: 127: 128: 129:
130: public function getProductData($productIds, $websiteId, $stockId = 1)
131: {
132: if (!is_array($productIds)) {
133: $productIds = array($productIds);
134: }
135:
136: $result = array();
137:
138: $select = $this->_getReadAdapter()->select()
139: ->from($this->getMainTable())
140: ->where('product_id IN(?)', $productIds)
141: ->where('stock_id=?', (int)$stockId)
142: ->where('website_id=?', (int)$websiteId);
143: $result = $this->_getReadAdapter()->fetchAssoc($select);
144: return $result;
145: }
146:
147: 148: 149: 150: 151: 152:
153: public function getWebsiteStores()
154: {
155: $select = Mage::getModel('core/website')->getDefaultStoresSelect(false);
156: return $this->_getReadAdapter()->fetchPairs($select);
157: }
158:
159: 160: 161: 162: 163: 164:
165: public function getProductsType($productIds)
166: {
167: if (!is_array($productIds)) {
168: $productIds = array($productIds);
169: }
170:
171: $select = $this->_getReadAdapter()->select()
172: ->from(
173: array('e' => $this->getTable('catalog/product')),
174: array('entity_id', 'type_id')
175: )
176: ->where('entity_id IN(?)', $productIds);
177: return $this->_getReadAdapter()->fetchPairs($select);
178: }
179:
180: 181: 182: 183: 184: 185: 186: 187:
188: public function getProductCollection($lastEntityId = 0, $limit = 1000)
189: {
190: $select = $this->_getReadAdapter()->select()
191: ->from(
192: array('e' => $this->getTable('catalog/product')),
193: array('entity_id', 'type_id')
194: )
195: ->order('entity_id ASC')
196: ->where('entity_id > :entity_id')
197: ->limit($limit);
198: return $this->_getReadAdapter()->fetchPairs($select, array(':entity_id' => $lastEntityId));
199: }
200:
201: 202: 203: 204: 205: 206: 207:
208: public function addStockStatusToSelect(Varien_Db_Select $select, Mage_Core_Model_Website $website)
209: {
210: $websiteId = $website->getId();
211: $select->joinLeft(
212: array('stock_status' => $this->getMainTable()),
213: 'e.entity_id = stock_status.product_id AND stock_status.website_id='.$websiteId,
214: array('salable' => 'stock_status.stock_status')
215: );
216:
217: return $this;
218: }
219:
220: 221: 222: 223: 224: 225: 226: 227:
228: public function prepareCatalogProductIndexSelect(Varien_Db_Select $select, $entityField, $websiteField)
229: {
230: $select->join(
231: array('ciss' => $this->getMainTable()),
232: "ciss.product_id = {$entityField} AND ciss.website_id = {$websiteField}",
233: array()
234: );
235: $select->where('ciss.stock_status = ?', Mage_CatalogInventory_Model_Stock_Status::STATUS_IN_STOCK);
236:
237: return $this;
238: }
239:
240: 241: 242: 243: 244: 245:
246: public function addIsInStockFilterToCollection($collection)
247: {
248: $websiteId = Mage::app()->getStore($collection->getStoreId())->getWebsiteId();
249: $joinCondition = $this->_getReadAdapter()
250: ->quoteInto('e.entity_id = stock_status_index.product_id'
251: . ' AND stock_status_index.website_id = ?', $websiteId
252: );
253:
254: $joinCondition .= $this->_getReadAdapter()->quoteInto(
255: ' AND stock_status_index.stock_id = ?',
256: Mage_CatalogInventory_Model_Stock::DEFAULT_STOCK_ID
257: );
258:
259: $collection->getSelect()
260: ->join(
261: array('stock_status_index' => $this->getMainTable()),
262: $joinCondition,
263: array()
264: )
265: ->where('stock_status_index.stock_status=?', Mage_CatalogInventory_Model_Stock_Status::STATUS_IN_STOCK);
266:
267: return $this;
268: }
269: }
270: