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: /**
29: * Flat sales order resource
30: *
31: * @category Mage
32: * @package Mage_Sales
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_Sales_Model_Resource_Order extends Mage_Sales_Model_Resource_Order_Abstract
36: {
37: /**
38: * Event prefix
39: *
40: * @var string
41: */
42: protected $_eventPrefix = 'sales_order_resource';
43:
44: /**
45: * Event object
46: *
47: * @var string
48: */
49: protected $_eventObject = 'resource';
50:
51: /**
52: * Is grid
53: *
54: * @var boolean
55: */
56: protected $_grid = true;
57:
58: /**
59: * Use increment id
60: *
61: * @var boolean
62: */
63: protected $_useIncrementId = true;
64:
65: /**
66: * Entity code for increment id
67: *
68: * @var string
69: */
70: protected $_entityCodeForIncrementId = 'order';
71:
72: /**
73: * Model Initialization
74: *
75: */
76: protected function _construct()
77: {
78: $this->_init('sales/order', 'entity_id');
79: }
80:
81: /**
82: * Init virtual grid records for entity
83: *
84: * @return Mage_Sales_Model_Resource_Order
85: */
86: protected function _initVirtualGridColumns()
87: {
88: parent::_initVirtualGridColumns();
89: $adapter = $this->getReadConnection();
90: $ifnullFirst = $adapter->getIfNullSql('{{table}}.firstname', $adapter->quote(''));
91: $ifnullLast = $adapter->getIfNullSql('{{table}}.lastname', $adapter->quote(''));
92: $concatAddress = $adapter->getConcatSql(array($ifnullFirst, $adapter->quote(' '), $ifnullLast));
93: $this->addVirtualGridColumn(
94: 'billing_name',
95: 'sales/order_address',
96: array('billing_address_id' => 'entity_id'),
97: $concatAddress
98: )
99: ->addVirtualGridColumn(
100: 'shipping_name',
101: 'sales/order_address',
102: array('shipping_address_id' => 'entity_id'),
103: $concatAddress
104: );
105:
106: return $this;
107: }
108:
109: /**
110: * Count existent products of order items by specified product types
111: *
112: * @param int $orderId
113: * @param array $productTypeIds
114: * @param bool $isProductTypeIn
115: * @return array
116: */
117: public function aggregateProductsByTypes($orderId, $productTypeIds = array(), $isProductTypeIn = false)
118: {
119: $adapter = $this->getReadConnection();
120: $select = $adapter->select()
121: ->from(
122: array('o' => $this->getTable('sales/order_item')),
123: array('o.product_type', new Zend_Db_Expr('COUNT(*)')))
124: ->joinInner(
125: array('p' => $this->getTable('catalog/product')),
126: 'o.product_id=p.entity_id',
127: array())
128: ->where('o.order_id=?', $orderId)
129: ->group('o.product_type')
130: ;
131: if ($productTypeIds) {
132: $select->where(
133: sprintf('(o.product_type %s (?))', ($isProductTypeIn ? 'IN' : 'NOT IN')),
134: $productTypeIds);
135: }
136: return $adapter->fetchPairs($select);
137: }
138:
139: /**
140: * Retrieve order_increment_id by order_id
141: *
142: * @param int $orderId
143: * @return string
144: */
145: public function getIncrementId($orderId)
146: {
147: $adapter = $this->getReadConnection();
148: $bind = array(':entity_id' => $orderId);
149: $select = $adapter->select()
150: ->from($this->getMainTable(), array("increment_id"))
151: ->where('entity_id = :entity_id');
152: return $adapter->fetchOne($select, $bind);
153: }
154: }
155: