vendor/bluue/shipments-bundle/src/EventSubscriber/SalesBundle/DeliveryNoteSubscriber.php line 163

Open in your IDE?
  1. <?php
  2. /**
  3.  * @author Léo BANNHOLTZER (contact@scaledev.fr)
  4.  * @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
  5.  * @license commercial
  6.  */
  7. declare(strict_types=1);
  8. namespace Bluue\ShipmentsBundle\EventSubscriber\SalesBundle;
  9. use App\Services\Configuration;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Bluue\ShipmentsBundle\Entity\OrderOptions;
  12. use Bluue\SalesBundle\Event\DeliveryNoteMovementsEvent;
  13. use Bluue\ProductsBundle\Services\WeightProductListService;
  14. use Bluue\ShipmentsBundle\Repository\OrderOptionsRepository;
  15. use Doctrine\Persistence\ManagerRegistry;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. class DeliveryNoteSubscriber implements EventSubscriberInterface
  18. {
  19.     /**
  20.      * @var OrderOptionsRepository $orderOptionsRepo
  21.      */
  22.     private OrderOptionsRepository $orderOptionsRepo;
  23.     /**
  24.      * @var WeightProductListService $weightProductListService
  25.      */
  26.     private WeightProductListService $weightProductListService;
  27.     /**
  28.      * @var EntityManagerInterface $em
  29.      */
  30.     private EntityManagerInterface $em;
  31.     /**
  32.      * @var Configuration $config
  33.      */
  34.     private Configuration $config;
  35.     /**
  36.      * @var ManagerRegistry $registry
  37.      */
  38.     private ManagerRegistry $registry;
  39.     public function __construct(
  40.         OrderOptionsRepository $orderOptionsRepo,
  41.         WeightProductListService $weightProductListService,
  42.         EntityManagerInterface $em,
  43.         Configuration $config,
  44.         ManagerRegistry $registry
  45.     ) {
  46.         $this->orderOptionsRepo $orderOptionsRepo;
  47.         $this->weightProductListService $weightProductListService;
  48.         $this->em $em;
  49.         $this->config $config;
  50.         $this->registry $registry;
  51.     }
  52.     /**
  53.      * @return array
  54.      */
  55.     public static function getSubscribedEvents(): array
  56.     {
  57.         return [
  58.             DeliveryNoteMovementsEvent::VALIDATE_DELIVERY => 'validateDelivery',
  59.             DeliveryNoteMovementsEvent::CANCEL_DELIVERY => 'cancelDelivery'
  60.         ];
  61.     }
  62.     /**
  63.      * @param DeliveryNoteMovementsEvent $event
  64.      * @return void
  65.      */
  66.     public function validateDelivery(DeliveryNoteMovementsEvent $event): void
  67.     {
  68.         if (!$this->em->isOpen()) {
  69.             $this->registry->resetManager();
  70.         }
  71.         $deliveryNote $event->getDeliveryNote();
  72.         $order $deliveryNote->getOrder();
  73.         if (!$order) {
  74.             return;
  75.         }
  76.         $deliveryNoteId $deliveryNote->getId();
  77.         $order_options $this->orderOptionsRepo->findOneBy(
  78.             ['order' => $order'delivery_note' => null]
  79.         );
  80.         if (!$order_options) {
  81.             $order_options $this->orderOptionsRepo->findOneBy(
  82.                 ['order' => $order'delivery_note' => $deliveryNote]
  83.             );
  84.         }
  85.         if ($order_options && $order_options->getBurst()) {
  86.             $order_options->setDeliveryNote($deliveryNote);
  87.         } else {
  88.             if (!$this->config->get('shipments_bundle__delivery_note_and_label_merged')) {
  89.                 $order_options $this->orderOptionsRepo->findOneBy(['delivery_note' => $deliveryNoteId]);
  90.                 if (!$order_options) {
  91.                     $order_options = new OrderOptions();
  92.                     $order_options->setOrder($order);
  93.                     $order_options->setDeliveryNote($deliveryNote);
  94.                 }
  95.             } else {
  96.                 if (!$order_options) {
  97.                     $order_options = new OrderOptions();
  98.                     $order_options->setOrder($order);
  99.                 }
  100.                 $order_options->setDeliveryNote($deliveryNote);
  101.             }
  102.         }
  103.         $product_list_element = [];
  104.         foreach ($deliveryNote->getProductLines() as $deliveryNoteLine) {
  105.             $quantity $deliveryNoteLine->getQuantity();
  106.             $options $deliveryNoteLine->getOptions();
  107.             $product $options['product'];
  108.             $virtualQuantity $product['virtual_quantity'] ?? 1;
  109.             if ($deliveryNoteLine->isDeclination()) {
  110.                 $product_list_element[] = [
  111.                     'quantity' => $quantity $virtualQuantity,
  112.                     'product_id' => $product['id'],
  113.                     'declination_id' => $product['declination_id']
  114.                 ];
  115.             } elseif ($deliveryNoteLine->isProduct()) {
  116.                 $product_list_element[] = [
  117.                     'quantity' => $quantity,
  118.                     'product_id' => $product['id']
  119.                 ];
  120.             }
  121.         }
  122.         if ($total_weight $this->weightProductListService->getTotalWeight($product_list_element)) {
  123.             $order_options->setWeight((string) $total_weight);
  124.         }
  125.         if (
  126.             $orderOptions $this->orderOptionsRepo->findOneBy(
  127.                 ['order' => $order->getId(), 'delivery_note' => null]
  128.             )
  129.         ) {
  130.             if ($carrier $orderOptions->getCarrier()) {
  131.                 $order_options->setCarrier($carrier);
  132.             }
  133.         }
  134.         $order_options->setStatus(1);
  135.         $this->em->persist($order_options);
  136.         $this->em->flush();
  137.     }
  138.     /**
  139.      * @param DeliveryNoteMovementsEvent $event
  140.      * @return void
  141.      */
  142.     public function cancelDelivery(DeliveryNoteMovementsEvent $event): void
  143.     {
  144.         $deliveryNote $event->getDeliveryNote();
  145.         if (!$deliveryNote->getOrder()) {
  146.             return;
  147.         }
  148.         $deliveryNoteOrderOptions $this->orderOptionsRepo->findOneBy(['delivery_note' => $deliveryNote]);
  149.         if ($deliveryNoteOrderOptions) {
  150.             $order_options $this->orderOptionsRepo->createQueryBuilder('oo')
  151.             ->select('COUNT(oo.id)')
  152.             ->where('oo.order = :order_id')
  153.             ->setParameter('order_id'$deliveryNote->getOrder()->getId()->toBinary())
  154.             ->andWhere('oo.id != :oo_id')
  155.             ->setParameter('oo_id'$deliveryNoteOrderOptions->getId()->toBinary())
  156.             ->getQuery()->getSingleScalarResult();
  157.             if ($order_options) {
  158.                 $this->em->remove($deliveryNoteOrderOptions);
  159.             } else {
  160.                 $deliveryNoteOrderOptions->setDeliveryNote(null);
  161.                 $deliveryNoteOrderOptions->setBurst(null);
  162.                 $deliveryNoteOrderOptions->setStatus(0);
  163.                 $this->em->persist($deliveryNoteOrderOptions);
  164.             }
  165.             $this->em->flush();
  166.         }
  167.     }
  168. }