PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Adminhtml/controllers/Sales/Order/ShipmentController.php

https://bitbucket.org/claudiu_marginean/magento-hg-mirror
PHP | 451 lines | 324 code | 34 blank | 93 comment | 49 complexity | 7b2ce7be829c6c31f145c4cfd4485296 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Adminhtml
  23. * @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Adminhtml sales order shipment controller
  28. *
  29. * @category Mage
  30. * @package Mage_Adminhtml
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Adminhtml_Sales_Order_ShipmentController extends Mage_Adminhtml_Controller_Sales_Shipment
  34. {
  35. /**
  36. * Initialize shipment items QTY
  37. */
  38. protected function _getItemQtys()
  39. {
  40. $data = $this->getRequest()->getParam('shipment');
  41. if (isset($data['items'])) {
  42. $qtys = $data['items'];
  43. } else {
  44. $qtys = array();
  45. }
  46. return $qtys;
  47. }
  48. /**
  49. * Initialize shipment model instance
  50. *
  51. * @return Mage_Sales_Model_Order_Shipment
  52. */
  53. protected function _initShipment()
  54. {
  55. $this->_title($this->__('Sales'))->_title($this->__('Shipments'));
  56. $shipment = false;
  57. $shipmentId = $this->getRequest()->getParam('shipment_id');
  58. $orderId = $this->getRequest()->getParam('order_id');
  59. if ($shipmentId) {
  60. $shipment = Mage::getModel('sales/order_shipment')->load($shipmentId);
  61. } elseif ($orderId) {
  62. $order = Mage::getModel('sales/order')->load($orderId);
  63. /**
  64. * Check order existing
  65. */
  66. if (!$order->getId()) {
  67. $this->_getSession()->addError($this->__('The order no longer exists.'));
  68. return false;
  69. }
  70. /**
  71. * Check shipment is available to create separate from invoice
  72. */
  73. if ($order->getForcedDoShipmentWithInvoice()) {
  74. $this->_getSession()->addError($this->__('Cannot do shipment for the order separately from invoice.'));
  75. return false;
  76. }
  77. /**
  78. * Check shipment create availability
  79. */
  80. if (!$order->canShip()) {
  81. $this->_getSession()->addError($this->__('Cannot do shipment for the order.'));
  82. return false;
  83. }
  84. $savedQtys = $this->_getItemQtys();
  85. $shipment = Mage::getModel('sales/service_order', $order)->prepareShipment($savedQtys);
  86. $tracks = $this->getRequest()->getPost('tracking');
  87. if ($tracks) {
  88. foreach ($tracks as $data) {
  89. if (empty($data['number'])) {
  90. Mage::throwException($this->__('Tracking number cannot be empty.'));
  91. }
  92. $track = Mage::getModel('sales/order_shipment_track')
  93. ->addData($data);
  94. $shipment->addTrack($track);
  95. }
  96. }
  97. }
  98. Mage::register('current_shipment', $shipment);
  99. return $shipment;
  100. }
  101. /**
  102. * Save shipment and order in one transaction
  103. * @param Mage_Sales_Model_Order_Shipment $shipment
  104. */
  105. protected function _saveShipment($shipment)
  106. {
  107. $shipment->getOrder()->setIsInProcess(true);
  108. $transactionSave = Mage::getModel('core/resource_transaction')
  109. ->addObject($shipment)
  110. ->addObject($shipment->getOrder())
  111. ->save();
  112. return $this;
  113. }
  114. /**
  115. * shipment information page
  116. */
  117. public function viewAction()
  118. {
  119. if ($shipment = $this->_initShipment()) {
  120. $this->_title($this->__('View Shipment'));
  121. $this->loadLayout();
  122. $this->getLayout()->getBlock('sales_shipment_view')
  123. ->updateBackButtonUrl($this->getRequest()->getParam('come_from'));
  124. $this->_setActiveMenu('sales/order')
  125. ->renderLayout();
  126. }
  127. else {
  128. $this->_forward('noRoute');
  129. }
  130. }
  131. /**
  132. * Start create shipment action
  133. */
  134. public function startAction()
  135. {
  136. /**
  137. * Clear old values for shipment qty's
  138. */
  139. $this->_redirect('*/*/new', array('order_id'=>$this->getRequest()->getParam('order_id')));
  140. }
  141. /**
  142. * Shipment create page
  143. */
  144. public function newAction()
  145. {
  146. if ($shipment = $this->_initShipment()) {
  147. $this->_title($this->__('New Shipment'));
  148. if ($comment = Mage::getSingleton('adminhtml/session')->getCommentText(true)) {
  149. $shipment->setCommentText($comment);
  150. }
  151. $this->loadLayout()
  152. ->_setActiveMenu('sales/order')
  153. ->renderLayout();
  154. } else {
  155. $this->_redirect('*/sales_order/view', array('order_id'=>$this->getRequest()->getParam('order_id')));
  156. }
  157. }
  158. /**
  159. * Save shipment
  160. * We can save only new shipment. Existing shipments are not editable
  161. */
  162. public function saveAction()
  163. {
  164. $data = $this->getRequest()->getPost('shipment');
  165. if (!empty($data['comment_text'])) {
  166. Mage::getSingleton('adminhtml/session')->setCommentText($data['comment_text']);
  167. }
  168. try {
  169. if ($shipment = $this->_initShipment()) {
  170. $shipment->register();
  171. $comment = '';
  172. if (!empty($data['comment_text'])) {
  173. $shipment->addComment(
  174. $data['comment_text'],
  175. isset($data['comment_customer_notify']),
  176. isset($data['is_visible_on_front'])
  177. );
  178. if (isset($data['comment_customer_notify'])) {
  179. $comment = $data['comment_text'];
  180. }
  181. }
  182. if (!empty($data['send_email'])) {
  183. $shipment->setEmailSent(true);
  184. }
  185. $shipment->getOrder()->setCustomerNoteNotify(!empty($data['send_email']));
  186. $this->_saveShipment($shipment);
  187. $shipment->sendEmail(!empty($data['send_email']), $comment);
  188. $this->_getSession()->addSuccess($this->__('The shipment has been created.'));
  189. Mage::getSingleton('adminhtml/session')->getCommentText(true);
  190. $this->_redirect('*/sales_order/view', array('order_id' => $shipment->getOrderId()));
  191. return;
  192. } else {
  193. $this->_forward('noRoute');
  194. return;
  195. }
  196. } catch (Mage_Core_Exception $e) {
  197. $this->_getSession()->addError($e->getMessage());
  198. } catch (Exception $e) {
  199. $this->_getSession()->addError($this->__('Cannot save shipment.'));
  200. }
  201. $this->_redirect('*/*/new', array('order_id' => $this->getRequest()->getParam('order_id')));
  202. }
  203. /**
  204. * Send email with shipment data to customer
  205. */
  206. public function emailAction()
  207. {
  208. try {
  209. if ($shipment = $this->_initShipment()) {
  210. $shipment->sendEmail(true)
  211. ->setEmailSent(true)
  212. ->save();
  213. $this->_getSession()->addSuccess($this->__('The shipment has been sent.'));
  214. }
  215. } catch (Mage_Core_Exception $e) {
  216. $this->_getSession()->addError($e->getMessage());
  217. } catch (Exception $e) {
  218. $this->_getSession()->addError($this->__('Cannot send shipment information.'));
  219. }
  220. $this->_redirect('*/*/view', array(
  221. 'shipment_id' => $this->getRequest()->getParam('shipment_id')
  222. ));
  223. }
  224. /**
  225. * Add new tracking number action
  226. */
  227. public function addTrackAction()
  228. {
  229. try {
  230. $carrier = $this->getRequest()->getPost('carrier');
  231. $number = $this->getRequest()->getPost('number');
  232. $title = $this->getRequest()->getPost('title');
  233. if (empty($carrier)) {
  234. Mage::throwException($this->__('The carrier needs to be specified.'));
  235. }
  236. if (empty($number)) {
  237. Mage::throwException($this->__('Tracking number cannot be empty.'));
  238. }
  239. if ($shipment = $this->_initShipment()) {
  240. $track = Mage::getModel('sales/order_shipment_track')
  241. ->setNumber($number)
  242. ->setCarrierCode($carrier)
  243. ->setTitle($title);
  244. $shipment->addTrack($track)
  245. ->save();
  246. $this->loadLayout();
  247. $response = $this->getLayout()->getBlock('shipment_tracking')->toHtml();
  248. } else {
  249. $response = array(
  250. 'error' => true,
  251. 'message' => $this->__('Cannot initialize shipment for adding tracking number.'),
  252. );
  253. }
  254. } catch (Mage_Core_Exception $e) {
  255. $response = array(
  256. 'error' => true,
  257. 'message' => $e->getMessage(),
  258. );
  259. } catch (Exception $e) {
  260. $response = array(
  261. 'error' => true,
  262. 'message' => $this->__('Cannot add tracking number.'),
  263. );
  264. }
  265. if (is_array($response)) {
  266. $response = Mage::helper('core')->jsonEncode($response);
  267. }
  268. $this->getResponse()->setBody($response);
  269. }
  270. /**
  271. * Remove tracking number from shipment
  272. */
  273. public function removeTrackAction()
  274. {
  275. $trackId = $this->getRequest()->getParam('track_id');
  276. $shipmentId = $this->getRequest()->getParam('shipment_id');
  277. $track = Mage::getModel('sales/order_shipment_track')->load($trackId);
  278. if ($track->getId()) {
  279. try {
  280. if ($shipmentId = $this->_initShipment()) {
  281. $track->delete();
  282. $this->loadLayout();
  283. $response = $this->getLayout()->getBlock('shipment_tracking')->toHtml();
  284. } else {
  285. $response = array(
  286. 'error' => true,
  287. 'message' => $this->__('Cannot initialize shipment for delete tracking number.'),
  288. );
  289. }
  290. } catch (Exception $e) {
  291. $response = array(
  292. 'error' => true,
  293. 'message' => $this->__('Cannot delete tracking number.'),
  294. );
  295. }
  296. } else {
  297. $response = array(
  298. 'error' => true,
  299. 'message' => $this->__('Cannot load track with retrieving identifier.'),
  300. );
  301. }
  302. if (is_array($response)) {
  303. $response = Mage::helper('core')->jsonEncode($response);
  304. }
  305. $this->getResponse()->setBody($response);
  306. }
  307. /**
  308. * View shipment tracking information
  309. */
  310. public function viewTrackAction()
  311. {
  312. $trackId = $this->getRequest()->getParam('track_id');
  313. $shipmentId = $this->getRequest()->getParam('shipment_id');
  314. $track = Mage::getModel('sales/order_shipment_track')->load($trackId);
  315. if ($track->getId()) {
  316. try {
  317. $response = $track->getNumberDetail();
  318. } catch (Exception $e) {
  319. $response = array(
  320. 'error' => true,
  321. 'message' => $this->__('Cannot retrieve tracking number detail.'),
  322. );
  323. }
  324. } else {
  325. $response = array(
  326. 'error' => true,
  327. 'message' => $this->__('Cannot load track with retrieving identifier.'),
  328. );
  329. }
  330. if ( is_object($response)){
  331. $className = Mage::getConfig()->getBlockClassName('adminhtml/template');
  332. $block = new $className();
  333. $block->setType('adminhtml/template')
  334. ->setIsAnonymous(true)
  335. ->setTemplate('sales/order/shipment/tracking/info.phtml');
  336. $block->setTrackingInfo($response);
  337. $this->getResponse()->setBody($block->toHtml());
  338. } else {
  339. if (is_array($response)) {
  340. $response = Mage::helper('core')->jsonEncode($response);
  341. }
  342. $this->getResponse()->setBody($response);
  343. }
  344. }
  345. /**
  346. * Add comment to shipment history
  347. */
  348. public function addCommentAction()
  349. {
  350. try {
  351. $this->getRequest()->setParam(
  352. 'shipment_id',
  353. $this->getRequest()->getParam('id')
  354. );
  355. $data = $this->getRequest()->getPost('comment');
  356. if (empty($data['comment'])) {
  357. Mage::throwException($this->__('Comment text field cannot be empty.'));
  358. }
  359. $shipment = $this->_initShipment();
  360. $shipment->addComment(
  361. $data['comment'],
  362. isset($data['is_customer_notified']),
  363. isset($data['is_visible_on_front'])
  364. );
  365. $shipment->sendUpdateEmail(!empty($data['is_customer_notified']), $data['comment']);
  366. $shipment->save();
  367. $this->loadLayout(false);
  368. $response = $this->getLayout()->getBlock('shipment_comments')->toHtml();
  369. } catch (Mage_Core_Exception $e) {
  370. $response = array(
  371. 'error' => true,
  372. 'message' => $e->getMessage()
  373. );
  374. $response = Mage::helper('core')->jsonEncode($response);
  375. } catch (Exception $e) {
  376. $response = array(
  377. 'error' => true,
  378. 'message' => $this->__('Cannot add new comment.')
  379. );
  380. $response = Mage::helper('core')->jsonEncode($response);
  381. }
  382. $this->getResponse()->setBody($response);
  383. }
  384. /**
  385. * Decides if we need to create dummy shipment item or not
  386. * for eaxample we don't need create dummy parent if all
  387. * children are not in process
  388. *
  389. * @deprecated after 1.4, Mage_Sales_Model_Service_Order used
  390. * @param Mage_Sales_Model_Order_Item $item
  391. * @param array $qtys
  392. * @return bool
  393. */
  394. protected function _needToAddDummy($item, $qtys) {
  395. if ($item->getHasChildren()) {
  396. foreach ($item->getChildrenItems() as $child) {
  397. if ($child->getIsVirtual()) {
  398. continue;
  399. }
  400. if ((isset($qtys[$child->getId()]) && $qtys[$child->getId()] > 0)
  401. || (!isset($qtys[$child->getId()]) && $child->getQtyToShip())) {
  402. return true;
  403. }
  404. }
  405. return false;
  406. } else if($item->getParentItem()) {
  407. if ($item->getIsVirtual()) {
  408. return false;
  409. }
  410. if ((isset($qtys[$item->getParentItem()->getId()]) && $qtys[$item->getParentItem()->getId()] > 0)
  411. || (!isset($qtys[$item->getParentItem()->getId()]) && $item->getParentItem()->getQtyToShip())) {
  412. return true;
  413. }
  414. return false;
  415. }
  416. }
  417. }