Skip to content

Commit

Permalink
Merge pull request #9 from magento-troll/MDEE-40-STORY-is-deleted-and…
Browse files Browse the repository at this point in the history
…-updated-at

MDEE-57: Handle is_deleted updates, MDEE-55: Prepare get updatedAt query
  • Loading branch information
mslabko committed Oct 26, 2021
2 parents 110ae7d + 1fab611 commit 2434187
Show file tree
Hide file tree
Showing 6 changed files with 280 additions and 19 deletions.
12 changes: 12 additions & 0 deletions InventoryDataExporter/Model/Provider/StockStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Magento\InventoryDataExporter\Model\Provider;

use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Stdlib\DateTime;
use Magento\InventoryDataExporter\Model\Query\InventoryStockQuery;
use Psr\Log\LoggerInterface;

Expand All @@ -35,6 +36,11 @@ class StockStatus
*/
private $query;

/**
* @var DateTime
*/
private $dateTime;

/**
* @var LoggerInterface
*/
Expand All @@ -43,16 +49,19 @@ class StockStatus
/**
* @param ResourceConnection $resourceConnection
* @param InventoryStockQuery $query
* @param DateTime $dateTime
* @param LoggerInterface $logger
*/
public function __construct(
ResourceConnection $resourceConnection,
InventoryStockQuery $query,
DateTime $dateTime,
LoggerInterface $logger
) {
$this->resourceConnection = $resourceConnection;
$this->query = $query;
$this->logger = $logger;
$this->dateTime = $dateTime;
}

/**
Expand Down Expand Up @@ -103,6 +112,9 @@ private function fillWithDefaultValues(array $row): array
throw new \RuntimeException("missed required field: " . \var_export($row, true));
}
$row['id'] = StockStatusIdBuilder::build($row);

// set updated at
$row['updatedAt'] = $this->dateTime->formatDate(time());
// set default values
$row['infiniteStock'] = false;
$row['qtyForSale'] = $row['qty'];
Expand Down
86 changes: 86 additions & 0 deletions InventoryDataExporter/Model/Query/StockStatusDeleteQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryDataExporter\Model\Query;

use Magento\DataExporter\Model\Indexer\FeedIndexMetadata;
use Magento\Framework\App\ResourceConnection;

/**
* Stock Status mark as deleted query builder
*/
class StockStatusDeleteQuery
{
/**
* @var ResourceConnection
*/
private $resourceConnection;

/**
* @var FeedIndexMetadata
*/
private $metadata;

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

/**
* Get stocks which are assigned to the list of provided SKUs
*
* @param array $skus
* @return array
*/
public function getStocksAssignedToSkus(array $skus): array
{
$connection = $this->resourceConnection->getConnection();
$select = $connection->select()
->from(
['source_item' => $this->resourceConnection->getTableName('inventory_source_item')],
['source_item.sku', 'source_stock_link.stock_id']
)->joinLeft(
['source_stock_link' => $this->resourceConnection->getTableName('inventory_source_stock_link')],
'source_item.source_code = source_stock_link.source_code'
)->where('source_item.sku IN (?)', $skus);

$fetchedSourceItems = [];
foreach ($connection->fetchAll($select) as $sourceItem) {
$fetchedSourceItems[$sourceItem['sku']][$sourceItem['stock_id']][] = $sourceItem['source_code'];
}

return $fetchedSourceItems;
}

/**
* Mark stock statuses as deleted
*
* @param array $stocksToDelete
*/
public function markStockStatusesAsDeleted(array $stocksToDelete): void
{
$connection = $this->resourceConnection->getConnection();
$feedTableName = $this->resourceConnection->getTableName($this->metadata->getFeedTableName());
foreach ($stocksToDelete as $stockId => $skus) {
$connection->update(
$feedTableName,
['is_deleted' => new \Zend_Db_Expr('1')],
[
'sku IN (?)' => $skus,
'stock_id = ?' => $stockId
]
);
}
}
}
80 changes: 80 additions & 0 deletions InventoryDataExporter/Plugin/BulkSourceUnassign.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\InventoryDataExporter\Plugin;

use Magento\InventoryDataExporter\Model\Query\StockStatusDeleteQuery;

