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

Issue 1306 rename website breaks grid #1344

Merged
merged 14 commits into from
Jun 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?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;

/**
* Get website code by website id
*/
class GetWebsiteCodeByWebsiteId
{
/**
* @var ResourceConnection
*/
private $resourceConnection;

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

/**
* @param int $websiteId
* @return string|null
*/
public function execute(int $websiteId): ?string
{
$connection = $this->resourceConnection->getConnection();
$tableName = $this->resourceConnection->getTableName('store_website');
$selectQry = $connection->select()->from($tableName, 'code')->where('website_id = ?', $websiteId);

$result = $connection->fetchOne($selectQry);
return (false === $result) ? null : $result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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\InventorySalesApi\Api\Data\SalesChannelInterface;

/**
* This class handles website code change and should not be used directly, but only
* from \Magento\InventorySales\Plugin\Store\WebsiteResourcePlugin to keep a soft integrity
* between 'store_website' table and 'inventory_stock_sales_channel' table on changes.
*/
class UpdateSalesChannelWebsiteCode
{
/**
* @var ResourceConnection
*/
private $resourceConnection;

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

/**
* @param string $oldCode
* @param string $newCode
* @return void
*/
public function execute(
string $oldCode,
string $newCode
): void {
$connection = $this->resourceConnection->getConnection();
$tableName = $this->resourceConnection->getTableName('inventory_stock_sales_channel');

$connection->update(
$tableName,
[
SalesChannelInterface::CODE => $newCode,
],
[
SalesChannelInterface::TYPE . ' = ?' => SalesChannelInterface::TYPE_WEBSITE,
SalesChannelInterface::CODE . ' = ?' => $oldCode,
]
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,25 @@
*/
declare(strict_types=1);

namespace Magento\InventorySales\Observer\Website;
namespace Magento\InventorySales\Plugin\Store\Model\ResourceModel\Website;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Model\AbstractModel;
use Magento\Framework\Validation\ValidationException;
use Magento\InventoryApi\Api\StockRepositoryInterface;
use Magento\InventoryCatalogApi\Api\DefaultStockProviderInterface;
use Magento\InventorySalesApi\Model\GetAssignedStockIdForWebsiteInterface;
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
use Magento\InventorySalesApi\Api\Data\SalesChannelInterfaceFactory;
use Magento\Store\Api\Data\WebsiteInterface;
use Magento\Store\Model\ResourceModel\Website as WebsiteResourceModel;
use Magento\Store\Model\Website;

/**
* Assign the website to the default stock
*/
class AssignWebsiteToDefaultStock implements ObserverInterface
class AssignWebsiteToDefaultStockPlugin
{
/**
* @var StockRepositoryInterface
Expand Down Expand Up @@ -61,21 +64,29 @@ public function __construct(
}

/**
* @inheritdoc
* @param WebsiteResourceModel $subject
* @param WebsiteResourceModel $result
* @param Website|AbstractModel $website
* @return WebsiteResourceModel
* @throws CouldNotSaveException
* @throws NoSuchEntityException
* @throws ValidationException
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function execute(Observer $observer)
{
/** @var Website $website */
$website = $observer->getData('website');
public function afterSave(
WebsiteResourceModel $subject,
WebsiteResourceModel $result,
AbstractModel $website
) {
$websiteCode = $website->getCode();

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

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

$defaultStockId = $this->defaultStockProvider->getId();
Expand All @@ -87,6 +98,8 @@ public function execute(Observer $observer)

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

return $result;
}

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

namespace Magento\InventorySales\Plugin\Store\Model\ResourceModel\Website;

use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Model\AbstractModel;
use Magento\Framework\Validation\ValidationException;
use Magento\InventorySales\Model\ResourceModel\GetWebsiteCodeByWebsiteId;
use Magento\InventorySales\Model\ResourceModel\UpdateSalesChannelWebsiteCode;
use Magento\Store\Api\Data\WebsiteInterface;
use Magento\Store\Model\ResourceModel\Website as WebsiteResourceModel;
use Magento\Store\Model\Website;

class UpdateSalesChannelWebsiteCodePlugin
{
/**
* @var UpdateSalesChannelWebsiteCode
*/
private $updateSalesChannelWebsiteCode;

/**
* @var GetWebsiteCodeByWebsiteId
*/
private $getWebsiteCodeByWebsiteId;

/**
* @param UpdateSalesChannelWebsiteCode $updateSalesChannelWebsiteCode
* @param GetWebsiteCodeByWebsiteId $getWebsiteCodeByWebsiteId
*/
public function __construct(
UpdateSalesChannelWebsiteCode $updateSalesChannelWebsiteCode,
GetWebsiteCodeByWebsiteId $getWebsiteCodeByWebsiteId
) {
$this->updateSalesChannelWebsiteCode = $updateSalesChannelWebsiteCode;
$this->getWebsiteCodeByWebsiteId = $getWebsiteCodeByWebsiteId;
}

/**
* @param WebsiteResourceModel $subject
* @param callable $proceed
* @param Website|AbstractModel $website
* @return WebsiteResourceModel
* @throws CouldNotSaveException
* @throws NoSuchEntityException
* @throws ValidationException
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function aroundSave(
WebsiteResourceModel $subject,
callable $proceed,
AbstractModel $website
) {
$newCode = $website->getCode();
$oldCode = null;

if (null !== $website->getId()) {
$oldCode = $this->getWebsiteCodeByWebsiteId->execute((int)$website->getId());
}

$result = $proceed($website);

if (($oldCode !== null) && ($oldCode !== WebsiteInterface::ADMIN_CODE) && ($oldCode !== $newCode)) {
$this->updateSalesChannelWebsiteCode->execute($oldCode, $newCode);
}
return $result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventorySales\Test\Integration\Website;

use Magento\InventorySalesApi\Model\GetAssignedStockIdForWebsiteInterface;
use Magento\Store\Model\Website;
use Magento\Store\Model\ResourceModel\Website as WebsiteResource;
use Magento\Store\Model\WebsiteFactory;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;

class RenameWebsiteToStockLinkTest extends TestCase
{
/**
* @var WebsiteFactory
*/
private $websiteFactory;

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

/**
* @var WebsiteResource
*/
private $websiteResource;

protected function setUp()
{
$this->websiteFactory = Bootstrap::getObjectManager()->get(WebsiteFactory::class);
$this->getAssignedStockIdForWebsite = Bootstrap::getObjectManager()->get(
GetAssignedStockIdForWebsiteInterface::class
);
$this->websiteResource = Bootstrap::getObjectManager()->get(WebsiteResource::class);
}

/**
* @throws \Exception
* @magentoDbIsolation enabled
*/
public function testRenameWebsiteToStockLink()
{
$oldWebsiteCode = 'old_website_code';
$newWebsiteCode = 'new_website_code';

/** @var Website $website */
$website = $this->websiteFactory->create();
$website->setCode($oldWebsiteCode);
$this->websiteResource->save($website);
$websiteId = $website->getId();

$website = $this->websiteFactory->create();
$this->websiteResource->load($website, $websiteId);
$website->setCode($newWebsiteCode);
$this->websiteResource->save($website);

self::assertNull(
$this->getAssignedStockIdForWebsite->execute($oldWebsiteCode),
'Old website link was not removed'
);

self::assertNotNull(
$this->getAssignedStockIdForWebsite->execute($newWebsiteCode),
'Website link was not renamed'
);
}
}
4 changes: 4 additions & 0 deletions app/code/Magento/InventorySales/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,8 @@
</arguments>
</type>
<preference for="Magento\InventorySalesApi\Model\StockByWebsiteIdResolverInterface" type="Magento\InventorySales\Model\StockByWebsiteIdResolver"/>
<type name="Magento\Store\Model\ResourceModel\Website">
<plugin name="assign_website_to_default_stock" type="Magento\InventorySales\Plugin\Store\Model\ResourceModel\Website\AssignWebsiteToDefaultStockPlugin"/>
<plugin name="update_sales_channel_website_code" type="Magento\InventorySales\Plugin\Store\Model\ResourceModel\Website\UpdateSalesChannelWebsiteCodePlugin"/>
</type>
</config>
3 changes: 0 additions & 3 deletions app/code/Magento/InventorySales/etc/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="website_save_after">
<observer name="assign_website_to_default_stock" instance="Magento\InventorySales\Observer\Website\AssignWebsiteToDefaultStock"/>
</event>
<event name="website_delete_after">
Copy link

Choose a reason for hiding this comment

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

Looks like we also need to update delete logic website_delete_after

Copy link
Contributor Author

Choose a reason for hiding this comment

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

An observer after delete seems to be an efficient way and seems to be working, do you prefer to pluginize it?

Copy link
Contributor Author

@phoenix128 phoenix128 Jun 11, 2018

Choose a reason for hiding this comment

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

Done in: #1356

<observer name="delete_website_to_stock_link" instance="Magento\InventorySales\Observer\Website\DeleteWebsiteToStockLink"/>
</event>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Magento\InventorySalesAdminUi\Ui\Component\Listing\Column;

use Magento\Framework\Exception\NoSuchEntityException;
use Magento\InventorySalesAdminUi\Ui\SalesChannelNameResolverInterface;
use Magento\Ui\Component\Listing\Columns\Column;
use Magento\Framework\View\Element\UiComponentFactory;
Expand Down