Skip to content

Commit

Permalink
Products being disabled with since N days filters (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
dxops authored Dec 30, 2022
1 parent 9b7c1ca commit 1a92b6d
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 113 deletions.
6 changes: 0 additions & 6 deletions Entity/AkeneoLocale.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ public function getCode()
return $this->code;
}

/**
* @return AkeneoLocale
*/
public function setCode(string $code): self
{
$this->code = $code;
Expand Down Expand Up @@ -93,9 +90,6 @@ public function getAkeneoSettings()
return $this->akeneoSettings;
}

/**
* @return AkeneoLocale
*/
public function setAkeneoSettings(AkeneoSettings $akeneoSettings = null): self
{
$this->akeneoSettings = $akeneoSettings;
Expand Down
8 changes: 0 additions & 8 deletions Entity/AkeneoSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -643,9 +643,6 @@ public function getTokenExpiryDateTime()
return $this->tokenExpiryDateTime;
}

/**
* @return AkeneoSettings
*/
public function setTokenExpiryDateTime(\DateTime $tokenExpiryDateTime): self
{
$this->tokenExpiryDateTime = $tokenExpiryDateTime;
Expand Down Expand Up @@ -680,8 +677,6 @@ public function setRootCategory(Category $rootCategory = null)
/**
* Get mapped locale.
*
* @param $locale
*
* @return null|string
*/
public function getMappedAkeneoLocale(string $locale)
Expand Down Expand Up @@ -723,9 +718,6 @@ public function getPriceList(): ?PriceList
return $this->priceList;
}

/**
* @return AkeneoSettings
*/
public function setPriceList(PriceList $priceList): self
{
$this->priceList = $priceList;
Expand Down
27 changes: 0 additions & 27 deletions ImportExport/DataConverter/ProductDataConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,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 @@ -181,22 +169,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
43 changes: 26 additions & 17 deletions ImportExport/Processor/ProductVariantProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class ProductVariantProcessor implements ProcessorInterface, StepExecutionAwareInterface
{
/** @var ManagerRegistry */
private $registry;
protected $registry;

/** @var ImportStrategyHelper */
private $strategyHelper;
Expand Down Expand Up @@ -64,10 +64,8 @@ public function process($items)
$context->setValue('itemData', ['configurable' => $parentSku, 'variants' => $variantSkus]);

$objectManager = $this->registry->getManagerForClass(Product::class);
/** @var ProductRepository $productRepository */
$productRepository = $objectManager->getRepository(Product::class);

$parentProduct = $productRepository->findOneBySku($parentSku);
$parentProduct = $this->findProduct($parentSku);
if (!$parentProduct instanceof Product) {
$context->incrementErrorEntriesCount();
$context->addError(
Expand Down Expand Up @@ -97,12 +95,11 @@ function ($variantSku) {
$variantSkus
);

$variantSkusUppercase = array_combine($variantSkusUppercase, $items);
$variantSkusUppercase = array_combine($variantSkusUppercase, $variantSkusUppercase);
foreach ($parentProduct->getVariantLinks() as $variantLink) {
$variantProduct = $variantLink->getProduct();
if (!$variantSkusUppercase) {
$parentProduct->removeVariantLink($variantLink);
$variantProduct->setStatus(Product::STATUS_DISABLED);
$objectManager->remove($variantLink);
$context->incrementDeleteCount();

Expand All @@ -111,22 +108,17 @@ function ($variantSku) {

if (!array_key_exists($variantProduct->getSkuUppercase(), $variantSkusUppercase)) {
$parentProduct->removeVariantLink($variantLink);
$variantProduct->setStatus(Product::STATUS_DISABLED);
$objectManager->remove($variantLink);
$context->incrementDeleteCount();

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) {
$variantProduct = $productRepository->findOneBySku($variantSku);
foreach ($variantSkusUppercase as $variantSku) {
$variantProduct = $this->findProduct($variantSku);
if (!$variantProduct instanceof Product) {
$context->incrementErrorEntriesCount();
$context->addError(
Expand Down Expand Up @@ -156,9 +148,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 All @@ -184,7 +173,7 @@ function ($variantSku) {

$objectManager->clear();

$parentProduct = $productRepository->findOneBySku($parentSku);
$parentProduct = $this->findProduct($parentSku);
if (!$parentProduct instanceof Product) {
return null;
}
Expand Down Expand Up @@ -214,10 +203,30 @@ 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;
}

protected function findProduct(string $sku): ?Product
{
$objectManager = $this->registry->getManagerForClass(Product::class);
/** @var ProductRepository $productRepository */
$productRepository = $objectManager->getRepository(Product::class);

return $productRepository->findOneBySku($sku);
}
}
14 changes: 14 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
21 changes: 18 additions & 3 deletions ImportExport/Writer/AsyncWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Doctrine\DBAL\Types\Types;
use Oro\Bundle\AkeneoBundle\Async\Topics;
use Oro\Bundle\AkeneoBundle\EventListener\AdditionalOptionalListenerManager;
use Oro\Bundle\BatchBundle\Item\Support\ClosableInterface;
use Oro\Bundle\AkeneoBundle\Tools\CacheProviderTrait;
use Oro\Bundle\EntityBundle\ORM\DoctrineHelper;
use Oro\Bundle\IntegrationBundle\Entity\FieldsChanges;
use Oro\Bundle\MessageQueueBundle\Client\BufferedMessageProducer;
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
46 changes: 28 additions & 18 deletions ImportExport/Writer/ConfigurableAsyncWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,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\EntityBundle\ORM\DoctrineHelper;
use Oro\Bundle\IntegrationBundle\Entity\FieldsChanges;
use Oro\Bundle\MessageQueueBundle\Client\BufferedMessageProducer;
Expand All @@ -22,6 +24,8 @@ class ConfigurableAsyncWriter implements
ItemWriterInterface,
StepExecutionAwareInterface
{
use CacheProviderTrait;

private const VARIANTS_BATCH_SIZE = 25;

/** @var MessageProducerInterface * */
Expand All @@ -45,6 +49,8 @@ class ConfigurableAsyncWriter implements

private $models = [];

private $configurable = [];

public function __construct(
MessageProducerInterface $messageProducer,
DoctrineHelper $doctrineHelper,
Expand All @@ -59,12 +65,10 @@ 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)
Expand All @@ -88,41 +92,43 @@ public function write(array $items)
}

$parent = $item['parent'];
if (!array_key_exists($parent, $this->origins)) {
continue;
}

$this->variants[$parent][$origin] = [
'parent' => $this->origins[$parent] ?? $parent,
'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;
}

$akeneoVariantLevels = $this->cacheProvider->fetch('akeneo_variant_levels') ?: AkeneoSettings::TWO_LEVEL_FAMILY_VARIANT_BOTH;
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;

if (array_key_exists($levelTwo, $this->configurable)) {
$this->configurable[$levelOne] = true;
}

$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;
}
}
}

$channelId = $this->stepExecution->getJobExecution()->getExecutionContext()->get('channel');

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

$chunks = array_chunk($this->variants, self::VARIANTS_BATCH_SIZE, true);

foreach ($chunks as $key => $chunk) {
Expand All @@ -138,6 +144,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 1a92b6d

Please sign in to comment.