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 class Mage_Adminhtml_Sales_Order_InvoiceController extends Mage_Adminhtml_Controller_Sales_Invoice
00035 {
00036 protected function _getItemQtys()
00037 {
00038 $data = $this->getRequest()->getParam('invoice');
00039 if (isset($data['items'])) {
00040 $qtys = $data['items'];
00041
00042 }
00043
00044
00045
00046 else {
00047 $qtys = array();
00048 }
00049 return $qtys;
00050 }
00051
00052
00053
00054
00055
00056
00057 protected function _initInvoice($update = false)
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
00067
00068 if (!$order->getId()) {
00069 $this->_getSession()->addError($this->__('Order not longer exist'));
00070 return false;
00071 }
00072
00073
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
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 }
00119
00120
00121
00122
00123
00124
00125
00126 protected function _saveInvoice($invoice)
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 }
00136
00137
00138
00139
00140
00141
00142
00143 protected function _prepareShipment($invoice)
00144 {
00145 $convertor = Mage::getModel('sales/convert_order');
00146
00147 $shipment = $convertor->toShipment($invoice->getOrder());
00148
00149 $savedQtys = $this->_getItemQtys();
00150 $skipedParent = array();
00151
00152 foreach ($invoice->getOrder()->getAllItems() as $item) {
00153
00154
00155
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
00173
00174
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
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 }
00213
00214
00215
00216
00217 public function viewAction()
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 }
00230
00231
00232
00233
00234 public function startAction()
00235 {
00236
00237
00238
00239 $this->_getSession()->getInvoiceItemQtys(true);
00240 $this->_redirect('*/*/new', array('order_id'=>$this->getRequest()->getParam('order_id')));
00241 }
00242
00243
00244
00245
00246 public function newAction()
00247 {
00248 if ($invoice = $this->_initInvoice()) {
00249 $this->loadLayout()
00250 ->_setActiveMenu('sales/order')
00251 ->renderLayout();
00252 }
00253 else {
00254
00255 $this->_redirect('*/sales_order/view', array('order_id'=>$this->getRequest()->getParam('order_id')));
00256 }
00257 }
00258
00259
00260
00261
00262 public function updateQtyAction()
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 }
00285
00286
00287
00288
00289
00290 public function saveAction()
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
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 }
00361
00362
00363
00364
00365
00366 public function captureAction()
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 }
00386
00387
00388
00389
00390 public function cancelAction()
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 }
00410
00411
00412
00413
00414 public function voidAction()
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 }
00434
00435 public function addCommentAction()
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 }
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477 protected function _needToAddDummy($item, $qtys) {
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 }
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502 protected function _needToAddDummyForShipment($item, $qtys) {
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 }
00526
00527 }