Packaging.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Shipping\Block\Adminhtml\Order;
  7. /**
  8. * Adminhtml shipment packaging
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Packaging extends \Magento\Backend\Block\Template
  14. {
  15. /**
  16. * Source size model
  17. *
  18. * @var \Magento\Shipping\Model\Carrier\Source\GenericInterface
  19. */
  20. protected $_sourceSizeModel;
  21. /**
  22. * Core registry
  23. *
  24. * @var \Magento\Framework\Registry
  25. */
  26. protected $_coreRegistry = null;
  27. /**
  28. * @var \Magento\Framework\Json\EncoderInterface
  29. */
  30. protected $_jsonEncoder;
  31. /**
  32. * @var \Magento\Shipping\Model\CarrierFactory
  33. */
  34. protected $_carrierFactory;
  35. /**
  36. * @param \Magento\Backend\Block\Template\Context $context
  37. * @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
  38. * @param \Magento\Shipping\Model\Carrier\Source\GenericInterface $sourceSizeModel
  39. * @param \Magento\Framework\Registry $coreRegistry
  40. * @param \Magento\Shipping\Model\CarrierFactory $carrierFactory
  41. * @param array $data
  42. */
  43. public function __construct(
  44. \Magento\Backend\Block\Template\Context $context,
  45. \Magento\Framework\Json\EncoderInterface $jsonEncoder,
  46. \Magento\Shipping\Model\Carrier\Source\GenericInterface $sourceSizeModel,
  47. \Magento\Framework\Registry $coreRegistry,
  48. \Magento\Shipping\Model\CarrierFactory $carrierFactory,
  49. array $data = []
  50. ) {
  51. $this->_jsonEncoder = $jsonEncoder;
  52. $this->_coreRegistry = $coreRegistry;
  53. $this->_sourceSizeModel = $sourceSizeModel;
  54. $this->_carrierFactory = $carrierFactory;
  55. parent::__construct($context, $data);
  56. }
  57. /**
  58. * Retrieve shipment model instance
  59. *
  60. * @return \Magento\Sales\Model\Order\Shipment
  61. */
  62. public function getShipment()
  63. {
  64. return $this->_coreRegistry->registry('current_shipment');
  65. }
  66. /**
  67. * Configuration for popup window for packaging
  68. *
  69. * @return string
  70. * @SuppressWarnings(PHPMD.RequestAwareBlockMethod)
  71. */
  72. public function getConfigDataJson()
  73. {
  74. $shipmentId = $this->getShipment()->getId();
  75. $orderId = $this->getRequest()->getParam('order_id');
  76. $urlParams = [];
  77. $itemsQty = [];
  78. $itemsPrice = [];
  79. $itemsName = [];
  80. $itemsWeight = [];
  81. $itemsProductId = [];
  82. $itemsOrderItemId = [];
  83. if ($shipmentId) {
  84. $urlParams['shipment_id'] = $shipmentId;
  85. $createLabelUrl = $this->getUrl('adminhtml/order_shipment/createLabel', $urlParams);
  86. $itemsGridUrl = $this->getUrl('adminhtml/order_shipment/getShippingItemsGrid', $urlParams);
  87. foreach ($this->getShipment()->getAllItems() as $item) {
  88. $itemsQty[$item->getId()] = $item->getQty();
  89. $itemsPrice[$item->getId()] = $item->getPrice();
  90. $itemsName[$item->getId()] = $item->getName();
  91. $itemsWeight[$item->getId()] = $item->getWeight();
  92. $itemsProductId[$item->getId()] = $item->getProductId();
  93. $itemsOrderItemId[$item->getId()] = $item->getOrderItemId();
  94. }
  95. } else {
  96. if ($orderId) {
  97. $urlParams['order_id'] = $orderId;
  98. $createLabelUrl = $this->getUrl('adminhtml/order_shipment/save', $urlParams);
  99. $itemsGridUrl = $this->getUrl('adminhtml/order_shipment/getShippingItemsGrid', $urlParams);
  100. foreach ($this->getShipment()->getAllItems() as $item) {
  101. $itemsQty[$item->getOrderItemId()] = $item->getQty() * 1;
  102. $itemsPrice[$item->getOrderItemId()] = $item->getPrice();
  103. $itemsName[$item->getOrderItemId()] = $item->getName();
  104. $itemsWeight[$item->getOrderItemId()] = $item->getWeight();
  105. $itemsProductId[$item->getOrderItemId()] = $item->getProductId();
  106. $itemsOrderItemId[$item->getOrderItemId()] = $item->getOrderItemId();
  107. }
  108. }
  109. }
  110. $data = [
  111. 'createLabelUrl' => $createLabelUrl,
  112. 'itemsGridUrl' => $itemsGridUrl,
  113. 'errorQtyOverLimit' => __(
  114. 'You are trying to add a quantity for some products that doesn\'t match the quantity that was shipped.'
  115. ),
  116. 'titleDisabledSaveBtn' => __('Products should be added to package(s)'),
  117. 'validationErrorMsg' => __('The value that you entered is not valid.'),
  118. 'shipmentItemsQty' => $itemsQty,
  119. 'shipmentItemsPrice' => $itemsPrice,
  120. 'shipmentItemsName' => $itemsName,
  121. 'shipmentItemsWeight' => $itemsWeight,
  122. 'shipmentItemsProductId' => $itemsProductId,
  123. 'shipmentItemsOrderItemId' => $itemsOrderItemId,
  124. 'customizable' => $this->_getCustomizableContainers(),
  125. ];
  126. return $this->_jsonEncoder->encode($data);
  127. }
  128. /**
  129. * Return container types of carrier
  130. *
  131. * @return array
  132. */
  133. public function getContainers()
  134. {
  135. $order = $this->getShipment()->getOrder();
  136. $storeId = $this->getShipment()->getStoreId();
  137. $address = $order->getShippingAddress();
  138. $carrier = $this->_carrierFactory->create($order->getShippingMethod(true)->getCarrierCode());
  139. $countryShipper = $this->_scopeConfig->getValue(
  140. \Magento\Sales\Model\Order\Shipment::XML_PATH_STORE_COUNTRY_ID,
  141. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  142. $storeId
  143. );
  144. if ($carrier) {
  145. $params = new \Magento\Framework\DataObject(
  146. [
  147. 'method' => $order->getShippingMethod(true)->getMethod(),
  148. 'country_shipper' => $countryShipper,
  149. 'country_recipient' => $address->getCountryId(),
  150. ]
  151. );
  152. return $carrier->getContainerTypes($params);
  153. }
  154. return [];
  155. }
  156. /**
  157. * Get codes of customizable container types of carrier
  158. *
  159. * @return array
  160. */
  161. protected function _getCustomizableContainers()
  162. {
  163. $order = $this->getShipment()->getOrder();
  164. $carrier = $this->_carrierFactory->create($order->getShippingMethod(true)->getCarrierCode());
  165. if ($carrier) {
  166. return $carrier->getCustomizableContainerTypes();
  167. }
  168. return [];
  169. }
  170. /**
  171. * Return name of container type by its code
  172. *
  173. * @param string $code
  174. * @return string
  175. */
  176. public function getContainerTypeByCode($code)
  177. {
  178. $order = $this->getShipment()->getOrder();
  179. $carrier = $this->_carrierFactory->create($order->getShippingMethod(true)->getCarrierCode());
  180. if ($carrier) {
  181. $containerTypes = $carrier->getContainerTypes();
  182. $containerType = !empty($containerTypes[$code]) ? $containerTypes[$code] : '';
  183. return $containerType;
  184. }
  185. return '';
  186. }
  187. /**
  188. * Return name of delivery confirmation type by its code
  189. *
  190. * @param string $code
  191. * @return string
  192. */
  193. public function getDeliveryConfirmationTypeByCode($code)
  194. {
  195. $countryId = $this->getShipment()->getOrder()->getShippingAddress()->getCountryId();
  196. $order = $this->getShipment()->getOrder();
  197. $carrier = $this->_carrierFactory->create($order->getShippingMethod(true)->getCarrierCode());
  198. if ($carrier) {
  199. $params = new \Magento\Framework\DataObject(['country_recipient' => $countryId]);
  200. $confirmationTypes = $carrier->getDeliveryConfirmationTypes($params);
  201. $confirmationType = !empty($confirmationTypes[$code]) ? $confirmationTypes[$code] : '';
  202. return $confirmationType;
  203. }
  204. return '';
  205. }
  206. /**
  207. * Return name of content type by its code
  208. *
  209. * @param string $code
  210. * @return string
  211. */
  212. public function getContentTypeByCode($code)
  213. {
  214. $contentTypes = $this->getContentTypes();
  215. if (!empty($contentTypes[$code])) {
  216. return $contentTypes[$code];
  217. }
  218. return '';
  219. }
  220. /**
  221. * Get packed products in packages
  222. *
  223. * @return array
  224. */
  225. public function getPackages()
  226. {
  227. return $this->getShipment()->getPackages();
  228. }
  229. /**
  230. * Get item of shipment by its id
  231. *
  232. * @param string $itemId
  233. * @param string $itemsOf
  234. * @return \Magento\Framework\DataObject
  235. */
  236. public function getShipmentItem($itemId, $itemsOf)
  237. {
  238. $items = $this->getShipment()->getAllItems();
  239. foreach ($items as $item) {
  240. if ($itemsOf == 'order' && $item->getOrderItemId() == $itemId) {
  241. return $item;
  242. } else {
  243. if ($itemsOf == 'shipment' && $item->getId() == $itemId) {
  244. return $item;
  245. }
  246. }
  247. }
  248. return new \Magento\Framework\DataObject();
  249. }
  250. /**
  251. * Can display customs value
  252. *
  253. * @return bool
  254. */
  255. public function displayCustomsValue()
  256. {
  257. $storeId = $this->getShipment()->getStoreId();
  258. $order = $this->getShipment()->getOrder();
  259. $address = $order->getShippingAddress();
  260. $shipperAddressCountryCode = $this->_scopeConfig->getValue(
  261. \Magento\Sales\Model\Order\Shipment::XML_PATH_STORE_COUNTRY_ID,
  262. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  263. $storeId
  264. );
  265. $recipientAddressCountryCode = $address->getCountryId();
  266. if ($shipperAddressCountryCode != $recipientAddressCountryCode) {
  267. return true;
  268. }
  269. return false;
  270. }
  271. /**
  272. * Return delivery confirmation types of current carrier
  273. *
  274. * @return array
  275. */
  276. public function getDeliveryConfirmationTypes()
  277. {
  278. $countryId = $this->getShipment()->getOrder()->getShippingAddress()->getCountryId();
  279. $order = $this->getShipment()->getOrder();
  280. $carrier = $this->_carrierFactory->create($order->getShippingMethod(true)->getCarrierCode());
  281. $params = new \Magento\Framework\DataObject(['country_recipient' => $countryId]);
  282. if ($carrier && is_array($carrier->getDeliveryConfirmationTypes($params))) {
  283. return $carrier->getDeliveryConfirmationTypes($params);
  284. }
  285. return [];
  286. }
  287. /**
  288. * Print button for creating pdf
  289. *
  290. * @return string
  291. */
  292. public function getPrintButton()
  293. {
  294. $data['shipment_id'] = $this->getShipment()->getId();
  295. return $this->getUrl('adminhtml/order_shipment/printPackage', $data);
  296. }
  297. /**
  298. * Check whether girth is allowed for current carrier
  299. *
  300. * @return bool
  301. */
  302. public function isGirthAllowed()
  303. {
  304. $order = $this->getShipment()->getOrder();
  305. $carrier = $this->_carrierFactory->create($order->getShippingMethod(true)->getCarrierCode());
  306. return $carrier->isGirthAllowed($this->getShipment()->getOrder()->getShippingAddress()->getCountryId());
  307. }
  308. /**
  309. * Is display girth value
  310. *
  311. * @return bool
  312. */
  313. public function isDisplayGirthValue()
  314. {
  315. return false;
  316. }
  317. /**
  318. * Return content types of package
  319. *
  320. * @return array
  321. */
  322. public function getContentTypes()
  323. {
  324. $order = $this->getShipment()->getOrder();
  325. $storeId = $this->getShipment()->getStoreId();
  326. $address = $order->getShippingAddress();
  327. $carrier = $this->_carrierFactory->create($order->getShippingMethod(true)->getCarrierCode());
  328. $countryShipper = $this->_scopeConfig->getValue(
  329. \Magento\Sales\Model\Order\Shipment::XML_PATH_STORE_COUNTRY_ID,
  330. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  331. $storeId
  332. );
  333. if ($carrier) {
  334. $params = new \Magento\Framework\DataObject(
  335. [
  336. 'method' => $order->getShippingMethod(true)->getMethod(),
  337. 'country_shipper' => $countryShipper,
  338. 'country_recipient' => $address->getCountryId(),
  339. ]
  340. );
  341. return $carrier->getContentTypes($params);
  342. }
  343. return [];
  344. }
  345. /**
  346. * Get Currency Code for Custom Value
  347. *
  348. * @return string
  349. */
  350. public function getCustomValueCurrencyCode()
  351. {
  352. $orderInfo = $this->getShipment()->getOrder();
  353. return $orderInfo->getBaseCurrency()->getCurrencyCode();
  354. }
  355. /**
  356. * Display formatted price
  357. *
  358. * @param float $price
  359. * @return string
  360. */
  361. public function displayPrice($price)
  362. {
  363. return $this->getShipment()->getOrder()->formatPriceTxt($price);
  364. }
  365. /**
  366. * Display formatted customs price
  367. *
  368. * @param float $price
  369. * @return string
  370. */
  371. public function displayCustomsPrice($price)
  372. {
  373. $orderInfo = $this->getShipment()->getOrder();
  374. return $orderInfo->getBaseCurrency()->formatTxt($price);
  375. }
  376. /**
  377. * Get ordered qty of item
  378. *
  379. * @param int $itemId
  380. * @return int|null
  381. */
  382. public function getQtyOrderedItem($itemId)
  383. {
  384. if ($itemId) {
  385. return $this->getShipment()->getOrder()->getItemById($itemId)->getQtyOrdered() * 1;
  386. } else {
  387. return;
  388. }
  389. }
  390. /**
  391. * Get source size model
  392. *
  393. * @return \Magento\Shipping\Model\Carrier\Source\GenericInterface
  394. */
  395. public function getSourceSizeModel()
  396. {
  397. return $this->_sourceSizeModel;
  398. }
  399. }