Skip to content

Commit

Permalink
fix: use inventory stock table
Browse files Browse the repository at this point in the history
  • Loading branch information
ah-net committed Jun 4, 2024
1 parent 56a6e71 commit 15deb66
Showing 1 changed file with 28 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Magento\InventoryCatalogApi\Api\DefaultStockProviderInterface;
use Zend_Db_Expr;
use Tweakwise\Magento2TweakwiseExport\Model\Write\Stock\Collection as StockCollection;
use Magento\InventoryIndexer\Model\StockIndexTableNameResolver;

/**
* Class DefaultImplementation
Expand Down Expand Up @@ -69,6 +70,11 @@ class SourceItemMapProvider implements StockMapProviderInterface
*/
protected $defaultStockProvider;

/**
* @var StockIndexTableNameResolver
*/
protected $stockIndexTableNameResolver;

/**
* StockData constructor.
*
Expand All @@ -87,7 +93,8 @@ public function __construct(
StoreManagerInterface $storeManager,
StockResolverFactory $stockResolverFactory,
DefaultStockProviderInterfaceFactory $defaultStockProviderFactory,
DbResourceHelper $resourceHelper
DbResourceHelper $resourceHelper,
StockIndexTableNameResolver $stockIndexTableNameResolver
) {
$this->dbResource = $dbResource;
$this->stockSourceProviderFactory = $stockSourceProviderFactory;
Expand All @@ -96,6 +103,7 @@ public function __construct(
$this->stockResolverFactory = $stockResolverFactory;
$this->dbResource = $resourceHelper;
$this->defaultStockProviderFactory = $defaultStockProviderFactory;
$this->stockIndexTableNameResolver = $stockIndexTableNameResolver;
}

/**
Expand All @@ -115,15 +123,13 @@ public function getStockItemMap(Collection|StockCollection $collection): array
$entityIds = $collection->getAllIds();

$store = $collection->getStore();
$sourceCodes = $this->getSourceCodesForStore($store);
$stockId = $this->getStockIdForStoreId($store);

$dbConnection = $this->dbResource->getConnection();

$sourceItemTableName = $this->dbResource->getTableName('inventory_source_item');
$stockIndexTableName = $this->stockIndexTableNameResolver->execute($stockId);
$reservationTableName = $this->dbResource->getTableName('inventory_reservation');
$productTableName = $this->dbResource->getTableName('catalog_product_entity');
$stockItemTable = $this->dbResource->getTableName('cataloginventory_stock_item');

$reservationSelect = $dbConnection
->select()
Expand All @@ -139,81 +145,41 @@ public function getStockItemMap(Collection|StockCollection $collection): array
)
->group("$reservationTableName.sku");

// When stock id is default apparently the standard stock items are still used.
// Todo We should check if we can use magento's api for this as this is feeling rather sensitive.
if ($this->getDefaultStockProvider()->getId() !== $stockId) {
$sourceItemSelect = $dbConnection
->select()
->from($sourceItemTableName)
->reset('columns')
->where("$sourceItemTableName.source_code IN (?)", $sourceCodes)
->columns(
[
'sku',
's_quantity' => "SUM($sourceItemTableName.quantity)",
's_status' => "MAX($sourceItemTableName.status)"
]
)
->group("$sourceItemTableName.sku");
} else {
$sourceItemSelect = $dbConnection
->select()
->from($stockItemTable)
->reset('columns')
->where("$stockItemTable.product_id IN (?)", $entityIds)
/*
$stock_id is in this case the default stock id (i.e. 1) this filter problably doesnt remove anything
but it is here just to be sure.
*/
->where("$stockItemTable.stock_id = ?", $stockId)
->columns(
[
'product_id',
's_quantity' => "$stockItemTable.qty",
's_status' => "$stockItemTable.is_in_stock"
]
);
}
$sourceItemSelect = $dbConnection
->select()
->from($stockIndexTableName)
->reset('columns')
->columns(
[
'sku',
's_quantity' => "$stockIndexTableName.quantity",
's_is_salable' => "$stockIndexTableName.is_salable"
]
);

$select = $dbConnection
->select()
->from($productTableName)
->reset('columns');

// When stock id is default apparently the standard stock items are still used.
// Todo We should check if we can use magento's api for this as this is feeling rather sensitive.
if ($this->getDefaultStockProvider()->getId() !== $stockId) {
$select->joinLeft(
['s' => $sourceItemSelect],
"s.sku = $productTableName.sku",
[]
);
} else {
$select->joinLeft(
['s' => $sourceItemSelect],
"s.product_id = $productTableName.entity_id",
[]
);
}
$select->joinLeft(
['s' => $sourceItemSelect],
"s.sku = $productTableName.sku",
[]
);

$select->joinLeft(
['r' => $reservationSelect],
"r.sku = $productTableName.sku AND r.stock_id = $stockId",
[]
);
$select->join(
$stockItemTable,
"$stockItemTable.product_id = $productTableName.entity_id",
[
'backorders',
]
)
->where("$productTableName.entity_id IN (?)", $entityIds)
->columns(
[
'product_entity_id' => "$productTableName.entity_id",
'qty' => new Zend_Db_Expr('COALESCE(s.s_quantity,0) + COALESCE(r.r_quantity,0)'),
'is_in_stock' => 'COALESCE(s.s_status,0)'
'is_in_stock' => 'COALESCE(s.s_is_salable,0)'
]
);

Expand All @@ -227,30 +193,6 @@ public function getStockItemMap(Collection|StockCollection $collection): array
return $map;
}

/**
* @param Store $store
* @return array|null[]|string[]
* @throws LocalizedException
*/
protected function getSourceCodesForStore(Store $store): array
{
$stockId = $this->getStockIdForStoreId($store);
$sourceModels = $this->getStockSourceProvider()->execute($stockId);

//don't get stock for disabled stock sources
foreach ($sourceModels as $key => $sourceModel) {
if (!$sourceModel->isEnabled()) {
unset($sourceModels[$key]);
}
}

$sourceCodeMapper = static function (SourceInterface $source) {
return $source->getSourceCode();
};

return array_map($sourceCodeMapper, $sourceModels);
}

/**
* This is necessary to remain compatible with Magento 2.2.X
* setup:di:compile fails when there is a reference to a non existing Interface or Class in the constructor
Expand Down Expand Up @@ -313,7 +255,7 @@ protected function getTweakwiseStockItem(array $item): StockItem
$tweakwiseStockItem = $this->tweakwiseStockItemFactory->create();

$qty = (int)$item['qty'];
$isInStock = max((int)$item['backorders'], (int)$item['is_in_stock']);
$isInStock = (int)$item['is_in_stock'];

$tweakwiseStockItem->setQty($qty);
$tweakwiseStockItem->setIsInStock($isInStock);
Expand Down

0 comments on commit 15deb66

Please sign in to comment.