From 5e81de0ad6240d06e06e9cc717eda754aaf85a12 Mon Sep 17 00:00:00 2001 From: vrobic Date: Thu, 19 Nov 2026 09:50:20 +0100 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=92=84=20les=20champs=20de=20formulai?= =?UTF-8?q?re=20perdent=20leur=20police=20=C3=A0=20chasse=20fixe,=20pour?= =?UTF-8?q?=20reprendre=20celle=20des=20champs=20de=20mouvements?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/css/comptes.css | 1 - 1 file changed, 1 deletion(-) diff --git a/public/css/comptes.css b/public/css/comptes.css index 677b29a..a68b1b5 100644 --- a/public/css/comptes.css +++ b/public/css/comptes.css @@ -269,7 +269,6 @@ form { form .form-control { border-radius: 2px; padding: 6px; - font-family: "ubuntu_monoregular"; } form .input-group .form-control { min-height: 37px; From 73923a487a94758cd469011caddd6fa4784288b2 Mon Sep 17 00:00:00 2001 From: vrobic Date: Thu, 19 Nov 2026 09:50:20 +0100 Subject: [PATCH 2/2] =?UTF-8?q?=E2=9C=A8=20cr=C3=A9ation,=20=C3=A9dition?= =?UTF-8?q?=20et=20suppression=20des=20comptes=20depuis=20l'interface?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 +- src/Domain/Compte/Compte.php | 19 ++ src/Domain/Compte/CompteCollection.php | 7 - .../Compte/CompteRepositoryInterface.php | 4 + .../MouvementRepositoryInterface.php | 8 + .../Controller/CompteController.php | 179 +++++++++++++++++- .../Controller/MouvementController.php | 13 ++ .../Repository/CompteRepository.php | 47 +++++ .../Repository/MouvementRepository.php | 52 +++++ templates/Compte/edit.html.twig | 72 +++++++ templates/Compte/index.html.twig | 15 +- templates/Compte/show.html.twig | 12 ++ 12 files changed, 413 insertions(+), 20 deletions(-) create mode 100644 templates/Compte/edit.html.twig diff --git a/README.md b/README.md index e49c727..be743a3 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,6 @@ L'infrastructure repose désormais sur [Symfony Docker](https://github.com/dungl * simplification de la configuration d'import * recherche de mouvements * recatégorisation en masse -* création, modification et suppression de comptes ## 📦️ Installation @@ -40,11 +39,11 @@ make down ### Création des comptes -Puisqu'il n'existe pas encore d'interface pour administrer les comptes, il faut les créer directement en base de données. +Sur la page d'accueil, cliquer sur « Afficher plus », puis sur « Nouveau compte ». La page de chaque compte permet ensuite de l'éditer, ou de le supprimer tant qu'il n'a aucun mouvement. ### Moteur d'import -En attendant que sa configuration soit simplifiée, le moteur d'import requiert un rapide paramétrage dans `config/comptes.yaml`. Modifier le tableau `handlers` pour ajuster les identifiants de comptes bancaires. +En attendant que sa configuration soit simplifiée, le moteur d'import requiert un rapide paramétrage dans `config/comptes.yaml`. Modifier le tableau `handlers` pour ajuster les identifiants de comptes bancaires : celui d'un compte figure dans l'URL de sa page. ### Catégories diff --git a/src/Domain/Compte/Compte.php b/src/Domain/Compte/Compte.php index 2fcead5..69846a9 100644 --- a/src/Domain/Compte/Compte.php +++ b/src/Domain/Compte/Compte.php @@ -4,6 +4,8 @@ namespace App\Domain\Compte; +use App\Domain\Temps\Periode; + /** * Compte bancaire. */ @@ -30,6 +32,23 @@ public function __construct( } } + /** + * Le compte est ouvert de sa date d'ouverture à sa date de fermeture, bornes comprises. + */ + public function estOuvertLe(\DateTimeImmutable $date): bool + { + return $date >= $this->dateOuverture + && (!$this->dateFermeture instanceof \DateTimeImmutable || $date <= $this->dateFermeture); + } + + /** + * Le compte est ouvert pendant toute la période : à son début comme à sa fin. + */ + public function estOuvertPendant(Periode $période): bool + { + return $this->estOuvertLe($période->début) && $this->estOuvertLe($période->fin); + } + public function __toString(): string { return $this->nom; diff --git a/src/Domain/Compte/CompteCollection.php b/src/Domain/Compte/CompteCollection.php index fc89238..2d1d95f 100644 --- a/src/Domain/Compte/CompteCollection.php +++ b/src/Domain/Compte/CompteCollection.php @@ -29,13 +29,6 @@ public function ouverts(): self ); } - public function fermés(): self - { - return $this->filter( - static fn (Compte $compte): bool => $compte->dateFermeture instanceof \DateTimeInterface - ); - } - public function solde(): Solde { return $this->reduce( diff --git a/src/Domain/Compte/CompteRepositoryInterface.php b/src/Domain/Compte/CompteRepositoryInterface.php index 0bba6f2..423774b 100644 --- a/src/Domain/Compte/CompteRepositoryInterface.php +++ b/src/Domain/Compte/CompteRepositoryInterface.php @@ -19,4 +19,8 @@ public function getSoldeÀDate( CompteId $compteId, \DateTimeImmutable $date, ): Solde; + + public function save(Compte ...$comptes): void; + + public function delete(CompteId ...$ids): void; } diff --git a/src/Domain/Mouvement/MouvementRepositoryInterface.php b/src/Domain/Mouvement/MouvementRepositoryInterface.php index abff0a5..e2ec229 100644 --- a/src/Domain/Mouvement/MouvementRepositoryInterface.php +++ b/src/Domain/Mouvement/MouvementRepositoryInterface.php @@ -7,6 +7,7 @@ use App\Domain\Categorie\CategorieIdCollection; use App\Domain\Compte\CompteId; use App\Domain\DataStructure\Maybe; +use App\Domain\Temps\Periode; interface MouvementRepositoryInterface { @@ -39,6 +40,13 @@ public function findFirstOne(?CompteId $compteId = null): ?Mouvement; */ public function findLatestOne(): ?Mouvement; + public function aDesMouvements(CompteId $compteId): bool; + + /** + * Période couverte par les mouvements du compte, de la date du premier à celle du dernier, ou null s'il n'en a aucun. + */ + public function getPériodeDesMouvements(CompteId $compteId): ?Periode; + /** * Parmi les mouvements donnés, renvoie ceux dont un mouvement de même compte, date et montant existe déjà. */ diff --git a/src/Infrastructure/Controller/CompteController.php b/src/Infrastructure/Controller/CompteController.php index a72f9f2..df6400b 100644 --- a/src/Infrastructure/Controller/CompteController.php +++ b/src/Infrastructure/Controller/CompteController.php @@ -7,9 +7,12 @@ use App\Domain\BalanceMensuelle; use App\Domain\Categorie\CategorieRepositoryInterface; use App\Domain\Compte\Compte; +use App\Domain\Compte\CompteId; use App\Domain\Compte\CompteRepositoryInterface; +use App\Domain\Compte\Plafond; use App\Domain\Compte\Solde; use App\Domain\DataStructure\Maybe; +use App\Domain\Id\IdGeneratorInterface; use App\Domain\Mouvement\Montant; use App\Domain\Mouvement\Mouvement; use App\Domain\Mouvement\MouvementId; @@ -19,8 +22,11 @@ use App\Domain\Temps\Periode; use App\Infrastructure\ValueResolver\PeriodeParDefautAttribute; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\Routing\Attribute\Route; final class CompteController extends AbstractController @@ -29,6 +35,7 @@ public function __construct( private readonly CompteRepositoryInterface $compteRepository, private readonly MouvementRepositoryInterface $mouvementRepository, private readonly CategorieRepositoryInterface $categorieRepository, + private readonly IdGeneratorInterface $idGenerator, ) { } @@ -36,8 +43,7 @@ public function __construct( public function liste(Request $request): Response { $comptes = $this->compteRepository->findAll(); - $aDesComptesFermés = !$comptes->fermés()->isEmpty(); - $avecFermés = filter_var($request->query->get('avec_fermes', false), FILTER_VALIDATE_BOOL); + $avecDétails = filter_var($request->query->get('avec_details', false), FILTER_VALIDATE_BOOL); $mouvements = $this->mouvementRepository->findAll(); @@ -86,9 +92,8 @@ public function liste(Request $request): Response return $this->render( 'Compte/index.html.twig', [ - 'comptes' => !$avecFermés ? $comptes->ouverts() : $comptes, - 'a_des_comptes_fermes' => $aDesComptesFermés, - 'avec_fermes' => $avecFermés, + 'comptes' => $avecDétails ? $comptes : $comptes->ouverts(), + 'avec_details' => $avecDétails, 'mouvements' => $mouvements->toArray(), // pour pouvoir faire mouvements[key+1] en Twig 'first_mouvement' => $firstMouvement, 'last_mouvement' => $lastMouvement, @@ -150,7 +155,171 @@ public function détail( 'categories' => $categories->toAssociativeArray(), 'solde_start' => $soldeStart, 'balance' => $balance, + 'a_des_mouvements' => $this->mouvementRepository->aDesMouvements($compte->id), ] ); } + + #[Route('/comptes/new', name: 'comptes_new', methods: ['GET', 'POST'])] + public function création(Request $request): Response + { + if ($request->isMethod('POST')) { + if (!$this->isCsrfTokenValid('comptes_edit', $request->request->getString('_token'))) { + throw new AccessDeniedHttpException('Jeton CSRF invalide.'); + } + + $compte = $this->construireLeCompte($request, null); + $this->compteRepository->save($compte); + + return $this->redirectToRoute('comptes_compte', ['compteId' => (string) $compte->id]); + } + + return $this->render( + 'Compte/edit.html.twig', + [ + 'compte' => null, + ] + ); + } + + #[Route('/compte/{compteId}/edit', name: 'comptes_edit', methods: ['GET', 'POST'])] + public function édition(Request $request, Compte $compte): Response + { + if ($request->isMethod('POST')) { + if (!$this->isCsrfTokenValid('comptes_edit', $request->request->getString('_token'))) { + throw new AccessDeniedHttpException('Jeton CSRF invalide.'); + } + + $compte = $this->construireLeCompte($request, $compte); + $this->compteRepository->save($compte); + + return $this->redirectToRoute('comptes_compte', ['compteId' => (string) $compte->id]); + } + + return $this->render( + 'Compte/edit.html.twig', + [ + 'compte' => $compte, + ] + ); + } + + #[Route('/compte/{compteId}/delete', name: 'comptes_delete', methods: ['POST'])] + public function suppression(Request $request, Compte $compte): RedirectResponse + { + if (!$this->isCsrfTokenValid('comptes_delete', $request->request->getString('_token'))) { + throw new AccessDeniedHttpException('Jeton CSRF invalide.'); + } + + // Un compte qui a des mouvements se ferme, mais ne se supprime pas + if ($this->mouvementRepository->aDesMouvements($compte->id)) { + throw new BadRequestHttpException(sprintf('Le compte [%s] a des mouvements : il ne peut pas être supprimé.', $compte)); + } + + $this->compteRepository->delete($compte->id); + + return $this->redirectToRoute('comptes_comptes'); + } + + /** + * Construit, à partir des valeurs postées, un nouveau compte ou le compte édité. + */ + private function construireLeCompte(Request $request, ?Compte $compte): Compte + { + // Nom + $nom = trim($request->request->getString('nom')); + if ('' === $nom) { + throw new BadRequestHttpException('Le nom du compte est obligatoire.'); + } + + // Banque + $banque = trim($request->request->getString('banque')); + if ('' === $banque) { + throw new BadRequestHttpException('La banque du compte est obligatoire.'); + } + + // Numéro + $numero = trim($request->request->getString('numero')); + if ('' === $numero) { + throw new BadRequestHttpException('Le numéro du compte est obligatoire.'); + } + + // Plafond, en euros entiers comme en base + $plafondString = trim($request->request->getString('plafond')); + $plafond = null; + if ('' !== $plafondString) { + $plafondEntier = filter_var($plafondString, FILTER_VALIDATE_INT); + if (false === $plafondEntier) { + throw new BadRequestHttpException(sprintf('Plafond du compte invalide : [%s].', $plafondString)); + } + $plafond = new Plafond((float) $plafondEntier); + } + + // Solde initial + $soldeInitialString = str_replace(',', '.', trim($request->request->getString('solde_initial'))); + if (!is_numeric($soldeInitialString)) { + throw new BadRequestHttpException(sprintf('Solde initial du compte invalide : [%s].', $soldeInitialString)); + } + $soldeInitial = new Solde((float) $soldeInitialString); + + // Date d'ouverture + $dateOuvertureString = trim($request->request->getString('date_ouverture')); + $dateOuverture = \DateTimeImmutable::createFromFormat('d-m-Y H:i:s', "$dateOuvertureString 00:00:00"); + // createFromFormat décale les dates impossibles (31-02 devient 03-03) et lit 26 comme l'an 26 : la date relue doit être celle saisie + if (!$dateOuverture instanceof \DateTimeImmutable || $dateOuverture->format('d-m-Y') !== $dateOuvertureString) { + throw new BadRequestHttpException(sprintf("Date d'ouverture du compte invalide : [%s].", $dateOuvertureString)); + } + + // Date de fermeture + $dateFermetureString = trim($request->request->getString('date_fermeture')); + $dateFermeture = null; + if ('' !== $dateFermetureString) { + $dateFermeture = \DateTimeImmutable::createFromFormat('d-m-Y H:i:s', "$dateFermetureString 00:00:00"); + // createFromFormat décale les dates impossibles (31-02 devient 03-03) et lit 26 comme l'an 26 : la date relue doit être celle saisie + if (!$dateFermeture instanceof \DateTimeImmutable || $dateFermeture->format('d-m-Y') !== $dateFermetureString) { + throw new BadRequestHttpException(sprintf('Date de fermeture du compte invalide : [%s].', $dateFermetureString)); + } + } + + // Rang + $rangString = trim($request->request->getString('rang')); + $rang = null; + if ('' !== $rangString) { + $rang = filter_var($rangString, FILTER_VALIDATE_INT); + if (false === $rang) { + throw new BadRequestHttpException(sprintf('Rang du compte invalide : [%s].', $rangString)); + } + } + + // Les règles du domaine (plafond positif, fermeture après l'ouverture) sont des erreurs de saisie + try { + $compteSaisi = new Compte( + $compte->id ?? new CompteId((string) $this->idGenerator->générer()), + $nom, + $numero, + $banque, + $plafond, + $soldeInitial, + $compte->solde ?? $soldeInitial, // calculé en base, le solde n'est pas enregistré + $dateOuverture, + $dateFermeture, + $rang, + ); + } catch (\DomainException $exception) { + throw new BadRequestHttpException($exception->getMessage(), $exception); + } + + // Les dates d'un compte encadrent celles de ses mouvements + $périodeDesMouvements = $compte instanceof Compte ? $this->mouvementRepository->getPériodeDesMouvements($compte->id) : null; + if ($périodeDesMouvements instanceof Periode && !$compteSaisi->estOuvertPendant($périodeDesMouvements)) { + $message = sprintf( + 'Les dates du compte doivent encadrer celles de ses mouvements, du [%s] au [%s].', + $périodeDesMouvements->début->format('d-m-Y'), + $périodeDesMouvements->fin->format('d-m-Y'), + ); + throw new BadRequestHttpException($message); + } + + return $compteSaisi; + } } diff --git a/src/Infrastructure/Controller/MouvementController.php b/src/Infrastructure/Controller/MouvementController.php index 5dd717d..1ab362b 100644 --- a/src/Infrastructure/Controller/MouvementController.php +++ b/src/Infrastructure/Controller/MouvementController.php @@ -150,6 +150,19 @@ public function __invoke(Request $request): RedirectResponse ); } + // La date d'un mouvement est encadrée par celles de son compte + if (!$mouvement->compte->estOuvertLe($mouvement->date)) { + $message = sprintf( + 'La date du mouvement [%s] est en dehors des dates du compte [%s], ouvert %s.', + $mouvement->date->format('d-m-Y'), + $mouvement->compte, + $mouvement->compte->dateFermeture instanceof \DateTimeImmutable ? + sprintf('du [%s] au [%s]', $mouvement->compte->dateOuverture->format('d-m-Y'), $mouvement->compte->dateFermeture->format('d-m-Y')) : + sprintf('depuis le [%s]', $mouvement->compte->dateOuverture->format('d-m-Y')), + ); + throw new BadRequestHttpException($message); + } + $this->mouvementRepository->save($mouvement); break; diff --git a/src/Infrastructure/Repository/CompteRepository.php b/src/Infrastructure/Repository/CompteRepository.php index 116cac1..bc5c71e 100644 --- a/src/Infrastructure/Repository/CompteRepository.php +++ b/src/Infrastructure/Repository/CompteRepository.php @@ -10,12 +10,15 @@ use App\Domain\Compte\CompteRepositoryInterface; use App\Domain\Compte\Solde; use App\Infrastructure\Denormalizer\CompteDenormalizer; +use Doctrine\DBAL\ArrayParameterType; use Doctrine\DBAL\Connection; use Doctrine\DBAL\Query\QueryBuilder; use Doctrine\DBAL\Types\Types; final readonly class CompteRepository implements CompteRepositoryInterface { + use UpsertTrait; + public function __construct( private Connection $connection, private CompteDenormalizer $compteDenormalizer, @@ -69,6 +72,50 @@ public function getSoldeÀDate( ); } + public function save(Compte ...$comptes): void + { + foreach ($comptes as $compte) { + $data = [ + 'nom' => $compte->nom, + 'numero' => $compte->numero, + 'banque' => $compte->banque, + 'plafond' => $compte->plafond?->montant, + 'solde_initial' => $compte->soldeInitial->montant, + 'rang' => $compte->rang, + 'date_ouverture' => $compte->dateOuverture, + 'date_fermeture' => $compte->dateFermeture, + ]; + + $this->upsert( + $this->connection, + 'comptes', + array_merge( + ['id' => (string) $compte->id], + $data, + ), + $data, + [ + 'date_ouverture' => Types::DATE_IMMUTABLE, + 'date_fermeture' => Types::DATE_IMMUTABLE, + ] + ); + } + } + + public function delete(CompteId ...$ids): void + { + $this->connection->executeStatement( + 'DELETE FROM comptes WHERE id IN (:ids);', + [ + 'ids' => array_map( + static fn (CompteId $id): string => (string) $id, + $ids + ), + ], + ['ids' => ArrayParameterType::STRING] + ); + } + private function getBaseQueryBuilder(): QueryBuilder { return $this->connection->createQueryBuilder() diff --git a/src/Infrastructure/Repository/MouvementRepository.php b/src/Infrastructure/Repository/MouvementRepository.php index b2b897e..950f0b5 100644 --- a/src/Infrastructure/Repository/MouvementRepository.php +++ b/src/Infrastructure/Repository/MouvementRepository.php @@ -13,6 +13,7 @@ use App\Domain\Mouvement\MouvementCollection; use App\Domain\Mouvement\MouvementId; use App\Domain\Mouvement\MouvementRepositoryInterface; +use App\Domain\Temps\Periode; use App\Infrastructure\Denormalizer\MouvementDenormalizer; use Doctrine\DBAL\ArrayParameterType; use Doctrine\DBAL\Connection; @@ -342,6 +343,57 @@ public function findLatestOne(): ?Mouvement /** * Ne lit que le compte, la date et le montant des mouvements existants, en une seule requête pour tous les mouvements donnés. */ + public function aDesMouvements(CompteId $compteId): bool + { + return false !== $this->connection->fetchOne( + << (string) $compteId, + ] + ); + } + + public function getPériodeDesMouvements(CompteId $compteId): ?Periode + { + $row = $this->connection->fetchAssociative( + << (string) $compteId, + ] + ); + + // Sans mouvement, MIN et MAX valent NULL + if (false === $row || !is_string($row['debut']) || !is_string($row['fin'])) { + return null; + } + + $début = \DateTimeImmutable::createFromFormat('Y-m-d H:i:s', "{$row['debut']} 00:00:00"); + $fin = \DateTimeImmutable::createFromFormat('Y-m-d H:i:s', "{$row['fin']} 00:00:00"); + + if (!$début instanceof \DateTimeImmutable || !$fin instanceof \DateTimeImmutable) { + $message = sprintf( + 'Dates des mouvements du compte [%s] invalides : [%s] et [%s].', + $compteId, + $row['debut'], + $row['fin'], + ); + throw new \UnexpectedValueException($message); + } + + return new Periode($début, $fin); + } + public function trouverLesDoublons(MouvementCollection $mouvements): MouvementCollection { $dates = $mouvements->toArray( diff --git a/templates/Compte/edit.html.twig b/templates/Compte/edit.html.twig new file mode 100644 index 0000000..eaaeab0 --- /dev/null +++ b/templates/Compte/edit.html.twig @@ -0,0 +1,72 @@ +{% extends 'layout.html.twig' %} + +{% set titre = compte is null ? "Nouveau compte" : "Édition du compte « %s »"|format(compte) %} + +{% block title %} - {{ titre }}{% endblock %} + +{% block content %} +

{{ titre }}

+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +

Solde avant le premier mouvement enregistré dans l'application.

+
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +

Ordre d'affichage parmi les comptes.

+
+
+ +
+
+ + + Annuler + +
+
+
+{% endblock %} diff --git a/templates/Compte/index.html.twig b/templates/Compte/index.html.twig index 5f1c73d..daa9f77 100644 --- a/templates/Compte/index.html.twig +++ b/templates/Compte/index.html.twig @@ -7,12 +7,17 @@ {% block content %}

Comptes

- {% if a_des_comptes_fermes %} - - - {{ avec_fermes ? 'Masquer les comptes fermés' : 'Afficher les comptes fermés' }} +