Mage_Adminhtml_Sales_Order_InvoiceController Class Reference

Inheritance diagram for Mage_Adminhtml_Sales_Order_InvoiceController:

Mage_Adminhtml_Controller_Sales_Invoice Mage_Adminhtml_Controller_Action Mage_Core_Controller_Varien_Action

List of all members.

Public Member Functions

 viewAction ()
 startAction ()
 newAction ()
 updateQtyAction ()
 saveAction ()
 captureAction ()
 cancelAction ()
 voidAction ()
 addCommentAction ()

Protected Member Functions

 _getItemQtys ()
 _initInvoice ($update=false)
 _saveInvoice ($invoice)
 _prepareShipment ($invoice)
 _needToAddDummy ($item, $qtys)
 _needToAddDummyForShipment ($item, $qtys)


Detailed Description

Definition at line 34 of file InvoiceController.php.


Member Function Documentation

_getItemQtys (  )  [protected]

Definition at line 36 of file InvoiceController.php.

00037     {
00038         $data = $this->getRequest()->getParam('invoice');
00039         if (isset($data['items'])) {
00040             $qtys = $data['items'];
00041             //$this->_getSession()->setInvoiceItemQtys($qtys);
00042         }
00043         /*elseif ($this->_getSession()->getInvoiceItemQtys()) {
00044             $qtys = $this->_getSession()->getInvoiceItemQtys();
00045         }*/
00046         else {
00047             $qtys = array();
00048         }
00049         return $qtys;
00050     }

_initInvoice ( update = false  )  [protected]

Initialize invoice model instance

Returns:
Mage_Sales_Model_Order_Invoice

Check order existing

Check invoice create availability

Definition at line 57 of file InvoiceController.php.

00058     {
00059         $invoice = false;
00060         if ($invoiceId = $this->getRequest()->getParam('invoice_id')) {
00061             $invoice = Mage::getModel('sales/order_invoice')->load($invoiceId);
00062         }
00063         elseif ($orderId = $this->getRequest()->getParam('order_id')) {
00064             $order      = Mage::getModel('sales/order')->load($orderId);
00065             /**
00066              * Check order existing
00067              */
00068             if (!$order->getId()) {
00069                 $this->_getSession()->addError($this->__('Order not longer exist'));
00070                 return false;
00071             }
00072             /**
00073              * Check invoice create availability
00074              */
00075             if (!$order->canInvoice()) {
00076                 $this->_getSession()->addError($this->__('Can not do invoice for order'));
00077                 return false;
00078             }
00079 
00080             $convertor  = Mage::getModel('sales/convert_order');
00081             $invoice    = $convertor->toInvoice($order);
00082 
00083             $savedQtys = $this->_getItemQtys();
00084             /* @var $orderItem Mage_Sales_Model_Order_Item */
00085             foreach ($order->getAllItems() as $orderItem) {
00086 
00087                 if (!$orderItem->isDummy() && !$orderItem->getQtyToInvoice() && $orderItem->getLockedDoInvoice()) {
00088                     continue;
00089                 }
00090 
00091                 if ($order->getForcedDoShipmentWithInvoice() && $orderItem->getLockedDoShip()) {
00092                     continue;
00093                 }
00094 
00095                 if (!$update && $orderItem->isDummy() && !empty($savedQtys) && !$this->_needToAddDummy($orderItem, $savedQtys)) {
00096                     continue;
00097                 }
00098                 $item = $convertor->itemToInvoiceItem($orderItem);
00099 
00100                 if (isset($savedQtys[$orderItem->getId()])) {
00101                     $qty = $savedQtys[$orderItem->getId()];
00102                 }
00103                 else {
00104                     if ($orderItem->isDummy()) {
00105                         $qty = 1;
00106                     } else {
00107                         $qty = $orderItem->getQtyToInvoice();
00108                     }
00109                 }
00110                 $item->setQty($qty);
00111                 $invoice->addItem($item);
00112             }
00113             $invoice->collectTotals();
00114         }
00115 
00116         Mage::register('current_invoice', $invoice);
00117         return $invoice;
00118     }

_needToAddDummy ( item,
qtys 
) [protected]

Decides if we need to create dummy invoice item or not for eaxample we don't need create dummy parent if all children are not in process

Parameters:
Mage_Sales_Model_Order_Item $item
array $qtys
Returns:
bool

Definition at line 477 of file InvoiceController.php.

00477                                                      {
00478         if ($item->getHasChildren()) {
00479             foreach ($item->getChildrenItems() as $child) {
00480                 if (isset($qtys[$child->getId()]) && $qtys[$child->getId()] > 0) {
00481                     return true;
00482                 }
00483             }
00484             return false;
00485         } else if($item->getParentItem()) {
00486             if (isset($qtys[$item->getParentItem()->getId()]) && $qtys[$item->getParentItem()->getId()] > 0) {
00487                 return true;
00488             }
00489             return false;
00490         }
00491     }

_needToAddDummyForShipment ( item,
qtys 
) [protected]

Decides if we need to create dummy shipment item or not for eaxample we don't need create dummy parent if all children are not in process

Parameters:
Mage_Sales_Model_Order_Item $item
array $qtys
Returns:
bool

Definition at line 502 of file InvoiceController.php.

00502                                                                 {
00503         if ($item->getHasChildren()) {
00504             foreach ($item->getChildrenItems() as $child) {
00505                 if ($child->getIsVirtual()) {
00506                     continue;
00507                 }
00508                 if (isset($qtys[$child->getId()]) && $qtys[$child->getId()] > 0) {
00509                     return true;
00510                 }
00511             }
00512             if ($item->isShipSeparately()) {
00513                 return true;
00514             }
00515             return false;
00516         } else if($item->getParentItem()) {
00517             if ($item->getIsVirtual()) {
00518                 return false;
00519             }
00520             if (isset($qtys[$item->getParentItem()->getId()]) && $qtys[$item->getParentItem()->getId()] > 0) {
00521                 return true;
00522             }
00523             return false;
00524         }
00525     }

_prepareShipment ( invoice  )  [protected]

Prepare shipment

Parameters:
Mage_Sales_Model_Order_Invoice $invoice
Returns:
Mage_Sales_Model_Order_Shipment

if this is a dummy item and we don't need it. we skip it. also if this item is parent we need to mark that we skipped it so children will be also skipped

Definition at line 143 of file InvoiceController.php.

00144     {
00145         $convertor  = Mage::getModel('sales/convert_order');
00146         /* @var $convertor Mage_Sales_Model_Convert_Order */
00147         $shipment    = $convertor->toShipment($invoice->getOrder());
00148 
00149         $savedQtys = $this->_getItemQtys();
00150         $skipedParent = array();
00151 
00152         foreach ($invoice->getOrder()->getAllItems() as $item) {
00153             /*
00154              * if this is child and its parent was skipped
00155              * bc of something we need to skip child also
00156              */
00157             if ($item->getParentItem() && isset($skipedParent[$item->getParentItem()->getId()])){
00158                 continue;
00159             }
00160 
00161             if (isset($savedQtys[$item->getId()])) {
00162                 $qty = min($savedQtys[$item->getId()], $item->getQtyToShip());
00163             } else {
00164                 $qty = $item->getQtyToShip();
00165             }
00166 
00167             if (!$item->isDummy(true) && !$item->getQtyToShip() && $item->getLockedDoShip()) {
00168                 continue;
00169             }
00170 
00171             /**
00172              * if this is a dummy item and we don't need it. we skip it.
00173              * also if this item is parent we need to mark that we skipped
00174              * it so children will be also skipped
00175              */
00176             if ($item->isDummy(true) && !$this->_needToAddDummyForShipment($item, $savedQtys)) {
00177                 if ($item->getChildrenItems()) {
00178                     $skipedParent[$item->getId()] = 1;
00179                 }
00180                 continue;
00181             }
00182 
00183             if ($item->getIsVirtual()) {
00184                 continue;
00185             }
00186 
00187             $shipItem = $convertor->itemToShipmentItem($item);
00188 
00189             if ($item->isDummy(true)) {
00190                 $qty = 1;
00191             }
00192 
00193             $shipItem->setQty($qty);
00194             $shipment->addItem($shipItem);
00195         }
00196 
00197         if (!count($shipment->getAllItems())) {
00198             // no need to create empty shipment
00199             return false;
00200         }
00201 
00202         $shipment->register();
00203 
00204         if ($tracks = $this->getRequest()->getPost('tracking')) {
00205             foreach ($tracks as $data) {
00206                 $track = Mage::getModel('sales/order_shipment_track')
00207                 ->addData($data);
00208                 $shipment->addTrack($track);
00209             }
00210         }
00211         return $shipment;
00212     }

_saveInvoice ( invoice  )  [protected]

Save data for invoice and related order

Parameters:
Mage_Sales_Model_Order_Invoice $invoice
Returns:
Mage_Adminhtml_Sales_Order_InvoiceController

Definition at line 126 of file InvoiceController.php.