/**
* Mark stock statuses as deleted on bulk unassign
*/
class BulkSourceUnassign
{
/**
* @var StockStatusDeleteQuery
*/
private $stockStatusDeleteQuery;

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

/**
* Check which stocks will be unassigned from products and mark them as deleted in feed table
*
* @param \Magento\InventoryCatalog\Model\ResourceModel\BulkSourceUnassign $subject
* @param array $skus
* @param array $sourceCodes
* @return void
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function beforeExecute(
\Magento\InventoryCatalog\Model\ResourceModel\BulkSourceUnassign $subject,
array $skus,
array $sourceCodes
): void {
$fetchedSourceItems = $this->stockStatusDeleteQuery->getStocksAssignedToSkus($skus);
$stocksToDelete = $this->getStocksToDelete($skus, $sourceCodes, $fetchedSourceItems);

if (!empty($stocksToDelete)) {
$this->stockStatusDeleteQuery->markStockStatusesAsDeleted($stocksToDelete);
}
}

/**
* @param array $affectedSkus
* @param array $deletedSources
* @return array
*/
private function getStocksToDelete(array $affectedSkus, array $deletedSources, $fetchedSourceItems): array
{
$stocksToDelete = [];
foreach ($affectedSkus as $deletedItemSku) {
foreach ($fetchedSourceItems[$deletedItemSku] as $fetchedItemStockId => $fetchedItemSources) {
if ($this->getContainsAllKeys($fetchedItemSources, $deletedSources)) {
$stocksToDelete[(string)$fetchedItemStockId][] = $deletedItemSku;
}
}
}

return $stocksToDelete;
}

/**
* @param array $fetchedSources
* @param array $deletedSources
* @return bool
*/
private function getContainsAllKeys(array $fetchedSources, array $deletedSources): bool
{
return empty(\array_diff($fetchedSources, $deletedSources));
}
}
85 changes: 85 additions & 0 deletions InventoryDataExporter/Plugin/MarkItemsAsDeleted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\InventoryDataExporter\Plugin;

use Magento\Inventory\Model\ResourceModel\SourceItem\DeleteMultiple;
use Magento\InventoryApi\Api\Data\SourceItemInterface;
use Magento\InventoryDataExporter\Model\Query\StockStatusDeleteQuery;

