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_Sales_Model_Resource_Quote extends Mage_Sales_Model_Resource_Abstract
36: {
37: 38: 39: 40:
41: protected function _construct()
42: {
43: $this->_init('sales/quote', 'entity_id');
44: }
45:
46: 47: 48: 49: 50: 51: 52: 53:
54: protected function _getLoadSelect($field, $value, $object)
55: {
56: $select = parent::_getLoadSelect($field, $value, $object);
57: $storeIds = $object->getSharedStoreIds();
58: if ($storeIds) {
59: $select->where('store_id IN (?)', $storeIds);
60: } else {
61: 62: 63:
64: $select->where('store_id < ?', 0);
65: }
66:
67: return $select;
68: }
69:
70: 71: 72: 73: 74: 75: 76:
77: public function loadByCustomerId($quote, $customerId)
78: {
79: $adapter = $this->_getReadAdapter();
80: $select = $this->_getLoadSelect('customer_id', $customerId, $quote)
81: ->where('is_active = ?', 1)
82: ->order('updated_at ' . Varien_Db_Select::SQL_DESC)
83: ->limit(1);
84:
85: $data = $adapter->fetchRow($select);
86:
87: if ($data) {
88: $quote->setData($data);
89: }
90:
91: $this->_afterLoad($quote);
92:
93: return $this;
94: }
95:
96: 97: 98: 99: 100: 101: 102:
103: public function loadActive($quote, $quoteId)
104: {
105: $adapter = $this->_getReadAdapter();
106: $select = $this->_getLoadSelect('entity_id', $quoteId, $quote)
107: ->where('is_active = ?', 1);
108:
109: $data = $adapter->fetchRow($select);
110: if ($data) {
111: $quote->setData($data);
112: }
113:
114: $this->_afterLoad($quote);
115:
116: return $this;
117: }
118:
119: 120: 121: 122: 123: 124: 125:
126: public function loadByIdWithoutStore($quote, $quoteId)
127: {
128: $read = $this->_getReadAdapter();
129: if ($read) {
130: $select = parent::_getLoadSelect('entity_id', $quoteId, $quote);
131:
132: $data = $read->fetchRow($select);
133:
134: if ($data) {
135: $quote->setData($data);
136: }
137: }
138:
139: $this->_afterLoad($quote);
140: return $this;
141: }
142:
143: 144: 145: 146: 147: 148:
149: public function getReservedOrderId($quote)
150: {
151: $storeId = (int)$quote->getStoreId();
152: return Mage::getSingleton('eav/config')->getEntityType(Mage_Sales_Model_Order::ENTITY)
153: ->fetchNewIncrementId($storeId);
154: }
155:
156: 157: 158: 159: 160: 161:
162: public function isOrderIncrementIdUsed($orderIncrementId)
163: {
164: $adapter = $this->_getReadAdapter();
165: $bind = array(':increment_id' => (int)$orderIncrementId);
166: $select = $adapter->select();
167: $select->from($this->getTable('sales/order'), 'entity_id')
168: ->where('increment_id = :increment_id');
169: $entity_id = $adapter->fetchOne($select, $bind);
170: if ($entity_id > 0) {
171: return true;
172: }
173:
174: return false;
175: }
176:
177: 178: 179: 180: 181:
182: public function markQuotesRecollectOnCatalogRules()
183: {
184: $tableQuote = $this->getTable('sales/quote');
185: $subSelect = $this->_getReadAdapter()
186: ->select()
187: ->from(array('t2' => $this->getTable('sales/quote_item')), array('entity_id' => 'quote_id'))
188: ->from(array('t3' => $this->getTable('catalogrule/rule_product_price')), array())
189: ->where('t2.product_id = t3.product_id')
190: ->group('quote_id');
191:
192: $select = $this->_getReadAdapter()->select()->join(
193: array('t2' => $subSelect),
194: 't1.entity_id = t2.entity_id',
195: array('trigger_recollect' => new Zend_Db_Expr('1'))
196: );
197:
198: $updateQuery = $select->crossUpdateFromSelect(array('t1' => $tableQuote));
199:
200: $this->_getWriteAdapter()->query($updateQuery);
201:
202: return $this;
203: }
204:
205: 206: 207: 208: 209: 210:
211: public function substractProductFromQuotes($product)
212: {
213: $productId = (int)$product->getId();
214: if (!$productId) {
215: return $this;
216: }
217: $adapter = $this->_getWriteAdapter();
218: $subSelect = $adapter->select();
219:
220: $subSelect->from(false, array(
221: 'items_qty' => new Zend_Db_Expr(
222: $adapter->quoteIdentifier('q.items_qty') . ' - ' . $adapter->quoteIdentifier('qi.qty')),
223: 'items_count' => new Zend_Db_Expr($adapter->quoteIdentifier('q.items_count') . ' - 1')
224: ))
225: ->join(
226: array('qi' => $this->getTable('sales/quote_item')),
227: implode(' AND ', array(
228: 'q.entity_id = qi.quote_id',
229: 'qi.parent_item_id IS NULL',
230: $adapter->quoteInto('qi.product_id = ?', $productId)
231: )),
232: array()
233: );
234:
235: $updateQuery = $adapter->updateFromSelect($subSelect, array('q' => $this->getTable('sales/quote')));
236:
237: $adapter->query($updateQuery);
238:
239: return $this;
240: }
241:
242: 243: 244: 245: 246: 247:
248: public function markQuotesRecollect($productIds)
249: {
250: $tableQuote = $this->getTable('sales/quote');
251: $tableItem = $this->getTable('sales/quote_item');
252: $subSelect = $this->_getReadAdapter()
253: ->select()
254: ->from($tableItem, array('entity_id' => 'quote_id'))
255: ->where('product_id IN ( ? )', $productIds)
256: ->group('quote_id');
257:
258: $select = $this->_getReadAdapter()->select()->join(
259: array('t2' => $subSelect),
260: 't1.entity_id = t2.entity_id',
261: array('trigger_recollect' => new Zend_Db_Expr('1'))
262: );
263: $updateQuery = $select->crossUpdateFromSelect(array('t1' => $tableQuote));
264: $this->_getWriteAdapter()->query($updateQuery);
265:
266: return $this;
267: }
268: }
269:
270: