Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Packaging.php
Go to the documentation of this file.
1 <?php
7 
15 {
21  protected $_sourceSizeModel;
22 
28  protected $_coreRegistry = null;
29 
33  protected $_jsonEncoder;
34 
38  protected $_carrierFactory;
39 
48  public function __construct(
49  \Magento\Backend\Block\Template\Context $context,
50  \Magento\Framework\Json\EncoderInterface $jsonEncoder,
51  \Magento\Shipping\Model\Carrier\Source\GenericInterface $sourceSizeModel,
52  \Magento\Framework\Registry $coreRegistry,
53  \Magento\Shipping\Model\CarrierFactory $carrierFactory,
54  array $data = []
55  ) {
56  $this->_jsonEncoder = $jsonEncoder;
57  $this->_coreRegistry = $coreRegistry;
58  $this->_sourceSizeModel = $sourceSizeModel;
59  $this->_carrierFactory = $carrierFactory;
60  parent::__construct($context, $data);
61  }
62 
68  public function getShipment()
69  {
70  return $this->_coreRegistry->registry('current_shipment');
71  }
72 
78  public function getConfigDataJson()
79  {
80  $shipmentId = $this->getShipment()->getId();
81  $orderId = $this->getRequest()->getParam('order_id');
82  $urlParams = [];
83 
84  $itemsQty = [];
85  $itemsPrice = [];
86  $itemsName = [];
87  $itemsWeight = [];
88  $itemsProductId = [];
89 
90  if ($shipmentId) {
91  $urlParams['shipment_id'] = $shipmentId;
92  $createLabelUrl = $this->getUrl('adminhtml/order_shipment/createLabel', $urlParams);
93  $itemsGridUrl = $this->getUrl('adminhtml/order_shipment/getShippingItemsGrid', $urlParams);
94  foreach ($this->getShipment()->getAllItems() as $item) {
95  $itemsQty[$item->getId()] = $item->getQty();
96  $itemsPrice[$item->getId()] = $item->getPrice();
97  $itemsName[$item->getId()] = $item->getName();
98  $itemsWeight[$item->getId()] = $item->getWeight();
99  $itemsProductId[$item->getId()] = $item->getProductId();
100  $itemsOrderItemId[$item->getId()] = $item->getOrderItemId();
101  }
102  } else {
103  if ($orderId) {
104  $urlParams['order_id'] = $orderId;
105  $createLabelUrl = $this->getUrl('adminhtml/order_shipment/save', $urlParams);
106  $itemsGridUrl = $this->getUrl('adminhtml/order_shipment/getShippingItemsGrid', $urlParams);
107 
108  foreach ($this->getShipment()->getAllItems() as $item) {
109  $itemsQty[$item->getOrderItemId()] = $item->getQty() * 1;
110  $itemsPrice[$item->getOrderItemId()] = $item->getPrice();
111  $itemsName[$item->getOrderItemId()] = $item->getName();
112  $itemsWeight[$item->getOrderItemId()] = $item->getWeight();
113  $itemsProductId[$item->getOrderItemId()] = $item->getProductId();
114  $itemsOrderItemId[$item->getOrderItemId()] = $item->getOrderItemId();
115  }
116  }
117  }
118  $data = [
119  'createLabelUrl' => $createLabelUrl,
120  'itemsGridUrl' => $itemsGridUrl,
121  'errorQtyOverLimit' => __(
122  'You are trying to add a quantity for some products that doesn\'t match the quantity that was shipped.'
123  ),
124  'titleDisabledSaveBtn' => __('Products should be added to package(s)'),
125  'validationErrorMsg' => __('The value that you entered is not valid.'),
126  'shipmentItemsQty' => $itemsQty,
127  'shipmentItemsPrice' => $itemsPrice,
128  'shipmentItemsName' => $itemsName,
129  'shipmentItemsWeight' => $itemsWeight,
130  'shipmentItemsProductId' => $itemsProductId,
131  'shipmentItemsOrderItemId' => $itemsOrderItemId,
132  'customizable' => $this->_getCustomizableContainers(),
133  ];
134  return $this->_jsonEncoder->encode($data);
135  }
136 
142  public function getContainers()
143  {
144  $order = $this->getShipment()->getOrder();
145  $storeId = $this->getShipment()->getStoreId();
146  $address = $order->getShippingAddress();
147  $carrier = $this->_carrierFactory->create($order->getShippingMethod(true)->getCarrierCode());
148  $countryShipper = $this->_scopeConfig->getValue(
149  \Magento\Sales\Model\Order\Shipment::XML_PATH_STORE_COUNTRY_ID,
150  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
151  $storeId
152  );
153  if ($carrier) {
154  $params = new \Magento\Framework\DataObject(
155  [
156  'method' => $order->getShippingMethod(true)->getMethod(),
157  'country_shipper' => $countryShipper,
158  'country_recipient' => $address->getCountryId(),
159  ]
160  );
161  return $carrier->getContainerTypes($params);
162  }
163  return [];
164  }
165 
171  protected function _getCustomizableContainers()
172  {
173  $order = $this->getShipment()->getOrder();
174  $carrier = $this->_carrierFactory->create($order->getShippingMethod(true)->getCarrierCode());
175  if ($carrier) {
176  return $carrier->getCustomizableContainerTypes();
177  }
178  return [];
179  }
180 
187  public function getContainerTypeByCode($code)
188  {
189  $order = $this->getShipment()->getOrder();
190  $carrier = $this->_carrierFactory->create($order->getShippingMethod(true)->getCarrierCode());
191  if ($carrier) {
192  $containerTypes = $carrier->getContainerTypes();
193  $containerType = !empty($containerTypes[$code]) ? $containerTypes[$code] : '';
194  return $containerType;
195  }
196  return '';
197  }
198 
206  {
207  $countryId = $this->getShipment()->getOrder()->getShippingAddress()->getCountryId();
208  $order = $this->getShipment()->getOrder();
209  $carrier = $this->_carrierFactory->create($order->getShippingMethod(true)->getCarrierCode());
210  if ($carrier) {
211  $params = new \Magento\Framework\DataObject(['country_recipient' => $countryId]);
212  $confirmationTypes = $carrier->getDeliveryConfirmationTypes($params);
213  $confirmationType = !empty($confirmationTypes[$code]) ? $confirmationTypes[$code] : '';
214  return $confirmationType;
215  }
216  return '';
217  }
218 
225  public function getContentTypeByCode($code)
226  {
227  $contentTypes = $this->getContentTypes();
228  if (!empty($contentTypes[$code])) {
229  return $contentTypes[$code];
230  }
231  return '';
232  }
233 
239  public function getPackages()
240  {
241  return $this->getShipment()->getPackages();
242  }
243 
251  public function getShipmentItem($itemId, $itemsOf)
252  {
253  $items = $this->getShipment()->getAllItems();
254  foreach ($items as $item) {
255  if ($itemsOf == 'order' && $item->getOrderItemId() == $itemId) {
256  return $item;
257  } else {
258  if ($itemsOf == 'shipment' && $item->getId() == $itemId) {
259  return $item;
260  }
261  }
262  }
263  return new \Magento\Framework\DataObject();
264  }
265 
271  public function displayCustomsValue()
272  {
273  $storeId = $this->getShipment()->getStoreId();
274  $order = $this->getShipment()->getOrder();
275  $address = $order->getShippingAddress();
276  $shipperAddressCountryCode = $this->_scopeConfig->getValue(
277  \Magento\Sales\Model\Order\Shipment::XML_PATH_STORE_COUNTRY_ID,
278  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
279  $storeId
280  );
281  $recipientAddressCountryCode = $address->getCountryId();
282  if ($shipperAddressCountryCode != $recipientAddressCountryCode) {
283  return true;
284  }
285  return false;
286  }
287 
294  {
295  $countryId = $this->getShipment()->getOrder()->getShippingAddress()->getCountryId();
296  $order = $this->getShipment()->getOrder();
297  $carrier = $this->_carrierFactory->create($order->getShippingMethod(true)->getCarrierCode());
298  $params = new \Magento\Framework\DataObject(['country_recipient' => $countryId]);
299  if ($carrier && is_array($carrier->getDeliveryConfirmationTypes($params))) {
300  return $carrier->getDeliveryConfirmationTypes($params);
301  }
302  return [];
303  }
304 
310  public function getPrintButton()
311  {
312  $data['shipment_id'] = $this->getShipment()->getId();
313  return $this->getUrl('adminhtml/order_shipment/printPackage', $data);
314  }
315 
321  public function isGirthAllowed()
322  {
323  $order = $this->getShipment()->getOrder();
324  $carrier = $this->_carrierFactory->create($order->getShippingMethod(true)->getCarrierCode());
325  return $carrier->isGirthAllowed($this->getShipment()->getOrder()->getShippingAddress()->getCountryId());
326  }
327 
333  public function isDisplayGirthValue()
334  {
335  return false;
336  }
337 
343  public function getContentTypes()
344  {
345  $order = $this->getShipment()->getOrder();
346  $storeId = $this->getShipment()->getStoreId();
347  $address = $order->getShippingAddress();
348  $carrier = $this->_carrierFactory->create($order->getShippingMethod(true)->getCarrierCode());
349  $countryShipper = $this->_scopeConfig->getValue(
350  \Magento\Sales\Model\Order\Shipment::XML_PATH_STORE_COUNTRY_ID,
351  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
352  $storeId
353  );
354  if ($carrier) {
355  $params = new \Magento\Framework\DataObject(
356  [
357  'method' => $order->getShippingMethod(true)->getMethod(),
358  'country_shipper' => $countryShipper,
359  'country_recipient' => $address->getCountryId(),
360  ]
361  );
362  return $carrier->getContentTypes($params);
363  }
364  return [];
365  }
366 
372  public function getCustomValueCurrencyCode()
373  {
374  $orderInfo = $this->getShipment()->getOrder();
375  return $orderInfo->getBaseCurrency()->getCurrencyCode();
376  }
377 
384  public function displayPrice($price)
385  {
386  return $this->getShipment()->getOrder()->formatPriceTxt($price);
387  }
388 
395  public function displayCustomsPrice($price)
396  {
397  $orderInfo = $this->getShipment()->getOrder();
398  return $orderInfo->getBaseCurrency()->formatTxt($price);
399  }
400 
407  public function getQtyOrderedItem($itemId)
408  {
409  if ($itemId) {
410  return $this->getShipment()->getOrder()->getItemById($itemId)->getQtyOrdered() * 1;
411  } else {
412  return;
413  }
414  }
415 
421  public function getSourceSizeModel()
422  {
424  }
425 }
$order
Definition: order.php:55
__()
Definition: __.php:13
$price
$address
Definition: customer.php:38
__construct(\Magento\Backend\Block\Template\Context $context, \Magento\Framework\Json\EncoderInterface $jsonEncoder, \Magento\Shipping\Model\Carrier\Source\GenericInterface $sourceSizeModel, \Magento\Framework\Registry $coreRegistry, \Magento\Shipping\Model\CarrierFactory $carrierFactory, array $data=[])
Definition: Packaging.php:48
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
$code
Definition: info.phtml:12
$items