123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Sales\Block\Adminhtml\Items;
- use Magento\Sales\Model\Order;
- use Magento\Sales\Model\Order\Creditmemo\Item;
- /**
- * Abstract items renderer
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- * @api
- * @since 100.0.2
- */
- class AbstractItems extends \Magento\Backend\Block\Template
- {
- /**
- * Block alias fallback
- */
- const DEFAULT_TYPE = 'default';
- /**
- * Renderers for other column with column name key
- * block => the block name
- * template => the template file
- * renderer => the block object
- *
- * @var array
- */
- protected $_columnRenders = [];
- /**
- * Flag - if it is set method canEditQty will return value of it
- *
- * @var bool|null
- */
- protected $_canEditQty;
- /**
- * Core registry
- *
- * @var \Magento\Framework\Registry
- */
- protected $_coreRegistry;
- /**
- * @var \Magento\CatalogInventory\Api\StockRegistryInterface
- */
- protected $stockRegistry;
- /**
- * @var \Magento\CatalogInventory\Api\StockConfigurationInterface
- */
- protected $stockConfiguration;
- /**
- * @param \Magento\Backend\Block\Template\Context $context
- * @param \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
- * @param \Magento\CatalogInventory\Api\StockConfigurationInterface $stockConfiguration
- * @param \Magento\Framework\Registry $registry
- * @param array $data
- */
- public function __construct(
- \Magento\Backend\Block\Template\Context $context,
- \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry,
- \Magento\CatalogInventory\Api\StockConfigurationInterface $stockConfiguration,
- \Magento\Framework\Registry $registry,
- array $data = []
- ) {
- $this->stockRegistry = $stockRegistry;
- $this->stockConfiguration = $stockConfiguration;
- $this->_coreRegistry = $registry;
- parent::__construct($context, $data);
- }
- /**
- * Add column renderers
- *
- * @param array $blocks
- * @return $this
- */
- public function setColumnRenders(array $blocks)
- {
- foreach ($blocks as $blockName) {
- $block = $this->getLayout()->getBlock($blockName);
- if ($block->getRenderedBlock() === null) {
- $block->setRenderedBlock($this);
- }
- $this->_columnRenders[$blockName] = $block;
- }
- return $this;
- }
- /**
- * Retrieve item renderer block
- *
- * @param string $type
- * @return \Magento\Framework\View\Element\AbstractBlock
- * @throws \RuntimeException
- */
- public function getItemRenderer($type)
- {
- /** @var $renderer \Magento\Sales\Block\Adminhtml\Items\AbstractItems */
- $renderer = $this->getChildBlock($type) ?: $this->getChildBlock(self::DEFAULT_TYPE);
- if (!$renderer instanceof \Magento\Framework\View\Element\BlockInterface) {
- throw new \RuntimeException('Renderer for type "' . $type . '" does not exist.');
- }
- $renderer->setColumnRenders($this->getLayout()->getGroupChildNames($this->getNameInLayout(), 'column'));
- return $renderer;
- }
- /**
- * Retrieve column renderer block
- *
- * @param string $column
- * @param string $compositePart
- * @return \Magento\Framework\View\Element\AbstractBlock
- */
- public function getColumnRenderer($column, $compositePart = '')
- {
- $column = 'column_' . $column;
- if (isset($this->_columnRenders[$column . '_' . $compositePart])) {
- $column .= '_' . $compositePart;
- }
- if (!isset($this->_columnRenders[$column])) {
- return false;
- }
- return $this->_columnRenders[$column];
- }
- /**
- * Retrieve rendered item html content
- *
- * @param \Magento\Framework\DataObject $item
- * @return string
- */
- public function getItemHtml(\Magento\Framework\DataObject $item)
- {
- if ($item->getOrderItem()) {
- $type = $item->getOrderItem()->getProductType();
- } else {
- $type = $item->getProductType();
- }
- return $this->getItemRenderer($type)->setItem($item)->setCanEditQty($this->canEditQty())->toHtml();
- }
- /**
- * Retrieve rendered item extra info html content
- *
- * @param \Magento\Framework\DataObject $item
- * @return string
- */
- public function getItemExtraInfoHtml(\Magento\Framework\DataObject $item)
- {
- $extraInfoBlock = $this->getChildBlock('order_item_extra_info');
- if ($extraInfoBlock) {
- return $extraInfoBlock->setItem($item)->toHtml();
- }
- return '';
- }
- /**
- * Retrieve rendered column html content
- *
- * @param \Magento\Framework\DataObject $item
- * @param string $column the column key
- * @param string $field the custom item field
- * @return string
- */
- public function getColumnHtml(\Magento\Framework\DataObject $item, $column, $field = null)
- {
- if ($item->getOrderItem()) {
- $block = $this->getColumnRenderer($column, $item->getOrderItem()->getProductType());
- } else {
- $block = $this->getColumnRenderer($column, $item->getProductType());
- }
- if ($block) {
- $block->setItem($item);
- if ($field !== null) {
- $block->setField($field);
- }
- return $block->toHtml();
- }
- return ' ';
- }
- /**
- * Get credit memo
- *
- * @return mixed
- */
- public function getCreditmemo()
- {
- return $this->_coreRegistry->registry('current_creditmemo');
- }
- /**
- * ######################### SALES ##################################
- */
- /**
- * Retrieve available order
- *
- * @throws \Magento\Framework\Exception\LocalizedException
- * @return Order
- */
- public function getOrder()
- {
- if ($this->hasOrder()) {
- return $this->getData('order');
- }
- if ($this->_coreRegistry->registry('current_order')) {
- return $this->_coreRegistry->registry('current_order');
- }
- if ($this->_coreRegistry->registry('order')) {
- return $this->_coreRegistry->registry('order');
- }
- if ($this->getInvoice()) {
- return $this->getInvoice()->getOrder();
- }
- if ($this->getCreditmemo()) {
- return $this->getCreditmemo()->getOrder();
- }
- if ($this->getItem()->getOrder()) {
- return $this->getItem()->getOrder();
- }
- throw new \Magento\Framework\Exception\LocalizedException(__('We can\'t get the order instance right now.'));
- }
- /**
- * Retrieve price data object
- *
- * @return Order
- */
- public function getPriceDataObject()
- {
- $obj = $this->getData('price_data_object');
- if ($obj === null) {
- return $this->getOrder();
- }
- return $obj;
- }
- /**
- * Retrieve price attribute html content
- *
- * @param string $code
- * @param bool $strong
- * @param string $separator
- * @return string
- */
- public function displayPriceAttribute($code, $strong = false, $separator = '<br />')
- {
- if ($code == 'tax_amount' && $this->getOrder()->getRowTaxDisplayPrecision()) {
- return $this->displayRoundedPrices(
- $this->getPriceDataObject()->getData('base_' . $code),
- $this->getPriceDataObject()->getData($code),
- $this->getOrder()->getRowTaxDisplayPrecision(),
- $strong,
- $separator
- );
- } else {
- return $this->displayPrices(
- $this->getPriceDataObject()->getData('base_' . $code),
- $this->getPriceDataObject()->getData($code),
- $strong,
- $separator
- );
- }
- }
- /**
- * Retrieve price formatted html content
- *
- * @param float $basePrice
- * @param float $price
- * @param bool $strong
- * @param string $separator
- * @return string
- */
- public function displayPrices($basePrice, $price, $strong = false, $separator = '<br />')
- {
- return $this->displayRoundedPrices($basePrice, $price, 2, $strong, $separator);
- }
- /**
- * Display base and regular prices with specified rounding precision
- *
- * @param float $basePrice
- * @param float $price
- * @param int $precision
- * @param bool $strong
- * @param string $separator
- * @return string
- */
- public function displayRoundedPrices($basePrice, $price, $precision = 2, $strong = false, $separator = '<br />')
- {
- if ($this->getOrder()->isCurrencyDifferent()) {
- $res = '';
- $res .= $this->getOrder()->formatBasePricePrecision($basePrice, $precision);
- $res .= $separator;
- $res .= $this->getOrder()->formatPricePrecision($price, $precision, true);
- } else {
- $res = $this->getOrder()->formatPricePrecision($price, $precision);
- if ($strong) {
- $res = '<strong>' . $res . '</strong>';
- }
- }
- return $res;
- }
- /**
- * Retrieve tax calculation html content
- *
- * @param \Magento\Framework\DataObject $item
- * @return string
- */
- public function displayTaxCalculation(\Magento\Framework\DataObject $item)
- {
- if ($item->getTaxPercent() && $item->getTaxString() == '') {
- $percents = [$item->getTaxPercent()];
- } elseif ($item->getTaxString()) {
- $percents = explode(\Magento\Tax\Model\Config::CALCULATION_STRING_SEPARATOR, $item->getTaxString());
- } else {
- return '0%';
- }
- foreach ($percents as &$percent) {
- $percent = sprintf('%.2f%%', $percent);
- }
- return implode(' + ', $percents);
- }
- /**
- * Retrieve tax with percent html content
- *
- * @param \Magento\Framework\DataObject $item
- * @return string
- */
- public function displayTaxPercent(\Magento\Framework\DataObject $item)
- {
- if ($item->getTaxPercent()) {
- return sprintf('%s%%', $item->getTaxPercent() + 0);
- } else {
- return '0%';
- }
- }
- /**
- * INVOICES
- */
- /**
- * Check shipment availability for current invoice
- *
- * @return bool
- */
- public function canCreateShipment()
- {
- foreach ($this->getInvoice()->getAllItems() as $item) {
- if ($item->getOrderItem()->getQtyToShip()) {
- return true;
- }
- }
- return false;
- }
- /**
- * Setter for flag _canEditQty
- *
- * @param bool $value
- * @return $this
- * @see self::_canEditQty
- * @see self::canEditQty
- */
- public function setCanEditQty($value)
- {
- $this->_canEditQty = $value;
- return $this;
- }
- /**
- * Check availability to edit quantity of item
- *
- * @return bool
- */
- public function canEditQty()
- {
- /**
- * If parent block has set
- */
- if ($this->_canEditQty !== null) {
- return $this->_canEditQty;
- }
- /**
- * Disable editing of quantity of item if creating of shipment forced
- * and ship partially disabled for order
- */
- if ($this->getOrder()->getForcedShipmentWithInvoice()
- && ($this->canShipPartially($this->getOrder()) || $this->canShipPartiallyItem($this->getOrder()))
- ) {
- return false;
- }
- if ($this->getOrder()->getPayment()->canCapture()) {
- return $this->getOrder()->getPayment()->canCapturePartial();
- }
- return true;
- }
- /**
- * Check capture availability
- *
- * @return bool
- */
- public function canCapture()
- {
- if ($this->_authorization->isAllowed('Magento_Sales::capture')) {
- return $this->getInvoice()->canCapture();
- }
- return false;
- }
- /**
- * Retrieve formatted price
- *
- * @param float $price
- * @return string
- */
- public function formatPrice($price)
- {
- return $this->getOrder()->formatPrice($price);
- }
- /**
- * Retrieve source
- *
- * @return \Magento\Sales\Model\Order\Invoice
- */
- public function getSource()
- {
- return $this->getInvoice();
- }
- /**
- * Retrieve invoice model instance
- *
- * @return \Magento\Sales\Model\Order\Invoice
- */
- public function getInvoice()
- {
- return $this->_coreRegistry->registry('current_invoice');
- }
- /**
- * @param \Magento\Store\Model\Store $store
- * @return bool
- */
- public function canReturnToStock($store = null)
- {
- return $this->stockConfiguration->canSubtractQty($store);
- }
- /**
- * Whether to show 'Return to stock' checkbox for item
- *
- * @param Item $item
- * @return bool
- */
- public function canReturnItemToStock($item = null)
- {
- if (null !== $item) {
- if (!$item->hasCanReturnToStock()) {
- $stockItem = $this->stockRegistry->getStockItem(
- $item->getOrderItem()->getProductId(),
- $item->getOrderItem()->getStore()->getWebsiteId()
- );
- $item->setCanReturnToStock($stockItem->getManageStock());
- }
- return $item->getCanReturnToStock();
- }
- return $this->canReturnToStock();
- }
- /**
- * Whether to show 'Return to stock' column for item parent
- *
- * @param Item $item
- * @return bool
- */
- public function canParentReturnToStock($item = null)
- {
- if ($item !== null) {
- if ($item->getCreditmemo()->getOrder()->hasCanReturnToStock()) {
- return $item->getCreditmemo()->getOrder()->getCanReturnToStock();
- }
- } elseif ($this->getOrder()->hasCanReturnToStock()) {
- return $this->getOrder()->getCanReturnToStock();
- }
- return $this->canReturnToStock();
- }
- /**
- * Return true if can ship partially
- *
- * @param Order|null $order
- * @return bool
- */
- public function canShipPartially($order = null)
- {
- if ($order === null || !$order instanceof Order) {
- $order = $this->_coreRegistry->registry('current_shipment')->getOrder();
- }
- $value = $order->getCanShipPartially();
- if ($value !== null && !$value) {
- return false;
- }
- return true;
- }
- /**
- * Return true if can ship items partially
- *
- * @param Order|null $order
- * @return bool
- */
- public function canShipPartiallyItem($order = null)
- {
- if ($order === null || !$order instanceof Order) {
- $order = $this->_coreRegistry->registry('current_shipment')->getOrder();
- }
- $value = $order->getCanShipPartiallyItem();
- if ($value !== null && !$value) {
- return false;
- }
- return true;
- }
- /**
- * Check is shipment is regular
- *
- * @return bool
- */
- public function isShipmentRegular()
- {
- if (!$this->canShipPartiallyItem() || !$this->canShipPartially()) {
- return false;
- }
- return true;
- }
- }
|