diff --git a/app/code/Magento/Inventory/view/adminhtml/ui_component/inventory_source_listing.xml b/app/code/Magento/Inventory/view/adminhtml/ui_component/inventory_source_listing.xml index cc0d8fa8f353..fb593ae7678c 100644 --- a/app/code/Magento/Inventory/view/adminhtml/ui_component/inventory_source_listing.xml +++ b/app/code/Magento/Inventory/view/adminhtml/ui_component/inventory_source_listing.xml @@ -268,7 +268,7 @@ - + inventory/source/edit diff --git a/app/code/Magento/Inventory/view/adminhtml/ui_component/inventory_stock_listing.xml b/app/code/Magento/Inventory/view/adminhtml/ui_component/inventory_stock_listing.xml index 04001ecf931f..b758548cc060 100644 --- a/app/code/Magento/Inventory/view/adminhtml/ui_component/inventory_stock_listing.xml +++ b/app/code/Magento/Inventory/view/adminhtml/ui_component/inventory_stock_listing.xml @@ -118,7 +118,7 @@ - + inventory/stock/edit diff --git a/app/code/Magento/InventorySales/Plugin/Inventory/Ui/StockDataProvider/SalesChannels.php b/app/code/Magento/InventorySales/Plugin/Inventory/Ui/StockDataProvider/SalesChannels.php index 1b348a95106b..580de417261a 100644 --- a/app/code/Magento/InventorySales/Plugin/Inventory/Ui/StockDataProvider/SalesChannels.php +++ b/app/code/Magento/InventorySales/Plugin/Inventory/Ui/StockDataProvider/SalesChannels.php @@ -7,13 +7,37 @@ namespace Magento\InventorySales\Plugin\Inventory\Ui\StockDataProvider; +use Magento\CatalogInventory\Model\Stock\StockRepository; use Magento\Inventory\Ui\DataProvider\StockDataProvider; +use Magento\InventorySales\Model\GetAssignedSalesChannelsForStockInterface; /** * Customize stock form. Add sales channels data */ class SalesChannels { + /** + * @var GetAssignedSalesChannelsForStockInterface + */ + private $getAssignedSalesChannelsForStock; + + /** + * @var StockRepository + */ + private $stockRepository; + + /** + * @param GetAssignedSalesChannelsForStockInterface $getAssignedSalesChannelsForStock + * @param StockRepository $stockRepository + */ + public function __construct( + GetAssignedSalesChannelsForStockInterface $getAssignedSalesChannelsForStock, + StockRepository $stockRepository + ) { + $this->getAssignedSalesChannelsForStock = $getAssignedSalesChannelsForStock; + $this->stockRepository = $stockRepository; + } + /** * @param StockDataProvider $subject * @param array $data @@ -23,12 +47,12 @@ public function afterGetData(StockDataProvider $subject, array $data): array { if ('inventory_stock_form_data_source' === $subject->getName()) { foreach ($data as &$stockData) { - $stockData['sales_channels'] = $this->getSalesChannelsDataForStock(); + $stockData['sales_channels'] = $this->getSalesChannelsDataForStock($stockData['general']); } unset($stockData); } elseif ($data['totalRecords'] > 0) { foreach ($data['items'] as &$stockData) { - $stockData['sales_channels'] = $this->getSalesChannelsDataForStock(); + $stockData['sales_channels'] = $this->getSalesChannelsDataForStock($stockData); } unset($stockData); } @@ -36,13 +60,17 @@ public function afterGetData(StockDataProvider $subject, array $data): array } /** + * @param array $stock * @return array */ - private function getSalesChannelsDataForStock(): array + private function getSalesChannelsDataForStock(array $stock): array { - // @todo: replace on real data - return [ - 'websites' => ['base'], - ]; + $salesChannelsData = []; + foreach ($stock['extension_attributes'] as $salesChannels) { + foreach ($salesChannels as $salesChannel) { + $salesChannelsData[$salesChannel['type']][] = $salesChannel['code']; + } + } + return $salesChannelsData; } } diff --git a/app/code/Magento/InventorySales/Ui/Component/Listing/Column/SalesChannels.php b/app/code/Magento/InventorySales/Ui/Component/Listing/Column/SalesChannels.php index 1c5581567e52..f393b51c6ef5 100644 --- a/app/code/Magento/InventorySales/Ui/Component/Listing/Column/SalesChannels.php +++ b/app/code/Magento/InventorySales/Ui/Component/Listing/Column/SalesChannels.php @@ -7,7 +7,10 @@ namespace Magento\InventorySales\Ui\Component\Listing\Column; +use Magento\InventorySales\Ui\SalesChannelNameResolverInterface; use Magento\Ui\Component\Listing\Columns\Column; +use Magento\Framework\View\Element\UiComponentFactory; +use Magento\Framework\View\Element\UiComponent\ContextInterface; /** * Add grid column for sales channels @@ -15,30 +18,59 @@ class SalesChannels extends Column { /** - * Prepare column value + * @var SalesChannelNameResolverInterface + */ + private $salesChannelNameResolver; + + /** + * @param ContextInterface $context + * @param UiComponentFactory $uiComponentFactory + * @param SalesChannelNameResolverInterface $salesChannelNameResolver + * @param array $components + * @param array $data + */ + public function __construct( + ContextInterface $context, + UiComponentFactory $uiComponentFactory, + SalesChannelNameResolverInterface $salesChannelNameResolver, + array $components = [], + array $data = [] + ) { + parent::__construct($context, $uiComponentFactory, $components, $data); + $this->salesChannelNameResolver = $salesChannelNameResolver; + } + + /** + * Prepare sales value * * @param array $salesChannelData - * @return string + * @return array */ - private function prepareStockChannelData(array $salesChannelData) + private function prepareSalesChannelData(array $salesChannelData): array { - $websiteData = ''; - foreach ($salesChannelData as $key => $channelData) { - $websiteData .= $key . ': ' . implode(',', $channelData); + $preparedChannelData = []; + foreach ($salesChannelData as $type => $salesChannel) { + foreach ($salesChannel as $key => $code) { + $preparedChannelData[$type][$key] = [ + 'name' => $this->salesChannelNameResolver->resolve($type, $code), + 'code' => $code, + ]; + } } - return $websiteData; + return $preparedChannelData; } /** - * @inheritdoc + * Prepare data source + * + * @param array $dataSource + * @return array */ public function prepareDataSource(array $dataSource) { if ($dataSource['data']['totalRecords'] > 0) { foreach ($dataSource['data']['items'] as &$row) { - if (isset($row['sales_channels'])) { - $row['sales_channels'] = $this->prepareStockChannelData($row['sales_channels']); - } + $row['sales_channels'] = $this->prepareSalesChannelData($row['sales_channels']); } } unset($row); diff --git a/app/code/Magento/InventorySales/Ui/SalesChannelNameResolverInterface.php b/app/code/Magento/InventorySales/Ui/SalesChannelNameResolverInterface.php new file mode 100644 index 000000000000..b8a055b9c269 --- /dev/null +++ b/app/code/Magento/InventorySales/Ui/SalesChannelNameResolverInterface.php @@ -0,0 +1,23 @@ +websiteRepository = $websiteRepository; + } + + /** + * @inheritdoc + */ + public function resolve(string $type, string $code): string + { + return SalesChannelInterface::TYPE_WEBSITE === $code ? $this->websiteRepository->get($code)->getName() : $code; + } +} diff --git a/app/code/Magento/InventorySales/etc/di.xml b/app/code/Magento/InventorySales/etc/di.xml index 20de14b1d48a..962fbdc81230 100644 --- a/app/code/Magento/InventorySales/etc/di.xml +++ b/app/code/Magento/InventorySales/etc/di.xml @@ -10,9 +10,13 @@ + + + + diff --git a/app/code/Magento/InventorySales/view/adminhtml/ui_component/inventory_stock_form.xml b/app/code/Magento/InventorySales/view/adminhtml/ui_component/inventory_stock_form.xml index 08e14fc318aa..559be475c7e9 100644 --- a/app/code/Magento/InventorySales/view/adminhtml/ui_component/inventory_stock_form.xml +++ b/app/code/Magento/InventorySales/view/adminhtml/ui_component/inventory_stock_form.xml @@ -18,7 +18,7 @@ true sales_channels - + text diff --git a/app/code/Magento/InventorySales/view/adminhtml/ui_component/inventory_stock_listing.xml b/app/code/Magento/InventorySales/view/adminhtml/ui_component/inventory_stock_listing.xml index 6148354d741b..86925d3c3a75 100644 --- a/app/code/Magento/InventorySales/view/adminhtml/ui_component/inventory_stock_listing.xml +++ b/app/code/Magento/InventorySales/view/adminhtml/ui_component/inventory_stock_listing.xml @@ -7,10 +7,13 @@ --> - + - + \ No newline at end of file diff --git a/app/code/Magento/InventorySales/view/adminhtml/web/js/stock/grid/cell/sales-channels.js b/app/code/Magento/InventorySales/view/adminhtml/web/js/stock/grid/cell/sales-channels.js new file mode 100644 index 000000000000..720aa538263e --- /dev/null +++ b/app/code/Magento/InventorySales/view/adminhtml/web/js/stock/grid/cell/sales-channels.js @@ -0,0 +1,50 @@ +/** + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ +define([ + 'Magento_Ui/js/grid/columns/column', + 'mage/template', + 'text!Magento_InventorySales/template/stock/grid/cell/sales-channel-content.html' +], function (Column, mageTemplate, channelTemplate) { + 'use strict'; + + return Column.extend({ + defaults: { + bodyTmpl: 'Magento_InventorySales/stock/grid/cell/sales-channel-cell.html' + }, + + /** + * render all sales channel records and return complete html + * each over all sales channels and render html for each channel + * + * @param {Object} records contains all results + * @returns {String} return rendered html content + */ + renderRecords: function (records) { + var salesChannels = records['sales_channels'], + htmlContent = '', + channelType; + + if (typeof salesChannels !== 'object') { + throw new Error( + 'Provided wrong salesChannel type ' + typeof salesChannels + ' the correct type would be object.' + ); + } + + for (channelType in salesChannels) { + htmlContent += mageTemplate( + channelTemplate, + { + data: { + channelType: channelType.charAt(0).toUpperCase() + channelType.slice(1), + values: salesChannels[channelType] + } + } + ); + } + + return htmlContent; + } + }); +}); diff --git a/app/code/Magento/InventorySales/view/adminhtml/web/template/stock/grid/cell/sales-channel-cell.html b/app/code/Magento/InventorySales/view/adminhtml/web/template/stock/grid/cell/sales-channel-cell.html new file mode 100644 index 000000000000..31021e8443ce --- /dev/null +++ b/app/code/Magento/InventorySales/view/adminhtml/web/template/stock/grid/cell/sales-channel-cell.html @@ -0,0 +1,7 @@ + +
diff --git a/app/code/Magento/InventorySales/view/adminhtml/web/template/stock/grid/cell/sales-channel-content.html b/app/code/Magento/InventorySales/view/adminhtml/web/template/stock/grid/cell/sales-channel-content.html new file mode 100644 index 000000000000..01fb4867d8a8 --- /dev/null +++ b/app/code/Magento/InventorySales/view/adminhtml/web/template/stock/grid/cell/sales-channel-content.html @@ -0,0 +1,17 @@ + +
+ + <%- data.channelType %>: + + + <% _.each(data.values, function(value, key) { %> + <% var separator = ((data.values.length -1) != key) ? ', ' : ''; %> + <%- value.name %> (<%- value.code %>)<%- separator %> + <% }); %> + +