Skip to content

Commit

Permalink
Products being disabled with since N days filters (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
dxops authored Dec 30, 2022
1 parent 3913624 commit b036bac
Show file tree
Hide file tree
Showing 12 changed files with 213 additions and 85 deletions.
27 changes: 0 additions & 27 deletions ImportExport/DataConverter/ProductDataConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,6 @@ private function setStatus(array &$importedRecord)
{
$importedRecord['status'] = empty($importedRecord['enabled']) ?
Product::STATUS_DISABLED : Product::STATUS_ENABLED;

if (!empty($importedRecord['family_variant'])) {
$importedRecord['status'] = Product::STATUS_DISABLED;

return;
}

if (!empty($importedRecord['parent'])) {
$importedRecord['status'] = Product::STATUS_DISABLED;

return;
}
}

private function setPrimaryUnitPrecision(array &$importedRecord): void
Expand Down Expand Up @@ -182,22 +170,7 @@ private function setFamilyVariant(array &$importedRecord)
return;
}

$importedRecord['status'] = Product::STATUS_ENABLED;
$importedRecord['variantFields'] = implode(',', $variantFields);

if ($isTwoLevelFamilyVariant) {
$allowSecondProductOnly = $this->getTransport()->getAkeneoVariantLevels() ===
AkeneoSettings::TWO_LEVEL_FAMILY_VARIANT_SECOND_ONLY;
if ($isFirstLevelProduct && $allowSecondProductOnly) {
$importedRecord['status'] = Product::STATUS_DISABLED;
}

$allowFirstProductOnly = $this->getTransport()->getAkeneoVariantLevels() ===
AkeneoSettings::TWO_LEVEL_FAMILY_VARIANT_FIRST_ONLY;
if ($isSecondLevelProduct && $allowFirstProductOnly) {
$importedRecord['status'] = Product::STATUS_DISABLED;
}
}
}

private function processValues(array &$importedRecord)
Expand Down
34 changes: 25 additions & 9 deletions ImportExport/Processor/ProductVariantProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function ($variantSku) {
$variantSkus
);

$variantSkusUppercase = array_combine($variantSkusUppercase, $items);
$variantSkusUppercase = array_combine($variantSkusUppercase, $variantSkusUppercase);
foreach ($parentProduct->getVariantLinks() as $variantLink) {
$variantProduct = $variantLink->getProduct();
if (!$variantSkusUppercase) {
Expand All @@ -118,14 +118,10 @@ function ($variantSku) {
continue;
}

$variantItem = $variantSkusUppercase[$variantProduct->getSkuUppercase()];
$status = empty($variantItem['enabled']) ? Product::STATUS_DISABLED : Product::STATUS_ENABLED;
$variantProduct->setStatus($status);

unset($variantSkusUppercase[$variantProduct->getSkuUppercase()]);
}

foreach ($variantSkusUppercase as $variantSku => $variantItem) {
foreach ($variantSkusUppercase as $variantSku) {
$variantProduct = $productRepository->findOneBySku($variantSku);
if (!$variantProduct instanceof Product) {
$context->incrementErrorEntriesCount();
Expand Down Expand Up @@ -156,9 +152,6 @@ function ($variantSku) {
$variantProduct->addParentVariantLink($variantLink);
$parentProduct->addVariantLink($variantLink);

$status = empty($variantItem['enabled']) ? Product::STATUS_DISABLED : Product::STATUS_ENABLED;
$variantProduct->setStatus($status);

$context->incrementAddCount();

$objectManager->persist($variantLink);
Expand Down Expand Up @@ -190,6 +183,18 @@ function ($variantSku) {
}

$parentProduct->setStatus(Product::STATUS_DISABLED);
foreach ($parentProduct->getVariantLinks() as $variantLink) {
$variantProduct = $variantLink->getProduct();
if ($variantProduct instanceof Product) {
$variantProduct->setStatus(Product::STATUS_DISABLED);
}
}
foreach ($variantSkusUppercase as $variantSku) {
$variantProduct = $productRepository->findOneBySku($variantSku);
if ($variantProduct instanceof Product) {
$variantProduct->setStatus(Product::STATUS_DISABLED);
}
}

return $parentProduct;
}
Expand All @@ -214,9 +219,20 @@ function ($variantSku) {
]
)
);

return $parentProduct;
}

$context->incrementUpdateCount();
$parentProduct->setStatus(Product::STATUS_ENABLED);

foreach ($items as $item) {
if (!empty($item['parent_disabled'])) {
$parentProduct->setStatus(Product::STATUS_DISABLED);
}

break;
}

return $parentProduct;
}
Expand Down
32 changes: 32 additions & 0 deletions ImportExport/Strategy/ProductImportStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ public function close()
parent::close();
}

