Skip to content

Commit

Permalink
Merge pull request #36 from jonfres/source-item-management
Browse files Browse the repository at this point in the history
source item management
  • Loading branch information
Valeriy Nayda authored Jun 30, 2017
2 parents 7280a26 + 4947eb3 commit b27c53b
Show file tree
Hide file tree
Showing 10 changed files with 668 additions and 0 deletions.
18 changes: 18 additions & 0 deletions app/code/Magento/Inventory/Model/ResourceModel/SourceItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Magento\Inventory\Model\ResourceModel;

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

class SourceItem extends AbstractDb
{
/**
* @inheritdoc
*/
protected function _construct()
{
$this->_init(InstallSchema::TABLE_NAME_SOURCE_ITEM, SourceItemInterface::SOURCE_ITEM_ID);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Magento\Inventory\Model\ResourceModel\SourceItem;

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\SourceItem as ResourceSource;
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
use Magento\Inventory\Model\SourceItem as SourceItemModel;
use Magento\InventoryApi\Api\Data\SourceItemInterface;
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(SourceItemModel::class, ResourceSource::class);
}

}
72 changes: 72 additions & 0 deletions app/code/Magento/Inventory/Model/SourceItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Magento\Inventory\Model;

use Magento\Framework\Model\AbstractExtensibleModel;
use \Magento\InventoryApi\Api\Data\SourceItemInterface;

class SourceItem extends AbstractExtensibleModel implements SourceItemInterface
{
/**
* @return int
*/
public function getSku()
{
return $this->getData(SourceItemInterface::SKU);
}

/**
* @param $sku
* @return int
*/
public function setSku($sku)
{
$this->setData(SourceItemInterface::SKU, $sku);
}

/**
* @return int
*/
public function getSourceId()
{
return $this->getData(SourceItemInterface::SOURCE_ID);
}

/**
* @return int
*/
public function getSourceItemId()
{
return $this->getData(SourceItemInterface::SOURCE_ITEM_ID);
}

/**
* @return float
*/
public function getQuantity()
{
return $this->getData(SourceItemInterface::QUANTITY);
}

/**
* @param $quantity
* @return float
*/
public function setQuantity($quantity)
{
$this->setData(SourceItemInterface::QUANTITY, $quantity);
}

/**
* @return int
*/
public function getStatus()
{
return $this->getData(SourceItemInterface::STATUS);
}

public function setStatus($status)
{
$this->setData(SourceItemInterface::STATUS, $status);
}
}
121 changes: 121 additions & 0 deletions app/code/Magento/Inventory/Model/SourceItemRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

namespace Magento\Inventory\Model;

use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Inventory\Model\ResourceModel\SourceItem as ResourceSource;
use Magento\Inventory\Model\ResourceModel\SourceItem\CollectionFactory;
use Magento\InventoryApi\Api\Data\SourceItemInterface;
use Magento\InventoryApi\Api\Data\SourceItemInterfaceFactory;
use Magento\InventoryApi\Api\Data\SourceItemSearchResultsInterfaceFactory;
use Psr\Log\LoggerInterface;
use Magento\InventoryApi\Api\SourceItemRepositoryInterface;

class SourceItemRepository implements SourceItemRepositoryInterface
{
/**
* @var ResourceSource
*/
private $resourceSource;

/**
* @var SourceItemInterfaceFactory
*/
private $sourceItemFactory;

/**
* @var CollectionProcessorInterface
*/
private $collectionProcessor;

/**
* @var CollectionFactory
*/
private $sourceItemCollectionFactory;

/**
* @var SourceItemSearchResultsInterfaceFactory
*/
private $sourceItemSearchResultsFactory;

/**
* @var SearchCriteriaBuilder
*/
private $searchCriteriaBuilder;

/**
* @var LoggerInterface
*/
private $logger;

/**
* SourceRepository constructor
*
* @param ResourceSource $resourceSource
* @param SourceItemInterfaceFactory $sourceItemFactory
* @param CollectionProcessorInterface $collectionProcessor
* @param CollectionFactory $sourceItemCollectionFactory
* @param SourceItemSearchResultsInterfaceFactory $sourceItemSearchResultsFactory
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param LoggerInterface $logger
*/
public function __construct(
ResourceSource $resourceSource,
SourceItemInterfaceFactory $sourceItemFactory,
CollectionProcessorInterface $collectionProcessor,
CollectionFactory $sourceItemCollectionFactory,
SourceItemSearchResultsInterfaceFactory $sourceItemSearchResultsFactory,
SearchCriteriaBuilder $searchCriteriaBuilder,
LoggerInterface $logger
) {
$this->resourceSource = $resourceSource;
$this->sourceItemFactory = $sourceItemFactory;
$this->collectionProcessor = $collectionProcessor;
$this->sourceItemCollectionFactory = $sourceItemCollectionFactory;
$this->sourceItemSearchResultsFactory = $sourceItemSearchResultsFactory;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->logger = $logger;
}

/**
* @param SourceItemInterface $sourceItem
* @return int
* @throws CouldNotSaveException
*/
public function save(SourceItemInterface $sourceItem)
{
try {
$this->resourceSource->save($sourceItem);
return $sourceItem->getSourceItemId();
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
throw new CouldNotSaveException(__('Could not save source item'), $e);
}
}

public function get($sourceItemId)
{
$sourceItem = $this->sourceItemFactory->create();
$this->resourceSource->load($sourceItem, $sourceItemId, SourceItemInterface::SOURCE_ITEM_ID);

if (!$sourceItem->getSourceItemId()) {
throw NoSuchEntityException::singleField(SourceItemInterface::SOURCE_ITEM_ID, $sourceItemId);
}

return $sourceItem;
}

public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria = null)
{

}

