src/EventSubscriber/ReceiptEventSubscriber.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Payment\InvoicePaymentOption;
  4. use App\Payment\CashPaymentOption;
  5. use App\Payment\PinPaymentOption;
  6. use App\Entity\OrderPickupDelivery;
  7. use App\Repository\PrinterOptionsRepository;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Boldr\Shop\ReceiptPrinterBundle\Event\QueueReceiptEvent;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. class ReceiptEventSubscriber implements EventSubscriberInterface
  12. {
  13.     public function __construct(
  14.         private readonly PrinterOptionsRepository $printerOptionsRepository,
  15.         private readonly EntityManagerInterface $em
  16.     ) {}
  17.     public static function getSubscribedEvents()
  18.     {
  19.         return [
  20.             QueueReceiptEvent::class => [
  21.                 ['onQueueReceipt'10]
  22.             ]
  23.         ];
  24.     }
  25.     public function onQueueReceipt(QueueReceiptEvent $event)
  26.     {
  27.         $printer $event->getPrinter();
  28.         $printerOptions $this->printerOptionsRepository->find($printer);
  29.         // If there are no HB-specific printer options for this printer, ignore
  30.         if ($printerOptions === null)
  31.         {
  32.             return;
  33.         }
  34.         if (str_contains($printer->getName(), 'enkele bon'))
  35.         {
  36.             $event->stopPropagation();
  37.             return;
  38.         }
  39.         $orders $event->getOrders();
  40.         if ($printerOptions->onlyOnInvoice)
  41.         {
  42.             $foundInvoicePaymentOption false;
  43.             foreach ($orders as $order)
  44.             {
  45.                 if ($order->getSelectedPaymentOption() instanceof InvoicePaymentOption || $order->getSelectedPaymentOption() instanceof CashPaymentOption || $order->getSelectedPaymentOption() instanceof PinPaymentOption)
  46.                 {
  47.                     $foundInvoicePaymentOption true;
  48.                     break;
  49.                 }
  50.             }
  51.             if (!$foundInvoicePaymentOption)
  52.             {
  53.                 $event->stopPropagation();
  54.             }
  55.         }
  56.         if ($printerOptions->onlyOnDelivery)
  57.         {
  58.             $isDelivery false;
  59.             foreach ($orders as $order)
  60.             {
  61.                 $orderPickupDelivery $this->em->getRepository(OrderPickupDelivery::class)->find($order);
  62.                 if ($orderPickupDelivery !== null && $orderPickupDelivery->getDelivery())
  63.                 {
  64.                     $isDelivery true;
  65.                     break;
  66.                 }
  67.                 foreach ($order->getDiscounts() as $discount)
  68.                 {
  69.                     $code $discount->getDiscount()->getCode();
  70.                     if ($code !== null && strtolower($code) === 'kortingsb10pro')
  71.                     {
  72.                         $isDelivery true;
  73.                         break;
  74.                     }
  75.                 }
  76.             }
  77.             if (!$isDelivery)
  78.             {
  79.                 $event->stopPropagation();
  80.             }
  81.         }
  82.     }
  83. }