src/EventSubscriber/ProductCommandeSubscriber.php line 54

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Product;
  4. use App\Event\Produit\ProductCommandeEvent;
  5. use App\Repository\EtatLivraisonRepository;
  6. use App\Repository\ProductCommandeRepository;
  7. use App\Repository\ProductRepository;
  8. use App\Service\ProductHelper;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class ProductCommandeSubscriber implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var EntityManagerInterface
  15.      */
  16.     private $entityManager;
  17.     /**
  18.      * @var EtatLivraisonRepository
  19.      */
  20.     private $etatLivraisonRepository;
  21.     /**
  22.      * @var ProductHelper
  23.      */
  24.     private $productHelper;
  25.     /**
  26.      * @var ProductRepository
  27.      */
  28.     private $productRepository;
  29.     /**
  30.      * @var ProductCommandeRepository
  31.      */
  32.     private $commandeRepository;
  33.     public function __construct(EntityManagerInterface $entityManagerEtatLivraisonRepository $etatLivraisonRepositoryProductHelper $productHelper,
  34.                                 ProductRepository $productRepositoryProductCommandeRepository $commandeRepository)
  35.     {
  36.         $this->entityManager $entityManager;
  37.         $this->etatLivraisonRepository $etatLivraisonRepository;
  38.         $this->productHelper $productHelper;
  39.         $this->productRepository $productRepository;
  40.         $this->commandeRepository $commandeRepository;
  41.     }
  42.     public static function getSubscribedEvents(): array
  43.     {
  44.         return [
  45.             ProductCommandeEvent::class => ['onCommandeCreated'100],
  46.         ];
  47.     }
  48.     public function onCommandeCreated(ProductCommandeEvent $event)
  49.     {
  50.         $commande $event->getCommande();
  51.         $request $event->getRequest()->get('new_commande_produit');
  52.         $etat $this->etatLivraisonRepository->findOneBy(['identifiant' => 'pre-commande']);
  53.         if ($request && isset($request['product'])) {
  54.             // On ajoute la ligne du produit, on recherche le meilleur tarif
  55.             /**
  56.              * @var Product $product
  57.              */
  58.             $product $this->productRepository->find($request['product']);
  59.             $quantite = (int)$request['quantite'];
  60.             $bestTarif $this->productHelper->getBestTarif($product);
  61.             $commande->setFournisseur($bestTarif->getFournisseur());
  62.             $this->productHelper->add_pre_order($product->getId(), $commande->getStructure()->getId(), $commande->getFournisseur()->getId(), $quantite$commande->getId());
  63.         }
  64.         // On ajoute les mails par défaut pour l'envoi de la commande
  65.         // Mail du fournisseur + mail du créateur de la commande
  66.         $emails = [$commande->getFournisseur()->getEmail(), $commande->getUser()->getLogin()];
  67.         $commande->setEmail(implode(","array_filter(array_unique($emails))));
  68.         // On ajoute l'état de pré commande
  69.         $commande->setEtat($etat);
  70.         // Numéro de commande
  71.         $numero $this->commandeRepository->getNextNumero();
  72.         $commande->setNumero($numero);
  73.         // Mise à jour du montant total
  74.         $ttc $ht 0;
  75.         foreach ($commande->getLignes() as $l) {
  76.             $ttc += $l->getTotalTtc();
  77.             $ht += $l->getTotalHt();
  78.         }
  79.         $commande->setTotalHt($ht)->setTotalTtc($ttc);
  80.         $this->entityManager->persist($commande);
  81.         $this->entityManager->flush();
  82.     }
  83. }