public function delete($sourceItemId)
{

}
}
96 changes: 96 additions & 0 deletions app/code/Magento/Inventory/Setup/InstallSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\InventoryApi\Api\Data\SourceInterface;
use Magento\InventoryApi\Api\Data\SourceItemInterface;
use Magento\InventoryApi\Api\Data\SourceCarrierLinkInterface;

/**
Expand All @@ -22,6 +23,7 @@ class InstallSchema implements InstallSchemaInterface
* Constant for table names of the model \Magento\Inventory\Model\Source
*/
const TABLE_NAME_SOURCE = 'inventory_source';
const TABLE_NAME_SOURCE_ITEM = 'inventory_source_item';

/**
* Constant for table name of \Magento\Inventory\Model\SourceCarrierLink
Expand Down Expand Up @@ -62,6 +64,7 @@ public function install(SchemaSetupInterface $setup, ModuleContextInterface $con
$setup->getConnection()->createTable($sourceTable);

$setup->getConnection()->createTable($this->createSourceCarrierLinkTable($setup));
$setup->getConnection()->createTable($this->createSourceItemTable($setup));
$setup->endSetup();
}

Expand Down Expand Up @@ -332,4 +335,97 @@ private function createSourceCarrierLinkTable(SchemaSetupInterface $setup)
AdapterInterface::FK_ACTION_CASCADE
);
}

/**
* @param SchemaSetupInterface $setup
* @return Table
*/
private function createSourceItemTable(SchemaSetupInterface $setup)
{
$sourceItemTable = $setup->getTable(InstallSchema::TABLE_NAME_SOURCE_ITEM);

return $setup->getConnection()->newTable(
$sourceItemTable
)->setComment(
'Inventory Source item Table'
)->addColumn(
SourceItemInterface::SOURCE_ITEM_ID,
Table::TYPE_INTEGER,
null,
[
InstallSchema::OPTION_IDENTITY => true,
InstallSchema::OPTION_UNSIGNED => true,
InstallSchema::OPTION_NULLABLE => false,
InstallSchema::OPTION_PRIMARY => true,
],
'Source Item ID'
)->addColumn(
SourceItemInterface::SOURCE_ID,
Table::TYPE_INTEGER,
null,
[
InstallSchema::OPTION_UNSIGNED => true,
InstallSchema::OPTION_NULLABLE => false,
],
'Source ID'
)->addColumn(
SourceItemInterface::SKU,
Table::TYPE_TEXT,
64,
[
InstallSchema::OPTION_UNSIGNED => true,
InstallSchema::OPTION_NULLABLE => false,
],
'Sku'
)->addColumn(
SourceItemInterface::QUANTITY,
Table::TYPE_DECIMAL,
null,
[
InstallSchema::OPTION_UNSIGNED => false,
InstallSchema::OPTION_NULLABLE => false,
InstallSchema::OPTION_DEFAULT => 0,
],
'Quantity'
)->addColumn(
SourceItemInterface::STATUS,
Table::TYPE_SMALLINT,
null,
[
InstallSchema::OPTION_NULLABLE => true,
InstallSchema::OPTION_UNSIGNED => true,
],
'Status'
)->addForeignKey(
$setup->getFkName(
$sourceItemTable,
SourceItemInterface::SOURCE_ID,
InstallSchema::TABLE_NAME_SOURCE,
SourceInterface::SOURCE_ID
),
SourceItemInterface::SOURCE_ID,
InstallSchema::TABLE_NAME_SOURCE,
SourceInterface::SOURCE_ID,
AdapterInterface::FK_ACTION_CASCADE
)->addForeignKey(
$setup->getFkName(
$sourceItemTable,
SourceItemInterface::SKU,
'catalog_product_entity',
'sku'
),
SourceItemInterface::SKU,
'catalog_product_entity',
'sku',
AdapterInterface::FK_ACTION_CASCADE
)->addIndex(
$setup->getIdxName(
$sourceItemTable,
[SourceItemInterface::SOURCE_ID, SourceItemInterface::SKU],
\Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE
),
[SourceItemInterface::SOURCE_ID, SourceItemInterface::SKU],
\Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE
);
}
}
Loading

0 comments on commit b27c53b

Please sign in to comment.