src/EventSubscriber/LigneCommandeSubscriber.php line 36

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\ProductLigneCommande;
  4. use App\Event\Produit\LigneCommandeCreatedEvent;
  5. use App\Event\Produit\LigneCommandeDeletedEvent;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class LigneCommandeSubscriber implements EventSubscriberInterface
  9. {
  10.     /**
  11.      * @var EntityManagerInterface
  12.      */
  13.     private $entityManager;
  14.     public function __construct(EntityManagerInterface $entityManager)
  15.     {
  16.         $this->entityManager $entityManager;
  17.     }
  18.     public static function getSubscribedEvents(): array
  19.     {
  20.         return [
  21.             LigneCommandeCreatedEvent::class => ['OnLigneCreated'100],
  22.             LigneCommandeDeletedEvent::class => ['OnLigneDeleted'100],
  23.         ];
  24.     }
  25.     public function OnLigneCreated(LigneCommandeCreatedEvent $event)
  26.     {
  27.         $this->updateStock($event->getLigne());
  28.     }
  29.     public function OnLigneDeleted(LigneCommandeDeletedEvent $event)
  30.     {
  31.         $ligne $event->getLigne();
  32.         $this->updateStock($ligne);
  33.         // Si lié a un BL, on met à jour le montant du BL
  34.         if ($ligne->getCommande()->getLivraisonChantier()) {
  35.             $livraison $ligne->getCommande()->getLivraisonChantier();
  36.             $livraison->setTotalTtc($livraison->getTotalTtc() - $ligne->getTotalTtc());
  37.             $this->entityManager->flush();
  38.         }
  39.     }
  40.     private function updateStock (ProductLigneCommande $ligne) {
  41.         // On met à jour le montant HT et TTC de la commande
  42.         $commande $ligne->getCommande();
  43.         $lignes $commande->getLignes();
  44.         $ttc $ht 0;
  45.         foreach ($lignes as $l) {
  46.             $ttc += $l->getTotalTtc();
  47.             $ht += $l->getTotalHt();
  48.         }
  49.         $commande->setTotalHt($ht)->setTotalTtc($ttc);
  50.         $this->entityManager->persist($commande);
  51.         $this->entityManager->flush();
  52.     }
  53. }