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_Reports_Model_Resource_Product_Lowstock_Collection extends Mage_Reports_Model_Resource_Product_Collection
36: {
37: 38: 39: 40: 41:
42: protected $_inventoryItemResource = null;
43:
44: 45: 46: 47: 48:
49: protected $_inventoryItemJoined = false;
50:
51: 52: 53: 54: 55:
56: protected $_inventoryItemTableAlias = 'lowstock_inventory_item';
57:
58: 59: 60: 61: 62:
63: protected function _getInventoryItemResource()
64: {
65: if ($this->_inventoryItemResource === null) {
66: $this->_inventoryItemResource = Mage::getResourceSingleton('cataloginventory/stock_item');
67: }
68: return $this->_inventoryItemResource;
69: }
70:
71: 72: 73: 74: 75:
76: protected function _getInventoryItemTable()
77: {
78: return $this->_getInventoryItemResource()->getMainTable();
79: }
80:
81: 82: 83: 84: 85:
86: protected function _getInventoryItemIdField()
87: {
88: return $this->_getInventoryItemResource()->getIdFieldName();
89: }
90:
91: 92: 93: 94: 95:
96: protected function _getInventoryItemTableAlias()
97: {
98: return $this->_inventoryItemTableAlias;
99: }
100:
101: 102: 103: 104: 105: 106: 107:
108: protected function _addInventoryItemFieldToSelect($field, $alias = null)
109: {
110: if (empty($alias)) {
111: $alias = $field;
112: }
113:
114: if (isset($this->_joinFields[$alias])) {
115: return $this;
116: }
117:
118: $this->_joinFields[$alias] = array(
119: 'table' => $this->_getInventoryItemTableAlias(),
120: 'field' => $field
121: );
122:
123: $this->getSelect()->columns(array($alias => $field), $this->_getInventoryItemTableAlias());
124: return $this;
125: }
126:
127: 128: 129: 130: 131: 132:
133: protected function _getInventoryItemField($field)
134: {
135: return sprintf('%s.%s', $this->_getInventoryItemTableAlias(), $field);
136: }
137:
138: 139: 140: 141: 142: 143:
144: public function joinInventoryItem($fields = array())
145: {
146: if (!$this->_inventoryItemJoined) {
147: $this->getSelect()->join(
148: array($this->_getInventoryItemTableAlias() => $this->_getInventoryItemTable()),
149: sprintf('e.%s = %s.product_id',
150: $this->getEntity()->getEntityIdField(),
151: $this->_getInventoryItemTableAlias()
152: ),
153: array()
154: );
155: $this->_inventoryItemJoined = true;
156: }
157:
158: if (!is_array($fields)) {
159: if (empty($fields)) {
160: $fields = array();
161: } else {
162: $fields = array($fields);
163: }
164: }
165:
166: foreach ($fields as $alias => $field) {
167: if (!is_string($alias)) {
168: $alias = null;
169: }
170: $this->_addInventoryItemFieldToSelect($field, $alias);
171: }
172:
173: return $this;
174: }
175:
176: 177: 178: 179: 180: 181:
182: public function filterByProductType($typeFilter)
183: {
184: if (!is_string($typeFilter) && !is_array($typeFilter)) {
185: Mage::throwException(
186: Mage::helper('catalog')->__('Wrong product type filter specified')
187: );
188: }
189: $this->addAttributeToFilter('type_id', $typeFilter);
190: return $this;
191: }
192:
193: 194: 195: 196: 197: 198:
199: public function filterByIsQtyProductTypes()
200: {
201: $this->filterByProductType(
202: array_keys(array_filter(Mage::helper('cataloginventory')->getIsQtyTypeIds()))
203: );
204: return $this;
205: }
206:
207: 208: 209: 210: 211: 212:
213: public function useManageStockFilter($storeId = null)
214: {
215: $this->joinInventoryItem();
216: $manageStockExpr = $this->getConnection()->getCheckSql(
217: $this->_getInventoryItemField('use_config_manage_stock') . ' = 1',
218: (int) Mage::getStoreConfig(Mage_CatalogInventory_Model_Stock_Item::XML_PATH_MANAGE_STOCK, $storeId),
219: $this->_getInventoryItemField('manage_stock')
220: );
221: $this->getSelect()->where($manageStockExpr . ' = ?', 1);
222: return $this;
223: }
224:
225: 226: 227: 228: 229: 230:
231: public function useNotifyStockQtyFilter($storeId = null)
232: {
233: $this->joinInventoryItem(array('qty'));
234: $notifyStockExpr = $this->getConnection()->getCheckSql(
235: $this->_getInventoryItemField('use_config_notify_stock_qty') . ' = 1',
236: (int)Mage::getStoreConfig(Mage_CatalogInventory_Model_Stock_Item::XML_PATH_NOTIFY_STOCK_QTY, $storeId),
237: $this->_getInventoryItemField('notify_stock_qty')
238: );
239: $this->getSelect()->where('qty < ?', $notifyStockExpr);
240: return $this;
241: }
242: }
243: