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: class Mage_Sales_Model_Api2_Order extends Mage_Api2_Model_Resource
35: {
36: 37: 38:
39: const PARAM_GIFT_MESSAGE = '_gift_message';
40: const = '_order_comments';
41: const PARAM_PAYMENT_METHOD = '_payment_method';
42: const PARAM_TAX_NAME = '_tax_name';
43: const PARAM_TAX_RATE = '_tax_rate';
44:
45:
46: 47: 48: 49: 50: 51:
52: protected function _addGiftMessageInfo(Mage_Sales_Model_Resource_Order_Collection $collection)
53: {
54: $collection->getSelect()->joinLeft(
55: array('gift_message' => $collection->getTable('giftmessage/message')),
56: 'main_table.gift_message_id = gift_message.gift_message_id',
57: array(
58: 'gift_message_from' => 'gift_message.sender',
59: 'gift_message_to' => 'gift_message.recipient',
60: 'gift_message_body' => 'gift_message.message'
61: )
62: );
63:
64: return $this;
65: }
66:
67: 68: 69: 70: 71: 72:
73: protected function _addPaymentMethodInfo(Mage_Sales_Model_Resource_Order_Collection $collection)
74: {
75: $collection->getSelect()->joinLeft(
76: array('payment_method' => $collection->getTable('sales/order_payment')),
77: 'main_table.entity_id = payment_method.parent_id',
78: array('payment_method' => 'payment_method.method')
79: );
80:
81: return $this;
82: }
83:
84: 85: 86: 87: 88: 89:
90: protected function _addTaxInfo(Mage_Sales_Model_Resource_Order_Collection $collection)
91: {
92: $taxInfoFields = array();
93:
94: if ($this->_isTaxNameAllowed()) {
95: $taxInfoFields['tax_name'] = 'order_tax.title';
96: }
97: if ($this->_isTaxRateAllowed()) {
98: $taxInfoFields['tax_rate'] = 'order_tax.percent';
99: }
100: if ($taxInfoFields) {
101: $collection->getSelect()->joinLeft(
102: array('order_tax' => $collection->getTable('sales/order_tax')),
103: 'main_table.entity_id = order_tax.order_id',
104: $taxInfoFields
105: );
106: }
107: return $this;
108: }
109:
110: 111: 112: 113: 114: 115:
116: protected function _getAddresses(array $orderIds)
117: {
118: $addresses = array();
119:
120: if ($this->_isSubCallAllowed('order_address')) {
121:
122: $addressesFilter = $this->_getSubModel('order_address', array())->getFilter();
123:
124: if ($addressesFilter->getAllowedAttributes()) {
125:
126: $collection = Mage::getResourceModel('sales/order_address_collection');
127:
128: $collection->addAttributeToFilter('parent_id', $orderIds);
129:
130: foreach ($collection->getItems() as $item) {
131: $addresses[$item->getParentId()][] = $addressesFilter->out($item->toArray());
132: }
133: }
134: }
135: return $addresses;
136: }
137:
138: 139: 140: 141: 142:
143: protected function _getCollectionForRetrieve()
144: {
145:
146: $collection = Mage::getResourceModel('sales/order_collection');
147:
148: $this->_applyCollectionModifiers($collection);
149:
150: return $collection;
151: }
152:
153: 154: 155: 156: 157: 158:
159: protected function _getCollectionForSingleRetrieve($orderId)
160: {
161:
162: $collection = Mage::getResourceModel('sales/order_collection');
163:
164: return $collection->addFieldToFilter($collection->getResource()->getIdFieldName(), $orderId);
165: }
166:
167: 168: 169: 170: 171: 172:
173: protected function (array $orderIds)
174: {
175: $comments = array();
176:
177: if ($this->_isOrderCommentsAllowed() && $this->_isSubCallAllowed('order_comment')) {
178:
179: $commentsFilter = $this->_getSubModel('order_comment', array())->getFilter();
180:
181: if ($commentsFilter->getAllowedAttributes()) {
182: foreach ($this->_getCommentsCollection($orderIds)->getItems() as $item) {
183: $comments[$item->getParentId()][] = $commentsFilter->out($item->toArray());
184: }
185: }
186: }
187: return $comments;
188: }
189:
190: 191: 192: 193: 194: 195:
196: protected function (array $orderIds)
197: {
198:
199: $collection = Mage::getResourceModel('sales/order_status_history_collection');
200: $collection->setOrderFilter($orderIds);
201:
202: return $collection;
203: }
204:
205: 206: 207: 208: 209: 210:
211: protected function _getItems(array $orderIds)
212: {
213: $items = array();
214:
215: if ($this->_isSubCallAllowed('order_item')) {
216:
217: $itemsFilter = $this->_getSubModel('order_item', array())->getFilter();
218:
219: if ($itemsFilter->getAllowedAttributes()) {
220:
221: $collection = Mage::getResourceModel('sales/order_item_collection');
222:
223: $collection->addAttributeToFilter('order_id', $orderIds);
224:
225: foreach ($collection->getItems() as $item) {
226: $items[$item->getOrderId()][] = $itemsFilter->out($item->toArray());
227: }
228: }
229: }
230: return $items;
231: }
232:
233: 234: 235: 236: 237:
238: public function _isGiftMessageAllowed()
239: {
240: return in_array(self::PARAM_GIFT_MESSAGE, $this->getFilter()->getAllowedAttributes());
241: }
242:
243: 244: 245: 246: 247:
248: public function ()
249: {
250: return in_array(self::PARAM_ORDER_COMMENTS, $this->getFilter()->getAllowedAttributes());
251: }
252:
253: 254: 255: 256: 257:
258: public function _isPaymentMethodAllowed()
259: {
260: return in_array(self::PARAM_PAYMENT_METHOD, $this->getFilter()->getAllowedAttributes());
261: }
262:
263: 264: 265: 266: 267:
268: public function _isTaxNameAllowed()
269: {
270: return in_array(self::PARAM_TAX_NAME, $this->getFilter()->getAllowedAttributes());
271: }
272:
273: 274: 275: 276: 277:
278: public function _isTaxRateAllowed()
279: {
280: return in_array(self::PARAM_TAX_RATE, $this->getFilter()->getAllowedAttributes());
281: }
282:
283: 284: 285: 286: 287:
288: protected function _retrieveCollection()
289: {
290: $collection = $this->_getCollectionForRetrieve();
291:
292: if ($this->_isPaymentMethodAllowed()) {
293: $this->_addPaymentMethodInfo($collection);
294: }
295: if ($this->_isGiftMessageAllowed()) {
296: $this->_addGiftMessageInfo($collection);
297: }
298: $this->_addTaxInfo($collection);
299:
300: $ordersData = array();
301:
302: foreach ($collection->getItems() as $order) {
303: $ordersData[$order->getId()] = $order->toArray();
304: }
305: if ($ordersData) {
306: foreach ($this->_getAddresses(array_keys($ordersData)) as $orderId => $addresses) {
307: $ordersData[$orderId]['addresses'] = $addresses;
308: }
309: foreach ($this->_getItems(array_keys($ordersData)) as $orderId => $items) {
310: $ordersData[$orderId]['order_items'] = $items;
311: }
312: foreach ($this->_getComments(array_keys($ordersData)) as $orderId => $comments) {
313: $ordersData[$orderId]['order_comments'] = $comments;
314: }
315: }
316: return $ordersData;
317: }
318: }
319: