From 01f23682b30272471c7867100b945b6c75aa6021 Mon Sep 17 00:00:00 2001 From: joshua Date: Sun, 6 Apr 2025 17:11:21 +0200 Subject: [PATCH] =?UTF-8?q?syst=C3=A8me=20de=20r=C3=A9duction=20pour=20l'a?= =?UTF-8?q?pp=20en=20fonction=20des=20clients,=20correcion=20de=20la=20bas?= =?UTF-8?q?e,=20maintenant=20les=20reductions=20sont=20appliquer=20automat?= =?UTF-8?q?iquement=20sur=20le=20client=20en=20fonction=20du=20montant=20d?= =?UTF-8?q?=C3=A9penser,=20ajout=20d'un=20syst=C3=A8me=20de=20verif=20pour?= =?UTF-8?q?=20changer=20en=20fonction=20des=20modif,=20les=20reductions=20?= =?UTF-8?q?sont=20automatiquement=20changer=20ou=20cr=C3=A9e=20en=20foncti?= =?UTF-8?q?on=20des=20d=C3=A9penses=20du=20client=20donc=20j'ai=20enlever?= =?UTF-8?q?=20le=20edit=20et=20new=20pcq=20on=20en=20a=20plus=20besoin=20e?= =?UTF-8?q?ncore=202=203=20truc=20a=20r=C3=A9gler=20mais=20le=20plus=20gro?= =?UTF-8?q?s=20est=20fait,=20FAUT=20UPDATE=20LA=20BASE=20AVEC=20LES=20MIGR?= =?UTF-8?q?ATIONS=20EN=20COURS=20SINON=20CA=20MARCHERA=20PAS?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 2 +- composer.lock | 24 ++--- src/Controller/ClientsController.php | 74 ++++++++++++++-- src/Entity/Clients.php | 63 ++++++++++++++ src/Entity/Plats.php | 15 ---- src/Entity/Reductions.php | 125 +++++++++------------------ src/Form/ClientsType.php | 3 +- src/Form/PlatsType.php | 4 - src/Form/ReductionsType.php | 14 ++- templates/clients/index.html.twig | 91 ++++++++++--------- templates/reductions/index.html.twig | 23 ++--- 11 files changed, 253 insertions(+), 185 deletions(-) diff --git a/composer.json b/composer.json index fc4788a..01e8e80 100644 --- a/composer.json +++ b/composer.json @@ -39,7 +39,7 @@ "symfony/string": "7.1.*", "symfony/translation": "7.1.*", "symfony/twig-bundle": "7.1.*", - "symfony/ux-icons": "^2.21", + "symfony/ux-icons": "^2.24", "symfony/ux-turbo": "^2.20", "symfony/validator": "7.1.*", "symfony/web-link": "7.1.*", diff --git a/composer.lock b/composer.lock index c0e03e7..7ef7ff5 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "7fff997f78db4185bb53c4543a245c74", + "content-hash": "d1751dd70a356412dfadda645c7118ec", "packages": [ { "name": "amphp/amp", @@ -8164,16 +8164,16 @@ }, { "name": "symfony/ux-icons", - "version": "v2.21.0", + "version": "v2.24.0", "source": { "type": "git", "url": "https://github.com/symfony/ux-icons.git", - "reference": "c3b3fd29b3f90e977b267251ef15ea06f3cec553" + "reference": "39f689b41081f7788ee9c4a188817599d546bfb2" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/ux-icons/zipball/c3b3fd29b3f90e977b267251ef15ea06f3cec553", - "reference": "c3b3fd29b3f90e977b267251ef15ea06f3cec553", + "url": "https://api.github.com/repos/symfony/ux-icons/zipball/39f689b41081f7788ee9c4a188817599d546bfb2", + "reference": "39f689b41081f7788ee9c4a188817599d546bfb2", "shasum": "" }, "require": { @@ -8197,8 +8197,8 @@ "type": "symfony-bundle", "extra": { "thanks": { - "name": "symfony/ux", - "url": "https://github.com/symfony/ux" + "url": "https://github.com/symfony/ux", + "name": "symfony/ux" } }, "autoload": { @@ -8233,7 +8233,7 @@ "twig" ], "support": { - "source": "https://github.com/symfony/ux-icons/tree/v2.21.0" + "source": "https://github.com/symfony/ux-icons/tree/v2.24.0" }, "funding": [ { @@ -8249,7 +8249,7 @@ "type": "tidelift" } ], - "time": "2024-10-21T19:01:22+00:00" + "time": "2025-04-04T17:32:18+00:00" }, { "name": "symfony/ux-turbo", @@ -11242,7 +11242,7 @@ ], "aliases": [], "minimum-stability": "stable", - "stability-flags": [], + "stability-flags": {}, "prefer-stable": true, "prefer-lowest": false, "platform": { @@ -11250,6 +11250,6 @@ "ext-ctype": "*", "ext-iconv": "*" }, - "platform-dev": [], - "plugin-api-version": "2.3.0" + "platform-dev": {}, + "plugin-api-version": "2.6.0" } diff --git a/src/Controller/ClientsController.php b/src/Controller/ClientsController.php index 2c41873..15d2f2d 100644 --- a/src/Controller/ClientsController.php +++ b/src/Controller/ClientsController.php @@ -1,19 +1,22 @@ handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { + + $montantTotal = $client->getTotalDepense(); + if($montantTotal > 1000){ + $this->appliquerReductionEtCreerSiNecessaire($client, $entityManager); + } + $entityManager->persist($client); $entityManager->flush(); @@ -42,7 +51,6 @@ final class ClientsController extends AbstractController ]); } - #[Route('/{id}/edit', name: 'app_clients_edit', methods: ['GET', 'POST'])] public function edit(Request $request, Clients $client, EntityManagerInterface $entityManager): Response { @@ -50,6 +58,13 @@ final class ClientsController extends AbstractController $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { + + $montantTotal = $client->getTotalDepense(); + if($montantTotal > 1000){ + $this->appliquerReductionEtCreerSiNecessaire($client, $entityManager); + } + + $entityManager->flush(); return $this->redirectToRoute('app_clients_index', [], Response::HTTP_SEE_OTHER); @@ -62,13 +77,62 @@ final class ClientsController extends AbstractController } #[Route('/{id}', name: 'app_clients_delete', methods: ['POST'])] - public function delete(Request $request, Clients $client, EntityManagerInterface $entityManager, int $id, ): Response + public function delete(Request $request, Clients $client, EntityManagerInterface $entityManager): Response { - if ($this->isCsrfTokenValid('delete'.$client->getId(), $request->getPayload()->getString('_token'))) { + if ($this->isCsrfTokenValid('delete'.$client->getId(), $request->request->get('_token'))) { $entityManager->remove($client); $entityManager->flush(); } return $this->redirectToRoute('app_clients_index', [], Response::HTTP_SEE_OTHER); } + + // Méthode pour appliquer ou créer une réduction pour un client + // src/Controller/ClientsController.php + + private function appliquerReductionEtCreerSiNecessaire(Clients $client, EntityManagerInterface $entityManager) + { + $montantTotal = $client->getTotalDepense(); + $entityManager->persist($client); + + $reductions = $entityManager->getRepository(Reductions::class) + ->createQueryBuilder('r') + ->where('r.montant_min <= :montantTotal') + ->andWhere('r.actif = :actif') + ->andWhere('r.DateDebut <= :currentDate') + ->andWhere('r.DateFin >= :currentDate') + ->setParameter('montantTotal', $montantTotal) + ->setParameter('actif', true) + ->setParameter('currentDate', new \DateTime()) + ->getQuery() + ->getResult(); + + $newReduction = new Reductions(); + + if ($montantTotal >= 2000) { + $montantMin = 2000; + $pourcentage = 10; + } elseif ($montantTotal >= 1000) { + $montantMin = 1000; + $pourcentage = 5; + } + + $newReduction->setPourcentage($pourcentage) + ->setMontantMin($montantMin) + ->setDateDebut(new \DateTime()) + ->setDateFin((new \DateTime())->modify('+1 year')) + ->setActif(true) + ->setClient($client); + + $entityManager->persist($newReduction); + $entityManager->flush(); + + if (!$client->getReductions()->contains($newReduction)) { + $client->addReduction($newReduction); + } + + $entityManager->flush(); + } + + } diff --git a/src/Entity/Clients.php b/src/Entity/Clients.php index 7367507..0c34287 100644 --- a/src/Entity/Clients.php +++ b/src/Entity/Clients.php @@ -40,10 +40,17 @@ class Clients #[ORM\ManyToMany(targetEntity: Commandes::class, mappedBy: 'Client')] private Collection $commandes; + #[ORM\Column] + private ?float $totalDepense = null; + + #[ORM\OneToMany(targetEntity: Reductions::class, mappedBy: 'client', cascade: ['persist', 'remove'])] + private Collection $reductions; + public function __construct() { $this->tables = new ArrayCollection(); $this->commandes = new ArrayCollection(); + $this->reductions = new ArrayCollection(); } public function getId(): ?int @@ -149,4 +156,60 @@ class Clients return $this; } + + + public function getTotalDepense(): ?float + { + return $this->totalDepense; + } + + public function setTotalDepense(float $totalDepense): static + { + $this->totalDepense = $totalDepense; + + return $this; + } + + // Dans l'entité Clients + public function appliquerReduction(): void + { + $montantTotal = $this->getTotalDepense(); + + // Vérifie s'il y a des réductions valides disponibles + $reductions = $this->getReductions(); // Récupérer toutes les réductions du client + + foreach ($reductions as $reduction) { + // Vérifie si la réduction est valide (date de validité et montant minimum) + if ($reduction->getDateDebut() <= new \DateTime() && $reduction->getDateFin() >= new \DateTime()) { + if ($montantTotal >= $reduction->getMontantMin()) { + // Applique la réduction en pourcentage au total des dépenses + $reductionMontant = ($montantTotal * $reduction->getPourcentage()) / 100; + $nouveauTotal = $montantTotal - $reductionMontant; // Nouveau montant après réduction + $this->setTotalDepense($nouveauTotal); // Mise à jour du total des dépenses du client + + // Met à jour l'objet client avec la réduction + break; // Si une réduction a été appliquée, on arrête + } + } + } + } + + public function getReductions(): Collection + { + return $this->reductions; + } + + public function addReduction(Reductions $reduction): static + { + if (!$this->reductions->contains($reduction)) { + $this->reductions->add($reduction); + } + return $this; + } + + public function removeReduction(Reductions $reduction): static + { + $this->reductions->removeElement($reduction); + return $this; + } } diff --git a/src/Entity/Plats.php b/src/Entity/Plats.php index 5515a14..34424c8 100644 --- a/src/Entity/Plats.php +++ b/src/Entity/Plats.php @@ -39,9 +39,6 @@ class Plats #[ORM\ManyToMany(targetEntity: Commandes::class, inversedBy: 'plats')] private Collection $Commande; - #[ORM\ManyToOne(inversedBy: 'plats')] - private ?Reductions $Reduction = null; - public function __construct() { $this->Commande = new ArrayCollection(); @@ -147,16 +144,4 @@ class Plats return $this; } - - public function getReduction(): ?Reductions - { - return $this->Reduction; - } - - public function setReduction(?Reductions $Reduction): static - { - $this->Reduction = $Reduction; - - return $this; - } } diff --git a/src/Entity/Reductions.php b/src/Entity/Reductions.php index ed93d26..509f1b2 100644 --- a/src/Entity/Reductions.php +++ b/src/Entity/Reductions.php @@ -16,88 +16,30 @@ class Reductions #[ORM\Column] private ?int $id = null; - #[ORM\Column(length: 255)] - private ?string $Description = null; + #[ORM\Column] + private ?int $pourcentage = null; #[ORM\Column] - private ?float $Prix = null; + private ?int $montant_min = null; #[ORM\Column] - private ?int $Pourcentage = null; - - #[ORM\Column(length: 255)] - private ?string $MontantFixe = null; - + private ?bool $actif = null; #[ORM\Column(type: Types::DATETIME_MUTABLE)] private ?\DateTimeInterface $DateDebut = null; #[ORM\Column(type: Types::DATETIME_MUTABLE)] private ?\DateTimeInterface $DateFin = null; - /** - * @var Collection - */ - #[ORM\OneToMany(targetEntity: Plats::class, mappedBy: 'Reduction')] - private Collection $plats; - public function __construct() - { - $this->plats = new ArrayCollection(); - } + #[ORM\ManyToOne(targetEntity: Clients::class, inversedBy: 'reductions')] + #[ORM\JoinColumn(nullable: false)] + private ?Clients $client = null; public function getId(): ?int { return $this->id; } - - public function getDescription(): ?string - { - return $this->Description; - } - - public function setDescription(string $Description): static - { - $this->Description = $Description; - - return $this; - } - - public function getPrix(): ?float - { - return $this->Prix; - } - - public function setPrix(float $Prix): static - { - $this->Prix = $Prix; - - return $this; - } - - public function getPourcentage(): ?int - { - return $this->Pourcentage; - } - - public function setPourcentage(int $Pourcentage): static - { - $this->Pourcentage = $Pourcentage; - - return $this; - } - - public function getMontantFixe(): ?string - { - return $this->MontantFixe; - } - - public function setMontantFixe(string $MontantFixe): static - { - $this->MontantFixe = $MontantFixe; - - return $this; - } - + public function getDateDebut(): ?\DateTimeInterface { return $this->DateDebut; @@ -122,33 +64,50 @@ class Reductions return $this; } - /** - * @return Collection - */ - public function getPlats(): Collection + public function getPourcentage(): ?int { - return $this->plats; + return $this->pourcentage; } - public function addPlat(Plats $plat): static + public function setPourcentage(int $pourcentage): static { - if (!$this->plats->contains($plat)) { - $this->plats->add($plat); - $plat->setReduction($this); - } + $this->pourcentage = $pourcentage; return $this; } - public function removePlat(Plats $plat): static + public function getMontantMin(): ?int { - if ($this->plats->removeElement($plat)) { - // set the owning side to null (unless already changed) - if ($plat->getReduction() === $this) { - $plat->setReduction(null); - } - } + return $this->montant_min; + } + public function setMontantMin(int $montant_min): static + { + $this->montant_min = $montant_min; + + return $this; + } + + public function isActif(): ?bool + { + return $this->actif; + } + + public function setActif(bool $actif): static + { + $this->actif = $actif; + + return $this; + } + + public function getClient(): ?Clients + { + return $this->client; + } + + public function setClient(?Clients $client): static + { + $this->client = $client; return $this; } } diff --git a/src/Form/ClientsType.php b/src/Form/ClientsType.php index 4444b85..1dcf24c 100644 --- a/src/Form/ClientsType.php +++ b/src/Form/ClientsType.php @@ -4,6 +4,7 @@ namespace App\Form; use App\Entity\Clients; use App\Entity\Commandes; +use App\Entity\Reductions; use App\Entity\Tables; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; @@ -25,7 +26,7 @@ class ClientsType extends AbstractType 'multiple' => true, 'expanded' => true, ]) - + ->add('totalDepense') ; } diff --git a/src/Form/PlatsType.php b/src/Form/PlatsType.php index 068fa54..a5d3e9d 100644 --- a/src/Form/PlatsType.php +++ b/src/Form/PlatsType.php @@ -27,10 +27,6 @@ class PlatsType extends AbstractType 'multiple' => true, 'expanded' => true, ]) - ->add('Reduction', EntityType::class, [ - 'class' => Reductions::class, - 'choice_label' => 'description', - ]) ; } diff --git a/src/Form/ReductionsType.php b/src/Form/ReductionsType.php index ff5957e..9cccbfc 100644 --- a/src/Form/ReductionsType.php +++ b/src/Form/ReductionsType.php @@ -2,7 +2,10 @@ namespace App\Form; +use App\Entity\Clients; use App\Entity\Reductions; +use http\Client; +use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -12,16 +15,19 @@ class ReductionsType extends AbstractType public function buildForm(FormBuilderInterface $builder, array $options): void { $builder - ->add('Description') - ->add('Prix') - ->add('Pourcentage') - ->add('MontantFixe') + ->add('Taux') ->add('DateDebut', null, [ 'widget' => 'single_text', ]) ->add('DateFin', null, [ 'widget' => 'single_text', ]) + ->add('client', EntityType::class, [ + 'class' => Clients::class, + 'choice_label' => 'nom', + 'multiple' => false, + 'expanded' => true, + ]) ; } diff --git a/templates/clients/index.html.twig b/templates/clients/index.html.twig index e78988b..9c14936 100644 --- a/templates/clients/index.html.twig +++ b/templates/clients/index.html.twig @@ -2,60 +2,59 @@ {% block title %}Client index{% endblock %} -{% block head %} - - - - - -{% endblock %} - -{% block stylesheets %} - - - - -{% endblock %} - {% block container_modal %} -
-

Clients index

+
+

Clients index

- - +
+ - + + + - - - {% for client in clients %} - - - - - - - - - {% else %} - - - - {% endfor %} - -
Id Prenom Nom Email TelephoneactionsDepenseRéduction Appliquée Actions
{{ client.id }}{{ client.Prenom }}{{ client.Nom }}{{ client.Email }}{{ client.Telephone }} -
- - {{ include('clients/_delete_form.html.twig') }} -
- Modifier -
no records found
+ + + {% for client in clients %} + + {{ client.id }} + {{ client.Prenom }} + {{ client.Nom }} + {{ client.Email }} + {{ client.Telephone }} + {{ client.totalDepense }} - Créer un nouveau client -
+ + {% if client.reductions|length > 0 %} + {% for reduction in client.reductions %} + Réduction de {{ reduction.pourcentage }}% - Valide jusqu'à {{ reduction.DateFin|date('d/m/Y') }}
+ {% endfor %} + {% else %} + Aucune réduction + {% endif %} + + + +
+ + {{ include('clients/_delete_form.html.twig') }} +
+ Modifier + + + {% else %} + + Aucun client trouvé + + {% endfor %} + + + + Créer un nouveau client +
{% endblock %} diff --git a/templates/reductions/index.html.twig b/templates/reductions/index.html.twig index ee999f6..7d27cf1 100644 --- a/templates/reductions/index.html.twig +++ b/templates/reductions/index.html.twig @@ -27,12 +27,11 @@ Id - Description - Prix Pourcentage - MontantFixe + Montant Minimum DateDebut DateFin + Client actions @@ -40,18 +39,16 @@ {% for reduction in reductions %} {{ reduction.id }} - {{ reduction.Description }} - {{ reduction.Prix }} - {{ reduction.Pourcentage }} - {{ reduction.MontantFixe }} - {{ reduction.DateDebut ? reduction.DateDebut|date('Y-m-d H:i:s') : '' }} - {{ reduction.DateFin ? reduction.DateFin|date('Y-m-d H:i:s') : '' }} + {{ reduction.pourcentage }}% + {{ reduction.montantMin }} € + {{ reduction.DateDebut ? reduction.DateDebut|date('d/m/Y H:i:s') : 'Non spécifié' }} + {{ reduction.DateFin ? reduction.DateFin|date('d/m/Y H:i:s') : 'Non spécifié' }} + {{ reduction.client.nom }} -
+ - {{ include('reductions/_delete_form.html.twig') }} +
- Modifier {% else %} @@ -61,8 +58,6 @@ {% endfor %} - - Créer une réduction {% endblock %}