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

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 Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. class DeliveryNoteSubscriber implements EventSubscriberInterface
  17. {
  18.     /**
  19.      * @var OrderOptionsRepository $orderOptionsRepo
  20.      */
  21.     private OrderOptionsRepository $orderOptionsRepo;
  22.     /**
  23.      * @var WeightProductListService $weightProductListService
  24.      */
  25.     private WeightProductListService $weightProductListService;
  26.     /**
  27.      * @var EntityManagerInterface $em
  28.      */
  29.     private EntityManagerInterface $em;
  30.     /**
  31.      * @var Configuration $config
  32.      */
  33.     private Configuration $config;
  34.     public function __construct(
  35.         OrderOptionsRepository $orderOptionsRepo,
  36.         WeightProductListService $weightProductListService,
  37.         EntityManagerInterface $em,
  38.         Configuration $config
  39.     ) {
  40.         $this->orderOptionsRepo $orderOptionsRepo;
  41.         $this->weightProductListService $weightProductListService;
  42.         $this->em $em;
  43.         $this->config $config;
  44.     }
  45.     /**
  46.      * @return array
  47.      */
  48.     public static function getSubscribedEvents(): array
  49.     {
  50.         return [
  51.             DeliveryNoteMovementsEvent::VALIDATE_DELIVERY => 'validateDelivery',
  52.             DeliveryNoteMovementsEvent::CANCEL_DELIVERY => 'cancelDelivery'
  53.         ];
  54.     }
  55.     /**
  56.      * @param DeliveryNoteMovementsEvent $event
  57.      * @return void
  58.      */
  59.     public function validateDelivery(DeliveryNoteMovementsEvent $event): void
  60.     {
  61.         $deliveryNote $event->getDeliveryNote();
  62.         $order $deliveryNote->getOrder();
  63.         if (!$order) {
  64.             return;
  65.         }
  66.         $deliveryNoteId $deliveryNote->getId();
  67.         $order_options $this->orderOptionsRepo->findOneBy(
  68.             ['order' => $order'delivery_note' => null]
  69.         );
  70.         if (!$order_options) {
  71.             $order_options $this->orderOptionsRepo->findOneBy(
  72.                 ['order' => $order'delivery_note' => $deliveryNote]
  73.             );
  74.         }
  75.         if ($order_options && $order_options->getBurst()) {
  76.             $order_options->setDeliveryNote($deliveryNote);
  77.         } else {
  78.             if (!$this->config->get('shipments_bundle__delivery_note_and_label_merged')) {
  79.                 $order_options $this->orderOptionsRepo->findOneBy(['delivery_note' => $deliveryNoteId]);
  80.                 if (!$order_options) {
  81.                     $order_options = new OrderOptions();
  82.                     $order_options->setOrder($order);
  83.                     $order_options->setDeliveryNote($deliveryNote);
  84.                 }
  85.             } else {
  86.                 if (!$order_options) {
  87.                     $order_options = new OrderOptions();
  88.                     $order_options->setOrder($order);
  89.                 }
  90.                 $order_options->setDeliveryNote($deliveryNote);
  91.             }
  92.         }
  93.         $product_list_element = [];
  94.         foreach ($deliveryNote->getProductLines() as $deliveryNoteLine) {
  95.             $quantity $deliveryNoteLine->getQuantity();
  96.             $options $deliveryNoteLine->getOptions();
  97.             $product $options['product'];
  98.             $virtualQuantity $product['virtual_quantity'] ?? 1;
  99.             if ($deliveryNoteLine->isDeclination()) {
  100.                 $product_list_element[] = [
  101.                     'quantity' => $quantity $virtualQuantity,
  102.                     'product_id' => $product['id'],
  103.                     'declination_id' => $product['declination_id']
  104.                 ];
  105.             } elseif ($deliveryNoteLine->isProduct()) {
  106.                 $product_list_element[] = [
  107.                     'quantity' => $quantity,
  108.                     'product_id' => $product['id']
  109.                 ];
  110.             }
  111.         }
  112.         if ($total_weight $this->weightProductListService->getTotalWeight($product_list_element)) {
  113.             $order_options->setWeight((string) $total_weight);
  114.         }
  115.         if (
  116.             $orderOptions $this->orderOptionsRepo->findOneBy(
  117.                 ['order' => $order->getId(), 'delivery_note' => null]
  118.             )
  119.         ) {
  120.             if ($carrier $orderOptions->getCarrier()) {
  121.                 $order_options->setCarrier($carrier);
  122.             }
  123.         }
  124.         $order_options->setStatus(1);
  125.         $this->em->persist($order_options);
  126.         $this->em->flush();
  127.     }
  128.     /**
  129.      * @param DeliveryNoteMovementsEvent $event
  130.      * @return void
  131.      */
  132.     public function cancelDelivery(DeliveryNoteMovementsEvent $event): void
  133.     {
  134.         $deliveryNote $event->getDeliveryNote();
  135.         if (!$deliveryNote->getOrder()) {
  136.             return;
  137.         }
  138.         $deliveryNoteOrderOptions $this->orderOptionsRepo->findOneBy(['delivery_note' => $deliveryNote]);
  139.         if ($deliveryNoteOrderOptions) {
  140.             $order_options $this->orderOptionsRepo->createQueryBuilder('oo')
  141.             ->select('COUNT(oo.id)')
  142.             ->where('oo.order = :order_id')
  143.             ->setParameter('order_id'$deliveryNote->getOrder()->getId()->toBinary())
  144.             ->andWhere('oo.id != :oo_id')
  145.             ->setParameter('oo_id'$deliveryNoteOrderOptions->getId()->toBinary())
  146.             ->getQuery()->getSingleScalarResult();
  147.             if ($order_options) {
  148.                 $this->em->remove($deliveryNoteOrderOptions);
  149.             } else {
  150.                 $deliveryNoteOrderOptions->setDeliveryNote(null);
  151.                 $deliveryNoteOrderOptions->setBurst(null);
  152.                 $deliveryNoteOrderOptions->setStatus(0);
  153.                 $this->em->persist($deliveryNoteOrderOptions);
  154.             }
  155.             $this->em->flush();
  156.         }
  157.     }
  158. }