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_Sales_Model_Mysql4_Quote extends Mage_Sales_Model_Mysql4_Abstract
00035 {
00036
00037
00038
00039 protected function _construct()
00040 {
00041 $this->_init('sales/quote', 'entity_id');
00042 }
00043
00044
00045
00046
00047
00048
00049
00050
00051 protected function _getLoadSelect($field, $value, $object)
00052 {
00053 $select = parent::_getLoadSelect($field, $value, $object);
00054 if ($storeIds = $object->getSharedStoreIds()) {
00055 $select->where('store_id IN (?)', $storeIds);
00056 }
00057 else {
00058
00059
00060
00061 $select->where('store_id<0');
00062 }
00063 return $select;
00064 }
00065
00066
00067
00068
00069
00070
00071
00072 public function loadByCustomerId($quote, $customerId)
00073 {
00074 $read = $this->_getReadAdapter();
00075 if ($read) {
00076 $select = $this->_getLoadSelect('customer_id', $customerId, $quote)
00077 ->where('is_active=1')
00078 ->order('updated_at desc')
00079 ->limit(1);
00080
00081 $data = $read->fetchRow($select);
00082
00083 if ($data) {
00084 $quote->setData($data);
00085 }
00086 }
00087
00088 $this->_afterLoad($quote);
00089 return $this;
00090 }
00091
00092 public function getReservedOrderId($quote)
00093 {
00094 return Mage::getSingleton('eav/config')->getEntityType('order')->fetchNewIncrementId($quote->getStoreId());
00095 }
00096
00097 public function isOrderIncrementIdUsed($orderIncrementId) {
00098 if ($this->_getReadAdapter()) {
00099 $select = $this->_getReadAdapter()->select();
00100 $select->from($this->getTable('sales/order'), 'entity_id')
00101 ->where('increment_id = ?', $orderIncrementId);
00102 $entity_id = $this->_getReadAdapter()->fetchOne($select);
00103 if ($entity_id > 0) {
00104 return true;
00105 } else {
00106 return false;
00107 }
00108 }
00109 return false;
00110 }
00111
00112
00113
00114
00115
00116 public function markQuotesRecollectOnCatalogRules()
00117 {
00118 $this->_getWriteAdapter()->query("
00119 UPDATE {$this->getTable('sales/quote')} SET trigger_recollect = 1
00120 WHERE entity_id IN (
00121 SELECT DISTINCT quote_id
00122 FROM {$this->getTable('sales/quote_item')}
00123 WHERE product_id IN (SELECT DISTINCT product_id FROM {$this->getTable('catalogrule/rule_product_price')})
00124 )"
00125 );
00126 }
00127
00128
00129
00130
00131
00132
00133 public function substractProductFromQuotes($product)
00134 {
00135 if ($product->getId()) {
00136 $this->_getWriteAdapter()->query(
00137 'update ' . $this->getTable('sales/quote_item') .
00138 ' as qi, ' . $this->getTable('sales/quote') .
00139 ' as q set q.items_qty = q.items_qty - qi.qty, q.items_count = q.items_count - 1 ' .
00140 ' where qi.product_id = "' . $product->getId() . '" and q.entity_id = qi.quote_id and qi.parent_item_id is null'
00141 );
00142 }
00143 }
00144
00145
00146
00147
00148
00149
00150
00151 public function markQuotesRecollect($productIds)
00152 {
00153 $this->_getWriteAdapter()->query("
00154 UPDATE `{$this->getTable('sales/quote')}` SET `trigger_recollect` = 1
00155 WHERE `entity_id` IN (
00156 SELECT DISTINCT `quote_id`
00157 FROM `{$this->getTable('sales/quote_item')}`
00158 WHERE `product_id` IN (?)
00159 )", $productIds
00160 );
00161
00162 return $this;
00163 }
00164 }