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_Order_Collection extends Mage_Sales_Model_Resource_Collection_Abstract
36: {
37: 38: 39: 40: 41:
42: protected $_eventPrefix = 'sales_order_collection';
43:
44: 45: 46: 47: 48:
49: protected $_eventObject = 'order_collection';
50:
51: 52: 53: 54:
55: protected function _construct()
56: {
57: $this->_init('sales/order');
58: $this
59: ->addFilterToMap('entity_id', 'main_table.entity_id')
60: ->addFilterToMap('customer_id', 'main_table.customer_id')
61: ->addFilterToMap('quote_address_id', 'main_table.quote_address_id');
62: }
63:
64: 65: 66: 67: 68:
69: public function addItemCountExpr()
70: {
71: if (is_null($this->_fieldsToSelect)) {
72:
73: $this->getSelect()->columns(array('items_count'=>'total_item_count'));
74: } else {
75: $this->addFieldToSelect('total_item_count', 'items_count');
76: }
77: return $this;
78: }
79:
80: 81: 82: 83: 84:
85: public function getSelectCountSql()
86: {
87:
88: $countSelect = parent::getSelectCountSql();
89: $countSelect->resetJoinLeft();
90: return $countSelect;
91: }
92:
93: 94: 95: 96: 97: 98: 99:
100: protected function _getAllIdsSelect($limit = null, $offset = null)
101: {
102: $idsSelect = parent::_getAllIdsSelect($limit, $offset);
103: $idsSelect->resetJoinLeft();
104: return $idsSelect;
105: }
106:
107: 108: 109: 110: 111: 112:
113: protected function _addAddressFields()
114: {
115: $billingAliasName = 'billing_o_a';
116: $shippingAliasName = 'shipping_o_a';
117: $joinTable = $this->getTable('sales/order_address');
118:
119: $this
120: ->addFilterToMap('billing_firstname', $billingAliasName . '.firstname')
121: ->addFilterToMap('billing_lastname', $billingAliasName . '.lastname')
122: ->addFilterToMap('billing_telephone', $billingAliasName . '.telephone')
123: ->addFilterToMap('billing_postcode', $billingAliasName . '.postcode')
124:
125: ->addFilterToMap('shipping_firstname', $shippingAliasName . '.firstname')
126: ->addFilterToMap('shipping_lastname', $shippingAliasName . '.lastname')
127: ->addFilterToMap('shipping_telephone', $shippingAliasName . '.telephone')
128: ->addFilterToMap('shipping_postcode', $shippingAliasName . '.postcode');
129:
130: $this
131: ->getSelect()
132: ->joinLeft(
133: array($billingAliasName => $joinTable),
134: "(main_table.entity_id = {$billingAliasName}.parent_id"
135: . " AND {$billingAliasName}.address_type = 'billing')",
136: array(
137: $billingAliasName . '.firstname',
138: $billingAliasName . '.lastname',
139: $billingAliasName . '.telephone',
140: $billingAliasName . '.postcode'
141: )
142: )
143: ->joinLeft(
144: array($shippingAliasName => $joinTable),
145: "(main_table.entity_id = {$shippingAliasName}.parent_id"
146: . " AND {$shippingAliasName}.address_type = 'shipping')",
147: array(
148: $shippingAliasName . '.firstname',
149: $shippingAliasName . '.lastname',
150: $shippingAliasName . '.telephone',
151: $shippingAliasName . '.postcode'
152: )
153: );
154: Mage::getResourceHelper('core')->prepareColumnsList($this->getSelect());
155: return $this;
156: }
157:
158: 159: 160: 161: 162:
163: public function addAddressFields()
164: {
165: return $this->_addAddressFields();
166: }
167:
168: 169: 170: 171: 172: 173: 174: 175: 176:
177: public function addFieldToSearchFilter($field, $condition = null)
178: {
179: $field = $this->_getMappedField($field);
180: $this->_select->orWhere($this->_getConditionSql($field, $condition));
181: return $this;
182: }
183:
184: 185: 186: 187: 188: 189: 190:
191: public function addAttributeToSearchFilter($attributes, $condition = null)
192: {
193: if (is_array($attributes) && !empty($attributes)) {
194: $this->_addAddressFields();
195:
196: $toFilterData = array();
197: foreach ($attributes as $attribute) {
198: $this->addFieldToSearchFilter($this->_attributeToField($attribute['attribute']), $attribute);
199: }
200: } else {
201: $this->addAttributeToFilter($attributes, $condition);
202: }
203:
204: return $this;
205: }
206:
207: 208: 209: 210: 211: 212:
213: public function addBillingAgreementsFilter($agreements)
214: {
215: $agreements = (is_array($agreements)) ? $agreements : array($agreements);
216: $this->getSelect()
217: ->joinInner(
218: array('sbao' => $this->getTable('sales/billing_agreement_order')),
219: 'main_table.entity_id = sbao.order_id',
220: array())
221: ->where('sbao.agreement_id IN(?)', $agreements);
222: return $this;
223: }
224:
225: 226: 227: 228: 229: 230:
231: public function addRecurringProfilesFilter($ids)
232: {
233: $ids = (is_array($ids)) ? $ids : array($ids);
234: $this->getSelect()
235: ->joinInner(
236: array('srpo' => $this->getTable('sales/recurring_profile_order')),
237: 'main_table.entity_id = srpo.order_id',
238: array())
239: ->where('srpo.profile_id IN(?)', $ids);
240: return $this;
241: }
242: }
243: