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
00035 class Mage_Sales_OrderController extends Mage_Core_Controller_Front_Action
00036 {
00037
00038
00039
00040
00041
00042
00043 public function preDispatch()
00044 {
00045 parent::preDispatch();
00046 $action = $this->getRequest()->getActionName();
00047 $loginUrl = Mage::helper('customer')->getLoginUrl();
00048
00049 if (!Mage::getSingleton('customer/session')->authenticate($this, $loginUrl)) {
00050 $this->setFlag('', self::FLAG_NO_DISPATCH, true);
00051 }
00052 }
00053
00054
00055
00056
00057 public function historyAction()
00058 {
00059 $this->loadLayout();
00060 $this->_initLayoutMessages('catalog/session');
00061
00062 $this->getLayout()->getBlock('head')->setTitle($this->__('My Orders'));
00063
00064 if ($block = $this->getLayout()->getBlock('customer.account.link.back')) {
00065 $block->setRefererUrl($this->_getRefererUrl());
00066 }
00067 $this->renderLayout();
00068 }
00069
00070
00071
00072
00073
00074
00075
00076 protected function _canViewOrder($order)
00077 {
00078 $customerId = Mage::getSingleton('customer/session')->getCustomerId();
00079 $availableStates = Mage::getSingleton('sales/order_config')->getVisibleOnFrontStates();
00080 if ($order->getId() && $order->getCustomerId() && ($order->getCustomerId() == $customerId)
00081 && in_array($order->getState(), $availableStates, $strict = true)
00082 ) {
00083 return true;
00084 }
00085 return false;
00086 }
00087
00088 protected function _viewAction()
00089 {
00090 if (!$this->_loadValidOrder()) {
00091 return;
00092 }
00093
00094 $this->loadLayout();
00095 $this->_initLayoutMessages('catalog/session');
00096
00097 if ($navigationBlock = $this->getLayout()->getBlock('customer_account_navigation')) {
00098 $navigationBlock->setActive('sales/order/history');
00099 }
00100 $this->renderLayout();
00101 }
00102
00103
00104
00105
00106
00107
00108
00109 protected function _loadValidOrder($orderId = null)
00110 {
00111 if (null === $orderId) {
00112 $orderId = (int) $this->getRequest()->getParam('order_id');
00113 }
00114 if (!$orderId) {
00115 $this->_forward('noRoute');
00116 return false;
00117 }
00118
00119 $order = Mage::getModel('sales/order')->load($orderId);
00120
00121 if ($this->_canViewOrder($order)) {
00122 Mage::register('current_order', $order);
00123 return true;
00124 }
00125 else {
00126 $this->_redirect('*/*/history');
00127 }
00128 return false;
00129 }
00130
00131
00132
00133
00134 public function viewAction()
00135 {
00136 $this->_viewAction();
00137 }
00138
00139
00140
00141
00142
00143
00144
00145 protected function _canViewOscommerceOrder($order)
00146 {
00147 $customerId = Mage::getSingleton('customer/session')->getCustomerId();
00148 if (isset($order['osc_magento_id']) && isset($order['magento_customers_id']) && $order['magento_customers_id'] == $customerId) {
00149 return true;
00150 }
00151 return false;
00152 }
00153
00154
00155
00156
00157 public function viewOldAction()
00158 {
00159
00160 $orderId = (int) $this->getRequest()->getParam('order_id');
00161 if (!$orderId) {
00162 $this->_forward('noRoute');
00163 return;
00164 }
00165
00166 $order = Mage::getModel('oscommerce/oscommerce')->loadOrderById($orderId);
00167 if ($this->_canViewOscommerceOrder($order['order'])) {
00168 Mage::register('current_oscommerce_order', $order);
00169 $this->loadLayout();
00170 if ($navigationBlock = $this->getLayout()->getBlock('customer_account_navigation')) {
00171 $navigationBlock->setActive('sales/order/history');
00172 }
00173
00174 $this->renderLayout();
00175 }
00176 else {
00177 $this->_redirect('*/*/history');
00178 }
00179
00180 }
00181
00182 public function invoiceAction()
00183 {
00184 $this->_viewAction();
00185 }
00186
00187 public function shipmentAction()
00188 {
00189 $this->_viewAction();
00190 }
00191
00192 public function creditmemoAction()
00193 {
00194 $this->_viewAction();
00195 }
00196
00197 public function reorderAction()
00198 {
00199 if (!$this->_loadValidOrder()) {
00200 return;
00201 }
00202 $order = Mage::registry('current_order');
00203
00204 $cart = Mage::getSingleton('checkout/cart');
00205 $cartTruncated = false;
00206
00207
00208 $items = $order->getItemsCollection();
00209 foreach ($items as $item) {
00210 try {
00211 $cart->addOrderItem($item);
00212 } catch (Mage_Core_Exception $e){
00213 if (Mage::getSingleton('checkout/session')->getUseNotice(true)) {
00214 Mage::getSingleton('checkout/session')->addNotice($e->getMessage());
00215 }
00216 else {
00217 Mage::getSingleton('checkout/session')->addError($e->getMessage());
00218 }
00219 $this->_redirect('*/*/history');
00220 } catch (Exception $e) {
00221 Mage::getSingleton('checkout/session')->addException($e,
00222 Mage::helper('checkout')->__('Can not add item to shopping cart')
00223 );
00224 $this->_redirect('checkout/cart');
00225 }
00226 }
00227
00228 $cart->save();
00229 $this->_redirect('checkout/cart');
00230 }
00231
00232 public function printAction()
00233 {
00234 if (!$this->_loadValidOrder()) {
00235 return;
00236 }
00237 $this->loadLayout('print');
00238 $this->renderLayout();
00239 }
00240
00241 public function printInvoiceAction()
00242 {
00243 $invoiceId = (int) $this->getRequest()->getParam('invoice_id');
00244 if ($invoiceId) {
00245 $invoice = Mage::getModel('sales/order_invoice')->load($invoiceId);
00246 $order = $invoice->getOrder();
00247 } else {
00248 $orderId = (int) $this->getRequest()->getParam('order_id');
00249 $order = Mage::getModel('sales/order')->load($orderId);
00250 }
00251
00252 if ($this->_canViewOrder($order)) {
00253 Mage::register('current_order', $order);
00254 if (isset($invoice)) {
00255 Mage::register('current_invoice', $invoice);
00256 }
00257 $this->loadLayout('print');
00258 $this->renderLayout();
00259 } else {
00260 $this->_redirect('*/*/history');
00261 }
00262 }
00263
00264 public function printShipmentAction()
00265 {
00266 $shipmentId = (int) $this->getRequest()->getParam('shipment_id');
00267 if ($shipmentId) {
00268 $shipment = Mage::getModel('sales/order_shipment')->load($shipmentId);
00269 $order = $shipment->getOrder();
00270 } else {
00271 $orderId = (int) $this->getRequest()->getParam('order_id');
00272 $order = Mage::getModel('sales/order')->load($orderId);
00273 }
00274 if ($this->_canViewOrder($order)) {
00275 Mage::register('current_order', $order);
00276 if (isset($shipment)) {
00277 Mage::register('current_shipment', $shipment);
00278 }
00279 $this->loadLayout('print');
00280 $this->renderLayout();
00281 } else {
00282 $this->_redirect('*/*/history');
00283 }
00284 }
00285
00286 public function printCreditmemoAction()
00287 {
00288 $creditmemoId = (int) $this->getRequest()->getParam('creditmemo_id');
00289 if ($creditmemoId) {
00290 $creditmemo = Mage::getModel('sales/order_creditmemo')->load($creditmemoId);
00291 $order = $creditmemo->getOrder();
00292 } else {
00293 $orderId = (int) $this->getRequest()->getParam('order_id');
00294 $order = Mage::getModel('sales/order')->load($orderId);
00295 }
00296
00297 if ($this->_canViewOrder($order)) {
00298 Mage::register('current_order', $order);
00299 if (isset($creditmemo)) {
00300 Mage::register('current_creditmemo', $creditmemo);
00301 }
00302 $this->loadLayout('print');
00303 $this->renderLayout();
00304 } else {
00305 $this->_redirect('*/*/history');
00306 }
00307 }
00308 }