00127     {
00128         $invoice->getOrder()->setIsInProcess(true);
00129         $transactionSave = Mage::getModel('core/resource_transaction')
00130             ->addObject($invoice)
00131             ->addObject($invoice->getOrder())
00132             ->save();
00133 
00134         return $this;
00135     }

addCommentAction (  ) 

Definition at line 435 of file InvoiceController.php.

00436     {
00437         try {
00438             $this->getRequest()->setParam('invoice_id', $this->getRequest()->getParam('id'));
00439             $data = $this->getRequest()->getPost('comment');
00440             if (empty($data['comment'])) {
00441                 Mage::throwException($this->__('Comment text field can not be empty.'));
00442             }
00443             $invoice = $this->_initInvoice();
00444             $invoice->addComment($data['comment'], isset($data['is_customer_notified']));
00445             $invoice->sendUpdateEmail(!empty($data['is_customer_notified']), $data['comment']);
00446             $invoice->save();
00447 
00448             $this->loadLayout();
00449             $response = $this->getLayout()->getBlock('invoice_comments')->toHtml();
00450         }
00451         catch (Mage_Core_Exception $e) {
00452             $response = array(
00453                 'error'     => true,
00454                 'message'   => $e->getMessage()
00455             );
00456             $response = Zend_Json::encode($response);
00457         }
00458         catch (Exception $e) {
00459             $response = array(
00460                 'error'     => true,
00461                 'message'   => $this->__('Can not add new comment.')
00462             );
00463             $response = Zend_Json::encode($response);
00464         }
00465         $this->getResponse()->setBody($response);
00466     }

cancelAction (  ) 

Cancel invoice action

Definition at line 390 of file InvoiceController.php.

00391     {
00392         if ($invoice = $this->_initInvoice()) {
00393             try {
00394                 $invoice->cancel();
00395                 $this->_saveInvoice($invoice);
00396                 $this->_getSession()->addSuccess($this->__('Invoice was successfully canceled.'));
00397             }
00398             catch (Mage_Core_Exception $e) {
00399                 $this->_getSession()->addError($e->getMessage());
00400             }
00401             catch (Exception $e) {
00402                 $this->_getSession()->addError($this->__('Invoice cancel error.'));
00403             }
00404             $this->_redirect('*/*/view', array('invoice_id'=>$invoice->getId()));
00405         }
00406         else {
00407             $this->_forward('noRoute');
00408         }
00409     }

captureAction (  ) 

Capture invoice action

Definition at line 366 of file InvoiceController.php.

00367     {
00368         if ($invoice = $this->_initInvoice()) {
00369             try {
00370                 $invoice->capture();
00371                 $this->_saveInvoice($invoice);
00372                 $this->_getSession()->addSuccess($this->__('Invoice was successfully captured'));
00373             }
00374             catch (Mage_Core_Exception $e) {
00375                 $this->_getSession()->addError($e->getMessage());
00376             }
00377             catch (Exception $e) {
00378                 $this->_getSession()->addError($this->__('Invoice capture error'));
00379             }
00380             $this->_redirect('*/*/view', array('invoice_id'=>$invoice->getId()));
00381         }
00382         else {
00383             $this->_forward('noRoute');
00384         }
00385     }

newAction (  ) 

Invoice create page

Definition at line 246 of file InvoiceController.php.

00247     {
00248         if ($invoice = $this->_initInvoice()) {
00249             $this->loadLayout()
00250                 ->_setActiveMenu('sales/order')
00251                 ->renderLayout();
00252         }
00253         else {
00254             // $this->_forward('noRoute');
00255             $this->_redirect('*/sales_order/view', array('order_id'=>$this->getRequest()->getParam('order_id')));
00256         }
00257     }

saveAction (  ) 

Save invoice We can save only new invoice. Existing invoices are not editable

Sending emails

Definition at line 290 of file InvoiceController.php.

