From f1c834c0911a3cd1aa0efeb82c45513fa67d814b Mon Sep 17 00:00:00 2001 From: Vadim Justus Date: Fri, 29 Sep 2017 14:44:16 +0200 Subject: [PATCH 01/37] MSI: Initial implementation of Magento_InventoryImportExport module --- .../Magento/InventoryImportExport/LICENSE.txt | 48 +++++++++++++++++++ .../InventoryImportExport/LICENSE_AFL.txt | 48 +++++++++++++++++++ .../Magento/InventoryImportExport/README.md | 6 +++ .../InventoryImportExport/composer.json | 28 +++++++++++ .../InventoryImportExport/etc/module.xml | 10 ++++ .../InventoryImportExport/registration.php | 11 +++++ 6 files changed, 151 insertions(+) create mode 100644 app/code/Magento/InventoryImportExport/LICENSE.txt create mode 100644 app/code/Magento/InventoryImportExport/LICENSE_AFL.txt create mode 100644 app/code/Magento/InventoryImportExport/README.md create mode 100644 app/code/Magento/InventoryImportExport/composer.json create mode 100644 app/code/Magento/InventoryImportExport/etc/module.xml create mode 100644 app/code/Magento/InventoryImportExport/registration.php diff --git a/app/code/Magento/InventoryImportExport/LICENSE.txt b/app/code/Magento/InventoryImportExport/LICENSE.txt new file mode 100644 index 000000000000..49525fd99da9 --- /dev/null +++ b/app/code/Magento/InventoryImportExport/LICENSE.txt @@ -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 " 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. \ No newline at end of file diff --git a/app/code/Magento/InventoryImportExport/LICENSE_AFL.txt b/app/code/Magento/InventoryImportExport/LICENSE_AFL.txt new file mode 100644 index 000000000000..f39d641b18a1 --- /dev/null +++ b/app/code/Magento/InventoryImportExport/LICENSE_AFL.txt @@ -0,0 +1,48 @@ + +Academic Free License ("AFL") v. 3.0 + +This Academic Free 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 Academic Free 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, under any license of your choice that does not contradict the terms and conditions, including Licensor's reserved rights and remedies, in this Academic Free 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 © 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 "Academic Free License" or "AFL" 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 " 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. diff --git a/app/code/Magento/InventoryImportExport/README.md b/app/code/Magento/InventoryImportExport/README.md new file mode 100644 index 000000000000..5b90238246a4 --- /dev/null +++ b/app/code/Magento/InventoryImportExport/README.md @@ -0,0 +1,6 @@ +# InventoryCatalog + +**InventoryCatalog** provides implementation for bridge between **Inventory** and **Catalog**. +This is dedicated module which add logic to Default Role of the Source on installations with single source/stock. +See [concept documentation](https://github.com/magento-engcom/magento2/wiki/Technical-Vision.-Catalog-Inventory) +for further information. diff --git a/app/code/Magento/InventoryImportExport/composer.json b/app/code/Magento/InventoryImportExport/composer.json new file mode 100644 index 000000000000..90fa845f6d9c --- /dev/null +++ b/app/code/Magento/InventoryImportExport/composer.json @@ -0,0 +1,28 @@ +{ + "name": "magento/module-inventory-import-export", + "description": "N/A", + "require": { + "php": "7.0.2|7.0.4|~7.0.6|~7.1.0", + "magento/framework": "100.2.*", + "magento/module-inventory-api": "100.0.*", + "magento/module-backend": "100.2.*", + "magento/module-catalog": "100.2.*", + "magento/module-directory": "100.2.*", + "magento/module-shipping": "100.2.*", + "magento/module-ui": "100.2.*" + }, + "type": "magento2-module", + "version": "100.0.0-dev", + "license": [ + "OSL-3.0", + "AFL-3.0" + ], + "autoload": { + "files": [ + "registration.php" + ], + "psr-4": { + "Magento\\InventoryCatalog\\": "" + } + } +} diff --git a/app/code/Magento/InventoryImportExport/etc/module.xml b/app/code/Magento/InventoryImportExport/etc/module.xml new file mode 100644 index 000000000000..56c03395b1d4 --- /dev/null +++ b/app/code/Magento/InventoryImportExport/etc/module.xml @@ -0,0 +1,10 @@ + + + + + diff --git a/app/code/Magento/InventoryImportExport/registration.php b/app/code/Magento/InventoryImportExport/registration.php new file mode 100644 index 000000000000..9b2496816f1a --- /dev/null +++ b/app/code/Magento/InventoryImportExport/registration.php @@ -0,0 +1,11 @@ + Date: Sun, 1 Oct 2017 14:11:32 +0200 Subject: [PATCH 02/37] MSI: Implement append bahaviour import for source items --- .../Model/Import/Command/Append.php | 72 +++++++++ .../Model/Import/Command/CommandException.php | 16 ++ .../Model/Import/Command/CommandInterface.php | 20 +++ .../Model/Import/Command/Delete.php | 21 +++ .../Model/Import/Command/Replace.php | 21 +++ .../Model/Import/Serializer/Json.php | 79 ++++++++++ .../Serializer/LegacyJsonHelperInterface.php | 32 ++++ .../Model/Import/Sources.php | 144 ++++++++++++++++++ .../Model/Import/Validator/QtyValidator.php | 47 ++++++ .../Model/Import/Validator/SkuValidator.php | 47 ++++++ .../Import/Validator/SourceValidator.php | 86 +++++++++++ .../Model/Import/Validator/ValidatorChain.php | 65 ++++++++ .../Import/Validator/ValidatorInterface.php | 24 +++ .../InventoryImportExport/composer.json | 2 +- .../Magento/InventoryImportExport/etc/di.xml | 30 ++++ .../InventoryImportExport/etc/import.xml | 10 ++ 16 files changed, 715 insertions(+), 1 deletion(-) create mode 100644 app/code/Magento/InventoryImportExport/Model/Import/Command/Append.php create mode 100644 app/code/Magento/InventoryImportExport/Model/Import/Command/CommandException.php create mode 100644 app/code/Magento/InventoryImportExport/Model/Import/Command/CommandInterface.php create mode 100644 app/code/Magento/InventoryImportExport/Model/Import/Command/Delete.php create mode 100644 app/code/Magento/InventoryImportExport/Model/Import/Command/Replace.php create mode 100644 app/code/Magento/InventoryImportExport/Model/Import/Serializer/Json.php create mode 100644 app/code/Magento/InventoryImportExport/Model/Import/Serializer/LegacyJsonHelperInterface.php create mode 100644 app/code/Magento/InventoryImportExport/Model/Import/Sources.php create mode 100644 app/code/Magento/InventoryImportExport/Model/Import/Validator/QtyValidator.php create mode 100644 app/code/Magento/InventoryImportExport/Model/Import/Validator/SkuValidator.php create mode 100644 app/code/Magento/InventoryImportExport/Model/Import/Validator/SourceValidator.php create mode 100644 app/code/Magento/InventoryImportExport/Model/Import/Validator/ValidatorChain.php create mode 100644 app/code/Magento/InventoryImportExport/Model/Import/Validator/ValidatorInterface.php create mode 100644 app/code/Magento/InventoryImportExport/etc/di.xml create mode 100644 app/code/Magento/InventoryImportExport/etc/import.xml diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Command/Append.php b/app/code/Magento/InventoryImportExport/Model/Import/Command/Append.php new file mode 100644 index 000000000000..51ae14440a59 --- /dev/null +++ b/app/code/Magento/InventoryImportExport/Model/Import/Command/Append.php @@ -0,0 +1,72 @@ +sourceItemFactory = $sourceItemFactory; + $this->sourceItemsSave = $sourceItemsSave; + } + + /** + * @inheritdoc + */ + public function execute(array $bunch) + { + $sourceItems = []; + foreach ($bunch as $rowNum => $rowData) { + $sourceItems[] = $this->buildSourceItem($rowData); + } + $this->sourceItemsSave->execute($sourceItems); + } + + /** + * @param array $rowData + * @return SourceItemInterface + */ + private function buildSourceItem(array $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); + + return $sourceItem; + } +} diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Command/CommandException.php b/app/code/Magento/InventoryImportExport/Model/Import/Command/CommandException.php new file mode 100644 index 000000000000..3e0f2dc638d9 --- /dev/null +++ b/app/code/Magento/InventoryImportExport/Model/Import/Command/CommandException.php @@ -0,0 +1,16 @@ +serializer = $serializer; + } + + /** + * Encode the mixed $valueToEncode into the JSON format + * + * @param mixed $valueToEncode + * @return string + */ + public function jsonEncode($valueToEncode) + { + return $this->serializer->serialize($valueToEncode); + } + + /** + * Decodes the given $encodedValue string which is + * encoded in the JSON format + * + * @param string $encodedValue + * @return mixed + */ + public function jsonDecode($encodedValue) + { + return $this->serializer->unserialize($encodedValue); + } + + /** + * Serialize data into string + * + * @param string|int|float|bool|array|null $data + * @return string|bool + * @throws \InvalidArgumentException + * @since 100.2.0 + */ + public function serialize($data) + { + return $this->serializer->serialize($data); + } + + /** + * Unserialize the given string + * + * @param string $string + * @return string|int|float|bool|array|null + * @throws \InvalidArgumentException + * @since 100.2.0 + */ + public function unserialize($string) + { + return $this->serializer->unserialize($string); + } +} diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Serializer/LegacyJsonHelperInterface.php b/app/code/Magento/InventoryImportExport/Model/Import/Serializer/LegacyJsonHelperInterface.php new file mode 100644 index 000000000000..864ab2b394b0 --- /dev/null +++ b/app/code/Magento/InventoryImportExport/Model/Import/Serializer/LegacyJsonHelperInterface.php @@ -0,0 +1,32 @@ +jsonHelper = $jsonHelper; + $this->errorAggregator = $errorAggregator; + $this->_resourceHelper = $resourceHelper; + $this->_importExportData = $dataHelper; + $this->_dataSourceModel = $importData; + $this->validator = $validator; + + foreach ($commands as $command) { + if (!$command instanceof CommandInterface) { + throw new LocalizedException( + __('Source Import Commands must implement %interface.', ['interface' => CommandInterface::class]) + ); + } + } + $this->commands = $commands; + } + + /** + * Import data rows. + * + * @return boolean + */ + protected function _importData() + { + $command = $this->getCommandByBehavior($this->getBehavior()); + while ($bunch = $this->_dataSourceModel->getNextBunch()) { + $command->execute($bunch); + } + + return true; + } + + /** + * @param string $behavior + * @return CommandInterface + * @throws LocalizedException + */ + public function getCommandByBehavior($behavior) + { + if (!isset($this->commands[$behavior])) { + throw new LocalizedException( + __('There is no command registered for behavior "%behavior".', ['behavior' => $behavior]) + ); + } + + return $this->commands[$behavior]; + } + + /** + * EAV entity type code getter. + * + * @return string + */ + public function getEntityTypeCode() + { + return 'stock_sources'; + } + + /** + * Validate data row. + * + * @param array $rowData + * @param int $rowNum + * @return boolean + */ + public function validateRow(array $rowData, $rowNum) + { + $result = $this->validator->validate($rowData, $rowNum); + if ($result->isValid()) { + return true; + } + + foreach ($result->getErrors() as $error) { + $this->addRowError($error, $rowNum); + } + + return false; + } +} diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Validator/QtyValidator.php b/app/code/Magento/InventoryImportExport/Model/Import/Validator/QtyValidator.php new file mode 100644 index 000000000000..2771a8630c0e --- /dev/null +++ b/app/code/Magento/InventoryImportExport/Model/Import/Validator/QtyValidator.php @@ -0,0 +1,47 @@ +validationResultFactory = $validationResultFactory; + } + + /** + * @inheritdoc + */ + public function validate(array $rowData, $rowNumber) + { + $errors = []; + + if (!isset($rowData[Sources::COL_QTY])) { + $errors[] = __('Missing required column "%column"', ['column' => Sources::COL_QTY]); + } + + return $this->validationResultFactory->create(['errors' => $errors]); + } +} diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Validator/SkuValidator.php b/app/code/Magento/InventoryImportExport/Model/Import/Validator/SkuValidator.php new file mode 100644 index 000000000000..97de46dbfe42 --- /dev/null +++ b/app/code/Magento/InventoryImportExport/Model/Import/Validator/SkuValidator.php @@ -0,0 +1,47 @@ +validationResultFactory = $validationResultFactory; + } + + /** + * @inheritdoc + */ + public function validate(array $rowData, $rowNumber) + { + $errors = []; + + if (!isset($rowData[Sources::COL_SKU])) { + $errors[] = __('Missing required column "%column"', ['column' => Sources::COL_SKU]); + } + + return $this->validationResultFactory->create(['errors' => $errors]); + } +} diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Validator/SourceValidator.php b/app/code/Magento/InventoryImportExport/Model/Import/Validator/SourceValidator.php new file mode 100644 index 000000000000..c8644464d4e8 --- /dev/null +++ b/app/code/Magento/InventoryImportExport/Model/Import/Validator/SourceValidator.php @@ -0,0 +1,86 @@ +validationResultFactory = $validationResultFactory; + $this->sourceRepository = $sourceRepository; + $this->loadSourceIds(); + } + + /** + * @inheritdoc + */ + public function validate(array $rowData, $rowNumber) + { + $errors = []; + + if (!isset($rowData[Sources::COL_SOURCE])) { + $errors[] = __('Missing required column "%column"', ['column' => Sources::COL_SOURCE]); + } elseif (!$this->isExistingSource($rowData[Sources::COL_SOURCE])) { + $errors[] = __('Source id "%id" does not exists', ['id' => $rowData[Sources::COL_SOURCE]]); + } + + return $this->validationResultFactory->create(['errors' => $errors]); + } + + /** + * @param int $sourceId + * @return bool + */ + private function isExistingSource($sourceId) + { + return isset($this->sourceIds[$sourceId]); + } + + /** + * Loads all existing source ids + * @return void + */ + private function loadSourceIds() + { + $sources = $this->sourceRepository->getList(); + foreach ($sources->getItems() as $source) { + $sourceId = $source->getSourceId(); + $this->sourceIds[$sourceId] = $sourceId; + } + } +} diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Validator/ValidatorChain.php b/app/code/Magento/InventoryImportExport/Model/Import/Validator/ValidatorChain.php new file mode 100644 index 000000000000..537f4abed679 --- /dev/null +++ b/app/code/Magento/InventoryImportExport/Model/Import/Validator/ValidatorChain.php @@ -0,0 +1,65 @@ +validationResultFactory = $validationResultFactory; + + foreach ($validators as $validator) { + if (!$validator instanceof ValidatorInterface) { + throw new LocalizedException( + __('Row Validator must implement %interface.', ['interface' => ValidatorInterface::class]) + ); + } + } + $this->validators = $validators; + } + + /** + * @inheritdoc + */ + public function validate(array $rowData, $rowNumber) + { + $errors = []; + foreach ($this->validators as $validator) { + $validationResult = $validator->validate($rowData, $rowNumber); + + if (!$validationResult->isValid()) { + $errors = array_merge($errors, $validationResult->getErrors()); + } + } + return $this->validationResultFactory->create(['errors' => $errors]); + } +} diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Validator/ValidatorInterface.php b/app/code/Magento/InventoryImportExport/Model/Import/Validator/ValidatorInterface.php new file mode 100644 index 000000000000..defc1f64c1db --- /dev/null +++ b/app/code/Magento/InventoryImportExport/Model/Import/Validator/ValidatorInterface.php @@ -0,0 +1,24 @@ + + + + + + + + Magento\InventoryImportExport\Model\Import\Validator\SkuValidator + Magento\InventoryImportExport\Model\Import\Validator\SourceValidator + Magento\InventoryImportExport\Model\Import\Validator\QtyValidator + + + + + + Magento\InventoryImportExport\Model\Import\Serializer\Json + Magento\InventoryImportExport\Model\Import\Validator\ValidatorChain + + Magento\InventoryImportExport\Model\Import\Command\Delete + Magento\InventoryImportExport\Model\Import\Command\Append + Magento\InventoryImportExport\Model\Import\Command\Replace + + + + diff --git a/app/code/Magento/InventoryImportExport/etc/import.xml b/app/code/Magento/InventoryImportExport/etc/import.xml new file mode 100644 index 000000000000..283b4dce0a5b --- /dev/null +++ b/app/code/Magento/InventoryImportExport/etc/import.xml @@ -0,0 +1,10 @@ + + + + + From 8dcbe3413719bbfdba8f52f0ee8669c2d2dd206a Mon Sep 17 00:00:00 2001 From: larsroettig Date: Tue, 3 Oct 2017 15:27:04 +0200 Subject: [PATCH 03/37] [task] #115 Refactoring - Add Documentation - phpcs fixes - Impelement phpunit test for ValidatorChain --- .../Model/Import/Command/Append.php | 1 + .../Model/Import/Command/CommandInterface.php | 4 + .../Model/Import/Serializer/Json.php | 5 +- .../Import/Validator/SourceValidator.php | 6 +- .../Model/Import/Validator/ValidatorChain.php | 12 +- .../Import/Validator/ValidatorInterface.php | 2 +- .../Test/Unit/Model/ValidatorChainTest.php | 127 ++++++++++++++++++ 7 files changed, 145 insertions(+), 12 deletions(-) create mode 100644 app/code/Magento/InventoryImportExport/Test/Unit/Model/ValidatorChainTest.php diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Command/Append.php b/app/code/Magento/InventoryImportExport/Model/Import/Command/Append.php index 51ae14440a59..530fcce22a68 100644 --- a/app/code/Magento/InventoryImportExport/Model/Import/Command/Append.php +++ b/app/code/Magento/InventoryImportExport/Model/Import/Command/Append.php @@ -5,6 +5,7 @@ */ namespace Magento\InventoryImportExport\Model\Import\Command; + use Magento\InventoryApi\Api\Data\SourceItemInterface; use Magento\InventoryApi\Api\Data\SourceItemInterfaceFactory; use Magento\InventoryApi\Api\SourceItemsSaveInterface; diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Command/CommandInterface.php b/app/code/Magento/InventoryImportExport/Model/Import/Command/CommandInterface.php index efc9b5cb05b5..7bb5a73bed0a 100644 --- a/app/code/Magento/InventoryImportExport/Model/Import/Command/CommandInterface.php +++ b/app/code/Magento/InventoryImportExport/Model/Import/Command/CommandInterface.php @@ -7,11 +7,15 @@ namespace Magento\InventoryImportExport\Model\Import\Command; /** + * It is extension point to implement import/export functionality (Service Provider Interface - SPI) + * * @api */ interface CommandInterface { /** + * Executes the current command. + * * @param array $bunch * @return void * @throws CommandException diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Serializer/Json.php b/app/code/Magento/InventoryImportExport/Model/Import/Serializer/Json.php index 2a9b52bf8a39..0a56563488e8 100644 --- a/app/code/Magento/InventoryImportExport/Model/Import/Serializer/Json.php +++ b/app/code/Magento/InventoryImportExport/Model/Import/Serializer/Json.php @@ -22,9 +22,8 @@ class Json implements SerializerInterface, LegacyJsonHelperInterface * Json constructor. * @param SerializerInterface $serializer */ - public function __construct( - SerializerInterface $serializer - ) { + public function __construct(SerializerInterface $serializer) + { $this->serializer = $serializer; } diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Validator/SourceValidator.php b/app/code/Magento/InventoryImportExport/Model/Import/Validator/SourceValidator.php index c8644464d4e8..efbce923cddc 100644 --- a/app/code/Magento/InventoryImportExport/Model/Import/Validator/SourceValidator.php +++ b/app/code/Magento/InventoryImportExport/Model/Import/Validator/SourceValidator.php @@ -6,7 +6,6 @@ namespace Magento\InventoryImportExport\Model\Import\Validator; -use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Validation\ValidationResultFactory; use Magento\InventoryApi\Api\SourceRepositoryInterface; use Magento\InventoryImportExport\Model\Import\Sources; @@ -63,16 +62,19 @@ public function validate(array $rowData, $rowNumber) } /** + * Returns exits already the source in sources. + * * @param int $sourceId * @return bool */ - private function isExistingSource($sourceId) + private function isExistingSource($sourceId): bool { return isset($this->sourceIds[$sourceId]); } /** * Loads all existing source ids + * * @return void */ private function loadSourceIds() diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Validator/ValidatorChain.php b/app/code/Magento/InventoryImportExport/Model/Import/Validator/ValidatorChain.php index 537f4abed679..d93fb69bc5a2 100644 --- a/app/code/Magento/InventoryImportExport/Model/Import/Validator/ValidatorChain.php +++ b/app/code/Magento/InventoryImportExport/Model/Import/Validator/ValidatorChain.php @@ -10,9 +10,7 @@ use Magento\Framework\Validation\ValidationResultFactory; /** - * Extension point for row validation - * - * @api + * @inheritdoc */ class ValidatorChain implements ValidatorInterface { @@ -52,14 +50,16 @@ public function __construct( */ public function validate(array $rowData, $rowNumber) { - $errors = []; + /* the inner empty array covers cases when no loops were made */ + $errors = [[]]; foreach ($this->validators as $validator) { $validationResult = $validator->validate($rowData, $rowNumber); if (!$validationResult->isValid()) { - $errors = array_merge($errors, $validationResult->getErrors()); + $errors[] = $validationResult->getErrors(); } } - return $this->validationResultFactory->create(['errors' => $errors]); + + return $this->validationResultFactory->create(['errors' => array_merge(...$errors)]); } } diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Validator/ValidatorInterface.php b/app/code/Magento/InventoryImportExport/Model/Import/Validator/ValidatorInterface.php index defc1f64c1db..0e52ccddac44 100644 --- a/app/code/Magento/InventoryImportExport/Model/Import/Validator/ValidatorInterface.php +++ b/app/code/Magento/InventoryImportExport/Model/Import/Validator/ValidatorInterface.php @@ -9,7 +9,7 @@ use Magento\Framework\Validation\ValidationResult; /** - * Extension point for row validation + * Extension point for row validation (Service Provider Interface - SPI) * * @api */ diff --git a/app/code/Magento/InventoryImportExport/Test/Unit/Model/ValidatorChainTest.php b/app/code/Magento/InventoryImportExport/Test/Unit/Model/ValidatorChainTest.php new file mode 100644 index 000000000000..fe739c5deaef --- /dev/null +++ b/app/code/Magento/InventoryImportExport/Test/Unit/Model/ValidatorChainTest.php @@ -0,0 +1,127 @@ +validationResultFactory = $this->getMockBuilder(ValidationResultFactory::class)->getMock(); + $this->qtyValidator = $this->getMockBuilder(ValidatorInterface::class)->getMock(); + $this->skuValidator = $this->getMockBuilder(ValidatorInterface::class)->getMock(); + } + + public function testValidateWithOutValidators() + { + $emptyValidatorResult = $this->createMock(\Magento\Framework\Validation\ValidationResult::class); + $this->validationResultFactory->expects($this->once()) + ->method('create') + ->with(['errors' =>[]]) + ->willReturn($emptyValidatorResult); + + $this->validatorChain = (new ObjectManager($this))->getObject( + ValidatorChain::class, + [ + 'validationResultFactory' => $this->validationResultFactory, + 'validators' => [] + ] + ); + + + $result = $this->validatorChain->validate([], 1); + $this->assertEquals($emptyValidatorResult, $result); + } + + public function testValidateWithOutErros() + { + $emptyValidatorResult = $this->createMock(\Magento\Framework\Validation\ValidationResult::class); + $emptyValidatorResult->expects($this->once())->method('isValid') + ->willReturn(true); + + $this->validationResultFactory->expects($this->once()) + ->method('create') + ->with(['errors' => []]) + ->willReturn($emptyValidatorResult); + + $this->qtyValidator->method('validate') + ->willReturn($emptyValidatorResult); + + $this->validatorChain = (new ObjectManager($this))->getObject( + ValidatorChain::class, + [ + 'validationResultFactory' => $this->validationResultFactory, + 'validators' => [$this->qtyValidator] + ] + ); + + $result = $this->validatorChain->validate([], 1); + $this->assertEquals($emptyValidatorResult, $result); + } + + + public function testValidateWithErros() + { + $validatorResult = $this->createMock(\Magento\Framework\Validation\ValidationResult::class); + + $validatorResult->expects($this->once())->method('isValid') + ->willReturn(false); + + $validatorResult->expects($this->once()) + ->method('getErrors') + ->willReturn(['Qty can not negative', 'Additional error']); + + $this->qtyValidator->expects($this->once())->method('validate') + ->willReturn($validatorResult); + + $this->validationResultFactory->expects($this->once()) + ->method('create') + ->with(['errors' => ['Qty can not negative', 'Additional error']]) + ->willReturn($validatorResult); + + + $this->validatorChain = (new ObjectManager($this))->getObject( + ValidatorChain::class, + [ + 'validationResultFactory' => $this->validationResultFactory, + 'validators' => [$this->qtyValidator] + ] + ); + + $result = $this->validatorChain->validate([-1], 1); + $this->assertEquals($validatorResult, $result); + } +} From 24f776ec3270f0210b3d7793fc02d8ae8b2635a0 Mon Sep 17 00:00:00 2001 From: Vadim Justus Date: Tue, 3 Oct 2017 20:23:08 +0200 Subject: [PATCH 04/37] MSI: Change method accessability to private --- app/code/Magento/InventoryImportExport/Model/Import/Sources.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Sources.php b/app/code/Magento/InventoryImportExport/Model/Import/Sources.php index fdad7da763ba..a2e151e8cc4a 100644 --- a/app/code/Magento/InventoryImportExport/Model/Import/Sources.php +++ b/app/code/Magento/InventoryImportExport/Model/Import/Sources.php @@ -100,7 +100,7 @@ protected function _importData() * @return CommandInterface * @throws LocalizedException */ - public function getCommandByBehavior($behavior) + private function getCommandByBehavior($behavior) { if (!isset($this->commands[$behavior])) { throw new LocalizedException( From 058d08ce66a278fae80c607ae81144c65bf4bff0 Mon Sep 17 00:00:00 2001 From: Vadim Justus Date: Tue, 3 Oct 2017 20:23:49 +0200 Subject: [PATCH 05/37] MSI: Implement intergration test for import entity --- .../Integration/Model/Import/SourcesTest.php | 166 ++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 app/code/Magento/InventoryImportExport/Test/Integration/Model/Import/SourcesTest.php diff --git a/app/code/Magento/InventoryImportExport/Test/Integration/Model/Import/SourcesTest.php b/app/code/Magento/InventoryImportExport/Test/Integration/Model/Import/SourcesTest.php new file mode 100644 index 000000000000..8b006077d849 --- /dev/null +++ b/app/code/Magento/InventoryImportExport/Test/Integration/Model/Import/SourcesTest.php @@ -0,0 +1,166 @@ +importDataMock = $this->getMockBuilder(ImportData::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->importer = Bootstrap::getObjectManager()->create(Sources::class, [ + 'importData' => $this->importDataMock + ]); + + $this->sourceItemRepository = Bootstrap::getObjectManager()->create(SourceItemRepositoryInterface::class); + $this->searchCriteriaBuilder = Bootstrap::getObjectManager()->create(SearchCriteriaBuilder::class); + } + + /** + * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php + */ + public function testValidateRowExpectsInvalidRow() + { + $rowData = $this->buildRowDataArray(10, 'SKU-55', 33, 1); + $result = $this->importer->validateRow($rowData, 2); + $this->assertNotTrue($result, 'Expect result FALSE as given source ID is not present in database.'); + } + + /** + * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php + */ + public function testValidateRowExpectsValidRow() + { + $rowData = $this->buildRowDataArray(2, 'SKU-55', 33, 1); + $result = $this->importer->validateRow($rowData, 2); + $this->assertTrue($result, 'Expect result TRUE as given data is valid.'); + } + + /** + * @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/stocks.php + * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php + * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_link.php + */ + public function testImportDataWithAppendBehavior() + { + $this->importer->setParameters([ + 'behavior' => Import::BEHAVIOR_APPEND + ]); + + $searchCriteria = $this->searchCriteriaBuilder->create(); + $sourceItems = $this->sourceItemRepository->getList($searchCriteria); + $beforeImportData = $this->buildDataArray($sourceItems->getItems()); + + $bunch = [ + $this->buildRowDataArray(1, 'SKU-1', 6.88, 1), + $this->buildRowDataArray(2, 'SKU-1', 5, 1), + $this->buildRowDataArray(5, 'SKU-2', 15, 1), + $this->buildRowDataArray(1, 'SKU-2', 33, 1), + ]; + $this->importDataMock->expects($this->atLeastOnce()) + ->method('getNextBunch') + ->will($this->returnValue($bunch)); + + $this->importer->importData(); + + $sourceItems = $this->sourceItemRepository->getList($searchCriteria); + $expectedData = $this->updateDataArrayByBunch($beforeImportData, $bunch); + $afterImportData = $this->buildDataArray($sourceItems->getItems()); + + $this->assertSame($expectedData, $afterImportData); + } + + /** + * @param int $sourceID + * @param string $sku + * @param int $qty + * @param int $status + * @return array + */ + private function buildRowDataArray($sourceID, $sku, $qty, $status) + { + return [ + Sources::COL_SOURCE => $sourceID, + Sources::COL_SKU => $sku, + Sources::COL_QTY => $qty, + Sources::COL_STATUS => $status, + ]; + } + + /** + * @param SourceItemInterface[] $sourceItems + * @return array + */ + private function buildDataArray(array $sourceItems) + { + $comparableArray = []; + foreach ($sourceItems as $sourceItem) { + $key = sprintf('%s-%s', $sourceItem->getSourceId(), $sourceItem->getSku()); + $comparableArray[$key] = $this->buildRowDataArray( + $sourceItem->getSourceId(), + $sourceItem->getSku(), + $sourceItem->getQuantity(), + $sourceItem->getStatus() + ); + } + return $comparableArray; + } + + /** + * @param array $data + * @param array $bunch + * @return array + */ + private function updateDataArrayByBunch(array $data, array $bunch) + { + foreach ($bunch as $bunchData) { + $key = sprintf('%s-%s', $bunchData[Sources::COL_SOURCE], $bunchData[Sources::COL_SKU]); + $data[$key] = $this->buildRowDataArray( + $bunchData[Sources::COL_SOURCE], + $bunchData[Sources::COL_SKU], + $bunchData[Sources::COL_QTY], + $bunchData[Sources::COL_STATUS] + ); + } + return $data; + } +} From 6b6153c2ee22cd115c8270fb02062376af64cb1f Mon Sep 17 00:00:00 2001 From: larsroettig Date: Sun, 8 Oct 2017 17:18:14 +0200 Subject: [PATCH 06/37] wip --- .../SourceItem/DeleteMultiple.php | 69 ++++++++++++ .../SourceItem/DeleteMultipleTest.php | 106 ++++++++++++++++++ .../Model/Import/Command/Delete.php | 49 +++++++- 3 files changed, 223 insertions(+), 1 deletion(-) create mode 100644 app/code/Magento/Inventory/Model/ResourceModel/SourceItem/DeleteMultiple.php create mode 100644 app/code/Magento/Inventory/Test/Integration/Model/ResourceModel/SourceItem/DeleteMultipleTest.php diff --git a/app/code/Magento/Inventory/Model/ResourceModel/SourceItem/DeleteMultiple.php b/app/code/Magento/Inventory/Model/ResourceModel/SourceItem/DeleteMultiple.php new file mode 100644 index 000000000000..039e0bb21895 --- /dev/null +++ b/app/code/Magento/Inventory/Model/ResourceModel/SourceItem/DeleteMultiple.php @@ -0,0 +1,69 @@ +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); + + $whereCond = [ + $connection->quoteInto(SourceItemInterface::SOURCE_ID . ' IN(?)', $this->getSourceIds($sourceItems)) + ]; + + $connection->delete($tableName, $whereCond); + } + + /** + * @param SourceItemInterface[] $sourceItems + * @return int[] + */ + private function getSourceIds($sourceItems): array + { + $sourceIds = []; + + foreach ($sourceItems as $sourceItem) { + $sourceIds[] = $sourceItem->getSourceId(); + } + + return $sourceIds; + } +} diff --git a/app/code/Magento/Inventory/Test/Integration/Model/ResourceModel/SourceItem/DeleteMultipleTest.php b/app/code/Magento/Inventory/Test/Integration/Model/ResourceModel/SourceItem/DeleteMultipleTest.php new file mode 100644 index 000000000000..c00d2b62ee65 --- /dev/null +++ b/app/code/Magento/Inventory/Test/Integration/Model/ResourceModel/SourceItem/DeleteMultipleTest.php @@ -0,0 +1,106 @@ +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/source_items.php + */ + public function testDeleteMultipleWithEmptySourceItems() + { + $beforeDeleteMultiple = $this->getSourceItems()->getTotalCount(); + + $sourceItems = []; + $this->deleteModel->execute($sourceItems); + + $afterDeleteMultiple = $this->getSourceItems()->getTotalCount(); + $this->assertEquals($beforeDeleteMultiple, $afterDeleteMultiple); + } + + /** + * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php + */ + public function testDeleteMultipleForTwoItems() + { + $sourceItems = $this->getSourceItems(); + $beforeDeleteMultiple = $this->buildDataArray($sourceItems->getItems()); + $itemsToDelete = array_slice($beforeDeleteMultiple, 0, 2); + $expectedData = array_slice($beforeDeleteMultiple, 2); + + $this->deleteModel->execute($itemsToDelete); + + $sourceItems = $this->getSourceItems(); + $afterDeleteMultiple = $this->buildDataArray($sourceItems->getItems()); + $this->assertEquals($expectedData, $afterDeleteMultiple); + } + + /** + * @param SourceItemInterface[] $sourceItems + * @return array + */ + private function buildDataArray(array $sourceItems) + { + $comparableArray = []; + foreach ($sourceItems as $sourceItem) { + $key = sprintf('%s-%s', $sourceItem->getSourceId(), $sourceItem->getSku()); + $comparableArray[$key] = $this->buildRowDataArray( + $sourceItem->getSourceId(), + $sourceItem->getSku(), + $sourceItem->getQuantity(), + $sourceItem->getStatus() + ); + } + return $comparableArray; + } + + + /** + * @return \Magento\InventoryApi\Api\Data\SourceItemSearchResultsInterface + */ + private function getSourceItems(): SourceItemSearchResultsInterface + { + $searchCriteria = $this->searchCriteriaBuilder->create(); + return $this->sourceItemRepository->getList($searchCriteria); + } +} diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Command/Delete.php b/app/code/Magento/InventoryImportExport/Model/Import/Command/Delete.php index 16c12258f781..78890e75b580 100644 --- a/app/code/Magento/InventoryImportExport/Model/Import/Command/Delete.php +++ b/app/code/Magento/InventoryImportExport/Model/Import/Command/Delete.php @@ -6,16 +6,63 @@ namespace Magento\InventoryImportExport\Model\Import\Command; +use Magento\Inventory\Model\ResourceModel\SourceItem\DeleteMultiple as SourceItemsDelete; +use Magento\InventoryApi\Api\Data\SourceItemInterface; +use Magento\InventoryApi\Api\Data\SourceItemInterfaceFactory; +use Magento\InventoryImportExport\Model\Import\Sources; + /** * @inheritdoc */ class Delete implements CommandInterface { + + /** + * @var SourceItemInterfaceFactory + */ + private $sourceItemFactory; + + /** + * @var SourceItemsDelete + */ + private $sourceItemsDelete; + + /** + * @param SourceItemInterfaceFactory $sourceItemFactory + * @param SourceItemsDelete $sourceItemsSave + */ + public function __construct( + SourceItemInterfaceFactory $sourceItemFactory, + SourceItemsDelete $sourceItemsDelete + ) { + $this->sourceItemFactory = $sourceItemFactory; + $this->sourceItemsDelete = $sourceItemsDelete; + } + /** * @inheritdoc */ public function execute(array $bunch) { - // TODO: Implement execute() method. + $sourceItems = []; + + foreach ($bunch as $rowNum => $rowData) { + $sourceItems[]= $this->buildSourceItem($rowData); + } + + $this->sourceItemsDelete->execute($sourceItems); + } + + /** + * @param array $rowData + * @return SourceItemInterface + */ + private function buildSourceItem(array $rowData) + { + /** @var SourceItemInterface $sourceItem */ + $sourceItem = $this->sourceItemFactory->create(); + $sourceItem->setSourceId($rowData[Sources::COL_SOURCE]); + + return $sourceItem; } } From 7d9eba51bdc85918b62641539901041bb599ba83 Mon Sep 17 00:00:00 2001 From: larsroettig Date: Sun, 8 Oct 2017 17:34:12 +0200 Subject: [PATCH 07/37] wip --- .../SourceItem/DeleteMultipleTest.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/app/code/Magento/Inventory/Test/Integration/Model/ResourceModel/SourceItem/DeleteMultipleTest.php b/app/code/Magento/Inventory/Test/Integration/Model/ResourceModel/SourceItem/DeleteMultipleTest.php index c00d2b62ee65..25d873a94c69 100644 --- a/app/code/Magento/Inventory/Test/Integration/Model/ResourceModel/SourceItem/DeleteMultipleTest.php +++ b/app/code/Magento/Inventory/Test/Integration/Model/ResourceModel/SourceItem/DeleteMultipleTest.php @@ -10,6 +10,7 @@ use Magento\Framework\App\ResourceConnection; use Magento\InventoryApi\Api\Data\SourceItemSearchResultsInterface; use Magento\InventoryApi\Api\SourceItemRepositoryInterface; +use Magento\InventoryImportExport\Model\Import\Sources; use Magento\TestFramework\Helper\Bootstrap; use PHPUnit\Framework\TestCase; @@ -45,6 +46,9 @@ protected function setUp() } /** + * @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/stocks.php * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php */ public function testDeleteMultipleWithEmptySourceItems() @@ -59,6 +63,9 @@ public function testDeleteMultipleWithEmptySourceItems() } /** + * @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/stocks.php * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php */ public function testDeleteMultipleForTwoItems() @@ -84,12 +91,7 @@ private function buildDataArray(array $sourceItems) $comparableArray = []; foreach ($sourceItems as $sourceItem) { $key = sprintf('%s-%s', $sourceItem->getSourceId(), $sourceItem->getSku()); - $comparableArray[$key] = $this->buildRowDataArray( - $sourceItem->getSourceId(), - $sourceItem->getSku(), - $sourceItem->getQuantity(), - $sourceItem->getStatus() - ); + $comparableArray[$key] = ''; } return $comparableArray; } From 9a7067ffff8aacb7d03d8ec8a6b2700539fc7058 Mon Sep 17 00:00:00 2001 From: larsroettig Date: Sun, 8 Oct 2017 18:15:27 +0200 Subject: [PATCH 08/37] MSI source: Impelment DelteMultiple Model and Tests --- .../SourceItem/DeleteMultiple.php | 4 +- .../SourceItem/DeleteMultipleTest.php | 43 ++++++------------- 2 files changed, 14 insertions(+), 33 deletions(-) diff --git a/app/code/Magento/Inventory/Model/ResourceModel/SourceItem/DeleteMultiple.php b/app/code/Magento/Inventory/Model/ResourceModel/SourceItem/DeleteMultiple.php index 039e0bb21895..3b753b3eb92a 100644 --- a/app/code/Magento/Inventory/Model/ResourceModel/SourceItem/DeleteMultiple.php +++ b/app/code/Magento/Inventory/Model/ResourceModel/SourceItem/DeleteMultiple.php @@ -8,7 +8,6 @@ use Magento\Framework\App\ResourceConnection; use Magento\Inventory\Model\ResourceModel\SourceItem as SourceItemResourceModel; -use Magento\Inventory\Model\SourceItem; use Magento\InventoryApi\Api\Data\SourceItemInterface; /** @@ -45,8 +44,9 @@ public function execute(array $sourceItems) $connection = $this->resourceConnection->getConnection(); $tableName = $this->resourceConnection->getTableName(SourceItemResourceModel::TABLE_NAME_SOURCE_ITEM); + $sourceIds = $this->getSourceIds($sourceItems); $whereCond = [ - $connection->quoteInto(SourceItemInterface::SOURCE_ID . ' IN(?)', $this->getSourceIds($sourceItems)) + $connection->quoteInto(SourceItemInterface::SOURCE_ID . ' IN(?)', $sourceIds) ]; $connection->delete($tableName, $whereCond); diff --git a/app/code/Magento/Inventory/Test/Integration/Model/ResourceModel/SourceItem/DeleteMultipleTest.php b/app/code/Magento/Inventory/Test/Integration/Model/ResourceModel/SourceItem/DeleteMultipleTest.php index 25d873a94c69..0349a7ba94b2 100644 --- a/app/code/Magento/Inventory/Test/Integration/Model/ResourceModel/SourceItem/DeleteMultipleTest.php +++ b/app/code/Magento/Inventory/Test/Integration/Model/ResourceModel/SourceItem/DeleteMultipleTest.php @@ -8,9 +8,7 @@ use Magento\Framework\Api\SearchCriteriaBuilder; use Magento\Framework\App\ResourceConnection; -use Magento\InventoryApi\Api\Data\SourceItemSearchResultsInterface; use Magento\InventoryApi\Api\SourceItemRepositoryInterface; -use Magento\InventoryImportExport\Model\Import\Sources; use Magento\TestFramework\Helper\Bootstrap; use PHPUnit\Framework\TestCase; @@ -53,13 +51,11 @@ protected function setUp() */ public function testDeleteMultipleWithEmptySourceItems() { - $beforeDeleteMultiple = $this->getSourceItems()->getTotalCount(); + $expectedCount = count($this->getSourceItems()); - $sourceItems = []; - $this->deleteModel->execute($sourceItems); + $this->deleteModel->execute([]); - $afterDeleteMultiple = $this->getSourceItems()->getTotalCount(); - $this->assertEquals($beforeDeleteMultiple, $afterDeleteMultiple); + $this->assertCount($expectedCount, $this->getSourceItems()); } /** @@ -71,38 +67,23 @@ public function testDeleteMultipleWithEmptySourceItems() public function testDeleteMultipleForTwoItems() { $sourceItems = $this->getSourceItems(); - $beforeDeleteMultiple = $this->buildDataArray($sourceItems->getItems()); - $itemsToDelete = array_slice($beforeDeleteMultiple, 0, 2); - $expectedData = array_slice($beforeDeleteMultiple, 2); + $expectedCount = count($sourceItems) - 2; + $itemsToDelete = array_slice($sourceItems, 0, 2); + $expectedResult = array_slice($sourceItems, 2); $this->deleteModel->execute($itemsToDelete); - $sourceItems = $this->getSourceItems(); - $afterDeleteMultiple = $this->buildDataArray($sourceItems->getItems()); - $this->assertEquals($expectedData, $afterDeleteMultiple); + $result = array_values($this->getSourceItems()); + $this->assertCount($expectedCount, $result); + $this->assertEquals($expectedResult, $result); } /** - * @param SourceItemInterface[] $sourceItems - * @return array - */ - private function buildDataArray(array $sourceItems) - { - $comparableArray = []; - foreach ($sourceItems as $sourceItem) { - $key = sprintf('%s-%s', $sourceItem->getSourceId(), $sourceItem->getSku()); - $comparableArray[$key] = ''; - } - return $comparableArray; - } - - - /** - * @return \Magento\InventoryApi\Api\Data\SourceItemSearchResultsInterface + * @return \Magento\InventoryApi\Api\Data\SourceItemInterface[] */ - private function getSourceItems(): SourceItemSearchResultsInterface + private function getSourceItems(): array { $searchCriteria = $this->searchCriteriaBuilder->create(); - return $this->sourceItemRepository->getList($searchCriteria); + return $this->sourceItemRepository->getList($searchCriteria)->getItems(); } } From 77877608bb4d0be3788fde10d2197d7c0cb1c61b Mon Sep 17 00:00:00 2001 From: larsroettig Date: Sun, 8 Oct 2017 18:23:41 +0200 Subject: [PATCH 09/37] Remove not required fixture from methodes --- .../Model/ResourceModel/SourceItem/DeleteMultipleTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/code/Magento/Inventory/Test/Integration/Model/ResourceModel/SourceItem/DeleteMultipleTest.php b/app/code/Magento/Inventory/Test/Integration/Model/ResourceModel/SourceItem/DeleteMultipleTest.php index 0349a7ba94b2..bb17a8c52a78 100644 --- a/app/code/Magento/Inventory/Test/Integration/Model/ResourceModel/SourceItem/DeleteMultipleTest.php +++ b/app/code/Magento/Inventory/Test/Integration/Model/ResourceModel/SourceItem/DeleteMultipleTest.php @@ -46,7 +46,6 @@ protected function setUp() /** * @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/stocks.php * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php */ public function testDeleteMultipleWithEmptySourceItems() @@ -61,7 +60,6 @@ public function testDeleteMultipleWithEmptySourceItems() /** * @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/stocks.php * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php */ public function testDeleteMultipleForTwoItems() From 3ffc60c3ad0d7569e2b53f874fa12f38675380b9 Mon Sep 17 00:00:00 2001 From: larsroettig Date: Sun, 8 Oct 2017 18:59:19 +0200 Subject: [PATCH 10/37] MSI source: Bugfix for Indexer Integration test --- .../Test/Integration/Model/Import/SourcesTest.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/app/code/Magento/InventoryImportExport/Test/Integration/Model/Import/SourcesTest.php b/app/code/Magento/InventoryImportExport/Test/Integration/Model/Import/SourcesTest.php index 8b006077d849..58efaec3b6ee 100644 --- a/app/code/Magento/InventoryImportExport/Test/Integration/Model/Import/SourcesTest.php +++ b/app/code/Magento/InventoryImportExport/Test/Integration/Model/Import/SourcesTest.php @@ -7,12 +7,12 @@ use Magento\Framework\Api\SearchCriteriaBuilder; use Magento\ImportExport\Model\Import; +use Magento\ImportExport\Model\ResourceModel\Import\Data as ImportData; use Magento\InventoryApi\Api\Data\SourceItemInterface; use Magento\InventoryApi\Api\SourceItemRepositoryInterface; use Magento\InventoryImportExport\Model\Import\Sources; use Magento\TestFramework\Helper\Bootstrap; use PHPUnit\Framework\TestCase; -use \Magento\ImportExport\Model\ResourceModel\Import\Data as ImportData; /** * TODO: fixture via composer @@ -96,9 +96,9 @@ public function testImportDataWithAppendBehavior() $this->buildRowDataArray(5, 'SKU-2', 15, 1), $this->buildRowDataArray(1, 'SKU-2', 33, 1), ]; - $this->importDataMock->expects($this->atLeastOnce()) + $this->importDataMock->expects($this->any()) ->method('getNextBunch') - ->will($this->returnValue($bunch)); + ->will($this->onConsecutiveCalls($bunch, false)); $this->importer->importData(); @@ -106,7 +106,7 @@ public function testImportDataWithAppendBehavior() $expectedData = $this->updateDataArrayByBunch($beforeImportData, $bunch); $afterImportData = $this->buildDataArray($sourceItems->getItems()); - $this->assertSame($expectedData, $afterImportData); + $this->assertEquals($expectedData, $afterImportData); } /** @@ -136,10 +136,10 @@ private function buildDataArray(array $sourceItems) foreach ($sourceItems as $sourceItem) { $key = sprintf('%s-%s', $sourceItem->getSourceId(), $sourceItem->getSku()); $comparableArray[$key] = $this->buildRowDataArray( - $sourceItem->getSourceId(), + (int) $sourceItem->getSourceId(), $sourceItem->getSku(), - $sourceItem->getQuantity(), - $sourceItem->getStatus() + (float) $sourceItem->getQuantity(), + (int) $sourceItem->getStatus() ); } return $comparableArray; From 60064e344c6471f0f35f732c29bd03366b9119d5 Mon Sep 17 00:00:00 2001 From: larsroettig Date: Fri, 13 Oct 2017 18:40:44 +0200 Subject: [PATCH 11/37] MSI source: - Add SPI for Delete Multiple - Improve Delte Comand - Implement SPI --- .../SourceItem/DeleteMultiple.php | 15 +++-- .../SourceItem/Command/SourceItemsDelete.php | 57 +++++++++++++++++++ app/code/Magento/Inventory/etc/di.xml | 1 + .../Api/SourceItemsDeleteInterface.php | 28 +++++++++ .../Model/Import/Command/Delete.php | 5 +- 5 files changed, 98 insertions(+), 8 deletions(-) create mode 100644 app/code/Magento/Inventory/Model/SourceItem/Command/SourceItemsDelete.php create mode 100644 app/code/Magento/InventoryApi/Api/SourceItemsDeleteInterface.php diff --git a/app/code/Magento/Inventory/Model/ResourceModel/SourceItem/DeleteMultiple.php b/app/code/Magento/Inventory/Model/ResourceModel/SourceItem/DeleteMultiple.php index 3b753b3eb92a..e8ad8dd8e007 100644 --- a/app/code/Magento/Inventory/Model/ResourceModel/SourceItem/DeleteMultiple.php +++ b/app/code/Magento/Inventory/Model/ResourceModel/SourceItem/DeleteMultiple.php @@ -41,15 +41,18 @@ 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); - $whereCond = [ - $connection->quoteInto(SourceItemInterface::SOURCE_ID . ' IN(?)', $sourceIds) - ]; - - $connection->delete($tableName, $whereCond); + foreach ($sourceIds as $sourceId => $skuList) { + $whereCond = [ + $connection->quoteInto(SourceItemInterface::SOURCE_ID . ' = ?', $sourceId), + $connection->quoteInto(SourceItemInterface::SKU . ' IN(?)', $skuList), + ]; + $connection->delete($tableName, $whereCond); + } } /** @@ -61,7 +64,7 @@ private function getSourceIds($sourceItems): array $sourceIds = []; foreach ($sourceItems as $sourceItem) { - $sourceIds[] = $sourceItem->getSourceId(); + $sourceIds[$sourceItem->getSourceId()][] = $sourceItem->getSku(); } return $sourceIds; diff --git a/app/code/Magento/Inventory/Model/SourceItem/Command/SourceItemsDelete.php b/app/code/Magento/Inventory/Model/SourceItem/Command/SourceItemsDelete.php new file mode 100644 index 000000000000..83fbae8ac3ea --- /dev/null +++ b/app/code/Magento/Inventory/Model/SourceItem/Command/SourceItemsDelete.php @@ -0,0 +1,57 @@ +deleteMultiple = $deleteMultiple; + $this->logger = $logger; + } + + /** + * @inheritdoc + */ + public function execute(array $sourceItems) + { + if (empty($sourceItems)) { + throw new InputException(__('Input data is empty')); + } + try { + $this->deleteMultiple->execute($sourceItems); + } catch (\Exception $e) { + $this->logger->error($e->getMessage()); + throw new CouldNotDeleteException(__('Could not delete Source Item'), $e); + } + } +} diff --git a/app/code/Magento/Inventory/etc/di.xml b/app/code/Magento/Inventory/etc/di.xml index cc3a20ee7a61..36dd518d251e 100644 --- a/app/code/Magento/Inventory/etc/di.xml +++ b/app/code/Magento/Inventory/etc/di.xml @@ -33,6 +33,7 @@ + diff --git a/app/code/Magento/InventoryApi/Api/SourceItemsDeleteInterface.php b/app/code/Magento/InventoryApi/Api/SourceItemsDeleteInterface.php new file mode 100644 index 000000000000..4481d594c663 --- /dev/null +++ b/app/code/Magento/InventoryApi/Api/SourceItemsDeleteInterface.php @@ -0,0 +1,28 @@ +sourceItemFactory = $sourceItemFactory; $this->sourceItemsDelete = $sourceItemsDelete; From 2a30f1994decad340051126b348895dc4131ec6b Mon Sep 17 00:00:00 2001 From: larsroettig Date: Fri, 13 Oct 2017 19:11:00 +0200 Subject: [PATCH 12/37] MSI source: - Bugfix for Intigrationstest to new source_ids - Impelment new test case for delete behavior --- .../Model/Import/Command/Delete.php | 2 +- .../Integration/Model/Import/SourcesTest.php | 48 ++++++++++++++++--- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Command/Delete.php b/app/code/Magento/InventoryImportExport/Model/Import/Command/Delete.php index 7e61b1d1f992..c6b1b4d636f2 100644 --- a/app/code/Magento/InventoryImportExport/Model/Import/Command/Delete.php +++ b/app/code/Magento/InventoryImportExport/Model/Import/Command/Delete.php @@ -63,7 +63,7 @@ private function buildSourceItem(array $rowData) /** @var SourceItemInterface $sourceItem */ $sourceItem = $this->sourceItemFactory->create(); $sourceItem->setSourceId($rowData[Sources::COL_SOURCE]); - + $sourceItem->setSku($rowData[Sources::COL_SKU]); return $sourceItem; } } diff --git a/app/code/Magento/InventoryImportExport/Test/Integration/Model/Import/SourcesTest.php b/app/code/Magento/InventoryImportExport/Test/Integration/Model/Import/SourcesTest.php index 58efaec3b6ee..747fde46961a 100644 --- a/app/code/Magento/InventoryImportExport/Test/Integration/Model/Import/SourcesTest.php +++ b/app/code/Magento/InventoryImportExport/Test/Integration/Model/Import/SourcesTest.php @@ -58,7 +58,7 @@ protected function setUp() */ public function testValidateRowExpectsInvalidRow() { - $rowData = $this->buildRowDataArray(10, 'SKU-55', 33, 1); + $rowData = $this->buildRowDataArray(880, 'SKU-55', 33, 1); $result = $this->importer->validateRow($rowData, 2); $this->assertNotTrue($result, 'Expect result FALSE as given source ID is not present in database.'); } @@ -68,7 +68,7 @@ public function testValidateRowExpectsInvalidRow() */ public function testValidateRowExpectsValidRow() { - $rowData = $this->buildRowDataArray(2, 'SKU-55', 33, 1); + $rowData = $this->buildRowDataArray(20, 'SKU-55', 33, 1); $result = $this->importer->validateRow($rowData, 2); $this->assertTrue($result, 'Expect result TRUE as given data is valid.'); } @@ -91,10 +91,10 @@ public function testImportDataWithAppendBehavior() $beforeImportData = $this->buildDataArray($sourceItems->getItems()); $bunch = [ - $this->buildRowDataArray(1, 'SKU-1', 6.88, 1), - $this->buildRowDataArray(2, 'SKU-1', 5, 1), - $this->buildRowDataArray(5, 'SKU-2', 15, 1), - $this->buildRowDataArray(1, 'SKU-2', 33, 1), + $this->buildRowDataArray(10, 'SKU-1', 6.88, 1), + $this->buildRowDataArray(20, 'SKU-1', 5, 1), + $this->buildRowDataArray(50, 'SKU-2', 15, 1), + $this->buildRowDataArray(10, 'SKU-2', 33, 1), ]; $this->importDataMock->expects($this->any()) ->method('getNextBunch') @@ -109,6 +109,42 @@ public function testImportDataWithAppendBehavior() $this->assertEquals($expectedData, $afterImportData); } + /** + * @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/stocks.php + * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php + * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_link.php + */ + public function testImportDataWithDelteBehavior() + { + $this->importer->setParameters([ + 'behavior' => Import::BEHAVIOR_DELETE + ]); + + $searchCriteria = $this->searchCriteriaBuilder->create(); + $sourceItems = $this->sourceItemRepository->getList($searchCriteria); + $beforeImportData = $this->buildDataArray($sourceItems->getItems()); + + $bunch = [ + $this->buildRowDataArray(10, 'SKU-1', 6.88, 1), + $this->buildRowDataArray(20, 'SKU-1', 5, 1), + ]; + $this->importDataMock->expects($this->any()) + ->method('getNextBunch') + ->will($this->onConsecutiveCalls($bunch, false)); + + $this->importer->importData(); + + $sourceItems = $this->sourceItemRepository->getList($searchCriteria); + $afterImportData = $this->buildDataArray($sourceItems->getItems()); + + $this->assertArrayNotHasKey('10-SKU-1', $afterImportData); + $this->assertArrayNotHasKey('20-SKU-1', $afterImportData); + + $this->assertCount(count($beforeImportData) - 2, $afterImportData); + } + /** * @param int $sourceID * @param string $sku From 98b61ffc036237959eee4d727b1137a657ad443f Mon Sep 17 00:00:00 2001 From: larsroettig Date: Fri, 13 Oct 2017 20:03:14 +0200 Subject: [PATCH 13/37] MSI source: - Refactoring: Add Helperfunction for convert - Impelment Replace Comand - Impelment Replace Comand Test TODO: - Implement clenup for Replace Comand --- .../Helper/SourceItemConvert.php | 55 +++++++++++++++++++ .../Model/Import/Command/Append.php | 41 +++----------- .../Model/Import/Command/Delete.php | 39 +++---------- .../Model/Import/Command/Replace.php | 31 ++++++++++- .../Integration/Model/Import/SourcesTest.php | 34 ++++++++++++ 5 files changed, 135 insertions(+), 65 deletions(-) create mode 100644 app/code/Magento/InventoryImportExport/Helper/SourceItemConvert.php diff --git a/app/code/Magento/InventoryImportExport/Helper/SourceItemConvert.php b/app/code/Magento/InventoryImportExport/Helper/SourceItemConvert.php new file mode 100644 index 000000000000..a3424e24a1dd --- /dev/null +++ b/app/code/Magento/InventoryImportExport/Helper/SourceItemConvert.php @@ -0,0 +1,55 @@ +sourceItemFactory = $sourceItemFactory; + } + + /** + * Converts a data in sourceItem list. + * @param array $rowData + * @return SourceItemInterface[] + */ + public function convert(array $bunch): array + { + $sourceItems = []; + foreach ($bunch as $rowNum => $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; + } +} \ No newline at end of file diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Command/Append.php b/app/code/Magento/InventoryImportExport/Model/Import/Command/Append.php index 530fcce22a68..ac9b9a07e8a9 100644 --- a/app/code/Magento/InventoryImportExport/Model/Import/Command/Append.php +++ b/app/code/Magento/InventoryImportExport/Model/Import/Command/Append.php @@ -6,10 +6,8 @@ namespace Magento\InventoryImportExport\Model\Import\Command; -use Magento\InventoryApi\Api\Data\SourceItemInterface; -use Magento\InventoryApi\Api\Data\SourceItemInterfaceFactory; use Magento\InventoryApi\Api\SourceItemsSaveInterface; -use Magento\InventoryImportExport\Model\Import\Sources; +use Magento\InventoryImportExport\Helper\SourceItemConvert; /** * @inheritdoc @@ -17,9 +15,9 @@ class Append implements CommandInterface { /** - * @var SourceItemInterfaceFactory + * @var SourceItemConvert */ - private $sourceItemFactory; + private $sourceItemConvert; /** * @var SourceItemsSaveInterface @@ -27,14 +25,13 @@ class Append implements CommandInterface private $sourceItemsSave; /** - * @param SourceItemInterfaceFactory $sourceItemFactory - * @param SourceItemsSaveInterface $sourceItemsSave + * @param SourceItemBuilder */ public function __construct( - SourceItemInterfaceFactory $sourceItemFactory, + SourceItemConvert $sourceItemConvert, SourceItemsSaveInterface $sourceItemsSave ) { - $this->sourceItemFactory = $sourceItemFactory; + $this->sourceItemConvert = $sourceItemConvert; $this->sourceItemsSave = $sourceItemsSave; } @@ -43,31 +40,7 @@ public function __construct( */ public function execute(array $bunch) { - $sourceItems = []; - foreach ($bunch as $rowNum => $rowData) { - $sourceItems[] = $this->buildSourceItem($rowData); - } + $sourceItems = $this->sourceItemConvert->convert($bunch); $this->sourceItemsSave->execute($sourceItems); } - - /** - * @param array $rowData - * @return SourceItemInterface - */ - private function buildSourceItem(array $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); - - return $sourceItem; - } } diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Command/Delete.php b/app/code/Magento/InventoryImportExport/Model/Import/Command/Delete.php index c6b1b4d636f2..9619be512765 100644 --- a/app/code/Magento/InventoryImportExport/Model/Import/Command/Delete.php +++ b/app/code/Magento/InventoryImportExport/Model/Import/Command/Delete.php @@ -6,11 +6,8 @@ namespace Magento\InventoryImportExport\Model\Import\Command; -use Magento\Inventory\Model\ResourceModel\SourceItem\DeleteMultiple as SourceItemsDelete; -use Magento\InventoryApi\Api\Data\SourceItemInterface; -use Magento\InventoryApi\Api\Data\SourceItemInterfaceFactory; use Magento\InventoryApi\Api\SourceItemsDeleteInterface; -use Magento\InventoryImportExport\Model\Import\Sources; +use Magento\InventoryImportExport\Helper\SourceItemConvert; /** * @inheritdoc @@ -19,24 +16,24 @@ class Delete implements CommandInterface { /** - * @var SourceItemInterfaceFactory + * @var SourceItemConvert */ - private $sourceItemFactory; + private $sourceItemConvert; /** - * @var SourceItemsDelete + * @var SourceItemsDeleteInterface */ private $sourceItemsDelete; /** - * @param SourceItemInterfaceFactory $sourceItemFactory - * @param SourceItemsDelete $sourceItemsSave + * @param SourceItemConvert $sourceItemConvert , + * @param SourceItemsDeleteInterface $sourceItemsSave */ public function __construct( - SourceItemInterfaceFactory $sourceItemFactory, + SourceItemConvert $sourceItemConvert, SourceItemsDeleteInterface $sourceItemsDelete ) { - $this->sourceItemFactory = $sourceItemFactory; + $this->sourceItemConvert = $sourceItemConvert; $this->sourceItemsDelete = $sourceItemsDelete; } @@ -45,25 +42,7 @@ public function __construct( */ public function execute(array $bunch) { - $sourceItems = []; - - foreach ($bunch as $rowNum => $rowData) { - $sourceItems[]= $this->buildSourceItem($rowData); - } - + $sourceItems = $this->sourceItemConvert->convert($bunch); $this->sourceItemsDelete->execute($sourceItems); } - - /** - * @param array $rowData - * @return SourceItemInterface - */ - private function buildSourceItem(array $rowData) - { - /** @var SourceItemInterface $sourceItem */ - $sourceItem = $this->sourceItemFactory->create(); - $sourceItem->setSourceId($rowData[Sources::COL_SOURCE]); - $sourceItem->setSku($rowData[Sources::COL_SKU]); - return $sourceItem; - } } diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Command/Replace.php b/app/code/Magento/InventoryImportExport/Model/Import/Command/Replace.php index bed98dac551b..49ccb2d34b0f 100644 --- a/app/code/Magento/InventoryImportExport/Model/Import/Command/Replace.php +++ b/app/code/Magento/InventoryImportExport/Model/Import/Command/Replace.php @@ -6,16 +6,45 @@ namespace Magento\InventoryImportExport\Model\Import\Command; +use Magento\InventoryApi\Api\SourceItemsDeleteInterface; +use Magento\InventoryApi\Api\SourceItemsSaveInterface; +use Magento\InventoryImportExport\Helper\SourceItemConvert; + /** * @inheritdoc */ class Replace implements CommandInterface { + /** + * @var SourceItemConvert + */ + private $sourceItemConvert; + + /** + * @var SourceItemsSaveInterface + */ + private $sourceItemsSave; + + /** + * @param SourceItemConvert $sourceItemFactory + * @param SourceItemsSaveInterface $sourceItemsSave + * @param SourceItemsDeleteInterface $sourceItemsDelete + */ + public function __construct( + SourceItemConvert $sourceItemConvert, + SourceItemsSaveInterface $sourceItemsSave + ) { + $this->sourceItemConvert = $sourceItemConvert; + $this->sourceItemsSave = $sourceItemsSave; + } + /** * @inheritdoc */ public function execute(array $bunch) { - // TODO: Implement execute() method. + // @todo implement clenup (DELETE from inventory_source_item;) + $sourceItems = $this->sourceItemConvert->convert($bunch); + $this->sourceItemsSave->execute($sourceItems); } } diff --git a/app/code/Magento/InventoryImportExport/Test/Integration/Model/Import/SourcesTest.php b/app/code/Magento/InventoryImportExport/Test/Integration/Model/Import/SourcesTest.php index 747fde46961a..344afcee83e6 100644 --- a/app/code/Magento/InventoryImportExport/Test/Integration/Model/Import/SourcesTest.php +++ b/app/code/Magento/InventoryImportExport/Test/Integration/Model/Import/SourcesTest.php @@ -144,6 +144,40 @@ public function testImportDataWithDelteBehavior() $this->assertCount(count($beforeImportData) - 2, $afterImportData); } + /** + * @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/stocks.php + * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php + * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_link.php + */ + public function testImportDataWithReplaceBehavior() + { + $this->markTestIncomplete('Implement clenup (DELETE from inventory_source_item;)'); + /** @see \Magento\InventoryImportExport\Model\Import\Command\Replace::execute */ + $this->importer->setParameters([ + 'behavior' => Import::BEHAVIOR_REPLACE + ]); + + $bunch = [ + $this->buildRowDataArray(20, 'SKU-1', 5, 1), + $this->buildRowDataArray(50, 'SKU-2', 15, 1), + ]; + $this->importDataMock->expects($this->any()) + ->method('getNextBunch') + ->will($this->onConsecutiveCalls($bunch, false)); + + $this->importer->importData(); + + $searchCriteria = $this->searchCriteriaBuilder->create(); + + $sourceItems = $this->sourceItemRepository->getList($searchCriteria); + $afterImportData = $this->buildDataArray($sourceItems->getItems()); + + $this->assertArrayHasKey('20-SKU-1', $afterImportData); + $this->assertArrayHasKey('50-SKU-2', $afterImportData); + $this->assertCount(2, $afterImportData); + } /** * @param int $sourceID From b68917ac245616c10fdde331e61fd68067bceb5e Mon Sep 17 00:00:00 2001 From: larsroettig Date: Tue, 17 Oct 2017 20:43:39 +0200 Subject: [PATCH 14/37] phpmd and phpcs fixes --- .../Model/ResourceModel/SourceItem/DeleteMultipleTest.php | 1 - .../Magento/InventoryImportExport/Helper/SourceItemConvert.php | 3 +-- .../Test/Unit/Model/ValidatorChainTest.php | 2 -- 3 files changed, 1 insertion(+), 5 deletions(-) diff --git a/app/code/Magento/Inventory/Test/Integration/Model/ResourceModel/SourceItem/DeleteMultipleTest.php b/app/code/Magento/Inventory/Test/Integration/Model/ResourceModel/SourceItem/DeleteMultipleTest.php index bb17a8c52a78..2eda275a6f73 100644 --- a/app/code/Magento/Inventory/Test/Integration/Model/ResourceModel/SourceItem/DeleteMultipleTest.php +++ b/app/code/Magento/Inventory/Test/Integration/Model/ResourceModel/SourceItem/DeleteMultipleTest.php @@ -35,7 +35,6 @@ class DeleteMultipleTest extends TestCase */ private $searchCriteriaBuilder; - protected function setUp() { $this->deleteModel = Bootstrap::getObjectManager()->create(DeleteMultiple::class); diff --git a/app/code/Magento/InventoryImportExport/Helper/SourceItemConvert.php b/app/code/Magento/InventoryImportExport/Helper/SourceItemConvert.php index a3424e24a1dd..1353070abf1b 100644 --- a/app/code/Magento/InventoryImportExport/Helper/SourceItemConvert.php +++ b/app/code/Magento/InventoryImportExport/Helper/SourceItemConvert.php @@ -46,10 +46,9 @@ public function convert(array $bunch): array } $sourceItem->setStatus($status); - $sourceItems[] = $sourceItem; } return $sourceItems; } -} \ No newline at end of file +} diff --git a/app/code/Magento/InventoryImportExport/Test/Unit/Model/ValidatorChainTest.php b/app/code/Magento/InventoryImportExport/Test/Unit/Model/ValidatorChainTest.php index fe739c5deaef..caed9aacbc05 100644 --- a/app/code/Magento/InventoryImportExport/Test/Unit/Model/ValidatorChainTest.php +++ b/app/code/Magento/InventoryImportExport/Test/Unit/Model/ValidatorChainTest.php @@ -61,7 +61,6 @@ public function testValidateWithOutValidators() ] ); - $result = $this->validatorChain->validate([], 1); $this->assertEquals($emptyValidatorResult, $result); } @@ -92,7 +91,6 @@ public function testValidateWithOutErros() $this->assertEquals($emptyValidatorResult, $result); } - public function testValidateWithErros() { $validatorResult = $this->createMock(\Magento\Framework\Validation\ValidationResult::class); From 457fc27a600a2b648b000f965aead4f111819369 Mon Sep 17 00:00:00 2001 From: larsroettig Date: Tue, 17 Oct 2017 20:57:59 +0200 Subject: [PATCH 15/37] MSI: Add Testcase for testImportDataWithWrongBehavior - phpcs fixes - phpmd fixes --- .../SourceItem/DeleteMultipleTest.php | 6 ------ .../Helper/SourceItemConvert.php | 2 +- .../Model/Import/Sources.php | 10 +++++----- .../Integration/Model/Import/SourcesTest.php | 19 +++++++++++++++++++ 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/app/code/Magento/Inventory/Test/Integration/Model/ResourceModel/SourceItem/DeleteMultipleTest.php b/app/code/Magento/Inventory/Test/Integration/Model/ResourceModel/SourceItem/DeleteMultipleTest.php index 2eda275a6f73..a756ddfd4194 100644 --- a/app/code/Magento/Inventory/Test/Integration/Model/ResourceModel/SourceItem/DeleteMultipleTest.php +++ b/app/code/Magento/Inventory/Test/Integration/Model/ResourceModel/SourceItem/DeleteMultipleTest.php @@ -7,7 +7,6 @@ namespace Magento\Inventory\Model\ResourceModel\SourceItem; use Magento\Framework\Api\SearchCriteriaBuilder; -use Magento\Framework\App\ResourceConnection; use Magento\InventoryApi\Api\SourceItemRepositoryInterface; use Magento\TestFramework\Helper\Bootstrap; use PHPUnit\Framework\TestCase; @@ -15,11 +14,6 @@ class DeleteMultipleTest extends TestCase { - /** - * @var ResourceConnection - */ - private $resource; - /** * @var DeleteMultiple */ diff --git a/app/code/Magento/InventoryImportExport/Helper/SourceItemConvert.php b/app/code/Magento/InventoryImportExport/Helper/SourceItemConvert.php index 1353070abf1b..661f9be817fd 100644 --- a/app/code/Magento/InventoryImportExport/Helper/SourceItemConvert.php +++ b/app/code/Magento/InventoryImportExport/Helper/SourceItemConvert.php @@ -33,7 +33,7 @@ public function __construct(SourceItemInterfaceFactory $sourceItemFactory) public function convert(array $bunch): array { $sourceItems = []; - foreach ($bunch as $rowNum => $rowData) { + foreach ($bunch as $rowData) { /** @var SourceItemInterface $sourceItem */ $sourceItem = $this->sourceItemFactory->create(); $sourceItem->setSourceId($rowData[Sources::COL_SOURCE]); diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Sources.php b/app/code/Magento/InventoryImportExport/Model/Import/Sources.php index a2e151e8cc4a..b9b5ce640a7d 100644 --- a/app/code/Magento/InventoryImportExport/Model/Import/Sources.php +++ b/app/code/Magento/InventoryImportExport/Model/Import/Sources.php @@ -8,11 +8,11 @@ use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Serialize\SerializerInterface; -use \Magento\ImportExport\Model\Import\Entity\AbstractEntity; +use Magento\ImportExport\Helper\Data as DataHelper; +use Magento\ImportExport\Model\Import\Entity\AbstractEntity; use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface; -use \Magento\ImportExport\Model\ResourceModel\Helper as ResourceHelper; -use \Magento\ImportExport\Helper\Data as DataHelper; -use \Magento\ImportExport\Model\ResourceModel\Import\Data as ImportData; +use Magento\ImportExport\Model\ResourceModel\Helper as ResourceHelper; +use Magento\ImportExport\Model\ResourceModel\Import\Data as ImportData; use Magento\InventoryImportExport\Model\Import\Command\CommandInterface; use Magento\InventoryImportExport\Model\Import\Validator\ValidatorInterface; @@ -82,8 +82,8 @@ public function __construct( /** * Import data rows. - * * @return boolean + * @throws LocalizedException */ protected function _importData() { diff --git a/app/code/Magento/InventoryImportExport/Test/Integration/Model/Import/SourcesTest.php b/app/code/Magento/InventoryImportExport/Test/Integration/Model/Import/SourcesTest.php index 344afcee83e6..235e86871070 100644 --- a/app/code/Magento/InventoryImportExport/Test/Integration/Model/Import/SourcesTest.php +++ b/app/code/Magento/InventoryImportExport/Test/Integration/Model/Import/SourcesTest.php @@ -73,6 +73,25 @@ public function testValidateRowExpectsValidRow() $this->assertTrue($result, 'Expect result TRUE as given data is valid.'); } + /** + * @expectedException \Magento\Framework\Exception\LocalizedException + */ + public function testImportDataWithWrongBehavior() + { + $this->importer->setParameters([ + 'behavior' => 'WrongBehavior' + ]); + + $bunch = [ + $this->buildRowDataArray(10, 'SKU-1', 6.88, 1) + ]; + $this->importDataMock->expects($this->any()) + ->method('getNextBunch') + ->will($this->onConsecutiveCalls($bunch, false)); + + $this->importer->importData(); + } + /** * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php From 705e7f9520480030942b4da7fb2329636d8b92c6 Mon Sep 17 00:00:00 2001 From: larsroettig Date: Fri, 20 Oct 2017 19:42:40 +0200 Subject: [PATCH 16/37] MSI: - declare strict_types fot Import/Export Module - Add Replace Comand - Improve Coverrage - phpmd und phpcs --- .../SourceItem/DeleteMultiple.php | 27 +++++-------------- .../SourceItem/DeleteMultipleTest.php | 24 +++-------------- .../Model/Import/Command/Append.php | 5 +++- .../Model/Import/Command/CommandException.php | 16 ----------- .../Model/Import/Command/CommandInterface.php | 3 ++- .../Model/Import/Command/Delete.php | 2 ++ .../Model/Import/Command/Replace.php | 13 +++++++-- .../Model/Import/Serializer/Json.php | 6 +++-- .../Serializer/LegacyJsonHelperInterface.php | 2 ++ .../Model/Import/Sources.php | 2 ++ .../Model/Import/Validator/QtyValidator.php | 6 ++--- .../Model/Import/Validator/SkuValidator.php | 6 ++--- .../Import/Validator/SourceValidator.php | 6 ++--- .../Model/Import/Validator/ValidatorChain.php | 4 ++- .../Import/Validator/ValidatorInterface.php | 4 ++- .../Integration/Model/Import/SourcesTest.php | 15 +++++------ .../Test/Unit/Model/ValidatorChainTest.php | 2 ++ 17 files changed, 62 insertions(+), 81 deletions(-) delete mode 100644 app/code/Magento/InventoryImportExport/Model/Import/Command/CommandException.php diff --git a/app/code/Magento/Inventory/Model/ResourceModel/SourceItem/DeleteMultiple.php b/app/code/Magento/Inventory/Model/ResourceModel/SourceItem/DeleteMultiple.php index e8ad8dd8e007..f52f8f57491a 100644 --- a/app/code/Magento/Inventory/Model/ResourceModel/SourceItem/DeleteMultiple.php +++ b/app/code/Magento/Inventory/Model/ResourceModel/SourceItem/DeleteMultiple.php @@ -45,28 +45,15 @@ public function execute(array $sourceItems) $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 - { - $sourceIds = []; - + $skuList = []; foreach ($sourceItems as $sourceItem) { - $sourceIds[$sourceItem->getSourceId()][] = $sourceItem->getSku(); + $skuList[] = $sourceItem->getSku(); } - return $sourceIds; + $whereCond = [ + $connection->quoteInto(SourceItemInterface::SKU . ' IN(?)', array_unique($skuList)) + ]; + + $connection->delete($tableName, $whereCond); } } diff --git a/app/code/Magento/Inventory/Test/Integration/Model/ResourceModel/SourceItem/DeleteMultipleTest.php b/app/code/Magento/Inventory/Test/Integration/Model/ResourceModel/SourceItem/DeleteMultipleTest.php index a756ddfd4194..9af9151f3e8d 100644 --- a/app/code/Magento/Inventory/Test/Integration/Model/ResourceModel/SourceItem/DeleteMultipleTest.php +++ b/app/code/Magento/Inventory/Test/Integration/Model/ResourceModel/SourceItem/DeleteMultipleTest.php @@ -4,7 +4,10 @@ * See COPYING.txt for license details. */ -namespace Magento\Inventory\Model\ResourceModel\SourceItem; + +declare(strict_types=1); + +namespace Magento\Inventory\Test\Model\ResourceModel\SourceItem; use Magento\Framework\Api\SearchCriteriaBuilder; use Magento\InventoryApi\Api\SourceItemRepositoryInterface; @@ -50,25 +53,6 @@ public function testDeleteMultipleWithEmptySourceItems() $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[] */ diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Command/Append.php b/app/code/Magento/InventoryImportExport/Model/Import/Command/Append.php index ac9b9a07e8a9..baabda55ea37 100644 --- a/app/code/Magento/InventoryImportExport/Model/Import/Command/Append.php +++ b/app/code/Magento/InventoryImportExport/Model/Import/Command/Append.php @@ -4,6 +4,8 @@ * See COPYING.txt for license details. */ +declare(strict_types=1); + namespace Magento\InventoryImportExport\Model\Import\Command; use Magento\InventoryApi\Api\SourceItemsSaveInterface; @@ -25,7 +27,8 @@ class Append implements CommandInterface private $sourceItemsSave; /** - * @param SourceItemBuilder + * @param SourceItemConvert $sourceItemConvert + * @param SourceItemsSaveInterface $sourceItemsSave */ public function __construct( SourceItemConvert $sourceItemConvert, diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Command/CommandException.php b/app/code/Magento/InventoryImportExport/Model/Import/Command/CommandException.php deleted file mode 100644 index 3e0f2dc638d9..000000000000 --- a/app/code/Magento/InventoryImportExport/Model/Import/Command/CommandException.php +++ /dev/null @@ -1,16 +0,0 @@ -sourceItemConvert = $sourceItemConvert; $this->sourceItemsSave = $sourceItemsSave; + $this->sourceItemsDelete = $sourceItemsDelete; } /** @@ -43,8 +52,8 @@ public function __construct( */ public function execute(array $bunch) { - // @todo implement clenup (DELETE from inventory_source_item;) $sourceItems = $this->sourceItemConvert->convert($bunch); + $this->sourceItemsDelete->execute($sourceItems); $this->sourceItemsSave->execute($sourceItems); } } diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Serializer/Json.php b/app/code/Magento/InventoryImportExport/Model/Import/Serializer/Json.php index 0a56563488e8..1b1e302ccd68 100644 --- a/app/code/Magento/InventoryImportExport/Model/Import/Serializer/Json.php +++ b/app/code/Magento/InventoryImportExport/Model/Import/Serializer/Json.php @@ -4,6 +4,8 @@ * See COPYING.txt for license details. */ +declare(strict_types=1); + namespace Magento\InventoryImportExport\Model\Import\Serializer; use Magento\Framework\Serialize\SerializerInterface; @@ -35,7 +37,7 @@ public function __construct(SerializerInterface $serializer) */ public function jsonEncode($valueToEncode) { - return $this->serializer->serialize($valueToEncode); + return $this->serialize($valueToEncode); } /** @@ -47,7 +49,7 @@ public function jsonEncode($valueToEncode) */ public function jsonDecode($encodedValue) { - return $this->serializer->unserialize($encodedValue); + return $this->unserialize($encodedValue); } /** diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Serializer/LegacyJsonHelperInterface.php b/app/code/Magento/InventoryImportExport/Model/Import/Serializer/LegacyJsonHelperInterface.php index 864ab2b394b0..ae75f6825331 100644 --- a/app/code/Magento/InventoryImportExport/Model/Import/Serializer/LegacyJsonHelperInterface.php +++ b/app/code/Magento/InventoryImportExport/Model/Import/Serializer/LegacyJsonHelperInterface.php @@ -4,6 +4,8 @@ * See COPYING.txt for license details. */ +declare(strict_types=1); + namespace Magento\InventoryImportExport\Model\Import\Serializer; /** diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Sources.php b/app/code/Magento/InventoryImportExport/Model/Import/Sources.php index b9b5ce640a7d..499a5c080fdc 100644 --- a/app/code/Magento/InventoryImportExport/Model/Import/Sources.php +++ b/app/code/Magento/InventoryImportExport/Model/Import/Sources.php @@ -4,6 +4,8 @@ * See COPYING.txt for license details. */ +declare(strict_types=1); + namespace Magento\InventoryImportExport\Model\Import; use Magento\Framework\Exception\LocalizedException; diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Validator/QtyValidator.php b/app/code/Magento/InventoryImportExport/Model/Import/Validator/QtyValidator.php index 2771a8630c0e..cdac0e5b43f1 100644 --- a/app/code/Magento/InventoryImportExport/Model/Import/Validator/QtyValidator.php +++ b/app/code/Magento/InventoryImportExport/Model/Import/Validator/QtyValidator.php @@ -4,6 +4,8 @@ * See COPYING.txt for license details. */ +declare(strict_types=1); + namespace Magento\InventoryImportExport\Model\Import\Validator; use Magento\Framework\Exception\LocalizedException; @@ -12,8 +14,6 @@ /** * Extension point for row validation - * - * @api */ class QtyValidator implements ValidatorInterface { @@ -34,7 +34,7 @@ public function __construct(ValidationResultFactory $validationResultFactory) /** * @inheritdoc */ - public function validate(array $rowData, $rowNumber) + public function validate(array $rowData, int $rowNumber) { $errors = []; diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Validator/SkuValidator.php b/app/code/Magento/InventoryImportExport/Model/Import/Validator/SkuValidator.php index 97de46dbfe42..be8d0c3d47ce 100644 --- a/app/code/Magento/InventoryImportExport/Model/Import/Validator/SkuValidator.php +++ b/app/code/Magento/InventoryImportExport/Model/Import/Validator/SkuValidator.php @@ -4,6 +4,8 @@ * See COPYING.txt for license details. */ +declare(strict_types=1); + namespace Magento\InventoryImportExport\Model\Import\Validator; use Magento\Framework\Exception\LocalizedException; @@ -12,8 +14,6 @@ /** * Extension point for row validation - * - * @api */ class SkuValidator implements ValidatorInterface { @@ -34,7 +34,7 @@ public function __construct(ValidationResultFactory $validationResultFactory) /** * @inheritdoc */ - public function validate(array $rowData, $rowNumber) + public function validate(array $rowData, int $rowNumber) { $errors = []; diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Validator/SourceValidator.php b/app/code/Magento/InventoryImportExport/Model/Import/Validator/SourceValidator.php index efbce923cddc..bc6c08037637 100644 --- a/app/code/Magento/InventoryImportExport/Model/Import/Validator/SourceValidator.php +++ b/app/code/Magento/InventoryImportExport/Model/Import/Validator/SourceValidator.php @@ -4,6 +4,8 @@ * See COPYING.txt for license details. */ +declare(strict_types=1); + namespace Magento\InventoryImportExport\Model\Import\Validator; use Magento\Framework\Validation\ValidationResultFactory; @@ -12,8 +14,6 @@ /** * Extension point for source validation - * - * @api */ class SourceValidator implements ValidatorInterface { @@ -48,7 +48,7 @@ public function __construct( /** * @inheritdoc */ - public function validate(array $rowData, $rowNumber) + public function validate(array $rowData, int $rowNumber) { $errors = []; diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Validator/ValidatorChain.php b/app/code/Magento/InventoryImportExport/Model/Import/Validator/ValidatorChain.php index d93fb69bc5a2..70e0bac9b3c6 100644 --- a/app/code/Magento/InventoryImportExport/Model/Import/Validator/ValidatorChain.php +++ b/app/code/Magento/InventoryImportExport/Model/Import/Validator/ValidatorChain.php @@ -4,6 +4,8 @@ * See COPYING.txt for license details. */ +declare(strict_types=1); + namespace Magento\InventoryImportExport\Model\Import\Validator; use Magento\Framework\Exception\LocalizedException; @@ -48,7 +50,7 @@ public function __construct( /** * @inheritdoc */ - public function validate(array $rowData, $rowNumber) + public function validate(array $rowData, int $rowNumber) { /* the inner empty array covers cases when no loops were made */ $errors = [[]]; diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Validator/ValidatorInterface.php b/app/code/Magento/InventoryImportExport/Model/Import/Validator/ValidatorInterface.php index 0e52ccddac44..c7d4505fe98e 100644 --- a/app/code/Magento/InventoryImportExport/Model/Import/Validator/ValidatorInterface.php +++ b/app/code/Magento/InventoryImportExport/Model/Import/Validator/ValidatorInterface.php @@ -4,6 +4,8 @@ * See COPYING.txt for license details. */ +declare(strict_types=1); + namespace Magento\InventoryImportExport\Model\Import\Validator; use Magento\Framework\Validation\ValidationResult; @@ -20,5 +22,5 @@ interface ValidatorInterface * @param int $rowNumber * @return ValidationResult */ - public function validate(array $rowData, $rowNumber); + public function validate(array $rowData, int $rowNumber); } diff --git a/app/code/Magento/InventoryImportExport/Test/Integration/Model/Import/SourcesTest.php b/app/code/Magento/InventoryImportExport/Test/Integration/Model/Import/SourcesTest.php index 235e86871070..dd1e89062302 100644 --- a/app/code/Magento/InventoryImportExport/Test/Integration/Model/Import/SourcesTest.php +++ b/app/code/Magento/InventoryImportExport/Test/Integration/Model/Import/SourcesTest.php @@ -1,4 +1,7 @@ searchCriteriaBuilder->create(); - $sourceItems = $this->sourceItemRepository->getList($searchCriteria); - $beforeImportData = $this->buildDataArray($sourceItems->getItems()); $bunch = [ $this->buildRowDataArray(10, 'SKU-1', 6.88, 1), @@ -160,9 +161,8 @@ public function testImportDataWithDelteBehavior() $this->assertArrayNotHasKey('10-SKU-1', $afterImportData); $this->assertArrayNotHasKey('20-SKU-1', $afterImportData); - - $this->assertCount(count($beforeImportData) - 2, $afterImportData); } + /** * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php @@ -172,7 +172,6 @@ public function testImportDataWithDelteBehavior() */ public function testImportDataWithReplaceBehavior() { - $this->markTestIncomplete('Implement clenup (DELETE from inventory_source_item;)'); /** @see \Magento\InventoryImportExport\Model\Import\Command\Replace::execute */ $this->importer->setParameters([ 'behavior' => Import::BEHAVIOR_REPLACE @@ -225,10 +224,10 @@ private function buildDataArray(array $sourceItems) foreach ($sourceItems as $sourceItem) { $key = sprintf('%s-%s', $sourceItem->getSourceId(), $sourceItem->getSku()); $comparableArray[$key] = $this->buildRowDataArray( - (int) $sourceItem->getSourceId(), + $sourceItem->getSourceId(), $sourceItem->getSku(), - (float) $sourceItem->getQuantity(), - (int) $sourceItem->getStatus() + $sourceItem->getQuantity(), + $sourceItem->getStatus() ); } return $comparableArray; diff --git a/app/code/Magento/InventoryImportExport/Test/Unit/Model/ValidatorChainTest.php b/app/code/Magento/InventoryImportExport/Test/Unit/Model/ValidatorChainTest.php index caed9aacbc05..d0b05401f2d8 100644 --- a/app/code/Magento/InventoryImportExport/Test/Unit/Model/ValidatorChainTest.php +++ b/app/code/Magento/InventoryImportExport/Test/Unit/Model/ValidatorChainTest.php @@ -4,6 +4,8 @@ * See COPYING.txt for license details. */ +declare(strict_types=1); + namespace Magento\Inventory\Test\Unit\Model; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; From 7c83028897800a74d5e636219cf3e6f92e825f17 Mon Sep 17 00:00:00 2001 From: larsroettig Date: Fri, 20 Oct 2017 20:44:29 +0200 Subject: [PATCH 17/37] MSI: - declare strict_types fot Import/Export Module - phpmd and phpunit fix - review fixes --- .../Model/SourceItem/Command/SourceItemsDelete.php | 1 + .../Model/ResourceModel/SourceItem/DeleteMultipleTest.php | 5 ++--- .../InventoryApi/Api/SourceItemsDeleteInterface.php | 1 + .../InventoryImportExport/Model/Import/Command/Append.php | 3 +-- .../Model/Import/Command/CommandInterface.php | 1 - .../InventoryImportExport/Model/Import/Command/Delete.php | 3 +-- .../Model/Import/Command/Replace.php | 3 +-- .../Model/Import/Serializer/Json.php | 1 - .../Model/Import/Serializer/LegacyJsonHelperInterface.php | 1 - .../{Helper => Model/Import}/SourceItemConvert.php | 8 ++++---- .../InventoryImportExport/Model/Import/Sources.php | 1 - .../Model/Import/Validator/QtyValidator.php | 1 - .../Model/Import/Validator/SkuValidator.php | 1 - .../Model/Import/Validator/SourceValidator.php | 1 - .../Model/Import/Validator/ValidatorChain.php | 1 - .../Model/Import/Validator/ValidatorInterface.php | 1 - .../Test/Integration/Model/Import/SourcesTest.php | 5 ++--- .../Test/Unit/Model/ValidatorChainTest.php | 2 -- 18 files changed, 13 insertions(+), 27 deletions(-) rename app/code/Magento/InventoryImportExport/{Helper => Model/Import}/SourceItemConvert.php (90%) diff --git a/app/code/Magento/Inventory/Model/SourceItem/Command/SourceItemsDelete.php b/app/code/Magento/Inventory/Model/SourceItem/Command/SourceItemsDelete.php index 83fbae8ac3ea..d74ad888dfdb 100644 --- a/app/code/Magento/Inventory/Model/SourceItem/Command/SourceItemsDelete.php +++ b/app/code/Magento/Inventory/Model/SourceItem/Command/SourceItemsDelete.php @@ -3,6 +3,7 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ +declare(strict_types=1); namespace Magento\Inventory\Model\SourceItem\Command; diff --git a/app/code/Magento/Inventory/Test/Integration/Model/ResourceModel/SourceItem/DeleteMultipleTest.php b/app/code/Magento/Inventory/Test/Integration/Model/ResourceModel/SourceItem/DeleteMultipleTest.php index 9af9151f3e8d..c02de3f64756 100644 --- a/app/code/Magento/Inventory/Test/Integration/Model/ResourceModel/SourceItem/DeleteMultipleTest.php +++ b/app/code/Magento/Inventory/Test/Integration/Model/ResourceModel/SourceItem/DeleteMultipleTest.php @@ -3,13 +3,12 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ - - declare(strict_types=1); -namespace Magento\Inventory\Test\Model\ResourceModel\SourceItem; +namespace Magento\Inventory\Test\Integration\Model\ResourceModel\SourceItem; use Magento\Framework\Api\SearchCriteriaBuilder; +use Magento\Inventory\Model\ResourceModel\SourceItem\DeleteMultiple; use Magento\InventoryApi\Api\SourceItemRepositoryInterface; use Magento\TestFramework\Helper\Bootstrap; use PHPUnit\Framework\TestCase; diff --git a/app/code/Magento/InventoryApi/Api/SourceItemsDeleteInterface.php b/app/code/Magento/InventoryApi/Api/SourceItemsDeleteInterface.php index 4481d594c663..1b1157bffa90 100644 --- a/app/code/Magento/InventoryApi/Api/SourceItemsDeleteInterface.php +++ b/app/code/Magento/InventoryApi/Api/SourceItemsDeleteInterface.php @@ -3,6 +3,7 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ +declare(strict_types=1); namespace Magento\InventoryApi\Api; diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Command/Append.php b/app/code/Magento/InventoryImportExport/Model/Import/Command/Append.php index baabda55ea37..48eec176810f 100644 --- a/app/code/Magento/InventoryImportExport/Model/Import/Command/Append.php +++ b/app/code/Magento/InventoryImportExport/Model/Import/Command/Append.php @@ -3,13 +3,12 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ - declare(strict_types=1); namespace Magento\InventoryImportExport\Model\Import\Command; use Magento\InventoryApi\Api\SourceItemsSaveInterface; -use Magento\InventoryImportExport\Helper\SourceItemConvert; +use Magento\InventoryImportExport\Model\Import\SourceItemConvert; /** * @inheritdoc diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Command/CommandInterface.php b/app/code/Magento/InventoryImportExport/Model/Import/Command/CommandInterface.php index 8989da500bd5..7c53e7c42922 100644 --- a/app/code/Magento/InventoryImportExport/Model/Import/Command/CommandInterface.php +++ b/app/code/Magento/InventoryImportExport/Model/Import/Command/CommandInterface.php @@ -3,7 +3,6 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ - declare(strict_types=1); namespace Magento\InventoryImportExport\Model\Import\Command; diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Command/Delete.php b/app/code/Magento/InventoryImportExport/Model/Import/Command/Delete.php index b4c9b4a20d84..1b652fed25a9 100644 --- a/app/code/Magento/InventoryImportExport/Model/Import/Command/Delete.php +++ b/app/code/Magento/InventoryImportExport/Model/Import/Command/Delete.php @@ -3,13 +3,12 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ - declare(strict_types=1); namespace Magento\InventoryImportExport\Model\Import\Command; use Magento\InventoryApi\Api\SourceItemsDeleteInterface; -use Magento\InventoryImportExport\Helper\SourceItemConvert; +use Magento\InventoryImportExport\Model\Import\SourceItemConvert; /** * @inheritdoc diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Command/Replace.php b/app/code/Magento/InventoryImportExport/Model/Import/Command/Replace.php index d1ebb63f7e0a..43f4fd8b519a 100644 --- a/app/code/Magento/InventoryImportExport/Model/Import/Command/Replace.php +++ b/app/code/Magento/InventoryImportExport/Model/Import/Command/Replace.php @@ -3,14 +3,13 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ - declare(strict_types=1); namespace Magento\InventoryImportExport\Model\Import\Command; use Magento\InventoryApi\Api\SourceItemsDeleteInterface; use Magento\InventoryApi\Api\SourceItemsSaveInterface; -use Magento\InventoryImportExport\Helper\SourceItemConvert; +use Magento\InventoryImportExport\Model\Import\SourceItemConvert; /** * @inheritdoc diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Serializer/Json.php b/app/code/Magento/InventoryImportExport/Model/Import/Serializer/Json.php index 1b1e302ccd68..5f31107bc341 100644 --- a/app/code/Magento/InventoryImportExport/Model/Import/Serializer/Json.php +++ b/app/code/Magento/InventoryImportExport/Model/Import/Serializer/Json.php @@ -3,7 +3,6 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ - declare(strict_types=1); namespace Magento\InventoryImportExport\Model\Import\Serializer; diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Serializer/LegacyJsonHelperInterface.php b/app/code/Magento/InventoryImportExport/Model/Import/Serializer/LegacyJsonHelperInterface.php index ae75f6825331..e1ef83edece0 100644 --- a/app/code/Magento/InventoryImportExport/Model/Import/Serializer/LegacyJsonHelperInterface.php +++ b/app/code/Magento/InventoryImportExport/Model/Import/Serializer/LegacyJsonHelperInterface.php @@ -3,7 +3,6 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ - declare(strict_types=1); namespace Magento\InventoryImportExport\Model\Import\Serializer; diff --git a/app/code/Magento/InventoryImportExport/Helper/SourceItemConvert.php b/app/code/Magento/InventoryImportExport/Model/Import/SourceItemConvert.php similarity index 90% rename from app/code/Magento/InventoryImportExport/Helper/SourceItemConvert.php rename to app/code/Magento/InventoryImportExport/Model/Import/SourceItemConvert.php index 661f9be817fd..b00bbd23a80b 100644 --- a/app/code/Magento/InventoryImportExport/Helper/SourceItemConvert.php +++ b/app/code/Magento/InventoryImportExport/Model/Import/SourceItemConvert.php @@ -3,16 +3,16 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ +declare(strict_types=1); -namespace Magento\InventoryImportExport\Helper; +namespace Magento\InventoryImportExport\Model\Import; use Magento\InventoryApi\Api\Data\SourceItemInterface; use Magento\InventoryApi\Api\Data\SourceItemInterfaceFactory; -use Magento\InventoryImportExport\Model\Import\Sources; class SourceItemConvert { - /** + /**´ * @var SourceItemInterfaceFactory */ private $sourceItemFactory; @@ -27,7 +27,7 @@ public function __construct(SourceItemInterfaceFactory $sourceItemFactory) /** * Converts a data in sourceItem list. - * @param array $rowData + * @param array $bunch * @return SourceItemInterface[] */ public function convert(array $bunch): array diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Sources.php b/app/code/Magento/InventoryImportExport/Model/Import/Sources.php index 499a5c080fdc..8ac6cfc33c1c 100644 --- a/app/code/Magento/InventoryImportExport/Model/Import/Sources.php +++ b/app/code/Magento/InventoryImportExport/Model/Import/Sources.php @@ -3,7 +3,6 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ - declare(strict_types=1); namespace Magento\InventoryImportExport\Model\Import; diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Validator/QtyValidator.php b/app/code/Magento/InventoryImportExport/Model/Import/Validator/QtyValidator.php index cdac0e5b43f1..5a5ca3ee6881 100644 --- a/app/code/Magento/InventoryImportExport/Model/Import/Validator/QtyValidator.php +++ b/app/code/Magento/InventoryImportExport/Model/Import/Validator/QtyValidator.php @@ -3,7 +3,6 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ - declare(strict_types=1); namespace Magento\InventoryImportExport\Model\Import\Validator; diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Validator/SkuValidator.php b/app/code/Magento/InventoryImportExport/Model/Import/Validator/SkuValidator.php index be8d0c3d47ce..db7c06d52ffb 100644 --- a/app/code/Magento/InventoryImportExport/Model/Import/Validator/SkuValidator.php +++ b/app/code/Magento/InventoryImportExport/Model/Import/Validator/SkuValidator.php @@ -3,7 +3,6 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ - declare(strict_types=1); namespace Magento\InventoryImportExport\Model\Import\Validator; diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Validator/SourceValidator.php b/app/code/Magento/InventoryImportExport/Model/Import/Validator/SourceValidator.php index bc6c08037637..94e6f3b8c9fb 100644 --- a/app/code/Magento/InventoryImportExport/Model/Import/Validator/SourceValidator.php +++ b/app/code/Magento/InventoryImportExport/Model/Import/Validator/SourceValidator.php @@ -3,7 +3,6 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ - declare(strict_types=1); namespace Magento\InventoryImportExport\Model\Import\Validator; diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Validator/ValidatorChain.php b/app/code/Magento/InventoryImportExport/Model/Import/Validator/ValidatorChain.php index 70e0bac9b3c6..595badc05b11 100644 --- a/app/code/Magento/InventoryImportExport/Model/Import/Validator/ValidatorChain.php +++ b/app/code/Magento/InventoryImportExport/Model/Import/Validator/ValidatorChain.php @@ -3,7 +3,6 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ - declare(strict_types=1); namespace Magento\InventoryImportExport\Model\Import\Validator; diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Validator/ValidatorInterface.php b/app/code/Magento/InventoryImportExport/Model/Import/Validator/ValidatorInterface.php index c7d4505fe98e..b64c44d43ef2 100644 --- a/app/code/Magento/InventoryImportExport/Model/Import/Validator/ValidatorInterface.php +++ b/app/code/Magento/InventoryImportExport/Model/Import/Validator/ValidatorInterface.php @@ -3,7 +3,6 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ - declare(strict_types=1); namespace Magento\InventoryImportExport\Model\Import\Validator; diff --git a/app/code/Magento/InventoryImportExport/Test/Integration/Model/Import/SourcesTest.php b/app/code/Magento/InventoryImportExport/Test/Integration/Model/Import/SourcesTest.php index dd1e89062302..3ead01ddb75c 100644 --- a/app/code/Magento/InventoryImportExport/Test/Integration/Model/Import/SourcesTest.php +++ b/app/code/Magento/InventoryImportExport/Test/Integration/Model/Import/SourcesTest.php @@ -1,11 +1,10 @@ with(['errors' => ['Qty can not negative', 'Additional error']]) ->willReturn($validatorResult); - $this->validatorChain = (new ObjectManager($this))->getObject( ValidatorChain::class, [ From 973d2ba44ff98a86549e867d5653b904e24cd1d9 Mon Sep 17 00:00:00 2001 From: Vadim Justus Date: Wed, 25 Oct 2017 14:25:24 +0200 Subject: [PATCH 18/37] MSI: Remove LegacyJsonHelperInterface --- .../Model/Import/Serializer/Json.php | 6 +++- .../Serializer/LegacyJsonHelperInterface.php | 33 ------------------- .../Model/Import/Sources.php | 8 ++--- 3 files changed, 9 insertions(+), 38 deletions(-) delete mode 100644 app/code/Magento/InventoryImportExport/Model/Import/Serializer/LegacyJsonHelperInterface.php diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Serializer/Json.php b/app/code/Magento/InventoryImportExport/Model/Import/Serializer/Json.php index 5f31107bc341..37bbae397a35 100644 --- a/app/code/Magento/InventoryImportExport/Model/Import/Serializer/Json.php +++ b/app/code/Magento/InventoryImportExport/Model/Import/Serializer/Json.php @@ -12,7 +12,7 @@ /** * @inheritdoc */ -class Json implements SerializerInterface, LegacyJsonHelperInterface +class Json implements SerializerInterface { /** * @var SerializerInterface @@ -31,8 +31,10 @@ public function __construct(SerializerInterface $serializer) /** * Encode the mixed $valueToEncode into the JSON format * + * @deprecated 100.2.0 @see \Magento\Framework\Serialize\Serializer\Json * @param mixed $valueToEncode * @return string + * @throws \InvalidArgumentException */ public function jsonEncode($valueToEncode) { @@ -43,8 +45,10 @@ public function jsonEncode($valueToEncode) * Decodes the given $encodedValue string which is * encoded in the JSON format * + * @deprecated 100.2.0 @see \Magento\Framework\Serialize\Serializer\Json * @param string $encodedValue * @return mixed + * @throws \InvalidArgumentException */ public function jsonDecode($encodedValue) { diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Serializer/LegacyJsonHelperInterface.php b/app/code/Magento/InventoryImportExport/Model/Import/Serializer/LegacyJsonHelperInterface.php deleted file mode 100644 index e1ef83edece0..000000000000 --- a/app/code/Magento/InventoryImportExport/Model/Import/Serializer/LegacyJsonHelperInterface.php +++ /dev/null @@ -1,33 +0,0 @@ - Date: Wed, 25 Oct 2017 14:32:20 +0200 Subject: [PATCH 19/37] MSI: #131 - Add Export Command --- .../Model/Export/Sources.php | 67 +++++++++++++++++++ .../InventoryImportExport/etc/export.xml | 10 +++ 2 files changed, 77 insertions(+) create mode 100644 app/code/Magento/InventoryImportExport/Model/Export/Sources.php create mode 100644 app/code/Magento/InventoryImportExport/etc/export.xml diff --git a/app/code/Magento/InventoryImportExport/Model/Export/Sources.php b/app/code/Magento/InventoryImportExport/Model/Export/Sources.php new file mode 100644 index 000000000000..34745065265a --- /dev/null +++ b/app/code/Magento/InventoryImportExport/Model/Export/Sources.php @@ -0,0 +1,67 @@ + + + + + \ No newline at end of file From 2ba6943085e9996d021e4516cade8db83a872cc3 Mon Sep 17 00:00:00 2001 From: larsroettig Date: Thu, 26 Oct 2017 14:21:31 +0200 Subject: [PATCH 20/37] wip --- .../Controller/Adminhtml/Export/Export.php | 4 +- .../Model/Export/Sources.php | 87 ++++++++++++++----- 2 files changed, 69 insertions(+), 22 deletions(-) diff --git a/app/code/Magento/ImportExport/Controller/Adminhtml/Export/Export.php b/app/code/Magento/ImportExport/Controller/Adminhtml/Export/Export.php index 06d8610a247c..74693b536ca9 100644 --- a/app/code/Magento/ImportExport/Controller/Adminhtml/Export/Export.php +++ b/app/code/Magento/ImportExport/Controller/Adminhtml/Export/Export.php @@ -48,7 +48,9 @@ public function __construct( */ public function execute() { - if ($this->getRequest()->getPost(ExportModel::FILTER_ELEMENT_GROUP)) { + $post = $this->getRequest()->getPost(ExportModel::FILTER_ELEMENT_GROUP); + + if ($post) { try { /** @var $model \Magento\ImportExport\Model\Export */ $model = $this->_objectManager->create(\Magento\ImportExport\Model\Export::class); diff --git a/app/code/Magento/InventoryImportExport/Model/Export/Sources.php b/app/code/Magento/InventoryImportExport/Model/Export/Sources.php index 34745065265a..53eae827096f 100644 --- a/app/code/Magento/InventoryImportExport/Model/Export/Sources.php +++ b/app/code/Magento/InventoryImportExport/Model/Export/Sources.php @@ -6,7 +6,11 @@ namespace Magento\InventoryImportExport\Model\Export; -use Magento\ImportExport\Model\Export\Entity\AbstractEntity; +use Magento\Framework\App\ObjectManager; +use Magento\ImportExport\Model\Export\AbstractEntity; +use Magento\Inventory\Model\ResourceModel\SourceItemFactory; +use Magento\InventoryApi\Api\Data\SourceItemInterface; + /** * @inheritdoc @@ -14,54 +18,95 @@ class Sources extends AbstractEntity { + /**#@+ + * Attribute collection name + */ + const ATTRIBUTE_COLLECTION_NAME = \Magento\Inventory\Model\ResourceModel\SourceItem\Collection::class; + /** - * Get header columns - * - * @return string[] + * @var SourceItemFactory */ - protected function _getHeaderColumns() - { - // TODO: Implement _getHeaderColumns() method. + private $sourceItemFactory; + + + public function __construct( + \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, + \Magento\Store\Model\StoreManagerInterface $storeManager, + \Magento\ImportExport\Model\Export\Factory $collectionFactory, + \Magento\ImportExport\Model\ResourceModel\CollectionByPagesIteratorFactory $resourceColFactory, + SourceItemFactory $sourceItemFactory, + array $data = [] + ) { + + $this->sourceItemFactory = $sourceItemFactory; + parent::__construct($scopeConfig, $storeManager, $collectionFactory, $resourceColFactory, $data); } /** - * Get entity collection - * - * @param bool $resetCollection - * @return \Magento\Framework\Data\Collection\AbstractDb + * @return \Magento\Framework\Data\Collection|void */ - protected function _getEntityCollection($resetCollection = false) + public function getAttributeCollection() { - // TODO: Implement _getEntityCollection() method. + $objectManager = ObjectManager::getInstance(); + /** @var \Magento\Framework\DataObject $dataObject * */ + $dataObject = $objectManager->create('\Magento\Framework\DataObject'); + $this->_attributeCollection->addItem($dataObject); + return $this->_attributeCollection; } /** - * Export process. + * Export process * * @return string */ public function export() { - // TODO: Implement export() method. + return 'teststtststststtsts'; } /** - * Entity attributes collection getter. + * Export one item * - * @return \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection + * @param \Magento\Framework\Model\AbstractModel $item + * @return void */ - public function getAttributeCollection() + public function exportItem($item) { - // TODO: Implement getAttributeCollection() method. + // TODO: Implement exportItem() method. } /** - * EAV entity type code getter. + * Entity type code getter * * @return string */ public function getEntityTypeCode() { - // TODO: Implement getEntityTypeCode() method. + return 'stock_sources'; + } + + /** + * Get header columns + * + * @return array + */ + protected function _getHeaderColumns() + { + return [ + SourceItemInterface::SOURCE_ID, + SourceItemInterface::SKU, + SourceItemInterface::STATUS, + SourceItemInterface::QUANTITY + ]; + } + + /** + * Get entity collection + * + * @return \Magento\Framework\Data\Collection\AbstractDb + */ + protected function _getEntityCollection() + { + return $this->sourceItemFactory->create(); } } From c59cde08ee2cd3ee12f1136e699a8b981d900176 Mon Sep 17 00:00:00 2001 From: larsroettig Date: Thu, 26 Oct 2017 15:49:28 +0200 Subject: [PATCH 21/37] WIP bugfix for export filter --- .../Controller/Adminhtml/Export/Export.php | 4 +- .../Model/Export/Sources.php | 43 +++++++++++++------ 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/app/code/Magento/ImportExport/Controller/Adminhtml/Export/Export.php b/app/code/Magento/ImportExport/Controller/Adminhtml/Export/Export.php index 74693b536ca9..06d8610a247c 100644 --- a/app/code/Magento/ImportExport/Controller/Adminhtml/Export/Export.php +++ b/app/code/Magento/ImportExport/Controller/Adminhtml/Export/Export.php @@ -48,9 +48,7 @@ public function __construct( */ public function execute() { - $post = $this->getRequest()->getPost(ExportModel::FILTER_ELEMENT_GROUP); - - if ($post) { + if ($this->getRequest()->getPost(ExportModel::FILTER_ELEMENT_GROUP)) { try { /** @var $model \Magento\ImportExport\Model\Export */ $model = $this->_objectManager->create(\Magento\ImportExport\Model\Export::class); diff --git a/app/code/Magento/InventoryImportExport/Model/Export/Sources.php b/app/code/Magento/InventoryImportExport/Model/Export/Sources.php index 53eae827096f..4980c4eb395a 100644 --- a/app/code/Magento/InventoryImportExport/Model/Export/Sources.php +++ b/app/code/Magento/InventoryImportExport/Model/Export/Sources.php @@ -6,28 +6,26 @@ namespace Magento\InventoryImportExport\Model\Export; -use Magento\Framework\App\ObjectManager; +use Magento\Eav\Model\Entity\AttributeFactory; use Magento\ImportExport\Model\Export\AbstractEntity; use Magento\Inventory\Model\ResourceModel\SourceItemFactory; use Magento\InventoryApi\Api\Data\SourceItemInterface; - /** * @inheritdoc */ class Sources extends AbstractEntity { - /**#@+ - * Attribute collection name - */ - const ATTRIBUTE_COLLECTION_NAME = \Magento\Inventory\Model\ResourceModel\SourceItem\Collection::class; - /** * @var SourceItemFactory */ private $sourceItemFactory; + /** + * @var AttributeFactory + */ + private $attributeFactory; public function __construct( \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, @@ -35,22 +33,39 @@ public function __construct( \Magento\ImportExport\Model\Export\Factory $collectionFactory, \Magento\ImportExport\Model\ResourceModel\CollectionByPagesIteratorFactory $resourceColFactory, SourceItemFactory $sourceItemFactory, + AttributeFactory $attributeFactory, array $data = [] ) { - $this->sourceItemFactory = $sourceItemFactory; + $this->attributeFactory = $attributeFactory; parent::__construct($scopeConfig, $storeManager, $collectionFactory, $resourceColFactory, $data); } /** - * @return \Magento\Framework\Data\Collection|void + * @inheritdoc */ public function getAttributeCollection() { - $objectManager = ObjectManager::getInstance(); - /** @var \Magento\Framework\DataObject $dataObject * */ - $dataObject = $objectManager->create('\Magento\Framework\DataObject'); - $this->_attributeCollection->addItem($dataObject); + if (count($this->_attributeCollection) === 0) { + /** @var \Magento\Eav\Model\Entity\Attribute $skuAttribute */ + $skuAttribute = $this->attributeFactory->create(); + $skuAttribute->setDefaultFrontendLabel(SourceItemInterface::SKU); + $skuAttribute->setAttributeCode(SourceItemInterface::SKU); + $this->_attributeCollection->addItem($skuAttribute); + + /** @var \Magento\Eav\Model\Entity\Attribute $sourceIdAttribute */ + $sourceIdAttribute = $this->attributeFactory->create(); + $sourceIdAttribute->setDefaultFrontendLabel(SourceItemInterface::SOURCE_ID); + $sourceIdAttribute->setAttributeCode(SourceItemInterface::SOURCE_ID); + $this->_attributeCollection->addItem($sourceIdAttribute); + + /** @var \Magento\Eav\Model\Entity\Attribute $skuAttribute */ + $quantityAttribute = $this->attributeFactory->create(); + $quantityAttribute->setDefaultFrontendLabel(SourceItemInterface::QUANTITY); + $quantityAttribute->setAttributeCode(SourceItemInterface::QUANTITY); + $this->_attributeCollection->addItem($quantityAttribute); + } + return $this->_attributeCollection; } @@ -61,7 +76,7 @@ public function getAttributeCollection() */ public function export() { - return 'teststtststststtsts'; + } /** From e472b697b15b75b9de968e8366b3747c9b9dae79 Mon Sep 17 00:00:00 2001 From: larsroettig Date: Thu, 26 Oct 2017 17:58:11 +0200 Subject: [PATCH 22/37] wip --- .../Model/Export/Sources.php | 95 +++++++++++++------ 1 file changed, 68 insertions(+), 27 deletions(-) diff --git a/app/code/Magento/InventoryImportExport/Model/Export/Sources.php b/app/code/Magento/InventoryImportExport/Model/Export/Sources.php index 4980c4eb395a..6afa63445f82 100644 --- a/app/code/Magento/InventoryImportExport/Model/Export/Sources.php +++ b/app/code/Magento/InventoryImportExport/Model/Export/Sources.php @@ -7,8 +7,11 @@ namespace Magento\InventoryImportExport\Model\Export; use Magento\Eav\Model\Entity\AttributeFactory; +use Magento\ImportExport\Model\Export; use Magento\ImportExport\Model\Export\AbstractEntity; -use Magento\Inventory\Model\ResourceModel\SourceItemFactory; +use Magento\Inventory\Model\ResourceModel\SourceItem; +use Magento\Inventory\Model\ResourceModel\SourceItem\Collection as SourceItemCollection; +use Magento\Inventory\Model\ResourceModel\SourceItem\CollectionFactory; use Magento\InventoryApi\Api\Data\SourceItemInterface; /** @@ -18,9 +21,9 @@ class Sources extends AbstractEntity { /** - * @var SourceItemFactory + * @var CollectionFactory */ - private $sourceItemFactory; + private $sourceItemCollectionFactory; /** * @var AttributeFactory @@ -32,12 +35,12 @@ public function __construct( \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\ImportExport\Model\Export\Factory $collectionFactory, \Magento\ImportExport\Model\ResourceModel\CollectionByPagesIteratorFactory $resourceColFactory, - SourceItemFactory $sourceItemFactory, + CollectionFactory $sourceItemCollectionFactory, AttributeFactory $attributeFactory, array $data = [] ) { - $this->sourceItemFactory = $sourceItemFactory; - $this->attributeFactory = $attributeFactory; + $this->sourceItemCollectionFactory = $sourceItemCollectionFactory; + $this->attributeFactory = $attributeFactory; parent::__construct($scopeConfig, $storeManager, $collectionFactory, $resourceColFactory, $data); } @@ -47,20 +50,30 @@ public function __construct( public function getAttributeCollection() { if (count($this->_attributeCollection) === 0) { - /** @var \Magento\Eav\Model\Entity\Attribute $skuAttribute */ + /** @var \Magento\Eav\Model\Entity\Attribute $skuAttribute */ $skuAttribute = $this->attributeFactory->create(); + $skuAttribute->setId(SourceItemInterface::SKU); $skuAttribute->setDefaultFrontendLabel(SourceItemInterface::SKU); $skuAttribute->setAttributeCode(SourceItemInterface::SKU); $this->_attributeCollection->addItem($skuAttribute); - /** @var \Magento\Eav\Model\Entity\Attribute $sourceIdAttribute */ + /** @var \Magento\Eav\Model\Entity\Attribute $sourceIdAttribute */ $sourceIdAttribute = $this->attributeFactory->create(); + $sourceIdAttribute->setId(SourceItemInterface::SOURCE_ID); $sourceIdAttribute->setDefaultFrontendLabel(SourceItemInterface::SOURCE_ID); $sourceIdAttribute->setAttributeCode(SourceItemInterface::SOURCE_ID); $this->_attributeCollection->addItem($sourceIdAttribute); - /** @var \Magento\Eav\Model\Entity\Attribute $skuAttribute */ + /** @var \Magento\Eav\Model\Entity\Attribute $statusIdAttribut */ + $statusIdAttribute = $this->attributeFactory->create(); + $statusIdAttribute->setId(SourceItemInterface::STATUS); + $statusIdAttribute->setDefaultFrontendLabel(SourceItemInterface::STATUS); + $statusIdAttribute->setAttributeCode(SourceItemInterface::STATUS); + $this->_attributeCollection->addItem($statusIdAttribute); + + /** @var \Magento\Eav\Model\Entity\Attribute $quantityAttribute */ $quantityAttribute = $this->attributeFactory->create(); + $quantityAttribute->setId(SourceItemInterface::QUANTITY); $quantityAttribute->setDefaultFrontendLabel(SourceItemInterface::QUANTITY); $quantityAttribute->setAttributeCode(SourceItemInterface::QUANTITY); $this->_attributeCollection->addItem($quantityAttribute); @@ -76,7 +89,50 @@ public function getAttributeCollection() */ public function export() { + $writer = $this->getWriter(); + + $columns = $this->_getHeaderColumns(); + $writer->setHeaderCols($columns); + + /** @var SourceItemCollection $collection */ + $collection = $this->sourceItemCollectionFactory->create(); + $collection->addFieldToSelect($columns); + + foreach ($collection->getData() as $data) { + unset($data[SourceItem::ID_FIELD_NAME]); + $writer->writeRow($data); + } + + return $writer->getContents(); + } + + /** + * Get header columns + * + * @return array + */ + protected function _getHeaderColumns() + { + $columns = [ + SourceItemInterface::SOURCE_ID, + SourceItemInterface::SKU, + SourceItemInterface::STATUS, + SourceItemInterface::QUANTITY + ]; + + if (!isset($this->_parameters[Export::FILTER_ELEMENT_SKIP])) { + return $columns; + } + + // remove the skipped from columns + $skippedAttributes = array_flip($this->_parameters[Export::FILTER_ELEMENT_SKIP]); + foreach ($columns as $key => $value) { + if (array_key_exists($value, $skippedAttributes) === true) { + unset($columns[$key]); + } + } + return $columns; } /** @@ -87,7 +143,7 @@ public function export() */ public function exportItem($item) { - // TODO: Implement exportItem() method. + // will not implement it is legacy interface method } /** @@ -97,22 +153,7 @@ public function exportItem($item) */ public function getEntityTypeCode() { - return 'stock_sources'; - } - - /** - * Get header columns - * - * @return array - */ - protected function _getHeaderColumns() - { - return [ - SourceItemInterface::SOURCE_ID, - SourceItemInterface::SKU, - SourceItemInterface::STATUS, - SourceItemInterface::QUANTITY - ]; + return 'stock_sources'; } /** @@ -122,6 +163,6 @@ protected function _getHeaderColumns() */ protected function _getEntityCollection() { - return $this->sourceItemFactory->create(); + // will not implement it is legacy interface method } } From 505f630871c202b744b554376384a7e1b9d474a6 Mon Sep 17 00:00:00 2001 From: Vadim Justus Date: Fri, 27 Oct 2017 10:20:16 +0200 Subject: [PATCH 23/37] MIS: Implement filtering for export functionality --- .../Model/Export/Sources.php | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/InventoryImportExport/Model/Export/Sources.php b/app/code/Magento/InventoryImportExport/Model/Export/Sources.php index 6afa63445f82..ce1cf716264b 100644 --- a/app/code/Magento/InventoryImportExport/Model/Export/Sources.php +++ b/app/code/Magento/InventoryImportExport/Model/Export/Sources.php @@ -7,6 +7,7 @@ namespace Magento\InventoryImportExport\Model\Export; use Magento\Eav\Model\Entity\AttributeFactory; +use Magento\Framework\Data\Collection; use Magento\ImportExport\Model\Export; use Magento\ImportExport\Model\Export\AbstractEntity; use Magento\Inventory\Model\ResourceModel\SourceItem; @@ -98,6 +99,8 @@ public function export() $collection = $this->sourceItemCollectionFactory->create(); $collection->addFieldToSelect($columns); + $this->applyFilters($collection); + foreach ($collection->getData() as $data) { unset($data[SourceItem::ID_FIELD_NAME]); $writer->writeRow($data); @@ -106,6 +109,29 @@ public function export() return $writer->getContents(); } + /** + * @param Collection $collection + */ + private function applyFilters(Collection $collection) + { + foreach ($this->retrieveFilterDataFromRequest() as $columnName => $value) { + $collection->addFilter($columnName, $value); + } + } + + /** + * @return array + */ + private function retrieveFilterDataFromRequest() + { + return array_filter( + $this->_parameters[Export::FILTER_ELEMENT_GROUP] ?? [], + function($value) { + return !empty($value); + } + ); + } + /** * Get header columns * @@ -143,7 +169,7 @@ protected function _getHeaderColumns() */ public function exportItem($item) { - // will not implement it is legacy interface method + // will not implement this method as it is legacy interface } /** @@ -163,6 +189,6 @@ public function getEntityTypeCode() */ protected function _getEntityCollection() { - // will not implement it is legacy interface method + // will not implement this method as it is legacy interface } } From 71584151e599b6c10d94b4d00992ea6e0fea95ef Mon Sep 17 00:00:00 2001 From: larsroettig Date: Fri, 27 Oct 2017 10:22:51 +0200 Subject: [PATCH 24/37] add filter type --- app/code/Magento/InventoryImportExport/Model/Export/Sources.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/code/Magento/InventoryImportExport/Model/Export/Sources.php b/app/code/Magento/InventoryImportExport/Model/Export/Sources.php index 6afa63445f82..f740c470773c 100644 --- a/app/code/Magento/InventoryImportExport/Model/Export/Sources.php +++ b/app/code/Magento/InventoryImportExport/Model/Export/Sources.php @@ -62,6 +62,7 @@ public function getAttributeCollection() $sourceIdAttribute->setId(SourceItemInterface::SOURCE_ID); $sourceIdAttribute->setDefaultFrontendLabel(SourceItemInterface::SOURCE_ID); $sourceIdAttribute->setAttributeCode(SourceItemInterface::SOURCE_ID); + $sourceIdAttribute->setBackendType('int'); $this->_attributeCollection->addItem($sourceIdAttribute); /** @var \Magento\Eav\Model\Entity\Attribute $statusIdAttribut */ @@ -74,6 +75,7 @@ public function getAttributeCollection() /** @var \Magento\Eav\Model\Entity\Attribute $quantityAttribute */ $quantityAttribute = $this->attributeFactory->create(); $quantityAttribute->setId(SourceItemInterface::QUANTITY); + $sourceIdAttribute->setBackendType('decimal'); $quantityAttribute->setDefaultFrontendLabel(SourceItemInterface::QUANTITY); $quantityAttribute->setAttributeCode(SourceItemInterface::QUANTITY); $this->_attributeCollection->addItem($quantityAttribute); From bc2709c9134596427b35a32dd424e39a386b756d Mon Sep 17 00:00:00 2001 From: Vadim Justus Date: Fri, 27 Oct 2017 10:57:49 +0200 Subject: [PATCH 25/37] MSI: Extend filtering logic for stock_source export --- .../Model/Export/Sources.php | 41 ++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/InventoryImportExport/Model/Export/Sources.php b/app/code/Magento/InventoryImportExport/Model/Export/Sources.php index 240c55c3646c..7b8d816163e8 100644 --- a/app/code/Magento/InventoryImportExport/Model/Export/Sources.php +++ b/app/code/Magento/InventoryImportExport/Model/Export/Sources.php @@ -89,6 +89,7 @@ public function getAttributeCollection() * Export process * * @return string + * @throws \Magento\Framework\Exception\LocalizedException */ public function export() { @@ -113,11 +114,49 @@ public function export() /** * @param Collection $collection + * @throws \Magento\Framework\Exception\LocalizedException */ private function applyFilters(Collection $collection) { foreach ($this->retrieveFilterDataFromRequest() as $columnName => $value) { - $collection->addFilter($columnName, $value); + $attributeDefinition = $this->getAttributeCollection()->getItemById($columnName); + $type = null; + if ($attributeDefinition) { + $type = $attributeDefinition->getData('backend_type'); + } + + if (is_array($value)) { + $from = $value[0] ?? null; + $to = $value[0] ?? null; + + if (in_array($type, ['int', 'decimal'], true)) { + if (is_numeric($from) && !empty($from)) { + $collection->addFieldToFilter($columnName, ['from' => $from]); + } + if (is_numeric($to) && !empty($to)) { + $collection->addFieldToFilter($columnName, ['from' => $to]); + } + continue; + } + + if ($type === 'datetime') { + $from = $value[0] ?? null; + $to = $value[0] ?? null; + if (is_scalar($from) && !empty($from)) { + $date = (new \DateTime($from))->format('m/d/Y'); + $collection->addFieldToFilter($columnName, ['from' => $date, 'date' => true]); + } + if (is_scalar($to) && !empty($to)) { + $date = (new \DateTime($to))->format('m/d/Y'); + $collection->addFieldToFilter($columnName, ['to' => $date, 'date' => true]); + } + continue; + } + + continue; + } + + $collection->addFieldToFilter($columnName, ['like' => '%' . $value . '%']); } } From f13268051e3189d98909de8ae84f724871557f9c Mon Sep 17 00:00:00 2001 From: Vadim Justus Date: Fri, 27 Oct 2017 11:04:54 +0200 Subject: [PATCH 26/37] MSI: remove useless code --- .../InventoryImportExport/Model/Export/Sources.php | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/app/code/Magento/InventoryImportExport/Model/Export/Sources.php b/app/code/Magento/InventoryImportExport/Model/Export/Sources.php index 7b8d816163e8..944bfcb9be0e 100644 --- a/app/code/Magento/InventoryImportExport/Model/Export/Sources.php +++ b/app/code/Magento/InventoryImportExport/Model/Export/Sources.php @@ -127,21 +127,16 @@ private function applyFilters(Collection $collection) if (is_array($value)) { $from = $value[0] ?? null; - $to = $value[0] ?? null; + $to = $value[1] ?? null; if (in_array($type, ['int', 'decimal'], true)) { if (is_numeric($from) && !empty($from)) { $collection->addFieldToFilter($columnName, ['from' => $from]); } if (is_numeric($to) && !empty($to)) { - $collection->addFieldToFilter($columnName, ['from' => $to]); + $collection->addFieldToFilter($columnName, ['to' => $to]); } - continue; - } - - if ($type === 'datetime') { - $from = $value[0] ?? null; - $to = $value[0] ?? null; + } elseif ($type === 'datetime') { if (is_scalar($from) && !empty($from)) { $date = (new \DateTime($from))->format('m/d/Y'); $collection->addFieldToFilter($columnName, ['from' => $date, 'date' => true]); @@ -150,7 +145,6 @@ private function applyFilters(Collection $collection) $date = (new \DateTime($to))->format('m/d/Y'); $collection->addFieldToFilter($columnName, ['to' => $date, 'date' => true]); } - continue; } continue; From c063161920ad68fb01eaa5b62aaa29ffae74a6a1 Mon Sep 17 00:00:00 2001 From: Vadim Justus Date: Fri, 27 Oct 2017 14:54:33 +0200 Subject: [PATCH 27/37] MSI: Refactore filtering logic in order to reduce complexity --- .../Model/Export/Source/StockStatus.php | 40 ++++++ .../Model/Export/Sources.php | 115 ++++++++++++++---- 2 files changed, 128 insertions(+), 27 deletions(-) create mode 100644 app/code/Magento/InventoryImportExport/Model/Export/Source/StockStatus.php diff --git a/app/code/Magento/InventoryImportExport/Model/Export/Source/StockStatus.php b/app/code/Magento/InventoryImportExport/Model/Export/Source/StockStatus.php new file mode 100644 index 000000000000..247faa52f1b6 --- /dev/null +++ b/app/code/Magento/InventoryImportExport/Model/Export/Source/StockStatus.php @@ -0,0 +1,40 @@ +sourceItemStatus = $sourceItemStatus; + } + + /** + * Retrieve All options + * + * @return array + */ + public function getAllOptions() + { + return $this->sourceItemStatus->toOptionArray(); + } +} diff --git a/app/code/Magento/InventoryImportExport/Model/Export/Sources.php b/app/code/Magento/InventoryImportExport/Model/Export/Sources.php index 944bfcb9be0e..eeb40a908217 100644 --- a/app/code/Magento/InventoryImportExport/Model/Export/Sources.php +++ b/app/code/Magento/InventoryImportExport/Model/Export/Sources.php @@ -14,6 +14,7 @@ use Magento\Inventory\Model\ResourceModel\SourceItem\Collection as SourceItemCollection; use Magento\Inventory\Model\ResourceModel\SourceItem\CollectionFactory; use Magento\InventoryApi\Api\Data\SourceItemInterface; +use Magento\InventoryImportExport\Model\Export\Source\StockStatus; /** * @inheritdoc @@ -47,6 +48,7 @@ public function __construct( /** * @inheritdoc + * @throws \Exception */ public function getAttributeCollection() { @@ -66,17 +68,20 @@ public function getAttributeCollection() $sourceIdAttribute->setBackendType('int'); $this->_attributeCollection->addItem($sourceIdAttribute); - /** @var \Magento\Eav\Model\Entity\Attribute $statusIdAttribut */ + /** @var \Magento\Eav\Model\Entity\Attribute $statusIdAttribute */ $statusIdAttribute = $this->attributeFactory->create(); $statusIdAttribute->setId(SourceItemInterface::STATUS); $statusIdAttribute->setDefaultFrontendLabel(SourceItemInterface::STATUS); $statusIdAttribute->setAttributeCode(SourceItemInterface::STATUS); + $statusIdAttribute->setBackendType('int'); + $statusIdAttribute->setFrontendInput('select'); + $statusIdAttribute->setSourceModel(StockStatus::class); $this->_attributeCollection->addItem($statusIdAttribute); /** @var \Magento\Eav\Model\Entity\Attribute $quantityAttribute */ $quantityAttribute = $this->attributeFactory->create(); $quantityAttribute->setId(SourceItemInterface::QUANTITY); - $sourceIdAttribute->setBackendType('decimal'); + $quantityAttribute->setBackendType('decimal'); $quantityAttribute->setDefaultFrontendLabel(SourceItemInterface::QUANTITY); $quantityAttribute->setAttributeCode(SourceItemInterface::QUANTITY); $this->_attributeCollection->addItem($quantityAttribute); @@ -125,33 +130,89 @@ private function applyFilters(Collection $collection) $type = $attributeDefinition->getData('backend_type'); } - if (is_array($value)) { - $from = $value[0] ?? null; - $to = $value[1] ?? null; - - if (in_array($type, ['int', 'decimal'], true)) { - if (is_numeric($from) && !empty($from)) { - $collection->addFieldToFilter($columnName, ['from' => $from]); - } - if (is_numeric($to) && !empty($to)) { - $collection->addFieldToFilter($columnName, ['to' => $to]); - } - } elseif ($type === 'datetime') { - if (is_scalar($from) && !empty($from)) { - $date = (new \DateTime($from))->format('m/d/Y'); - $collection->addFieldToFilter($columnName, ['from' => $date, 'date' => true]); - } - if (is_scalar($to) && !empty($to)) { - $date = (new \DateTime($to))->format('m/d/Y'); - $collection->addFieldToFilter($columnName, ['to' => $date, 'date' => true]); - } - } - - continue; + $filterMethodName = 'applyFilterFor' . ucfirst($type); + if (method_exists($this, $filterMethodName)) { + $this->$filterMethodName($collection, $columnName, $value); + } else { + $this->applyDefaultFilter($collection, $columnName, $value); } + } + } - $collection->addFieldToFilter($columnName, ['like' => '%' . $value . '%']); + /** + * @param Collection $collection + * @param string $columnName + * @param mixed $value + * @throws \Magento\Framework\Exception\LocalizedException + */ + public function applyFilterForInt(Collection $collection, $columnName, $value) + { + if (is_array($value)) { + $from = $value[0] ?? null; + $to = $value[1] ?? null; + + if (is_numeric($from) && !empty($from)) { + $collection->addFieldToFilter($columnName, ['from' => $from]); + } + + if (is_numeric($to) && !empty($to)) { + $collection->addFieldToFilter($columnName, ['to' => $to]); + } + + return; } + + $collection->addFieldToFilter($columnName, ['eq' => $value]); + } + + /** + * @param Collection $collection + * @param string $columnName + * @param mixed $value + * @throws \Magento\Framework\Exception\LocalizedException + */ + public function applyFilterForDecimal(Collection $collection, $columnName, $value) + { + $this->applyFilterForInt($collection, $columnName, $value); + } + + /** + * @param Collection $collection + * @param string $columnName + * @param mixed $value + * @throws \Magento\Framework\Exception\LocalizedException + */ + public function applyDefaultFilter(Collection $collection, $columnName, $value) + { + $collection->addFieldToFilter($columnName, ['like' => '%' . $value . '%']); + } + + /** + * @param Collection $collection + * @param string $columnName + * @param mixed $value + * @throws \Magento\Framework\Exception\LocalizedException + */ + public function applyFilterForDatetime(Collection $collection, $columnName, $value) + { + if (is_array($value)) { + $from = $value[0] ?? null; + $to = $value[1] ?? null; + + if (is_scalar($from) && !empty($from)) { + $date = (new \DateTime($from))->format('m/d/Y'); + $collection->addFieldToFilter($columnName, ['from' => $date, 'date' => true]); + } + + if (is_scalar($to) && !empty($to)) { + $date = (new \DateTime($to))->format('m/d/Y'); + $collection->addFieldToFilter($columnName, ['to' => $date, 'date' => true]); + } + + return; + } + + $this->applyDefaultFilter($collection, $columnName, $value); } /** @@ -162,7 +223,7 @@ private function retrieveFilterDataFromRequest() return array_filter( $this->_parameters[Export::FILTER_ELEMENT_GROUP] ?? [], function($value) { - return !empty($value); + return $value !== ''; } ); } From 03018f8f607d850c509f299008bfe5002a84f036 Mon Sep 17 00:00:00 2001 From: Vadim Justus Date: Sat, 28 Oct 2017 10:24:25 +0200 Subject: [PATCH 28/37] MSI: Fix intergration tests --- .../Test/Integration/Model/Import/SourcesTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/InventoryImportExport/Test/Integration/Model/Import/SourcesTest.php b/app/code/Magento/InventoryImportExport/Test/Integration/Model/Import/SourcesTest.php index 3ead01ddb75c..e8ed795b0dbf 100644 --- a/app/code/Magento/InventoryImportExport/Test/Integration/Model/Import/SourcesTest.php +++ b/app/code/Magento/InventoryImportExport/Test/Integration/Model/Import/SourcesTest.php @@ -112,8 +112,8 @@ public function testImportDataWithAppendBehavior() $beforeImportData = $this->buildDataArray($sourceItems->getItems()); $bunch = [ - $this->buildRowDataArray(10, 'SKU-1', 6.88, 1), - $this->buildRowDataArray(20, 'SKU-1', 5, 1), + $this->buildRowDataArray(10, 'SKU-1', 6.8800, 1), + $this->buildRowDataArray(20, 'SKU-1', 5.0000, 1), $this->buildRowDataArray(50, 'SKU-2', 15, 1), $this->buildRowDataArray(10, 'SKU-2', 33, 1), ]; @@ -244,7 +244,7 @@ private function updateDataArrayByBunch(array $data, array $bunch) $data[$key] = $this->buildRowDataArray( $bunchData[Sources::COL_SOURCE], $bunchData[Sources::COL_SKU], - $bunchData[Sources::COL_QTY], + number_format($bunchData[Sources::COL_QTY], 4), $bunchData[Sources::COL_STATUS] ); } From d7a72561195072002c92dac84a9e5d4cbe00de29 Mon Sep 17 00:00:00 2001 From: Vadim Justus Date: Sat, 28 Oct 2017 11:26:05 +0200 Subject: [PATCH 29/37] MSI: Implement integration tests for stock_source export entity --- .../Integration/Model/Export/SourcesTest.php | 168 ++++++++++++++++++ .../Export/_files/export_filtered_by_sku.csv | 5 + .../_files/export_filtered_by_source.csv | 4 + .../export_filtered_without_status_column.csv | 4 + .../Model/Export/_files/export_full.csv | 6 + 5 files changed, 187 insertions(+) create mode 100644 app/code/Magento/InventoryImportExport/Test/Integration/Model/Export/SourcesTest.php create mode 100644 app/code/Magento/InventoryImportExport/Test/Integration/Model/Export/_files/export_filtered_by_sku.csv create mode 100644 app/code/Magento/InventoryImportExport/Test/Integration/Model/Export/_files/export_filtered_by_source.csv create mode 100644 app/code/Magento/InventoryImportExport/Test/Integration/Model/Export/_files/export_filtered_without_status_column.csv create mode 100644 app/code/Magento/InventoryImportExport/Test/Integration/Model/Export/_files/export_full.csv diff --git a/app/code/Magento/InventoryImportExport/Test/Integration/Model/Export/SourcesTest.php b/app/code/Magento/InventoryImportExport/Test/Integration/Model/Export/SourcesTest.php new file mode 100644 index 000000000000..c46d7acd1b6c --- /dev/null +++ b/app/code/Magento/InventoryImportExport/Test/Integration/Model/Export/SourcesTest.php @@ -0,0 +1,168 @@ +getBootstrap()->getApplication()->getTempDir(); + $this->exportFilePath = implode(DIRECTORY_SEPARATOR, [ + $sandboxDir, + 'var', + uniqid('test-export_', false) . '.csv' + ]); + + $this->exporter = Bootstrap::getObjectManager()->create(Sources::class); + $this->exporter->setWriter(Bootstrap::getObjectManager()->create( + Csv::class, + ['destination' => $this->exportFilePath] + )); + } + + protected function tearDown() + { + unlink($this->exportFilePath); + } + + /** + * @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/stocks.php + * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php + * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_link.php + */ + public function testExportWithoutAnyFiltering() + { + $this->exporter->setParameters([]); + $this->exporter->export(); + + $this->assertEquals( + file_get_contents(implode(DIRECTORY_SEPARATOR, [__DIR__, '_files', 'export_full.csv'])), + file_get_contents($this->exportFilePath) + ); + } + + /** + * @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/stocks.php + * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php + * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_link.php + */ + public function testExportWithSkuFilter() + { + $this->exporter->setParameters([ + Export::FILTER_ELEMENT_GROUP => [ + 'sku' => 'SKU-1' + ] + ]); + $this->exporter->export(); + + $this->assertEquals( + file_get_contents(implode(DIRECTORY_SEPARATOR, [__DIR__, '_files', 'export_filtered_by_sku.csv'])), + file_get_contents($this->exportFilePath) + ); + } + + /** + * @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/stocks.php + * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php + * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_link.php + */ + public function testExportWithSkuFilterByLikeQuery() + { + $this->exporter->setParameters([ + Export::FILTER_ELEMENT_GROUP => [ + 'sku' => 'U-1' + ] + ]); + $this->exporter->export(); + + $this->assertEquals( + file_get_contents(implode(DIRECTORY_SEPARATOR, [__DIR__, '_files', 'export_filtered_by_sku.csv'])), + file_get_contents($this->exportFilePath) + ); + } + + /** + * @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/stocks.php + * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php + * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_link.php + */ + public function testExportWithSourceFilter() + { + $this->exporter->setParameters([ + Export::FILTER_ELEMENT_GROUP => [ + 'source_id' => [ + 22, + 62 + ] + ] + ]); + $this->exporter->export(); + + $this->assertEquals( + file_get_contents(implode(DIRECTORY_SEPARATOR, [__DIR__, '_files', 'export_filtered_by_source.csv'])), + file_get_contents($this->exportFilePath) + ); + } + + /** + * @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/stocks.php + * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php + * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_link.php + */ + public function testExportFilteredWithoutStatusColumn() + { + $this->exporter->setParameters([ + Export::FILTER_ELEMENT_GROUP => [ + 'sku' => 'SKU-1', + 'status' => 1 + ], + Export::FILTER_ELEMENT_SKIP => [ + 'status' + ] + ]); + $this->exporter->export(); + + $this->assertEquals( + file_get_contents(implode(DIRECTORY_SEPARATOR, [ + __DIR__, + '_files', + 'export_filtered_without_status_column.csv' + ])), + file_get_contents($this->exportFilePath) + ); + } +} diff --git a/app/code/Magento/InventoryImportExport/Test/Integration/Model/Export/_files/export_filtered_by_sku.csv b/app/code/Magento/InventoryImportExport/Test/Integration/Model/Export/_files/export_filtered_by_sku.csv new file mode 100644 index 000000000000..449aeb78494e --- /dev/null +++ b/app/code/Magento/InventoryImportExport/Test/Integration/Model/Export/_files/export_filtered_by_sku.csv @@ -0,0 +1,5 @@ +source_id,sku,status,quantity +10,SKU-1,1,5.5000 +20,SKU-1,1,3.0000 +30,SKU-1,0,10.0000 +40,SKU-1,1,10.0000 diff --git a/app/code/Magento/InventoryImportExport/Test/Integration/Model/Export/_files/export_filtered_by_source.csv b/app/code/Magento/InventoryImportExport/Test/Integration/Model/Export/_files/export_filtered_by_source.csv new file mode 100644 index 000000000000..752c4c8699a2 --- /dev/null +++ b/app/code/Magento/InventoryImportExport/Test/Integration/Model/Export/_files/export_filtered_by_source.csv @@ -0,0 +1,4 @@ +source_id,sku,status,quantity +30,SKU-1,0,10.0000 +40,SKU-1,1,10.0000 +50,SKU-2,1,5.0000 diff --git a/app/code/Magento/InventoryImportExport/Test/Integration/Model/Export/_files/export_filtered_without_status_column.csv b/app/code/Magento/InventoryImportExport/Test/Integration/Model/Export/_files/export_filtered_without_status_column.csv new file mode 100644 index 000000000000..89cb0643b37c --- /dev/null +++ b/app/code/Magento/InventoryImportExport/Test/Integration/Model/Export/_files/export_filtered_without_status_column.csv @@ -0,0 +1,4 @@ +source_id,sku,quantity +10,SKU-1,5.5000 +20,SKU-1,3.0000 +40,SKU-1,10.0000 diff --git a/app/code/Magento/InventoryImportExport/Test/Integration/Model/Export/_files/export_full.csv b/app/code/Magento/InventoryImportExport/Test/Integration/Model/Export/_files/export_full.csv new file mode 100644 index 000000000000..aca33a8fff41 --- /dev/null +++ b/app/code/Magento/InventoryImportExport/Test/Integration/Model/Export/_files/export_full.csv @@ -0,0 +1,6 @@ +source_id,sku,status,quantity +10,SKU-1,1,5.5000 +20,SKU-1,1,3.0000 +30,SKU-1,0,10.0000 +40,SKU-1,1,10.0000 +50,SKU-2,1,5.0000 From 7173a7130683b74e676797c119893ab4e684bed2 Mon Sep 17 00:00:00 2001 From: Vadim Justus Date: Sat, 28 Oct 2017 14:16:29 +0200 Subject: [PATCH 30/37] MSI: Refactore getAttributeCollection method for stock_sources export model --- .../Model/Export/CollectionBuilder.php | 86 +++++++++++++++++++ .../Model/Export/Sources.php | 69 ++++----------- 2 files changed, 105 insertions(+), 50 deletions(-) create mode 100644 app/code/Magento/InventoryImportExport/Model/Export/CollectionBuilder.php diff --git a/app/code/Magento/InventoryImportExport/Model/Export/CollectionBuilder.php b/app/code/Magento/InventoryImportExport/Model/Export/CollectionBuilder.php new file mode 100644 index 000000000000..97157be7fa0e --- /dev/null +++ b/app/code/Magento/InventoryImportExport/Model/Export/CollectionBuilder.php @@ -0,0 +1,86 @@ +collection = $collectionFactory->create(Collection::class); + $this->attributeFactory = $attributeFactory; + } + + /** + * @return Collection + * @throws \Exception + */ + public function create(): Collection + { + if (count($this->collection) === 0) { + /** @var \Magento\Eav\Model\Entity\Attribute $sourceIdAttribute */ + $sourceIdAttribute = $this->attributeFactory->create(); + $sourceIdAttribute->setId(SourceItemInterface::SOURCE_ID); + $sourceIdAttribute->setDefaultFrontendLabel(SourceItemInterface::SOURCE_ID); + $sourceIdAttribute->setAttributeCode(SourceItemInterface::SOURCE_ID); + $sourceIdAttribute->setBackendType('int'); + $this->collection->addItem($sourceIdAttribute); + + /** @var \Magento\Eav\Model\Entity\Attribute $skuAttribute */ + $skuAttribute = $this->attributeFactory->create(); + $skuAttribute->setId(SourceItemInterface::SKU); + $skuAttribute->setDefaultFrontendLabel(SourceItemInterface::SKU); + $skuAttribute->setAttributeCode(SourceItemInterface::SKU); + $this->collection->addItem($skuAttribute); + + /** @var \Magento\Eav\Model\Entity\Attribute $statusIdAttribute */ + $statusIdAttribute = $this->attributeFactory->create(); + $statusIdAttribute->setId(SourceItemInterface::STATUS); + $statusIdAttribute->setDefaultFrontendLabel(SourceItemInterface::STATUS); + $statusIdAttribute->setAttributeCode(SourceItemInterface::STATUS); + $statusIdAttribute->setBackendType('int'); + $statusIdAttribute->setFrontendInput('select'); + $statusIdAttribute->setSourceModel(StockStatus::class); + $this->collection->addItem($statusIdAttribute); + + /** @var \Magento\Eav\Model\Entity\Attribute $quantityAttribute */ + $quantityAttribute = $this->attributeFactory->create(); + $quantityAttribute->setId(SourceItemInterface::QUANTITY); + $quantityAttribute->setBackendType('decimal'); + $quantityAttribute->setDefaultFrontendLabel(SourceItemInterface::QUANTITY); + $quantityAttribute->setAttributeCode(SourceItemInterface::QUANTITY); + $this->collection->addItem($quantityAttribute); + } + + return $this->collection; + } +} diff --git a/app/code/Magento/InventoryImportExport/Model/Export/Sources.php b/app/code/Magento/InventoryImportExport/Model/Export/Sources.php index eeb40a908217..f05007c35431 100644 --- a/app/code/Magento/InventoryImportExport/Model/Export/Sources.php +++ b/app/code/Magento/InventoryImportExport/Model/Export/Sources.php @@ -6,21 +6,22 @@ namespace Magento\InventoryImportExport\Model\Export; -use Magento\Eav\Model\Entity\AttributeFactory; use Magento\Framework\Data\Collection; use Magento\ImportExport\Model\Export; use Magento\ImportExport\Model\Export\AbstractEntity; use Magento\Inventory\Model\ResourceModel\SourceItem; use Magento\Inventory\Model\ResourceModel\SourceItem\Collection as SourceItemCollection; use Magento\Inventory\Model\ResourceModel\SourceItem\CollectionFactory; -use Magento\InventoryApi\Api\Data\SourceItemInterface; -use Magento\InventoryImportExport\Model\Export\Source\StockStatus; /** * @inheritdoc */ class Sources extends AbstractEntity { + /** + * @var CollectionBuilder + */ + private $collectionBuilder; /** * @var CollectionFactory @@ -28,21 +29,25 @@ class Sources extends AbstractEntity private $sourceItemCollectionFactory; /** - * @var AttributeFactory + * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig + * @param \Magento\Store\Model\StoreManagerInterface $storeManager + * @param Export\Factory $collectionFactory + * @param \Magento\ImportExport\Model\ResourceModel\CollectionByPagesIteratorFactory $resourceColFactory + * @param CollectionBuilder $collectionBuilder + * @param CollectionFactory $sourceItemCollectionFactory + * @param array $data */ - private $attributeFactory; - public function __construct( \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\ImportExport\Model\Export\Factory $collectionFactory, \Magento\ImportExport\Model\ResourceModel\CollectionByPagesIteratorFactory $resourceColFactory, + CollectionBuilder $collectionBuilder, CollectionFactory $sourceItemCollectionFactory, - AttributeFactory $attributeFactory, array $data = [] ) { + $this->collectionBuilder = $collectionBuilder; $this->sourceItemCollectionFactory = $sourceItemCollectionFactory; - $this->attributeFactory = $attributeFactory; parent::__construct($scopeConfig, $storeManager, $collectionFactory, $resourceColFactory, $data); } @@ -52,42 +57,7 @@ public function __construct( */ public function getAttributeCollection() { - if (count($this->_attributeCollection) === 0) { - /** @var \Magento\Eav\Model\Entity\Attribute $skuAttribute */ - $skuAttribute = $this->attributeFactory->create(); - $skuAttribute->setId(SourceItemInterface::SKU); - $skuAttribute->setDefaultFrontendLabel(SourceItemInterface::SKU); - $skuAttribute->setAttributeCode(SourceItemInterface::SKU); - $this->_attributeCollection->addItem($skuAttribute); - - /** @var \Magento\Eav\Model\Entity\Attribute $sourceIdAttribute */ - $sourceIdAttribute = $this->attributeFactory->create(); - $sourceIdAttribute->setId(SourceItemInterface::SOURCE_ID); - $sourceIdAttribute->setDefaultFrontendLabel(SourceItemInterface::SOURCE_ID); - $sourceIdAttribute->setAttributeCode(SourceItemInterface::SOURCE_ID); - $sourceIdAttribute->setBackendType('int'); - $this->_attributeCollection->addItem($sourceIdAttribute); - - /** @var \Magento\Eav\Model\Entity\Attribute $statusIdAttribute */ - $statusIdAttribute = $this->attributeFactory->create(); - $statusIdAttribute->setId(SourceItemInterface::STATUS); - $statusIdAttribute->setDefaultFrontendLabel(SourceItemInterface::STATUS); - $statusIdAttribute->setAttributeCode(SourceItemInterface::STATUS); - $statusIdAttribute->setBackendType('int'); - $statusIdAttribute->setFrontendInput('select'); - $statusIdAttribute->setSourceModel(StockStatus::class); - $this->_attributeCollection->addItem($statusIdAttribute); - - /** @var \Magento\Eav\Model\Entity\Attribute $quantityAttribute */ - $quantityAttribute = $this->attributeFactory->create(); - $quantityAttribute->setId(SourceItemInterface::QUANTITY); - $quantityAttribute->setBackendType('decimal'); - $quantityAttribute->setDefaultFrontendLabel(SourceItemInterface::QUANTITY); - $quantityAttribute->setAttributeCode(SourceItemInterface::QUANTITY); - $this->_attributeCollection->addItem($quantityAttribute); - } - - return $this->_attributeCollection; + return $this->collectionBuilder->create(); } /** @@ -232,15 +202,14 @@ function($value) { * Get header columns * * @return array + * @throws \Exception */ protected function _getHeaderColumns() { - $columns = [ - SourceItemInterface::SOURCE_ID, - SourceItemInterface::SKU, - SourceItemInterface::STATUS, - SourceItemInterface::QUANTITY - ]; + $columns = []; + foreach ($this->getAttributeCollection()->getItems() as $item) { + $columns[] = $item->getData('id'); + } if (!isset($this->_parameters[Export::FILTER_ELEMENT_SKIP])) { return $columns; From 56317f4e39a16e41bde5031c00dde9e395a15b61 Mon Sep 17 00:00:00 2001 From: Vadim Justus Date: Sat, 28 Oct 2017 15:17:05 +0200 Subject: [PATCH 31/37] MSI: Refactore filtering logic for stock_source export entity --- .../Api/FilterProcessorInterface.php | 23 ++++ .../Model/Export/CollectionBuilder.php | 1 + .../Model/Export/Filter/DatetimeFilter.php | 44 +++++++ .../Model/Export/Filter/IntFilter.php | 42 +++++++ .../Model/Export/Filter/VarcharFilter.php | 27 +++++ .../Export/FilterProcessorAggregator.php | 58 ++++++++++ .../Model/Export/Sources.php | 109 ++++-------------- .../Magento/InventoryImportExport/etc/di.xml | 13 +++ .../InventoryImportExport/etc/export.xml | 2 +- 9 files changed, 232 insertions(+), 87 deletions(-) create mode 100644 app/code/Magento/InventoryImportExport/Api/FilterProcessorInterface.php create mode 100644 app/code/Magento/InventoryImportExport/Model/Export/Filter/DatetimeFilter.php create mode 100644 app/code/Magento/InventoryImportExport/Model/Export/Filter/IntFilter.php create mode 100644 app/code/Magento/InventoryImportExport/Model/Export/Filter/VarcharFilter.php create mode 100644 app/code/Magento/InventoryImportExport/Model/Export/FilterProcessorAggregator.php diff --git a/app/code/Magento/InventoryImportExport/Api/FilterProcessorInterface.php b/app/code/Magento/InventoryImportExport/Api/FilterProcessorInterface.php new file mode 100644 index 000000000000..59540cddb7c1 --- /dev/null +++ b/app/code/Magento/InventoryImportExport/Api/FilterProcessorInterface.php @@ -0,0 +1,23 @@ +attributeFactory->create(); $skuAttribute->setId(SourceItemInterface::SKU); + $skuAttribute->setBackendType('varchar'); $skuAttribute->setDefaultFrontendLabel(SourceItemInterface::SKU); $skuAttribute->setAttributeCode(SourceItemInterface::SKU); $this->collection->addItem($skuAttribute); diff --git a/app/code/Magento/InventoryImportExport/Model/Export/Filter/DatetimeFilter.php b/app/code/Magento/InventoryImportExport/Model/Export/Filter/DatetimeFilter.php new file mode 100644 index 000000000000..f14b8a060927 --- /dev/null +++ b/app/code/Magento/InventoryImportExport/Model/Export/Filter/DatetimeFilter.php @@ -0,0 +1,44 @@ +format('m/d/Y'); + $collection->addFieldToFilter($columnName, ['from' => $date, 'date' => true]); + } + + if (is_scalar($to) && !empty($to)) { + $date = (new \DateTime($to))->format('m/d/Y'); + $collection->addFieldToFilter($columnName, ['to' => $date, 'date' => true]); + } + + return; + } + + $collection->addFieldToFilter($columnName, ['eq' => $value, 'date' => true]); + } +} diff --git a/app/code/Magento/InventoryImportExport/Model/Export/Filter/IntFilter.php b/app/code/Magento/InventoryImportExport/Model/Export/Filter/IntFilter.php new file mode 100644 index 000000000000..fcf454284851 --- /dev/null +++ b/app/code/Magento/InventoryImportExport/Model/Export/Filter/IntFilter.php @@ -0,0 +1,42 @@ +addFieldToFilter($columnName, ['from' => $from]); + } + + if (is_numeric($to) && !empty($to)) { + $collection->addFieldToFilter($columnName, ['to' => $to]); + } + + return; + } + + $collection->addFieldToFilter($columnName, ['eq' => $value]); + } +} diff --git a/app/code/Magento/InventoryImportExport/Model/Export/Filter/VarcharFilter.php b/app/code/Magento/InventoryImportExport/Model/Export/Filter/VarcharFilter.php new file mode 100644 index 000000000000..129c4b976347 --- /dev/null +++ b/app/code/Magento/InventoryImportExport/Model/Export/Filter/VarcharFilter.php @@ -0,0 +1,27 @@ +addFieldToFilter($columnName, ['like' => '%' . $value . '%']); + } +} diff --git a/app/code/Magento/InventoryImportExport/Model/Export/FilterProcessorAggregator.php b/app/code/Magento/InventoryImportExport/Model/Export/FilterProcessorAggregator.php new file mode 100644 index 000000000000..bfe5c5f9d7e2 --- /dev/null +++ b/app/code/Magento/InventoryImportExport/Model/Export/FilterProcessorAggregator.php @@ -0,0 +1,58 @@ + FilterProcessorInterface::class] + )); + } + } + + $this->handler = $handler; + } + + /** + * @param string $type + * @param Collection $collection + * @param string $columnName + * @param string|array $value + * @throws LocalizedException + */ + public function process($type, Collection $collection, $columnName, $value) + { + if (!isset($this->handler[$type])) { + throw new LocalizedException(__( + 'No filter processor for "%type" given.', + ['type' => $type] + )); + } + $this->handler[$type]->process($collection, $columnName, $value); + } +} diff --git a/app/code/Magento/InventoryImportExport/Model/Export/Sources.php b/app/code/Magento/InventoryImportExport/Model/Export/Sources.php index f05007c35431..41825ebbb4ef 100644 --- a/app/code/Magento/InventoryImportExport/Model/Export/Sources.php +++ b/app/code/Magento/InventoryImportExport/Model/Export/Sources.php @@ -6,7 +6,7 @@ namespace Magento\InventoryImportExport\Model\Export; -use Magento\Framework\Data\Collection; +use Magento\Framework\Exception\LocalizedException; use Magento\ImportExport\Model\Export; use Magento\ImportExport\Model\Export\AbstractEntity; use Magento\Inventory\Model\ResourceModel\SourceItem; @@ -28,6 +28,11 @@ class Sources extends AbstractEntity */ private $sourceItemCollectionFactory; + /** + * @var FilterProcessorAggregator + */ + private $filterProcessor; + /** * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig * @param \Magento\Store\Model\StoreManagerInterface $storeManager @@ -35,6 +40,7 @@ class Sources extends AbstractEntity * @param \Magento\ImportExport\Model\ResourceModel\CollectionByPagesIteratorFactory $resourceColFactory * @param CollectionBuilder $collectionBuilder * @param CollectionFactory $sourceItemCollectionFactory + * @param FilterProcessorAggregator $filterProcessor * @param array $data */ public function __construct( @@ -44,10 +50,12 @@ public function __construct( \Magento\ImportExport\Model\ResourceModel\CollectionByPagesIteratorFactory $resourceColFactory, CollectionBuilder $collectionBuilder, CollectionFactory $sourceItemCollectionFactory, + FilterProcessorAggregator $filterProcessor, array $data = [] ) { $this->collectionBuilder = $collectionBuilder; $this->sourceItemCollectionFactory = $sourceItemCollectionFactory; + $this->filterProcessor = $filterProcessor; parent::__construct($scopeConfig, $storeManager, $collectionFactory, $resourceColFactory, $data); } @@ -88,101 +96,30 @@ public function export() } /** - * @param Collection $collection + * @param SourceItemCollection $collection * @throws \Magento\Framework\Exception\LocalizedException */ - private function applyFilters(Collection $collection) + private function applyFilters(SourceItemCollection $collection) { foreach ($this->retrieveFilterDataFromRequest() as $columnName => $value) { $attributeDefinition = $this->getAttributeCollection()->getItemById($columnName); - $type = null; - if ($attributeDefinition) { - $type = $attributeDefinition->getData('backend_type'); - } - - $filterMethodName = 'applyFilterFor' . ucfirst($type); - if (method_exists($this, $filterMethodName)) { - $this->$filterMethodName($collection, $columnName, $value); - } else { - $this->applyDefaultFilter($collection, $columnName, $value); - } - } - } - - /** - * @param Collection $collection - * @param string $columnName - * @param mixed $value - * @throws \Magento\Framework\Exception\LocalizedException - */ - public function applyFilterForInt(Collection $collection, $columnName, $value) - { - if (is_array($value)) { - $from = $value[0] ?? null; - $to = $value[1] ?? null; - - if (is_numeric($from) && !empty($from)) { - $collection->addFieldToFilter($columnName, ['from' => $from]); - } - - if (is_numeric($to) && !empty($to)) { - $collection->addFieldToFilter($columnName, ['to' => $to]); - } - - return; - } - - $collection->addFieldToFilter($columnName, ['eq' => $value]); - } - - /** - * @param Collection $collection - * @param string $columnName - * @param mixed $value - * @throws \Magento\Framework\Exception\LocalizedException - */ - public function applyFilterForDecimal(Collection $collection, $columnName, $value) - { - $this->applyFilterForInt($collection, $columnName, $value); - } - - /** - * @param Collection $collection - * @param string $columnName - * @param mixed $value - * @throws \Magento\Framework\Exception\LocalizedException - */ - public function applyDefaultFilter(Collection $collection, $columnName, $value) - { - $collection->addFieldToFilter($columnName, ['like' => '%' . $value . '%']); - } - - /** - * @param Collection $collection - * @param string $columnName - * @param mixed $value - * @throws \Magento\Framework\Exception\LocalizedException - */ - public function applyFilterForDatetime(Collection $collection, $columnName, $value) - { - if (is_array($value)) { - $from = $value[0] ?? null; - $to = $value[1] ?? null; - - if (is_scalar($from) && !empty($from)) { - $date = (new \DateTime($from))->format('m/d/Y'); - $collection->addFieldToFilter($columnName, ['from' => $date, 'date' => true]); + if (!$attributeDefinition) { + throw new LocalizedException(__( + 'Given column name "%columnName" is not present in collection.', + ['columnName' => $columnName] + )); } - if (is_scalar($to) && !empty($to)) { - $date = (new \DateTime($to))->format('m/d/Y'); - $collection->addFieldToFilter($columnName, ['to' => $date, 'date' => true]); + $type = $attributeDefinition->getData('backend_type'); + if (!$type) { + throw new LocalizedException(__( + 'There is no backend type specified for column "%columnName".', + ['columnName' => $columnName] + )); } - return; + $this->filterProcessor->process($type, $collection, $columnName, $value); } - - $this->applyDefaultFilter($collection, $columnName, $value); } /** diff --git a/app/code/Magento/InventoryImportExport/etc/di.xml b/app/code/Magento/InventoryImportExport/etc/di.xml index 3b147254f706..fd9e4625f46c 100644 --- a/app/code/Magento/InventoryImportExport/etc/di.xml +++ b/app/code/Magento/InventoryImportExport/etc/di.xml @@ -27,4 +27,17 @@ + + + + + + Magento\InventoryImportExport\Model\Export\Filter\IntFilter + + Magento\InventoryImportExport\Model\Export\Filter\IntFilter + Magento\InventoryImportExport\Model\Export\Filter\DatetimeFilter + Magento\InventoryImportExport\Model\Export\Filter\VarcharFilter + + + diff --git a/app/code/Magento/InventoryImportExport/etc/export.xml b/app/code/Magento/InventoryImportExport/etc/export.xml index ace04ed0d202..da4f062e5c3f 100644 --- a/app/code/Magento/InventoryImportExport/etc/export.xml +++ b/app/code/Magento/InventoryImportExport/etc/export.xml @@ -7,4 +7,4 @@ --> - \ No newline at end of file + From de65ba58ff3ce7f4cdbb2fde785daca50b12702f Mon Sep 17 00:00:00 2001 From: Vadim Justus Date: Sat, 28 Oct 2017 15:35:07 +0200 Subject: [PATCH 32/37] MSI: Static improvements --- .../Model/Export/Sources.php | 36 +++++++------------ 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/app/code/Magento/InventoryImportExport/Model/Export/Sources.php b/app/code/Magento/InventoryImportExport/Model/Export/Sources.php index 41825ebbb4ef..a772f97d5b6f 100644 --- a/app/code/Magento/InventoryImportExport/Model/Export/Sources.php +++ b/app/code/Magento/InventoryImportExport/Model/Export/Sources.php @@ -11,7 +11,8 @@ use Magento\ImportExport\Model\Export\AbstractEntity; use Magento\Inventory\Model\ResourceModel\SourceItem; use Magento\Inventory\Model\ResourceModel\SourceItem\Collection as SourceItemCollection; -use Magento\Inventory\Model\ResourceModel\SourceItem\CollectionFactory; +use Magento\Inventory\Model\ResourceModel\SourceItem\CollectionFactory as SourceItemCollectionFactory; +use Exception; /** * @inheritdoc @@ -24,7 +25,7 @@ class Sources extends AbstractEntity private $collectionBuilder; /** - * @var CollectionFactory + * @var SourceItemCollectionFactory */ private $sourceItemCollectionFactory; @@ -39,7 +40,7 @@ class Sources extends AbstractEntity * @param Export\Factory $collectionFactory * @param \Magento\ImportExport\Model\ResourceModel\CollectionByPagesIteratorFactory $resourceColFactory * @param CollectionBuilder $collectionBuilder - * @param CollectionFactory $sourceItemCollectionFactory + * @param SourceItemCollectionFactory $sourceItemCollectionFactory * @param FilterProcessorAggregator $filterProcessor * @param array $data */ @@ -49,7 +50,7 @@ public function __construct( \Magento\ImportExport\Model\Export\Factory $collectionFactory, \Magento\ImportExport\Model\ResourceModel\CollectionByPagesIteratorFactory $resourceColFactory, CollectionBuilder $collectionBuilder, - CollectionFactory $sourceItemCollectionFactory, + SourceItemCollectionFactory $sourceItemCollectionFactory, FilterProcessorAggregator $filterProcessor, array $data = [] ) { @@ -69,10 +70,8 @@ public function getAttributeCollection() } /** - * Export process - * - * @return string - * @throws \Magento\Framework\Exception\LocalizedException + * @inheritdoc + * @throws LocalizedException */ public function export() { @@ -97,7 +96,7 @@ public function export() /** * @param SourceItemCollection $collection - * @throws \Magento\Framework\Exception\LocalizedException + * @throws LocalizedException */ private function applyFilters(SourceItemCollection $collection) { @@ -136,10 +135,8 @@ function($value) { } /** - * Get header columns - * - * @return array - * @throws \Exception + * @inheritdoc + * @throws Exception */ protected function _getHeaderColumns() { @@ -164,10 +161,7 @@ protected function _getHeaderColumns() } /** - * Export one item - * - * @param \Magento\Framework\Model\AbstractModel $item - * @return void + * @inheritdoc */ public function exportItem($item) { @@ -175,9 +169,7 @@ public function exportItem($item) } /** - * Entity type code getter - * - * @return string + * @inheritdoc */ public function getEntityTypeCode() { @@ -185,9 +177,7 @@ public function getEntityTypeCode() } /** - * Get entity collection - * - * @return \Magento\Framework\Data\Collection\AbstractDb + * @inheritdoc */ protected function _getEntityCollection() { From fe30b3d7391b4392661df0c21f3e38b52657a061 Mon Sep 17 00:00:00 2001 From: Vadim Justus Date: Sat, 28 Oct 2017 17:37:29 +0200 Subject: [PATCH 33/37] MSI: Refactore stock_source export entity dependencies --- .../Api/ColumnProviderInterface.php | 29 ++++ .../SourceItemCollectionFactoryInterface.php | 25 ++++ ...er.php => AttributeCollectionProvider.php} | 4 +- .../Model/Export/ColumnProvider.php | 54 ++++++++ .../Export/SourceItemCollectionFactory.php | 100 ++++++++++++++ .../Model/Export/Sources.php | 129 +++++------------- .../Magento/InventoryImportExport/etc/di.xml | 2 + 7 files changed, 249 insertions(+), 94 deletions(-) create mode 100644 app/code/Magento/InventoryImportExport/Api/ColumnProviderInterface.php create mode 100644 app/code/Magento/InventoryImportExport/Api/SourceItemCollectionFactoryInterface.php rename app/code/Magento/InventoryImportExport/Model/Export/{CollectionBuilder.php => AttributeCollectionProvider.php} (97%) create mode 100644 app/code/Magento/InventoryImportExport/Model/Export/ColumnProvider.php create mode 100644 app/code/Magento/InventoryImportExport/Model/Export/SourceItemCollectionFactory.php diff --git a/app/code/Magento/InventoryImportExport/Api/ColumnProviderInterface.php b/app/code/Magento/InventoryImportExport/Api/ColumnProviderInterface.php new file mode 100644 index 000000000000..b4292883977e --- /dev/null +++ b/app/code/Magento/InventoryImportExport/Api/ColumnProviderInterface.php @@ -0,0 +1,29 @@ +collection) === 0) { /** @var \Magento\Eav\Model\Entity\Attribute $sourceIdAttribute */ diff --git a/app/code/Magento/InventoryImportExport/Model/Export/ColumnProvider.php b/app/code/Magento/InventoryImportExport/Model/Export/ColumnProvider.php new file mode 100644 index 000000000000..1876e2e05c4f --- /dev/null +++ b/app/code/Magento/InventoryImportExport/Model/Export/ColumnProvider.php @@ -0,0 +1,54 @@ +getItems() as $item) { + $columns[] = $item->getData('id'); + } + + if (!isset($filters[Export::FILTER_ELEMENT_SKIP])) { + return $columns; + } + + // remove the skipped from columns + $skippedAttributes = array_flip($filters[Export::FILTER_ELEMENT_SKIP]); + foreach ($columns as $key => $value) { + if (array_key_exists($value, $skippedAttributes) === true) { + unset($columns[$key]); + } + } + + return $columns; + } + + /** + * @param AttributeCollection $attributeCollection + * @param array $filters + * @return array + */ + public function getColumns(AttributeCollection $attributeCollection, array $filters): array + { + return $this->getHeaders($attributeCollection, $filters); + } +} diff --git a/app/code/Magento/InventoryImportExport/Model/Export/SourceItemCollectionFactory.php b/app/code/Magento/InventoryImportExport/Model/Export/SourceItemCollectionFactory.php new file mode 100644 index 000000000000..b572b0d2cdc3 --- /dev/null +++ b/app/code/Magento/InventoryImportExport/Model/Export/SourceItemCollectionFactory.php @@ -0,0 +1,100 @@ +objectManager = $objectManager; + $this->filterProcessor = $filterProcessor; + $this->columnProvider = $columnProvider; + } + + /** + * @param AttributeCollection $attributeCollection + * @param array $filters + * @return Collection + * @throws LocalizedException + */ + public function create(AttributeCollection $attributeCollection, array $filters): Collection + { + /** @var Collection $collection */ + $collection = $this->objectManager->create(Collection::class); + $collection->addFieldToSelect($this->columnProvider->getColumns($attributeCollection, $filters)); + + foreach ($this->retrieveFilterData($filters) as $columnName => $value) { + $attributeDefinition = $attributeCollection->getItemById($columnName); + if (!$attributeDefinition) { + throw new LocalizedException(__( + 'Given column name "%columnName" is not present in collection.', + ['columnName' => $columnName] + )); + } + + $type = $attributeDefinition->getData('backend_type'); + if (!$type) { + throw new LocalizedException(__( + 'There is no backend type specified for column "%columnName".', + ['columnName' => $columnName] + )); + } + + $this->filterProcessor->process($type, $collection, $columnName, $value); + } + + return $collection; + } + + /** + * @param array $filters + * @return array + */ + private function retrieveFilterData(array $filters) + { + return array_filter( + $filters[Export::FILTER_ELEMENT_GROUP] ?? [], + function($value) { + return $value !== ''; + } + ); + } +} diff --git a/app/code/Magento/InventoryImportExport/Model/Export/Sources.php b/app/code/Magento/InventoryImportExport/Model/Export/Sources.php index a772f97d5b6f..b88ba26ca2cb 100644 --- a/app/code/Magento/InventoryImportExport/Model/Export/Sources.php +++ b/app/code/Magento/InventoryImportExport/Model/Export/Sources.php @@ -6,13 +6,16 @@ namespace Magento\InventoryImportExport\Model\Export; -use Magento\Framework\Exception\LocalizedException; -use Magento\ImportExport\Model\Export; +use Exception; +use Magento\Framework\App\Config\ScopeConfigInterface; +use Magento\ImportExport\Model\Export\Factory as ExportFactory; use Magento\ImportExport\Model\Export\AbstractEntity; use Magento\Inventory\Model\ResourceModel\SourceItem; use Magento\Inventory\Model\ResourceModel\SourceItem\Collection as SourceItemCollection; -use Magento\Inventory\Model\ResourceModel\SourceItem\CollectionFactory as SourceItemCollectionFactory; -use Exception; +use Magento\InventoryImportExport\Api\SourceItemCollectionFactoryInterface; +use Magento\InventoryImportExport\Api\ColumnProviderInterface; +use Magento\Store\Model\StoreManagerInterface; +use Magento\ImportExport\Model\ResourceModel\CollectionByPagesIteratorFactory; /** * @inheritdoc @@ -20,43 +23,43 @@ class Sources extends AbstractEntity { /** - * @var CollectionBuilder + * @var AttributeCollectionProvider */ - private $collectionBuilder; + private $attributeCollectionProvider; /** - * @var SourceItemCollectionFactory + * @var SourceItemCollectionFactoryInterface */ private $sourceItemCollectionFactory; /** - * @var FilterProcessorAggregator + * @var ColumnProviderInterface */ - private $filterProcessor; + private $columnProvider; /** - * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig - * @param \Magento\Store\Model\StoreManagerInterface $storeManager - * @param Export\Factory $collectionFactory - * @param \Magento\ImportExport\Model\ResourceModel\CollectionByPagesIteratorFactory $resourceColFactory - * @param CollectionBuilder $collectionBuilder - * @param SourceItemCollectionFactory $sourceItemCollectionFactory - * @param FilterProcessorAggregator $filterProcessor + * @param ScopeConfigInterface $scopeConfig + * @param StoreManagerInterface $storeManager + * @param ExportFactory $collectionFactory + * @param CollectionByPagesIteratorFactory $resourceColFactory + * @param AttributeCollectionProvider $attributeCollectionProvider + * @param SourceItemCollectionFactoryInterface $sourceItemCollectionFactory + * @param ColumnProviderInterface $columnProvider * @param array $data */ public function __construct( - \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, - \Magento\Store\Model\StoreManagerInterface $storeManager, - \Magento\ImportExport\Model\Export\Factory $collectionFactory, - \Magento\ImportExport\Model\ResourceModel\CollectionByPagesIteratorFactory $resourceColFactory, - CollectionBuilder $collectionBuilder, - SourceItemCollectionFactory $sourceItemCollectionFactory, - FilterProcessorAggregator $filterProcessor, + ScopeConfigInterface $scopeConfig, + StoreManagerInterface $storeManager, + ExportFactory $collectionFactory, + CollectionByPagesIteratorFactory $resourceColFactory, + AttributeCollectionProvider $attributeCollectionProvider, + SourceItemCollectionFactoryInterface $sourceItemCollectionFactory, + ColumnProviderInterface $columnProvider, array $data = [] ) { - $this->collectionBuilder = $collectionBuilder; + $this->attributeCollectionProvider = $attributeCollectionProvider; $this->sourceItemCollectionFactory = $sourceItemCollectionFactory; - $this->filterProcessor = $filterProcessor; + $this->columnProvider = $columnProvider; parent::__construct($scopeConfig, $storeManager, $collectionFactory, $resourceColFactory, $data); } @@ -66,25 +69,23 @@ public function __construct( */ public function getAttributeCollection() { - return $this->collectionBuilder->create(); + return $this->attributeCollectionProvider->get(); } /** * @inheritdoc - * @throws LocalizedException + * @throws Exception */ public function export() { $writer = $this->getWriter(); - - $columns = $this->_getHeaderColumns(); - $writer->setHeaderCols($columns); + $writer->setHeaderCols($this->_getHeaderColumns()); /** @var SourceItemCollection $collection */ - $collection = $this->sourceItemCollectionFactory->create(); - $collection->addFieldToSelect($columns); - - $this->applyFilters($collection); + $collection = $this->sourceItemCollectionFactory->create( + $this->getAttributeCollection(), + $this->_parameters + ); foreach ($collection->getData() as $data) { unset($data[SourceItem::ID_FIELD_NAME]); @@ -94,74 +95,18 @@ public function export() return $writer->getContents(); } - /** - * @param SourceItemCollection $collection - * @throws LocalizedException - */ - private function applyFilters(SourceItemCollection $collection) - { - foreach ($this->retrieveFilterDataFromRequest() as $columnName => $value) { - $attributeDefinition = $this->getAttributeCollection()->getItemById($columnName); - if (!$attributeDefinition) { - throw new LocalizedException(__( - 'Given column name "%columnName" is not present in collection.', - ['columnName' => $columnName] - )); - } - - $type = $attributeDefinition->getData('backend_type'); - if (!$type) { - throw new LocalizedException(__( - 'There is no backend type specified for column "%columnName".', - ['columnName' => $columnName] - )); - } - - $this->filterProcessor->process($type, $collection, $columnName, $value); - } - } - - /** - * @return array - */ - private function retrieveFilterDataFromRequest() - { - return array_filter( - $this->_parameters[Export::FILTER_ELEMENT_GROUP] ?? [], - function($value) { - return $value !== ''; - } - ); - } - /** * @inheritdoc * @throws Exception */ protected function _getHeaderColumns() { - $columns = []; - foreach ($this->getAttributeCollection()->getItems() as $item) { - $columns[] = $item->getData('id'); - } - - if (!isset($this->_parameters[Export::FILTER_ELEMENT_SKIP])) { - return $columns; - } - - // remove the skipped from columns - $skippedAttributes = array_flip($this->_parameters[Export::FILTER_ELEMENT_SKIP]); - foreach ($columns as $key => $value) { - if (array_key_exists($value, $skippedAttributes) === true) { - unset($columns[$key]); - } - } - - return $columns; + return $this->columnProvider->getHeaders($this->getAttributeCollection(), $this->_parameters); } /** * @inheritdoc + * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function exportItem($item) { diff --git a/app/code/Magento/InventoryImportExport/etc/di.xml b/app/code/Magento/InventoryImportExport/etc/di.xml index fd9e4625f46c..97519fd653d6 100644 --- a/app/code/Magento/InventoryImportExport/etc/di.xml +++ b/app/code/Magento/InventoryImportExport/etc/di.xml @@ -29,6 +29,8 @@ + + From c16378fc5a73bb7cb73f261c8adefdb10e7e03f6 Mon Sep 17 00:00:00 2001 From: Vadim Justus Date: Sun, 29 Oct 2017 09:01:33 +0100 Subject: [PATCH 34/37] MSI: Fix static code style issues --- .../Model/Export/SourceItemCollectionFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/InventoryImportExport/Model/Export/SourceItemCollectionFactory.php b/app/code/Magento/InventoryImportExport/Model/Export/SourceItemCollectionFactory.php index b572b0d2cdc3..687e783cb908 100644 --- a/app/code/Magento/InventoryImportExport/Model/Export/SourceItemCollectionFactory.php +++ b/app/code/Magento/InventoryImportExport/Model/Export/SourceItemCollectionFactory.php @@ -92,7 +92,7 @@ private function retrieveFilterData(array $filters) { return array_filter( $filters[Export::FILTER_ELEMENT_GROUP] ?? [], - function($value) { + function ($value) { return $value !== ''; } ); From 18ad3787280c10f2187c23d0d2fcedfe244c1e80 Mon Sep 17 00:00:00 2001 From: Vadim Justus Date: Sun, 29 Oct 2017 09:13:29 +0100 Subject: [PATCH 35/37] MSI: Move SPI interfaces for export to model directory --- .../InventoryImportExport/Model/Export/ColumnProvider.php | 2 +- .../{Api => Model/Export}/ColumnProviderInterface.php | 2 +- .../Model/Export/Filter/DatetimeFilter.php | 2 +- .../InventoryImportExport/Model/Export/Filter/IntFilter.php | 2 +- .../Model/Export/Filter/VarcharFilter.php | 2 +- .../Model/Export/FilterProcessorAggregator.php | 2 +- .../{Api => Model/Export}/FilterProcessorInterface.php | 2 +- .../Model/Export/SourceItemCollectionFactory.php | 4 ++-- .../Export}/SourceItemCollectionFactoryInterface.php | 2 +- .../Magento/InventoryImportExport/Model/Export/Sources.php | 4 ++-- app/code/Magento/InventoryImportExport/etc/di.xml | 4 ++-- 11 files changed, 14 insertions(+), 14 deletions(-) rename app/code/Magento/InventoryImportExport/{Api => Model/Export}/ColumnProviderInterface.php (92%) rename app/code/Magento/InventoryImportExport/{Api => Model/Export}/FilterProcessorInterface.php (89%) rename app/code/Magento/InventoryImportExport/{Api => Model/Export}/SourceItemCollectionFactoryInterface.php (91%) diff --git a/app/code/Magento/InventoryImportExport/Model/Export/ColumnProvider.php b/app/code/Magento/InventoryImportExport/Model/Export/ColumnProvider.php index 1876e2e05c4f..57902772f6a1 100644 --- a/app/code/Magento/InventoryImportExport/Model/Export/ColumnProvider.php +++ b/app/code/Magento/InventoryImportExport/Model/Export/ColumnProvider.php @@ -7,7 +7,7 @@ namespace Magento\InventoryImportExport\Model\Export; use Magento\Framework\Data\Collection as AttributeCollection; -use Magento\InventoryImportExport\Api\ColumnProviderInterface; +use Magento\InventoryImportExport\Model\Export\ColumnProviderInterface; use Magento\ImportExport\Model\Export; /** diff --git a/app/code/Magento/InventoryImportExport/Api/ColumnProviderInterface.php b/app/code/Magento/InventoryImportExport/Model/Export/ColumnProviderInterface.php similarity index 92% rename from app/code/Magento/InventoryImportExport/Api/ColumnProviderInterface.php rename to app/code/Magento/InventoryImportExport/Model/Export/ColumnProviderInterface.php index b4292883977e..4d74dab03c79 100644 --- a/app/code/Magento/InventoryImportExport/Api/ColumnProviderInterface.php +++ b/app/code/Magento/InventoryImportExport/Model/Export/ColumnProviderInterface.php @@ -4,7 +4,7 @@ * See COPYING.txt for license details. */ -namespace Magento\InventoryImportExport\Api; +namespace Magento\InventoryImportExport\Model\Export; use Magento\Framework\Data\Collection as AttributeCollection; diff --git a/app/code/Magento/InventoryImportExport/Model/Export/Filter/DatetimeFilter.php b/app/code/Magento/InventoryImportExport/Model/Export/Filter/DatetimeFilter.php index f14b8a060927..ce3c078cbd8b 100644 --- a/app/code/Magento/InventoryImportExport/Model/Export/Filter/DatetimeFilter.php +++ b/app/code/Magento/InventoryImportExport/Model/Export/Filter/DatetimeFilter.php @@ -7,7 +7,7 @@ namespace Magento\InventoryImportExport\Model\Export\Filter; use Magento\Inventory\Model\ResourceModel\SourceItem\Collection; -use Magento\InventoryImportExport\Api\FilterProcessorInterface; +use Magento\InventoryImportExport\Model\Export\FilterProcessorInterface; /** * @inheritdoc diff --git a/app/code/Magento/InventoryImportExport/Model/Export/Filter/IntFilter.php b/app/code/Magento/InventoryImportExport/Model/Export/Filter/IntFilter.php index fcf454284851..4ec23a53eb0d 100644 --- a/app/code/Magento/InventoryImportExport/Model/Export/Filter/IntFilter.php +++ b/app/code/Magento/InventoryImportExport/Model/Export/Filter/IntFilter.php @@ -7,7 +7,7 @@ namespace Magento\InventoryImportExport\Model\Export\Filter; use Magento\Inventory\Model\ResourceModel\SourceItem\Collection; -use Magento\InventoryImportExport\Api\FilterProcessorInterface; +use Magento\InventoryImportExport\Model\Export\FilterProcessorInterface; /** * @inheritdoc diff --git a/app/code/Magento/InventoryImportExport/Model/Export/Filter/VarcharFilter.php b/app/code/Magento/InventoryImportExport/Model/Export/Filter/VarcharFilter.php index 129c4b976347..e23ad36639c9 100644 --- a/app/code/Magento/InventoryImportExport/Model/Export/Filter/VarcharFilter.php +++ b/app/code/Magento/InventoryImportExport/Model/Export/Filter/VarcharFilter.php @@ -7,7 +7,7 @@ namespace Magento\InventoryImportExport\Model\Export\Filter; use Magento\Inventory\Model\ResourceModel\SourceItem\Collection; -use Magento\InventoryImportExport\Api\FilterProcessorInterface; +use Magento\InventoryImportExport\Model\Export\FilterProcessorInterface; /** * @inheritdoc diff --git a/app/code/Magento/InventoryImportExport/Model/Export/FilterProcessorAggregator.php b/app/code/Magento/InventoryImportExport/Model/Export/FilterProcessorAggregator.php index bfe5c5f9d7e2..a7e999f4ef77 100644 --- a/app/code/Magento/InventoryImportExport/Model/Export/FilterProcessorAggregator.php +++ b/app/code/Magento/InventoryImportExport/Model/Export/FilterProcessorAggregator.php @@ -8,7 +8,7 @@ use Magento\Framework\Exception\LocalizedException; use Magento\Inventory\Model\ResourceModel\SourceItem\Collection; -use Magento\InventoryImportExport\Api\FilterProcessorInterface; +use Magento\InventoryImportExport\Model\Export\FilterProcessorInterface; /** * @api diff --git a/app/code/Magento/InventoryImportExport/Api/FilterProcessorInterface.php b/app/code/Magento/InventoryImportExport/Model/Export/FilterProcessorInterface.php similarity index 89% rename from app/code/Magento/InventoryImportExport/Api/FilterProcessorInterface.php rename to app/code/Magento/InventoryImportExport/Model/Export/FilterProcessorInterface.php index 59540cddb7c1..70cfea877c70 100644 --- a/app/code/Magento/InventoryImportExport/Api/FilterProcessorInterface.php +++ b/app/code/Magento/InventoryImportExport/Model/Export/FilterProcessorInterface.php @@ -4,7 +4,7 @@ * See COPYING.txt for license details. */ -namespace Magento\InventoryImportExport\Api; +namespace Magento\InventoryImportExport\Model\Export; use Magento\Inventory\Model\ResourceModel\SourceItem\Collection; diff --git a/app/code/Magento/InventoryImportExport/Model/Export/SourceItemCollectionFactory.php b/app/code/Magento/InventoryImportExport/Model/Export/SourceItemCollectionFactory.php index 687e783cb908..154b6ed4d1c6 100644 --- a/app/code/Magento/InventoryImportExport/Model/Export/SourceItemCollectionFactory.php +++ b/app/code/Magento/InventoryImportExport/Model/Export/SourceItemCollectionFactory.php @@ -10,8 +10,8 @@ use Magento\Framework\Exception\LocalizedException; use Magento\Framework\ObjectManagerInterface; use Magento\Inventory\Model\ResourceModel\SourceItem\Collection; -use Magento\InventoryImportExport\Api\ColumnProviderInterface; -use Magento\InventoryImportExport\Api\SourceItemCollectionFactoryInterface; +use Magento\InventoryImportExport\Model\Export\ColumnProviderInterface; +use Magento\InventoryImportExport\Model\Export\SourceItemCollectionFactoryInterface; use Magento\ImportExport\Model\Export; /** diff --git a/app/code/Magento/InventoryImportExport/Api/SourceItemCollectionFactoryInterface.php b/app/code/Magento/InventoryImportExport/Model/Export/SourceItemCollectionFactoryInterface.php similarity index 91% rename from app/code/Magento/InventoryImportExport/Api/SourceItemCollectionFactoryInterface.php rename to app/code/Magento/InventoryImportExport/Model/Export/SourceItemCollectionFactoryInterface.php index d2c255d503f2..ba2fb9679fe9 100644 --- a/app/code/Magento/InventoryImportExport/Api/SourceItemCollectionFactoryInterface.php +++ b/app/code/Magento/InventoryImportExport/Model/Export/SourceItemCollectionFactoryInterface.php @@ -4,7 +4,7 @@ * See COPYING.txt for license details. */ -namespace Magento\InventoryImportExport\Api; +namespace Magento\InventoryImportExport\Model\Export; use Magento\Framework\Exception\LocalizedException; use Magento\Inventory\Model\ResourceModel\SourceItem\Collection; diff --git a/app/code/Magento/InventoryImportExport/Model/Export/Sources.php b/app/code/Magento/InventoryImportExport/Model/Export/Sources.php index b88ba26ca2cb..e900c193ed9a 100644 --- a/app/code/Magento/InventoryImportExport/Model/Export/Sources.php +++ b/app/code/Magento/InventoryImportExport/Model/Export/Sources.php @@ -12,8 +12,8 @@ use Magento\ImportExport\Model\Export\AbstractEntity; use Magento\Inventory\Model\ResourceModel\SourceItem; use Magento\Inventory\Model\ResourceModel\SourceItem\Collection as SourceItemCollection; -use Magento\InventoryImportExport\Api\SourceItemCollectionFactoryInterface; -use Magento\InventoryImportExport\Api\ColumnProviderInterface; +use Magento\InventoryImportExport\Model\Export\SourceItemCollectionFactoryInterface; +use Magento\InventoryImportExport\Model\Export\ColumnProviderInterface; use Magento\Store\Model\StoreManagerInterface; use Magento\ImportExport\Model\ResourceModel\CollectionByPagesIteratorFactory; diff --git a/app/code/Magento/InventoryImportExport/etc/di.xml b/app/code/Magento/InventoryImportExport/etc/di.xml index 97519fd653d6..31227dfc44a3 100644 --- a/app/code/Magento/InventoryImportExport/etc/di.xml +++ b/app/code/Magento/InventoryImportExport/etc/di.xml @@ -29,8 +29,8 @@ - - + + From d41337aa73273751fa1e469ab534c50ca4dba9d3 Mon Sep 17 00:00:00 2001 From: Vadim Justus Date: Sun, 29 Oct 2017 09:22:36 +0100 Subject: [PATCH 36/37] MSI: remove useless datetime filter processor --- .../Model/Export/ColumnProvider.php | 8 +--- .../Model/Export/Filter/DatetimeFilter.php | 44 ------------------- .../Magento/InventoryImportExport/etc/di.xml | 1 - 3 files changed, 2 insertions(+), 51 deletions(-) delete mode 100644 app/code/Magento/InventoryImportExport/Model/Export/Filter/DatetimeFilter.php diff --git a/app/code/Magento/InventoryImportExport/Model/Export/ColumnProvider.php b/app/code/Magento/InventoryImportExport/Model/Export/ColumnProvider.php index 57902772f6a1..1592f80edabf 100644 --- a/app/code/Magento/InventoryImportExport/Model/Export/ColumnProvider.php +++ b/app/code/Magento/InventoryImportExport/Model/Export/ColumnProvider.php @@ -16,9 +16,7 @@ class ColumnProvider implements ColumnProviderInterface { /** - * @param AttributeCollection $attributeCollection - * @param array $filters - * @return array + * @inheritdoc */ public function getHeaders(AttributeCollection $attributeCollection, array $filters): array { @@ -43,9 +41,7 @@ public function getHeaders(AttributeCollection $attributeCollection, array $filt } /** - * @param AttributeCollection $attributeCollection - * @param array $filters - * @return array + * @inheritdoc */ public function getColumns(AttributeCollection $attributeCollection, array $filters): array { diff --git a/app/code/Magento/InventoryImportExport/Model/Export/Filter/DatetimeFilter.php b/app/code/Magento/InventoryImportExport/Model/Export/Filter/DatetimeFilter.php deleted file mode 100644 index ce3c078cbd8b..000000000000 --- a/app/code/Magento/InventoryImportExport/Model/Export/Filter/DatetimeFilter.php +++ /dev/null @@ -1,44 +0,0 @@ -format('m/d/Y'); - $collection->addFieldToFilter($columnName, ['from' => $date, 'date' => true]); - } - - if (is_scalar($to) && !empty($to)) { - $date = (new \DateTime($to))->format('m/d/Y'); - $collection->addFieldToFilter($columnName, ['to' => $date, 'date' => true]); - } - - return; - } - - $collection->addFieldToFilter($columnName, ['eq' => $value, 'date' => true]); - } -} diff --git a/app/code/Magento/InventoryImportExport/etc/di.xml b/app/code/Magento/InventoryImportExport/etc/di.xml index 31227dfc44a3..9fca572cce16 100644 --- a/app/code/Magento/InventoryImportExport/etc/di.xml +++ b/app/code/Magento/InventoryImportExport/etc/di.xml @@ -37,7 +37,6 @@ Magento\InventoryImportExport\Model\Export\Filter\IntFilter Magento\InventoryImportExport\Model\Export\Filter\IntFilter - Magento\InventoryImportExport\Model\Export\Filter\DatetimeFilter Magento\InventoryImportExport\Model\Export\Filter\VarcharFilter From d70bb8f229ca2b055b14be3ae79d6eee23cc63cd Mon Sep 17 00:00:00 2001 From: Igor Miniailo Date: Sun, 29 Oct 2017 11:22:00 +0200 Subject: [PATCH 37/37] MSI Import/Export: added comments for SPI interfaces --- .../Model/Export/ColumnProviderInterface.php | 4 ++++ .../Model/Export/FilterProcessorInterface.php | 4 ++++ .../Model/Export/SourceItemCollectionFactoryInterface.php | 2 ++ 3 files changed, 10 insertions(+) diff --git a/app/code/Magento/InventoryImportExport/Model/Export/ColumnProviderInterface.php b/app/code/Magento/InventoryImportExport/Model/Export/ColumnProviderInterface.php index 4d74dab03c79..864029e2c784 100644 --- a/app/code/Magento/InventoryImportExport/Model/Export/ColumnProviderInterface.php +++ b/app/code/Magento/InventoryImportExport/Model/Export/ColumnProviderInterface.php @@ -14,6 +14,8 @@ interface ColumnProviderInterface { /** + * Returns header names for exported file + * * @param AttributeCollection $attributeCollection * @param array $filters * @return array @@ -21,6 +23,8 @@ interface ColumnProviderInterface public function getHeaders(AttributeCollection $attributeCollection, array $filters): array; /** + * Returns column names for Collection Select + * * @param AttributeCollection $attributeCollection * @param array $filters * @return array diff --git a/app/code/Magento/InventoryImportExport/Model/Export/FilterProcessorInterface.php b/app/code/Magento/InventoryImportExport/Model/Export/FilterProcessorInterface.php index 70cfea877c70..a2081ede8441 100644 --- a/app/code/Magento/InventoryImportExport/Model/Export/FilterProcessorInterface.php +++ b/app/code/Magento/InventoryImportExport/Model/Export/FilterProcessorInterface.php @@ -14,6 +14,10 @@ interface FilterProcessorInterface { /** + * Filter Processor Interface is used as an Extension Point for each Attribute Data Type (Backend Type) + * to process filtering applied from Export Grid UI + * to all attributes of Entity being exported + * * @param Collection $collection * @param string $columnName * @param array|string $value diff --git a/app/code/Magento/InventoryImportExport/Model/Export/SourceItemCollectionFactoryInterface.php b/app/code/Magento/InventoryImportExport/Model/Export/SourceItemCollectionFactoryInterface.php index ba2fb9679fe9..a65bedfeb0e9 100644 --- a/app/code/Magento/InventoryImportExport/Model/Export/SourceItemCollectionFactoryInterface.php +++ b/app/code/Magento/InventoryImportExport/Model/Export/SourceItemCollectionFactoryInterface.php @@ -16,6 +16,8 @@ interface SourceItemCollectionFactoryInterface { /** + * SourceItemCollection is used to gather all the data (with filters applied) which need to be exported + * * @param AttributeCollection $attributeCollection * @param array $filters * @return Collection