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

Sales channels saving #173

Merged
merged 28 commits into from
Nov 6, 2017
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
7a3648a
Merge branch 'msi-inventory-mapping' into saleschannels-integration-w…
steinkopffp Oct 27, 2017
ea6a9d3
Merge branch 'msi-inventory-mapping' into saleschannels-integration-w…
steinkopffp Oct 27, 2017
9aa0010
Merge branch 'msi-inventory-mapping' into saleschannels-integration-w…
steinkopffp Oct 27, 2017
5c59b7b
Merge branch 'msi-inventory-mapping' into saleschannels-integration-w…
steinkopffp Oct 28, 2017
85a86e7
Merge branch 'msi-inventory-mapping' into saleschannels-integration-w…
steinkopffp Oct 28, 2017
9a0c3c6
Merge branch 'msi-inventory-mapping' into saleschannels-integration-w…
steinkopffp Oct 28, 2017
798ed3c
Providing grid with rendered content
steinkopffp Oct 29, 2017
57331a9
adding website names
steinkopffp Oct 29, 2017
bc158c1
Merge branch 'msi-inventory-mapping' into saleschannels-integration-w…
steinkopffp Oct 29, 2017
dbc1782
Merge remote-tracking branch 'origin/saleschannels-integration-with-a…
belinda-tschampel Oct 29, 2017
f0fd2f7
adding code refactoring
steinkopffp Oct 29, 2017
fd58c1b
refactoring and cleaning structur
steinkopffp Oct 29, 2017
df14d78
refactoring and cleaning structur
steinkopffp Oct 29, 2017
d9e8ffb
WIP: Extended saving of stock to include the extions attribute data f…
belinda-tschampel Oct 29, 2017
b533c11
Merge remote-tracking branch 'origin/develop' into saleschannels-inte…
Nov 2, 2017
21082cd
MSI: Integrate the Sales Channel output to the Manage Stock view
Nov 3, 2017
6685c19
MSI: Integrate the Sales Channel output to the Manage Stock view
Nov 3, 2017
374eea8
Merge remote-tracking branch 'origin/develop' into sales-channels-ui-…
Nov 3, 2017
0618cc2
Merge remote-tracking branch 'origin/saleschannels-integration-with-a…
Nov 3, 2017
b9d579d
MSI: Integrate the Sales Channel output to the Manage Stock view
Nov 6, 2017
14ae0bd
MSI: Integrate the Sales Channel output to the Manage Stock view
Nov 6, 2017
e23ce30
MSI: Integrate the Sales Channel output to the Manage Stock view
Nov 6, 2017
a2f0430
MSI: Integrate the Sales Channel output to the Manage Stock view
Nov 6, 2017
1ac98b4
MSI: Integrate the Sales Channel output to the Manage Stock view
Nov 6, 2017
b37b37c
MSI: Integrate the Sales Channel output to the Manage Stock view
Nov 6, 2017
59a30af
MSI: Integrate the Sales Channel output to the Manage Stock view
Nov 6, 2017
480643b
MSI: Integrate the Sales Channel output to the Manage Stock view
Nov 6, 2017
ae325b7
MSI: Integrate the Sales Channel output to the Manage Stock view
Nov 6, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion app/code/Magento/Inventory/Controller/Adminhtml/Stock/Save.php
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@
use Magento\Backend\App\Action\Context;
use Magento\Framework\Api\DataObjectHelper;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\EntityManager\EventManager;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\NoSuchEntityException;
@@ -48,25 +49,33 @@ class Save extends Action
*/
private $stockSourceLinkProcessor;

/**
* @var EventManager
*/
private $eventManager;

