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_CreditmemoController extends Mage_Adminhtml_Controller_Sales_Creditmemo
35: {
36: 37: 38:
39: protected function _getItemData()
40: {
41: $data = $this->getRequest()->getParam('creditmemo');
42: if (!$data) {
43: $data = Mage::getSingleton('adminhtml/session')->getFormData(true);
44: }
45:
46: if (isset($data['items'])) {
47: $qtys = $data['items'];
48: } else {
49: $qtys = array();
50: }
51: return $qtys;
52: }
53:
54: 55: 56: 57: 58:
59: protected function _canCreditmemo($order)
60: {
61: 62: 63:
64: if (!$order->getId()) {
65: $this->_getSession()->addError($this->__('The order no longer exists.'));
66: return false;
67: }
68:
69: 70: 71:
72: if (!$order->canCreditmemo()) {
73: $this->_getSession()->addError($this->__('Cannot create credit memo for the order.'));
74: return false;
75: }
76: return true;
77: }
78:
79: 80: 81: 82:
83: protected function _initInvoice($order)
84: {
85: $invoiceId = $this->getRequest()->getParam('invoice_id');
86: if ($invoiceId) {
87: $invoice = Mage::getModel('sales/order_invoice')
88: ->load($invoiceId)
89: ->setOrder($order);
90: if ($invoice->getId()) {
91: return $invoice;
92: }
93: }
94: return false;
95: }
96:
97: 98: 99: 100: 101:
102: protected function _initCreditmemo($update = false)
103: {
104: $this->_title($this->__('Sales'))->_title($this->__('Credit Memos'));
105:
106: $creditmemo = false;
107: $creditmemoId = $this->getRequest()->getParam('creditmemo_id');
108: $orderId = $this->getRequest()->getParam('order_id');
109: if ($creditmemoId) {
110: $creditmemo = Mage::getModel('sales/order_creditmemo')->load($creditmemoId);
111: } elseif ($orderId) {
112: $data = $this->getRequest()->getParam('creditmemo');
113: $order = Mage::getModel('sales/order')->load($orderId);
114: $invoice = $this->_initInvoice($order);
115:
116: if (!$this->_canCreditmemo($order)) {
117: return false;
118: }
119:
120: $savedData = $this->_getItemData();
121:
122: $qtys = array();
123: $backToStock = array();
124: foreach ($savedData as $orderItemId =>$itemData) {
125: if (isset($itemData['qty'])) {
126: $qtys[$orderItemId] = $itemData['qty'];
127: }
128: if (isset($itemData['back_to_stock'])) {
129: $backToStock[$orderItemId] = true;
130: }
131: }
132: $data['qtys'] = $qtys;
133:
134: $service = Mage::getModel('sales/service_order', $order);
135: if ($invoice) {
136: $creditmemo = $service->prepareInvoiceCreditmemo($invoice, $data);
137: } else {
138: $creditmemo = $service->prepareCreditmemo($data);
139: }
140:
141: 142: 143:
144: foreach ($creditmemo->getAllItems() as $creditmemoItem) {
145: $orderItem = $creditmemoItem->getOrderItem();
146: $parentId = $orderItem->getParentItemId();
147: if (isset($backToStock[$orderItem->getId()])) {
148: $creditmemoItem->setBackToStock(true);
149: } elseif ($orderItem->getParentItem() && isset($backToStock[$parentId]) && $backToStock[$parentId]) {
150: $creditmemoItem->setBackToStock(true);
151: } elseif (empty($savedData)) {
152: $creditmemoItem->setBackToStock(Mage::helper('cataloginventory')->isAutoReturnEnabled());
153: } else {
154: $creditmemoItem->setBackToStock(false);
155: }
156: }
157: }
158:
159: $args = array('creditmemo' => $creditmemo, 'request' => $this->getRequest());
160: Mage::dispatchEvent('adminhtml_sales_order_creditmemo_register_before', $args);
161:
162: Mage::register('current_creditmemo', $creditmemo);
163: return $creditmemo;
164: }
165:
166: 167: 168: 169:
170: protected function _saveCreditmemo($creditmemo)
171: {
172: $transactionSave = Mage::getModel('core/resource_transaction')
173: ->addObject($creditmemo)
174: ->addObject($creditmemo->getOrder());
175: if ($creditmemo->getInvoice()) {
176: $transactionSave->addObject($creditmemo->getInvoice());
177: }
178: $transactionSave->save();
179:
180: return $this;
181: }
182:
183: 184: 185:
186: public function viewAction()
187: {
188: $creditmemo = $this->_initCreditmemo();
189: if ($creditmemo) {
190: if ($creditmemo->getInvoice()) {
191: $this->_title($this->__("View Memo for #%s", $creditmemo->getInvoice()->getIncrementId()));
192: } else {
193: $this->_title($this->__("View Memo"));
194: }
195:
196: $this->loadLayout();
197: $this->getLayout()->getBlock('sales_creditmemo_view')
198: ->updateBackButtonUrl($this->getRequest()->getParam('come_from'));
199: $this->_setActiveMenu('sales/order')
200: ->renderLayout();
201: } else {
202: $this->_forward('noRoute');
203: }
204: }
205:
206: 207: 208:
209: public function startAction()
210: {
211: 212: 213:
214: $this->_redirect('*/*/new', array('_current'=>true));
215: }
216:
217: 218: 219:
220: public function newAction()
221: {
222: if ($creditmemo = $this->_initCreditmemo()) {
223: if ($creditmemo->getInvoice()) {
224: $this->_title($this->__("New Memo for #%s", $creditmemo->getInvoice()->getIncrementId()));
225: } else {
226: $this->_title($this->__("New Memo"));
227: }
228:
229: if ($comment = Mage::getSingleton('adminhtml/session')->getCommentText(true)) {
230: $creditmemo->setCommentText($comment);
231: }
232:
233: $this->loadLayout()
234: ->_setActiveMenu('sales/order')
235: ->renderLayout();
236: } else {
237: $this->_forward('noRoute');
238: }
239: }
240:
241: 242: 243:
244: public function updateQtyAction()
245: {
246: try {
247: $creditmemo = $this->_initCreditmemo(true);
248: $this->loadLayout();
249: $response = $this->getLayout()->getBlock('order_items')->toHtml();
250: } catch (Mage_Core_Exception $e) {
251: $response = array(
252: 'error' => true,
253: 'message' => $e->getMessage()
254: );
255: $response = Mage::helper('core')->jsonEncode($response);
256: } catch (Exception $e) {
257: $response = array(
258: 'error' => true,
259: 'message' => $this->__('Cannot update the item\'s quantity.')
260: );
261: $response = Mage::helper('core')->jsonEncode($response);
262: }
263: $this->getResponse()->setBody($response);
264: }
265:
266: 267: 268: 269:
270: public function saveAction()
271: {
272: $data = $this->getRequest()->getPost('creditmemo');
273: if (!empty($data['comment_text'])) {
274: Mage::getSingleton('adminhtml/session')->setCommentText($data['comment_text']);
275: }
276:
277: try {
278: $creditmemo = $this->_initCreditmemo();
279: if ($creditmemo) {
280: if (($creditmemo->getGrandTotal() <=0) && (!$creditmemo->getAllowZeroGrandTotal())) {
281: Mage::throwException(
282: $this->__('Credit memo\'s total must be positive.')
283: );
284: }
285:
286: $comment = '';
287: if (!empty($data['comment_text'])) {
288: $creditmemo->addComment(
289: $data['comment_text'],
290: isset($data['comment_customer_notify']),
291: isset($data['is_visible_on_front'])
292: );
293: if (isset($data['comment_customer_notify'])) {
294: $comment = $data['comment_text'];
295: }
296: }
297:
298: if (isset($data['do_refund'])) {
299: $creditmemo->setRefundRequested(true);
300: }
301: if (isset($data['do_offline'])) {
302: $creditmemo->setOfflineRequested((bool)(int)$data['do_offline']);
303: }
304:
305: $creditmemo->register();
306: if (!empty($data['send_email'])) {
307: $creditmemo->setEmailSent(true);
308: }
309:
310: $creditmemo->getOrder()->setCustomerNoteNotify(!empty($data['send_email']));
311: $this->_saveCreditmemo($creditmemo);
312: $creditmemo->sendEmail(!empty($data['send_email']), $comment);
313: $this->_getSession()->addSuccess($this->__('The credit memo has been created.'));
314: Mage::getSingleton('adminhtml/session')->getCommentText(true);
315: $this->_redirect('*/sales_order/view', array('order_id' => $creditmemo->getOrderId()));
316: return;
317: } else {
318: $this->_forward('noRoute');
319: return;
320: }
321: } catch (Mage_Core_Exception $e) {
322: $this->_getSession()->addError($e->getMessage());
323: Mage::getSingleton('adminhtml/session')->setFormData($data);
324: } catch (Exception $e) {
325: Mage::logException($e);
326: $this->_getSession()->addError($this->__('Cannot save the credit memo.'));
327: }
328: $this->_redirect('*/*/new', array('_current' => true));
329: }
330:
331: 332: 333:
334: public function cancelAction()
335: {
336: $creditmemo = $this->_initCreditmemo();
337: if ($creditmemo) {
338: try {
339: $creditmemo->cancel();
340: $this->_saveCreditmemo($creditmemo);
341: $this->_getSession()->addSuccess($this->__('The credit memo has been canceled.'));
342: } catch (Mage_Core_Exception $e) {
343: $this->_getSession()->addError($e->getMessage());
344: } catch (Exception $e) {
345: $this->_getSession()->addError($this->__('Unable to cancel the credit memo.'));
346: }
347: $this->_redirect('*/*/view', array('creditmemo_id'=>$creditmemo->getId()));
348: } else {
349: $this->_forward('noRoute');
350: }
351: }
352:
353: 354: 355:
356: public function voidAction()
357: {
358: $creditmemo = $this->_initCreditmemo();
359: if ($creditmemo) {
360: try {
361: $creditmemo->void();
362: $this->_saveCreditmemo($creditmemo);
363: $this->_getSession()->addSuccess($this->__('The credit memo has been voided.'));
364: } catch (Mage_Core_Exception $e) {
365: $this->_getSession()->addError($e->getMessage());
366: } catch (Exception $e) {
367: $this->_getSession()->addError($this->__('Unable to void the credit memo.'));
368: }
369: $this->_redirect('*/*/view', array('creditmemo_id'=>$creditmemo->getId()));
370: } else {
371: $this->_forward('noRoute');
372: }
373: }
374:
375: 376: 377:
378: public function ()
379: {
380: try {
381: $this->getRequest()->setParam(
382: 'creditmemo_id',
383: $this->getRequest()->getParam('id')
384: );
385: $data = $this->getRequest()->getPost('comment');
386: if (empty($data['comment'])) {
387: Mage::throwException($this->__('The Comment Text field cannot be empty.'));
388: }
389: $creditmemo = $this->_initCreditmemo();
390: $creditmemo->addComment(
391: $data['comment'],
392: isset($data['is_customer_notified']),
393: isset($data['is_visible_on_front'])
394: );
395: $creditmemo->save();
396: $creditmemo->sendUpdateEmail(!empty($data['is_customer_notified']), $data['comment']);
397:
398: $this->loadLayout();
399: $response = $this->getLayout()->getBlock('creditmemo_comments')->toHtml();
400: } catch (Mage_Core_Exception $e) {
401: $response = array(
402: 'error' => true,
403: 'message' => $e->getMessage()
404: );
405: $response = Mage::helper('core')->jsonEncode($response);
406: } catch (Exception $e) {
407: $response = array(
408: 'error' => true,
409: 'message' => $this->__('Cannot add new comment.')
410: );
411: $response = Mage::helper('core')->jsonEncode($response);
412: }
413: $this->getResponse()->setBody($response);
414: }
415:
416: 417: 418: 419: 420: 421: 422: 423: 424: 425:
426: protected function _needToAddDummy($item, $qtys) {
427: if ($item->getHasChildren()) {
428: foreach ($item->getChildrenItems() as $child) {
429: if (isset($qtys[$child->getId()])
430: && isset($qtys[$child->getId()]['qty'])
431: && $qtys[$child->getId()]['qty'] > 0)
432: {
433: return true;
434: }
435: }
436: return false;
437: } else if($item->getParentItem()) {
438: if (isset($qtys[$item->getParentItem()->getId()])
439: && isset($qtys[$item->getParentItem()->getId()]['qty'])
440: && $qtys[$item->getParentItem()->getId()]['qty'] > 0)
441: {
442: return true;
443: }
444: return false;
445: }
446: }
447:
448: 449: 450:
451: public function printAction()
452: {
453: $this->_initCreditmemo();
454: parent::printAction();
455: }
456: }
457: