00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Mage_CatalogInventory_Model_Stock_Status extends Mage_Core_Model_Abstract
00035 {
00036 const STATUS_OUT_OF_STOCK = 0;
00037 const STATUS_IN_STOCK = 1;
00038
00039
00040
00041
00042
00043
00044 protected $_productTypes;
00045
00046
00047
00048
00049
00050
00051 protected $_websites;
00052
00053
00054
00055
00056
00057 protected function _construct()
00058 {
00059 $this->_init('cataloginventory/stock_status');
00060 }
00061
00062
00063
00064
00065
00066
00067
00068 public function getProductTypeInstances()
00069 {
00070 if (is_null($this->_productTypes)) {
00071 $this->_productTypes = array();
00072 $productEmulator = new Varien_Object();
00073
00074 foreach (array_keys(Mage_Catalog_Model_Product_Type::getTypes()) as $typeId) {
00075 $productEmulator->setTypeId($typeId);
00076 $this->_productTypes[$typeId] = Mage::getSingleton('catalog/product_type')
00077 ->factory($productEmulator);
00078 }
00079 }
00080 return $this->_productTypes;
00081 }
00082
00083
00084
00085
00086
00087
00088
00089 public function getProductTypeInstance($productType)
00090 {
00091 $types = $this->getProductTypeInstances();
00092 if (isset($types[$productType])) {
00093 return $types[$productType];
00094 }
00095 return false;
00096 }
00097
00098
00099
00100
00101
00102
00103 public function getWebsites($websiteId = null)
00104 {
00105 if (is_null($this->_websites)) {
00106 $this->_websites = $this->getResource()->getWebsiteStores();
00107 }
00108
00109 $websites = $this->_websites;
00110 if (!is_null($websiteId) && isset($this->_websites[$websiteId])) {
00111 $websites = array($websiteId => $this->_websites[$websiteId]);
00112 }
00113
00114 return $websites;
00115 }
00116
00117
00118
00119
00120
00121
00122
00123 public function getWebsiteDefaultStoreId($websiteId)
00124 {
00125 $websites = $this->getWebsites();
00126 if (isset($websites[$websiteId])) {
00127 return $websites[$websiteId];
00128 }
00129 return 0;
00130 }
00131
00132
00133
00134
00135
00136
00137 public function getProductStatusModel()
00138 {
00139 return Mage::getSingleton('catalog/product_status');
00140 }
00141
00142
00143
00144
00145
00146
00147 public function getStockItemModel()
00148 {
00149 return Mage::getModel('cataloginventory/stock_item');
00150 }
00151
00152
00153
00154
00155
00156
00157 public function getProductStatusEnabled()
00158 {
00159 return Mage_Catalog_Model_Product_Status::STATUS_ENABLED;
00160 }
00161
00162
00163
00164
00165
00166
00167
00168 public function changeItemStatus(Mage_CatalogInventory_Model_Stock_Item $item)
00169 {
00170 $productId = $item->getProductId();
00171 if (!$productType = $item->getProductTypeId()) {
00172 $productType = $this->getProductType($productId);
00173 }
00174
00175 $status = (int)$item->getIsInStock();
00176 $qty = (int)$item->getQty();
00177
00178 $this->_processChildren($productId, $productType, $qty, $status, $item->getStockId());
00179 $this->_processParents($productId, $item->getStockId());
00180
00181 return $this;
00182 }
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192 public function assignProduct(Mage_Catalog_Model_Product $product, $stockId = 1, $stockStatus = null)
00193 {
00194 if (is_null($stockStatus)) {
00195 $websiteId = $product->getStore()->getWebsiteId();
00196 $status = $this->getProductStatus($product->getId(), $websiteId, $stockId);
00197 $stockStatus = isset($status[$product->getId()]) ? $status[$product->getId()] : null;
00198 }
00199
00200 $product->setIsSalable($stockStatus);
00201
00202 return $this;
00203 }
00204
00205
00206
00207
00208
00209
00210
00211 public function rebuild($websiteId = null)
00212 {
00213 $lastProductId = 0;
00214 while (true) {
00215 $productCollection = $this->getResource()->getProductCollection($lastProductId);
00216 if (!$productCollection) {
00217 break;
00218 }
00219
00220 foreach ($productCollection as $productId => $productType) {
00221 $lastProductId = $productId;
00222 $this->updateStatus($productId, $productType, $websiteId);
00223 }
00224 }
00225
00226 return $this;
00227 }
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237 public function updateStatus($productId, $productType = null, $websiteId = null)
00238 {
00239 if (is_null($productType)) {
00240 $productType = $this->getProductType($productId);
00241 }
00242
00243 $item = $this->getStockItemModel()->loadByProduct($productId);
00244
00245 $status = self::STATUS_IN_STOCK;
00246 $qty = 0;
00247 if ($item->getId()) {
00248 $status = $item->getIsInStock();
00249 $qty = $item->getQty();
00250 }
00251
00252 $this->_processChildren($productId, $productType, $qty, $status, $item->getStockId(), $websiteId);
00253 $this->_processParents($productId, $item->getStockId(), $websiteId);
00254
00255 return $this;
00256 }
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269 protected function _processChildren($productId, $productType, $qty = 0, $status = self::STATUS_IN_STOCK, $stockId = 1, $websiteId = null)
00270 {
00271 if ($status == self::STATUS_OUT_OF_STOCK) {
00272 $this->saveProductStatus($productId, $status, $qty, $stockId, $websiteId);
00273 return $this;
00274 }
00275
00276 $statuses = array();
00277 $websites = $this->getWebsites($websiteId);
00278
00279 foreach (array_keys($websites) as $websiteId) {
00280
00281 $statuses[$websiteId] = $status;
00282 }
00283
00284 if (!$typeInstance = $this->getProductTypeInstance($productType)) {
00285 return $this;
00286 }
00287
00288 $requiredChildrenIds = $typeInstance->getChildrenIds($productId, true);
00289 if ($requiredChildrenIds) {
00290 $childrenIds = array();
00291 foreach ($requiredChildrenIds as $groupedChildrenIds) {
00292 $childrenIds = array_merge($childrenIds, $groupedChildrenIds);
00293 }
00294 $childrenWebsites = Mage::getSingleton('catalog/product_website')
00295 ->getWebsites($childrenIds);
00296 foreach ($websites as $websiteId => $storeId) {
00297 $childrenStatus = $this->getProductStatusModel()
00298 ->getProductStatus($childrenIds, $storeId);
00299 $childrenStock = $this->getProductStatus($childrenIds, $websiteId, $stockId);
00300
00301 $websiteStatus = $statuses[$websiteId];
00302 foreach ($requiredChildrenIds as $groupedChildrenIds) {
00303 $optionStatus = false;
00304 foreach ($groupedChildrenIds as $childId) {
00305 if (isset($childrenStatus[$childId])
00306 and isset($childrenWebsites[$childId])
00307 and in_array($websiteId, $childrenWebsites[$childId])
00308 and $childrenStatus[$childId] == $this->getProductStatusEnabled()
00309 and isset($childrenStock[$childId])
00310 and $childrenStock[$childId] == self::STATUS_IN_STOCK
00311 ) {
00312 $optionStatus = true;
00313 }
00314 }
00315 $websiteStatus = $websiteStatus && $optionStatus;
00316 }
00317 $statuses[$websiteId] = (int)$websiteStatus;
00318 }
00319 }
00320
00321 foreach ($statuses as $websiteId => $websiteStatus) {
00322 $this->saveProductStatus($productId, $websiteStatus, $qty, $stockId, $websiteId);
00323 }
00324
00325 return $this;
00326 }
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336 protected function _processParents($productId, $stockId = 1, $websiteId = null)
00337 {
00338 $parentIds = array();
00339 foreach ($this->getProductTypeInstances() as $typeInstance) {
00340
00341 $parentIds = array_merge($parentIds, $typeInstance->getParentIdsByChild($productId));
00342 }
00343
00344 if (!$parentIds) {
00345 return $this;
00346 }
00347
00348 $productTypes = $this->getProductsType($parentIds);
00349 $item = $this->getStockItemModel();
00350
00351 foreach ($parentIds as $parentId) {
00352 $parentType = isset($productTypes[$parentId]) ? $productTypes[$parentId] : null;
00353 $item->setData(array('stock_id' => $stockId))
00354 ->setOrigData()
00355 ->loadByProduct($parentId);
00356 $status = self::STATUS_IN_STOCK;
00357 $qty = 0;
00358 if ($item->getId()) {
00359 $status = $item->getIsInStock();
00360 $qty = $item->getQty();
00361 }
00362
00363 $this->_processChildren($parentId, $parentType, $qty, $status, $item->getStockId(), $websiteId);
00364 }
00365
00366 return $this;
00367 }
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380 public function saveProductStatus($productId, $status, $qty = 0, $stockId = 1, $websiteId = null)
00381 {
00382 $this->getResource()->saveProductStatus($this, $productId, $status, $qty, $stockId, $websiteId);
00383 return $this;
00384 }
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394 public function getProductStatus($productIds, $websiteId, $stockId = 1)
00395 {
00396 return $this->getResource()->getProductStatus($productIds, $websiteId, $stockId);
00397 }
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407 public function getProductData($productIds, $websiteId, $stockId = 1)
00408 {
00409 return $this->getResource()->getProductData($productIds, $websiteId, $stockId);
00410 }
00411
00412
00413
00414
00415
00416
00417 public function getResource()
00418 {
00419 return parent::getResource();
00420 }
00421
00422
00423
00424
00425
00426
00427
00428 public function getProductType($productId)
00429 {
00430 $types = $this->getResource()->getProductsType($productId);
00431 if (isset($types[$productId])) {
00432 return $types[$productId];
00433 }
00434 return false;
00435 }
00436
00437
00438
00439
00440
00441
00442
00443
00444 public function getProductsType($productIds)
00445 {
00446 return $this->getResource()->getProductsType($productIds);
00447 }
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457 public function addStockStatusToProducts($productCollection, $websiteId = null, $stockId = null)
00458 {
00459 if ($stockId === null) {
00460 $stockId = Mage_CatalogInventory_Model_Stock::DEFAULT_STOCK_ID;
00461 }
00462 if ($websiteId === null) {
00463 $websiteId = Mage::app()->getStore()->getWebsiteId();
00464 }
00465 $productIds = array();
00466 foreach ($productCollection as $product) {
00467 $productIds[] = $product->getId();
00468 }
00469
00470 if (!empty($productIds)) {
00471 $stockStatuses = $this->_getResource()->getProductStatus($productIds, $websiteId, $stockId);
00472 foreach ($stockStatuses as $productId => $status) {
00473 if ($product = $productCollection->getItemById($productId)) {
00474 $product->setIsSalable($status);
00475 }
00476 }
00477 }
00478
00479
00480 foreach ($productCollection as $product) {
00481 $object = new Varien_Object(array('is_in_stock' => $product->getData('is_salable')));
00482 $product->setStockItem($object);
00483 }
00484
00485 return $this;
00486 }
00487
00488
00489
00490
00491
00492
00493
00494
00495 public function addStockStatusToSelect(Varien_Db_Select $select, Mage_Core_Model_Website $website)
00496 {
00497 $this->_getResource()->addStockStatusToSelect($select, $website);
00498 return $this;
00499 }
00500 }