diff --git a/composer.json b/composer.json index fdd02d3..ba4fe40 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ "require": { "php": "^8.0", "beberlei/doctrineextensions": "^1.2", - "sylius/sylius": "~1.12 || ~1.13", + "sylius/sylius": "~1.11 || ~1.12 || ~1.13", "sylius/resource-bundle": "^1.6", "symfony/config": "^5.4 || ^6.0", "symfony/lock": "^5.4 || ^6.0", @@ -23,7 +23,7 @@ "roave/security-advisories": "dev-master", "setono/code-quality-pack": "^2.1.2", "setono/sylius-behat-pack": "^0.2.1", - "behat/behat": "^3.6.1", + "behat/behat": "^1.8 || 3.6.1", "behat/mink-selenium2-driver": "^1.4", "dmore/behat-chrome-extension": "^1.3", "dmore/chrome-mink-driver": "^2.7", @@ -50,7 +50,8 @@ "symfony/intl": "^5.4 || ^6.0", "symfony/web-profiler-bundle": "^5.4 || ^6.0", "symfony/webpack-encore-bundle": "^1.14", - "doctrine/persistence": "^3.0" + "symfony/http-foundation": "^4.4 || ^5.0.7", + "doctrine/persistence": "^2.5 || ^3.0" }, "conflict": { "behat/mink-selenium2-driver": ">=1.7.0", diff --git a/src/EventListener/UpdateManuallyDiscountedPropertySubscriber.php b/src/EventListener/UpdateManuallyDiscountedPropertySubscriber.php index bda67b7..c3cec35 100644 --- a/src/EventListener/UpdateManuallyDiscountedPropertySubscriber.php +++ b/src/EventListener/UpdateManuallyDiscountedPropertySubscriber.php @@ -9,12 +9,20 @@ use Doctrine\ORM\Event\PreUpdateEventArgs; use Doctrine\ORM\Events; use Setono\SyliusCatalogPromotionPlugin\Model\ChannelPricingInterface; +use Symfony\Component\HttpFoundation\RequestStack; /** * This subscriber has the responsibility to mark a channel pricing as manually discounted if the user did so manually */ final class UpdateManuallyDiscountedPropertySubscriber implements EventSubscriber { + private RequestStack $requestStack; + + public function __construct(RequestStack $requestStack) + { + $this->requestStack = $requestStack; + } + public function getSubscribedEvents(): array { return [ @@ -25,31 +33,45 @@ public function getSubscribedEvents(): array public function prePersist(LifecycleEventArgs $event): void { - self::update($event); - } + $channelPricing = $event->getEntity(); + if (!$channelPricing instanceof ChannelPricingInterface) { + return; + } - public function preUpdate(LifecycleEventArgs $event): void - { - self::update($event); + $channelPricing->setManuallyDiscounted($channelPricing->hasDiscount()); + $this->setOrigin($channelPricing); } - private static function update(LifecycleEventArgs $event): void + public function preUpdate(PreUpdateEventArgs $event): void { $channelPricing = $event->getEntity(); if (!$channelPricing instanceof ChannelPricingInterface) { return; } - if ($event instanceof PreUpdateEventArgs - && !$event->hasChangedField('price') - && !$event->hasChangedField('originalPrice') - ) { + if (!$event->hasChangedField('price') && !$event->hasChangedField('originalPrice')) { return; } - $channelPricing->resetBulkIdentifier(); - $channelPricing->setManuallyDiscounted( - $channelPricing->hasDiscount() - ); + $channelPricing->setManuallyDiscounted($channelPricing->hasDiscount()); + $this->setOrigin($channelPricing); + } + + private function setOrigin(ChannelPricingInterface $channelPricing): void + { + if (!$channelPricing->isManuallyDiscounted()) { + return; + } + + $origin = ''; + + $request = $this->requestStack->getMasterRequest(); + if (null !== $request) { + $origin .= $request->getUri() . "\n\n"; + } + + $origin .= print_r(debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS), true); + + $channelPricing->setManuallyDiscountedOrigin($origin); } } diff --git a/src/Model/ChannelPricingInterface.php b/src/Model/ChannelPricingInterface.php index 47204a0..f8530f8 100644 --- a/src/Model/ChannelPricingInterface.php +++ b/src/Model/ChannelPricingInterface.php @@ -25,6 +25,13 @@ public function isManuallyDiscounted(): bool; public function setManuallyDiscounted(bool $manuallyDiscounted): void; + /** + * Returns the origin of the setManuallyDiscounted() method call if isManuallyDiscounted() is true + */ + public function getManuallyDiscountedOrigin(): ?string; + + public function setManuallyDiscountedOrigin(?string $manuallyDiscountedOrigin): void; + public function getMultiplier(): float; public function getBulkIdentifier(): ?string; diff --git a/src/Model/ChannelPricingTrait.php b/src/Model/ChannelPricingTrait.php index 7bd5b56..822c66f 100644 --- a/src/Model/ChannelPricingTrait.php +++ b/src/Model/ChannelPricingTrait.php @@ -24,6 +24,9 @@ trait ChannelPricingTrait */ protected $manuallyDiscounted = false; + /** @ORM\Column(type="text", nullable=true) */ + protected ?string $manuallyDiscountedOrigin = null; + /** * @ORM\Column(type="decimal", precision=8, scale=4, options={"default": 1}) * @@ -92,6 +95,16 @@ public function setManuallyDiscounted(bool $manuallyDiscounted): void $this->manuallyDiscounted = $manuallyDiscounted; } + public function getManuallyDiscountedOrigin(): ?string + { + return $this->manuallyDiscountedOrigin; + } + + public function setManuallyDiscountedOrigin(?string $manuallyDiscountedOrigin): void + { + $this->manuallyDiscountedOrigin = $manuallyDiscountedOrigin; + } + public function getMultiplier(): float { return $this->multiplier; diff --git a/src/Resources/config/services/event_listener.xml b/src/Resources/config/services/event_listener.xml index 5fc19ad..0e6c7c4 100644 --- a/src/Resources/config/services/event_listener.xml +++ b/src/Resources/config/services/event_listener.xml @@ -20,7 +20,10 @@ + class="Setono\SyliusCatalogPromotionPlugin\EventListener\UpdateManuallyDiscountedPropertySubscriber" + > + +