BulkConfigurationTransfer.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\InventoryLowQuantityNotification\Model\ResourceModel;
  8. use Magento\Framework\App\ResourceConnection;
  9. use Magento\Framework\DB\Adapter\DuplicateException;
  10. use Magento\InventoryCatalogApi\Model\GetProductTypesBySkusInterface;
  11. use Magento\InventoryConfigurationApi\Model\IsSourceItemManagementAllowedForProductTypeInterface;
  12. /**
  13. * Bulk configuration transfer resource model
  14. */
  15. class BulkConfigurationTransfer
  16. {
  17. /**
  18. * @var ResourceConnection
  19. */
  20. private $resourceConnection;
  21. /**
  22. * @var GetProductTypesBySkusInterface
  23. */
  24. private $getProductTypesBySkus;
  25. /**
  26. * @var IsSourceItemManagementAllowedForProductTypeInterface
  27. */
  28. private $isSourceItemManagementAllowedForProductType;
  29. /**
  30. * @param ResourceConnection $resourceConnection
  31. * @param GetProductTypesBySkusInterface $getProductTypesBySkus
  32. * @param IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemManagementAllowedForProductType
  33. * @SuppressWarnings(PHPMD.LongVariable)
  34. */
  35. public function __construct(
  36. ResourceConnection $resourceConnection,
  37. GetProductTypesBySkusInterface $getProductTypesBySkus,
  38. IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemManagementAllowedForProductType
  39. ) {
  40. $this->resourceConnection = $resourceConnection;
  41. $this->getProductTypesBySkus = $getProductTypesBySkus;
  42. $this->isSourceItemManagementAllowedForProductType = $isSourceItemManagementAllowedForProductType;
  43. }
  44. /**
  45. * Bulk transfer source items configurations from origin source to destination source
  46. *
  47. * @param array $skus
  48. * @param string $originSource
  49. * @param string $destinationSource
  50. */
  51. public function execute(
  52. array $skus,
  53. string $originSource,
  54. string $destinationSource
  55. ) {
  56. $tableName = $this->resourceConnection->getTableName('inventory_low_stock_notification_configuration');
  57. $connection = $this->resourceConnection->getConnection();
  58. $skuTypes = $this->getProductTypesBySkus->execute($skus);
  59. /**
  60. * We are filtering SKU list for products which are not allowed to move configuration for
  61. */
  62. foreach ($skuTypes as $sku => $type) {
  63. if (!$this->isSourceItemManagementAllowedForProductType->execute($type)) {
  64. unset($skuTypes[$sku]);
  65. }
  66. }
  67. $destinationItemsQuery = $connection
  68. ->select()
  69. ->from($tableName, ['sku', 'notify_stock_qty'])
  70. ->where('sku IN (?)', array_keys($skuTypes))
  71. ->where('source_code = ?', $destinationSource);
  72. $destinationQueryResult = $connection->fetchPairs($destinationItemsQuery);
  73. $skusForDestination = array_diff_key($skuTypes, $destinationQueryResult);
  74. if (!empty($skusForDestination)) {
  75. /**
  76. * Get configuration from DB to transfer
  77. */
  78. $allowedSkus = array_keys($skusForDestination);
  79. $sourceItemsQuery = $connection
  80. ->select()
  81. ->from($tableName, ['sku','notify_stock_qty'])
  82. ->where('sku IN (?)', $allowedSkus)
  83. ->where('source_code = ?', $originSource);
  84. $sourceQueryResult = $connection->fetchAll($sourceItemsQuery);
  85. /**
  86. * Collect information about items to be copied
  87. * and also create an array of items that are not presented in original source
  88. */
  89. $notPresentedSkus = $skusForDestination;
  90. $itemsAllowedToMove = [];
  91. foreach ($sourceQueryResult as $inventoryNotificationItem) {
  92. $inventoryNotificationItem['source_code'] = $destinationSource;
  93. $itemsAllowedToMove[] = $inventoryNotificationItem;
  94. unset($notPresentedSkus[$inventoryNotificationItem['sku']]);
  95. }
  96. foreach ($notPresentedSkus as $sku => $type) {
  97. $itemsAllowedToMove[] = [
  98. 'sku' => $sku,
  99. 'notify_stock_qty' => null,
  100. 'source_code' => $destinationSource,
  101. ];
  102. }
  103. $connection->insertMultiple(
  104. $tableName,
  105. $itemsAllowedToMove
  106. );
  107. }
  108. }
  109. }