00291     {
00292         $data = $this->getRequest()->getPost('invoice');
00293         try {
00294             if ($invoice = $this->_initInvoice()) {
00295 
00296                 if (!empty($data['capture_case'])) {
00297                     $invoice->setRequestedCaptureCase($data['capture_case']);
00298                 }
00299 
00300                 if (!empty($data['comment_text'])) {
00301                     $invoice->addComment($data['comment_text'], isset($data['comment_customer_notify']));
00302                 }
00303 
00304                 $invoice->register();
00305 
00306                 if (!empty($data['send_email'])) {
00307                     $invoice->setEmailSent(true);
00308                 }
00309 
00310                 $invoice->getOrder()->setIsInProcess(true);
00311 
00312                 $transactionSave = Mage::getModel('core/resource_transaction')
00313                     ->addObject($invoice)
00314                     ->addObject($invoice->getOrder());
00315                 $shipment = false;
00316                 if (!empty($data['do_shipment']) || (int) $invoice->getOrder()->getForcedDoShipmentWithInvoice()) {
00317                     $shipment = $this->_prepareShipment($invoice);
00318                     if ($shipment) {
00319                         $shipment->setEmailSent($invoice->getEmailSent());
00320                         $transactionSave->addObject($shipment);
00321                     }
00322                 }
00323                 $transactionSave->save();
00324 
00325                 /**
00326                  * Sending emails
00327                  */
00328                 $comment = '';
00329                 if (isset($data['comment_customer_notify'])) {
00330                     $comment = $data['comment_text'];
00331                 }
00332                 $invoice->sendEmail(!empty($data['send_email']), $comment);
00333                 if ($shipment) {
00334                     $shipment->sendEmail(!empty($data['send_email']));
00335                 }
00336 
00337                 if (!empty($data['do_shipment'])) {
00338                     $this->_getSession()->addSuccess($this->__('Invoice and shipment was successfully created.'));
00339                 }
00340                 else {
00341                     $this->_getSession()->addSuccess($this->__('Invoice was successfully created.'));
00342                 }
00343 
00344                 $this->_redirect('*/sales_order/view', array('order_id' => $invoice->getOrderId()));
00345                 return;
00346             }
00347             else {
00348                 $this->_forward('noRoute');
00349                 return;
00350             }
00351         }
00352         catch (Mage_Core_Exception $e) {
00353             $this->_getSession()->addError($e->getMessage());
00354         }
00355         catch (Exception $e) {
00356             $this->_getSession()->addError($this->__('Can not save invoice'));
00357         }
00358 
00359         $this->_redirect('*/*/new', array('order_id' => $this->getRequest()->getParam('order_id')));
00360     }

startAction (  ) 

Start create invoice action

Clear old values for invoice qty's

Definition at line 234 of file InvoiceController.php.

00235     {
00236         /**
00237          * Clear old values for invoice qty's
00238          */
00239         $this->_getSession()->getInvoiceItemQtys(true);
00240         $this->_redirect('*/*/new', array('order_id'=>$this->getRequest()->getParam('order_id')));
00241     }

updateQtyAction (  ) 

Update items qty action

Definition at line 262 of file InvoiceController.php.

00263     {
00264         try {
00265             $invoice = $this->_initInvoice(true);
00266             $this->loadLayout();
00267             $response = $this->getLayout()->getBlock('order_items')->toHtml();
00268         }
00269         catch (Mage_Core_Exception $e) {
00270             $response = array(
00271                 'error'     => true,
00272                 'message'   => $e->getMessage()
00273             );
00274             $response = Zend_Json::encode($response);
00275         }
00276         catch (Exception $e) {
00277             $response = array(
00278                 'error'     => true,
00279                 'message'   => $this->__('Can not update item qty')
00280             );
00281             $response = Zend_Json::encode($response);
00282         }
00283         $this->getResponse()->setBody($response);
00284     }

viewAction (  ) 

Invoice information page

Reimplemented from Mage_Adminhtml_Controller_Sales_Invoice.

Definition at line 217 of file InvoiceController.php.

00218     {
00219         if ($invoice = $this->_initInvoice()) {
00220             $this->loadLayout()
00221                 ->_setActiveMenu('sales/order');
00222             $this->getLayout()->getBlock('sales_invoice_view')
00223                 ->updateBackButtonUrl($this->getRequest()->getParam('come_from'));
00224             $this->renderLayout();
00225         }
00226         else {
00227             $this->_forward('noRoute');
00228         }
00229     }

voidAction (  ) 

Void invoice action

Definition at line 414 of file InvoiceController.php.

00415     {
00416         if ($invoice = $this->_initInvoice()) {
00417             try {
00418                 $invoice->void();
00419                 $this->_saveInvoice($invoice);
00420                 $this->_getSession()->addSuccess($this->__('Invoice was successfully voided'));
00421             }
00422             catch (Mage_Core_Exception $e) {
00423                 $this->_getSession()->addError($e->getMessage());
00424             }
00425             catch (Exception $e) {
00426                 $this->_getSession()->addError($this->__('Invoice void error'));
00427             }
00428             $this->_redirect('*/*/view', array('invoice_id'=>$invoice->getId()));
00429         }
00430         else {
00431             $this->_forward('noRoute');
00432         }
00433     }


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:23:29 2009 for Magento by  doxygen 1.5.8