Skip to content

Commit

Permalink
Merge pull request #8 from delyriand/fix/avoid-error-during-remove-ex…
Browse files Browse the repository at this point in the history
…pired-carts

Fix: avoid error during remove expired cart if coupon used
  • Loading branch information
delyriand authored Nov 14, 2024
2 parents 9a937bb + 4f44b64 commit 694b74b
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 7 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ COMPOSER=symfony composer
CONSOLE=${SYMFONY} console
export COMPOSE_PROJECT_NAME=advanced-promotion
PLUGIN_NAME=sylius-${COMPOSE_PROJECT_NAME}-plugin
COMPOSE=docker-compose
COMPOSE=docker compose
YARN=yarn
DOCTRINE_MIGRATIONS_NAMESPACE=MonsieurBiz\SyliusAdvancedPromotionPlugin\Migrations

Expand Down Expand Up @@ -85,6 +85,8 @@ setup_application:
${APP_DIR}/docker-compose.yaml:
rm -f ${APP_DIR}/docker-compose.yml
rm -f ${APP_DIR}/docker-compose.yaml
rm -f ${APP_DIR}/compose.yml # Remove Sylius file about Docker
rm -f ${APP_DIR}/compose.override.dist.yml # Remove Sylius file about Docker
ln -s ../../docker-compose.yaml.dist ${APP_DIR}/docker-compose.yaml
.PHONY: ${APP_DIR}/docker-compose.yaml

Expand Down
44 changes: 44 additions & 0 deletions dist/src/Migrations/Version20241114094753.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of Monsieur Biz' Advanced Promotion plugin for Sylius.
* (c) Monsieur Biz <[email protected]>
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20241114094753 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE monsieurbiz_advanced_promotion_order_promotion_coupon DROP FOREIGN KEY FK_5C4132AF17B24436');
$this->addSql('ALTER TABLE monsieurbiz_advanced_promotion_order_promotion_coupon DROP FOREIGN KEY FK_5C4132AF8D9F6D38');
$this->addSql('ALTER TABLE monsieurbiz_advanced_promotion_order_promotion_coupon ADD CONSTRAINT FK_5C4132AF17B24436 FOREIGN KEY (promotion_coupon_id) REFERENCES sylius_promotion_coupon (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE monsieurbiz_advanced_promotion_order_promotion_coupon ADD CONSTRAINT FK_5C4132AF8D9F6D38 FOREIGN KEY (order_id) REFERENCES sylius_order (id) ON DELETE CASCADE');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE monsieurbiz_advanced_promotion_order_promotion_coupon DROP FOREIGN KEY FK_5C4132AF8D9F6D38');
$this->addSql('ALTER TABLE monsieurbiz_advanced_promotion_order_promotion_coupon DROP FOREIGN KEY FK_5C4132AF17B24436');
$this->addSql('ALTER TABLE monsieurbiz_advanced_promotion_order_promotion_coupon ADD CONSTRAINT FK_5C4132AF8D9F6D38 FOREIGN KEY (order_id) REFERENCES sylius_order (id) ON UPDATE NO ACTION ON DELETE NO ACTION');
$this->addSql('ALTER TABLE monsieurbiz_advanced_promotion_order_promotion_coupon ADD CONSTRAINT FK_5C4132AF17B24436 FOREIGN KEY (promotion_coupon_id) REFERENCES sylius_promotion_coupon (id) ON UPDATE NO ACTION ON DELETE NO ACTION');
}
}
8 changes: 4 additions & 4 deletions src/Entity/PromotionCouponsAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ trait PromotionCouponsAwareTrait
/**
* @ORM\ManyToMany(targetEntity=PromotionCouponInterface::class)
* @ORM\JoinTable(name="monsieurbiz_advanced_promotion_order_promotion_coupon",
* joinColumns={@ORM\JoinColumn(name="order_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="promotion_coupon_id", referencedColumnName="id")}
* joinColumns={@ORM\JoinColumn(name="order_id", referencedColumnName="id", onDelete="CASCADE")},
* inverseJoinColumns={@ORM\JoinColumn(name="promotion_coupon_id", referencedColumnName="id", onDelete="CASCADE")}
* )
*
* @var Collection<array-key, PromotionCouponInterface>
*/
#[ORM\ManyToMany(targetEntity: PromotionCouponInterface::class)]
#[ORM\JoinTable(name: 'monsieurbiz_advanced_promotion_order_promotion_coupon')]
#[ORM\JoinColumn(name: 'order_id', referencedColumnName: 'id')]
#[ORM\InverseJoinColumn(name: 'promotion_coupon_id', referencedColumnName: 'id')]
#[ORM\JoinColumn(name: 'order_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
#[ORM\InverseJoinColumn(name: 'promotion_coupon_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
private $promotionCoupons;

private function initializePromotionCoupons(): void
Expand Down
16 changes: 14 additions & 2 deletions src/Fixture/AdvancedPromotionFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace MonsieurBiz\SyliusAdvancedPromotionPlugin\Fixture;

use Doctrine\ORM\EntityManagerInterface;
use InvalidArgumentException;
use MonsieurBiz\SyliusAdvancedPromotionPlugin\Entity\AfterTaxAwareInterface;
use Sylius\Bundle\FixturesBundle\Fixture\AbstractFixture;
use Sylius\Bundle\FixturesBundle\Fixture\FixtureInterface;
Expand All @@ -31,14 +32,25 @@ public function getName(): string
return 'monsieurbiz_advanced_promotion';
}

/**
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function load(array $options): void
{
foreach ($options['promotion_advanced_configuration'] as $data) {
$config = $options['promotion_advanced_configuration'] ?? [];
if (!\is_array($config)) {
throw new InvalidArgumentException('The "promotion_advanced_configuration" option must be an array.');
}
/** @var array $data */
foreach ($config as $data) {
if (!isset($data['code'])) {
continue;
}
$promotion = $this->promotionRepository->findOneBy(['code' => $data['code']]);
if (null === $promotion || !$promotion instanceof AfterTaxAwareInterface) {
continue;
}
$promotion->setAfterTax($data['after_tax']);
$promotion->setAfterTax($data['after_tax'] ?? false);
$this->entityManager->persist($promotion);
}

Expand Down

0 comments on commit 694b74b

Please sign in to comment.