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_Adminhtml_Sales_Order_CreateController extends Mage_Adminhtml_Controller_Action
00035 {
00036
00037
00038
00039
00040 protected function _construct()
00041 {
00042 $this->setUsedModuleName('Mage_Sales');
00043 }
00044
00045
00046
00047
00048
00049
00050 protected function _getSession()
00051 {
00052 return Mage::getSingleton('adminhtml/session_quote');
00053 }
00054
00055
00056
00057
00058
00059
00060 protected function _getQuote()
00061 {
00062 return $this->_getSession()->getQuote();
00063 }
00064
00065
00066
00067
00068
00069
00070 protected function _getOrderCreateModel()
00071 {
00072 return Mage::getSingleton('adminhtml/sales_order_create');
00073 }
00074
00075
00076
00077
00078
00079
00080 protected function _getGiftmessageSaveModel()
00081 {
00082 return Mage::getSingleton('adminhtml/giftmessage_save');
00083 }
00084
00085
00086
00087
00088
00089
00090 protected function _initSession()
00091 {
00092
00093
00094
00095 if ($customerId = $this->getRequest()->getParam('customer_id')) {
00096 $this->_getSession()->setCustomerId((int) $customerId);
00097 }
00098
00099
00100
00101
00102 if ($storeId = $this->getRequest()->getParam('store_id')) {
00103 $this->_getSession()->setStoreId((int) $storeId);
00104 }
00105
00106
00107
00108
00109 if ($currencyId = $this->getRequest()->getParam('currency_id')) {
00110 $this->_getSession()->setCurrencyId((string) $currencyId);
00111 $this->_getOrderCreateModel()->setRecollect(true);
00112 }
00113 return $this;
00114 }
00115
00116
00117
00118
00119
00120
00121 protected function _processData()
00122 {
00123
00124
00125
00126 if ($data = $this->getRequest()->getPost('order')) {
00127 $this->_getOrderCreateModel()->importPostData($data);
00128 }
00129
00130
00131
00132
00133 $this->_getOrderCreateModel()->getBillingAddress();
00134
00135
00136
00137
00138 if (!$this->_getOrderCreateModel()->getQuote()->isVirtual()) {
00139 $syncFlag = $this->getRequest()->getPost('shipping_as_billing');
00140 if (!is_null($syncFlag)) {
00141 $this->_getOrderCreateModel()->setShippingAsBilling((int)$syncFlag);
00142 }
00143 }
00144
00145
00146
00147
00148 if (!$this->_getOrderCreateModel()->getQuote()->isVirtual() && $this->getRequest()->getPost('reset_shipping')) {
00149 $this->_getOrderCreateModel()->resetShippingMethod(true);
00150 }
00151
00152
00153
00154
00155 if (!$this->_getOrderCreateModel()->getQuote()->isVirtual() && $this->getRequest()->getPost('collect_shipping_rates')) {
00156 $this->_getOrderCreateModel()->collectShippingRates();
00157 }
00158
00159
00160
00161
00162
00163 if ($data = $this->getRequest()->getPost('sidebar')) {
00164 $this->_getOrderCreateModel()->applySidebarData($data);
00165 }
00166
00167
00168
00169
00170 if ($productId = (int) $this->getRequest()->getPost('add_product')) {
00171 $this->_getOrderCreateModel()->addProduct($productId);
00172 }
00173
00174
00175
00176
00177 if ($data = $this->getRequest()->getPost('add_products')) {
00178 $this->_getOrderCreateModel()->addProducts(Zend_Json::decode($data));
00179 }
00180
00181
00182
00183
00184 if ($this->getRequest()->getPost('update_items')) {
00185 $items = $this->getRequest()->getPost('item', array());
00186 $this->_getOrderCreateModel()->updateQuoteItems($items);
00187 }
00188
00189
00190
00191
00192 if ( ($itemId = (int) $this->getRequest()->getPost('remove_item'))
00193 && ($from = (string) $this->getRequest()->getPost('from'))) {
00194 $this->_getOrderCreateModel($itemId)->removeItem($itemId, $from);
00195 }
00196
00197
00198
00199
00200 if ( ($itemId = (int) $this->getRequest()->getPost('move_item'))
00201 && ($moveTo = (string) $this->getRequest()->getPost('to')) ) {
00202 $this->_getOrderCreateModel()->moveQuoteItem($itemId, $moveTo);
00203 }
00204
00205
00206
00207
00208 if ($paymentData = $this->getRequest()->getPost('payment')) {
00209 $this->_getOrderCreateModel()->getQuote()->getPayment()->addData($paymentData);
00210 }
00211
00212 $eventData = array(
00213 'order_create_model' => $this->_getOrderCreateModel(),
00214 'request' => $this->getRequest()->getPost(),
00215 );
00216
00217 Mage::dispatchEvent('adminhtml_sales_order_create_process_data', $eventData);
00218
00219 $this->_getOrderCreateModel()
00220 ->initRuleData()
00221 ->saveQuote();
00222
00223 if ($paymentData = $this->getRequest()->getPost('payment')) {
00224 $this->_getOrderCreateModel()->getQuote()->getPayment()->addData($paymentData);
00225 }
00226
00227
00228
00229
00230 if ($giftmessages = $this->getRequest()->getPost('giftmessage')) {
00231 $this->_getGiftmessageSaveModel()->setGiftmessages($giftmessages)
00232 ->saveAllInQuote();
00233 }
00234
00235
00236
00237
00238 if ($data = $this->getRequest()->getPost('add_products')) {
00239 $this->_getGiftmessageSaveModel()->importAllowQuoteItemsFromProducts(Zend_Json::decode($data));
00240 }
00241
00242
00243
00244
00245 if ($this->getRequest()->getPost('update_items')) {
00246 $items = $this->getRequest()->getPost('item', array());
00247 $this->_getGiftmessageSaveModel()->importAllowQuoteItemsFromItems($items);
00248 }
00249
00250 $data = $this->getRequest()->getPost('order');
00251 if (!empty($data['coupon']['code'])) {
00252 if ($this->_getQuote()->getCouponCode() !== $data['coupon']['code']) {
00253 $this->_getSession()->addError($this->__('"%s" coupon code is not valid.', $data['coupon']['code']));
00254 }
00255 }
00256
00257 return $this;
00258 }
00259
00260
00261
00262
00263 public function indexAction()
00264 {
00265 $this->loadLayout();
00266
00267 $this->_initSession()
00268 ->_setActiveMenu('sales/order')
00269 ->renderLayout();
00270 }
00271
00272
00273 public function reorderAction()
00274 {
00275
00276 $this->_getSession()->clear();
00277 $orderId = $this->getRequest()->getParam('order_id');
00278 $order = Mage::getModel('sales/order')->load($orderId);
00279
00280 if ($order->getId()) {
00281 $order->setReordered(true);
00282 $this->_getSession()->setUseOldShippingMethod(true);
00283 $this->_getOrderCreateModel()->initFromOrder($order);
00284
00285 $this->_redirect('*/*');
00286 }
00287 else {
00288 $this->_redirect('*/sales_order/');
00289 }
00290 }
00291
00292 protected function _reloadQuote()
00293 {
00294 $id = $this->_getQuote()->getId();
00295 $this->_getQuote()->load($id);
00296 return $this;
00297 }
00298
00299
00300
00301
00302 public function loadBlockAction()
00303 {
00304 try {
00305 $this->_initSession()
00306 ->_processData();
00307 }
00308 catch (Mage_Core_Exception $e){
00309 $this->_reloadQuote();
00310 $this->_getSession()->addError($e->getMessage());
00311 }
00312 catch (Exception $e){
00313 $this->_reloadQuote();
00314
00315 $this->_getSession()->addException($e, $e->getMessage());
00316 }
00317
00318
00319 $asJson= $this->getRequest()->getParam('json');
00320 $block = $this->getRequest()->getParam('block');
00321
00322 $update = $this->getLayout()->getUpdate();
00323 if ($asJson) {
00324 $update->addHandle('adminhtml_sales_order_create_load_block_json');
00325 } else {
00326 $update->addHandle('adminhtml_sales_order_create_load_block_plain');
00327 }
00328
00329 if ($block) {
00330 $blocks = explode(',', $block);
00331 if ($asJson && !in_array('message', $blocks)) {
00332 $blocks[] = 'message';
00333 }
00334
00335 foreach ($blocks as $block) {
00336 $update->addHandle('adminhtml_sales_order_create_load_block_' . $block);
00337 }
00338 }
00339 $this->loadLayoutUpdates()->generateLayoutXml()->generateLayoutBlocks();
00340 $this->getResponse()->setBody($this->getLayout()->getBlock('content')->toHtml());
00341 }
00342
00343
00344
00345
00346 public function startAction()
00347 {
00348 $this->_getSession()->clear();
00349 $this->_redirect('*/*', array('customer_id' => $this->getRequest()->getParam('customer_id')));
00350 }
00351
00352
00353
00354
00355 public function cancelAction()
00356 {
00357 if ($orderId = $this->_getSession()->getReordered()) {
00358 $this->_getSession()->clear();
00359 $this->_redirect('*/sales_order/view', array(
00360 'order_id'=>$orderId
00361 ));
00362 } else {
00363 $this->_getSession()->clear();
00364 $this->_redirect('*/*');
00365 }
00366
00367 }
00368
00369
00370
00371
00372 public function saveAction()
00373 {
00374 try {
00375 $this->_processData();
00376 if ($paymentData = $this->getRequest()->getPost('payment')) {
00377 $this->_getOrderCreateModel()->setPaymentData($paymentData);
00378 $this->_getOrderCreateModel()->getQuote()->getPayment()->addData($paymentData);
00379 }
00380
00381 $order = $this->_getOrderCreateModel()
00382 ->importPostData($this->getRequest()->getPost('order'))
00383 ->createOrder();
00384
00385 $this->_getSession()->clear();
00386 $url = $this->_redirect('*/sales_order/view', array('order_id' => $order->getId()));
00387 }
00388 catch (Mage_Core_Exception $e){
00389 $message = $e->getMessage();
00390 if( !empty($message) ) {
00391 $this->_getSession()->addError($message);
00392 }
00393 $url = $this->_redirect('*/*/');
00394 }
00395 catch (Exception $e){
00396 $this->_getSession()->addException($e, $this->__('Order saving error: %s', $e->getMessage()));
00397 $url = $this->_redirect('*/*/');
00398 }
00399 }
00400
00401 protected function _isAllowed()
00402 {
00403 switch ($this->getRequest()->getActionName()) {
00404 case 'index':
00405 $aclResource = 'sales/order/actions/create';
00406 break;
00407 case 'reorder':
00408 $aclResource = 'sales/order/actions/reorder';
00409 break;
00410 case 'cancel':
00411 $aclResource = 'sales/order/actions/cancel';
00412 break;
00413 case 'save':
00414 $aclResource = 'sales/order/actions/edit';
00415 break;
00416 default:
00417 $aclResource = 'sales/order/actions';
00418 break;
00419 }
00420 return Mage::getSingleton('admin/session')->isAllowed('sales/order');
00421 }
00422 }