/**
* @param Context $context
* @param StockInterfaceFactory $stockFactory
* @param StockRepositoryInterface $stockRepository
* @param DataObjectHelper $sourceHydrator
* @param StockSourceLinkProcessor $stockSourceLinkProcessor
* @param EventManager $eventManager
*/
public function __construct(
Context $context,
StockInterfaceFactory $stockFactory,
StockRepositoryInterface $stockRepository,
DataObjectHelper $sourceHydrator,
StockSourceLinkProcessor $stockSourceLinkProcessor
StockSourceLinkProcessor $stockSourceLinkProcessor,
EventManager $eventManager
) {
parent::__construct($context);
$this->stockFactory = $stockFactory;
$this->stockRepository = $stockRepository;
$this->dataObjectHelper = $sourceHydrator;
$this->stockSourceLinkProcessor = $stockSourceLinkProcessor;
$this->eventManager = $eventManager;
}

/**
@@ -126,6 +135,13 @@ private function processSave(array $requestData, int $stockId = null): int
$stock = $this->stockRepository->get($stockId);
}
$this->dataObjectHelper->populateWithArray($stock, $requestData['general'], StockInterface::class);
$this->eventManager->dispatch(
'save_stock_controller_populate_stock_with_data',
[
'request_data' => $requestData,
'stock' => $stock,
]
);
$stockId = $this->stockRepository->save($stock);

$assignedSources =
Original file line number Diff line number Diff line change
@@ -268,7 +268,7 @@
<label translate="true">Priority</label>
</settings>
</column>
<actionsColumn name="actions" class="Magento\Backend\Ui\Component\Listing\Column\EditAction">
<actionsColumn name="actions" class="Magento\Backend\Ui\Component\Listing\Column\EditAction" sortOrder="100">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="editUrlPath" xsi:type="string">inventory/source/edit</item>
Original file line number Diff line number Diff line change
@@ -118,7 +118,7 @@
</editor>
</settings>
</column>
<actionsColumn name="actions" class="Magento\Backend\Ui\Component\Listing\Column\EditAction">
<actionsColumn name="actions" class="Magento\Backend\Ui\Component\Listing\Column\EditAction" sortOrder="100">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="editUrlPath" xsi:type="string">inventory/stock/edit</item>
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventorySales\Observer\Stock;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\InventoryApi\Api\Data\StockInterface;
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
use Magento\InventorySalesApi\Api\Data\SalesChannelInterfaceFactory;

/**
* Populate stock with sales channels during saving via controller
*
* This needs to be handled in dedicated observer, because there is no pre-defined way of making several API calls for
* Form submission handling
*/
class PopulateWithWebsiteSalesChannelsObserver implements ObserverInterface
{
/**
* @var SalesChannelInterfaceFactory
*/
private $salesChannelFactory;

/**
* @param SalesChannelInterfaceFactory $salesChannelFactory
*/
public function __construct(SalesChannelInterfaceFactory $salesChannelFactory)
{
$this->salesChannelFactory = $salesChannelFactory;
}

/**
* Populate stock with sales channels during saving via controller
*
* @param EventObserver $observer
* @return void
*/
public function execute(EventObserver $observer)
{
/** @var StockInterface $stock */
$stock = $observer->getEvent()->getStock();
$requestData = $observer->getEvent()->getRequestData();

$extensionAttributes = $stock->getExtensionAttributes();
$assignedSalesChannels = $extensionAttributes->getSalesChannels();

foreach ($assignedSalesChannels as $key => $assignedSalesChannel) {
if ($assignedSalesChannel->getType() === SalesChannelInterface::TYPE_WEBSITE) {
unset($assignedSalesChannels[$key]);
}
}

if (isset($requestData['sales_channels'][SalesChannelInterface::TYPE_WEBSITE])
&& is_array($requestData['sales_channels'][SalesChannelInterface::TYPE_WEBSITE])
) {
foreach ($requestData['sales_channels'][SalesChannelInterface::TYPE_WEBSITE] as $websiteCode) {
$assignedSalesChannels[] = $this->createSalesChannelByWebsiteCode($websiteCode);
}
}
$extensionAttributes->setSalesChannels($assignedSalesChannels);
}

/**
* Create the sales channel by given website code
*
* @param string $websiteCode
* @return SalesChannelInterface
*/
private function createSalesChannelByWebsiteCode(string $websiteCode): SalesChannelInterface
{
$salesChannel = $this->salesChannelFactory->create();
$salesChannel->setCode($websiteCode);
$salesChannel->setType(SalesChannelInterface::TYPE_WEBSITE);
return $salesChannel;
}
}
Original file line number Diff line number Diff line change
@@ -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,26 +47,36 @@ 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();
$salesChannelsData = $this->getSalesChannelsDataForStock($stockData['general']);
if (count($salesChannelsData)) {
$stockData['sales_channels'] = $salesChannelsData;
}
}
unset($stockData);
} elseif ($data['totalRecords'] > 0) {
foreach ($data['items'] as &$stockData) {
$stockData['sales_channels'] = $this->getSalesChannelsDataForStock();
$salesChannelsData = $this->getSalesChannelsDataForStock($stockData);
if (count($salesChannelsData)) {
$stockData['sales_channels'] = $salesChannelsData;
}
}
unset($stockData);
}
return $data;
}

/**
* @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;
}
}
Original file line number Diff line number Diff line change
@@ -7,42 +7,74 @@

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
* Add grid column for sales channels. Prepare data
*/
class SalesChannels extends Column
{
/**
* Prepare column value
*
* @param array $salesChannelData
* @return string
* @var SalesChannelNameResolverInterface
*/
private function prepareStockChannelData(array $salesChannelData)
{
$websiteData = '';
foreach ($salesChannelData as $key => $channelData) {
$websiteData .= $key . ': ' . implode(',', $channelData);
}
return $websiteData;
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;
}

/**
* @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);

return $dataSource;
}

/**
* Prepare sales value
*
* @param array $salesChannelData
* @return array
*/
private function prepareSalesChannelData(array $salesChannelData): array
{
$preparedChannelData = [];
foreach ($salesChannelData as $type => $salesChannel) {
foreach ($salesChannel as $code) {
$preparedChannelData[$type][] = [
'name' => $this->salesChannelNameResolver->resolve($type, $code),
'code' => $code,
];
}
}
return $preparedChannelData;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventorySales\Ui;

/**
* Resolve sales channel name by type and code
*/
interface SalesChannelNameResolverInterface
{
/**
* Resolve sales channel name by type and code
*
* @param string $type
* @param string $code
* @return string
*/
public function resolve(string $type, string $code): string;
}
41 changes: 41 additions & 0 deletions app/code/Magento/InventorySales/Ui/WebsiteNameResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventorySales\Ui;

use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
use Magento\Store\Api\WebsiteRepositoryInterface;

/**
* {@inheritdoc}
*
* In default implementation works only with website
*/
class WebsiteNameResolver implements SalesChannelNameResolverInterface
{
/**
* @var WebsiteRepositoryInterface
*/
private $websiteRepository;

/**
* @param WebsiteRepositoryInterface $websiteRepository
*/
public function __construct(
WebsiteRepositoryInterface $websiteRepository
) {
$this->websiteRepository = $websiteRepository;
}

/**
* @inheritdoc
*/
public function resolve(string $type, string $code): string
{
return SalesChannelInterface::TYPE_WEBSITE === $type ? $this->websiteRepository->get($code)->getName() : $code;
}
}
12 changes: 12 additions & 0 deletions app/code/Magento/InventorySales/etc/adminhtml/events.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="save_stock_controller_populate_stock_with_data">
<observer name="populate_stock_with_website_sales_channels" instance="Magento\InventorySales\Observer\Stock\PopulateWithWebsiteSalesChannelsObserver"/>
</event>
</config>
4 changes: 4 additions & 0 deletions app/code/Magento/InventorySales/etc/di.xml
Original file line number Diff line number Diff line change
@@ -10,9 +10,13 @@
<preference for="Magento\InventorySalesApi\Api\Data\SalesChannelInterface" type="Magento\InventorySales\Model\SalesChannel"/>
<preference for="Magento\InventorySales\Model\GetAssignedSalesChannelsForStockInterface" type="Magento\InventorySales\Model\GetAssignedSalesChannelsForStock"/>
<preference for="Magento\InventorySales\Model\ReplaceSalesChannelsForStockInterface" type="Magento\InventorySales\Model\ResourceModel\ReplaceSalesChannelsDataForStock"/>
<preference for="Magento\InventorySales\Ui\SalesChannelNameResolverInterface" type="Magento\InventorySales\Ui\WebsiteNameResolver"/>
<type name="Magento\InventoryApi\Api\StockRepositoryInterface">
<plugin name="load_sales_channels_on_get_list" type="Magento\InventorySales\Plugin\Inventory\StockRepository\LoadSalesChannelsOnGetListPlugin"/>
<plugin name="load_sales_channels_on_get" type="Magento\InventorySales\Plugin\Inventory\StockRepository\LoadSalesChannelsOnGetPlugin"/>
<plugin name="save_sales_channels_links" type="Magento\InventorySales\Plugin\Inventory\StockRepository\SaveSalesChannelsLinksPlugin"/>
</type>
<type name="Magento\Inventory\Ui\DataProvider\StockDataProvider">
<plugin name="sales_channel_data" type="Magento\InventorySales\Plugin\Inventory\Ui\StockDataProvider\SalesChannels" />
</type>
</config>
Original file line number Diff line number Diff line change
@@ -6,22 +6,19 @@
*/
-->
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<settings>
<buttons>
<button name="delete" class="Magento\InventoryCatalog\Ui\Component\Control\Stock\DeleteButton"/>
</buttons>
</settings>
<fieldset name="sales_channels" sortOrder="30">
<settings>
<label translate="true">Sales Channels</label>
<collapsible>true</collapsible>
<opened>true</opened>
<dataScope>sales_channels</dataScope>
</settings>
<field name="websites" formElement="multiselect" sortOrder="20">
<field name="website" formElement="multiselect" sortOrder="20">
<settings>
<dataType>text</dataType>
<label translate="true">Websites</label>
<dataScope>website</dataScope>
<notice>Use Ctrl+Click for check several values or uncheck value</notice>
</settings>
<formElements>
<multiselect>
Original file line number Diff line number Diff line change
@@ -7,10 +7,13 @@
-->
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<columns name="inventory_stock_listing_columns">
<column name="sales_channels" class="Magento\InventorySales\Ui\Component\Listing\Column\SalesChannels" sortOrder="30">
<column name="sales_channels"
class="Magento\InventorySales\Ui\Component\Listing\Column\SalesChannels"
component="Magento_InventorySales/js/stock/grid/cell/sales-channels"
sortOrder="40">
<settings>
<label translate="true">Sales Channels</label>
</settings>
</column>
</columns>
</listing>
</listing>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
define([
'Magento_Ui/js/grid/columns/column',
'underscore'
], function (Column, _) {
'use strict';

return Column.extend({
defaults: {
bodyTmpl: 'Magento_InventorySales/stock/grid/cell/sales-channel-cell.html'
},

/**
* Get sales channels grouped by type
*
* @param {Object} record - Record object
* @returns {Array} Result array
*/
getSalesChannelsGroupedByType: function (record) {
var result = [];

_.each(record[this.index], function (channels, type) {
result.push({
type: type,
channels: channels
});
});

return result;
}
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<div class="data-grid-cell-content">
<ul data-bind="foreach: { data: $col.getSalesChannelsGroupedByType($row()), as: '$salesChannels' }">
<li>
<strong data-bind="text: $salesChannels.type"></strong>
<ul data-bind="foreach: { data: $salesChannels.channels, as: '$salesChannel' }">
<li>
<span data-bind="text: $salesChannel.name"></span>
(<span data-bind="text: $salesChannel.code"></span>)
</li>
</ul>
</li>
</ul>
</div>