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:
35: abstract class Mage_Sales_Controller_Abstract extends Mage_Core_Controller_Front_Action
36: {
37: 38: 39: 40: 41: 42:
43: protected function _canViewOrder($order)
44: {
45: $customerId = Mage::getSingleton('customer/session')->getCustomerId();
46: $availableStates = Mage::getSingleton('sales/order_config')->getVisibleOnFrontStates();
47: if ($order->getId() && $order->getCustomerId() && ($order->getCustomerId() == $customerId)
48: && in_array($order->getState(), $availableStates, $strict = true)
49: ) {
50: return true;
51: }
52: return false;
53: }
54:
55: 56: 57: 58: 59:
60: protected function _viewAction()
61: {
62: if (!$this->_loadValidOrder()) {
63: return;
64: }
65:
66: $this->loadLayout();
67: $this->_initLayoutMessages('catalog/session');
68:
69: $navigationBlock = $this->getLayout()->getBlock('customer_account_navigation');
70: if ($navigationBlock) {
71: $navigationBlock->setActive('sales/order/history');
72: }
73: $this->renderLayout();
74: }
75:
76: 77: 78: 79: 80: 81:
82: protected function _loadValidOrder($orderId = null)
83: {
84: if (null === $orderId) {
85: $orderId = (int) $this->getRequest()->getParam('order_id');
86: }
87: if (!$orderId) {
88: $this->_forward('noRoute');
89: return false;
90: }
91:
92: $order = Mage::getModel('sales/order')->load($orderId);
93:
94: if ($this->_canViewOrder($order)) {
95: Mage::register('current_order', $order);
96: return true;
97: } else {
98: $this->_redirect('*/*/history');
99: }
100: return false;
101: }
102:
103: 104: 105:
106: public function viewAction()
107: {
108: $this->_viewAction();
109: }
110:
111: 112: 113:
114: public function invoiceAction()
115: {
116: $this->_viewAction();
117: }
118:
119: 120: 121:
122: public function shipmentAction()
123: {
124: $this->_viewAction();
125: }
126:
127: 128: 129:
130: public function creditmemoAction()
131: {
132: $this->_viewAction();
133: }
134:
135: 136: 137:
138: public function reorderAction()
139: {
140: if (!$this->_loadValidOrder()) {
141: return;
142: }
143: $order = Mage::registry('current_order');
144:
145: $cart = Mage::getSingleton('checkout/cart');
146: $cartTruncated = false;
147:
148:
149: $items = $order->getItemsCollection();
150: foreach ($items as $item) {
151: try {
152: $cart->addOrderItem($item);
153: } catch (Mage_Core_Exception $e){
154: if (Mage::getSingleton('checkout/session')->getUseNotice(true)) {
155: Mage::getSingleton('checkout/session')->addNotice($e->getMessage());
156: }
157: else {
158: Mage::getSingleton('checkout/session')->addError($e->getMessage());
159: }
160: $this->_redirect('*/*/history');
161: } catch (Exception $e) {
162: Mage::getSingleton('checkout/session')->addException($e,
163: Mage::helper('checkout')->__('Cannot add the item to shopping cart.')
164: );
165: $this->_redirect('checkout/cart');
166: }
167: }
168:
169: $cart->save();
170: $this->_redirect('checkout/cart');
171: }
172:
173: 174: 175:
176: public function printAction()
177: {
178: if (!$this->_loadValidOrder()) {
179: return;
180: }
181: $this->loadLayout('print');
182: $this->renderLayout();
183: }
184:
185: 186: 187:
188: public function printInvoiceAction()
189: {
190: $invoiceId = (int) $this->getRequest()->getParam('invoice_id');
191: if ($invoiceId) {
192: $invoice = Mage::getModel('sales/order_invoice')->load($invoiceId);
193: $order = $invoice->getOrder();
194: } else {
195: $orderId = (int) $this->getRequest()->getParam('order_id');
196: $order = Mage::getModel('sales/order')->load($orderId);
197: }
198:
199: if ($this->_canViewOrder($order)) {
200: Mage::register('current_order', $order);
201: if (isset($invoice)) {
202: Mage::register('current_invoice', $invoice);
203: }
204: $this->loadLayout('print');
205: $this->renderLayout();
206: } else {
207: if (Mage::getSingleton('customer/session')->isLoggedIn()) {
208: $this->_redirect('*/*/history');
209: } else {
210: $this->_redirect('sales/guest/form');
211: }
212: }
213: }
214:
215: 216: 217:
218: public function printShipmentAction()
219: {
220: $shipmentId = (int) $this->getRequest()->getParam('shipment_id');
221: if ($shipmentId) {
222: $shipment = Mage::getModel('sales/order_shipment')->load($shipmentId);
223: $order = $shipment->getOrder();
224: } else {
225: $orderId = (int) $this->getRequest()->getParam('order_id');
226: $order = Mage::getModel('sales/order')->load($orderId);
227: }
228: if ($this->_canViewOrder($order)) {
229: Mage::register('current_order', $order);
230: if (isset($shipment)) {
231: Mage::register('current_shipment', $shipment);
232: }
233: $this->loadLayout('print');
234: $this->renderLayout();
235: } else {
236: if (Mage::getSingleton('customer/session')->isLoggedIn()) {
237: $this->_redirect('*/*/history');
238: } else {
239: $this->_redirect('sales/guest/form');
240: }
241: }
242: }
243:
244: 245: 246:
247: public function printCreditmemoAction()
248: {
249: $creditmemoId = (int) $this->getRequest()->getParam('creditmemo_id');
250: if ($creditmemoId) {
251: $creditmemo = Mage::getModel('sales/order_creditmemo')->load($creditmemoId);
252: $order = $creditmemo->getOrder();
253: } else {
254: $orderId = (int) $this->getRequest()->getParam('order_id');
255: $order = Mage::getModel('sales/order')->load($orderId);
256: }
257:
258: if ($this->_canViewOrder($order)) {
259: Mage::register('current_order', $order);
260: if (isset($creditmemo)) {
261: Mage::register('current_creditmemo', $creditmemo);
262: }
263: $this->loadLayout('print');
264: $this->renderLayout();
265: } else {
266: if (Mage::getSingleton('customer/session')->isLoggedIn()) {
267: $this->_redirect('*/*/history');
268: } else {
269: $this->_redirect('sales/guest/form');
270: }
271: }
272: }
273: }
274: