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_ShipmentController extends Mage_Adminhtml_Controller_Sales_Shipment
00035 {
00036 protected function _getItemQtys()
00037 {
00038 $data = $this->getRequest()->getParam('shipment');
00039 if (isset($data['items'])) {
00040 $qtys = $data['items'];
00041 }
00042 else {
00043 $qtys = array();
00044 }
00045 return $qtys;
00046 }
00047
00048
00049
00050
00051
00052
00053 protected function _initShipment()
00054 {
00055 $shipment = false;
00056 if ($shipmentId = $this->getRequest()->getParam('shipment_id')) {
00057 $shipment = Mage::getModel('sales/order_shipment')->load($shipmentId);
00058 }
00059 elseif ($orderId = $this->getRequest()->getParam('order_id')) {
00060 $order = Mage::getModel('sales/order')->load($orderId);
00061
00062
00063
00064
00065 if (!$order->getId()) {
00066 $this->_getSession()->addError($this->__('Order not longer exist.'));
00067 return false;
00068 }
00069
00070
00071
00072 if ($order->getForcedDoShipmentWithInvoice()) {
00073 $this->_getSession()->addError($this->__('Can not do shipment for order separate from invoice.'));
00074 return false;
00075 }
00076
00077
00078
00079 if (!$order->canShip()) {
00080 $this->_getSession()->addError($this->__('Can not do shipment for order.'));
00081 return false;
00082 }
00083
00084 $convertor = Mage::getModel('sales/convert_order');
00085 $shipment = $convertor->toShipment($order);
00086 $savedQtys = $this->_getItemQtys();
00087 foreach ($order->getAllItems() as $orderItem) {
00088 if (!$orderItem->isDummy(true) && !$orderItem->getQtyToShip()) {
00089 continue;
00090 }
00091 if ($orderItem->isDummy(true) && !$this->_needToAddDummy($orderItem, $savedQtys)) {
00092 continue;
00093 }
00094 if ($orderItem->getIsVirtual()) {
00095 continue;
00096 }
00097 $item = $convertor->itemToShipmentItem($orderItem);
00098 if (isset($savedQtys[$orderItem->getId()])) {
00099 if ($savedQtys[$orderItem->getId()] > 0) {
00100 $qty = $savedQtys[$orderItem->getId()];
00101 } else {
00102 continue;
00103 }
00104 }
00105 else {
00106 if ($orderItem->isDummy(true)) {
00107 $qty = 1;
00108 } else {
00109 $qty = $orderItem->getQtyToShip();
00110 }
00111 }
00112 $item->setQty($qty);
00113 $shipment->addItem($item);
00114 }
00115 if ($tracks = $this->getRequest()->getPost('tracking')) {
00116 foreach ($tracks as $data) {
00117 $track = Mage::getModel('sales/order_shipment_track')
00118 ->addData($data);
00119 $shipment->addTrack($track);
00120 }
00121 }
00122 }
00123
00124 Mage::register('current_shipment', $shipment);
00125 return $shipment;
00126 }
00127
00128 protected function _saveShipment($shipment)
00129 {
00130 $shipment->getOrder()->setIsInProcess(true);
00131 $transactionSave = Mage::getModel('core/resource_transaction')
00132 ->addObject($shipment)
00133 ->addObject($shipment->getOrder())
00134 ->save();
00135
00136 return $this;
00137 }
00138
00139
00140
00141
00142 public function viewAction()
00143 {
00144 if ($shipment = $this->_initShipment()) {
00145 $this->loadLayout();
00146 $this->getLayout()->getBlock('sales_shipment_view')
00147 ->updateBackButtonUrl($this->getRequest()->getParam('come_from'));
00148 $this->_setActiveMenu('sales/order')
00149 ->renderLayout();
00150 }
00151 else {
00152 $this->_forward('noRoute');
00153 }
00154 }
00155
00156
00157
00158
00159 public function startAction()
00160 {
00161
00162
00163
00164 $this->_redirect('*/*/new', array('order_id'=>$this->getRequest()->getParam('order_id')));
00165 }
00166
00167
00168
00169
00170 public function newAction()
00171 {
00172 if ($shipment = $this->_initShipment()) {
00173 $this->loadLayout()
00174 ->_setActiveMenu('sales/order')
00175 ->renderLayout();
00176 }
00177 else {
00178 $this->_redirect('*/sales_order/view', array('order_id'=>$this->getRequest()->getParam('order_id')));
00179 }
00180 }
00181
00182
00183
00184
00185
00186 public function saveAction()
00187 {
00188 $data = $this->getRequest()->getPost('shipment');
00189
00190 try {
00191 if ($shipment = $this->_initShipment()) {
00192 $shipment->register();
00193
00194 $comment = '';
00195 if (!empty($data['comment_text'])) {
00196 $shipment->addComment($data['comment_text'], isset($data['comment_customer_notify']));
00197 $comment = $data['comment_text'];
00198 }
00199
00200 if (!empty($data['send_email'])) {
00201 $shipment->setEmailSent(true);
00202 }
00203
00204 $this->_saveShipment($shipment);
00205 $shipment->sendEmail(!empty($data['send_email']), $comment);
00206 $this->_getSession()->addSuccess($this->__('Shipment was successfully created.'));
00207 $this->_redirect('*/sales_order/view', array('order_id' => $shipment->getOrderId()));
00208 return;
00209 }
00210 else {
00211 $this->_forward('noRoute');
00212 return;
00213 }
00214 }
00215 catch (Mage_Core_Exception $e) {
00216 $this->_getSession()->addError($e->getMessage());
00217 }
00218 catch (Exception $e) {
00219 $this->_getSession()->addError($this->__('Can not save shipment.'));
00220 }
00221 $this->_redirect('*/*/new', array('order_id' => $this->getRequest()->getParam('order_id')));
00222 }
00223
00224 public function emailAction()
00225 {
00226 try {
00227 if ($shipment = $this->_initShipment()) {
00228 $shipment->sendEmail(true)
00229 ->setEmailSent(true)
00230 ->save();
00231 $this->_getSession()->addSuccess($this->__('Shipment was successfully sent.'));
00232 }
00233 }
00234 catch (Mage_Core_Exception $e) {
00235 $this->_getSession()->addError($e->getMessage());
00236 }
00237 catch (Exception $e) {
00238 $this->_getSession()->addError($this->__('Can not send shipment information.'));
00239 }
00240 $this->_redirect('*/*/view', array(
00241 'shipment_id' => $this->getRequest()->getParam('shipment_id')
00242 ));
00243 }
00244
00245
00246
00247
00248 public function addTrackAction()
00249 {
00250 try {
00251 $carrier = $this->getRequest()->getPost('carrier');
00252 $number = $this->getRequest()->getPost('number');
00253 $title = $this->getRequest()->getPost('title');
00254 if (empty($carrier)) {
00255 Mage::throwException($this->__('You need specify carrier.'));
00256 }
00257 if (empty($number)) {
00258 Mage::throwException($this->__('Tracking number can not be empty.'));
00259 }
00260 if ($shipment = $this->_initShipment()) {
00261 $track = Mage::getModel('sales/order_shipment_track')
00262 ->setNumber($number)
00263 ->setCarrierCode($carrier)
00264 ->setTitle($title);
00265 $shipment->addTrack($track)
00266 ->save();
00267
00268 $this->loadLayout();
00269 $response = $this->getLayout()->getBlock('shipment_tracking')->toHtml();
00270 }
00271 else {
00272 $response = array(
00273 'error' => true,
00274 'message' => $this->__('Can not initialize shipment for adding tracking number.'),
00275 );
00276 }
00277 }
00278 catch (Mage_Core_Exception $e) {
00279 $response = array(
00280 'error' => true,
00281 'message' => $e->getMessage(),
00282 );
00283 }
00284 catch (Exception $e) {
00285 $response = array(
00286 'error' => true,
00287 'message' => $this->__('Can not add tracking number.'),
00288 );
00289 }
00290 if (is_array($response)) {
00291 $response = Zend_Json::encode($response);
00292 }
00293 $this->getResponse()->setBody($response);
00294 }
00295
00296 public function removeTrackAction()
00297 {
00298 $trackId = $this->getRequest()->getParam('track_id');
00299 $shipmentId = $this->getRequest()->getParam('shipment_id');
00300 $track = Mage::getModel('sales/order_shipment_track')->load($trackId);
00301 if ($track->getId()) {
00302 try {
00303 if ($shipmentId = $this->_initShipment()) {
00304 $track->delete();
00305
00306 $this->loadLayout();
00307 $response = $this->getLayout()->getBlock('shipment_tracking')->toHtml();
00308 }
00309 else {
00310 $response = array(
00311 'error' => true,
00312 'message' => $this->__('Can not initialize shipment for delete tracking number.'),
00313 );
00314 }
00315 }
00316 catch (Exception $e) {
00317 $response = array(
00318 'error' => true,
00319 'message' => $this->__('Can not delete tracking number.'),
00320 );
00321 }
00322 }
00323 else {
00324 $response = array(
00325 'error' => true,
00326 'message' => $this->__('Can not load track with retrieving identifier.'),
00327 );
00328 }
00329 if (is_array($response)) {
00330 $response = Zend_Json::encode($response);
00331 }
00332 $this->getResponse()->setBody($response);
00333 }
00334
00335 public function viewTrackAction()
00336 {
00337 $trackId = $this->getRequest()->getParam('track_id');
00338 $shipmentId = $this->getRequest()->getParam('shipment_id');
00339 $track = Mage::getModel('sales/order_shipment_track')->load($trackId);
00340 if ($track->getId()) {
00341 try {
00342 $response = $track->getNumberDetail();
00343 }
00344 catch (Exception $e) {
00345 $response = array(
00346 'error' => true,
00347 'message' => $this->__('Can not retrieve tracking number detail.'),
00348 );
00349 }
00350 }
00351 else {
00352 $response = array(
00353 'error' => true,
00354 'message' => $this->__('Can not load track with retrieving identifier.'),
00355 );
00356 }
00357
00358 if ( is_object($response)){
00359 $className = Mage::getConfig()->getBlockClassName('adminhtml/template');
00360 $block = new $className();
00361 $block->setType('adminhtml/template')
00362 ->setIsAnonymous(true)
00363 ->setTemplate('sales/order/shipment/tracking/info.phtml');
00364
00365 $block->setTrackingInfo($response);
00366
00367 $this->getResponse()->setBody($block->toHtml());
00368 }
00369 else {
00370 if (is_array($response)) {
00371 $response = Zend_Json::encode($response);
00372 }
00373
00374 $this->getResponse()->setBody($response);
00375 }
00376 }
00377
00378 public function addCommentAction()
00379 {
00380 try {
00381 $this->getRequest()->setParam(
00382 'shipment_id',
00383 $this->getRequest()->getParam('id')
00384 );
00385 $data = $this->getRequest()->getPost('comment');
00386 if (empty($data['comment'])) {
00387 Mage::throwException($this->__('Comment text field can not be empty.'));
00388 }
00389 $shipment = $this->_initShipment();
00390 $shipment->addComment($data['comment'], isset($data['is_customer_notified']));
00391 $shipment->sendUpdateEmail(!empty($data['is_customer_notified']), $data['comment']);
00392 $shipment->save();
00393
00394 $this->loadLayout();
00395 $response = $this->getLayout()->getBlock('shipment_comments')->toHtml();
00396 }
00397 catch (Mage_Core_Exception $e) {
00398 $response = array(
00399 'error' => true,
00400 'message' => $e->getMessage()
00401 );
00402 $response = Zend_Json::encode($response);
00403 }
00404 catch (Exception $e) {
00405 $response = array(
00406 'error' => true,
00407 'message' => $this->__('Can not add new comment.')
00408 );
00409 $response = Zend_Json::encode($response);
00410 }
00411 $this->getResponse()->setBody($response);
00412 }
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423 protected function _needToAddDummy($item, $qtys) {
00424 if ($item->getHasChildren()) {
00425 foreach ($item->getChildrenItems() as $child) {
00426 if ($child->getIsVirtual()) {
00427 continue;
00428 }
00429 if ((isset($qtys[$child->getId()]) && $qtys[$child->getId()] > 0) || (!isset($qtys[$child->getId()]) && $child->getQtyToShip())) {
00430 return true;
00431 }
00432 }
00433 return false;
00434 } else if($item->getParentItem()) {
00435 if ($item->getIsVirtual()) {
00436 return false;
00437 }
00438 if ((isset($qtys[$item->getParentItem()->getId()]) && $qtys[$item->getParentItem()->getId()] > 0)
00439 || (!isset($qtys[$item->getParentItem()->getId()]) && $item->getParentItem()->getQtyToShip())) {
00440 return true;
00441 }
00442 return false;
00443 }
00444 }
00445 }