/**
* Plugin for setting stock item statuses as deleted
*/
class MarkItemsAsDeleted
{
/**
* @var StockStatusDeleteQuery
*/
private $stockStatusDeleteQuery;

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

/**
* Set is_deleted value to 1 for deleted stock statuses
*
* @param DeleteMultiple $subject
* @param SourceItemInterface[] $sourceItems
* @return void
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function beforeExecute(
DeleteMultiple $subject,
array $sourceItems
): void {
$deletedSourceItems = [];
foreach ($sourceItems as $sourceItem) {
$deletedSourceItems[$sourceItem->getSku()][] = $sourceItem->getSourceCode();
}

$fetchedSourceItems = $this->stockStatusDeleteQuery->getStocksAssignedToSkus(array_keys($deletedSourceItems));

$stocksToDelete = $this->getStocksToDelete($deletedSourceItems, $fetchedSourceItems);
if (!empty($stocksToDelete)) {
$this->stockStatusDeleteQuery->markStockStatusesAsDeleted($stocksToDelete);
}
}

/**
* @param array $deletedSourceItems
* @param $fetchedSourceItems
* @return array
*/
private function getStocksToDelete(array $deletedSourceItems, $fetchedSourceItems): array
{
$stocksToDelete = [];
foreach ($deletedSourceItems as $deletedItemSku => $deletedItemSources) {
foreach ($fetchedSourceItems[$deletedItemSku] as $fetchedItemStockId => $fetchedItemSources) {
if ($this->getContainsAllKeys($fetchedItemSources, $deletedItemSources)) {
$stocksToDelete[(string)$fetchedItemStockId][] = $deletedItemSku;
}
}
}

return $stocksToDelete;
}

/**
* @param array $fetchedSources
* @param array $deletedSources
* @return bool
*/
private function getContainsAllKeys(array $fetchedSources, array $deletedSources): bool
{
return empty(\array_diff($fetchedSources, $deletedSources));
}
}
1 change: 0 additions & 1 deletion InventoryDataExporter/etc/db_schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
/>
<constraint xsi:type="primary" referenceId="PRIMARY">
<column name="id"/>
<column name="stock_id"/>
</constraint>
<index referenceId="inventory_data_exporter_stock_status_modified_at" indexType="btree">
<column name="modified_at"/>
Expand Down
35 changes: 17 additions & 18 deletions InventoryDataExporter/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\InventoryDataExporter\Model\Query\MainProductQuery">
<arguments>
<argument name="mainTable" xsi:type="string">catalog_product_entity</argument>
</arguments>
</type>
<type name="Magento\InventoryDataExporter\Model\Provider\StockStatus">
<arguments>
<argument name="queryName" xsi:type="string">stockStatus</argument>
Expand All @@ -26,10 +21,10 @@
<argument name="feedName" xsi:type="string">stock_statuses</argument>
<argument name="feedIdentity" xsi:type="string">sku</argument>
<!-- source table used only during full reindex -->
<argument name="sourceTableName" xsi:type="string">catalog_product_entity</argument>
<argument name="sourceTableName" xsi:type="string">inventory_source_item</argument>
<argument name="sourceTableField" xsi:type="string">sku</argument>
<argument name="feedTableName" xsi:type="string">inventory_data_exporter_stock_status</argument>
<argument name="feedTableField" xsi:type="string">id</argument>
<argument name="feedTableField" xsi:type="string">sku</argument>
<argument name="feedTableMutableColumns" xsi:type="array">
<item name="feed_data" xsi:type="string">feed_data</item>
<item name="is_deleted" xsi:type="string">is_deleted</item>
Expand All @@ -45,21 +40,10 @@
</argument>
</arguments>
</virtualType>
<virtualType name="Magento\InventoryDataExporter\Model\Indexer\StockStatusFeedIndexProcessorCreateUpdateDelete" type="Magento\DataExporter\Model\Indexer\FeedIndexProcessorCreateUpdateDelete">
<arguments>
<argument name="markRemovedEntities" xsi:type="object">Magento\InventoryDataExporter\Model\Indexer\StockStatusMarkRemovedEntities</argument>
</arguments>
</virtualType>
<virtualType name="Magento\InventoryDataExporter\Model\Indexer\StockStatusMarkRemovedEntities" type="Magento\DataExporter\Model\Indexer\MarkRemovedEntities">
<arguments>
<argument name="markRemovedEntitiesQuery" xsi:type="object">Magento\InventoryDataExporter\Model\Query\MarkRemovedEntitiesQuery</argument>
</arguments>
</virtualType>
<virtualType name="Magento\InventoryDataExporter\Model\Indexer\StockStatusFeedIndexer" type="Magento\DataExporter\Model\Indexer\FeedIndexer">
<arguments>
<argument name="feedIndexMetadata" xsi:type="object">Magento\InventoryDataExporter\Model\Indexer\StockStatusFeedIndexMetadata</argument>
<argument name="serializer" xsi:type="object">Magento\InventoryDataExporter\Model\Indexer\StockStatusDataSerializer</argument>
<!-- <argument name="entityIdsProvider" xsi:type="object">\Magento\InventoryDataExporter\Indexer\StockStatusIdsProvider</argument>-->
</arguments>
</virtualType>
<virtualType name="Magento\InventoryDataExporter\Model\StockStatusFeed" type="Magento\DataExporter\Model\Feed">
Expand All @@ -72,4 +56,19 @@
<plugin name="create_stock_item_changelog_table" type="\Magento\InventoryDataExporter\Plugin\Mview\StockStatusChangelog"/>
</type>

<type name="Magento\Inventory\Model\ResourceModel\SourceItem\DeleteMultiple">
<plugin name="mark-stock-status-items-as-deleted"
type="Magento\InventoryDataExporter\Plugin\MarkItemsAsDeleted"/>
</type>
<type name="Magento\InventoryCatalog\Model\ResourceModel\BulkSourceUnassign">
<plugin name="mark-stock-status-items-unassigned-on-bulk-unassign"
type="Magento\InventoryDataExporter\Plugin\BulkSourceUnassign"/>
</type>
<type name="Magento\InventoryDataExporter\Model\Query\StockStatusDeleteQuery">
<arguments>
<argument name="metadata" xsi:type="object">
Magento\InventoryDataExporter\Model\Indexer\StockStatusFeedIndexMetadata
</argument>
</arguments>
</type>
</config>

0 comments on commit 2434187

Please sign in to comment.