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_CatalogInventory
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: * Stock item resource model
30: *
31: * @category Mage
32: * @package Mage_CatalogInventory
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_CatalogInventory_Model_Resource_Stock_Item extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: /**
38: * Define main table and initialize connection
39: *
40: */
41: protected function _construct()
42: {
43: $this->_init('cataloginventory/stock_item', 'item_id');
44: }
45:
46: /**
47: * Loading stock item data by product
48: *
49: * @param Mage_CatalogInventory_Model_Stock_Item $item
50: * @param int $productId
51: * @return Mage_CatalogInventory_Model_Resource_Stock_Item
52: */
53: public function loadByProductId(Mage_CatalogInventory_Model_Stock_Item $item, $productId)
54: {
55: $select = $this->_getLoadSelect('product_id', $productId, $item)
56: ->where('stock_id = :stock_id');
57: $data = $this->_getReadAdapter()->fetchRow($select, array(':stock_id' => $item->getStockId()));
58: if ($data) {
59: $item->setData($data);
60: }
61: $this->_afterLoad($item);
62: return $this;
63: }
64:
65: /**
66: * Retrieve select object and join it to product entity table to get type ids
67: *
68: * @param string $field
69: * @param mixed $value
70: * @param Mage_CatalogInventory_Model_Stock_Item $object
71: * @return Varien_Db_Select
72: */
73: protected function _getLoadSelect($field, $value, $object)
74: {
75: $select = parent::_getLoadSelect($field, $value, $object)
76: ->join(array('p' => $this->getTable('catalog/product')),
77: 'product_id=p.entity_id',
78: array('type_id')
79: );
80: return $select;
81: }
82:
83: /**
84: * Add join for catalog in stock field to product collection
85: *
86: * @param Mage_Catalog_Model_Resource_Product_Collection $productCollection
87: * @return Mage_CatalogInventory_Model_Resource_Stock_Item
88: */
89: public function addCatalogInventoryToProductCollection($productCollection)
90: {
91: $adapter = $this->_getReadAdapter();
92: $isManageStock = (int)Mage::getStoreConfig(Mage_CatalogInventory_Model_Stock_Item::XML_PATH_MANAGE_STOCK);
93: $stockExpr = $adapter->getCheckSql('cisi.use_config_manage_stock = 1', $isManageStock, 'cisi.manage_stock');
94: $stockExpr = $adapter->getCheckSql("({$stockExpr} = 1)", 'cisi.is_in_stock', '1');
95:
96: $productCollection->joinTable(
97: array('cisi' => 'cataloginventory/stock_item'),
98: 'product_id=entity_id',
99: array(
100: 'is_saleable' => new Zend_Db_Expr($stockExpr),
101: 'inventory_in_stock' => 'is_in_stock'
102: ),
103: null,
104: 'left'
105: );
106: return $this;
107: }
108:
109: /**
110: * Use qty correction for qty column update
111: *
112: * @param Mage_Core_Model_Abstract $object
113: * @param string $table
114: * @return array
115: */
116: protected function _prepareDataForTable(Varien_Object $object, $table)
117: {
118: $data = parent::_prepareDataForTable($object, $table);
119: if (!$object->isObjectNew() && $object->getQtyCorrection()) {
120: $qty = abs($object->getQtyCorrection());
121: if ($object->getQtyCorrection() < 0) {
122: $data['qty'] = new Zend_Db_Expr('qty-' . $qty);
123: } else {
124: $data['qty'] = new Zend_Db_Expr('qty+' . $object->getQtyCorrection());
125: }
126: }
127: return $data;
128: }
129: }
130: