Skip to content

Commit

Permalink
fix manuallyDiscounted price
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherhero committed Oct 6, 2024
1 parent 6dea34f commit b73ff2d
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 18 deletions.
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down
50 changes: 36 additions & 14 deletions src/EventListener/UpdateManuallyDiscountedPropertySubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand All @@ -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);
}
}
7 changes: 7 additions & 0 deletions src/Model/ChannelPricingInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions src/Model/ChannelPricingTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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})
*
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion src/Resources/config/services/event_listener.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
</service>

<service id="setono_sylius_catalog_promotion.event_listener.update_manually_discounted_property"
class="Setono\SyliusCatalogPromotionPlugin\EventListener\UpdateManuallyDiscountedPropertySubscriber">
class="Setono\SyliusCatalogPromotionPlugin\EventListener\UpdateManuallyDiscountedPropertySubscriber"
>
<argument type="service" id="request_stack"/>

<tag name="doctrine.event_subscriber"/>
</service>
</services>
Expand Down

0 comments on commit b73ff2d

Please sign in to comment.