1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Sales
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27: /**
28: * Quote entity resource model
29: *
30: * @category Mage
31: * @package Mage_Sales
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_Sales_Model_Entity_Quote extends Mage_Eav_Model_Entity_Abstract
35: {
36:
37: public function __construct()
38: {
39: $resource = Mage::getSingleton('core/resource');
40: $this->setType('quote')->setConnection(
41: $resource->getConnection('sales_read'),
42: $resource->getConnection('sales_write')
43: );
44: }
45:
46: /**
47: * Retrieve select object for loading base entity row
48: *
49: * @param Varien_Object $object
50: * @param mixed $rowId
51: * @return Zend_Db_Select
52: */
53: protected function _getLoadRowSelect($object, $rowId)
54: {
55: $select = parent::_getLoadRowSelect($object, $rowId);
56: if ($object->getSharedStoreIds()) {
57: $select->where('store_id IN (?)', $object->getSharedStoreIds());
58: }
59: return $select;
60: }
61:
62: /**
63: * Loading quote by customer identifier
64: *
65: * @param Mage_Sales_Model_Quote $quote
66: * @param int $customerId
67: */
68: public function loadByCustomerId($quote, $customerId)
69: {
70: $collection = Mage::getResourceModel('sales/quote_collection')
71: ->addAttributeToSelect('entity_id')
72: ->addAttributeToFilter('customer_id', $customerId)
73: ->addAttributeToFilter('is_active', 1);
74:
75: if ($quote->getSharedStoreIds()) {
76: $collection->addAttributeToFilter('store_id', array('in', $quote->getSharedStoreIds()));
77: }
78:
79: $collection->setOrder('updated_at', 'desc')
80: ->setPageSize(1)
81: ->load();
82:
83: if ($collection->getSize()) {
84: foreach ($collection as $item) {
85: $this->load($quote, $item->getId());
86: return $this;
87: }
88: }
89: return $this;
90: }
91:
92: /**
93: * Loading quote by identifier
94: *
95: * @param Mage_Sales_Model_Quote $quote
96: * @param int $quoteId
97: */
98: public function loadByIdWithoutStore($quote, $quoteId)
99: {
100: $collection = Mage::getResourceModel('sales/quote_collection')
101: ->addAttributeToSelect('entity_id')
102: ->addAttributeToFilter('entity_id', $quoteId);
103:
104: $collection->setPageSize(1)
105: ->load();
106:
107: if ($collection->getSize()) {
108: foreach ($collection as $item) {
109: $this->load($quote, $item->getId());
110: return $this;
111: }
112: }
113: return $this;
114: }
115:
116: public function getReservedOrderId($quote)
117: {
118: return Mage::getSingleton('eav/config')->getEntityType('order')->fetchNewIncrementId($quote->getStoreId());
119: }
120: }
121: