From 2d659751b177110b1f1a87a5dd3ad522b9744cca Mon Sep 17 00:00:00 2001 From: Nathan Boiron Date: Mon, 12 Jan 2026 13:49:01 +0100 Subject: [PATCH] =?UTF-8?q?Refactor=20Doctrine=20des=20cat=C3=A9gories=20d?= =?UTF-8?q?e=20la=20tr=C3=A9so?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AppBundle/Accounting/Entity/Category.php | 26 +++++++ .../Entity/Repository/CategoryRepository.php | 31 ++++++++ sources/AppBundle/Accounting/Entity/Rule.php | 5 +- .../AppBundle/Accounting/Form/RuleType.php | 15 ++-- .../AppBundle/Accounting/Model/Category.php | 70 ------------------- .../Model/Repository/CategoryRepository.php | 65 ----------------- .../Configuration/AddCategoryAction.php | 6 +- .../Configuration/EditCategoryAction.php | 6 +- .../Configuration/ListCategoryAction.php | 8 +-- .../configuration/rule_form.html.twig | 2 +- .../Admin/Tresorerie/Configuration.feature | 4 +- 11 files changed, 78 insertions(+), 160 deletions(-) create mode 100644 sources/AppBundle/Accounting/Entity/Category.php create mode 100644 sources/AppBundle/Accounting/Entity/Repository/CategoryRepository.php delete mode 100644 sources/AppBundle/Accounting/Model/Category.php delete mode 100644 sources/AppBundle/Accounting/Model/Repository/CategoryRepository.php diff --git a/sources/AppBundle/Accounting/Entity/Category.php b/sources/AppBundle/Accounting/Entity/Category.php new file mode 100644 index 000000000..98072f573 --- /dev/null +++ b/sources/AppBundle/Accounting/Entity/Category.php @@ -0,0 +1,26 @@ + + */ +final class CategoryRepository extends EntityRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, Category::class); + } + + /** + * @return array + */ + public function getAllSortedByName(): array + { + return $this->createQueryBuilder('c') + ->orderBy('c.name', 'asc') + ->getQuery() + ->execute(); + } +} diff --git a/sources/AppBundle/Accounting/Entity/Rule.php b/sources/AppBundle/Accounting/Entity/Rule.php index 97ee71cb5..1e412bcd0 100644 --- a/sources/AppBundle/Accounting/Entity/Rule.php +++ b/sources/AppBundle/Accounting/Entity/Rule.php @@ -27,8 +27,9 @@ class Rule #[ORM\Column(nullable: true)] public ?string $vat = null; - #[ORM\Column(nullable: true)] - public ?int $categoryId = null; + #[ORM\OneToOne()] + #[ORM\JoinColumn(nullable: true)] + public ?Category $category = null; #[ORM\Column(nullable: true)] public ?int $eventId = null; diff --git a/sources/AppBundle/Accounting/Form/RuleType.php b/sources/AppBundle/Accounting/Form/RuleType.php index 900b85e29..6b23eb427 100644 --- a/sources/AppBundle/Accounting/Form/RuleType.php +++ b/sources/AppBundle/Accounting/Form/RuleType.php @@ -4,9 +4,10 @@ namespace AppBundle\Accounting\Form; -use AppBundle\Accounting\Model\Repository\CategoryRepository; +use AppBundle\Accounting\Entity\Category; use AppBundle\Accounting\Model\Repository\EventRepository; use AppBundle\Model\ComptaModeReglement; +use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\TextType; @@ -16,18 +17,11 @@ class RuleType extends AbstractType { public function __construct( - private readonly CategoryRepository $categoryRepository, private readonly EventRepository $eventRepository, ) {} public function buildForm(FormBuilderInterface $builder, array $options): void { - $categories = []; - $categories[''] = null; - foreach ($this->categoryRepository->getAllSortedByName() as $category) { - $categories[$category->getName()] = $category->getId(); - } - $events = []; $events[''] = null; foreach ($this->eventRepository->getAllSortedByName() as $event) { @@ -68,9 +62,10 @@ public function buildForm(FormBuilderInterface $builder, array $options): void 'placeholder' => false, 'choices' => ['N.C.' => '', '0%' => '0', '5.5%' => '5_5', '10%' => '10', '20%' => '20'], 'required' => false, - ])->add('categoryId', ChoiceType::class, [ + ])->add('category', EntityType::class, [ 'label' => 'Catégorie', - 'choices' => $categories, + 'class' => Category::class, + 'choice_label' => 'name', 'required' => false, ])->add('eventId', ChoiceType::class, [ 'label' => 'Évènement', diff --git a/sources/AppBundle/Accounting/Model/Category.php b/sources/AppBundle/Accounting/Model/Category.php deleted file mode 100644 index 5ce9747e3..000000000 --- a/sources/AppBundle/Accounting/Model/Category.php +++ /dev/null @@ -1,70 +0,0 @@ -id; - } - - public function setId(int $id): self - { - $this->propertyChanged('id', $this->id, $id); - $this->id = $id; - return $this; - } - - public function getEventId(): ?int - { - return $this->eventId; - } - - public function setEventId(?int $eventId): void - { - $this->propertyChanged('eventId', $this->eventId, $eventId); - $this->eventId = $eventId; - } - - - - public function getName(): ?string - { - return $this->name; - } - - public function setName(?string $name): self - { - $this->propertyChanged('name', $this->name, $name); - $this->name = $name; - - return $this; - } - - public function getHideInAccountingJournalAt(): ?\DateTime - { - return $this->hideInAccountingJournalAt; - } - - public function setHideInAccountingJournalAt(?\DateTime $hideInAccountingJournalAt): void - { - $this->propertyChanged('hideInAccountingJournalAt', $this->hideInAccountingJournalAt, $hideInAccountingJournalAt); - $this->hideInAccountingJournalAt = $hideInAccountingJournalAt; - } -} diff --git a/sources/AppBundle/Accounting/Model/Repository/CategoryRepository.php b/sources/AppBundle/Accounting/Model/Repository/CategoryRepository.php deleted file mode 100644 index d997f9aab..000000000 --- a/sources/AppBundle/Accounting/Model/Repository/CategoryRepository.php +++ /dev/null @@ -1,65 +0,0 @@ - - */ -class CategoryRepository extends Repository implements MetadataInitializer -{ - /** - * @return CollectionInterface - */ - public function getAllSortedByName(): CollectionInterface - { - $query = $this->getQuery('SELECT * FROM compta_categorie ORDER BY categorie asc'); - return $query->query($this->getCollection(new HydratorSingleObject())); - } - - public static function initMetadata(SerializerFactoryInterface $serializerFactory, array $options = []) - { - $metadata = new Metadata($serializerFactory); - - $metadata->setEntity(Category::class); - $metadata->setConnectionName('main'); - $metadata->setDatabase($options['database']); - $metadata->setTable('compta_categorie'); - - $metadata - ->addField([ - 'columnName' => 'id', - 'fieldName' => 'id', - 'primary' => true, - 'autoincrement' => true, - 'type' => 'int', - ]) - ->addField([ - 'columnName' => 'idevenement', - 'fieldName' => 'eventId', - 'type' => 'int', - ]) - ->addField([ - 'columnName' => 'categorie', - 'fieldName' => 'name', - 'type' => 'string', - ]) - ->addField([ - 'columnName' => 'hide_in_accounting_journal_at', - 'fieldName' => 'hideInAccountingJournalAt', - 'type' => 'datetime', - ]) - ; - - return $metadata; - } -} diff --git a/sources/AppBundle/Controller/Admin/Accounting/Configuration/AddCategoryAction.php b/sources/AppBundle/Controller/Admin/Accounting/Configuration/AddCategoryAction.php index 33a719d23..64077bc19 100644 --- a/sources/AppBundle/Controller/Admin/Accounting/Configuration/AddCategoryAction.php +++ b/sources/AppBundle/Controller/Admin/Accounting/Configuration/AddCategoryAction.php @@ -4,9 +4,9 @@ namespace AppBundle\Controller\Admin\Accounting\Configuration; +use AppBundle\Accounting\Entity\Category; +use AppBundle\Accounting\Entity\Repository\CategoryRepository; use AppBundle\Accounting\Form\CategoryType; -use AppBundle\Accounting\Model\Category; -use AppBundle\Accounting\Model\Repository\CategoryRepository; use AppBundle\AuditLog\Audit; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; @@ -26,7 +26,7 @@ public function __invoke(Request $request): Response $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $this->categoryRepository->save($category); - $this->audit->log('Ajout de la catégorie ' . $category->getName()); + $this->audit->log('Ajout de la catégorie ' . $category->name); $this->addFlash('notice', 'La catégorie a été ajoutée'); return $this->redirectToRoute('admin_accounting_categories_list'); } diff --git a/sources/AppBundle/Controller/Admin/Accounting/Configuration/EditCategoryAction.php b/sources/AppBundle/Controller/Admin/Accounting/Configuration/EditCategoryAction.php index 182cfb779..cdde749f6 100644 --- a/sources/AppBundle/Controller/Admin/Accounting/Configuration/EditCategoryAction.php +++ b/sources/AppBundle/Controller/Admin/Accounting/Configuration/EditCategoryAction.php @@ -4,8 +4,8 @@ namespace AppBundle\Controller\Admin\Accounting\Configuration; +use AppBundle\Accounting\Entity\Repository\CategoryRepository; use AppBundle\Accounting\Form\CategoryType; -use AppBundle\Accounting\Model\Repository\CategoryRepository; use AppBundle\AuditLog\Audit; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; @@ -20,12 +20,12 @@ public function __construct( public function __invoke(int $id,Request $request): Response { - $category = $this->categoryRepository->get($id); + $category = $this->categoryRepository->find($id); $form = $this->createForm(CategoryType::class, $category); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $this->categoryRepository->save($category); - $this->audit->log('Modification de la catégorie ' . $category->getName()); + $this->audit->log('Modification de la catégorie ' . $category->name); $this->addFlash('notice', 'La catégorie a été modifiée'); return $this->redirectToRoute('admin_accounting_categories_list'); } diff --git a/sources/AppBundle/Controller/Admin/Accounting/Configuration/ListCategoryAction.php b/sources/AppBundle/Controller/Admin/Accounting/Configuration/ListCategoryAction.php index 57d892a33..0dceef00a 100644 --- a/sources/AppBundle/Controller/Admin/Accounting/Configuration/ListCategoryAction.php +++ b/sources/AppBundle/Controller/Admin/Accounting/Configuration/ListCategoryAction.php @@ -4,16 +4,16 @@ namespace AppBundle\Controller\Admin\Accounting\Configuration; -use AppBundle\Accounting\Model\Repository\CategoryRepository; +use AppBundle\Accounting\Entity\Repository\CategoryRepository; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Twig\Environment; -class ListCategoryAction +final readonly class ListCategoryAction { public function __construct( - private readonly CategoryRepository $categoryRepository, - private readonly Environment $twig, + private CategoryRepository $categoryRepository, + private Environment $twig, ) {} public function __invoke(Request $request): Response diff --git a/templates/admin/accounting/configuration/rule_form.html.twig b/templates/admin/accounting/configuration/rule_form.html.twig index e64b0e852..74fc40b8c 100644 --- a/templates/admin/accounting/configuration/rule_form.html.twig +++ b/templates/admin/accounting/configuration/rule_form.html.twig @@ -12,7 +12,7 @@ {{ form_row(form.isCredit) }} {{ form_row(form.paymentTypeId) }} {{ form_row(form.vat) }} - {{ form_row(form.categoryId) }} + {{ form_row(form.category) }} {{ form_row(form.eventId) }} {{ form_row(form.attachmentRequired) }} diff --git a/tests/behat/features/Admin/Tresorerie/Configuration.feature b/tests/behat/features/Admin/Tresorerie/Configuration.feature index 28b5f267e..0cc5e723d 100644 --- a/tests/behat/features/Admin/Tresorerie/Configuration.feature +++ b/tests/behat/features/Admin/Tresorerie/Configuration.feature @@ -104,7 +104,7 @@ Feature: Administration - Trésorerie - Configuration # TVA à 5.5% When I fill in "rule[vat]" with "5_5" # À determiner - When I fill in "rule[categoryId]" with "26" + When I fill in "rule[category]" with "26" # Association AFUP When I fill in "rule[eventId]" with "27" # Justification obligatoire @@ -119,6 +119,6 @@ Feature: Administration - Trésorerie - Configuration And The "rule[isCredit]" field should have the following selected value "1" And The "rule[paymentTypeId]" field should have the following selected value "2" And The "rule[vat]" field should have the following selected value "5_5" - And The "rule[categoryId]" field should have the following selected value "26" + And The "rule[category]" field should have the following selected value "26" And The "rule[eventId]" field should have the following selected value "27" And The "rule[attachmentRequired]" field should have the following selected value "1"