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_Order_Api extends Mage_Sales_Model_Api_Resource
35: {
36: 37: 38:
39: public function __construct()
40: {
41: $this->_attributesMap = array(
42: 'order' => array('order_id' => 'entity_id'),
43: 'order_address' => array('address_id' => 'entity_id'),
44: 'order_payment' => array('payment_id' => 'entity_id')
45: );
46: }
47:
48: 49: 50: 51: 52: 53:
54: protected function _initOrder($orderIncrementId)
55: {
56: $order = Mage::getModel('sales/order');
57:
58:
59:
60: $order->loadByIncrementId($orderIncrementId);
61:
62: if (!$order->getId()) {
63: $this->_fault('not_exists');
64: }
65:
66: return $order;
67: }
68:
69: 70: 71: 72: 73: 74:
75: public function items($filters = null)
76: {
77: $orders = array();
78:
79:
80: $billingAliasName = 'billing_o_a';
81: $shippingAliasName = 'shipping_o_a';
82:
83:
84: $orderCollection = Mage::getModel("sales/order")->getCollection();
85: $billingFirstnameField = "$billingAliasName.firstname";
86: $billingLastnameField = "$billingAliasName.lastname";
87: $shippingFirstnameField = "$shippingAliasName.firstname";
88: $shippingLastnameField = "$shippingAliasName.lastname";
89: $orderCollection->addAttributeToSelect('*')
90: ->addAddressFields()
91: ->addExpressionFieldToSelect('billing_firstname', "{{billing_firstname}}",
92: array('billing_firstname' => $billingFirstnameField))
93: ->addExpressionFieldToSelect('billing_lastname', "{{billing_lastname}}",
94: array('billing_lastname' => $billingLastnameField))
95: ->addExpressionFieldToSelect('shipping_firstname', "{{shipping_firstname}}",
96: array('shipping_firstname' => $shippingFirstnameField))
97: ->addExpressionFieldToSelect('shipping_lastname', "{{shipping_lastname}}",
98: array('shipping_lastname' => $shippingLastnameField))
99: ->addExpressionFieldToSelect('billing_name', "CONCAT({{billing_firstname}}, ' ', {{billing_lastname}})",
100: array('billing_firstname' => $billingFirstnameField, 'billing_lastname' => $billingLastnameField))
101: ->addExpressionFieldToSelect('shipping_name', 'CONCAT({{shipping_firstname}}, " ", {{shipping_lastname}})',
102: array('shipping_firstname' => $shippingFirstnameField, 'shipping_lastname' => $shippingLastnameField)
103: );
104:
105:
106: $apiHelper = Mage::helper('api');
107: $filters = $apiHelper->parseFilters($filters, $this->_attributesMap['order']);
108: try {
109: foreach ($filters as $field => $value) {
110: $orderCollection->addFieldToFilter($field, $value);
111: }
112: } catch (Mage_Core_Exception $e) {
113: $this->_fault('filters_invalid', $e->getMessage());
114: }
115: foreach ($orderCollection as $order) {
116: $orders[] = $this->_getAttributes($order, 'order');
117: }
118: return $orders;
119: }
120:
121: 122: 123: 124: 125: 126:
127: public function info($orderIncrementId)
128: {
129: $order = $this->_initOrder($orderIncrementId);
130:
131: if ($order->getGiftMessageId() > 0) {
132: $order->setGiftMessage(
133: Mage::getSingleton('giftmessage/message')->load($order->getGiftMessageId())->getMessage()
134: );
135: }
136:
137: $result = $this->_getAttributes($order, 'order');
138:
139: $result['shipping_address'] = $this->_getAttributes($order->getShippingAddress(), 'order_address');
140: $result['billing_address'] = $this->_getAttributes($order->getBillingAddress(), 'order_address');
141: $result['items'] = array();
142:
143: foreach ($order->getAllItems() as $item) {
144: if ($item->getGiftMessageId() > 0) {
145: $item->setGiftMessage(
146: Mage::getSingleton('giftmessage/message')->load($item->getGiftMessageId())->getMessage()
147: );
148: }
149:
150: $result['items'][] = $this->_getAttributes($item, 'order_item');
151: }
152:
153: $result['payment'] = $this->_getAttributes($order->getPayment(), 'order_payment');
154:
155: $result['status_history'] = array();
156:
157: foreach ($order->getAllStatusHistory() as $history) {
158: $result['status_history'][] = $this->_getAttributes($history, 'order_status_history');
159: }
160:
161: return $result;
162: }
163:
164: 165: 166: 167: 168: 169: 170: 171: 172:
173: public function ($orderIncrementId, $status, $comment = null, $notify = false)
174: {
175: $order = $this->_initOrder($orderIncrementId);
176:
177: $order->addStatusToHistory($status, $comment, $notify);
178:
179:
180: try {
181: if ($notify && $comment) {
182: $oldStore = Mage::getDesign()->getStore();
183: $oldArea = Mage::getDesign()->getArea();
184: Mage::getDesign()->setStore($order->getStoreId());
185: Mage::getDesign()->setArea('frontend');
186: }
187:
188: $order->save();
189: $order->sendOrderUpdateEmail($notify, $comment);
190: if ($notify && $comment) {
191: Mage::getDesign()->setStore($oldStore);
192: Mage::getDesign()->setArea($oldArea);
193: }
194:
195: } catch (Mage_Core_Exception $e) {
196: $this->_fault('status_not_changed', $e->getMessage());
197: }
198:
199: return true;
200: }
201:
202: 203: 204: 205: 206: 207:
208: public function hold($orderIncrementId)
209: {
210: $order = $this->_initOrder($orderIncrementId);
211:
212: try {
213: $order->hold();
214: $order->save();
215: } catch (Mage_Core_Exception $e) {
216: $this->_fault('status_not_changed', $e->getMessage());
217: }
218:
219: return true;
220: }
221:
222: 223: 224: 225: 226: 227:
228: public function unhold($orderIncrementId)
229: {
230: $order = $this->_initOrder($orderIncrementId);
231:
232: try {
233: $order->unhold();
234: $order->save();
235: } catch (Mage_Core_Exception $e) {
236: $this->_fault('status_not_changed', $e->getMessage());
237: }
238:
239: return true;
240: }
241:
242: 243: 244: 245: 246: 247:
248: public function cancel($orderIncrementId)
249: {
250: $order = $this->_initOrder($orderIncrementId);
251:
252: if (Mage_Sales_Model_Order::STATE_CANCELED == $order->getState()) {
253: $this->_fault('status_not_changed');
254: }
255: try {
256: $order->cancel();
257: $order->save();
258: } catch (Mage_Core_Exception $e) {
259: $this->_fault('status_not_changed', $e->getMessage());
260: }
261: if (Mage_Sales_Model_Order::STATE_CANCELED != $order->getState()) {
262: $this->_fault('status_not_changed');
263: }
264: return true;
265: }
266:
267: }
268: