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

MSI source Import #116

Merged
merged 44 commits into from
Oct 29, 2017
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
f1c834c
MSI: Initial implementation of Magento_InventoryImportExport module
vadimjustus Sep 29, 2017
eca76b5
MSI: Implement append bahaviour import for source items
vadimjustus Oct 1, 2017
0e48a30
Merge pull request #115 from vadimjustus/msi-source-import-export
larsroettig Oct 3, 2017
8dcbe34
[task] #115 Refactoring
Oct 3, 2017
24f776e
MSI: Change method accessability to private
vadimjustus Oct 3, 2017
058d08c
MSI: Implement intergration test for import entity
vadimjustus Oct 3, 2017
6b6153c
wip
Oct 8, 2017
b11c9e8
Merge remote-tracking branch 'origin/develop' into msi-source-import-…
Oct 8, 2017
7d9eba5
wip
Oct 8, 2017
9a7067f
MSI source: Impelment DelteMultiple Model and Tests
Oct 8, 2017
7787760
Remove not required fixture from methodes
Oct 8, 2017
3ffc60c
MSI source: Bugfix for Indexer Integration test
Oct 8, 2017
338688b
Merge remote-tracking branch 'origin/develop' into msi-source-import-…
Oct 13, 2017
60064e3
MSI source:
Oct 13, 2017
2a30f19
MSI source:
Oct 13, 2017
98b61ff
MSI source:
Oct 13, 2017
b68917a
phpmd and phpcs fixes
Oct 17, 2017
457fc27
MSI: Add Testcase for testImportDataWithWrongBehavior
Oct 17, 2017
705e7f9
MSI:
Oct 20, 2017
7c83028
MSI:
Oct 20, 2017
973d2ba
MSI: Remove LegacyJsonHelperInterface
vadimjustus Oct 25, 2017
27ac9ab
MSI: #131
Oct 25, 2017
43380b5
Merge branch 'msi-source-import-export' into msi-source-export
vadimjustus Oct 25, 2017
2ba6943
wip
Oct 26, 2017
c15424a
Merge branch 'msi-source-export' of github.com:magento-engcom/magento…
Oct 26, 2017
c59cde0
WIP bugfix for export filter
Oct 26, 2017
e472b69
wip
Oct 26, 2017
505f630
MIS: Implement filtering for export functionality
vadimjustus Oct 27, 2017
7158415
add filter type
Oct 27, 2017
36ac202
Merge branch 'msi-source-export' of github.com:magento-engcom/msi int…
vadimjustus Oct 27, 2017
bc2709c
MSI: Extend filtering logic for stock_source export
vadimjustus Oct 27, 2017
f132680
MSI: remove useless code
vadimjustus Oct 27, 2017
c063161
MSI: Refactore filtering logic in order to reduce complexity
vadimjustus Oct 27, 2017
03018f8
MSI: Fix intergration tests
vadimjustus Oct 28, 2017
d7a7256
MSI: Implement integration tests for stock_source export entity
vadimjustus Oct 28, 2017
7173a71
MSI: Refactore getAttributeCollection method for stock_sources export…
vadimjustus Oct 28, 2017
56317f4
MSI: Refactore filtering logic for stock_source export entity
vadimjustus Oct 28, 2017
de65ba5
MSI: Static improvements
vadimjustus Oct 28, 2017
fe30b3d
MSI: Refactore stock_source export entity dependencies
vadimjustus Oct 28, 2017
c16378f
MSI: Fix static code style issues
vadimjustus Oct 29, 2017
18ad378
MSI: Move SPI interfaces for export to model directory
vadimjustus Oct 29, 2017
d41337a
MSI: remove useless datetime filter processor
vadimjustus Oct 29, 2017
d70bb8f
MSI Import/Export: added comments for SPI interfaces
maghamed Oct 29, 2017
709ec81
Merge pull request #147 from magento-engcom/msi-source-export
maghamed Oct 29, 2017
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,72 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Inventory\Model\ResourceModel\SourceItem;

use Magento\Framework\App\ResourceConnection;
use Magento\Inventory\Model\ResourceModel\SourceItem as SourceItemResourceModel;
use Magento\InventoryApi\Api\Data\SourceItemInterface;

/**
* Implementation of SourceItem delete multiple operation for specific db layer
* Delete Multiple used here for performance efficient purposes over single delete operation
*/
class DeleteMultiple
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like we need to remove
SourceItemRepositoryInterface::delete(SourceItemInterface $sourceItem);
method, as we have
SourceItemsDeleteInterface::execute(array $sourceItems);

which do the same

Copy link
Member Author

@larsroettig larsroettig Oct 22, 2017

Choose a reason for hiding this comment

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

Hi @maghamed if we remove we will break the current delete function
behaviour. Because the current Delete Multiple removes all entries from the Database by only matching SKU.

If you mean it is okay I open for this an extra PR:
#140

{
/**
* @var ResourceConnection
*/
private $resourceConnection;

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

/**
* Multiple delete source items
*
* @param SourceItemInterface[] $sourceItems
* @return void
*/
public function execute(array $sourceItems)
{
if (!count($sourceItems)) {
return;
}

$connection = $this->resourceConnection->getConnection();
$tableName = $this->resourceConnection->getTableName(SourceItemResourceModel::TABLE_NAME_SOURCE_ITEM);

$sourceIds = $this->getSourceIds($sourceItems);
foreach ($sourceIds as $sourceId => $skuList) {
$whereCond = [
$connection->quoteInto(SourceItemInterface::SOURCE_ID . ' = ?', $sourceId),
$connection->quoteInto(SourceItemInterface::SKU . ' IN(?)', $skuList),
];
$connection->delete($tableName, $whereCond);
}
}

/**
* @param SourceItemInterface[] $sourceItems
* @return int[]
*/
private function getSourceIds($sourceItems): array
Copy link
Contributor

Choose a reason for hiding this comment

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

name of the method is not correct, as it returns source_id and sku list assigned to it

{
$sourceIds = [];

foreach ($sourceItems as $sourceItem) {
$sourceIds[$sourceItem->getSourceId()][] = $sourceItem->getSku();
}

return $sourceIds;
}
}
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.
*/

namespace Magento\Inventory\Model\SourceItem\Command;

use Magento\Framework\Exception\CouldNotDeleteException;
use Magento\Framework\Exception\InputException;
use Magento\Inventory\Model\ResourceModel\SourceItem\DeleteMultiple;
use Magento\InventoryApi\Api\SourceItemsDeleteInterface;
use Psr\Log\LoggerInterface;

