Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Be able to manage promotion after tax #2

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file removed dist/.gitkeep
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
imports:
resource: '@MonsieurBizSyliusAdvancedPromotionPlugin/Resources/config/config.yaml'
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
imports:
resource: '@MonsieurBizSyliusAdvancedPromotionPlugin/Resources/config/routes.yaml'
27 changes: 27 additions & 0 deletions dist/src/Entity/Promotion/Promotion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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\Entity\Promotion;

use Doctrine\ORM\Mapping as ORM;
use MonsieurBiz\SyliusAdvancedPromotionPlugin\Entity\AfterTaxAwareTrait;
use Sylius\Component\Core\Model\Promotion as BasePromotion;

/**
* @ORM\Entity
* @ORM\Table(name="sylius_promotion")
*/
#[ORM\Entity]
#[ORM\Table(name: 'sylius_promotion')]
class Promotion extends BasePromotion implements PromotionInterface
{
use AfterTaxAwareTrait;
}
19 changes: 19 additions & 0 deletions dist/src/Entity/Promotion/PromotionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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\Entity\Promotion;

use MonsieurBiz\SyliusAdvancedPromotionPlugin\Entity\AfterTaxAwareInterface;
use Sylius\Component\Core\Model\PromotionInterface as BasePromotionInterface;

interface PromotionInterface extends BasePromotionInterface, AfterTaxAwareInterface
{
}
38 changes: 38 additions & 0 deletions dist/src/Migrations/Version20231211134913.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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 Version20231211134913 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 sylius_promotion ADD after_tax TINYINT(1) DEFAULT 0 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_promotion DROP after_tax');
}
}
27 changes: 27 additions & 0 deletions src/Context/PromotionContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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 MonsieurBiz\SyliusAdvancedPromotionPlugin\Context;

class PromotionContext implements PromotionContextInterface
{
protected bool $afterTax = false;

public function isAfterTax(): bool
{
return $this->afterTax ?? false;
}

public function setAfterTax(bool $afterTax): void
{
$this->afterTax = $afterTax;
}
}
19 changes: 19 additions & 0 deletions src/Context/PromotionContextInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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 MonsieurBiz\SyliusAdvancedPromotionPlugin\Context;

interface PromotionContextInterface
{
public function isAfterTax(): bool;

public function setAfterTax(bool $afterTax): void;
}
19 changes: 19 additions & 0 deletions src/Entity/AfterTaxAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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 MonsieurBiz\SyliusAdvancedPromotionPlugin\Entity;

interface AfterTaxAwareInterface
{
public function isAfterTax(): bool;

public function setAfterTax(?bool $afterTax): void;
}
33 changes: 33 additions & 0 deletions src/Entity/AfterTaxAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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 MonsieurBiz\SyliusAdvancedPromotionPlugin\Entity;

use Doctrine\ORM\Mapping as ORM;

trait AfterTaxAwareTrait
{
/**
* @ORM\Column(name="after_tax", type="boolean", nullable=false, options={"default": false})
*/
#[ORM\Column(name: 'after_tax', type: 'boolean', nullable: false, options: ['default' => false])]
protected bool $afterTax = false;

public function isAfterTax(): bool
{
return $this->afterTax;
}

public function setAfterTax(?bool $afterTax): void
{
$this->afterTax = (bool) $afterTax;
}
}
63 changes: 63 additions & 0 deletions src/Fixture/AdvancedPromotionFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?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 MonsieurBiz\SyliusAdvancedPromotionPlugin\Fixture;

use Doctrine\ORM\EntityManagerInterface;
use MonsieurBiz\SyliusAdvancedPromotionPlugin\Entity\AfterTaxAwareInterface;
use Sylius\Bundle\FixturesBundle\Fixture\AbstractFixture;
use Sylius\Bundle\FixturesBundle\Fixture\FixtureInterface;
use Sylius\Component\Core\Repository\PromotionRepositoryInterface;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;

final class AdvancedPromotionFixture extends AbstractFixture implements FixtureInterface
{
public function __construct(
private PromotionRepositoryInterface $promotionRepository,
private EntityManagerInterface $entityManager,
) {
}

public function getName(): string
{
return 'monsieurbiz_advanced_promotion';
}

public function load(array $options): void
{
foreach ($options['promotion_advanced_configuration'] as $data) {
$promotion = $this->promotionRepository->findOneBy(['code' => $data['code']]);
if (null === $promotion || !$promotion instanceof AfterTaxAwareInterface) {
continue;
}
$promotion->setAfterTax($data['after_tax']);
$this->entityManager->persist($promotion);
}

$this->entityManager->flush();
}

protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void
{
/** @phpstan-ignore-next-line */
$optionsNode
->children()
->arrayNode('promotion_advanced_configuration')
->arrayPrototype()
->children()
->scalarNode('code')->cannotBeEmpty()->end()
->booleanNode('after_tax')->defaultFalse()->end()
->end()
->end()
->end()
;
}
}
40 changes: 40 additions & 0 deletions src/Form/Extension/PromotionTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?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 MonsieurBiz\SyliusAdvancedPromotionPlugin\Form\Extension;

use Sylius\Bundle\PromotionBundle\Form\Type\PromotionType;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\FormBuilderInterface;

final class PromotionTypeExtension extends AbstractTypeExtension
{
/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('afterTax', CheckboxType::class, [
'required' => false,
'label' => 'monsieurbiz_sylius_advanced_promotion.promotion.applied_after_tax',
])
;
}

public static function getExtendedTypes(): iterable
{
return [
PromotionType::class,
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?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 MonsieurBiz\SyliusAdvancedPromotionPlugin\Promotion\Checker\Eligibility;

use MonsieurBiz\SyliusAdvancedPromotionPlugin\Context\PromotionContextInterface;
use MonsieurBiz\SyliusAdvancedPromotionPlugin\Entity\AfterTaxAwareInterface;
use Sylius\Component\Promotion\Checker\Eligibility\PromotionEligibilityCheckerInterface;
use Sylius\Component\Promotion\Model\PromotionInterface;
use Sylius\Component\Promotion\Model\PromotionSubjectInterface;
use Webmozart\Assert\Assert;

final class PromotionTaxContextEligibilityChecker implements PromotionEligibilityCheckerInterface
{
public function __construct(
private PromotionContextInterface $promotionContext
) {
}

/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function isEligible(
PromotionSubjectInterface $promotionSubject,
PromotionInterface $promotion
): bool {
/** @var AfterTaxAwareInterface $promotion */
Assert::isInstanceOf($promotion, AfterTaxAwareInterface::class);

return $this->promotionContext->isAfterTax() === $promotion->isAfterTax();
}
}
Loading
Loading