Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractItems.php
Go to the documentation of this file.
1 <?php
7 
10 
18 {
22  const DEFAULT_TYPE = 'default';
23 
32  protected $_columnRenders = [];
33 
39  protected $_canEditQty;
40 
46  protected $_coreRegistry;
47 
51  protected $stockRegistry;
52 
57 
65  public function __construct(
66  \Magento\Backend\Block\Template\Context $context,
67  \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry,
68  \Magento\CatalogInventory\Api\StockConfigurationInterface $stockConfiguration,
69  \Magento\Framework\Registry $registry,
70  array $data = []
71  ) {
72  $this->stockRegistry = $stockRegistry;
73  $this->stockConfiguration = $stockConfiguration;
74  $this->_coreRegistry = $registry;
75  parent::__construct($context, $data);
76  }
77 
84  public function setColumnRenders(array $blocks)
85  {
86  foreach ($blocks as $blockName) {
87  $block = $this->getLayout()->getBlock($blockName);
88  if ($block->getRenderedBlock() === null) {
89  $block->setRenderedBlock($this);
90  }
91  $this->_columnRenders[$blockName] = $block;
92  }
93  return $this;
94  }
95 
103  public function getItemRenderer($type)
104  {
106  $renderer = $this->getChildBlock($type) ?: $this->getChildBlock(self::DEFAULT_TYPE);
107  if (!$renderer instanceof \Magento\Framework\View\Element\BlockInterface) {
108  throw new \RuntimeException('Renderer for type "' . $type . '" does not exist.');
109  }
110  $renderer->setColumnRenders($this->getLayout()->getGroupChildNames($this->getNameInLayout(), 'column'));
111 
112  return $renderer;
113  }
114 
122  public function getColumnRenderer($column, $compositePart = '')
123  {
124  $column = 'column_' . $column;
125  if (isset($this->_columnRenders[$column . '_' . $compositePart])) {
126  $column .= '_' . $compositePart;
127  }
128  if (!isset($this->_columnRenders[$column])) {
129  return false;
130  }
131  return $this->_columnRenders[$column];
132  }
133 
140  public function getItemHtml(\Magento\Framework\DataObject $item)
141  {
142  if ($item->getOrderItem()) {
143  $type = $item->getOrderItem()->getProductType();
144  } else {
145  $type = $item->getProductType();
146  }
147 
148  return $this->getItemRenderer($type)->setItem($item)->setCanEditQty($this->canEditQty())->toHtml();
149  }
150 
157  public function getItemExtraInfoHtml(\Magento\Framework\DataObject $item)
158  {
159  $extraInfoBlock = $this->getChildBlock('order_item_extra_info');
160  if ($extraInfoBlock) {
161  return $extraInfoBlock->setItem($item)->toHtml();
162  }
163  return '';
164  }
165 
174  public function getColumnHtml(\Magento\Framework\DataObject $item, $column, $field = null)
175  {
176  if ($item->getOrderItem()) {
177  $block = $this->getColumnRenderer($column, $item->getOrderItem()->getProductType());
178  } else {
179  $block = $this->getColumnRenderer($column, $item->getProductType());
180  }
181 
182  if ($block) {
183  $block->setItem($item);
184  if ($field !== null) {
185  $block->setField($field);
186  }
187  return $block->toHtml();
188  }
189  return '&nbsp;';
190  }
191 
197  public function getCreditmemo()
198  {
199  return $this->_coreRegistry->registry('current_creditmemo');
200  }
201 
212  public function getOrder()
213  {
214  if ($this->hasOrder()) {
215  return $this->getData('order');
216  }
217  if ($this->_coreRegistry->registry('current_order')) {
218  return $this->_coreRegistry->registry('current_order');
219  }
220  if ($this->_coreRegistry->registry('order')) {
221  return $this->_coreRegistry->registry('order');
222  }
223  if ($this->getInvoice()) {
224  return $this->getInvoice()->getOrder();
225  }
226  if ($this->getCreditmemo()) {
227  return $this->getCreditmemo()->getOrder();
228  }
229  if ($this->getItem()->getOrder()) {
230  return $this->getItem()->getOrder();
231  }
232 
233  throw new \Magento\Framework\Exception\LocalizedException(__('We can\'t get the order instance right now.'));
234  }
235 
241  public function getPriceDataObject()
242  {
243  $obj = $this->getData('price_data_object');
244  if ($obj === null) {
245  return $this->getOrder();
246  }
247  return $obj;
248  }
249 
258  public function displayPriceAttribute($code, $strong = false, $separator = '<br />')
259  {
260  if ($code == 'tax_amount' && $this->getOrder()->getRowTaxDisplayPrecision()) {
261  return $this->displayRoundedPrices(
262  $this->getPriceDataObject()->getData('base_' . $code),
263  $this->getPriceDataObject()->getData($code),
264  $this->getOrder()->getRowTaxDisplayPrecision(),
265  $strong,
266  $separator
267  );
268  } else {
269  return $this->displayPrices(
270  $this->getPriceDataObject()->getData('base_' . $code),
271  $this->getPriceDataObject()->getData($code),
272  $strong,
273  $separator
274  );
275  }
276  }
277 
287  public function displayPrices($basePrice, $price, $strong = false, $separator = '<br />')
288  {
289  return $this->displayRoundedPrices($basePrice, $price, 2, $strong, $separator);
290  }
291 
302  public function displayRoundedPrices($basePrice, $price, $precision = 2, $strong = false, $separator = '<br />')
303  {
304  if ($this->getOrder()->isCurrencyDifferent()) {
305  $res = '';
306  $res .= $this->getOrder()->formatBasePricePrecision($basePrice, $precision);
307  $res .= $separator;
308  $res .= $this->getOrder()->formatPricePrecision($price, $precision, true);
309  } else {
310  $res = $this->getOrder()->formatPricePrecision($price, $precision);
311  if ($strong) {
312  $res = '<strong>' . $res . '</strong>';
313  }
314  }
315  return $res;
316  }
317 
324  public function displayTaxCalculation(\Magento\Framework\DataObject $item)
325  {
326  if ($item->getTaxPercent() && $item->getTaxString() == '') {
327  $percents = [$item->getTaxPercent()];
328  } elseif ($item->getTaxString()) {
329  $percents = explode(\Magento\Tax\Model\Config::CALCULATION_STRING_SEPARATOR, $item->getTaxString());
330  } else {
331  return '0%';
332  }
333 
334  foreach ($percents as &$percent) {
335  $percent = sprintf('%.2f%%', $percent);
336  }
337  return implode(' + ', $percents);
338  }
339 
346  public function displayTaxPercent(\Magento\Framework\DataObject $item)
347  {
348  if ($item->getTaxPercent()) {
349  return sprintf('%s%%', $item->getTaxPercent() + 0);
350  } else {
351  return '0%';
352  }
353  }
354 
364  public function canCreateShipment()
365  {
366  foreach ($this->getInvoice()->getAllItems() as $item) {
367  if ($item->getOrderItem()->getQtyToShip()) {
368  return true;
369  }
370  }
371  return false;
372  }
373 
382  public function setCanEditQty($value)
383  {
384  $this->_canEditQty = $value;
385  return $this;
386  }
387 
393  public function canEditQty()
394  {
398  if ($this->_canEditQty !== null) {
399  return $this->_canEditQty;
400  }
401 
406  if ($this->getOrder()->getForcedShipmentWithInvoice()
407  && ($this->canShipPartially($this->getOrder()) || $this->canShipPartiallyItem($this->getOrder()))
408  ) {
409  return false;
410  }
411  if ($this->getOrder()->getPayment()->canCapture()) {
412  return $this->getOrder()->getPayment()->canCapturePartial();
413  }
414  return true;
415  }
416 
422  public function canCapture()
423  {
424  if ($this->_authorization->isAllowed('Magento_Sales::capture')) {
425  return $this->getInvoice()->canCapture();
426  }
427  return false;
428  }
429 
436  public function formatPrice($price)
437  {
438  return $this->getOrder()->formatPrice($price);
439  }
440 
446  public function getSource()
447  {
448  return $this->getInvoice();
449  }
450 
456  public function getInvoice()
457  {
458  return $this->_coreRegistry->registry('current_invoice');
459  }
460 
465  public function canReturnToStock($store = null)
466  {
467  return $this->stockConfiguration->canSubtractQty($store);
468  }
469 
476  public function canReturnItemToStock($item = null)
477  {
478  if (null !== $item) {
479  if (!$item->hasCanReturnToStock()) {
480  $stockItem = $this->stockRegistry->getStockItem(
481  $item->getOrderItem()->getProductId(),
482  $item->getOrderItem()->getStore()->getWebsiteId()
483  );
484  $item->setCanReturnToStock($stockItem->getManageStock());
485  }
486  return $item->getCanReturnToStock();
487  }
488 
489  return $this->canReturnToStock();
490  }
491 
498  public function canParentReturnToStock($item = null)
499  {
500  if ($item !== null) {
501  if ($item->getCreditmemo()->getOrder()->hasCanReturnToStock()) {
502  return $item->getCreditmemo()->getOrder()->getCanReturnToStock();
503  }
504  } elseif ($this->getOrder()->hasCanReturnToStock()) {
505  return $this->getOrder()->getCanReturnToStock();
506  }
507  return $this->canReturnToStock();
508  }
509 
516  public function canShipPartially($order = null)
517  {
518  if ($order === null || !$order instanceof Order) {
519  $order = $this->_coreRegistry->registry('current_shipment')->getOrder();
520  }
521  $value = $order->getCanShipPartially();
522  if ($value !== null && !$value) {
523  return false;
524  }
525  return true;
526  }
527 
534  public function canShipPartiallyItem($order = null)
535  {
536  if ($order === null || !$order instanceof Order) {
537  $order = $this->_coreRegistry->registry('current_shipment')->getOrder();
538  }
539  $value = $order->getCanShipPartiallyItem();
540  if ($value !== null && !$value) {
541  return false;
542  }
543  return true;
544  }
545 
551  public function isShipmentRegular()
552  {
553  if (!$this->canShipPartiallyItem() || !$this->canShipPartially()) {
554  return false;
555  }
556  return true;
557  }
558 }
displayPrices($basePrice, $price, $strong=false, $separator='< br/>')
getItemHtml(\Magento\Framework\DataObject $item)
if($this->helper('Magento\Tax\Helper\Data') ->displayFullSummary()) foreach( $block->getTotal() ->getFullInfo() as $info)(isset($info['hidden']) && $info['hidden']) $percent
Definition: tax.phtml:33
getData($key='', $index=null)
Definition: DataObject.php:119
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
displayTaxPercent(\Magento\Framework\DataObject $item)
$order
Definition: order.php:55
__()
Definition: __.php:13
$price
$block
Definition: block.php:8
$type
Definition: item.phtml:13
displayPriceAttribute($code, $strong=false, $separator='< br/>')
$value
Definition: gender.phtml:16
getItemExtraInfoHtml(\Magento\Framework\DataObject $item)
getColumnHtml(\Magento\Framework\DataObject $item, $column, $field=null)
displayTaxCalculation(\Magento\Framework\DataObject $item)
displayRoundedPrices($basePrice, $price, $precision=2, $strong=false, $separator='< br/>')
__construct(\Magento\Backend\Block\Template\Context $context, \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry, \Magento\CatalogInventory\Api\StockConfigurationInterface $stockConfiguration, \Magento\Framework\Registry $registry, array $data=[])
$code
Definition: info.phtml:12