src/EventListener/InterventionListener.php line 48

Open in your IDE?
  1. <?php
  2. /*
  3. * Disclaimer: This source code is protected by copyright law and by
  4. * international conventions.
  5. *
  6. * Any reproduction or partial or total distribution of the source code, by any
  7. * means whatsoever, is strictly forbidden.
  8. *
  9. * Anyone not complies with these provisions will be guilty of the offense of
  10. * infringement and the penal sanctions provided for by law.
  11. *
  12. * © 2023 All rights reserved.
  13. */
  14. namespace App\EventListener;
  15. use App\Entity\Intervention;
  16. use App\Entity\ProviderProduct;
  17. use App\Entity\Stock;
  18. use App\Event\InterventionEvent;
  19. use App\Factory\StockFactory;
  20. use App\Repository\InterventionRepository;
  21. use App\Repository\ProviderProductRepository;
  22. use App\Repository\StockRepository;
  23. use Doctrine\Persistence\ManagerRegistry;
  24. /**
  25.  * Class ProviderProductOrderDeliveryListener
  26.  *
  27.  * @author Rémy P. <r.peyron@ingeno.eu>
  28.  * @package App\EventListener
  29.  */
  30. class InterventionListener
  31. {
  32.     /**
  33.      * @var ManagerRegistry
  34.      */
  35.     private $manager;
  36.     public function __construct (ManagerRegistry $manager)
  37.     {
  38.         $this->manager $manager;
  39.     }
  40.     public function onPostUpdated (InterventionEvent $event)
  41.     {
  42.         $entity $event->getIntervention();
  43.         if (!$entity->isStep6Valid()) return;
  44.         if ($entity->getStockUpdated()) return;
  45.         if (!$entity->getInvoice()) return;
  46.         if (!$entity->getAgency()) return;
  47.         $agency $entity->getAgency();
  48.         /** @var ProviderProductRepository $providerProductRepository */
  49.         $providerProductRepository $this->manager->getRepository(ProviderProduct::class);
  50.         /** @var InterventionRepository $interventionRepository */
  51.         $interventionRepository $this->manager->getRepository(Intervention::class);
  52.         /** @var StockRepository $stockRepository */
  53.         $stockRepository $this->manager->getRepository(Stock::class);
  54.         $em $this->manager->getManager();
  55.         foreach ($entity->getInvoice()->getDetails() as $detail) {
  56.             $product $providerProductRepository->findOneBy(['code' => $detail->getCode()]);
  57.             if (!$product) continue;
  58.             $stock $stockRepository->findOneBy(['agency' => $agency->getId(), 'product' => $product->getId()]);
  59.             if (!$stock) {
  60.                 $stock StockFactory::create();
  61.                 $stock
  62.                     ->setQuantity(0.00)
  63.                     ->setProduct($product)
  64.                     ->setAgency($agency);
  65.                 $em->persist($stock);
  66.                 $em->flush();
  67.             }
  68.             $stockRepository->update($stock->setQuantity($stock->getQuantity() - $detail->getQuantity()));
  69.         }
  70.         $interventionRepository->update($entity->setStockUpdated(true));
  71.     }
  72. }