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_OrderController extends Mage_Adminhtml_Controller_Action
00035 {
00036
00037
00038
00039
00040
00041 protected $_publicActions = array('view');
00042
00043
00044
00045
00046
00047 protected function _construct()
00048 {
00049 $this->setUsedModuleName('Mage_Sales');
00050 }
00051
00052
00053
00054
00055
00056
00057 protected function _initAction()
00058 {
00059 $this->loadLayout()
00060 ->_setActiveMenu('sales/order')
00061 ->_addBreadcrumb($this->__('Sales'), $this->__('Sales'))
00062 ->_addBreadcrumb($this->__('Orders'), $this->__('Orders'));
00063 return $this;
00064 }
00065
00066
00067
00068
00069
00070
00071 protected function _initOrder()
00072 {
00073 $id = $this->getRequest()->getParam('order_id');
00074 $order = Mage::getModel('sales/order')->load($id);
00075
00076 if (!$order->getId()) {
00077 $this->_getSession()->addError($this->__('This order no longer exists.'));
00078 $this->_redirect('*/*/');
00079 $this->setFlag('', self::FLAG_NO_DISPATCH, true);
00080 return false;
00081 }
00082 Mage::register('sales_order', $order);
00083 Mage::register('current_order', $order);
00084 return $order;
00085 }
00086
00087
00088
00089
00090 public function indexAction()
00091 {
00092 $this->_initAction()
00093 ->_addContent($this->getLayout()->createBlock('adminhtml/sales_order'))
00094 ->renderLayout();
00095 }
00096
00097
00098
00099
00100 public function gridAction()
00101 {
00102 $this->loadLayout();
00103 $this->getResponse()->setBody(
00104 $this->getLayout()->createBlock('adminhtml/sales_order_grid')->toHtml()
00105 );
00106 }
00107
00108
00109
00110
00111 public function viewAction()
00112 {
00113 if ($order = $this->_initOrder()) {
00114 $this->_initAction()
00115 ->renderLayout();
00116 }
00117 }
00118
00119
00120
00121
00122 public function cancelAction()
00123 {
00124 if ($order = $this->_initOrder()) {
00125 try {
00126 $order->cancel()
00127 ->save();
00128 $this->_getSession()->addSuccess(
00129 $this->__('Order was successfully cancelled.')
00130 );
00131 }
00132 catch (Mage_Core_Exception $e) {
00133 $this->_getSession()->addError($e->getMessage());
00134 }
00135 catch (Exception $e) {
00136 $this->_getSession()->addError($this->__('Order was not cancelled.'));
00137 }
00138 $this->_redirect('*/sales_order/view', array('order_id' => $order->getId()));
00139 }
00140 }
00141
00142
00143
00144
00145 public function holdAction()
00146 {
00147 if ($order = $this->_initOrder()) {
00148 try {
00149 $order->hold()
00150 ->save();
00151 $this->_getSession()->addSuccess(
00152 $this->__('Order was successfully put on hold.')
00153 );
00154 }
00155 catch (Mage_Core_Exception $e) {
00156 $this->_getSession()->addError($e->getMessage());
00157 }
00158 catch (Exception $e) {
00159 $this->_getSession()->addError($this->__('Order was not put on hold.'));
00160 }
00161 $this->_redirect('*/sales_order/view', array('order_id' => $order->getId()));
00162 }
00163 }
00164
00165
00166
00167
00168 public function unholdAction()
00169 {
00170 if ($order = $this->_initOrder()) {
00171 try {
00172 $order->unhold()
00173 ->save();
00174 $this->_getSession()->addSuccess(
00175 $this->__('Order was successfully released from holding status.')
00176 );
00177 }
00178 catch (Mage_Core_Exception $e) {
00179 $this->_getSession()->addError($e->getMessage());
00180 }
00181 catch (Exception $e) {
00182 $this->_getSession()->addError($this->__('Order was not unholded.'));
00183 }
00184 $this->_redirect('*/sales_order/view', array('order_id' => $order->getId()));
00185 }
00186 }
00187
00188
00189
00190
00191 public function addCommentAction()
00192 {
00193 if ($order = $this->_initOrder()) {
00194 try {
00195 $response = false;
00196 $data = $this->getRequest()->getPost('history');
00197 $notify = isset($data['is_customer_notified']) ? $data['is_customer_notified'] : false;
00198 $order->addStatusToHistory($data['status'], $data['comment'], $notify);
00199 $comment = trim(strip_tags($data['comment']));
00200
00201 $order->sendOrderUpdateEmail($notify, $comment);
00202 $order->save();
00203
00204 $this->loadLayout('empty');
00205 $this->renderLayout();
00206 }
00207 catch (Mage_Core_Exception $e) {
00208 $response = array(
00209 'error' => true,
00210 'message' => $e->getMessage(),
00211 );
00212 }
00213 catch (Exception $e) {
00214 $response = array(
00215 'error' => true,
00216 'message' => $this->__('Can not add order history.')
00217 );
00218 }
00219 if (is_array($response)) {
00220 $response = Zend_Json::encode($response);
00221 $this->getResponse()->setBody($response);
00222 }
00223 }
00224 }
00225
00226
00227
00228
00229 public function invoicesAction()
00230 {
00231 $this->_initOrder();
00232 $this->getResponse()->setBody(
00233 $this->getLayout()->createBlock('adminhtml/sales_order_view_tab_invoices')->toHtml()
00234 );
00235 }
00236
00237
00238
00239
00240 public function shipmentsAction()
00241 {
00242 $this->_initOrder();
00243 $this->getResponse()->setBody(
00244 $this->getLayout()->createBlock('adminhtml/sales_order_view_tab_shipments')->toHtml()
00245 );
00246 }
00247
00248
00249
00250
00251 public function creditmemosAction()
00252 {
00253 $this->_initOrder();
00254 $this->getResponse()->setBody(
00255 $this->getLayout()->createBlock('adminhtml/sales_order_view_tab_creditmemos')->toHtml()
00256 );
00257 }
00258
00259
00260
00261
00262 public function commentsHistoryAction()
00263 {
00264 $this->_initOrder();
00265 $this->getResponse()->setBody(
00266 $this->getLayout()->createBlock('adminhtml/sales_order_view_tab_history')->toHtml()
00267 );
00268 }
00269
00270
00271
00272
00273 public function massCancelAction()
00274 {
00275 $orderIds = $this->getRequest()->getPost('order_ids', array());
00276 $countCancelOrder = 0;
00277 foreach ($orderIds as $orderId) {
00278 $order = Mage::getModel('sales/order')->load($orderId);
00279 if ($order->canCancel()) {
00280 $order->cancel()
00281 ->save();
00282 $countCancelOrder++;
00283 }
00284 }
00285 if ($countCancelOrder>0) {
00286 $this->_getSession()->addSuccess($this->__('%s order(s) successfully canceled', $countCancelOrder));
00287 }
00288 else {
00289
00290 }
00291 $this->_redirect('*/*/');
00292 }
00293
00294
00295
00296
00297 public function massHoldAction()
00298 {
00299 $orderIds = $this->getRequest()->getPost('order_ids', array());
00300 $countHoldOrder = 0;
00301 foreach ($orderIds as $orderId) {
00302 $order = Mage::getModel('sales/order')->load($orderId);
00303 if ($order->canHold()) {
00304 $order->hold()
00305 ->save();
00306 $countHoldOrder++;
00307 }
00308 }
00309 if ($countHoldOrder>0) {
00310 $this->_getSession()->addSuccess($this->__('%s order(s) successfully put on hold', $countHoldOrder));
00311 }
00312 else {
00313
00314 }
00315 $this->_redirect('*/*/');
00316 }
00317
00318
00319
00320
00321 public function massUnholdAction()
00322 {
00323 $orderIds = $this->getRequest()->getPost('order_ids', array());
00324 $countUnholdOrder = 0;
00325 foreach ($orderIds as $orderId) {
00326 $order = Mage::getModel('sales/order')->load($orderId);
00327 if ($order->canUnhold()) {
00328 $order->unhold()
00329 ->save();
00330 $countUnholdOrder++;
00331 }
00332 }
00333 if ($countUnholdOrder>0) {
00334 $this->_getSession()->addSuccess($this->__('%s order(s) successfully released from holding status', $countUnholdOrder));
00335 }
00336 else {
00337
00338 }
00339 $this->_redirect('*/*/');
00340 }
00341
00342
00343
00344
00345 public function massStatusAction()
00346 {
00347
00348 }
00349
00350
00351
00352
00353 public function massPrintAction()
00354 {
00355 $orderIds = $this->getRequest()->getPost('order_ids');
00356 $document = $this->getRequest()->getPost('document');
00357 }
00358
00359 public function pdfinvoicesAction(){
00360 $orderIds = $this->getRequest()->getPost('order_ids');
00361 $flag = false;
00362 if (!empty($orderIds)) {
00363 foreach ($orderIds as $orderId) {
00364 $order = Mage::getModel('sales/order')->load($orderId);
00365
00366 $invoices = Mage::getResourceModel('sales/order_invoice_collection')
00367 ->addAttributeToSelect('*')
00368 ->setOrderFilter($orderId)
00369 ->load();
00370 if ($invoices->getSize() > 0) {
00371 $flag = true;
00372 if (!isset($pdf)){
00373 $pdf = Mage::getModel('sales/order_pdf_invoice')->getPdf($invoices);
00374 } else {
00375 $pages = Mage::getModel('sales/order_pdf_invoice')->getPdf($invoices);
00376 $pdf->pages = array_merge ($pdf->pages, $pages->pages);
00377 }
00378 }
00379 }
00380 if ($flag) {
00381 return $this->_prepareDownloadResponse('invoice'.Mage::getSingleton('core/date')->date('Y-m-d_H-i-s').'.pdf', $pdf->render(), 'application/pdf');
00382 } else {
00383 $this->_getSession()->addError($this->__('There are no printable documents related to selected orders'));
00384 $this->_redirect('*/*/');
00385 }
00386
00387 }
00388 $this->_redirect('*/*/');
00389
00390 }
00391
00392 public function pdfshipmentsAction(){
00393 $orderIds = $this->getRequest()->getPost('order_ids');
00394 $flag = false;
00395 if (!empty($orderIds)) {
00396 foreach ($orderIds as $orderId) {
00397 $order = Mage::getModel('sales/order')->load($orderId);
00398 $shipments = Mage::getResourceModel('sales/order_shipment_collection')
00399 ->addAttributeToSelect('*')
00400 ->setOrderFilter($orderId)
00401 ->load();
00402 if ($shipments->getSize()) {
00403 $flag = true;
00404 if (!isset($pdf)){
00405 $pdf = Mage::getModel('sales/order_pdf_shipment')->getPdf($shipments);
00406 } else {
00407 $pages = Mage::getModel('sales/order_pdf_shipment')->getPdf($shipments);
00408 $pdf->pages = array_merge ($pdf->pages, $pages->pages);
00409 }
00410 }
00411 }
00412 if ($flag) {
00413 return $this->_prepareDownloadResponse('packingslip'.Mage::getSingleton('core/date')->date('Y-m-d_H-i-s').'.pdf', $pdf->render(), 'application/pdf');
00414 } else {
00415 $this->_getSession()->addError($this->__('There are no printable documents related to selected orders'));
00416 $this->_redirect('*/*/');
00417 }
00418 }
00419 $this->_redirect('*/*/');
00420 }
00421
00422 public function pdfcreditmemosAction(){
00423 $orderIds = $this->getRequest()->getPost('order_ids');
00424 $flag = false;
00425 if (!empty($orderIds)) {
00426 foreach ($orderIds as $orderId) {
00427 $order = Mage::getModel('sales/order')->load($orderId);
00428
00429 $creditmemos = Mage::getResourceModel('sales/order_creditmemo_collection')
00430 ->addAttributeToSelect('*')
00431 ->setOrderFilter($orderId)
00432 ->load();
00433 if ($creditmemos->getSize()) {
00434 $flag = true;
00435 if (!isset($pdf)){
00436 $pdf = Mage::getModel('sales/order_pdf_creditmemo')->getPdf($creditmemos);
00437 } else {
00438 $pages = Mage::getModel('sales/order_pdf_creditmemo')->getPdf($creditmemos);
00439 $pdf->pages = array_merge ($pdf->pages, $pages->pages);
00440 }
00441 }
00442 }
00443 if ($flag) {
00444 return $this->_prepareDownloadResponse('creditmemo'.Mage::getSingleton('core/date')->date('Y-m-d_H-i-s').'.pdf', $pdf->render(), 'application/pdf');
00445 } else {
00446 $this->_getSession()->addError($this->__('There are no printable documents related to selected orders'));
00447 $this->_redirect('*/*/');
00448 }
00449 }
00450 $this->_redirect('*/*/');
00451 }
00452
00453 public function pdfdocsAction(){
00454 $orderIds = $this->getRequest()->getPost('order_ids');
00455 $flag = false;
00456 if (!empty($orderIds)) {
00457 foreach ($orderIds as $orderId) {
00458 $order = Mage::getModel('sales/order')->load($orderId);
00459
00460 $invoices = Mage::getResourceModel('sales/order_invoice_collection')
00461 ->addAttributeToSelect('*')
00462 ->setOrderFilter($orderId)
00463 ->load();
00464 if ($invoices->getSize()){
00465 $flag = true;
00466 if (!isset($pdf)){
00467 $pdf = Mage::getModel('sales/order_pdf_invoice')->getPdf($invoices);
00468 } else {
00469 $pages = Mage::getModel('sales/order_pdf_invoice')->getPdf($invoices);
00470 $pdf->pages = array_merge ($pdf->pages, $pages->pages);
00471 }
00472 }
00473
00474 $shipments = Mage::getResourceModel('sales/order_shipment_collection')
00475 ->addAttributeToSelect('*')
00476 ->setOrderFilter($orderId)
00477 ->load();
00478 if ($shipments->getSize()){
00479 $flag = true;
00480 if (!isset($pdf)){
00481 $pdf = Mage::getModel('sales/order_pdf_shipment')->getPdf($shipments);
00482 } else {
00483 $pages = Mage::getModel('sales/order_pdf_shipment')->getPdf($shipments);
00484 $pdf->pages = array_merge ($pdf->pages, $pages->pages);
00485 }
00486 }
00487
00488 $creditmemos = Mage::getResourceModel('sales/order_creditmemo_collection')
00489 ->addAttributeToSelect('*')
00490 ->setOrderFilter($orderId)
00491 ->load();
00492 if ($creditmemos->getSize()) {
00493 $flag = true;
00494 if (!isset($pdf)){
00495 $pdf = Mage::getModel('sales/order_pdf_creditmemo')->getPdf($creditmemos);
00496 } else {
00497 $pages = Mage::getModel('sales/order_pdf_creditmemo')->getPdf($creditmemos);
00498 $pdf->pages = array_merge ($pdf->pages, $pages->pages);
00499 }
00500 }
00501 }
00502 if ($flag) {
00503 return $this->_prepareDownloadResponse('docs'.Mage::getSingleton('core/date')->date('Y-m-d_H-i-s').'.pdf', $pdf->render(), 'application/pdf');
00504 } else {
00505 $this->_getSession()->addError($this->__('There are no printable documents related to selected orders'));
00506 $this->_redirect('*/*/');
00507 }
00508 }
00509 $this->_redirect('*/*/');
00510 }
00511
00512 protected function _isAllowed()
00513 {
00514 if ($this->getRequest()->getActionName() == 'view') {
00515 return Mage::getSingleton('admin/session')->isAllowed('sales/order/actions/view');
00516 }
00517 return Mage::getSingleton('admin/session')->isAllowed('sales/order');
00518 }
00519 }