/**
* @inheritdoc
*/
class SourceItemsDelete implements SourceItemsDeleteInterface
{
/**
* @var DeleteMultiple
*/
private $deleteMultiple;

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

/**
* @param DeleteMultiple $deleteMultiple
* @param LoggerInterface $logger
*/
public function __construct(
DeleteMultiple $deleteMultiple,
LoggerInterface $logger
) {
$this->deleteMultiple = $deleteMultiple;
$this->logger = $logger;
}

/**
* @inheritdoc
*/
public function execute(array $sourceItems)
{
if (empty($sourceItems)) {
throw new InputException(__('Input data is empty'));
}
Copy link
Contributor

Choose a reason for hiding this comment

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

not sure that we need to throw an exception when someone tries to delete empty array of source Items.

For example, you have a replace strategy and you want to delete existing SourceItems and substitute with new ones, but on clean Magento installation with empty DB - delete would have an empty set

try {
$this->deleteMultiple->execute($sourceItems);
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
throw new CouldNotDeleteException(__('Could not delete Source Item'), $e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Inventory\Model\ResourceModel\SourceItem;
Copy link
Contributor

Choose a reason for hiding this comment

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

all the tests should be stored under the namespace

Magento\Inventory\Test\Unit|Integration\Model

not mixed with code. Such tests would not be even run by Travis


use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\InventoryApi\Api\SourceItemRepositoryInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;

class DeleteMultipleTest extends TestCase
{

/**
* @var DeleteMultiple
*/
private $deleteModel;

/**
* @var SourceItemRepositoryInterface
*/
private $sourceItemRepository;

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

protected function setUp()
{
$this->deleteModel = Bootstrap::getObjectManager()->create(DeleteMultiple::class);
$this->sourceItemRepository = Bootstrap::getObjectManager()->create(SourceItemRepositoryInterface::class);
$this->searchCriteriaBuilder = Bootstrap::getObjectManager()->create(SearchCriteriaBuilder::class);
}

/**
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
*/
public function testDeleteMultipleWithEmptySourceItems()
{
$expectedCount = count($this->getSourceItems());

$this->deleteModel->execute([]);

$this->assertCount($expectedCount, $this->getSourceItems());
}

/**
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
*/
public function testDeleteMultipleForTwoItems()
{
$sourceItems = $this->getSourceItems();
$expectedCount = count($sourceItems) - 2;
$itemsToDelete = array_slice($sourceItems, 0, 2);
$expectedResult = array_slice($sourceItems, 2);

$this->deleteModel->execute($itemsToDelete);

$result = array_values($this->getSourceItems());
$this->assertCount($expectedCount, $result);
$this->assertEquals($expectedResult, $result);
}

/**
* @return \Magento\InventoryApi\Api\Data\SourceItemInterface[]
*/
private function getSourceItems(): array
{
$searchCriteria = $this->searchCriteriaBuilder->create();
return $this->sourceItemRepository->getList($searchCriteria)->getItems();
}
}
1 change: 1 addition & 0 deletions app/code/Magento/Inventory/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<!-- Source Item -->
<preference for="Magento\InventoryApi\Api\SourceItemRepositoryInterface" type="Magento\Inventory\Model\SourceItemRepository"/>
<preference for="Magento\InventoryApi\Api\SourceItemsSaveInterface" type="Magento\Inventory\Model\SourceItem\Command\SourceItemsSave"/>
<preference for="Magento\InventoryApi\Api\SourceItemsDeleteInterface" type="Magento\Inventory\Model\SourceItem\Command\SourceItemsDelete"/>
<preference for="Magento\InventoryApi\Api\Data\SourceItemInterface" type="Magento\Inventory\Model\SourceItem"/>
<preference for="Magento\InventoryApi\Api\Data\SourceItemSearchResultsInterface" type="Magento\Inventory\Model\SourceItemSearchResults"/>
<preference for="Magento\Inventory\Model\SourceItem\Command\DeleteInterface" type="Magento\Inventory\Model\SourceItem\Command\Delete"/>
Expand Down
28 changes: 28 additions & 0 deletions app/code/Magento/InventoryApi/Api/SourceItemsDeleteInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\InventoryApi\Api;
Copy link
Contributor

Choose a reason for hiding this comment

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

don't forget to use strict typing for newly created code


/**
* Service method for source items delete multiple
* Performance efficient API, used for stock synchronization
*
* Used fully qualified namespaces in annotations for proper work of WebApi request parser
*
* @api
*/
interface SourceItemsDeleteInterface
{
/**
* Delete Multiple Source item data
*
* @param \Magento\InventoryApi\Api\Data\SourceItemInterface[] $sourceItems
* @return void
* @throws \Magento\Framework\Exception\InputException
* @throws \Magento\Framework\Exception\CouldNotSaveException
*/
public function execute(array $sourceItems);
}
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\InventoryImportExport\Helper;

use Magento\InventoryApi\Api\Data\SourceItemInterface;
use Magento\InventoryApi\Api\Data\SourceItemInterfaceFactory;
use Magento\InventoryImportExport\Model\Import\Sources;

class SourceItemConvert
Copy link
Contributor

Choose a reason for hiding this comment

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

no need to Introduce Helper in the new Code.

Helpers should be discouraged in newly created code.
You could create SourceItemConvert under the Model namespace instead

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

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

/**
* Converts a data in sourceItem list.
* @param array $rowData
* @return SourceItemInterface[]
*/
public function convert(array $bunch): array
{
$sourceItems = [];
foreach ($bunch as $rowData) {
/** @var SourceItemInterface $sourceItem */
$sourceItem = $this->sourceItemFactory->create();
$sourceItem->setSourceId($rowData[Sources::COL_SOURCE]);
$sourceItem->setSku($rowData[Sources::COL_SKU]);
$sourceItem->setQuantity($rowData[Sources::COL_QTY]);

$status = (int)$rowData[Sources::COL_QTY] > 0;
if (isset($rowData[Sources::COL_STATUS])) {
$status = (int)$rowData[Sources::COL_STATUS];
}
$sourceItem->setStatus($status);

$sourceItems[] = $sourceItem;
}

return $sourceItems;
}
}
48 changes: 48 additions & 0 deletions app/code/Magento/InventoryImportExport/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

Open Software License ("OSL") v. 3.0

This Open Software License (the "License") applies to any original work of authorship (the "Original Work") whose owner (the "Licensor") has placed the following licensing notice adjacent to the copyright notice for the Original Work:

Licensed under the Open Software License version 3.0

1. Grant of Copyright License. Licensor grants You a worldwide, royalty-free, non-exclusive, sublicensable license, for the duration of the copyright, to do the following:

1. to reproduce the Original Work in copies, either alone or as part of a collective work;

2. to translate, adapt, alter, transform, modify, or arrange the Original Work, thereby creating derivative works ("Derivative Works") based upon the Original Work;

3. to distribute or communicate copies of the Original Work and Derivative Works to the public, with the proviso that copies of Original Work or Derivative Works that You distribute or communicate shall be licensed under this Open Software License;

4. to perform the Original Work publicly; and

5. to display the Original Work publicly.

2. Grant of Patent License. Licensor grants You a worldwide, royalty-free, non-exclusive, sublicensable license, under patent claims owned or controlled by the Licensor that are embodied in the Original Work as furnished by the Licensor, for the duration of the patents, to make, use, sell, offer for sale, have made, and import the Original Work and Derivative Works.

3. Grant of Source Code License. The term "Source Code" means the preferred form of the Original Work for making modifications to it and all available documentation describing how to modify the Original Work. Licensor agrees to provide a machine-readable copy of the Source Code of the Original Work along with each copy of the Original Work that Licensor distributes. Licensor reserves the right to satisfy this obligation by placing a machine-readable copy of the Source Code in an information repository reasonably calculated to permit inexpensive and convenient access by You for as long as Licensor continues to distribute the Original Work.

4. Exclusions From License Grant. Neither the names of Licensor, nor the names of any contributors to the Original Work, nor any of their trademarks or service marks, may be used to endorse or promote products derived from this Original Work without express prior permission of the Licensor. Except as expressly stated herein, nothing in this License grants any license to Licensor's trademarks, copyrights, patents, trade secrets or any other intellectual property. No patent license is granted to make, use, sell, offer for sale, have made, or import embodiments of any patent claims other than the licensed claims defined in Section 2. No license is granted to the trademarks of Licensor even if such marks are included in the Original Work. Nothing in this License shall be interpreted to prohibit Licensor from licensing under terms different from this License any Original Work that Licensor otherwise would have a right to license.

5. External Deployment. The term "External Deployment" means the use, distribution, or communication of the Original Work or Derivative Works in any way such that the Original Work or Derivative Works may be used by anyone other than You, whether those works are distributed or communicated to those persons or made available as an application intended for use over a network. As an express condition for the grants of license hereunder, You must treat any External Deployment by You of the Original Work or a Derivative Work as a distribution under section 1(c).

6. Attribution Rights. You must retain, in the Source Code of any Derivative Works that You create, all copyright, patent, or trademark notices from the Source Code of the Original Work, as well as any notices of licensing and any descriptive text identified therein as an "Attribution Notice." You must cause the Source Code for any Derivative Works that You create to carry a prominent Attribution Notice reasonably calculated to inform recipients that You have modified the Original Work.

7. Warranty of Provenance and Disclaimer of Warranty. Licensor warrants that the copyright in and to the Original Work and the patent rights granted herein by Licensor are owned by the Licensor or are sublicensed to You under the terms of this License with the permission of the contributor(s) of those copyrights and patent rights. Except as expressly stated in the immediately preceding sentence, the Original Work is provided under this License on an "AS IS" BASIS and WITHOUT WARRANTY, either express or implied, including, without limitation, the warranties of non-infringement, merchantability or fitness for a particular purpose. THE ENTIRE RISK AS TO THE QUALITY OF THE ORIGINAL WORK IS WITH YOU. This DISCLAIMER OF WARRANTY constitutes an essential part of this License. No license to the Original Work is granted by this License except under this disclaimer.

8. Limitation of Liability. Under no circumstances and under no legal theory, whether in tort (including negligence), contract, or otherwise, shall the Licensor be liable to anyone for any indirect, special, incidental, or consequential damages of any character arising as a result of this License or the use of the Original Work including, without limitation, damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses. This limitation of liability shall not apply to the extent applicable law prohibits such limitation.

9. Acceptance and Termination. If, at any time, You expressly assented to this License, that assent indicates your clear and irrevocable acceptance of this License and all of its terms and conditions. If You distribute or communicate copies of the Original Work or a Derivative Work, You must make a reasonable effort under the circumstances to obtain the express assent of recipients to the terms of this License. This License conditions your rights to undertake the activities listed in Section 1, including your right to create Derivative Works based upon the Original Work, and doing so without honoring these terms and conditions is prohibited by copyright law and international treaty. Nothing in this License is intended to affect copyright exceptions and limitations (including 'fair use' or 'fair dealing'). This License shall terminate immediately and You may no longer exercise any of the rights granted to You by this License upon your failure to honor the conditions in Section 1(c).

10. Termination for Patent Action. This License shall terminate automatically and You may no longer exercise any of the rights granted to You by this License as of the date You commence an action, including a cross-claim or counterclaim, against Licensor or any licensee alleging that the Original Work infringes a patent. This termination provision shall not apply for an action alleging patent infringement by combinations of the Original Work with other software or hardware.

11. Jurisdiction, Venue and Governing Law. Any action or suit relating to this License may be brought only in the courts of a jurisdiction wherein the Licensor resides or in which Licensor conducts its primary business, and under the laws of that jurisdiction excluding its conflict-of-law provisions. The application of the United Nations Convention on Contracts for the International Sale of Goods is expressly excluded. Any use of the Original Work outside the scope of this License or after its termination shall be subject to the requirements and penalties of copyright or patent law in the appropriate jurisdiction. This section shall survive the termination of this License.

12. Attorneys' Fees. In any action to enforce the terms of this License or seeking damages relating thereto, the prevailing party shall be entitled to recover its costs and expenses, including, without limitation, reasonable attorneys' fees and costs incurred in connection with such action, including any appeal of such action. This section shall survive the termination of this License.

13. Miscellaneous. If any provision of this License is held to be unenforceable, such provision shall be reformed only to the extent necessary to make it enforceable.

14. Definition of "You" in This License. "You" throughout this License, whether in upper or lower case, means an individual or a legal entity exercising rights under, and complying with all of the terms of, this License. For legal entities, "You" includes any entity that controls, is controlled by, or is under common control with you. For purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.

15. Right to Use. You may use the Original Work in all ways not otherwise restricted or conditioned by this License or by law, and Licensor promises not to interfere with or be responsible for such uses by You.

16. Modification of This License. This License is Copyright (C) 2005 Lawrence Rosen. Permission is granted to copy, distribute, or communicate this License without modification. Nothing in this License permits You to modify this License as applied to the Original Work or to Derivative Works. However, You may modify the text of this License and copy, distribute or communicate your modified version (the "Modified License") and apply it to other original works of authorship subject to the following conditions: (i) You may not indicate in any way that your Modified License is the "Open Software License" or "OSL" and you may not use those names in the name of your Modified License; (ii) You must replace the notice specified in the first paragraph above with the notice "Licensed under <insert your license name here>" or with a notice of your own that is not confusingly similar to the notice in this License; and (iii) You may not claim that your original works are open source software unless your Modified License has been approved by Open Source Initiative (OSI) and You comply with its license review and certification process.
Loading