-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stock.php
242 lines (227 loc) · 7.83 KB
/
Stock.php
1
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magento.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magento.com for more information.
*
* @category Mage
* @package Mage_CatalogInventory
<<<<<<< HEAD
* @copyright Copyright (c) 2006-2015 X.commerce, Inc. (http://www.magento.com)
=======
* @copyright Copyright (c) 2006-2014 X.commerce, Inc. (http://www.magento.com)
>>>>>>> origin/master
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
/**
* Stock model
*
* @method Mage_CatalogInventory_Model_Resource_Stock _getResource()
* @method Mage_CatalogInventory_Model_Resource_Stock getResource()
* @method string getStockName()
* @method Mage_CatalogInventory_Model_Stock setStockName(string $value)
*
* @category Mage
* @package Mage_CatalogInventory
* @author Magento Core Team <core@magentocommerce.com>
*/
class Mage_CatalogInventory_Model_Stock extends Mage_Core_Model_Abstract
{
const BACKORDERS_NO = 0;
const BACKORDERS_YES_NONOTIFY = 1;
const BACKORDERS_YES_NOTIFY = 2;
/* deprecated */
const BACKORDERS_BELOW = 1;
const BACKORDERS_YES = 2;
const STOCK_OUT_OF_STOCK = 0;
const STOCK_IN_STOCK = 1;
const DEFAULT_STOCK_ID = 1;
protected function _construct()
{
$this->_init('cataloginventory/stock');
}
/**
* Retrieve stock identifier
*
* @return int
*/
public function getId()
{
return self::DEFAULT_STOCK_ID;
}
/**
* Add stock item objects to products
*
* @param collection $products
* @return Mage_CatalogInventory_Model_Stock
*/
public function addItemsToProducts($productCollection)
{
$items = $this->getItemCollection()
->addProductsFilter($productCollection)
->joinStockStatus($productCollection->getStoreId())
->load();
$stockItems = array();
foreach ($items as $item) {
$stockItems[$item->getProductId()] = $item;
}
foreach ($productCollection as $product) {
if (isset($stockItems[$product->getId()])) {
$stockItems[$product->getId()]->assignProduct($product);
}
}
return $this;
}
/**
* Retrieve items collection object with stock filter
*
* @return unknown
*/
public function getItemCollection()
{
return Mage::getResourceModel('cataloginventory/stock_item_collection')
->addStockFilter($this->getId());
}
/**
* Prepare array($productId=>$qty) based on array($productId => array('qty'=>$qty, 'item'=>$stockItem))
*
* @param array $items
*/
protected function _prepareProductQtys($items)
{
$qtys = array();
foreach ($items as $productId => $item) {
if (empty($item['item'])) {
$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($productId);
} else {
$stockItem = $item['item'];
}
$canSubtractQty = $stockItem->getId() && $stockItem->canSubtractQty();
if ($canSubtractQty && Mage::helper('catalogInventory')->isQty($stockItem->getTypeId())) {
$qtys[$productId] = $item['qty'];
}
}
return $qtys;
}
/**
* Subtract product qtys from stock.
* Return array of items that require full save
*
* @param array $items
* @return array
*/
public function registerProductsSale($items)
{
$qtys = $this->_prepareProductQtys($items);
$item = Mage::getModel('cataloginventory/stock_item');
$this->_getResource()->beginTransaction();
$stockInfo = $this->_getResource()->getProductsStock($this, array_keys($qtys), true);
$fullSaveItems = array();
foreach ($stockInfo as $itemInfo) {
$item->setData($itemInfo);
if (!$item->checkQty($qtys[$item->getProductId()])) {
$this->_getResource()->commit();
Mage::throwException(Mage::helper('cataloginventory')->__('Not all products are available in the requested quantity'));
}
$item->subtractQty($qtys[$item->getProductId()]);
if (!$item->verifyStock() || $item->verifyNotification()) {
$fullSaveItems[] = clone $item;
}
}
$this->_getResource()->correctItemsQty($this, $qtys, '-');
$this->_getResource()->commit();
return $fullSaveItems;
}
/**
*
* @param unknown_type $items
*/
public function revertProductsSale($items)
{
$qtys = $this->_prepareProductQtys($items);
$this->_getResource()->correctItemsQty($this, $qtys, '+');
return $this;
}
/**
* Subtract ordered qty for product
*
* @param Varien_Object $item
* @return Mage_CatalogInventory_Model_Stock
*/
public function registerItemSale(Varien_Object $item)
{
$productId = $item->getProductId();
if ($productId) {
$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($productId);
if (Mage::helper('catalogInventory')->isQty($stockItem->getTypeId())) {
if ($item->getStoreId()) {
$stockItem->setStoreId($item->getStoreId());
}
if ($stockItem->checkQty($item->getQtyOrdered()) || Mage::app()->getStore()->isAdmin()) {
$stockItem->subtractQty($item->getQtyOrdered());
$stockItem->save();
}
}
}
else {
Mage::throwException(Mage::helper('cataloginventory')->__('Cannot specify product identifier for the order item.'));
}
return $this;
}
/**
* Get back to stock (when order is canceled or whatever else)
*
* @param int $productId
* @param numeric $qty
* @return Mage_CatalogInventory_Model_Stock
*/
public function backItemQty($productId, $qty)
{
$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($productId);
if ($stockItem->getId() && Mage::helper('catalogInventory')->isQty($stockItem->getTypeId())) {
$stockItem->addQty($qty);
if ($stockItem->getCanBackInStock() && $stockItem->getQty() > $stockItem->getMinQty()) {
$stockItem->setIsInStock(true)
->setStockStatusChangedAutomaticallyFlag(true);
}
$stockItem->save();
}
return $this;
}
/**
* Lock stock items for product ids array
*
* @param array $productIds
* @return Mage_CatalogInventory_Model_Stock
*/
public function lockProductItems($productIds)
{
$this->_getResource()->lockProductItems($this, $productIds);
return $this;
}
/**
* Adds filtering for collection to return only in stock products
*
* @param Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Link_Product_Collection $collection
* @return Mage_CatalogInventory_Model_Stock $this
*/
public function addInStockFilterToCollection($collection)
{
$this->getResource()->setInStockFilterToCollection($collection);
return $this;
}
}