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_Mysql4_Stock_Status extends Mage_Core_Model_Mysql4_Abstract
00035 {
00036
00037
00038
00039
00040 protected function _construct()
00041 {
00042 $this->_init('cataloginventory/stock_status', 'product_id');
00043 }
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 public function saveProductStatus(Mage_CatalogInventory_Model_Stock_Status $object,
00057 $productId, $status, $qty = 0, $stockId = 1, $websiteId = null)
00058 {
00059 $websites = array_keys($object->getWebsites($websiteId));
00060
00061 foreach ($websites as $websiteId) {
00062 $select = $this->_getWriteAdapter()->select()
00063 ->from($this->getMainTable())
00064 ->where('product_id=?', $productId)
00065 ->where('website_id=?', $websiteId)
00066 ->where('stock_id=?', $stockId);
00067 if ($row = $this->_getWriteAdapter()->fetchRow($select)) {
00068 $bind = array(
00069 'qty' => $qty,
00070 'stock_status' => $status
00071 );
00072 $where = array(
00073 $this->_getWriteAdapter()->quoteInto('product_id=?', $row['product_id']),
00074 $this->_getWriteAdapter()->quoteInto('website_id=?', $row['website_id']),
00075 $this->_getWriteAdapter()->quoteInto('stock_id=?', $row['stock_id']),
00076 );
00077 $this->_getWriteAdapter()->update($this->getMainTable(), $bind, $where);
00078 }
00079 else {
00080 $bind = array(
00081 'product_id' => $productId,
00082 'website_id' => $websiteId,
00083 'stock_id' => $stockId,
00084 'qty' => $qty,
00085 'stock_status' => $status
00086 );
00087 $this->_getWriteAdapter()->insert($this->getMainTable(), $bind);
00088 }
00089 }
00090
00091 return $this;
00092 }
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104 public function getProductStatus($productIds, $websiteId, $stockId = 1)
00105 {
00106 if (!is_array($productIds)) {
00107 $productIds = array($productIds);
00108 }
00109
00110 $select = $this->_getReadAdapter()->select()
00111 ->from($this->getMainTable(), array('product_id', 'stock_status'))
00112 ->where('product_id IN(?)', $productIds)
00113 ->where('stock_id=?', $stockId)
00114 ->where('website_id=?', $websiteId);
00115 return $this->_getReadAdapter()->fetchPairs($select);
00116 }
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127 public function getProductData($productIds, $websiteId, $stockId = 1)
00128 {
00129 if (!is_array($productIds)) {
00130 $productIds = array($productIds);
00131 }
00132
00133 $data = array();
00134
00135 $select = $this->_getReadAdapter()->select()
00136 ->from($this->getMainTable())
00137 ->where('product_id IN(?)', $productIds)
00138 ->where('stock_id=?', $stockId)
00139 ->where('website_id=?', $websiteId);
00140 $query = $this->_getReadAdapter()->query($select);
00141 while ($row = $query->fetch()) {
00142 $data[$row['product_id']] = $row;
00143 }
00144 return $data;
00145 }
00146
00147
00148
00149
00150
00151
00152
00153 public function getWebsiteStores() {
00154 $select = Mage::getModel('core/website')->getDefaultStoresSelect(false);
00155 return $this->_getReadAdapter()->fetchPairs($select);
00156 }
00157
00158
00159
00160
00161
00162
00163
00164 public function getProductsType($productIds)
00165 {
00166 if (!is_array($productIds)) {
00167 $productIds = array($productIds);
00168 }
00169
00170 $select = $this->_getReadAdapter()->select()
00171 ->from(
00172 array('e' => $this->getTable('catalog/product')),
00173 array('entity_id', 'type_id'))
00174 ->where('entity_id IN(?)', $productIds);
00175 return $this->_getReadAdapter()->fetchPairs($select);
00176 }
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186 public function getProductCollection($lastEntityId = 0, $limit = 1000) {
00187 $select = $this->_getReadAdapter()->select()
00188 ->from(
00189 array('e' => $this->getTable('catalog/product')),
00190 array('entity_id', 'type_id'))
00191 ->order('entity_id ASC')
00192 ->where('entity_id>?', $lastEntityId)
00193 ->limit($limit);
00194 return $this->_getReadAdapter()->fetchPairs($select);
00195 }
00196
00197
00198
00199
00200
00201
00202
00203
00204 public function addStockStatusToSelect(Varien_Db_Select $select, Mage_Core_Model_Website $website)
00205 {
00206 $websiteId = $website->getId();
00207 $select->joinLeft(
00208 array('stock_status' => $this->getMainTable()),
00209 'e.entity_id=stock_status.product_id AND stock_status.website_id='.$websiteId,
00210 array('salable' => 'stock_status.stock_status')
00211 );
00212
00213 return $this;
00214 }
00215 }