![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/Ecombricks/InventoryInventoryCatalog/Model/ResourceModel/SourceItemOption/ |
<?php /** * Copyright © eComBricks. All rights reserved. * See LICENSE.txt for license details. */ declare(strict_types=1); namespace Ecombricks\InventoryInventoryCatalog\Model\ResourceModel\SourceItemOption; /** * Transfer source item options resource */ class Transfer { /** * Connection provider * * @var \Ecombricks\Framework\Model\ResourceModel\Db\ConnectionProvider */ protected $connectionProvider; /** * Get product types by SKUs * * @var \Magento\InventoryCatalogApi\Model\GetProductTypesBySkusInterface */ protected $getProductTypesBySkus; /** * Is source item management allowed for product type * * @var \Magento\InventoryConfigurationApi\Model\IsSourceItemManagementAllowedForProductTypeInterface */ protected $isSourceItemManagementAllowedForProductType; /** * Table name * * @var string */ protected $tableName; /** * Constructor * * @param \Ecombricks\Framework\Model\ResourceModel\Db\ConnectionProvider $connectionProvider * @param \Magento\InventoryCatalogApi\Model\GetProductTypesBySkusInterface $getProductTypesBySkus * @param \Magento\InventoryConfigurationApi\Model\IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemManagementAllowedForProductType * @param string $tableName * @return void */ public function __construct( \Ecombricks\Framework\Model\ResourceModel\Db\ConnectionProvider $connectionProvider, \Magento\InventoryCatalogApi\Model\GetProductTypesBySkusInterface $getProductTypesBySkus, \Magento\InventoryConfigurationApi\Model\IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemManagementAllowedForProductType, string $tableName ) { $this->connectionProvider = $connectionProvider; $this->getProductTypesBySkus = $getProductTypesBySkus; $this->isSourceItemManagementAllowedForProductType = $isSourceItemManagementAllowedForProductType; $this->tableName = $tableName; } /** * Filter SKUs * * @param array $skus * @return array */ protected function filterSkus(array $skus): array { $productTypes = $this->getProductTypesBySkus->execute($skus); foreach ($productTypes as $sku => $productType) { if (!$this->isSourceItemManagementAllowedForProductType->execute($productType)) { unset($productTypes[$sku]); } } return array_keys($productTypes); } /** * Get source item option values * * @param array $skus * @param string $sourceCode * @return array */ protected function getSourceItemOptionValues(array $skus, string $sourceCode): array { return $this->connectionProvider->getConnection()->fetchPairs( $this->connectionProvider->getSelect() ->from($this->connectionProvider->getTable($this->tableName), [ \Ecombricks\InventoryInventoryCatalog\Api\Data\SourceItemOptionInterface::SKU, \Ecombricks\InventoryInventoryCatalog\Api\Data\SourceItemOptionInterface::VALUE, ]) ->where(\Ecombricks\InventoryInventoryCatalog\Api\Data\SourceItemOptionInterface::SKU.' IN (?)', $skus) ->where(\Ecombricks\InventoryInventoryCatalog\Api\Data\SourceItemOptionInterface::SOURCE_CODE.' = ?', $sourceCode) ); } /** * Get source item option data * * @param string $sourceCode * @param string $sku * @param mixed $value * @return array */ protected function getSourceItemOptionData(string $sourceCode, string $sku, $value = null): array { return [ \Ecombricks\InventoryInventoryCatalog\Api\Data\SourceItemOptionInterface::SOURCE_CODE => $sourceCode, \Ecombricks\InventoryInventoryCatalog\Api\Data\SourceItemOptionInterface::SKU => $sku, \Ecombricks\InventoryInventoryCatalog\Api\Data\SourceItemOptionInterface::VALUE => $value, ]; } /** * Execute * * @param array $skus * @param string $originSource * @param string $destinationSource * @return $this */ public function execute( array $skus, string $originSource, string $destinationSource ): \Ecombricks\InventoryInventoryCatalog\Model\ResourceModel\SourceItemOption\Transfer { $filteredSkus = $this->filterSkus($skus); if (empty($filteredSkus)) { return $this; } $destinationSourceItemValues = $this->getSourceItemOptionValues($filteredSkus, $destinationSource); $skusToInsert = array_diff($filteredSkus, array_keys($destinationSourceItemValues)); if (empty($skusToInsert)) { return $this; } $originSourceItemValues = $this->getSourceItemOptionValues($skusToInsert, $originSource); $sourceItemOptionsData = []; foreach ($originSourceItemValues as $sku => $value) { $sourceItemOptionsData[] = $this->getSourceItemOptionData($destinationSource, $sku, $value); } foreach (array_diff($skusToInsert, array_keys($originSourceItemValues)) as $sku) { $sourceItemOptionsData[] = $this->getSourceItemOptionData($destinationSource, $sku); } $this->connectionProvider->getConnection()->insertMultiple( $this->connectionProvider->getTable($this->tableName), $sourceItemOptionsData ); return $this; } }