Skip to content

Commit

Permalink
Merge pull request #35 from jonfres/msi-stock-ui-aggregation
Browse files Browse the repository at this point in the history
Merge Msi stock ui aggregation to msi-stock-ui-aggregation
  • Loading branch information
Valeriy Nayda authored Jul 4, 2017
2 parents 5f97701 + 32f80ff commit 629bfa8
Show file tree
Hide file tree
Showing 23 changed files with 1,547 additions and 1 deletion.
67 changes: 67 additions & 0 deletions app/code/Magento/Inventory/Controller/Adminhtml/Stock/Edit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Inventory\Controller\Adminhtml\Stock;

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Backend\Model\View\Result\Page;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\InventoryApi\Api\StockRepositoryInterface;
use Magento\InventoryApi\Api\Data\StockInterface;

class Edit extends Action
{
/**
* @see _isAllowed()
*/
const ADMIN_RESOURCE = 'Magento_Inventory::stock';

/**
* @var StockRepositoryInterface
*/
private $stockRepository;

/**
* @param Context $context
* @param StockRepositoryInterface $stockRepository
*/
public function __construct(
Context $context,
StockRepositoryInterface $stockRepository
) {
parent::__construct($context);
$this->stockRepository = $stockRepository;
}

/**
* @inheritdoc
*/
public function execute()
{
$stockId = $this->getRequest()->getParam(StockInterface::STOCK_ID);
try {
$stock = $this->stockRepository->get($stockId);

/** @var Page $result */
$result = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
$result->setActiveMenu('Magento_Inventory::stock')
->addBreadcrumb(__('Edit Stock'), __('Edit Stock'));
$result->getConfig()
->getTitle()
->prepend(__('Edit Stock: %1', $stock->getName()));
} catch (NoSuchEntityException $e) {
/** @var Redirect $result */
$result = $this->resultRedirectFactory->create();
$this->messageManager->addErrorMessage(
__('Stock with id "%1" does not exist.', $stockId)
);
$result->setPath('*/*');
}
return $result;
}
}
31 changes: 31 additions & 0 deletions app/code/Magento/Inventory/Controller/Adminhtml/Stock/Index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Inventory\Controller\Adminhtml\Stock;

use Magento\Backend\App\Action;
use Magento\Backend\Model\View\Result\Page;
use Magento\Framework\Controller\ResultFactory;

