Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Source Delivery awareness to Order page #315

Merged
merged 10 commits into from
Dec 19, 2017
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php
/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls add also strict type declare(strict_types=1);

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

* 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; ?>