vendor/bluue/sales-bundle/src/EventSubscriber/ProductsBundle/ProductDocumentEventSubscriber.php line 135

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Bluue\SalesBundle\EventSubscriber\ProductsBundle;
  4. use Bluue\ProductsBundle\Entity\Declination;
  5. use Bluue\ProductsBundle\Entity\Product;
  6. use Bluue\ProductsBundle\Event\ProductDocumentEvent;
  7. use Bluue\SalesBundle\Entity\CreditNote;
  8. use Bluue\SalesBundle\Entity\DeliveryNote;
  9. use Bluue\SalesBundle\Entity\Invoice;
  10. use Bluue\SalesBundle\Entity\Order;
  11. use Bluue\SalesBundle\Entity\Quotation;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Doctrine\ORM\EntityRepository;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\Routing\RouterInterface;
  16. class ProductDocumentEventSubscriber implements EventSubscriberInterface
  17. {
  18.     /**
  19.      * @var EntityManagerInterface
  20.      */
  21.     private EntityManagerInterface $em;
  22.     /**
  23.      * @var RouterInterface
  24.      */
  25.     private RouterInterface $router;
  26.     /**
  27.      * @param EntityManagerInterface $em
  28.      * @param RouterInterface $router
  29.      */
  30.     public function __construct(
  31.         EntityManagerInterface $em,
  32.         RouterInterface $router
  33.     ) {
  34.         $this->em $em;
  35.         $this->router $router;
  36.     }
  37.     /**
  38.      * @return array
  39.      */
  40.     public static function getSubscribedEvents(): array
  41.     {
  42.         return [
  43.             ProductDocumentEvent::PRODUCT => 'onProduct',
  44.             ProductDocumentEvent::DECLINATION => 'onDeclination'
  45.         ];
  46.     }
  47.     /**
  48.      * @param object $object
  49.      * @return array
  50.      */
  51.     private function getDocuments(object $object): array
  52.     {
  53.         $entities = ['Quotation''Order''Invoice''CreditNote''DeliveryNote'];
  54.         $documents = [];
  55.         foreach ($entities as $entity) {
  56.             if ($entity == 'Quotation') {
  57.                 $repo $this->em->getRepository(Quotation::class);
  58.             } elseif ($entity == 'Order') {
  59.                 $repo $this->em->getRepository(Order::class);
  60.             } elseif ($entity == 'Invoice') {
  61.                 $repo $this->em->getRepository(Invoice::class);
  62.             } elseif ($entity == 'CreditNote') {
  63.                 $repo $this->em->getRepository(CreditNote::class);
  64.             } elseif ($entity == 'DeliveryNote') {
  65.                 $repo $this->em->getRepository(DeliveryNote::class);
  66.             }
  67.             /** @var EntityRepository $repo */
  68.             $query $repo->createQueryBuilder('u')
  69.                 ->select('u.id, u.reference')
  70.                 ->leftJoin('u.lines''lines')
  71.                 ->groupBy('u.id')
  72.                 ->andWhere('u.validatedAt IS NULL')
  73.                 ->setParameter('object_id'$object->getId()->toBinary());
  74.             if ($object instanceof Product) {
  75.                 $query->andWhere('lines.product = :object_id');
  76.             } elseif ($object instanceof Declination) {
  77.                 $query->andWhere('lines.declination = :object_id');
  78.             }
  79.             $results $query->getQuery()->getResult();
  80.             if ($results) {
  81.                 foreach ($results as &$result) {
  82.                     if ($entity == 'Invoice' || $entity == 'CreditNote') {
  83.                         if (!$result['reference']) {
  84.                             $result['reference'] = $entity == 'Invoice' 'Facture' 'Avoir';
  85.                         }
  86.                     }
  87.                     if ($entity == 'CreditNote') {
  88.                         $entityUrl 'credit_note';
  89.                     } elseif ($entity == 'DeliveryNote') {
  90.                         $entityUrl 'delivery_note';
  91.                     } else {
  92.                         $entityUrl strtolower($entity);
  93.                     }
  94.                     $result['url'] = $this->router->generate(
  95.                         'sales_bundle__' $entityUrl '_view',
  96.                         ['id' => $result['id']]
  97.                     );
  98.                 }
  99.                 $documents array_merge($documents$results);
  100.             }
  101.         }
  102.         return $documents;
  103.     }
  104.     /**
  105.      * @param ProductDocumentEvent $event
  106.      * @return void
  107.      */
  108.     public function onProduct(ProductDocumentEvent $event): void
  109.     {
  110.         $product $event->getObject();
  111.         $documents $event->getDocuments();
  112.         $documents[] = $this->getDocuments($product);
  113.         $event->setDocuments($documents);
  114.     }
  115.     /**
  116.      * @param ProductDocumentEvent $event
  117.      * @return void
  118.      */
  119.     public function onDeclination(ProductDocumentEvent $event): void
  120.     {
  121.         $declination $event->getObject();
  122.         $documents $event->getDocuments();
  123.         $documents[] = $this->getDocuments($declination);
  124.         $event->setDocuments($documents);
  125.     }
  126. }