class Index extends Action
{
/**
* @see _isAllowed()
*/
const ADMIN_RESOURCE = 'Magento_Inventory::stock';

/**
* @inheritdoc
*/
public function execute()
{
/** @var Page $resultPage */
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
$resultPage->setActiveMenu('Magento_Inventory::stock')
->addBreadcrumb(__('Sources'), __('List'));
$resultPage->getConfig()->getTitle()->prepend(__('Manage Stock'));
return $resultPage;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Inventory\Controller\Adminhtml\Stock;

use Magento\Backend\App\Action;
use Magento\Backend\Model\View\Result\Page;
use Magento\Framework\Controller\ResultFactory;

class NewAction extends Action
{
/**
* @see _isAllowed()
*/
const ADMIN_RESOURCE = 'Magento_Inventory::stock';

/**
* @inheritdoc
*/
public function execute()
{
/** @var Page $resultPage */
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
$resultPage->setActiveMenu('Magento_Inventory::stock');
$resultPage->getConfig()->getTitle()->prepend(__('New Stock'));
return $resultPage;
}
}
170 changes: 170 additions & 0 deletions app/code/Magento/Inventory/Controller/Adminhtml/Stock/Save.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Inventory\Controller\Adminhtml\Stock;

use Exception;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Api\DataObjectHelper;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Registry;
use Magento\InventoryApi\Api\Data\StockInterface;
use Magento\InventoryApi\Api\Data\StockInterfaceFactory;
use Magento\InventoryApi\Api\StockRepositoryInterface;

class Save extends Action
{
/**
* @see _isAllowed()
*/
const ADMIN_RESOURCE = 'Magento_Inventory::stock';

/**
* Registry stock_id key
*/
const REGISTRY_STOCK_ID_KEY = 'stock_id';

/**
* @var StockInterfaceFactory
*/
private $stockFactory;

/**
* @var StockRepositoryInterface
*/
private $stockRepository;

/**
* @var DataObjectHelper
*/
private $dataObjectHelper;

/**
* @var Registry
*/
private $registry;

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

/**
* @inheritdoc
*/
public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
$requestData = $this->getRequest()->getParam('general');
if ($this->getRequest()->isPost() && $requestData) {
try {
$stockId = $requestData[StockInterface::STOCK_ID]??null;

$stockId = $this->processSave($stockId, $requestData);
// Keep data for plugins on Save controller. Now we can not call separate services from one form.
$this->registry->register(self::REGISTRY_STOCK_ID_KEY, $stockId);

$this->messageManager->addSuccessMessage(__('The Stock has been saved.'));
$this->processRedirectAfterSuccessSave($resultRedirect, $stockId);

} catch (NoSuchEntityException $e) {
$this->messageManager->addErrorMessage(__('The Stock does not exist.'));
$this->processRedirectAfterFailureSave($resultRedirect);
} catch (CouldNotSaveException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
$this->processRedirectAfterFailureSave($resultRedirect, $stockId);
} catch (InputException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
$this->processRedirectAfterFailureSave($resultRedirect, $stockId);
} catch (Exception $e) {
$this->messageManager->addErrorMessage(__('Could not save stock'));
$this->processRedirectAfterFailureSave($resultRedirect, $stockId);
}
} else {
$this->messageManager->addErrorMessage(__('Wrong request.'));
$this->processRedirectAfterFailureSave($resultRedirect);
}
return $resultRedirect;
}

/**
* Saves inventory stock and returns stock id
*
* @param int $stockId
* @param array $requestData
* @return int
*/
private function processSave($stockId, array $requestData)
{
if ($stockId) {
$stock = $this->stockRepository->get($stockId);
} else {
/** @var StockInterface $stock */
$stock = $this->stockFactory->create();
}
$this->dataObjectHelper->populateWithArray($stock, $requestData, StockInterface::class);

$stockId = $this->stockRepository->save($stock);
return $stockId;
}

/**
* @param Redirect $resultRedirect
* @param int $stockId
* @return void
*/
private function processRedirectAfterSuccessSave(Redirect $resultRedirect, $stockId)
{
if ($this->getRequest()->getParam('back')) {
$resultRedirect->setPath('*/*/edit', [
StockInterface::STOCK_ID => $stockId,
'_current' => true,
]);
} elseif ($this->getRequest()->getParam('redirect_to_new')) {
$resultRedirect->setPath('*/*/new', [
'_current' => true,
]);
} else {
$resultRedirect->setPath('*/*/');
}
}

/**
* @param Redirect $resultRedirect
* @param int|null $stockId
* @return void
*/
private function processRedirectAfterFailureSave(Redirect $resultRedirect, $stockId = null)
{
if (null === $stockId) {
$resultRedirect->setPath('*/*/');
} else {
$resultRedirect->setPath('*/*/edit', [
StockInterface::STOCK_ID => $stockId,
'_current' => true,
]);
}
}
}
35 changes: 35 additions & 0 deletions app/code/Magento/Inventory/Model/ResourceModel/Stock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Inventory\Model\ResourceModel;

use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
use Magento\Framework\Model\ResourceModel\Db\Context;
use Magento\Inventory\Setup\InstallSchema;
use Magento\InventoryApi\Api\Data\StockInterface;

class Stock extends AbstractDb
{
/**
* Source constructor
*
* @param Context $context
* @param null $connectionName
*/
public function __construct(
Context $context,
$connectionName = null
) {
parent::__construct($context, $connectionName);
}

/**
* @inheritdoc
*/
protected function _construct()
{
$this->_init(InstallSchema::TABLE_NAME_STOCK, StockInterface::STOCK_ID);
}
}
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.
*/
namespace Magento\Inventory\Model\ResourceModel\Stock;

use Magento\Framework\Data\Collection\EntityFactoryInterface;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\Event\ManagerInterface;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
use Magento\Inventory\Model\ResourceModel\Stock as ResourceStock;
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
use Magento\Inventory\Model\Stock as StockModel;
use Psr\Log\LoggerInterface;

class Collection extends AbstractCollection
{
/**
* Collection constructor
*
* @param EntityFactoryInterface $entityFactory
* @param LoggerInterface $logger
* @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
* @param ManagerInterface $eventManager
* @param AdapterInterface $connection
* @param AbstractDb $resource
*/
public function __construct(
EntityFactoryInterface $entityFactory,
LoggerInterface $logger,
\Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
ManagerInterface $eventManager,
AdapterInterface $connection = null,
AbstractDb $resource = null
) {
parent::__construct(
$entityFactory,
$logger,
$fetchStrategy,
$eventManager,
$connection,
$resource
);
}

/**
* @inheritdoc
*/
protected function _construct()
{
$this->_init(StockModel::class, ResourceStock::class);
}
}
Loading

0 comments on commit 629bfa8

Please sign in to comment.