From afe157e1e0836c5cdd6254579e71997c29d09881 Mon Sep 17 00:00:00 2001 From: Mateusz Zalewski Date: Wed, 8 May 2024 13:21:41 +0200 Subject: [PATCH] Gift cards module --- config/packages/_sylius.yaml | 1 + config/packages/doctrine.yaml | 6 ++ config/packages/gift_card.yaml | 20 +++++++ config/services.yaml | 2 +- src/GiftCard/Entity/GiftCard.php | 60 ++++++++++++++++++++ src/GiftCard/Fixture/GiftCardFixture.php | 49 ++++++++++++++++ src/GiftCard/Form/Type/GiftCardAdminType.php | 23 ++++++++ src/GiftCard/Grid/AdminGiftCardGrid.php | 45 +++++++++++++++ src/GiftCard/Menu/AdminMenuListener.php | 22 +++++++ src/Migrations/Version20240508104628.php | 31 ++++++++++ templates/Admin/GiftCard/_amount.html.twig | 1 + translations/messages.en.yaml | 1 + 12 files changed, 260 insertions(+), 1 deletion(-) create mode 100644 config/packages/gift_card.yaml create mode 100644 src/GiftCard/Entity/GiftCard.php create mode 100644 src/GiftCard/Fixture/GiftCardFixture.php create mode 100644 src/GiftCard/Form/Type/GiftCardAdminType.php create mode 100644 src/GiftCard/Grid/AdminGiftCardGrid.php create mode 100644 src/GiftCard/Menu/AdminMenuListener.php create mode 100644 src/Migrations/Version20240508104628.php create mode 100644 templates/Admin/GiftCard/_amount.html.twig diff --git a/config/packages/_sylius.yaml b/config/packages/_sylius.yaml index 602401d..c8dffb8 100644 --- a/config/packages/_sylius.yaml +++ b/config/packages/_sylius.yaml @@ -264,4 +264,5 @@ sylius_theme: sylius_resource: mapping: paths: + - '%kernel.project_dir%/src/GiftCard/Entity/' - '%kernel.project_dir%/src/Packaging/Entity/' diff --git a/config/packages/doctrine.yaml b/config/packages/doctrine.yaml index 9a883e9..540dd76 100644 --- a/config/packages/doctrine.yaml +++ b/config/packages/doctrine.yaml @@ -21,6 +21,12 @@ doctrine: dir: '%kernel.project_dir%/src/Entity' prefix: 'App\Entity' alias: App + GiftCard: + is_bundle: false + type: attribute + dir: '%kernel.project_dir%/src/GiftCard/Entity' + prefix: 'App\GiftCard\Entity' + alias: GiftCard Packaging: is_bundle: false type: attribute diff --git a/config/packages/gift_card.yaml b/config/packages/gift_card.yaml new file mode 100644 index 0000000..a3cd104 --- /dev/null +++ b/config/packages/gift_card.yaml @@ -0,0 +1,20 @@ +sylius_fixtures: + suites: + gift_card: + listeners: + logger: ~ + fixtures: + gift_card: + name: gift_card + options: + amount: 5 + +services: + App\GiftCard\: + resource: '../../src/GiftCard/*' + autowire: true + autoconfigure: true + + App\GiftCard\Menu\AdminMenuListener: + tags: + - { name: kernel.event_listener, event: sylius.menu.admin.main, method: addGiftCardMenu } diff --git a/config/services.yaml b/config/services.yaml index f39631c..aff5b92 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -21,7 +21,7 @@ services: # this creates a service per class whose id is the fully-qualified class name App\: resource: '../src/*' - exclude: '../src/{Entity,Packaging,Migrations,Tests,Kernel.php}' + exclude: '../src/{Entity,GiftCard,Packaging,Migrations,Tests,Kernel.php}' # Controllers are imported separately to make sure services can be injected # as action arguments even if you don't extend any base controller class diff --git a/src/GiftCard/Entity/GiftCard.php b/src/GiftCard/Entity/GiftCard.php new file mode 100644 index 0000000..3dd8276 --- /dev/null +++ b/src/GiftCard/Entity/GiftCard.php @@ -0,0 +1,60 @@ +id; + } + + public function getCode(): ?string + { + return $this->code; + } + + public function setCode(?string $code): void + { + $this->code = $code; + } + + public function getAmount(): ?int + { + return $this->amount; + } + + public function setAmount(?int $amount): void + { + $this->amount = $amount; + } +} diff --git a/src/GiftCard/Fixture/GiftCardFixture.php b/src/GiftCard/Fixture/GiftCardFixture.php new file mode 100644 index 0000000..4328ac4 --- /dev/null +++ b/src/GiftCard/Fixture/GiftCardFixture.php @@ -0,0 +1,49 @@ +faker = Factory::create(); + } + + public function load(array $options): void + { + for ($i = 0; $i < $options['amount']; ++$i) { + $giftCard = new GiftCard(); + $giftCard->setCode($this->faker->uuid); + $giftCard->setAmount($this->faker->randomElement([100, 200, 500, 1000, 10000])); + + $this->entityManager->persist($giftCard); + } + + $this->entityManager->flush(); + } + + public function getName(): string + { + return 'gift_card'; + } + + protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void + { + $optionsNode + ->children() + ->integerNode('amount')->isRequired()->min(0)->end() + ->end() + ; + } +} diff --git a/src/GiftCard/Form/Type/GiftCardAdminType.php b/src/GiftCard/Form/Type/GiftCardAdminType.php new file mode 100644 index 0000000..288b58a --- /dev/null +++ b/src/GiftCard/Form/Type/GiftCardAdminType.php @@ -0,0 +1,23 @@ +add('code', TextType::class) + ->add('amount', MoneyType::class, [ + 'currency' => 'USD', + ]) + ; + } +} diff --git a/src/GiftCard/Grid/AdminGiftCardGrid.php b/src/GiftCard/Grid/AdminGiftCardGrid.php new file mode 100644 index 0000000..9f44916 --- /dev/null +++ b/src/GiftCard/Grid/AdminGiftCardGrid.php @@ -0,0 +1,45 @@ +addField(StringField::create('code')->setLabel('Code')) + ->addField(TwigField::create('amount', 'Admin/GiftCard/_amount.html.twig')->setLabel('Amount')) + ->addActionGroup(MainActionGroup::create( + CreateAction::create(), + )) + ->addActionGroup(ItemActionGroup::create() + ->addAction(UpdateAction::create()) + ->addAction(DeleteAction::create()) + ); + ; + } + + public function getResourceClass(): string + { + return GiftCard::class; + } +} diff --git a/src/GiftCard/Menu/AdminMenuListener.php b/src/GiftCard/Menu/AdminMenuListener.php new file mode 100644 index 0000000..324b302 --- /dev/null +++ b/src/GiftCard/Menu/AdminMenuListener.php @@ -0,0 +1,22 @@ +getMenu(); + $salesSubmenu = $menu->getChild('sales'); + + $salesSubmenu + ->addChild('gift_cards', ['route' => 'app_admin_gift_card_index']) + ->setLabel('app.ui.gift_cards') + ->setLabelAttribute('icon', 'gift') + ; + } +} diff --git a/src/Migrations/Version20240508104628.php b/src/Migrations/Version20240508104628.php new file mode 100644 index 0000000..b089d73 --- /dev/null +++ b/src/Migrations/Version20240508104628.php @@ -0,0 +1,31 @@ +addSql('CREATE TABLE app_gift_card (id INT AUTO_INCREMENT NOT NULL, code VARCHAR(255) NOT NULL, amount INT NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('DROP TABLE app_gift_card'); + } +} diff --git a/templates/Admin/GiftCard/_amount.html.twig b/templates/Admin/GiftCard/_amount.html.twig new file mode 100644 index 0000000..ef50394 --- /dev/null +++ b/templates/Admin/GiftCard/_amount.html.twig @@ -0,0 +1 @@ +{{ data|sylius_format_money('USD', app.locale) }} diff --git a/translations/messages.en.yaml b/translations/messages.en.yaml index 42deea9..333dfd5 100644 --- a/translations/messages.en.yaml +++ b/translations/messages.en.yaml @@ -1,3 +1,4 @@ app: ui: + gift_cards: 'Gift Cards' packagings: 'Packagings'