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_Adminhtml_Sales_Order_CreateController extends Mage_Adminhtml_Controller_Action
35: {
36: 37: 38: 39:
40: protected function _construct()
41: {
42: $this->setUsedModuleName('Mage_Sales');
43:
44:
45: Mage::helper('catalog/product')->setSkipSaleableCheck(true);
46: }
47:
48: 49: 50: 51: 52:
53: protected function _getSession()
54: {
55: return Mage::getSingleton('adminhtml/session_quote');
56: }
57:
58: 59: 60: 61: 62:
63: protected function _getQuote()
64: {
65: return $this->_getSession()->getQuote();
66: }
67:
68: 69: 70: 71: 72:
73: protected function _getOrderCreateModel()
74: {
75: return Mage::getSingleton('adminhtml/sales_order_create');
76: }
77:
78: 79: 80: 81: 82:
83: protected function _getGiftmessageSaveModel()
84: {
85: return Mage::getSingleton('adminhtml/giftmessage_save');
86: }
87:
88: 89: 90: 91: 92:
93: protected function _initSession()
94: {
95: 96: 97:
98: if ($customerId = $this->getRequest()->getParam('customer_id')) {
99: $this->_getSession()->setCustomerId((int) $customerId);
100: }
101:
102: 103: 104:
105: if ($storeId = $this->getRequest()->getParam('store_id')) {
106: $this->_getSession()->setStoreId((int) $storeId);
107: }
108:
109: 110: 111:
112: if ($currencyId = $this->getRequest()->getParam('currency_id')) {
113: $this->_getSession()->setCurrencyId((string) $currencyId);
114: $this->_getOrderCreateModel()->setRecollect(true);
115: }
116: return $this;
117: }
118:
119: 120: 121: 122: 123:
124: protected function _processData()
125: {
126: return $this->_processActionData();
127: }
128:
129: 130: 131: 132: 133: 134:
135: protected function _processActionData($action = null)
136: {
137: $eventData = array(
138: 'order_create_model' => $this->_getOrderCreateModel(),
139: 'request_model' => $this->getRequest(),
140: 'session' => $this->_getSession(),
141: );
142:
143: Mage::dispatchEvent('adminhtml_sales_order_create_process_data_before', $eventData);
144:
145: 146: 147:
148: if ($data = $this->getRequest()->getPost('order')) {
149: $this->_getOrderCreateModel()->importPostData($data);
150: }
151:
152: 153: 154:
155: $this->_getOrderCreateModel()->initRuleData();
156:
157: 158: 159:
160: $this->_getOrderCreateModel()->getBillingAddress();
161:
162: 163: 164:
165: if (!$this->_getOrderCreateModel()->getQuote()->isVirtual()) {
166: $syncFlag = $this->getRequest()->getPost('shipping_as_billing');
167: $shippingMethod = $this->_getOrderCreateModel()->getShippingAddress()->getShippingMethod();
168: if (is_null($syncFlag)
169: && $this->_getOrderCreateModel()->getShippingAddress()->getSameAsBilling()
170: && empty($shippingMethod)
171: ) {
172: $this->_getOrderCreateModel()->setShippingAsBilling(1);
173: } else {
174: $this->_getOrderCreateModel()->setShippingAsBilling((int)$syncFlag);
175: }
176: }
177:
178: 179: 180:
181: if (!$this->_getOrderCreateModel()->getQuote()->isVirtual() && $this->getRequest()->getPost('reset_shipping')) {
182: $this->_getOrderCreateModel()->resetShippingMethod(true);
183: }
184:
185: 186: 187:
188: if (!$this->_getOrderCreateModel()->getQuote()->isVirtual() &&
189: $this->getRequest()->getPost('collect_shipping_rates')
190: ) {
191: $this->_getOrderCreateModel()->collectShippingRates();
192: }
193:
194:
195: 196: 197:
198: if ($data = $this->getRequest()->getPost('sidebar')) {
199: $this->_getOrderCreateModel()->applySidebarData($data);
200: }
201:
202: 203: 204:
205: if ($productId = (int) $this->getRequest()->getPost('add_product')) {
206: $this->_getOrderCreateModel()->addProduct($productId, $this->getRequest()->getPost());
207: }
208:
209: 210: 211:
212: if ($this->getRequest()->has('item') && !$this->getRequest()->getPost('update_items') && !($action == 'save')) {
213: $items = $this->getRequest()->getPost('item');
214: $items = $this->_processFiles($items);
215: $this->_getOrderCreateModel()->addProducts($items);
216: }
217:
218: 219: 220:
221: if ($this->getRequest()->getPost('update_items')) {
222: $items = $this->getRequest()->getPost('item', array());
223: $items = $this->_processFiles($items);
224: $this->_getOrderCreateModel()->updateQuoteItems($items);
225: }
226:
227: 228: 229:
230: $removeItemId = (int) $this->getRequest()->getPost('remove_item');
231: $removeFrom = (string) $this->getRequest()->getPost('from');
232: if ($removeItemId && $removeFrom) {
233: $this->_getOrderCreateModel()->removeItem($removeItemId, $removeFrom);
234: }
235:
236: 237: 238:
239: $moveItemId = (int) $this->getRequest()->getPost('move_item');
240: $moveTo = (string) $this->getRequest()->getPost('to');
241: if ($moveItemId && $moveTo) {
242: $this->_getOrderCreateModel()->moveQuoteItem($moveItemId, $moveTo);
243: }
244:
245: 246: 247:
248: if ($paymentData = $this->getRequest()->getPost('payment')) {
249: $this->_getOrderCreateModel()->getQuote()->getPayment()->addData($paymentData);
250: }
251:
252: $eventData = array(
253: 'order_create_model' => $this->_getOrderCreateModel(),
254: 'request' => $this->getRequest()->getPost(),
255: );
256:
257: Mage::dispatchEvent('adminhtml_sales_order_create_process_data', $eventData);
258:
259: $this->_getOrderCreateModel()
260: ->saveQuote();
261:
262: if ($paymentData = $this->getRequest()->getPost('payment')) {
263: $this->_getOrderCreateModel()->getQuote()->getPayment()->addData($paymentData);
264: }
265:
266: 267: 268:
269: $giftmessages = $this->getRequest()->getPost('giftmessage');
270: if ($giftmessages) {
271: $this->_getGiftmessageSaveModel()->setGiftmessages($giftmessages)
272: ->saveAllInQuote();
273: }
274:
275: 276: 277:
278: if ($data = $this->getRequest()->getPost('add_products')) {
279: $this->_getGiftmessageSaveModel()
280: ->importAllowQuoteItemsFromProducts(Mage::helper('core')->jsonDecode($data));
281: }
282:
283: 284: 285:
286: if ($this->getRequest()->getPost('update_items')) {
287: $items = $this->getRequest()->getPost('item', array());
288: $this->_getGiftmessageSaveModel()->importAllowQuoteItemsFromItems($items);
289: }
290:
291: $data = $this->getRequest()->getPost('order');
292: $couponCode = '';
293: if (isset($data) && isset($data['coupon']['code'])) {
294: $couponCode = trim($data['coupon']['code']);
295: }
296: if (!empty($couponCode)) {
297: if ($this->_getQuote()->getCouponCode() !== $couponCode) {
298: $this->_getSession()->addError(
299: $this->__('"%s" coupon code is not valid.', $this->_getHelper()->escapeHtml($couponCode)));
300: } else {
301: $this->_getSession()->addSuccess($this->__('The coupon code has been accepted.'));
302: }
303: }
304:
305: return $this;
306: }
307:
308: 309: 310: 311: 312: 313:
314: protected function _processFiles($items)
315: {
316:
317: $productHelper = Mage::helper('catalog/product');
318: foreach ($items as $id => $item) {
319: $buyRequest = new Varien_Object($item);
320: $params = array('files_prefix' => 'item_' . $id . '_');
321: $buyRequest = $productHelper->addParamsToBuyRequest($buyRequest, $params);
322: if ($buyRequest->hasData()) {
323: $items[$id] = $buyRequest->toArray();
324: }
325: }
326: return $items;
327: }
328:
329: 330: 331:
332: public function indexAction()
333: {
334: $this->_title($this->__('Sales'))->_title($this->__('Orders'))->_title($this->__('New Order'));
335: $this->_initSession();
336: $this->loadLayout();
337:
338: $this->_setActiveMenu('sales/order')
339: ->renderLayout();
340: }
341:
342:
343: public function reorderAction()
344: {
345:
346: $this->_getSession()->clear();
347: $orderId = $this->getRequest()->getParam('order_id');
348: $order = Mage::getModel('sales/order')->load($orderId);
349: if (!Mage::helper('sales/reorder')->canReorder($order)) {
350: return $this->_forward('noRoute');
351: }
352:
353: if ($order->getId()) {
354: $order->setReordered(true);
355: $this->_getSession()->setUseOldShippingMethod(true);
356: $this->_getOrderCreateModel()->initFromOrder($order);
357:
358: $this->_redirect('*/*');
359: }
360: else {
361: $this->_redirect('*/sales_order/');
362: }
363: }
364:
365: protected function _reloadQuote()
366: {
367: $id = $this->_getQuote()->getId();
368: $this->_getQuote()->load($id);
369: return $this;
370: }
371:
372: 373: 374:
375: public function loadBlockAction()
376: {
377: $request = $this->getRequest();
378: try {
379: $this->_initSession()
380: ->_processData();
381: }
382: catch (Mage_Core_Exception $e){
383: $this->_reloadQuote();
384: $this->_getSession()->addError($e->getMessage());
385: }
386: catch (Exception $e){
387: $this->_reloadQuote();
388: $this->_getSession()->addException($e, $e->getMessage());
389: }
390:
391:
392: $asJson= $request->getParam('json');
393: $block = $request->getParam('block');
394:
395: $update = $this->getLayout()->getUpdate();
396: if ($asJson) {
397: $update->addHandle('adminhtml_sales_order_create_load_block_json');
398: } else {
399: $update->addHandle('adminhtml_sales_order_create_load_block_plain');
400: }
401:
402: if ($block) {
403: $blocks = explode(',', $block);
404: if ($asJson && !in_array('message', $blocks)) {
405: $blocks[] = 'message';
406: }
407:
408: foreach ($blocks as $block) {
409: $update->addHandle('adminhtml_sales_order_create_load_block_' . $block);
410: }
411: }
412: $this->loadLayoutUpdates()->generateLayoutXml()->generateLayoutBlocks();
413: $result = $this->getLayout()->getBlock('content')->toHtml();
414: if ($request->getParam('as_js_varname')) {
415: Mage::getSingleton('adminhtml/session')->setUpdateResult($result);
416: $this->_redirect('*/*/showUpdateResult');
417: } else {
418: $this->getResponse()->setBody($result);
419: }
420: }
421:
422: 423: 424:
425: public function addConfiguredAction()
426: {
427: $errorMessage = null;
428: try {
429: $this->_initSession()
430: ->_processData();
431: }
432: catch (Exception $e){
433: $this->_reloadQuote();
434: $errorMessage = $e->getMessage();
435: }
436:
437:
438: $updateResult = new Varien_Object();
439: if ($errorMessage) {
440: $updateResult->setError(true);
441: $updateResult->setMessage($errorMessage);
442: } else {
443: $updateResult->setOk(true);
444: }
445:
446: $updateResult->setJsVarName($this->getRequest()->getParam('as_js_varname'));
447: Mage::getSingleton('adminhtml/session')->setCompositeProductResult($updateResult);
448: $this->_redirect('*/catalog_product/showUpdateResult');
449: }
450:
451: 452: 453:
454: public function startAction()
455: {
456: $this->_getSession()->clear();
457: $this->_redirect('*/*', array('customer_id' => $this->getRequest()->getParam('customer_id')));
458: }
459:
460: 461: 462:
463: public function cancelAction()
464: {
465: if ($orderId = $this->_getSession()->getReordered()) {
466: $this->_getSession()->clear();
467: $this->_redirect('*/sales_order/view', array(
468: 'order_id'=>$orderId
469: ));
470: } else {
471: $this->_getSession()->clear();
472: $this->_redirect('*/*');
473: }
474:
475: }
476:
477: 478: 479:
480: public function saveAction()
481: {
482: try {
483: $this->_processActionData('save');
484: if ($paymentData = $this->getRequest()->getPost('payment')) {
485: $this->_getOrderCreateModel()->setPaymentData($paymentData);
486: $this->_getOrderCreateModel()->getQuote()->getPayment()->addData($paymentData);
487: }
488:
489: $order = $this->_getOrderCreateModel()
490: ->setIsValidate(true)
491: ->importPostData($this->getRequest()->getPost('order'))
492: ->createOrder();
493:
494: $this->_getSession()->clear();
495: Mage::getSingleton('adminhtml/session')->addSuccess($this->__('The order has been created.'));
496: $this->_redirect('*/sales_order/view', array('order_id' => $order->getId()));
497: } catch (Mage_Payment_Model_Info_Exception $e) {
498: $this->_getOrderCreateModel()->saveQuote();
499: $message = $e->getMessage();
500: if( !empty($message) ) {
501: $this->_getSession()->addError($message);
502: }
503: $this->_redirect('*/*/');
504: } catch (Mage_Core_Exception $e){
505: $message = $e->getMessage();
506: if( !empty($message) ) {
507: $this->_getSession()->addError($message);
508: }
509: $this->_redirect('*/*/');
510: }
511: catch (Exception $e){
512: $this->_getSession()->addException($e, $this->__('Order saving error: %s', $e->getMessage()));
513: $this->_redirect('*/*/');
514: }
515: }
516:
517: 518: 519: 520: 521:
522: protected function _isAllowed()
523: {
524: $action = strtolower($this->getRequest()->getActionName());
525: switch ($action) {
526: case 'index':
527: $aclResource = 'sales/order/actions/create';
528: break;
529: case 'reorder':
530: $aclResource = 'sales/order/actions/reorder';
531: break;
532: case 'cancel':
533: $aclResource = 'sales/order/actions/cancel';
534: break;
535: case 'save':
536: $aclResource = 'sales/order/actions/edit';
537: break;
538: default:
539: $aclResource = 'sales/order/actions';
540: break;
541: }
542: return Mage::getSingleton('admin/session')->isAllowed($aclResource);
543: }
544:
545: 546: 547: 548: 549:
550: public function configureProductToAddAction()
551: {
552:
553: $productId = (int) $this->getRequest()->getParam('id');
554:
555: $configureResult = new Varien_Object();
556: $configureResult->setOk(true);
557: $configureResult->setProductId($productId);
558: $sessionQuote = Mage::getSingleton('adminhtml/session_quote');
559: $configureResult->setCurrentStoreId($sessionQuote->getStore()->getId());
560: $configureResult->setCurrentCustomerId($sessionQuote->getCustomerId());
561:
562:
563:
564: $helper = Mage::helper('adminhtml/catalog_product_composite');
565: $helper->renderConfigureResult($this, $configureResult);
566:
567: return $this;
568: }
569:
570: 571: 572: 573: 574:
575: public function configureQuoteItemsAction()
576: {
577:
578: $configureResult = new Varien_Object();
579: try {
580: $quoteItemId = (int) $this->getRequest()->getParam('id');
581: if (!$quoteItemId) {
582: Mage::throwException($this->__('Quote item id is not received.'));
583: }
584:
585: $quoteItem = Mage::getModel('sales/quote_item')->load($quoteItemId);
586: if (!$quoteItem->getId()) {
587: Mage::throwException($this->__('Quote item is not loaded.'));
588: }
589:
590: $configureResult->setOk(true);
591: $optionCollection = Mage::getModel('sales/quote_item_option')->getCollection()
592: ->addItemFilter(array($quoteItemId));
593: $quoteItem->setOptions($optionCollection->getOptionsByItem($quoteItem));
594:
595: $configureResult->setBuyRequest($quoteItem->getBuyRequest());
596: $configureResult->setCurrentStoreId($quoteItem->getStoreId());
597: $configureResult->setProductId($quoteItem->getProductId());
598: $sessionQuote = Mage::getSingleton('adminhtml/session_quote');
599: $configureResult->setCurrentCustomerId($sessionQuote->getCustomerId());
600:
601: } catch (Exception $e) {
602: $configureResult->setError(true);
603: $configureResult->setMessage($e->getMessage());
604: }
605:
606:
607:
608: $helper = Mage::helper('adminhtml/catalog_product_composite');
609: $helper->renderConfigureResult($this, $configureResult);
610:
611: return $this;
612: }
613:
614:
615: 616: 617: 618: 619:
620: public function showUpdateResultAction()
621: {
622: $session = Mage::getSingleton('adminhtml/session');
623: if ($session->hasUpdateResult() && is_scalar($session->getUpdateResult())){
624: $this->getResponse()->setBody($session->getUpdateResult());
625: $session->unsUpdateResult();
626: } else {
627: $session->unsUpdateResult();
628: return false;
629: }
630: }
631:
632: 633: 634:
635: public function processDataAction()
636: {
637: $this->_initSession();
638: $this->_processData();
639: $this->_forward('index');
640: }
641: }
642: