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_InvoiceController extends Mage_Adminhtml_Controller_Sales_Invoice
35: {
36: 37: 38:
39: protected function _getItemQtys()
40: {
41: $data = $this->getRequest()->getParam('invoice');
42: if (isset($data['items'])) {
43: $qtys = $data['items'];
44: } else {
45: $qtys = array();
46: }
47: return $qtys;
48: }
49:
50: 51: 52: 53: 54:
55: protected function _initInvoice($update = false)
56: {
57: $this->_title($this->__('Sales'))->_title($this->__('Invoices'));
58:
59: $invoice = false;
60: $itemsToInvoice = 0;
61: $invoiceId = $this->getRequest()->getParam('invoice_id');
62: $orderId = $this->getRequest()->getParam('order_id');
63: if ($invoiceId) {
64: $invoice = Mage::getModel('sales/order_invoice')->load($invoiceId);
65: if (!$invoice->getId()) {
66: $this->_getSession()->addError($this->__('The invoice no longer exists.'));
67: return false;
68: }
69: } elseif ($orderId) {
70: $order = Mage::getModel('sales/order')->load($orderId);
71: 72: 73:
74: if (!$order->getId()) {
75: $this->_getSession()->addError($this->__('The order no longer exists.'));
76: return false;
77: }
78: 79: 80:
81: if (!$order->canInvoice()) {
82: $this->_getSession()->addError($this->__('The order does not allow creating an invoice.'));
83: return false;
84: }
85: $savedQtys = $this->_getItemQtys();
86: $invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice($savedQtys);
87: if (!$invoice->getTotalQty()) {
88: Mage::throwException($this->__('Cannot create an invoice without products.'));
89: }
90: }
91:
92: Mage::register('current_invoice', $invoice);
93: return $invoice;
94: }
95:
96: 97: 98: 99: 100: 101:
102: protected function _saveInvoice($invoice)
103: {
104: $invoice->getOrder()->setIsInProcess(true);
105: $transactionSave = Mage::getModel('core/resource_transaction')
106: ->addObject($invoice)
107: ->addObject($invoice->getOrder())
108: ->save();
109:
110: return $this;
111: }
112:
113: 114: 115: 116: 117: 118:
119: protected function _prepareShipment($invoice)
120: {
121: $savedQtys = $this->_getItemQtys();
122: $shipment = Mage::getModel('sales/service_order', $invoice->getOrder())->prepareShipment($savedQtys);
123: if (!$shipment->getTotalQty()) {
124: return false;
125: }
126:
127:
128: $shipment->register();
129: $tracks = $this->getRequest()->getPost('tracking');
130: if ($tracks) {
131: foreach ($tracks as $data) {
132: $track = Mage::getModel('sales/order_shipment_track')
133: ->addData($data);
134: $shipment->addTrack($track);
135: }
136: }
137: return $shipment;
138: }
139:
140: 141: 142:
143: public function viewAction()
144: {
145: $invoice = $this->_initInvoice();
146: if ($invoice) {
147: $this->_title(sprintf("#%s", $invoice->getIncrementId()));
148:
149: $this->loadLayout()
150: ->_setActiveMenu('sales/order');
151: $this->getLayout()->getBlock('sales_invoice_view')
152: ->updateBackButtonUrl($this->getRequest()->getParam('come_from'));
153: $this->renderLayout();
154: }
155: else {
156: $this->_forward('noRoute');
157: }
158: }
159:
160: 161: 162:
163: public function startAction()
164: {
165: 166: 167:
168: $this->_getSession()->getInvoiceItemQtys(true);
169: $this->_redirect('*/*/new', array('order_id'=>$this->getRequest()->getParam('order_id')));
170: }
171:
172: 173: 174:
175: public function newAction()
176: {
177: $invoice = $this->_initInvoice();
178: if ($invoice) {
179: $this->_title($this->__('New Invoice'));
180:
181: if ($comment = Mage::getSingleton('adminhtml/session')->getCommentText(true)) {
182: $invoice->setCommentText($comment);
183: }
184:
185: $this->loadLayout()
186: ->_setActiveMenu('sales/order')
187: ->renderLayout();
188: } else {
189: $this->_redirect('*/sales_order/view', array('order_id'=>$this->getRequest()->getParam('order_id')));
190: }
191: }
192:
193: 194: 195:
196: public function updateQtyAction()
197: {
198: try {
199: $invoice = $this->_initInvoice(true);
200:
201: $invoiceRawData = $this->getRequest()->getParam('invoice');
202: $invoiceRawCommentText = $invoiceRawData['comment_text'];
203: $invoice->setCommentText($invoiceRawCommentText);
204:
205: $this->loadLayout();
206: $response = $this->getLayout()->getBlock('order_items')->toHtml();
207: } catch (Mage_Core_Exception $e) {
208: $response = array(
209: 'error' => true,
210: 'message' => $e->getMessage()
211: );
212: $response = Mage::helper('core')->jsonEncode($response);
213: } catch (Exception $e) {
214: $response = array(
215: 'error' => true,
216: 'message' => $this->__('Cannot update item quantity.')
217: );
218: $response = Mage::helper('core')->jsonEncode($response);
219: }
220: $this->getResponse()->setBody($response);
221: }
222:
223: 224: 225: 226:
227: public function saveAction()
228: {
229: $data = $this->getRequest()->getPost('invoice');
230: $orderId = $this->getRequest()->getParam('order_id');
231:
232: if (!empty($data['comment_text'])) {
233: Mage::getSingleton('adminhtml/session')->setCommentText($data['comment_text']);
234: }
235:
236: try {
237: $invoice = $this->_initInvoice();
238: if ($invoice) {
239:
240: if (!empty($data['capture_case'])) {
241: $invoice->setRequestedCaptureCase($data['capture_case']);
242: }
243:
244: if (!empty($data['comment_text'])) {
245: $invoice->addComment(
246: $data['comment_text'],
247: isset($data['comment_customer_notify']),
248: isset($data['is_visible_on_front'])
249: );
250: }
251:
252: $invoice->register();
253:
254: if (!empty($data['send_email'])) {
255: $invoice->setEmailSent(true);
256: }
257:
258: $invoice->getOrder()->setCustomerNoteNotify(!empty($data['send_email']));
259: $invoice->getOrder()->setIsInProcess(true);
260:
261: $transactionSave = Mage::getModel('core/resource_transaction')
262: ->addObject($invoice)
263: ->addObject($invoice->getOrder());
264: $shipment = false;
265: if (!empty($data['do_shipment']) || (int) $invoice->getOrder()->getForcedDoShipmentWithInvoice()) {
266: $shipment = $this->_prepareShipment($invoice);
267: if ($shipment) {
268: $shipment->setEmailSent($invoice->getEmailSent());
269: $transactionSave->addObject($shipment);
270: }
271: }
272: $transactionSave->save();
273:
274: if (isset($shippingResponse) && $shippingResponse->hasErrors()) {
275: $this->_getSession()->addError($this->__('The invoice and the shipment have been created. The shipping label cannot be created at the moment.'));
276: } elseif (!empty($data['do_shipment'])) {
277: $this->_getSession()->addSuccess($this->__('The invoice and shipment have been created.'));
278: } else {
279: $this->_getSession()->addSuccess($this->__('The invoice has been created.'));
280: }
281:
282:
283: $comment = '';
284: if (isset($data['comment_customer_notify'])) {
285: $comment = $data['comment_text'];
286: }
287: try {
288: $invoice->sendEmail(!empty($data['send_email']), $comment);
289: } catch (Exception $e) {
290: Mage::logException($e);
291: $this->_getSession()->addError($this->__('Unable to send the invoice email.'));
292: }
293: if ($shipment) {
294: try {
295: $shipment->sendEmail(!empty($data['send_email']));
296: } catch (Exception $e) {
297: Mage::logException($e);
298: $this->_getSession()->addError($this->__('Unable to send the shipment email.'));
299: }
300: }
301: Mage::getSingleton('adminhtml/session')->getCommentText(true);
302: $this->_redirect('*/sales_order/view', array('order_id' => $orderId));
303: } else {
304: $this->_redirect('*/*/new', array('order_id' => $orderId));
305: }
306: return;
307: } catch (Mage_Core_Exception $e) {
308: $this->_getSession()->addError($e->getMessage());
309: } catch (Exception $e) {
310: $this->_getSession()->addError($this->__('Unable to save the invoice.'));
311: Mage::logException($e);
312: }
313: $this->_redirect('*/*/new', array('order_id' => $orderId));
314: }
315:
316:
317: 318: 319:
320: public function captureAction()
321: {
322: if ($invoice = $this->_initInvoice()) {
323: try {
324: $invoice->capture();
325: $this->_saveInvoice($invoice);
326: $this->_getSession()->addSuccess($this->__('The invoice has been captured.'));
327: } catch (Mage_Core_Exception $e) {
328: $this->_getSession()->addError($e->getMessage());
329: } catch (Exception $e) {
330: $this->_getSession()->addError($this->__('Invoice capturing error.'));
331: }
332: $this->_redirect('*/*/view', array('invoice_id'=>$invoice->getId()));
333: } else {
334: $this->_forward('noRoute');
335: }
336: }
337:
338: 339: 340:
341: public function cancelAction()
342: {
343: if ($invoice = $this->_initInvoice()) {
344: try {
345: $invoice->cancel();
346: $this->_saveInvoice($invoice);
347: $this->_getSession()->addSuccess($this->__('The invoice has been canceled.'));
348: } catch (Mage_Core_Exception $e) {
349: $this->_getSession()->addError($e->getMessage());
350: } catch (Exception $e) {
351: $this->_getSession()->addError($this->__('Invoice canceling error.'));
352: }
353: $this->_redirect('*/*/view', array('invoice_id'=>$invoice->getId()));
354: } else {
355: $this->_forward('noRoute');
356: }
357: }
358:
359: 360: 361:
362: public function voidAction()
363: {
364: if ($invoice = $this->_initInvoice()) {
365: try {
366: $invoice->void();
367: $this->_saveInvoice($invoice);
368: $this->_getSession()->addSuccess($this->__('The invoice has been voided.'));
369: } catch (Mage_Core_Exception $e) {
370: $this->_getSession()->addError($e->getMessage());
371: } catch (Exception $e) {
372: $this->_getSession()->addError($this->__('Invoice voiding error.'));
373: }
374: $this->_redirect('*/*/view', array('invoice_id'=>$invoice->getId()));
375: } else {
376: $this->_forward('noRoute');
377: }
378: }
379:
380: public function ()
381: {
382: try {
383: $this->getRequest()->setParam('invoice_id', $this->getRequest()->getParam('id'));
384: $data = $this->getRequest()->getPost('comment');
385: if (empty($data['comment'])) {
386: Mage::throwException($this->__('The Comment Text field cannot be empty.'));
387: }
388: $invoice = $this->_initInvoice();
389: $invoice->addComment(
390: $data['comment'],
391: isset($data['is_customer_notified']),
392: isset($data['is_visible_on_front'])
393: );
394: $invoice->sendUpdateEmail(!empty($data['is_customer_notified']), $data['comment']);
395: $invoice->save();
396:
397: $this->loadLayout();
398: $response = $this->getLayout()->getBlock('invoice_comments')->toHtml();
399: } catch (Mage_Core_Exception $e) {
400: $response = array(
401: 'error' => true,
402: 'message' => $e->getMessage()
403: );
404: $response = Mage::helper('core')->jsonEncode($response);
405: } catch (Exception $e) {
406: $response = array(
407: 'error' => true,
408: 'message' => $this->__('Cannot add new comment.')
409: );
410: $response = Mage::helper('core')->jsonEncode($response);
411: }
412: $this->getResponse()->setBody($response);
413: }
414:
415:
416:
417:
418:
419:
420:
421:
422: 423: 424: 425: 426: 427: 428: 429: 430: 431:
432: protected function _needToAddDummy($item, $qtys) {
433: if ($item->getHasChildren()) {
434: foreach ($item->getChildrenItems() as $child) {
435: if (isset($qtys[$child->getId()]) && $qtys[$child->getId()] > 0) {
436: return true;
437: }
438: }
439: return false;
440: } else if($item->getParentItem()) {
441: if (isset($qtys[$item->getParentItem()->getId()]) && $qtys[$item->getParentItem()->getId()] > 0) {
442: return true;
443: }
444: return false;
445: }
446: }
447:
448: 449: 450: 451: 452: 453: 454: 455: 456: 457:
458: protected function _needToAddDummyForShipment($item, $qtys) {
459: if ($item->getHasChildren()) {
460: foreach ($item->getChildrenItems() as $child) {
461: if ($child->getIsVirtual()) {
462: continue;
463: }
464: if (isset($qtys[$child->getId()]) && $qtys[$child->getId()] > 0) {
465: return true;
466: }
467: }
468: if ($item->isShipSeparately()) {
469: return true;
470: }
471: return false;
472: } else if($item->getParentItem()) {
473: if ($item->getIsVirtual()) {
474: return false;
475: }
476: if (isset($qtys[$item->getParentItem()->getId()]) && $qtys[$item->getParentItem()->getId()] > 0) {
477: return true;
478: }
479: return false;
480: }
481: }
482:
483: 484: 485:
486: public function printAction()
487: {
488: $this->_initInvoice();
489: parent::printAction();
490: }
491:
492: }
493: