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 extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: 38: 39: 40: 41:
42: protected $_isConfig;
43:
44: 45: 46: 47: 48:
49: protected $_isConfigManageStock;
50:
51: 52: 53: 54: 55:
56: protected $_isConfigBackorders;
57:
58: 59: 60: 61: 62:
63: protected $_configMinQty;
64:
65: 66: 67: 68: 69:
70: protected $_configTypeIds;
71:
72: 73: 74: 75: 76:
77: protected $_configNotifyStockQty;
78:
79: 80: 81: 82: 83:
84: protected $_stock;
85:
86: 87: 88: 89:
90: protected function _construct()
91: {
92: $this->_init('cataloginventory/stock', 'stock_id');
93: }
94:
95: 96: 97: 98: 99: 100: 101:
102: public function lockProductItems($stock, $productIds)
103: {
104: $itemTable = $this->getTable('cataloginventory/stock_item');
105: $select = $this->_getWriteAdapter()->select()
106: ->from($itemTable)
107: ->where('stock_id=?', $stock->getId())
108: ->where('product_id IN(?)', $productIds)
109: ->forUpdate(true);
110: 111: 112:
113: $this->_getWriteAdapter()->query($select);
114: return $this;
115: }
116:
117: 118: 119: 120: 121: 122: 123: 124:
125: public function getProductsStock($stock, $productIds, $lockRows = false)
126: {
127: if (empty($productIds)) {
128: return array();
129: }
130: $itemTable = $this->getTable('cataloginventory/stock_item');
131: $productTable = $this->getTable('catalog/product');
132: $select = $this->_getWriteAdapter()->select()
133: ->from(array('si' => $itemTable))
134: ->join(array('p' => $productTable), 'p.entity_id=si.product_id', array('type_id'))
135: ->where('stock_id=?', $stock->getId())
136: ->where('product_id IN(?)', $productIds)
137: ->forUpdate($lockRows);
138: return $this->_getWriteAdapter()->fetchAll($select);
139: }
140:
141: 142: 143: 144: 145: 146: 147: 148:
149: public function correctItemsQty($stock, $productQtys, $operator = '-')
150: {
151: if (empty($productQtys)) {
152: return $this;
153: }
154:
155: $adapter = $this->_getWriteAdapter();
156: $conditions = array();
157: foreach ($productQtys as $productId => $qty) {
158: $case = $adapter->quoteInto('?', $productId);
159: $result = $adapter->quoteInto("qty{$operator}?", $qty);
160: $conditions[$case] = $result;
161: }
162:
163: $value = $adapter->getCaseSql('product_id', $conditions, 'qty');
164:
165: $where = array(
166: 'product_id IN (?)' => array_keys($productQtys),
167: 'stock_id = ?' => $stock->getId()
168: );
169:
170: $adapter->beginTransaction();
171: $adapter->update($this->getTable('cataloginventory/stock_item'), array('qty' => $value), $where);
172: $adapter->commit();
173:
174: return $this;
175: }
176:
177: 178: 179: 180: 181: 182:
183: public function setInStockFilterToCollection($collection)
184: {
185: $manageStock = Mage::getStoreConfig(Mage_CatalogInventory_Model_Stock_Item::XML_PATH_MANAGE_STOCK);
186: $cond = array(
187: '{{table}}.use_config_manage_stock = 0 AND {{table}}.manage_stock=1 AND {{table}}.is_in_stock=1',
188: '{{table}}.use_config_manage_stock = 0 AND {{table}}.manage_stock=0',
189: );
190:
191: if ($manageStock) {
192: $cond[] = '{{table}}.use_config_manage_stock = 1 AND {{table}}.is_in_stock=1';
193: } else {
194: $cond[] = '{{table}}.use_config_manage_stock = 1';
195: }
196:
197: $collection->joinField(
198: 'inventory_in_stock',
199: 'cataloginventory/stock_item',
200: 'is_in_stock',
201: 'product_id=entity_id',
202: '(' . join(') OR (', $cond) . ')'
203: );
204: return $this;
205: }
206:
207: 208: 209: 210:
211: protected function _initConfig()
212: {
213: if (!$this->_isConfig) {
214: $this->_isConfig = true;
215: $this->_isConfigManageStock = (int)Mage::getStoreConfigFlag(Mage_CatalogInventory_Model_Stock_Item::XML_PATH_MANAGE_STOCK);
216: $this->_isConfigBackorders = (int)Mage::getStoreConfig(Mage_CatalogInventory_Model_Stock_Item::XML_PATH_BACKORDERS);
217: $this->_configMinQty = (int)Mage::getStoreConfig(Mage_CatalogInventory_Model_Stock_Item::XML_PATH_MIN_QTY);
218: $this->_configNotifyStockQty = (int)Mage::getStoreConfig(Mage_CatalogInventory_Model_Stock_Item::XML_PATH_NOTIFY_STOCK_QTY);
219: $this->_configTypeIds = array_keys(Mage::helper('catalogInventory')->getIsQtyTypeIds(true));
220: $this->_stock = Mage::getModel('cataloginventory/stock');
221: }
222: }
223:
224: 225: 226: 227:
228: public function updateSetOutOfStock()
229: {
230: $this->_initConfig();
231: $adapter = $this->_getWriteAdapter();
232: $values = array(
233: 'is_in_stock' => 0,
234: 'stock_status_changed_auto' => 1
235: );
236:
237: $select = $adapter->select()
238: ->from($this->getTable('catalog/product'), 'entity_id')
239: ->where('type_id IN(?)', $this->_configTypeIds);
240:
241: $where = sprintf('stock_id = %1$d'
242: . ' AND is_in_stock = 1'
243: . ' AND ((use_config_manage_stock = 1 AND 1 = %2$d) OR (use_config_manage_stock = 0 AND manage_stock = 1))'
244: . ' AND ((use_config_backorders = 1 AND %3$d = %4$d) OR (use_config_backorders = 0 AND backorders = %3$d))'
245: . ' AND ((use_config_min_qty = 1 AND qty <= %5$d) OR (use_config_min_qty = 0 AND qty <= min_qty))'
246: . ' AND product_id IN (%6$s)',
247: $this->_stock->getId(),
248: $this->_isConfigManageStock,
249: Mage_CatalogInventory_Model_Stock::BACKORDERS_NO,
250: $this->_isConfigBackorders,
251: $this->_configMinQty,
252: $select->assemble()
253: );
254:
255: $adapter->update($this->getTable('cataloginventory/stock_item'), $values, $where);
256: }
257:
258: 259: 260: 261:
262: public function updateSetInStock()
263: {
264: $this->_initConfig();
265: $adapter = $this->_getWriteAdapter();
266: $values = array(
267: 'is_in_stock' => 1,
268: );
269:
270: $select = $adapter->select()
271: ->from($this->getTable('catalog/product'), 'entity_id')
272: ->where('type_id IN(?)', $this->_configTypeIds);
273:
274: $where = sprintf('stock_id = %1$d'
275: . ' AND is_in_stock = 0'
276: . ' AND stock_status_changed_auto = 1'
277: . ' AND ((use_config_manage_stock = 1 AND 1 = %2$d) OR (use_config_manage_stock = 0 AND manage_stock = 1))'
278: . ' AND ((use_config_min_qty = 1 AND qty > %3$d) OR (use_config_min_qty = 0 AND qty > min_qty))'
279: . ' AND product_id IN (%4$s)',
280: $this->_stock->getId(),
281: $this->_isConfigManageStock,
282: $this->_configMinQty,
283: $select->assemble()
284: );
285:
286: $adapter->update($this->getTable('cataloginventory/stock_item'), $values, $where);
287: }
288:
289: 290: 291: 292:
293: public function updateLowStockDate()
294: {
295: $this->_initConfig();
296:
297: $adapter = $this->_getWriteAdapter();
298: $condition = $adapter->quoteInto('(use_config_notify_stock_qty = 1 AND qty < ?)',
299: $this->_configNotifyStockQty) . ' OR (use_config_notify_stock_qty = 0 AND qty < notify_stock_qty)';
300: $currentDbTime = $adapter->quoteInto('?', $this->formatDate(true));
301: $conditionalDate = $adapter->getCheckSql($condition, $currentDbTime, 'NULL');
302:
303: $value = array(
304: 'low_stock_date' => new Zend_Db_Expr($conditionalDate),
305: );
306:
307: $select = $adapter->select()
308: ->from($this->getTable('catalog/product'), 'entity_id')
309: ->where('type_id IN(?)', $this->_configTypeIds);
310:
311: $where = sprintf('stock_id = %1$d'
312: . ' AND ((use_config_manage_stock = 1 AND 1 = %2$d) OR (use_config_manage_stock = 0 AND manage_stock = 1))'
313: . ' AND product_id IN (%3$s)',
314: $this->_stock->getId(),
315: $this->_isConfigManageStock,
316: $select->assemble()
317: );
318:
319: $adapter->update($this->getTable('cataloginventory/stock_item'), $value, $where);
320: }
321: }
322: