Skip to content

Commit fd78592

Browse files
authored
Refactor Doctrine des catégories de la tréso (#2109)
1 parent 96abf19 commit fd78592

File tree

11 files changed

+78
-160
lines changed

11 files changed

+78
-160
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AppBundle\Accounting\Entity;
6+
7+
use Doctrine\ORM\Mapping as ORM;
8+
9+
#[ORM\Entity]
10+
#[ORM\Table(name: 'compta_categorie')]
11+
class Category
12+
{
13+
#[ORM\Id]
14+
#[ORM\GeneratedValue]
15+
#[ORM\Column]
16+
public ?int $id = null;
17+
18+
#[ORM\Column(name: 'categorie', length: 255, nullable: false)]
19+
public string $name;
20+
21+
#[ORM\Column(name: 'idevenement', nullable: true)]
22+
public ?int $eventId = null;
23+
24+
#[ORM\Column(type: 'datetime_immutable', nullable: true)]
25+
public ?\DateTimeImmutable $hideInAccountingJournalAt = null;
26+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AppBundle\Accounting\Entity\Repository;
6+
7+
use AppBundle\Accounting\Entity\Category;
8+
use AppBundle\Doctrine\EntityRepository;
9+
use Doctrine\Persistence\ManagerRegistry;
10+
11+
/**
12+
* @extends EntityRepository<Category>
13+
*/
14+
final class CategoryRepository extends EntityRepository
15+
{
16+
public function __construct(ManagerRegistry $registry)
17+
{
18+
parent::__construct($registry, Category::class);
19+
}
20+
21+
/**
22+
* @return array<Category>
23+
*/
24+
public function getAllSortedByName(): array
25+
{
26+
return $this->createQueryBuilder('c')
27+
->orderBy('c.name', 'asc')
28+
->getQuery()
29+
->execute();
30+
}
31+
}

sources/AppBundle/Accounting/Entity/Rule.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ class Rule
2727
#[ORM\Column(nullable: true)]
2828
public ?string $vat = null;
2929

30-
#[ORM\Column(nullable: true)]
31-
public ?int $categoryId = null;
30+
#[ORM\OneToOne()]
31+
#[ORM\JoinColumn(nullable: true)]
32+
public ?Category $category = null;
3233

3334
#[ORM\Column(nullable: true)]
3435
public ?int $eventId = null;

sources/AppBundle/Accounting/Form/RuleType.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
namespace AppBundle\Accounting\Form;
66

7-
use AppBundle\Accounting\Model\Repository\CategoryRepository;
7+
use AppBundle\Accounting\Entity\Category;
88
use AppBundle\Accounting\Model\Repository\EventRepository;
99
use AppBundle\Model\ComptaModeReglement;
10+
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
1011
use Symfony\Component\Form\AbstractType;
1112
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
1213
use Symfony\Component\Form\Extension\Core\Type\TextType;
@@ -16,18 +17,11 @@
1617
class RuleType extends AbstractType
1718
{
1819
public function __construct(
19-
private readonly CategoryRepository $categoryRepository,
2020
private readonly EventRepository $eventRepository,
2121
) {}
2222

2323
public function buildForm(FormBuilderInterface $builder, array $options): void
2424
{
25-
$categories = [];
26-
$categories[''] = null;
27-
foreach ($this->categoryRepository->getAllSortedByName() as $category) {
28-
$categories[$category->getName()] = $category->getId();
29-
}
30-
3125
$events = [];
3226
$events[''] = null;
3327
foreach ($this->eventRepository->getAllSortedByName() as $event) {
@@ -68,9 +62,10 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
6862
'placeholder' => false,
6963
'choices' => ['N.C.' => '', '0%' => '0', '5.5%' => '5_5', '10%' => '10', '20%' => '20'],
7064
'required' => false,
71-
])->add('categoryId', ChoiceType::class, [
65+
])->add('category', EntityType::class, [
7266
'label' => 'Catégorie',
73-
'choices' => $categories,
67+
'class' => Category::class,
68+
'choice_label' => 'name',
7469
'required' => false,
7570
])->add('eventId', ChoiceType::class, [
7671
'label' => 'Évènement',

sources/AppBundle/Accounting/Model/Category.php

Lines changed: 0 additions & 70 deletions
This file was deleted.

sources/AppBundle/Accounting/Model/Repository/CategoryRepository.php

Lines changed: 0 additions & 65 deletions
This file was deleted.

sources/AppBundle/Controller/Admin/Accounting/Configuration/AddCategoryAction.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
namespace AppBundle\Controller\Admin\Accounting\Configuration;
66

7+
use AppBundle\Accounting\Entity\Category;
8+
use AppBundle\Accounting\Entity\Repository\CategoryRepository;
79
use AppBundle\Accounting\Form\CategoryType;
8-
use AppBundle\Accounting\Model\Category;
9-
use AppBundle\Accounting\Model\Repository\CategoryRepository;
1010
use AppBundle\AuditLog\Audit;
1111
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1212
use Symfony\Component\HttpFoundation\Request;
@@ -26,7 +26,7 @@ public function __invoke(Request $request): Response
2626
$form->handleRequest($request);
2727
if ($form->isSubmitted() && $form->isValid()) {
2828
$this->categoryRepository->save($category);
29-
$this->audit->log('Ajout de la catégorie ' . $category->getName());
29+
$this->audit->log('Ajout de la catégorie ' . $category->name);
3030
$this->addFlash('notice', 'La catégorie a été ajoutée');
3131
return $this->redirectToRoute('admin_accounting_categories_list');
3232
}

sources/AppBundle/Controller/Admin/Accounting/Configuration/EditCategoryAction.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
namespace AppBundle\Controller\Admin\Accounting\Configuration;
66

7+
use AppBundle\Accounting\Entity\Repository\CategoryRepository;
78
use AppBundle\Accounting\Form\CategoryType;
8-
use AppBundle\Accounting\Model\Repository\CategoryRepository;
99
use AppBundle\AuditLog\Audit;
1010
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1111
use Symfony\Component\HttpFoundation\Request;
@@ -20,12 +20,12 @@ public function __construct(
2020

2121
public function __invoke(int $id,Request $request): Response
2222
{
23-
$category = $this->categoryRepository->get($id);
23+
$category = $this->categoryRepository->find($id);
2424
$form = $this->createForm(CategoryType::class, $category);
2525
$form->handleRequest($request);
2626
if ($form->isSubmitted() && $form->isValid()) {
2727
$this->categoryRepository->save($category);
28-
$this->audit->log('Modification de la catégorie ' . $category->getName());
28+
$this->audit->log('Modification de la catégorie ' . $category->name);
2929
$this->addFlash('notice', 'La catégorie a été modifiée');
3030
return $this->redirectToRoute('admin_accounting_categories_list');
3131
}

sources/AppBundle/Controller/Admin/Accounting/Configuration/ListCategoryAction.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44

55
namespace AppBundle\Controller\Admin\Accounting\Configuration;
66

7-
use AppBundle\Accounting\Model\Repository\CategoryRepository;
7+
use AppBundle\Accounting\Entity\Repository\CategoryRepository;
88
use Symfony\Component\HttpFoundation\Request;
99
use Symfony\Component\HttpFoundation\Response;
1010
use Twig\Environment;
1111

12-
class ListCategoryAction
12+
final readonly class ListCategoryAction
1313
{
1414
public function __construct(
15-
private readonly CategoryRepository $categoryRepository,
16-
private readonly Environment $twig,
15+
private CategoryRepository $categoryRepository,
16+
private Environment $twig,
1717
) {}
1818

1919
public function __invoke(Request $request): Response

templates/admin/accounting/configuration/rule_form.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
{{ form_row(form.isCredit) }}
1313
{{ form_row(form.paymentTypeId) }}
1414
{{ form_row(form.vat) }}
15-
{{ form_row(form.categoryId) }}
15+
{{ form_row(form.category) }}
1616
{{ form_row(form.eventId) }}
1717
{{ form_row(form.attachmentRequired) }}
1818
</div>

0 commit comments

Comments
 (0)