-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from magento-troll/MDEE-40-STORY-is-deleted-and…
…-updated-at MDEE-57: Handle is_deleted updates, MDEE-55: Prepare get updatedAt query
- Loading branch information
Showing
6 changed files
with
280 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
InventoryDataExporter/Model/Query/StockStatusDeleteQuery.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
] | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters