Skip to content

Commit

Permalink
Merge pull request #315 from magento-engcom/309-add-source-to-order-page
Browse files Browse the repository at this point in the history
Add Source Delivery awareness to Order page
  • Loading branch information
Valeriy Nayda authored Dec 19, 2017
2 parents 8cc2f03 + a4e374a commit 83c345b
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventorySales\Block\Adminhtml\Order\View\Tab;

use Magento\Backend\Block\Template;
use Magento\Backend\Block\Template\Context;
use Magento\Backend\Block\Widget\Tab\TabInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Registry;
use Magento\InventoryApi\Api\Data\SourceItemInterface;
use Magento\InventoryApi\Api\SourceItemRepositoryInterface;
use Magento\InventoryApi\Api\SourceRepositoryInterface;

/**
* Tab for source items display on the order editing page
*
* @api
*/
class Sources extends Template implements TabInterface
{
/**
* @var Registry
*/
private $registry;

/**
* @var SearchCriteriaBuilder
*/
private $searchCriteriaBuilder;

/**
* @var SourceItemRepositoryInterface
*/
private $sourceItemRepository;

/**
* @var SourceRepositoryInterface
*/
private $sourceRepository;

/**
* @param Context $context
* @param Registry $registry
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param SourceItemRepositoryInterface $sourceItemRepository
* @param SourceRepositoryInterface $sourceRepository
* @param array $data
*/
public function __construct(
Context $context,
Registry $registry,
SearchCriteriaBuilder $searchCriteriaBuilder,
SourceItemRepositoryInterface $sourceItemRepository,
SourceRepositoryInterface $sourceRepository,
array $data = []
) {
parent::__construct($context, $data);
$this->registry = $registry;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->sourceItemRepository = $sourceItemRepository;
$this->sourceRepository = $sourceRepository;
}

/**
* Getting source items for order
*
* @return array
*/
public function getSourceItemsData()
{
$order = $this->registry->registry('current_order');

$sourceItemsData = [];
foreach ($order->getAllVisibleItems() as $orderItem) {
$sourceItems = $this->getSourceItemsBySku($orderItem->getSku());

foreach ($sourceItems as $sourceItem) {
$sourceName = $this->sourceRepository->get((int)$sourceItem->getSourceId())->getName();
$sourceItemsData[$sourceName][] = [
'sku' => $sourceItem->getSku(),
'qty' => $sourceItem->getQuantity(),
];
}
}
return $sourceItemsData;
}

/**
* @param string $sku
* @return SourceItemInterface[]
*/
private function getSourceItemsBySku(string $sku): array
{
$searchCriteria = $this->searchCriteriaBuilder
->addFilter(SourceItemInterface::SKU, $sku)
->create();

$sourceItemSearchResult = $this->sourceItemRepository->getList($searchCriteria);
return $sourceItemSearchResult->getItems();
}

/**
* @inheritdoc
*/
public function getTabLabel()
{
return __('Source Delivery');
}

/**
* @inheritdoc
*/
public function getTabTitle()
{
return __('Source Delivery');
}

/**
* @inheritdoc
*/
public function canShowTab()
{
return true;
}

/**
* @inheritdoc
*/
public function isHidden()
{
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="sales_order_tabs">
<block class="Magento\InventorySales\Block\Adminhtml\Order\View\Tab\Sources" name="order_tab_sources" template="Magento_InventorySales::order/view/tab/sources.phtml"/>
<action method="addTab">
<argument name="name" xsi:type="string">order_sources</argument>
<argument name="block" xsi:type="string">order_tab_sources</argument>
</action>
</referenceBlock>
</body>
</page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

// @codingStandardsIgnoreFile

/** @var \Magento\InventorySales\Block\Adminhtml\Order\View\Tab\Sources $block */
?>
<?php foreach ($block->getSourceItemsData() as $sourceName => $sourceItems): ?>
<table class="data-table admin__table-primary edit-order-table">
<div class="admin__page-section-item-title">
<span class="title"><?= $block->escapeHtml($sourceName) ?></span>
</div>
<thead>
<tr class="headings">
<th><span><?= $block->escapeHtml(__('Sku')) ?></span></th>
<th><span><?= $block->escapeHtml(__('Qty')) ?></span></th>
</tr>
</thead>
<?php foreach ($sourceItems as $sourceItem): ?>
<tr>
<td><?= $block->escapeHtml($sourceItem['sku']) ?></td>
<td><?= $block->escapeHtml($sourceItem['qty']) ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php endforeach; ?>

0 comments on commit 83c345b

Please sign in to comment.