Skip to content

Commit

Permalink
Merge pull request magento#559 from magento-engcom/451_assigned_sourc…
Browse files Browse the repository at this point in the history
…es_disabled

 magento#451 disabled sources tab content on manage_stock update
  • Loading branch information
Valeriy Nayda authored Mar 26, 2018
2 parents 5cade93 + 1a0e59d commit 70ba0ea
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryCatalog\Model;

use Magento\CatalogInventory\Model\Configuration;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\InventoryCatalog\Api\DefaultStockProviderInterface;
use Magento\InventoryConfigurationApi\Api\GetStockItemConfigurationInterface;

/**
* Check source items should be managed for given product sku
*/
class CanManageSourceItemsBySku
{
/**
* Provides manage stock global config value.
*
* @var ScopeConfigInterface
*/
private $config;

/**
* Provides default stock id for current website in order to get correct stock configuration for product.
*
* @var DefaultStockProviderInterface
*/
private $defaultStockProvider;

/**
* Provides stock item configuration for given product sku.
*
* @var GetStockItemConfigurationInterface
*/
private $getStockItemConfiguration;

/**
* @param ScopeConfigInterface $config
* @param GetStockItemConfigurationInterface $getStockItemConfiguration
* @param DefaultStockProviderInterface $defaultStockProvider
*/
public function __construct(
ScopeConfigInterface $config,
GetStockItemConfigurationInterface $getStockItemConfiguration,
DefaultStockProviderInterface $defaultStockProvider
) {
$this->config = $config;
$this->defaultStockProvider = $defaultStockProvider;
$this->getStockItemConfiguration = $getStockItemConfiguration;
}

/**
* @param string $sku Sku can be null if product is new
* @return bool
*/
public function execute(string $sku = null): bool
{
if (null !== $sku) {
$stockId = $this->defaultStockProvider->getId();
$itemConfiguration = $this->getStockItemConfiguration->execute($sku, $stockId);

if (null !== $itemConfiguration) {
return $itemConfiguration->isUseConfigManageStock()
? (bool)$this->config->getValue(Configuration::XML_PATH_MANAGE_STOCK)
: $itemConfiguration->isManageStock();
}
}
return (bool)$this->config->getValue(Configuration::XML_PATH_MANAGE_STOCK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@

namespace Magento\InventoryCatalog\Ui\DataProvider\Product\Form\Modifier;

use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier;
use Magento\Catalog\Model\Locator\LocatorInterface;
use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier;
use Magento\Framework\App\ResourceConnection;
use Magento\Inventory\Model\ResourceModel\Source as SourceResourceModel;
use Magento\Inventory\Model\ResourceModel\SourceItem\Collection;
use Magento\Inventory\Model\ResourceModel\SourceItem\CollectionFactory;
use Magento\InventoryApi\Api\Data\SourceInterface;
use Magento\InventoryApi\Api\Data\SourceItemInterface;
use Magento\InventoryCatalog\Model\CanManageSourceItemsBySku;
use Magento\InventoryCatalog\Model\IsSingleSourceModeInterface;
use Magento\InventoryConfiguration\Model\IsSourceItemsAllowedForProductTypeInterface;

Expand Down Expand Up @@ -48,25 +49,33 @@ class SourceItems extends AbstractModifier
*/
private $resourceConnection;

/**
* @var CanManageSourceItemsBySku
*/
private $canManageSourceItemsBySku;

/**
* @param IsSourceItemsAllowedForProductTypeInterface $isSourceItemsAllowedForProductType
* @param IsSingleSourceModeInterface $isSingleSourceMode
* @param LocatorInterface $locator
* @param CollectionFactory $sourceItemCollectionFactory
* @param ResourceConnection $resourceConnection
* @param CanManageSourceItemsBySku $canManageSourceItemsBySku
*/
public function __construct(
IsSourceItemsAllowedForProductTypeInterface $isSourceItemsAllowedForProductType,
IsSingleSourceModeInterface $isSingleSourceMode,
LocatorInterface $locator,
CollectionFactory $sourceItemCollectionFactory,
ResourceConnection $resourceConnection
ResourceConnection $resourceConnection,
CanManageSourceItemsBySku $canManageSourceItemsBySku
) {
$this->isSourceItemsAllowedForProductType = $isSourceItemsAllowedForProductType;
$this->isSingleSourceMode = $isSingleSourceMode;
$this->locator = $locator;
$this->sourceItemCollectionFactory = $sourceItemCollectionFactory;
$this->resourceConnection = $resourceConnection;
$this->canManageSourceItemsBySku = $canManageSourceItemsBySku;
}

/**
Expand Down Expand Up @@ -128,6 +137,7 @@ public function modifyMeta(array $meta)
return $meta;
}

$canMangeSourceItems = $this->canManageSourceItemsBySku->execute($product->getSku());
$meta['sources'] = [
'arguments' => [
'data' => [
Expand All @@ -136,7 +146,32 @@ public function modifyMeta(array $meta)
],
],
],
'children' => [
'assign_sources_container' => [
'children' => [
'assign_sources_button' => [
'arguments' => [
'data' => [
'config' => [
'visible' => $canMangeSourceItems,
],
],
],
],
],
],
'assigned_sources' => [
'arguments' => [
'data' => [
'config' => [
'visible' => $canMangeSourceItems,
],
],
],
],
],
];

return $meta;
}
}
1 change: 1 addition & 0 deletions app/code/Magento/InventoryCatalog/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"magento/module-catalog-inventory": "100.3.*",
"magento/module-inventory": "100.0.*",
"magento/module-inventory-api": "100.0.*",
"magento/module-inventory-configuration-api": "100.0.*",
"magento/module-ui": "100.0.*",
"magento/module-store": "100.3.*",
"magento/module-inventory-configuration": "100.0.*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
-->
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<fieldset name="sources" sortOrder="5">
<fieldset name="sources" sortOrder="5" component="Magento_InventoryCatalog/js/product/form/components/source-items">
<settings>
<label translate="true">Sources</label>
<collapsible>true</collapsible>
Expand Down Expand Up @@ -37,6 +37,9 @@
<item name="additionalForGroup" xsi:type="boolean">true</item>
<item name="additionalClasses" xsi:type="string">admin__field-medium</item>
<item name="dataScope" xsi:type="string"/>
<item name="imports" xsi:type="array">
<item name="visible" xsi:type="string">${ $.provider }:data.product.stock_data.manage_stock</item>
</item>
</item>
</argument>
</container>
Expand Down Expand Up @@ -85,6 +88,9 @@
<param name="enabled" xsi:type="boolean">false</param>
</dndConfig>
<columnsHeader>true</columnsHeader>
<imports>
<link name="visible">${ $.provider }:data.product.stock_data.manage_stock</link>
</imports>
</settings>
<container name="record" component="Magento_Ui/js/dynamic-rows/record">
<argument name="data" xsi:type="array">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

define([
'Magento_Ui/js/form/components/fieldset'
], function (Fieldset) {
'use strict';

return Fieldset.extend({
defaults: {
imports: {
onStockChange: '${ $.provider }:data.product.stock_data.manage_stock'
}
},

/**
* Disable all child elements if manage stock is zero
* @param {Integer} canManageStock
*/
onStockChange: function (canManageStock) {
if (canManageStock === 0) {
this.delegate('disabled', true);
}
}
});
});

0 comments on commit 70ba0ea

Please sign in to comment.