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_OrderController extends Mage_Adminhtml_Controller_Action
35: {
36: 37: 38: 39: 40:
41: protected $_publicActions = array('view', 'index');
42:
43: 44: 45: 46:
47: protected function _construct()
48: {
49: $this->setUsedModuleName('Mage_Sales');
50: }
51:
52: 53: 54: 55: 56:
57: protected function _initAction()
58: {
59: $this->loadLayout()
60: ->_setActiveMenu('sales/order')
61: ->_addBreadcrumb($this->__('Sales'), $this->__('Sales'))
62: ->_addBreadcrumb($this->__('Orders'), $this->__('Orders'));
63: return $this;
64: }
65:
66: 67: 68: 69: 70:
71: protected function _initOrder()
72: {
73: $id = $this->getRequest()->getParam('order_id');
74: $order = Mage::getModel('sales/order')->load($id);
75:
76: if (!$order->getId()) {
77: $this->_getSession()->addError($this->__('This order no longer exists.'));
78: $this->_redirect('*/*/');
79: $this->setFlag('', self::FLAG_NO_DISPATCH, true);
80: return false;
81: }
82: Mage::register('sales_order', $order);
83: Mage::register('current_order', $order);
84: return $order;
85: }
86:
87: 88: 89:
90: public function indexAction()
91: {
92: $this->_title($this->__('Sales'))->_title($this->__('Orders'));
93:
94: $this->_initAction()
95: ->renderLayout();
96: }
97:
98: 99: 100:
101: public function gridAction()
102: {
103: $this->loadLayout(false);
104: $this->renderLayout();
105: }
106:
107: 108: 109:
110: public function viewAction()
111: {
112: $this->_title($this->__('Sales'))->_title($this->__('Orders'));
113:
114: if ($order = $this->_initOrder()) {
115: $this->_initAction();
116:
117: $this->_title(sprintf("#%s", $order->getRealOrderId()));
118:
119: $this->renderLayout();
120: }
121: }
122:
123: 124: 125:
126: public function emailAction()
127: {
128: if ($order = $this->_initOrder()) {
129: try {
130: $order->sendNewOrderEmail();
131: $historyItem = Mage::getResourceModel('sales/order_status_history_collection')
132: ->getUnnotifiedForInstance($order, Mage_Sales_Model_Order::HISTORY_ENTITY_NAME);
133: if ($historyItem) {
134: $historyItem->setIsCustomerNotified(1);
135: $historyItem->save();
136: }
137: $this->_getSession()->addSuccess($this->__('The order email has been sent.'));
138: } catch (Mage_Core_Exception $e) {
139: $this->_getSession()->addError($e->getMessage());
140: } catch (Exception $e) {
141: $this->_getSession()->addError($this->__('Failed to send the order email.'));
142: Mage::logException($e);
143: }
144: }
145: $this->_redirect('*/sales_order/view', array('order_id' => $order->getId()));
146: }
147: 148: 149:
150: public function cancelAction()
151: {
152: if ($order = $this->_initOrder()) {
153: try {
154: $order->cancel()
155: ->save();
156: $this->_getSession()->addSuccess(
157: $this->__('The order has been cancelled.')
158: );
159: }
160: catch (Mage_Core_Exception $e) {
161: $this->_getSession()->addError($e->getMessage());
162: }
163: catch (Exception $e) {
164: $this->_getSession()->addError($this->__('The order has not been cancelled.'));
165: Mage::logException($e);
166: }
167: $this->_redirect('*/sales_order/view', array('order_id' => $order->getId()));
168: }
169: }
170:
171: 172: 173:
174: public function holdAction()
175: {
176: if ($order = $this->_initOrder()) {
177: try {
178: $order->hold()
179: ->save();
180: $this->_getSession()->addSuccess(
181: $this->__('The order has been put on hold.')
182: );
183: }
184: catch (Mage_Core_Exception $e) {
185: $this->_getSession()->addError($e->getMessage());
186: }
187: catch (Exception $e) {
188: $this->_getSession()->addError($this->__('The order was not put on hold.'));
189: }
190: $this->_redirect('*/sales_order/view', array('order_id' => $order->getId()));
191: }
192: }
193:
194: 195: 196:
197: public function unholdAction()
198: {
199: if ($order = $this->_initOrder()) {
200: try {
201: $order->unhold()
202: ->save();
203: $this->_getSession()->addSuccess(
204: $this->__('The order has been released from holding status.')
205: );
206: }
207: catch (Mage_Core_Exception $e) {
208: $this->_getSession()->addError($e->getMessage());
209: }
210: catch (Exception $e) {
211: $this->_getSession()->addError($this->__('The order was not unheld.'));
212: }
213: $this->_redirect('*/sales_order/view', array('order_id' => $order->getId()));
214: }
215: }
216:
217: 218: 219: 220: 221:
222: public function reviewPaymentAction()
223: {
224: try {
225: if (!$order = $this->_initOrder()) {
226: return;
227: }
228: $action = $this->getRequest()->getParam('action', '');
229: switch ($action) {
230: case 'accept':
231: $order->getPayment()->accept();
232: $message = $this->__('The payment has been accepted.');
233: break;
234: case 'deny':
235: $order->getPayment()->deny();
236: $message = $this->__('The payment has been denied.');
237: break;
238: case 'update':
239: $order->getPayment()
240: ->registerPaymentReviewAction(Mage_Sales_Model_Order_Payment::REVIEW_ACTION_UPDATE, true);
241: $message = $this->__('Payment update has been made.');
242: break;
243: default:
244: throw new Exception(sprintf('Action "%s" is not supported.', $action));
245: }
246: $order->save();
247: $this->_getSession()->addSuccess($message);
248: } catch (Mage_Core_Exception $e) {
249: $this->_getSession()->addError($e->getMessage());
250: } catch (Exception $e) {
251: $this->_getSession()->addError($this->__('Failed to update the payment.'));
252: Mage::logException($e);
253: }
254: $this->_redirect('*/sales_order/view', array('order_id' => $order->getId()));
255: }
256:
257: 258: 259:
260: public function ()
261: {
262: if ($order = $this->_initOrder()) {
263: try {
264: $response = false;
265: $data = $this->getRequest()->getPost('history');
266: $notify = isset($data['is_customer_notified']) ? $data['is_customer_notified'] : false;
267: $visible = isset($data['is_visible_on_front']) ? $data['is_visible_on_front'] : false;
268:
269: $order->addStatusHistoryComment($data['comment'], $data['status'])
270: ->setIsVisibleOnFront($visible)
271: ->setIsCustomerNotified($notify);
272:
273: $comment = trim(strip_tags($data['comment']));
274:
275: $order->save();
276: $order->sendOrderUpdateEmail($notify, $comment);
277:
278: $this->loadLayout('empty');
279: $this->renderLayout();
280: }
281: catch (Mage_Core_Exception $e) {
282: $response = array(
283: 'error' => true,
284: 'message' => $e->getMessage(),
285: );
286: }
287: catch (Exception $e) {
288: $response = array(
289: 'error' => true,
290: 'message' => $this->__('Cannot add order history.')
291: );
292: }
293: if (is_array($response)) {
294: $response = Mage::helper('core')->jsonEncode($response);
295: $this->getResponse()->setBody($response);
296: }
297: }
298: }
299:
300: 301: 302:
303: public function invoicesAction()
304: {
305: $this->_initOrder();
306: $this->getResponse()->setBody(
307: $this->getLayout()->createBlock('adminhtml/sales_order_view_tab_invoices')->toHtml()
308: );
309: }
310:
311: 312: 313:
314: public function shipmentsAction()
315: {
316: $this->_initOrder();
317: $this->getResponse()->setBody(
318: $this->getLayout()->createBlock('adminhtml/sales_order_view_tab_shipments')->toHtml()
319: );
320: }
321:
322: 323: 324:
325: public function creditmemosAction()
326: {
327: $this->_initOrder();
328: $this->getResponse()->setBody(
329: $this->getLayout()->createBlock('adminhtml/sales_order_view_tab_creditmemos')->toHtml()
330: );
331: }
332:
333: 334: 335:
336: public function ()
337: {
338: $this->_initOrder();
339: $html = $this->getLayout()->createBlock('adminhtml/sales_order_view_tab_history')->toHtml();
340:
341: $translate = Mage::getModel('core/translate_inline');
342: if ($translate->isAllowed()) {
343: $translate->processResponseBody($html);
344: }
345: $this->getResponse()->setBody($html);
346: }
347:
348: 349: 350:
351: public function massCancelAction()
352: {
353: $orderIds = $this->getRequest()->getPost('order_ids', array());
354: $countCancelOrder = 0;
355: $countNonCancelOrder = 0;
356: foreach ($orderIds as $orderId) {
357: $order = Mage::getModel('sales/order')->load($orderId);
358: if ($order->canCancel()) {
359: $order->cancel()
360: ->save();
361: $countCancelOrder++;
362: } else {
363: $countNonCancelOrder++;
364: }
365: }
366: if ($countNonCancelOrder) {
367: if ($countCancelOrder) {
368: $this->_getSession()->addError($this->__('%s order(s) cannot be canceled', $countNonCancelOrder));
369: } else {
370: $this->_getSession()->addError($this->__('The order(s) cannot be canceled'));
371: }
372: }
373: if ($countCancelOrder) {
374: $this->_getSession()->addSuccess($this->__('%s order(s) have been canceled.', $countCancelOrder));
375: }
376: $this->_redirect('*/*/');
377: }
378:
379: 380: 381:
382: public function massHoldAction()
383: {
384: $orderIds = $this->getRequest()->getPost('order_ids', array());
385: $countHoldOrder = 0;
386: $countNonHoldOrder = 0;
387: foreach ($orderIds as $orderId) {
388: $order = Mage::getModel('sales/order')->load($orderId);
389: if ($order->canHold()) {
390: $order->hold()
391: ->save();
392: $countHoldOrder++;
393: } else {
394: $countNonHoldOrder++;
395: }
396: }
397: if ($countNonHoldOrder) {
398: if ($countHoldOrder) {
399: $this->_getSession()->addError($this->__('%s order(s) were not put on hold.', $countNonHoldOrder));
400: } else {
401: $this->_getSession()->addError($this->__('No order(s) were put on hold.'));
402: }
403: }
404: if ($countHoldOrder) {
405: $this->_getSession()->addSuccess($this->__('%s order(s) have been put on hold.', $countHoldOrder));
406: }
407:
408: $this->_redirect('*/*/');
409: }
410:
411: 412: 413:
414: public function massUnholdAction()
415: {
416: $orderIds = $this->getRequest()->getPost('order_ids', array());
417: $countUnholdOrder = 0;
418: $countNonUnholdOrder = 0;
419:
420: foreach ($orderIds as $orderId) {
421: $order = Mage::getModel('sales/order')->load($orderId);
422: if ($order->canUnhold()) {
423: $order->unhold()
424: ->save();
425: $countUnholdOrder++;
426: } else {
427: $countNonUnholdOrder++;
428: }
429: }
430: if ($countNonUnholdOrder) {
431: if ($countUnholdOrder) {
432: $this->_getSession()->addError($this->__('%s order(s) were not released from holding status.', $countNonUnholdOrder));
433: } else {
434: $this->_getSession()->addError($this->__('No order(s) were released from holding status.'));
435: }
436: }
437: if ($countUnholdOrder) {
438: $this->_getSession()->addSuccess($this->__('%s order(s) have been released from holding status.', $countUnholdOrder));
439: }
440: $this->_redirect('*/*/');
441: }
442:
443: 444: 445:
446: public function massStatusAction()
447: {
448:
449: }
450:
451: 452: 453:
454: public function massPrintAction()
455: {
456: $orderIds = $this->getRequest()->getPost('order_ids');
457: $document = $this->getRequest()->getPost('document');
458: }
459:
460: 461: 462:
463: public function pdfinvoicesAction(){
464: $orderIds = $this->getRequest()->getPost('order_ids');
465: $flag = false;
466: if (!empty($orderIds)) {
467: foreach ($orderIds as $orderId) {
468: $invoices = Mage::getResourceModel('sales/order_invoice_collection')
469: ->setOrderFilter($orderId)
470: ->load();
471: if ($invoices->getSize() > 0) {
472: $flag = true;
473: if (!isset($pdf)){
474: $pdf = Mage::getModel('sales/order_pdf_invoice')->getPdf($invoices);
475: } else {
476: $pages = Mage::getModel('sales/order_pdf_invoice')->getPdf($invoices);
477: $pdf->pages = array_merge ($pdf->pages, $pages->pages);
478: }
479: }
480: }
481: if ($flag) {
482: return $this->_prepareDownloadResponse(
483: 'invoice'.Mage::getSingleton('core/date')->date('Y-m-d_H-i-s').'.pdf', $pdf->render(),
484: 'application/pdf'
485: );
486: } else {
487: $this->_getSession()->addError($this->__('There are no printable documents related to selected orders.'));
488: $this->_redirect('*/*/');
489: }
490: }
491: $this->_redirect('*/*/');
492: }
493:
494: 495: 496:
497: public function pdfshipmentsAction(){
498: $orderIds = $this->getRequest()->getPost('order_ids');
499: $flag = false;
500: if (!empty($orderIds)) {
501: foreach ($orderIds as $orderId) {
502: $shipments = Mage::getResourceModel('sales/order_shipment_collection')
503: ->setOrderFilter($orderId)
504: ->load();
505: if ($shipments->getSize()) {
506: $flag = true;
507: if (!isset($pdf)){
508: $pdf = Mage::getModel('sales/order_pdf_shipment')->getPdf($shipments);
509: } else {
510: $pages = Mage::getModel('sales/order_pdf_shipment')->getPdf($shipments);
511: $pdf->pages = array_merge ($pdf->pages, $pages->pages);
512: }
513: }
514: }
515: if ($flag) {
516: return $this->_prepareDownloadResponse(
517: 'packingslip'.Mage::getSingleton('core/date')->date('Y-m-d_H-i-s').'.pdf', $pdf->render(),
518: 'application/pdf'
519: );
520: } else {
521: $this->_getSession()->addError($this->__('There are no printable documents related to selected orders.'));
522: $this->_redirect('*/*/');
523: }
524: }
525: $this->_redirect('*/*/');
526: }
527:
528: 529: 530:
531: public function pdfcreditmemosAction(){
532: $orderIds = $this->getRequest()->getPost('order_ids');
533: $flag = false;
534: if (!empty($orderIds)) {
535: foreach ($orderIds as $orderId) {
536: $creditmemos = Mage::getResourceModel('sales/order_creditmemo_collection')
537: ->setOrderFilter($orderId)
538: ->load();
539: if ($creditmemos->getSize()) {
540: $flag = true;
541: if (!isset($pdf)){
542: $pdf = Mage::getModel('sales/order_pdf_creditmemo')->getPdf($creditmemos);
543: } else {
544: $pages = Mage::getModel('sales/order_pdf_creditmemo')->getPdf($creditmemos);
545: $pdf->pages = array_merge ($pdf->pages, $pages->pages);
546: }
547: }
548: }
549: if ($flag) {
550: return $this->_prepareDownloadResponse(
551: 'creditmemo'.Mage::getSingleton('core/date')->date('Y-m-d_H-i-s').'.pdf', $pdf->render(),
552: 'application/pdf'
553: );
554: } else {
555: $this->_getSession()->addError($this->__('There are no printable documents related to selected orders.'));
556: $this->_redirect('*/*/');
557: }
558: }
559: $this->_redirect('*/*/');
560: }
561:
562: 563: 564:
565: public function pdfdocsAction(){
566: $orderIds = $this->getRequest()->getPost('order_ids');
567: $flag = false;
568: if (!empty($orderIds)) {
569: foreach ($orderIds as $orderId) {
570: $invoices = Mage::getResourceModel('sales/order_invoice_collection')
571: ->setOrderFilter($orderId)
572: ->load();
573: if ($invoices->getSize()){
574: $flag = true;
575: if (!isset($pdf)){
576: $pdf = Mage::getModel('sales/order_pdf_invoice')->getPdf($invoices);
577: } else {
578: $pages = Mage::getModel('sales/order_pdf_invoice')->getPdf($invoices);
579: $pdf->pages = array_merge ($pdf->pages, $pages->pages);
580: }
581: }
582:
583: $shipments = Mage::getResourceModel('sales/order_shipment_collection')
584: ->setOrderFilter($orderId)
585: ->load();
586: if ($shipments->getSize()){
587: $flag = true;
588: if (!isset($pdf)){
589: $pdf = Mage::getModel('sales/order_pdf_shipment')->getPdf($shipments);
590: } else {
591: $pages = Mage::getModel('sales/order_pdf_shipment')->getPdf($shipments);
592: $pdf->pages = array_merge ($pdf->pages, $pages->pages);
593: }
594: }
595:
596: $creditmemos = Mage::getResourceModel('sales/order_creditmemo_collection')
597: ->setOrderFilter($orderId)
598: ->load();
599: if ($creditmemos->getSize()) {
600: $flag = true;
601: if (!isset($pdf)){
602: $pdf = Mage::getModel('sales/order_pdf_creditmemo')->getPdf($creditmemos);
603: } else {
604: $pages = Mage::getModel('sales/order_pdf_creditmemo')->getPdf($creditmemos);
605: $pdf->pages = array_merge ($pdf->pages, $pages->pages);
606: }
607: }
608: }
609: if ($flag) {
610: return $this->_prepareDownloadResponse(
611: 'docs'.Mage::getSingleton('core/date')->date('Y-m-d_H-i-s').'.pdf',
612: $pdf->render(), 'application/pdf'
613: );
614: } else {
615: $this->_getSession()->addError($this->__('There are no printable documents related to selected orders.'));
616: $this->_redirect('*/*/');
617: }
618: }
619: $this->_redirect('*/*/');
620: }
621:
622: 623: 624:
625: public function voidPaymentAction()
626: {
627: if (!$order = $this->_initOrder()) {
628: return;
629: }
630: try {
631: $order->getPayment()->void(
632: new Varien_Object()
633: );
634: $order->save();
635: $this->_getSession()->addSuccess($this->__('The payment has been voided.'));
636: } catch (Mage_Core_Exception $e) {
637: $this->_getSession()->addError($e->getMessage());
638: } catch (Exception $e) {
639: $this->_getSession()->addError($this->__('Failed to void the payment.'));
640: Mage::logException($e);
641: }
642: $this->_redirect('*/*/view', array('order_id' => $order->getId()));
643: }
644:
645: 646: 647: 648: 649:
650: protected function _isAllowed()
651: {
652: $action = strtolower($this->getRequest()->getActionName());
653: switch ($action) {
654: case 'hold':
655: $aclResource = 'sales/order/actions/hold';
656: break;
657: case 'unhold':
658: $aclResource = 'sales/order/actions/unhold';
659: break;
660: case 'email':
661: $aclResource = 'sales/order/actions/email';
662: break;
663: case 'cancel':
664: $aclResource = 'sales/order/actions/cancel';
665: break;
666: case 'view':
667: $aclResource = 'sales/order/actions/view';
668: break;
669: case 'addcomment':
670: $aclResource = 'sales/order/actions/comment';
671: break;
672: case 'creditmemos':
673: $aclResource = 'sales/order/actions/creditmemo';
674: break;
675: case 'reviewpayment':
676: $aclResource = 'sales/order/actions/review_payment';
677: break;
678: default:
679: $aclResource = 'sales/order';
680: break;
681:
682: }
683: return Mage::getSingleton('admin/session')->isAllowed($aclResource);
684: }
685:
686: 687: 688:
689: public function exportCsvAction()
690: {
691: $fileName = 'orders.csv';
692: $grid = $this->getLayout()->createBlock('adminhtml/sales_order_grid');
693: $this->_prepareDownloadResponse($fileName, $grid->getCsvFile());
694: }
695:
696: 697: 698:
699: public function exportExcelAction()
700: {
701: $fileName = 'orders.xml';
702: $grid = $this->getLayout()->createBlock('adminhtml/sales_order_grid');
703: $this->_prepareDownloadResponse($fileName, $grid->getExcelFile($fileName));
704: }
705:
706: 707: 708: 709:
710: public function transactionsAction()
711: {
712: $this->_initOrder();
713: $this->loadLayout(false);
714: $this->renderLayout();
715: }
716:
717: 718: 719:
720: public function addressAction()
721: {
722: $addressId = $this->getRequest()->getParam('address_id');
723: $address = Mage::getModel('sales/order_address')
724: ->getCollection()
725: ->addFilter('entity_id', $addressId)
726: ->getItemById($addressId);
727: if ($address) {
728: Mage::register('order_address', $address);
729: $this->loadLayout();
730:
731: $addressFormContainer = $this->getLayout()->getBlock('sales_order_address.form.container');
732: if ($addressFormContainer) {
733: $addressFormContainer->getChild('form')->setDisplayVatValidationButton(false);
734: }
735:
736: $this->renderLayout();
737: } else {
738: $this->_redirect('*/*/');
739: }
740: }
741:
742: 743: 744:
745: public function addressSaveAction()
746: {
747: $addressId = $this->getRequest()->getParam('address_id');
748: $address = Mage::getModel('sales/order_address')->load($addressId);
749: $data = $this->getRequest()->getPost();
750: if ($data && $address->getId()) {
751: $address->addData($data);
752: try {
753: $address->implodeStreetAddress()
754: ->save();
755: $this->_getSession()->addSuccess(Mage::helper('sales')->__('The order address has been updated.'));
756: $this->_redirect('*/*/view', array('order_id'=>$address->getParentId()));
757: return;
758: } catch (Mage_Core_Exception $e) {
759: $this->_getSession()->addError($e->getMessage());
760: } catch (Exception $e) {
761: $this->_getSession()->addException(
762: $e,
763: Mage::helper('sales')->__('An error occurred while updating the order address. The address has not been changed.')
764: );
765: }
766: $this->_redirect('*/*/address', array('address_id'=>$address->getId()));
767: } else {
768: $this->_redirect('*/*/');
769: }
770: }
771: }
772: