Skip to content

Commit

Permalink
Merge pull request #165 from magento-engcom/msi-website-default-stock
Browse files Browse the repository at this point in the history
Assign Website at the time of Creation to the Default Stock
  • Loading branch information
Valeriy Nayda authored Nov 13, 2017
2 parents fc4a71b + 0698655 commit c5f3252
Show file tree
Hide file tree
Showing 15 changed files with 536 additions and 15 deletions.
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\Model;

/**
* Delete link between Stock and Sales Channel (Service Provider Interface - SPI)
*
* @api
*/
interface DeleteSalesChannelToStockLinkInterface
{
/**
* @param string $type
* @param string $code
* @return void
*/
public function execute(string $type, string $code);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
namespace Magento\InventorySales\Model;

use Magento\InventorySales\Model\ResourceModel\GetAssignedSalesChannelsDataForStock;
use Magento\InventorySalesApi\Api\Data\SalesChannelInterfaceFactory;
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
use Magento\InventorySalesApi\Api\Data\SalesChannelInterfaceFactory;

/**
* @inheritdoc
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventorySales\Model;

/**
* Get assigned Stock id for Website (Service Provider Interface - SPI)
*
* @api
*/
interface GetAssignedStockIdForWebsiteInterface
{
/**
* Get assigned stock to website
*
* @param string $websiteCode
* @return int|null
*/
public function execute(string $websiteCode);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventorySales\Model\ResourceModel;

use Magento\Framework\App\ResourceConnection;
use Magento\InventorySales\Model\DeleteSalesChannelToStockLinkInterface;
use Magento\InventorySales\Setup\Operation\CreateSalesChannelTable;
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;

/**
* Implementation of link deleting between Stock and Sales Channels for specific db layer
*
* There is no additional business logic on SPI (Service Provider Interface) level so could use resource model as
* SPI implementation directly
*/
class DeleteSalesChannelToStockLink implements DeleteSalesChannelToStockLinkInterface
{
/**
* @var ResourceConnection
*/
private $resourceConnection;

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

/**
* @inheritdoc
*/
public function execute(string $type, string $code)
{
$connection = $this->resourceConnection->getConnection();
$tableName = $this->resourceConnection->getTableName(CreateSalesChannelTable::TABLE_NAME_SALES_CHANNEL);

$connection->delete($tableName, [
SalesChannelInterface::TYPE . ' = ?' => $type,
SalesChannelInterface::CODE . ' = ?' => $code,
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventorySales\Model\ResourceModel;

use Magento\Framework\App\ResourceConnection;
use Magento\InventorySales\Model\GetAssignedStockIdForWebsiteInterface;
use Magento\InventorySales\Setup\Operation\CreateSalesChannelTable;
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;

/**
* @inheritdoc
*/
class GetAssignedStockIdForWebsite implements GetAssignedStockIdForWebsiteInterface
{
/**
* @var ResourceConnection
*/
private $resourceConnection;

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

/**
* @inheritdoc
*/
public function execute(string $websiteCode)
{
$connection = $this->resourceConnection->getConnection();
$tableName = $this->resourceConnection->getTableName(CreateSalesChannelTable::TABLE_NAME_SALES_CHANNEL);

$select = $connection->select()
->from($tableName, [CreateSalesChannelTable::STOCK_ID])
->where('code = ?', $websiteCode)
->where('type = ?', SalesChannelInterface::TYPE_WEBSITE);

$result = $connection->fetchCol($select);

if (count($result) === 0) {
return null;
}
return reset($result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ public function __construct(
}

/**
* Replace Sales Channels for Stock
*
* @param SalesChannelInterface[] $salesChannels
* @param int $stockId
* @return void
* @inheritdoc
*/
public function execute(array $salesChannels, int $stockId)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ public function execute(EventObserver $observer)
$extensionAttributes = $stock->getExtensionAttributes();
$assignedSalesChannels = $extensionAttributes->getSalesChannels();

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventorySales\Observer\Website;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\InventoryApi\Api\StockRepositoryInterface;
use Magento\InventoryCatalog\Api\DefaultStockProviderInterface;
use Magento\InventorySales\Model\GetAssignedStockIdForWebsiteInterface;
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
use Magento\InventorySalesApi\Api\Data\SalesChannelInterfaceFactory;
use Magento\Store\Api\Data\WebsiteInterface;
use Magento\Store\Model\Website;

/**
* Assign the website to the default stock
*/
class AssignWebsiteToDefaultStock implements ObserverInterface
{
/**
* @var StockRepositoryInterface
*/
private $stockRepository;

/**
* @var DefaultStockProviderInterface
*/
private $defaultStockProvider;

/**
* @var SalesChannelInterfaceFactory
*/
private $salesChannelFactory;

/**
* @var GetAssignedStockIdForWebsiteInterface
*/
private $getAssignedStockIdForWebsite;

/**
* @param StockRepositoryInterface $stockRepository
* @param DefaultStockProviderInterface $defaultStockProvider
* @param SalesChannelInterfaceFactory $salesChannelFactory
* @param GetAssignedStockIdForWebsiteInterface $getAssignedStockIdForWebsite
*/
public function __construct(
StockRepositoryInterface $stockRepository,
DefaultStockProviderInterface $defaultStockProvider,
SalesChannelInterfaceFactory $salesChannelFactory,
GetAssignedStockIdForWebsiteInterface $getAssignedStockIdForWebsite
) {
$this->stockRepository = $stockRepository;
$this->defaultStockProvider = $defaultStockProvider;
$this->salesChannelFactory = $salesChannelFactory;
$this->getAssignedStockIdForWebsite = $getAssignedStockIdForWebsite;
}

/**
* @inheritdoc
*/
public function execute(Observer $observer)
{
/** @var Website $website */
$website = $observer->getData('website');
$websiteCode = $website->getCode();

if ($websiteCode === WebsiteInterface::ADMIN_CODE) {
return;
}

// checks is some stock already assigned to this website
if ($this->getAssignedStockIdForWebsite->execute($websiteCode) !== null) {
return;
}

$defaultStockId = $this->defaultStockProvider->getId();
$defaultStock = $this->stockRepository->get($defaultStockId);

$extensionAttributes = $defaultStock->getExtensionAttributes();
$salesChannels = $extensionAttributes->getSalesChannels();
$salesChannels[] = $this->createSalesChannelByWebsiteCode($websiteCode);

$extensionAttributes->setSalesChannels($salesChannels);
$this->stockRepository->save($defaultStock);
}

/**
* 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
@@ -0,0 +1,50 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventorySales\Observer\Website;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\InventorySales\Model\DeleteSalesChannelToStockLinkInterface;
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
use Magento\Store\Api\Data\WebsiteInterface;
use Magento\Store\Model\Website;

/**
* Delete link between Stock and Website
*/
class DeleteWebsiteToStockLink implements ObserverInterface
{
/**
* @var DeleteSalesChannelToStockLinkInterface
*/
private $deleteSalesChannelToStockLink;

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

/**
* @inheritdoc
*/
public function execute(Observer $observer)
{
/** @var Website $website */
$website = $observer->getData('website');
$websiteCode = $website->getCode();

if ($websiteCode === WebsiteInterface::ADMIN_CODE) {
return;
}
$this->deleteSalesChannelToStockLink->execute(SalesChannelInterface::TYPE_WEBSITE, $websiteCode);
}
}
Loading

0 comments on commit c5f3252

Please sign in to comment.