00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Mage_Sales_Model_Order_Api extends Mage_Sales_Model_Api_Resource
00035 {
00036 public function __construct()
00037 {
00038 $this->_attributesMap['order'] = array('order_id' => 'entity_id');
00039 $this->_attributesMap['order_address'] = array('address_id' => 'entity_id');
00040 $this->_attributesMap['order_payment'] = array('payment_id' => 'entity_id');
00041
00042 }
00043
00044
00045
00046
00047
00048
00049
00050 protected function _initOrder($orderIncrementId)
00051 {
00052 $order = Mage::getModel('sales/order');
00053
00054
00055
00056 $order->loadByIncrementId($orderIncrementId);
00057
00058 if (!$order->getId()) {
00059 $this->_fault('not_exists');
00060 }
00061
00062 return $order;
00063 }
00064
00065
00066
00067
00068
00069
00070
00071 public function items($filters = null)
00072 {
00073
00074 $collection = Mage::getResourceModel('sales/order_collection')
00075 ->addAttributeToSelect('*')
00076 ->joinAttribute('billing_firstname', 'order_address/firstname', 'billing_address_id', null, 'left')
00077 ->joinAttribute('billing_lastname', 'order_address/lastname', 'billing_address_id', null, 'left')
00078 ->joinAttribute('shipping_firstname', 'order_address/firstname', 'shipping_address_id', null, 'left')
00079 ->joinAttribute('shipping_lastname', 'order_address/lastname', 'shipping_address_id', null, 'left')
00080 ->addExpressionAttributeToSelect('billing_name',
00081 'CONCAT({{billing_firstname}}, " ", {{billing_lastname}})',
00082 array('billing_firstname', 'billing_lastname'))
00083 ->addExpressionAttributeToSelect('shipping_name',
00084 'CONCAT({{shipping_firstname}}, " ", {{shipping_lastname}})',
00085 array('shipping_firstname', 'shipping_lastname'));
00086
00087
00088 if (is_array($filters)) {
00089 try {
00090 foreach ($filters as $field => $value) {
00091 if (isset($this->_attributesMap['order'][$field])) {
00092 $field = $this->_attributesMap['order'][$field];
00093 }
00094
00095 $collection->addFieldToFilter($field, $value);
00096 }
00097 } catch (Mage_Core_Exception $e) {
00098 $this->_fault('filters_invalid', $e->getMessage());
00099 }
00100 }
00101
00102 $result = array();
00103
00104 foreach ($collection as $order) {
00105 $result[] = $this->_getAttributes($order, 'order');
00106 }
00107
00108 return $result;
00109 }
00110
00111
00112
00113
00114
00115
00116
00117 public function info($orderIncrementId)
00118 {
00119 $order = $this->_initOrder($orderIncrementId);
00120
00121 $result = $this->_getAttributes($order, 'order');
00122
00123 $result['shipping_address'] = $this->_getAttributes($order->getShippingAddress(), 'order_address');
00124 $result['billing_address'] = $this->_getAttributes($order->getBillingAddress(), 'order_address');
00125 $result['items'] = array();
00126
00127 foreach ($order->getAllItems() as $item) {
00128 $result['items'][] = $this->_getAttributes($item, 'order_item');
00129 }
00130
00131 $result['payment'] = $this->_getAttributes($order->getPayment(), 'order_payment');
00132
00133 $result['status_history'] = array();
00134
00135 foreach ($order->getAllStatusHistory() as $history) {
00136 $result['status_history'][] = $this->_getAttributes($history, 'order_status_history');
00137 }
00138
00139 return $result;
00140 }
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151 public function addComment($orderIncrementId, $status, $comment = null, $notify = false)
00152 {
00153 $order = $this->_initOrder($orderIncrementId);
00154
00155 $order->addStatusToHistory($status, $comment, $notify);
00156
00157
00158 try {
00159 if ($notify && $comment) {
00160 $oldStore = Mage::getDesign()->getStore();
00161 $oldArea = Mage::getDesign()->getArea();
00162 Mage::getDesign()->setStore($order->getStoreId());
00163 Mage::getDesign()->setArea('frontend');
00164 }
00165
00166 $order->sendOrderUpdateEmail($notify, $comment);
00167 $order->save();
00168 if ($notify && $comment) {
00169 Mage::getDesign()->setStore($oldStore);
00170 Mage::getDesign()->setArea($oldArea);
00171 }
00172
00173 } catch (Mage_Core_Exception $e) {
00174 $this->_fault('status_not_changed', $e->getMessage());
00175 }
00176
00177 return true;
00178 }
00179
00180
00181
00182
00183
00184
00185
00186 public function hold($orderIncrementId)
00187 {
00188 $order = $this->_initOrder($orderIncrementId);
00189
00190 try {
00191 $order->hold();
00192 $order->save();
00193 } catch (Mage_Core_Exception $e) {
00194 $this->_fault('status_not_changed', $e->getMessage());
00195 }
00196
00197 return true;
00198 }
00199
00200
00201
00202
00203
00204
00205
00206 public function unhold($orderIncrementId)
00207 {
00208 $order = $this->_initOrder($orderIncrementId);
00209
00210 try {
00211 $order->unhold();
00212 $order->save();
00213 } catch (Mage_Core_Exception $e) {
00214 $this->_fault('status_not_changed', $e->getMessage());
00215 }
00216
00217 return true;
00218 }
00219
00220
00221
00222
00223
00224
00225
00226 public function cancel($orderIncrementId)
00227 {
00228 $order = $this->_initOrder($orderIncrementId);
00229
00230 try {
00231 $order->cancel();
00232 $order->save();
00233 } catch (Mage_Core_Exception $e) {
00234 $this->_fault('status_not_changed', $e->getMessage());
00235 }
00236
00237 return true;
00238 }
00239
00240 }