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_CreditmemoController extends Mage_Adminhtml_Controller_Sales_Creditmemo
00035 {
00036 protected function _getItemData()
00037 {
00038 $data = $this->getRequest()->getParam('creditmemo');
00039 if (isset($data['items'])) {
00040 $qtys = $data['items'];
00041 }
00042 else {
00043 $qtys = array();
00044 }
00045 return $qtys;
00046 }
00047
00048 protected function _canCreditmemo($order)
00049 {
00050
00051
00052
00053 if (!$order->getId()) {
00054 $this->_getSession()->addError($this->__('Order not longer exist'));
00055 return false;
00056 }
00057
00058
00059
00060
00061 if (!$order->canCreditmemo()) {
00062 $this->_getSession()->addError($this->__('Can not do credit memo for order'));
00063 return false;
00064 }
00065 return true;
00066 }
00067
00068
00069
00070
00071
00072
00073 protected function _initCreditmemo($update = false)
00074 {
00075 $creditmemo = false;
00076 if ($creditmemoId = $this->getRequest()->getParam('creditmemo_id')) {
00077 $creditmemo = Mage::getModel('sales/order_creditmemo')->load($creditmemoId);
00078 }
00079 elseif ($orderId = $this->getRequest()->getParam('order_id')) {
00080 $data = $this->getRequest()->getParam('creditmemo');
00081 $order = Mage::getModel('sales/order')->load($orderId);
00082 $invoiceId = $this->getRequest()->getParam('invoice_id');
00083 $invoice= null;
00084
00085 if (!$this->_canCreditmemo($order)) {
00086 return false;
00087 }
00088
00089 if ($invoiceId) {
00090 $invoice = Mage::getModel('sales/order_invoice')
00091 ->load($invoiceId)
00092 ->setOrder($order);
00093 }
00094
00095 $convertor = Mage::getModel('sales/convert_order');
00096 $creditmemo = $convertor->toCreditmemo($order)
00097 ->setInvoice($invoice);
00098
00099 $savedData = $this->_getItemData();
00100
00101 if ($invoice && $invoice->getId()) {
00102 foreach ($invoice->getAllItems() as $invoiceItem) {
00103 $orderItem = $invoiceItem->getOrderItem();
00104
00105 if (!$orderItem->isDummy() && !$orderItem->getQtyToRefund()) {
00106 continue;
00107 }
00108
00109 if (!$update && $orderItem->isDummy() && !empty($savedData) && !$this->_needToAddDummy($orderItem, $savedData)) {
00110 continue;
00111 }
00112
00113 $item = $convertor->itemToCreditmemoItem($orderItem);
00114 if (isset($savedData[$orderItem->getId()]['qty'])) {
00115 $qty = $savedData[$orderItem->getId()]['qty'];
00116 }
00117 else {
00118 if ($orderItem->isDummy()) {
00119 if ($orderItem->getParentItem() && isset($savedData[$orderItem->getParentItem()->getId()]['qty'])) {
00120 $parentItemNewQty = $savedData[$orderItem->getParentItem()->getId()]['qty'];
00121 $parentItemOrigQty = $orderItem->getParentItem()->getQtyOrdered();
00122 $itemOrigQty = $orderItem->getQtyOrdered()/$parentItemOrigQty;
00123 $qty = $itemOrigQty*$parentItemNewQty;
00124 if (isset($savedData[$orderItem->getParentItem()->getId()]['back_to_stock'])) {
00125 $savedData[$orderItem->getId()]['back_to_stock'] = 1;
00126 }
00127 } else {
00128 $qty = 1;
00129 }
00130 } else {
00131 $qty = min($orderItem->getQtyToRefund(), $invoiceItem->getQty());
00132 }
00133 }
00134
00135 $item->setQty($qty);
00136
00137 $children = $orderItem->getChildrenItems();
00138 if (!empty($children)) {
00139 $item->setBackToStock(false);
00140 } else {
00141 $item->setBackToStock(isset($savedData[$orderItem->getId()]['back_to_stock']));
00142 }
00143
00144 $creditmemo->addItem($item);
00145 }
00146 } else {
00147 foreach ($order->getAllItems() as $orderItem) {
00148
00149 if (!$orderItem->isDummy() && !$orderItem->getQtyToRefund()) {
00150 continue;
00151 }
00152
00153 if (!$update && $orderItem->isDummy() && !empty($savedData) && !$this->_needToAddDummy($orderItem, $savedData)) {
00154 continue;
00155 }
00156
00157 $item = $convertor->itemToCreditmemoItem($orderItem);
00158 if (isset($savedData[$orderItem->getId()]['qty'])) {
00159 $qty = $savedData[$orderItem->getId()]['qty'];
00160 }
00161 else {
00162 if ($orderItem->isDummy()) {
00163 if ($orderItem->getParentItem() && isset($savedData[$orderItem->getParentItem()->getId()]['qty'])) {
00164 $parentItemNewQty = $savedData[$orderItem->getParentItem()->getId()]['qty'];
00165 $parentItemOrigQty = $orderItem->getParentItem()->getQtyOrdered();
00166 $itemOrigQty = $orderItem->getQtyOrdered()/$parentItemOrigQty;
00167 $qty = $itemOrigQty*$parentItemNewQty;
00168 if (isset($savedData[$orderItem->getParentItem()->getId()]['back_to_stock'])) {
00169 $savedData[$orderItem->getId()]['back_to_stock'] = 1;
00170 }
00171 } else {
00172 $qty = 1;
00173 }
00174 } else {
00175 $qty = $orderItem->getQtyToRefund();
00176 }
00177 }
00178
00179 $item->setQty($qty);
00180
00181 $children = $orderItem->getChildrenItems();
00182 if (!empty($children)) {
00183 $item->setBackToStock(false);
00184 } else {
00185 $item->setBackToStock(isset($savedData[$orderItem->getId()]['back_to_stock']));
00186 }
00187
00188 $creditmemo->addItem($item);
00189 }
00190 }
00191
00192 if (isset($data['shipping_amount'])) {
00193 $creditmemo->setShippingAmount($data['shipping_amount']);
00194 } elseif ($invoice) {
00195 $creditmemo->setShippingAmount($invoice->getShippingAmount());
00196 }
00197 else {
00198 $creditmemo->setShippingAmount(
00199 $order->getBaseShippingAmount()-$order->getBaseShippingRefunded()
00200 );
00201 }
00202
00203 if (isset($data['adjustment_positive'])) {
00204 $creditmemo->setAdjustmentPositive($data['adjustment_positive']);
00205 }
00206 if (isset($data['adjustment_negative'])) {
00207 $creditmemo->setAdjustmentNegative($data['adjustment_negative']);
00208 }
00209
00210 $creditmemo->collectTotals();
00211 }
00212
00213 $args = array(
00214 'creditmemo' => $creditmemo,
00215 'request' => $this->getRequest(),
00216 );
00217 Mage::dispatchEvent('adminhtml_sales_order_creditmemo_register_before', $args);
00218
00219 Mage::register('current_creditmemo', $creditmemo);
00220 return $creditmemo;
00221 }
00222
00223 protected function _saveCreditmemo($creditmemo)
00224 {
00225 $transactionSave = Mage::getModel('core/resource_transaction')
00226 ->addObject($creditmemo)
00227 ->addObject($creditmemo->getOrder());
00228 if ($creditmemo->getInvoice()) {
00229 $transactionSave->addObject($creditmemo->getInvoice());
00230 }
00231 $transactionSave->save();
00232
00233 return $this;
00234 }
00235
00236
00237
00238
00239 public function viewAction()
00240 {
00241 if ($creditmemo = $this->_initCreditmemo()) {
00242 $this->loadLayout();
00243 $this->getLayout()->getBlock('sales_creditmemo_view')
00244 ->updateBackButtonUrl($this->getRequest()->getParam('come_from'));
00245 $this->_setActiveMenu('sales/order')
00246 ->renderLayout();
00247 }
00248 else {
00249 $this->_forward('noRoute');
00250 }
00251 }
00252
00253
00254
00255
00256 public function startAction()
00257 {
00258
00259
00260
00261 $this->_redirect('*/*/new', array('_current'=>true));
00262 }
00263
00264
00265
00266
00267 public function newAction()
00268 {
00269 if ($creditmemo = $this->_initCreditmemo()) {
00270 $commentText = Mage::getSingleton('adminhtml/session')->getCommentText(true);
00271
00272 $creditmemo->addData(array('commentText'=>$commentText));
00273
00274 $this->loadLayout()
00275 ->_setActiveMenu('sales/order')
00276 ->renderLayout();
00277 }
00278 else {
00279 $this->_forward('noRoute');
00280 }
00281 }
00282
00283
00284
00285
00286 public function updateQtyAction()
00287 {
00288 try {
00289 $creditmemo = $this->_initCreditmemo(true);
00290 $this->loadLayout();
00291 $response = $this->getLayout()->getBlock('order_items')->toHtml();
00292 }
00293 catch (Mage_Core_Exception $e) {
00294 $response = array(
00295 'error' => true,
00296 'message' => $e->getMessage()
00297 );
00298 $response = Zend_Json::encode($response);
00299 }
00300 catch (Exception $e) {
00301 $response = array(
00302 'error' => true,
00303 'message' => $this->__('Can not update item qty')
00304 );
00305 $response = Zend_Json::encode($response);
00306 }
00307 $this->getResponse()->setBody($response);
00308 }
00309
00310
00311
00312
00313
00314 public function saveAction()
00315 {
00316 $data = $this->getRequest()->getPost('creditmemo');
00317 try {
00318 if ($creditmemo = $this->_initCreditmemo()) {
00319 if (($creditmemo->getGrandTotal() <=0) && (!$creditmemo->getAllowZeroGrandTotal())) {
00320 Mage::throwException(
00321 $this->__('Credit Memo total must be positive.')
00322 );
00323 }
00324
00325 Mage::getSingleton('adminhtml/session')->setCommentText($data['comment_text']);
00326
00327 $comment = '';
00328 if (!empty($data['comment_text'])) {
00329 $comment = $data['comment_text'];
00330 $creditmemo->addComment($data['comment_text'], isset($data['comment_customer_notify']));
00331 }
00332
00333 if (isset($data['do_refund'])) {
00334 $creditmemo->setRefundRequested(true);
00335 }
00336 if (isset($data['do_offline'])) {
00337 $creditmemo->setOfflineRequested($data['do_offline']);
00338 }
00339
00340 $creditmemo->register();
00341 if (!empty($data['send_email'])) {
00342 $creditmemo->setEmailSent(true);
00343 }
00344
00345 $this->_saveCreditmemo($creditmemo);
00346 $creditmemo->sendEmail(!empty($data['send_email']), $comment);
00347 $this->_getSession()->addSuccess($this->__('Credit Memo was successfully created'));
00348 Mage::getSingleton('adminhtml/session')->getCommentText(true);
00349 $this->_redirect('*/sales_order/view', array('order_id' => $creditmemo->getOrderId()));
00350 return;
00351 }
00352 else {
00353 $this->_forward('noRoute');
00354 return;
00355 }
00356 }
00357 catch (Mage_Core_Exception $e) {
00358 $this->_getSession()->addError($e->getMessage());
00359 }
00360 catch (Exception $e) {
00361 $this->_getSession()->addError($this->__('Can not save credit memo'));
00362 }
00363 $this->_redirect('*/*/new', array('_current' => true));
00364 }
00365
00366
00367
00368
00369 public function cancelAction()
00370 {
00371 if ($creditmemo = $this->_initCreditmemo()) {
00372 try {
00373 $creditmemo->cancel();
00374 $this->_saveCreditmemo($creditmemo);
00375 $this->_getSession()->addSuccess($this->__('Credit Memo was successfully canceled.'));
00376 }
00377 catch (Mage_Core_Exception $e) {
00378 $this->_getSession()->addError($e->getMessage());
00379 }
00380 catch (Exception $e) {
00381 $this->_getSession()->addError($this->__('Credit Memo cancel error.'));
00382 }
00383 $this->_redirect('*/*/view', array('creditmemo_id'=>$creditmemo->getId()));
00384 }
00385 else {
00386 $this->_forward('noRoute');
00387 }
00388 }
00389
00390
00391
00392
00393 public function voidAction()
00394 {
00395 if ($invoice = $this->_initCreditmemo()) {
00396 try {
00397 $creditmemo->void();
00398 $this->_saveCreditmemo($creditmemo);
00399 $this->_getSession()->addSuccess($this->__('Credit Memo was successfully voided'));
00400 }
00401 catch (Mage_Core_Exception $e) {
00402 $this->_getSession()->addError($e->getMessage());
00403 }
00404 catch (Exception $e) {
00405 $this->_getSession()->addError($this->__('Credit Memo void error'));
00406 }
00407 $this->_redirect('*/*/view', array('creditmemo_id'=>$creditmemo->getId()));
00408 }
00409 else {
00410 $this->_forward('noRoute');
00411 }
00412 }
00413
00414 public function addCommentAction()
00415 {
00416 try {
00417 $this->getRequest()->setParam(
00418 'creditmemo_id',
00419 $this->getRequest()->getParam('id')
00420 );
00421 $data = $this->getRequest()->getPost('comment');
00422 if (empty($data['comment'])) {
00423 Mage::throwException($this->__('Comment text field can not be empty.'));
00424 }
00425 $creditmemo = $this->_initCreditmemo();
00426 $creditmemo->addComment($data['comment'], isset($data['is_customer_notified']));
00427 $creditmemo->save();
00428 $creditmemo->sendUpdateEmail(!empty($data['is_customer_notified']), $data['comment']);
00429
00430 $this->loadLayout();
00431 $response = $this->getLayout()->getBlock('creditmemo_comments')->toHtml();
00432 }
00433 catch (Mage_Core_Exception $e) {
00434 $response = array(
00435 'error' => true,
00436 'message' => $e->getMessage()
00437 );
00438 $response = Zend_Json::encode($response);
00439 }
00440 catch (Exception $e) {
00441 $response = array(
00442 'error' => true,
00443 'message' => $this->__('Can not add new comment.')
00444 );
00445 $response = Zend_Json::encode($response);
00446 }
00447 $this->getResponse()->setBody($response);
00448 }
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459 protected function _needToAddDummy($item, $qtys) {
00460 if ($item->getHasChildren()) {
00461 foreach ($item->getChildrenItems() as $child) {
00462 if (isset($qtys[$child->getId()])
00463 && isset($qtys[$child->getId()]['qty'])
00464 && $qtys[$child->getId()]['qty'] > 0)
00465 {
00466 return true;
00467 }
00468 }
00469 return false;
00470 } else if($item->getParentItem()) {
00471 if (isset($qtys[$item->getParentItem()->getId()])
00472 && isset($qtys[$item->getParentItem()->getId()]['qty'])
00473 && $qtys[$item->getParentItem()->getId()]['qty'] > 0)
00474 {
00475 return true;
00476 }
00477 return false;
00478 }
00479 }
00480 }