forked from magento/magento2
/
Collection.php
executable file
·242 lines (224 loc) · 7.83 KB
/
Collection.php
1
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* @category Mage
* @package Mage_Sales
* @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
/**
* Flat sales order collection
*
* @category Mage
* @package Mage_Sales
* @author Magento Core Team <core@magentocommerce.com>
*/
class Mage_Sales_Model_Resource_Order_Collection extends Mage_Sales_Model_Resource_Collection_Abstract
{
/**
* Event prefix
*
* @var string
*/
protected $_eventPrefix = 'sales_order_collection';
/**
* Event object
*
* @var string
*/
protected $_eventObject = 'order_collection';
/**
* Model initialization
*
*/
protected function _construct()
{
$this->_init('Mage_Sales_Model_Order', 'Mage_Sales_Model_Resource_Order');
$this
->addFilterToMap('entity_id', 'main_table.entity_id')
->addFilterToMap('customer_id', 'main_table.customer_id')
->addFilterToMap('quote_address_id', 'main_table.quote_address_id');
}
/**
* Add items count expr to collection select, backward capability with eav structure
*
* @return Mage_Sales_Model_Resource_Order_Collection
*/
public function addItemCountExpr()
{
if (is_null($this->_fieldsToSelect)) {
// If we select all fields from table, we need to add column alias
$this->getSelect()->columns(array('items_count'=>'total_item_count'));
} else {
$this->addFieldToSelect('total_item_count', 'items_count');
}
return $this;
}
/**
* Minimize usual count select
*
* @return Varien_Db_Select
*/
public function getSelectCountSql()
{
/* @var $countSelect Varien_Db_Select */
$countSelect = parent::getSelectCountSql();
$countSelect->resetJoinLeft();
return $countSelect;
}
/**
* Reset left join
*
* @param int $limit
* @param int $offset
* @return Mage_Eav_Model_Entity_Collection_Abstract
*/
protected function _getAllIdsSelect($limit = null, $offset = null)
{
$idsSelect = parent::_getAllIdsSelect($limit, $offset);
$idsSelect->resetJoinLeft();
return $idsSelect;
}
/**
* Join table sales_flat_order_address to select for billing and shipping order addresses.
* Create corillation map
*
* @return Mage_Sales_Model_Resource_Order_Collection
*/
protected function _addAddressFields()
{
$billingAliasName = 'billing_o_a';
$shippingAliasName = 'shipping_o_a';
$joinTable = $this->getTable('sales_flat_order_address');
$this
->addFilterToMap('billing_firstname', $billingAliasName . '.firstname')
->addFilterToMap('billing_lastname', $billingAliasName . '.lastname')
->addFilterToMap('billing_telephone', $billingAliasName . '.telephone')
->addFilterToMap('billing_postcode', $billingAliasName . '.postcode')
->addFilterToMap('shipping_firstname', $shippingAliasName . '.firstname')
->addFilterToMap('shipping_lastname', $shippingAliasName . '.lastname')
->addFilterToMap('shipping_telephone', $shippingAliasName . '.telephone')
->addFilterToMap('shipping_postcode', $shippingAliasName . '.postcode');
$this
->getSelect()
->joinLeft(
array($billingAliasName => $joinTable),
"(main_table.entity_id = {$billingAliasName}.parent_id"
. " AND {$billingAliasName}.address_type = 'billing')",
array(
$billingAliasName . '.firstname',
$billingAliasName . '.lastname',
$billingAliasName . '.telephone',
$billingAliasName . '.postcode'
)
)
->joinLeft(
array($shippingAliasName => $joinTable),
"(main_table.entity_id = {$shippingAliasName}.parent_id"
. " AND {$shippingAliasName}.address_type = 'shipping')",
array(
$shippingAliasName . '.firstname',
$shippingAliasName . '.lastname',
$shippingAliasName . '.telephone',
$shippingAliasName . '.postcode'
)
);
Mage::getResourceHelper('Mage_Core')->prepareColumnsList($this->getSelect());
return $this;
}
/**
* Add addresses information to select
*
* @return Mage_Sales_Model_Resource_Collection_Abstract
*/
public function addAddressFields()
{
return $this->_addAddressFields();
}
/**
* Add field search filter to collection as OR condition
*
* @see self::_getConditionSql for $condition
*
* @param string $field
* @param null|string|array $condition
* @return Mage_Sales_Model_Resource_Order_Collection
*/
public function addFieldToSearchFilter($field, $condition = null)
{
$field = $this->_getMappedField($field);
$this->_select->orWhere($this->_getConditionSql($field, $condition));
return $this;
}
/**
* Specify collection select filter by attribute value
*
* @param array $attributes
* @param array|integer|string|null $condition
* @return Mage_Sales_Model_Resource_Order_Collection
*/
public function addAttributeToSearchFilter($attributes, $condition = null)
{
if (is_array($attributes) && !empty($attributes)) {
$this->_addAddressFields();
$toFilterData = array();
foreach ($attributes as $attribute) {
$this->addFieldToSearchFilter($this->_attributeToField($attribute['attribute']), $attribute);
}
} else {
$this->addAttributeToFilter($attributes, $condition);
}
return $this;
}
/**
* Add filter by specified billing agreements
*
* @param int|array $agreements
* @return Mage_Sales_Model_Resource_Order_Collection
*/
public function addBillingAgreementsFilter($agreements)
{
$agreements = (is_array($agreements)) ? $agreements : array($agreements);
$this->getSelect()
->joinInner(
array('sbao' => $this->getTable('sales_billing_agreement_order')),
'main_table.entity_id = sbao.order_id',
array())
->where('sbao.agreement_id IN(?)', $agreements);
return $this;
}
/**
* Add filter by specified recurring profile id(s)
*
* @param array|int $ids
* @return Mage_Sales_Model_Resource_Order_Collection
*/
public function addRecurringProfilesFilter($ids)
{
$ids = (is_array($ids)) ? $ids : array($ids);
$this->getSelect()
->joinInner(
array('srpo' => $this->getTable('sales_recurring_profile_order')),
'main_table.entity_id = srpo.order_id',
array())
->where('srpo.profile_id IN(?)', $ids);
return $this;
}
}