protected function beforeProcessEntity($entity)
{
/** @var Product $entity */
if ($entity->isConfigurable()) {
/** @var Product $existingProduct */
$existingProduct = $this->findExistingEntity($entity);
if ($existingProduct) {
$entity->setStatus($existingProduct->getStatus());
}
}

return parent::beforeProcessEntity($entity);
}

protected function afterProcessEntity($entity)
{
if ($entity instanceof Product && $entity->getCategory() && !$entity->getCategory()->getId()) {
Expand Down Expand Up @@ -74,6 +88,24 @@ protected function afterProcessEntity($entity)
return parent::afterProcessEntity($entity);
}

protected function validateAndUpdateContext($entity)
{
$validationErrors = $this->strategyHelper->validateEntity($entity);
if ($validationErrors) {
$this->processValidationErrors($entity, $validationErrors);

/** @var Product $entity */
$entity = $this->findExistingEntity($entity);
$entity->setStatus(Product::STATUS_DISABLED);

return $entity;
}

$this->updateContextCounters($entity);

return $entity;
}

protected function populateOwner(Product $entity)
{
}
Expand Down
21 changes: 18 additions & 3 deletions ImportExport/Writer/AsyncWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
use Doctrine\DBAL\Types\Types;
use Oro\Bundle\AkeneoBundle\Async\Topics;
use Oro\Bundle\AkeneoBundle\EventListener\AdditionalOptionalListenerManager;
use Oro\Bundle\AkeneoBundle\Tools\CacheProviderTrait;
use Oro\Bundle\BatchBundle\Entity\StepExecution;
use Oro\Bundle\BatchBundle\Item\ItemWriterInterface;
use Oro\Bundle\BatchBundle\Item\Support\ClosableInterface;
use Oro\Bundle\BatchBundle\Step\StepExecutionAwareInterface;
use Oro\Bundle\EntityBundle\ORM\DoctrineHelper;
use Oro\Bundle\IntegrationBundle\Entity\FieldsChanges;
Expand All @@ -21,9 +21,10 @@

class AsyncWriter implements
ItemWriterInterface,
ClosableInterface,
StepExecutionAwareInterface
{
use CacheProviderTrait;

/** @var MessageProducerInterface * */
private $messageProducer;

Expand All @@ -42,6 +43,8 @@ class AsyncWriter implements
/** @var AdditionalOptionalListenerManager */
private $additionalOptionalListenerManager;

private $configurable = [];

public function __construct(
MessageProducerInterface $messageProducer,
DoctrineHelper $doctrineHelper,
Expand Down Expand Up @@ -76,6 +79,13 @@ public function write(array $items)
$this->size = $newSize;
$this->stepExecution->setWriteCount($this->size);

foreach ($items as $item) {
$this->configurable[$item['identifier'] ?? $item['code']] = true;
if (!empty($item['parent'])) {
$this->configurable[$item['parent']] = true;
}
}

$jobId = $this->insertJob($jobName);
if ($jobId && $this->createFieldsChanges($jobId, $items, 'items')) {
$this->sendMessage($channelId, $jobId, true);
Expand Down Expand Up @@ -136,8 +146,13 @@ private function getRootJob(): ?int
return (int)$rootJobId;
}

public function close()
public function flush()
{
if ($this->configurable) {
$this->cacheProvider->save('akeneo_configurable', $this->configurable);
}

$this->configurable = [];
$this->size = 0;

$this->optionalListenerManager->enableListeners($this->optionalListenerManager->getListeners());
Expand Down
83 changes: 70 additions & 13 deletions ImportExport/Writer/ConfigurableAsyncWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Types\Types;
use Oro\Bundle\AkeneoBundle\Async\Topics;
use Oro\Bundle\AkeneoBundle\Entity\AkeneoSettings;
use Oro\Bundle\AkeneoBundle\EventListener\AdditionalOptionalListenerManager;
use Oro\Bundle\AkeneoBundle\Tools\CacheProviderTrait;
use Oro\Bundle\BatchBundle\Entity\StepExecution;
use Oro\Bundle\BatchBundle\Item\ItemWriterInterface;
use Oro\Bundle\BatchBundle\Step\StepExecutionAwareInterface;
Expand All @@ -17,11 +19,14 @@
use Oro\Component\MessageQueue\Client\Message;
use Oro\Component\MessageQueue\Client\MessagePriority;
use Oro\Component\MessageQueue\Client\MessageProducerInterface;
use Symfony\Contracts\Cache\CacheInterface;

class ConfigurableAsyncWriter implements
ItemWriterInterface,
StepExecutionAwareInterface
{
use CacheProviderTrait;

private const VARIANTS_BATCH_SIZE = 25;

/** @var MessageProducerInterface * */
Expand All @@ -45,6 +50,16 @@ class ConfigurableAsyncWriter implements

private $models = [];

private $configurable = [];

/** @var CacheInterface */
private $cache;

public function setCache(CacheInterface $cache): void
{
$this->cache = $cache;
}

public function __construct(
MessageProducerInterface $messageProducer,
DoctrineHelper $doctrineHelper,
Expand All @@ -59,16 +74,30 @@ public function __construct(

public function initialize()
{
$this->variants = [];
$this->origins = [];
$this->models = [];

$this->additionalOptionalListenerManager->disableListeners();
$this->optionalListenerManager->disableListeners($this->optionalListenerManager->getListeners());

$this->configurable = $this->cacheProvider->fetch('akeneo_configurable') ?: [];
}

public function write(array $items)
{
if (!$this->variants) {
$this->variants = $this->cache->get('variants', function () {
return [];
});
}
if (!$this->origins) {
$this->origins = $this->cache->get('origins', function () {
return [];
});
}
if (!$this->models) {
$this->models = $this->cache->get('models', function () {
return [];
});
}

foreach ($items as $item) {
$origin = $item['origin'];
$sku = $item['sku'];
Expand All @@ -95,28 +124,52 @@ public function write(array $items)
$this->variants[$parent][$origin] = [
'parent' => $this->origins[$parent] ?? $parent,
'variant' => $sku,
'enabled' => $item['enabled'] ?? false,
];
}
}

public function close()
public function flush()
{
$this->variants = [];
$this->origins = [];
$this->models = [];

$this->optionalListenerManager->enableListeners($this->optionalListenerManager->getListeners());
$this->additionalOptionalListenerManager->enableListeners();
}

public function flush()
{
if (!$this->variants) {
return;
}

if ($this->variants) {
$variants = $this->cache->getItem('variants');
$variants->set($this->variants);
$this->cache->save($variants);
}

if ($this->origins) {
$origins = $this->cache->getItem('origins');
$origins->set($this->origins);
$this->cache->save($origins);
}

if ($this->models) {
$models = $this->cache->getItem('models');
$models->set($this->models);
$this->cache->save($models);
}

$updated = $this->cache->getItem('time');
$updated->set($this->cacheProvider->fetch('time'));
$this->cache->save($updated);

$this->variants = array_intersect_key($this->variants, $this->configurable);

foreach ($this->models as $levelTwo => $levelOne) {
if (array_key_exists($levelTwo, $this->variants)) {
foreach ($this->variants[$levelTwo] as $sku => $item) {
$item['parent'] = $this->origins[$levelOne] ?? $levelOne;
$this->variants[$levelOne][$sku] = $item;

$akeneoVariantLevels = $this->cacheProvider->fetch('akeneo_variant_levels');
$this->variants[$levelOne][$sku]['parent_disabled'] = $akeneoVariantLevels === AkeneoSettings::TWO_LEVEL_FAMILY_VARIANT_SECOND_ONLY;
$this->variants[$levelTwo][$sku]['parent_disabled'] = $akeneoVariantLevels === AkeneoSettings::TWO_LEVEL_FAMILY_VARIANT_FIRST_ONLY;
}
}
}
Expand All @@ -138,6 +191,10 @@ public function flush()
$this->sendMessage($channelId, $jobId);
}
}

$this->variants = [];
$this->origins = [];
$this->models = [];
}

private function createFieldsChanges(int $jobId, array &$data, string $key): bool
Expand Down
Loading

0 comments on commit b036bac

Please sign in to comment.