Skip to content

Sales channels saving #173

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

Merged
merged 28 commits into from
Nov 6, 2017
Merged
Show file tree
Hide file tree
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
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
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
Expand Up @@ -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
Expand All @@ -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
Expand Up @@ -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;
}
}
Loading