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 collection 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_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
36: {
37: /**
38: * Initialize resource model
39: *
40: */
41: protected function _construct()
42: {
43: $this->_init('cataloginventory/stock_item');
44: }
45:
46: /**
47: * Add stock filter to collection
48: *
49: * @param mixed $stock
50: * @return Mage_CatalogInventory_Model_Resource_Stock_Item_Collection
51: */
52: public function addStockFilter($stock)
53: {
54: if ($stock instanceof Mage_CatalogInventory_Model_Stock) {
55: $this->addFieldToFilter('main_table.stock_id', $stock->getId());
56: } else {
57: $this->addFieldToFilter('main_table.stock_id', $stock);
58: }
59: return $this;
60: }
61:
62: /**
63: * Add product filter to collection
64: *
65: * @param array $products
66: * @return Mage_CatalogInventory_Model_Resource_Stock_Item_Collection
67: */
68: public function addProductsFilter($products)
69: {
70: $productIds = array();
71: foreach ($products as $product) {
72: if ($product instanceof Mage_Catalog_Model_Product) {
73: $productIds[] = $product->getId();
74: } else {
75: $productIds[] = $product;
76: }
77: }
78: if (empty($productIds)) {
79: $productIds[] = false;
80: $this->_setIsLoaded(true);
81: }
82: $this->addFieldToFilter('main_table.product_id', array('in' => $productIds));
83: return $this;
84: }
85:
86: /**
87: * Join Stock Status to collection
88: *
89: * @param int $storeId
90: * @return Mage_CatalogInventory_Model_Resource_Stock_Item_Collection
91: */
92: public function joinStockStatus($storeId = null)
93: {
94: $websiteId = Mage::app()->getStore($storeId)->getWebsiteId();
95: $this->getSelect()->joinLeft(
96: array('status_table' => $this->getTable('cataloginventory/stock_status')),
97: 'main_table.product_id=status_table.product_id'
98: . ' AND main_table.stock_id=status_table.stock_id'
99: . $this->getConnection()->quoteInto(' AND status_table.website_id=?', $websiteId),
100: array('stock_status')
101: );
102:
103: return $this;
104: }
105:
106: /**
107: * Add Managed Stock products filter to collection
108: *
109: * @param boolean $isStockManagedInConfig
110: * @return Mage_CatalogInventory_Model_Resource_Stock_Item_Collection
111: */
112: public function addManagedFilter($isStockManagedInConfig)
113: {
114: if ($isStockManagedInConfig) {
115: $this->getSelect()->where('(manage_stock = 1 OR use_config_manage_stock = 1)');
116: } else {
117: $this->addFieldToFilter('manage_stock', 1);
118: }
119:
120: return $this;
121: }
122:
123: /**
124: * Add filter by quantity to collection
125: *
126: * @param string $comparsionMethod
127: * @param float $qty
128: * @return Mage_CatalogInventory_Model_Resource_Stock_Item_Collection
129: */
130: public function addQtyFilter($comparsionMethod, $qty)
131: {
132: $methods = array(
133: '<' => 'lt',
134: '>' => 'gt',
135: '=' => 'eq',
136: '<=' => 'lteq',
137: '>=' => 'gteq',
138: '<>' => 'neq'
139: );
140: if (!isset($methods[$comparsionMethod])) {
141: Mage::throwException(
142: Mage::helper('cataloginventory')->__('%s is not a correct comparsion method.', $comparsionMethod)
143: );
144: }
145:
146: return $this->addFieldToFilter('main_table.qty', array($methods[$comparsionMethod] => $qty));
147: }
148:
149: /**
150: * Initialize select object
151: *
152: * @return Mage_CatalogInventory_Model_Resource_Stock_Item_Collection
153: */
154: protected function _initSelect()
155: {
156: return parent::_initSelect()->getSelect()
157: ->join(
158: array('cp_table' => $this->getTable('catalog/product')),
159: 'main_table.product_id = cp_table.entity_id',
160: array('type_id')
161: );
162: }
163: }
164: