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.         $orders $event->getOrders();
  35.         if ($printerOptions->onlyOnInvoice)
  36.         {
  37.             $foundInvoicePaymentOption false;
  38.             foreach ($orders as $order)
  39.             {
  40.                 if ($order->getSelectedPaymentOption() instanceof InvoicePaymentOption || $order->getSelectedPaymentOption() instanceof CashPaymentOption || $order->getSelectedPaymentOption() instanceof PinPaymentOption)
  41.                 {
  42.                     $foundInvoicePaymentOption true;
  43.                     break;
  44.                 }
  45.             }
  46.             if (!$foundInvoicePaymentOption)
  47.             {
  48.                 $event->stopPropagation();
  49.             }
  50.         }
  51.         if ($printerOptions->onlyOnDelivery)
  52.         {
  53.             $isDelivery false;
  54.             foreach ($orders as $order)
  55.             {
  56.                 $orderPickupDelivery $this->em->getRepository(OrderPickupDelivery::class)->find($order);
  57.                 if ($orderPickupDelivery !== null && $orderPickupDelivery->getDelivery())
  58.                 {
  59.                     $isDelivery true;
  60.                     break;
  61.                 }
  62.             }
  63.             if (!$isDelivery)
  64.             {
  65.                 $event->stopPropagation();
  66.             }
  67.         }
  68.     }
  69. }