PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Adminhtml/Block/Sales/Order/Shipment/Packaging.php

https://github.com/cosmocommerce/magento-mirror
PHP | 371 lines | 220 code | 22 blank | 129 comment | 26 complexity | f668a4f51730152b33a35444dfeb982f MD5 | raw file
  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) 2014 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 shipment packaging
  28. *
  29. * @category Mage
  30. * @package Mage_Adminhtml
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Adminhtml_Block_Sales_Order_Shipment_Packaging extends Mage_Adminhtml_Block_Template
  34. {
  35. /**
  36. * Retrieve shipment model instance
  37. *
  38. * @return Mage_Sales_Model_Order_Shipment
  39. */
  40. public function getShipment()
  41. {
  42. return Mage::registry('current_shipment');
  43. }
  44. /**
  45. * Configuration for popup window for packaging
  46. *
  47. * @return string
  48. */
  49. public function getConfigDataJson()
  50. {
  51. $shipmentId = $this->getShipment()->getId();
  52. $orderId = $this->getRequest()->getParam('order_id');
  53. $urlParams = array();
  54. $itemsQty = array();
  55. $itemsPrice = array();
  56. $itemsName = array();
  57. $itemsWeight = array();
  58. $itemsProductId = array();
  59. if ($shipmentId) {
  60. $urlParams['shipment_id'] = $shipmentId;
  61. $createLabelUrl = $this->getUrl('*/sales_order_shipment/createLabel', $urlParams);
  62. $itemsGridUrl = $this->getUrl('*/sales_order_shipment/getShippingItemsGrid', $urlParams);
  63. foreach ($this->getShipment()->getAllItems() as $item) {
  64. $itemsQty[$item->getId()] = $item->getQty();
  65. $itemsPrice[$item->getId()] = $item->getPrice();
  66. $itemsName[$item->getId()] = $item->getName();
  67. $itemsWeight[$item->getId()] = $item->getWeight();
  68. $itemsProductId[$item->getId()] = $item->getProductId();
  69. $itemsOrderItemId[$item->getId()] = $item->getOrderItemId();
  70. }
  71. } else if ($orderId) {
  72. $urlParams['order_id'] = $orderId;
  73. $createLabelUrl = $this->getUrl('*/sales_order_shipment/save', $urlParams);
  74. $itemsGridUrl = $this->getUrl('*/sales_order_shipment/getShippingItemsGrid', $urlParams);
  75. foreach ($this->getShipment()->getAllItems() as $item) {
  76. $itemsQty[$item->getOrderItemId()] = $item->getQty()*1;
  77. $itemsPrice[$item->getOrderItemId()] = $item->getPrice();
  78. $itemsName[$item->getOrderItemId()] = $item->getName();
  79. $itemsWeight[$item->getOrderItemId()] = $item->getWeight();
  80. $itemsProductId[$item->getOrderItemId()] = $item->getProductId();
  81. $itemsOrderItemId[$item->getOrderItemId()] = $item->getOrderItemId();
  82. }
  83. }
  84. $data = array(
  85. 'createLabelUrl' => $createLabelUrl,
  86. 'itemsGridUrl' => $itemsGridUrl,
  87. 'errorQtyOverLimit' => Mage::helper('sales')->__('The quantity you want to add exceeds the total shipped quantity for some of selected Product(s)'),
  88. 'titleDisabledSaveBtn' => Mage::helper('sales')->__('Products should be added to package(s)'),
  89. 'validationErrorMsg' => Mage::helper('sales')->__('The value that you entered is not valid.'),
  90. 'shipmentItemsQty' => $itemsQty,
  91. 'shipmentItemsPrice' => $itemsPrice,
  92. 'shipmentItemsName' => $itemsName,
  93. 'shipmentItemsWeight' => $itemsWeight,
  94. 'shipmentItemsProductId' => $itemsProductId,
  95. 'shipmentItemsOrderItemId' => $itemsOrderItemId,
  96. 'customizable' => $this->_getCustomizableContainers(),
  97. );
  98. return Mage::helper('core')->jsonEncode($data);
  99. }
  100. /**
  101. * Return container types of carrier
  102. *
  103. * @return array
  104. */
  105. public function getContainers()
  106. {
  107. $order = $this->getShipment()->getOrder();
  108. $storeId = $this->getShipment()->getStoreId();
  109. $address = $order->getShippingAddress();
  110. $carrier = $order->getShippingCarrier();
  111. $countryShipper = Mage::getStoreConfig(Mage_Shipping_Model_Shipping::XML_PATH_STORE_COUNTRY_ID, $storeId);
  112. if ($carrier) {
  113. $params = new Varien_Object(array(
  114. 'method' => $order->getShippingMethod(true)->getMethod(),
  115. 'country_shipper' => $countryShipper,
  116. 'country_recipient' => $address->getCountryId(),
  117. ));
  118. return $carrier->getContainerTypes($params);
  119. }
  120. return array();
  121. }
  122. /**
  123. * Get codes of customizable container types of carrier
  124. *
  125. * @return array
  126. */
  127. protected function _getCustomizableContainers()
  128. {
  129. $carrier = $this->getShipment()->getOrder()->getShippingCarrier();
  130. if ($carrier) {
  131. return $carrier->getCustomizableContainerTypes();
  132. }
  133. return array();
  134. }
  135. /**
  136. * Return name of container type by its code
  137. *
  138. * @param string $code
  139. * @return string
  140. */
  141. public function getContainerTypeByCode($code)
  142. {
  143. $carrier = $this->getShipment()->getOrder()->getShippingCarrier();
  144. if ($carrier) {
  145. $containerTypes = $carrier->getContainerTypes();
  146. $containerType = !empty($containerTypes[$code]) ? $containerTypes[$code] : '';
  147. return $containerType;
  148. }
  149. return '';
  150. }
  151. /**
  152. * Return name of delivery confirmation type by its code
  153. *
  154. * @param string $code
  155. * @return string
  156. */
  157. public function getDeliveryConfirmationTypeByCode($code)
  158. {
  159. $countryId = $this->getShipment()->getOrder()->getShippingAddress()->getCountryId();
  160. $carrier = $this->getShipment()->getOrder()->getShippingCarrier();
  161. if ($carrier) {
  162. $params = new Varien_Object(array('country_recipient' => $countryId));
  163. $confirmationTypes = $carrier->getDeliveryConfirmationTypes($params);
  164. $confirmationType = !empty($confirmationTypes[$code]) ? $confirmationTypes[$code] : '';
  165. return $confirmationType;
  166. }
  167. return '';
  168. }
  169. /**
  170. * Return name of content type by its code
  171. *
  172. * @param string $code
  173. * @return string
  174. */
  175. public function getContentTypeByCode($code)
  176. {
  177. $contentTypes = $this->getContentTypes();
  178. if (!empty($contentTypes[$code])) {
  179. return $contentTypes[$code];
  180. }
  181. return '';
  182. }
  183. /**
  184. * Get packed products in packages
  185. *
  186. * @return array
  187. */
  188. public function getPackages()
  189. {
  190. $packages = $this->getShipment()->getPackages();
  191. if ($packages) {
  192. $packages = unserialize($packages);
  193. } else {
  194. $packages = array();
  195. }
  196. return $packages;
  197. }
  198. /**
  199. * Get item of shipment by its id
  200. *
  201. * @param $itemId
  202. * @param $itemsOf
  203. * @return Varien_Object
  204. */
  205. public function getShipmentItem($itemId, $itemsOf)
  206. {
  207. $items = $this->getShipment()->getAllItems();
  208. foreach ($items as $item) {
  209. if ($itemsOf == 'order' && $item->getOrderItemId() == $itemId) {
  210. return $item;
  211. } else if ($itemsOf == 'shipment' && $item->getId() == $itemId) {
  212. return $item;
  213. }
  214. }
  215. return new Varien_Object();
  216. }
  217. /**
  218. * Can display customs value
  219. *
  220. * @return bool
  221. */
  222. public function displayCustomsValue()
  223. {
  224. $storeId = $this->getShipment()->getStoreId();
  225. $order = $this->getShipment()->getOrder();
  226. $address = $order->getShippingAddress();
  227. $shipperAddressCountryCode = Mage::getStoreConfig(
  228. Mage_Shipping_Model_Shipping::XML_PATH_STORE_COUNTRY_ID,
  229. $storeId
  230. );
  231. $recipientAddressCountryCode = $address->getCountryId();
  232. if ($shipperAddressCountryCode != $recipientAddressCountryCode) {
  233. return true;
  234. }
  235. return false;
  236. }
  237. /**
  238. * Return delivery confirmation types of current carrier
  239. *
  240. * @return array
  241. */
  242. public function getDeliveryConfirmationTypes()
  243. {
  244. $countryId = $this->getShipment()->getOrder()->getShippingAddress()->getCountryId();
  245. $carrier = $this->getShipment()->getOrder()->getShippingCarrier();
  246. $params = new Varien_Object(array('country_recipient' => $countryId));
  247. if ($carrier && is_array($carrier->getDeliveryConfirmationTypes($params))) {
  248. return $carrier->getDeliveryConfirmationTypes($params);
  249. }
  250. return array();
  251. }
  252. /**
  253. * Print button for creating pdf
  254. *
  255. * @return string
  256. */
  257. public function getPrintButton()
  258. {
  259. $data['shipment_id'] = $this->getShipment()->getId();
  260. $url = $this->getUrl('*/sales_order_shipment/printPackage', $data);
  261. return $this->getLayout()
  262. ->createBlock('adminhtml/widget_button')
  263. ->setData(array(
  264. 'label' => Mage::helper('sales')->__('Print'),
  265. 'onclick' => 'setLocation(\'' . $url . '\')'
  266. ))
  267. ->toHtml();
  268. }
  269. /**
  270. * Check whether girth is allowed for current carrier
  271. *
  272. * @return void
  273. */
  274. public function isGirthAllowed()
  275. {
  276. return $this
  277. ->getShipment()
  278. ->getOrder()
  279. ->getShippingCarrier()
  280. ->isGirthAllowed($this->getShipment()->getOrder()->getShippingAddress()->getCountryId());
  281. }
  282. /**
  283. * Return content types of package
  284. *
  285. * @return array
  286. */
  287. public function getContentTypes()
  288. {
  289. $order = $this->getShipment()->getOrder();
  290. $storeId = $this->getShipment()->getStoreId();
  291. $address = $order->getShippingAddress();
  292. $carrier = $order->getShippingCarrier();
  293. $countryShipper = Mage::getStoreConfig(Mage_Shipping_Model_Shipping::XML_PATH_STORE_COUNTRY_ID, $storeId);
  294. if ($carrier) {
  295. $params = new Varien_Object(array(
  296. 'method' => $order->getShippingMethod(true)->getMethod(),
  297. 'country_shipper' => $countryShipper,
  298. 'country_recipient' => $address->getCountryId(),
  299. ));
  300. return $carrier->getContentTypes($params);
  301. }
  302. return array();
  303. }
  304. /**
  305. * Get Currency Code for Custom Value
  306. *
  307. * @return string
  308. */
  309. public function getCustomValueCurrencyCode()
  310. {
  311. $orderInfo = $this->getShipment()->getOrder();
  312. return $orderInfo->getBaseCurrency()->getCurrencyCode();
  313. }
  314. /**
  315. * Display formatted price
  316. *
  317. * @param float $price
  318. * @return string
  319. */
  320. public function displayPrice($price)
  321. {
  322. return $this->getShipment()->getOrder()->formatPriceTxt($price);
  323. }
  324. /**
  325. * Display formatted customs price
  326. *
  327. * @param float $price
  328. * @return string
  329. */
  330. public function displayCustomsPrice($price)
  331. {
  332. $orderInfo = $this->getShipment()->getOrder();
  333. return $orderInfo->getBaseCurrency()->formatTxt($price);
  334. }
  335. /**
  336. * Get ordered qty of item
  337. *
  338. * @param int $itemId
  339. * @return int|null
  340. */
  341. public function getQtyOrderedItem($itemId)
  342. {
  343. if ($itemId) {
  344. return $this->getShipment()->getOrder()->getItemById($itemId)->getQtyOrdered()*1;
  345. } else {
  346. return;
  347. }
  348. }
  349. }