src/EventSubscriber/ReceptionProduitSubscriber.php line 47

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\ProductLigneFacture;
  4. use App\Entity\ProductStock;
  5. use App\Entity\ProductStockMouvement;
  6. use App\Event\Produit\ReceptionProduitEvent;
  7. use App\Event\Produit\StockMvtEvent;
  8. use App\Repository\ProductLigneFactureRepository;
  9. use App\Repository\ProductStockRepository;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Psr\EventDispatcher\EventDispatcherInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class ReceptionProduitSubscriber implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var EntityManagerInterface
  17.      */
  18.     private $entityManager;
  19.     /**
  20.      * @var ProductLigneFactureRepository
  21.      */
  22.     private $productLigneFactureRepository;
  23.     /**
  24.      * @var EventDispatcherInterface
  25.      */
  26.     private $eventDispatcher;
  27.     public function __construct(EntityManagerInterface $entityManagerProductLigneFactureRepository $productLigneFactureRepository,
  28.                                 EventDispatcherInterface $eventDispatcher)
  29.     {
  30.         $this->entityManager $entityManager;
  31.         $this->productLigneFactureRepository $productLigneFactureRepository;
  32.         $this->eventDispatcher $eventDispatcher;
  33.     }
  34.     public static function getSubscribedEvents(): array
  35.     {
  36.         return [
  37.             ReceptionProduitEvent::class => ['onReceptionProduit'100],
  38.         ];
  39.     }
  40.     // Ajout des produits en stock lors de la récpetion
  41.     public function onReceptionProduit(ReceptionProduitEvent $event)
  42.     {
  43.         $facture $event->getFacture();
  44.         $commande $facture->getCommande();
  45.         $request $event->getRequest();
  46.         $lignes $facture->getCommande()->getLignes();
  47.         // Enregistrement des produits reçus
  48.         foreach ($lignes as $ligne) {
  49.             $ligneFacture $this->productLigneFactureRepository->findOneBy(['facture' => $facture'ligne' => $ligne]);
  50.             $quantiteRecu = (int)$request->get('qte_'.$ligne->getId());
  51.             $mouvementdestock $quantiteRecu;
  52.             if ($quantiteRecu 0) {
  53.                 $prixFacture = (float)$request->get('montant_'.$ligne->getId());
  54.                 if (!$ligneFacture) {
  55.                     $ligneFacture = new ProductLigneFacture();
  56.                     $ligneFacture->setFacture($facture)
  57.                         ->setLigne($ligne);
  58.                 } else {
  59.                     $mouvementdestock $quantiteRecu $ligneFacture->getQuantiteRecue();
  60.                 }
  61.                 $ligneFacture->setQuantiteRecue($quantiteRecu)
  62.                             ->setPrixFactureHt($prixFacture);
  63.                 $this->entityManager->persist($ligneFacture);
  64.                 if ($mouvementdestock != 0) {
  65.                     // Ajout des produits en stock
  66.                     $mvt = new ProductStockMouvement();
  67.                     $mvt->setProduct($ligne->getProduct())
  68.                         ->setStructure($commande->getStructure())
  69.                         ->setDateMouvement(new \Datetime())
  70.                         ->setQuantite(abs($mouvementdestock))
  71.                         ->setTypeMouvement($mouvementdestock 'in' 'out')
  72.                         ->setCommentaire('Réception commande '.$commande->getId());
  73.                     $this->entityManager->persist($mvt);
  74.                     $this->entityManager->persist($mvt);
  75.                     $this->entityManager->flush();
  76.                     $this->eventDispatcher->dispatch(new StockMvtEvent($mvt));
  77.                 }
  78.             }
  79.         }
  80.     }
  81. }