From 0aba60cd322389158c5ed8920af61e7d6391d469 Mon Sep 17 00:00:00 2001 From: Kevin Kaniaburka Date: Thu, 2 Feb 2023 11:42:18 +0100 Subject: [PATCH 1/4] Add ProductVariantVisibilityChecker --- .../ProductVariantVisibilityCheckerSpec.php | 84 +++++++++++++++++++ .../ProductVariantVisibilityChecker.php | 29 +++++++ ...oductVariantVisibilityCheckerInterface.php | 22 +++++ 3 files changed, 135 insertions(+) create mode 100644 spec/Checker/ProductVariantVisibilityCheckerSpec.php create mode 100644 src/Checker/ProductVariantVisibilityChecker.php create mode 100644 src/Checker/ProductVariantVisibilityCheckerInterface.php diff --git a/spec/Checker/ProductVariantVisibilityCheckerSpec.php b/spec/Checker/ProductVariantVisibilityCheckerSpec.php new file mode 100644 index 00000000..276eaf5e --- /dev/null +++ b/spec/Checker/ProductVariantVisibilityCheckerSpec.php @@ -0,0 +1,84 @@ +shouldHaveType(ProductVariantVisibilityCheckerInterface::class); + } + + function it_returns_true_if_product_variant_is_visible( + ProductVariantInterface $productVariant, + ChannelInterface $channel, + ProductInterface $product, + ): void { + $productVariant->getProduct()->willReturn($product); + + $productVariant->isEnabled()->willReturn(true); + $product->isEnabled()->willReturn(true); + $product->hasChannel($channel)->willReturn(true); + + $this->isVisibleInChannel($productVariant, $channel)->shouldReturn(true); + } + + function it_returns_false_if_product_is_disabled( + ProductVariantInterface $productVariant, + ChannelInterface $channel, + ProductInterface $product, + ): void { + $productVariant->getProduct()->willReturn($product); + + $productVariant->isEnabled()->willReturn(true); + $product->isEnabled()->willReturn(false); + $product->hasChannel($channel)->willReturn(true); + + $this->isVisibleInChannel($productVariant, $channel)->shouldReturn(false); + } + + function it_returns_false_if_product_is_disabled_for_a_given_channel( + ProductVariantInterface $productVariant, + ChannelInterface $channel, + ProductInterface $product, + ): void { + $productVariant->getProduct()->willReturn($product); + + $productVariant->isEnabled()->willReturn(true); + $product->isEnabled()->willReturn(true); + $product->hasChannel($channel)->willReturn(false); + + $this->isVisibleInChannel($productVariant, $channel)->shouldReturn(false); + } + + function it_returns_false_if_product_variant_is_disabled( + ProductVariantInterface $productVariant, + ChannelInterface $channel, + ProductInterface $product, + ): void { + $productVariant->getProduct()->willReturn($product); + + $productVariant->isEnabled()->willReturn(false); + $product->isEnabled()->willReturn(true); + $product->hasChannel($channel)->willReturn(true); + + $this->isVisibleInChannel($productVariant, $channel)->shouldReturn(false); + } +} diff --git a/src/Checker/ProductVariantVisibilityChecker.php b/src/Checker/ProductVariantVisibilityChecker.php new file mode 100644 index 00000000..564a6a47 --- /dev/null +++ b/src/Checker/ProductVariantVisibilityChecker.php @@ -0,0 +1,29 @@ +getProduct(); + Assert::notNull($product); + + return $productVariant->isEnabled() && $product->isEnabled() && $product->hasChannel($channel); + } +} diff --git a/src/Checker/ProductVariantVisibilityCheckerInterface.php b/src/Checker/ProductVariantVisibilityCheckerInterface.php new file mode 100644 index 00000000..099df186 --- /dev/null +++ b/src/Checker/ProductVariantVisibilityCheckerInterface.php @@ -0,0 +1,22 @@ + Date: Thu, 2 Feb 2023 13:23:53 +0100 Subject: [PATCH 2/4] [Behat] Add scenarios for seeing entry logs but only visible changes on a product --- ...ory_after_creating_product_variant.feature | 35 ++++++++++++++++++ ...tory_after_editing_product_variant.feature | 27 ++++++++++++++ .../Admin/ChannelPricingLogEntryContext.php | 10 +++++ tests/Behat/Context/Setup/ProductContext.php | 37 +++++++++++++++++++ .../Resources/services/contexts/setup.xml | 3 ++ 5 files changed, 112 insertions(+) diff --git a/features/product/managing_product_variants/seeing_correct_catalog_price_history_after_creating_product_variant.feature b/features/product/managing_product_variants/seeing_correct_catalog_price_history_after_creating_product_variant.feature index a3485eaa..0f784064 100644 --- a/features/product/managing_product_variants/seeing_correct_catalog_price_history_after_creating_product_variant.feature +++ b/features/product/managing_product_variants/seeing_correct_catalog_price_history_after_creating_product_variant.feature @@ -43,3 +43,38 @@ Feature: Seeing the correct catalog price history after creating a product varia Then I should see 2 log entries in the catalog price history And there should be a log entry on the 1st position with the "$10.00" selling price, "$20.00" original price and datetime of the price change And there should be a log entry on the 2nd position with the "$20.00" selling price, no original price and datetime of the price change + + @todo + Scenario: Seeing historical product variant prices visible in the catalog only, after the product variant has been created but disabled + Given the store has a "Wyborowa Vodka" configurable product + When I want to create a new variant of this product + And I specify its code as "WYBOROWA_VODKA" + And I set its price to "$20.00" for "United States" channel + And I set its original price to "$25.00" for "United States" channel + #And I disable it # It's not possible to disable a product variant in the current API state + And I add it + And I go to the "Wyborowa Vodka" product variant price history + Then I should not see any log entries in the catalog price history + + @api + Scenario: Seeing historical product variant prices visible in the catalog only, after the product variant has been created within the disabled product + Given the store has a "Wyborowa Vodka" configurable product + And this product has been disabled + When I want to create a new variant of this product + And I specify its code as "WYBOROWA_VODKA" + And I set its price to "$20.00" for "United States" channel + And I set its original price to "$25.00" for "United States" channel + And I add it + And I go to the "Wyborowa Vodka" product variant price history + Then I should not see any log entries in the catalog price history + + @api + Scenario: Seeing historical product variant prices visible in the catalog only, after the product variant has been created with no channel enabled + Given the store has a "Wyborowa Vodka" configurable product with no channel enabled + When I want to create a new variant of this product + And I specify its code as "WYBOROWA_VODKA" + And I set its price to "$20.00" for "United States" channel + And I set its original price to "$25.00" for "United States" channel + And I add it + And I go to the "Wyborowa Vodka" product variant price history + Then I should not see any log entries in the catalog price history diff --git a/features/product/managing_product_variants/seeing_correct_catalog_price_history_after_editing_product_variant.feature b/features/product/managing_product_variants/seeing_correct_catalog_price_history_after_editing_product_variant.feature index 2debfa70..a6ee8441 100644 --- a/features/product/managing_product_variants/seeing_correct_catalog_price_history_after_editing_product_variant.feature +++ b/features/product/managing_product_variants/seeing_correct_catalog_price_history_after_editing_product_variant.feature @@ -7,6 +7,7 @@ Feature: Seeing the correct catalog price history after editing a product varian Background: Given the store operates on a single channel in "United States" And the store has a product "Wyborowa Vodka" priced at "$40.00" in "United States" channel + And the store has a product "Orange Juice" priced at "$10.00" in "United States" channel And I am logged in as an administrator @api @@ -18,3 +19,29 @@ Feature: Seeing the correct catalog price history after editing a product varian Then I should see 2 log entries in the catalog price history And there should be a log entry on the 1st position with the "$42.00" selling price, no original price and datetime of the price change And there should be a log entry on the 2nd position with the "$40.00" selling price, no original price and datetime of the price change + + @api + Scenario: Seeing historical product variant prices after the product variant has been edited but the product disabled + Given the "Orange Juice" product is disabled + When I want to modify the "Orange Juice" product variant + And I change its price to "$15.00" for "United States" channel + And I save my changes + And I go to the "Orange Juice" product variant price history + Then I should see a single log entry in the catalog price history + And there should be a log entry with the "$10.00" selling price, no original price and datetime of the price change + + @api + Scenario: Seeing historical product variant prices after the product variant has been edited while it has been disabled + Given the "Orange Juice" product is disabled with a new price "$15.00" + When I go to the "Orange Juice" product variant price history + Then I should see a single log entry in the catalog price history + And there should be a log entry with the "$10.00" selling price, no original price and datetime of the price change + + @api + Scenario: Seeing historical product variant prices after the product variant has been edited while it has been disabled and then enabled + Given the "Orange Juice" product is disabled with a new price "$15.00" + And the "Orange Juice" product is enabled + When I go to the "Orange Juice" product variant price history + Then I should see 2 log entries in the catalog price history + And there should be a log entry on the 1st position with the "$15.00" selling price, no original price and datetime of the price change + And there should be a log entry on the 2nd position with the "$10.00" selling price, no original price and datetime of the price change diff --git a/tests/Behat/Context/Api/Admin/ChannelPricingLogEntryContext.php b/tests/Behat/Context/Api/Admin/ChannelPricingLogEntryContext.php index 6a83bb44..feee14b1 100644 --- a/tests/Behat/Context/Api/Admin/ChannelPricingLogEntryContext.php +++ b/tests/Behat/Context/Api/Admin/ChannelPricingLogEntryContext.php @@ -44,6 +44,7 @@ public function iGoToThePriceHistoryOfAVariant(ProductVariantInterface $productV $this->client->index(); $this->client->addFilter('channelPricing.channelCode', $channel->getCode()); $this->client->addFilter('channelPricing.productVariant.code', $productVariant->getCode()); + $this->client->addFilter('visible', true); $this->client->filter(); } @@ -56,6 +57,14 @@ public function iShouldSeeLogEntriesInTheCatalogPriceHistoryForTheVariant(int $c Assert::same($this->responseChecker->countCollectionItems($this->client->getLastResponse()), $count); } + /** + * @Then I should not see any log entries in the catalog price history + */ + public function iShouldNotSeeAnyLogEntriesInTheCatalogPriceHistoryForTheVariant(): void + { + $this->iShouldSeeLogEntriesInTheCatalogPriceHistoryForTheVariant(0); + } + /** * @Then /^there should be a log entry on the (\d+)(?:|st|nd|rd|th) position with the ("[^"]+") selling price, (no|"[^"]+") original price and datetime of the price change$/ */ @@ -72,6 +81,7 @@ public function thereShouldBeALogEntryOnThePositionWithTheSellingPriceOriginalPr Assert::same($logEntry['price'], $price); Assert::same($logEntry['originalPrice'], $originalPrice); + Assert::same($logEntry['isVisible'], true); Assert::keyExists($logEntry, 'loggedAt'); } diff --git a/tests/Behat/Context/Setup/ProductContext.php b/tests/Behat/Context/Setup/ProductContext.php index 34f84e99..815ebd1a 100644 --- a/tests/Behat/Context/Setup/ProductContext.php +++ b/tests/Behat/Context/Setup/ProductContext.php @@ -21,6 +21,8 @@ use Sylius\Component\Core\Model\ProductVariantInterface; use Sylius\Component\Core\Repository\ProductRepositoryInterface; use Sylius\Component\Core\Model\ProductInterface; +use Sylius\Component\Locale\Provider\LocaleProviderInterface; +use Sylius\Component\Product\Generator\SlugGeneratorInterface; use Sylius\Component\Product\Resolver\ProductVariantResolverInterface; use Sylius\Component\Resource\Factory\FactoryInterface; use Symfony\Component\Messenger\MessageBusInterface; @@ -32,11 +34,32 @@ public function __construct( private ProductRepositoryInterface $productRepository, private ProductVariantResolverInterface $defaultVariantResolver, private MessageBusInterface $eventBus, + private FactoryInterface $productFactory, private FactoryInterface $productVariantFactory, private FactoryInterface $channelPricingFactory, + private SlugGeneratorInterface $slugGenerator, + private LocaleProviderInterface $localeProvider, ) { } + /** + * @Given /^the store has(?:| a| an) "([^"]+)" configurable product with no channel enabled$/ + */ + public function storeHasAConfigurableProductWithNoChannelEnabled($productName) + { + /** @var ProductInterface $product */ + $product = $this->productFactory->createNew(); + $product->setCode(StringInflector::nameToUppercaseCode($productName)); + + $product->setFallbackLocale($this->localeProvider->getDefaultLocaleCode()); + $product->setCurrentLocale($this->localeProvider->getDefaultLocaleCode()); + + $product->setName($productName); + $product->setSlug($this->slugGenerator->generate($productName)); + + $this->saveProduct($product); + } + /** * @Given /^the ("[^"]+" product) is now priced at ("[^"]+") and originally priced at ("[^"]+")$/ */ @@ -56,6 +79,20 @@ public function theProductIsPricedAtAndOriginallyPricedAt( $this->saveProduct($product); } + /** + * @Given /^the ("[^"]+" product) is disabled with a new price ("[^"]+")$/ + */ + public function theProductIsDisabledWithANewPrice(ProductInterface $product, int $price) + { + /** @var ProductVariantInterface $productVariant */ + $productVariant = $this->defaultVariantResolver->getVariant($product); + $productVariant->disable(); + $channelPricing = $productVariant->getChannelPricingForChannel($this->sharedStorage->get('channel')); + $channelPricing->setPrice($price); + + $this->saveProduct($product); + } + /** * @Given /^the (product "[^"]+") has a "([^"]+)" variant priced at ("[^"]+") and originally priced at ("[^"]+")$/ */ diff --git a/tests/Behat/Resources/services/contexts/setup.xml b/tests/Behat/Resources/services/contexts/setup.xml index 978f2f02..0e1e44e3 100644 --- a/tests/Behat/Resources/services/contexts/setup.xml +++ b/tests/Behat/Resources/services/contexts/setup.xml @@ -25,8 +25,11 @@ + + + From 68c7f5e546e3641a0567cc5171a0e69532c0252d Mon Sep 17 00:00:00 2001 From: Kevin Kaniaburka Date: Thu, 2 Feb 2023 13:55:26 +0100 Subject: [PATCH 3/4] [Api][Admin] Add contract tests for seeing entry logs with a visible flag --- .../Api/Admin/ChannelPricingLogEntryTest.php | 25 +++++++++ .../Api/DataFixtures/ORM/product_variant.yaml | 54 +++++++++++++++++++ ..._channel_pricing_log_entries_response.json | 48 +++++++++++++++-- ...et_channel_pricing_log_entry_response.json | 1 + ..._channel_pricing_log_entries_response.json | 1 + 5 files changed, 125 insertions(+), 4 deletions(-) diff --git a/tests/Api/Admin/ChannelPricingLogEntryTest.php b/tests/Api/Admin/ChannelPricingLogEntryTest.php index 80db4f46..80d1e240 100644 --- a/tests/Api/Admin/ChannelPricingLogEntryTest.php +++ b/tests/Api/Admin/ChannelPricingLogEntryTest.php @@ -110,6 +110,31 @@ public function it_gets_filtered_channel_pricing_log_entries(): void ); } + /** @test */ + public function it_gets_visible_channel_pricing_log_entries(): void + { + $fixtures = $this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'product_variant.yaml']); + $header = $this->getLoggedHeader(); + + $uri = '/api/v2/admin/channel-pricing-log-entries'; + $uri .= '?channelPricing.channelCode=' . $fixtures['channel_home']->getCode(); + $uri .= '&channelPricing.productVariant.code[]=' . $fixtures['product_variant_mug_blue']->getCode(); + $uri .= '&channelPricing.productVariant.code[]=' . $fixtures['product_variant_mug_white']->getCode(); + $uri .= '&visible=true'; + + $this->client->request( + method: 'GET', + uri: $uri, + server: $header, + ); + + $this->assertResponse( + $this->client->getResponse(), + 'admin/get_filtered_channel_pricing_log_entries_response', + Response::HTTP_OK + ); + } + private function getLoggedHeader(): array { $token = $this->logInAdminUser('api@example.com'); diff --git a/tests/Api/DataFixtures/ORM/product_variant.yaml b/tests/Api/DataFixtures/ORM/product_variant.yaml index d7ae4c46..57e12a51 100644 --- a/tests/Api/DataFixtures/ORM/product_variant.yaml +++ b/tests/Api/DataFixtures/ORM/product_variant.yaml @@ -41,6 +41,13 @@ Sylius\Component\Core\Model\Product: currentLocale: 'en_US' translations: en_US: '@product_translation_mug' + product_cup: + code: 'CUP' + enabled: false + channels: ['@channel_home', '@channel_fashion'] + currentLocale: 'en_US' + translations: + en_US: '@product_translation_cup' Sylius\Component\Core\Model\ProductTranslation: product_translation_mug: @@ -49,6 +56,12 @@ Sylius\Component\Core\Model\ProductTranslation: name: 'Mug' description: '' translatable: '@product_mug' + product_translation_cup: + slug: 'cup' + locale: 'en_US' + name: 'Cup' + description: '' + translatable: '@product_cup' Sylius\Component\Core\Model\ProductVariant: product_variant_mug_blue: @@ -70,6 +83,27 @@ Sylius\Component\Core\Model\ProductVariant: channelPricings: HOME: '@channel_pricing_product_variant_mug_red_home' FASHION: '@channel_pricing_product_variant_mug_red_fashion' + + product_variant_mug_white: + code: 'MUG_WHITE' + enabled: false + product: '@product_mug' + currentLocale: 'en_US' + translations: + en_US: '@product_variant_translation_mug_white' + channelPricings: + HOME: '@channel_pricing_product_variant_mug_white_home' + FASHION: '@channel_pricing_product_variant_mug_white_fashion' + + product_variant_cup: + code: 'CUP' + product: '@product_cup' + currentLocale: 'en_US' + translations: + en_US: '@product_variant_translation_cup' + channelPricings: + HOME: '@channel_pricing_product_variant_cup_home' + FASHION: '@channel_pricing_product_variant_cup_fashion' Sylius\Component\Product\Model\ProductVariantTranslation: product_variant_translation_mug_blue: @@ -80,6 +114,14 @@ Sylius\Component\Product\Model\ProductVariantTranslation: locale: 'en_US' name: 'Red Mug' translatable: '@product_variant_mug_red' + product_variant_translation_mug_white: + locale: 'en_US' + name: 'White Mug' + translatable: '@product_variant_mug_white' + product_variant_translation_cup: + locale: 'en_US' + name: 'Cup' + translatable: '@product_variant_cup' Sylius\Component\Core\Model\ChannelPricing: channel_pricing_product_variant_mug_blue_home: @@ -95,3 +137,15 @@ Sylius\Component\Core\Model\ChannelPricing: channel_pricing_product_variant_mug_red_fashion: channelCode: 'FASHION' price: 3000 + channel_pricing_product_variant_mug_white_home: + channelCode: 'HOME' + price: 1500 + channel_pricing_product_variant_mug_white_fashion: + channelCode: 'FASHION' + price: 2000 + channel_pricing_product_variant_cup_home: + channelCode: 'HOME' + price: 1500 + channel_pricing_product_variant_cup_fashion: + channelCode: 'FASHION' + price: 2500 diff --git a/tests/Api/Responses/admin/get_channel_pricing_log_entries_response.json b/tests/Api/Responses/admin/get_channel_pricing_log_entries_response.json index 664738f6..d75e28cf 100644 --- a/tests/Api/Responses/admin/get_channel_pricing_log_entries_response.json +++ b/tests/Api/Responses/admin/get_channel_pricing_log_entries_response.json @@ -7,16 +7,45 @@ "@id": "\/api\/v2\/admin\/channel-pricing-log-entries\/@integer@", "@type": "ChannelPricingLogEntry", "channelPricing": "\/api\/v2\/admin\/channel-pricings\/@integer@", - "price": 3000, + "price": 2500, "originalPrice": null, + "visible": false, "loggedAt": "@date@" }, { "@id": "\/api\/v2\/admin\/channel-pricing-log-entries\/@integer@", "@type": "ChannelPricingLogEntry", "channelPricing": "\/api\/v2\/admin\/channel-pricings\/@integer@", - "price": 1000, - "originalPrice": 2000, + "price": 1500, + "originalPrice": null, + "visible": false, + "loggedAt": "@date@" + }, + { + "@id": "\/api\/v2\/admin\/channel-pricing-log-entries\/@integer@", + "@type": "ChannelPricingLogEntry", + "channelPricing": "\/api\/v2\/admin\/channel-pricings\/@integer@", + "price": 2000, + "originalPrice": null, + "visible": false, + "loggedAt": "@date@" + }, + { + "@id": "\/api\/v2\/admin\/channel-pricing-log-entries\/@integer@", + "@type": "ChannelPricingLogEntry", + "channelPricing": "\/api\/v2\/admin\/channel-pricings\/@integer@", + "price": 1500, + "originalPrice": null, + "visible": false, + "loggedAt": "@date@" + }, + { + "@id": "\/api\/v2\/admin\/channel-pricing-log-entries\/@integer@", + "@type": "ChannelPricingLogEntry", + "channelPricing": "\/api\/v2\/admin\/channel-pricings\/@integer@", + "price": 3000, + "originalPrice": null, + "visible": true, "loggedAt": "@date@" }, { @@ -25,6 +54,7 @@ "channelPricing": "\/api\/v2\/admin\/channel-pricings\/@integer@", "price": 3000, "originalPrice": null, + "visible": true, "loggedAt": "@date@" }, { @@ -33,10 +63,20 @@ "channelPricing": "\/api\/v2\/admin\/channel-pricings\/@integer@", "price": 2000, "originalPrice": null, + "visible": true, + "loggedAt": "@date@" + }, + { + "@id": "\/api\/v2\/admin\/channel-pricing-log-entries\/@integer@", + "@type": "ChannelPricingLogEntry", + "channelPricing": "\/api\/v2\/admin\/channel-pricings\/@integer@", + "price": 1000, + "originalPrice": 2000, + "visible": true, "loggedAt": "@date@" } ], - "hydra:totalItems": 4, + "hydra:totalItems": 8, "hydra:search": { "@type": "hydra:IriTemplate", "hydra:template": "\/api\/v2\/admin\/channel-pricing-log-entries{?channelPricing.channelCode,channelPricing.channelCode[],channelPricing.productVariant.code,channelPricing.productVariant.code[]}", diff --git a/tests/Api/Responses/admin/get_channel_pricing_log_entry_response.json b/tests/Api/Responses/admin/get_channel_pricing_log_entry_response.json index 45ab8c17..0ca88fd4 100644 --- a/tests/Api/Responses/admin/get_channel_pricing_log_entry_response.json +++ b/tests/Api/Responses/admin/get_channel_pricing_log_entry_response.json @@ -5,5 +5,6 @@ "channelPricing": "\/api\/v2\/admin\/channel-pricings\/@integer@", "price": 1000, "originalPrice": 2000, + "visible": true, "loggedAt": "@date@" } diff --git a/tests/Api/Responses/admin/get_filtered_channel_pricing_log_entries_response.json b/tests/Api/Responses/admin/get_filtered_channel_pricing_log_entries_response.json index 8e4f0b72..c6a16923 100644 --- a/tests/Api/Responses/admin/get_filtered_channel_pricing_log_entries_response.json +++ b/tests/Api/Responses/admin/get_filtered_channel_pricing_log_entries_response.json @@ -9,6 +9,7 @@ "channelPricing": "\/api\/v2\/admin\/channel-pricings\/@integer@", "price": 1000, "originalPrice": 2000, + "visible": true, "loggedAt": "@date@" } ], From 28c78cbb3e408b636a29cbc5038e425ccbab43ee Mon Sep 17 00:00:00 2001 From: Kevin Kaniaburka Date: Thu, 2 Feb 2023 15:30:13 +0100 Subject: [PATCH 4/4] WIP --- .../doctrine/ChannelPricingLogEntry.orm.xml | 1 + .../serialization/ChannelPricingLogEntry.xml | 3 ++ config/services/checkers.xml | 24 ++++++++++++++ config/services/filters.xml | 7 +++++ config/services/listeners.xml | 2 ++ spec/Model/ChannelPricingLogEntrySpec.php | 16 ++++++++-- .../ChannelPricingChangeListener.php | 15 ++++++++- src/Migrations/Version20230202130642.php | 31 +++++++++++++++++++ src/Model/ChannelPricingLogEntry.php | 6 ++++ src/Model/ChannelPricingLogEntryInterface.php | 2 ++ 10 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 config/services/checkers.xml create mode 100644 src/Migrations/Version20230202130642.php diff --git a/config/doctrine/ChannelPricingLogEntry.orm.xml b/config/doctrine/ChannelPricingLogEntry.orm.xml index 6aa42b4a..1db10593 100644 --- a/config/doctrine/ChannelPricingLogEntry.orm.xml +++ b/config/doctrine/ChannelPricingLogEntry.orm.xml @@ -24,6 +24,7 @@ + diff --git a/config/serialization/ChannelPricingLogEntry.xml b/config/serialization/ChannelPricingLogEntry.xml index 7eb29004..499b9683 100644 --- a/config/serialization/ChannelPricingLogEntry.xml +++ b/config/serialization/ChannelPricingLogEntry.xml @@ -25,6 +25,9 @@ admin:channel_pricing_log_entry:read + + admin:channel-pricing-log-entry:read + admin:channel_pricing_log_entry:read diff --git a/config/services/checkers.xml b/config/services/checkers.xml new file mode 100644 index 00000000..055ff133 --- /dev/null +++ b/config/services/checkers.xml @@ -0,0 +1,24 @@ + + + + + + + + + diff --git a/config/services/filters.xml b/config/services/filters.xml index fe975023..cdfc3c58 100644 --- a/config/services/filters.xml +++ b/config/services/filters.xml @@ -29,5 +29,12 @@ + + + + + + + diff --git a/config/services/listeners.xml b/config/services/listeners.xml index e17bfa7a..3ebefef9 100644 --- a/config/services/listeners.xml +++ b/config/services/listeners.xml @@ -17,6 +17,8 @@ > + + diff --git a/spec/Model/ChannelPricingLogEntrySpec.php b/spec/Model/ChannelPricingLogEntrySpec.php index 2ec8933b..02978509 100644 --- a/spec/Model/ChannelPricingLogEntrySpec.php +++ b/spec/Model/ChannelPricingLogEntrySpec.php @@ -21,7 +21,7 @@ class ChannelPricingLogEntrySpec extends ObjectBehavior { function let(ChannelPricingInterface $channelPricing): void { - $this->beConstructedWith($channelPricing, 1000, 2000); + $this->beConstructedWith($channelPricing, 1000, 2000, true); } function it_implements_channel_pricing_log_entry_interface(): void @@ -31,7 +31,7 @@ function it_implements_channel_pricing_log_entry_interface(): void function it_initialize_with_no_original_price(ChannelPricingInterface $channelPricing): void { - $this->beConstructedWith($channelPricing, 1000, null); + $this->beConstructedWith($channelPricing, 1000, null, true); $this->getOriginalPrice()->shouldReturn(null); } @@ -54,4 +54,16 @@ function it_gets_a_logged_at(): void { $this->getLoggedAt()->shouldReturnAnInstanceOf(\DateTimeImmutable::class); } + + function it_can_be_visible(): void + { + $this->isVisible()->shouldReturn(true); + } + + function it_can_be_invisible(ChannelPricingInterface $channelPricing): void + { + $this->beConstructedWith($channelPricing, 1000, 2000, false); + + $this->isVisible()->shouldReturn(false); + } } diff --git a/src/EventListener/ChannelPricingChangeListener.php b/src/EventListener/ChannelPricingChangeListener.php index eb7328ce..64b557d6 100644 --- a/src/EventListener/ChannelPricingChangeListener.php +++ b/src/EventListener/ChannelPricingChangeListener.php @@ -16,7 +16,10 @@ use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Event\OnFlushEventArgs; use Doctrine\ORM\UnitOfWork; +use Sylius\Component\Channel\Repository\ChannelRepositoryInterface; use Sylius\Component\Core\Model\ChannelPricingInterface; +use Sylius\Component\Resource\Repository\RepositoryInterface; +use Sylius\PriceHistoryPlugin\Checker\ProductVariantVisibilityCheckerInterface; use Sylius\PriceHistoryPlugin\Model\ChannelPricingLogEntry; use Webmozart\Assert\Assert; @@ -24,6 +27,12 @@ final class ChannelPricingChangeListener { private const SUPPORTED_FIELDS = ['price', 'originalPrice']; + public function __construct( + private ProductVariantVisibilityCheckerInterface $productVariantVisibilityChecker, + private ChannelRepositoryInterface $channelRepository, + ) { + } + public function onFlush(OnFlushEventArgs $eventArgs): void { $entityManager = $eventArgs->getObjectManager(); @@ -65,7 +74,11 @@ private function createLogEntry(ChannelPricingInterface $model): ChannelPricingL { Assert::notNull($price = $model->getPrice()); - return new ChannelPricingLogEntry($model, $price, $model->getOriginalPrice()); + $channel = $this->channelRepository->findOneByCode($model->getChannelCode()); + dd($channel); + $isChangeVisible = $this->productVariantVisibilityChecker->isVisibleInChannel($model->getProductVariant(), $channel); + + return new ChannelPricingLogEntry($model, $price, $model->getOriginalPrice(), $isChangeVisible); } private function isPriceChanged(UnitOfWork $unitOfWork, ChannelPricingInterface $channelPricing): bool diff --git a/src/Migrations/Version20230202130642.php b/src/Migrations/Version20230202130642.php new file mode 100644 index 00000000..f25544da --- /dev/null +++ b/src/Migrations/Version20230202130642.php @@ -0,0 +1,31 @@ +addSql('ALTER TABLE sylius_price_history_channel_pricing_log_entry ADD visible TINYINT(1) NOT NULL'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE sylius_price_history_channel_pricing_log_entry DROP visible'); + } +} diff --git a/src/Model/ChannelPricingLogEntry.php b/src/Model/ChannelPricingLogEntry.php index 3878edd2..a232209a 100644 --- a/src/Model/ChannelPricingLogEntry.php +++ b/src/Model/ChannelPricingLogEntry.php @@ -26,6 +26,7 @@ public function __construct( protected ChannelPricingInterface $channelPricing, protected int $price, protected ?int $originalPrice, + protected bool $visible, ) { $this->loggedAt = new \DateTimeImmutable(); } @@ -57,4 +58,9 @@ public function getLoggedAt(): \DateTimeInterface { return $this->loggedAt; } + + public function isVisible(): bool + { + return $this->visible; + } } diff --git a/src/Model/ChannelPricingLogEntryInterface.php b/src/Model/ChannelPricingLogEntryInterface.php index 6d184204..5c79a1cb 100644 --- a/src/Model/ChannelPricingLogEntryInterface.php +++ b/src/Model/ChannelPricingLogEntryInterface.php @@ -24,5 +24,7 @@ public function getPrice(): int; public function getOriginalPrice(): ?int; + public function isVisible(): bool; + public function getLoggedAt(): \DateTimeInterface; }