<?phpnamespace App\EventSubscriber;use App\Entity\CustomerOptions;use App\Payment\InvoicePaymentOption;use App\Payment\CashPaymentOption;use App\Payment\PinPaymentOption;use App\Entity\OrderPickupDelivery;use App\Repository\CustomerOptionsRepository;use App\Repository\PrinterOptionsRepository;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Boldr\Shop\ReceiptPrinterBundle\Event\QueueReceiptEvent;use Doctrine\ORM\EntityManagerInterface;class ReceiptEventSubscriber implements EventSubscriberInterface{ public function __construct( private readonly PrinterOptionsRepository $printerOptionsRepository, private readonly CustomerOptionsRepository $customerOptionsRepository, private readonly EntityManagerInterface $em ) {} public static function getSubscribedEvents() { return [ QueueReceiptEvent::class => [ ['onQueueReceipt', 10] ] ]; } public function onQueueReceipt(QueueReceiptEvent $event) { $printer = $event->getPrinter(); $printerOptions = $this->printerOptionsRepository->find($printer); // If there are no HB-specific printer options for this printer, ignore if ($printerOptions === null) { return; } if (str_contains($printer->getName(), 'enkele bon')) { $event->stopPropagation(); return; } $orders = $event->getOrders(); if ($printerOptions->onlyOnInvoice) { $foundInvoicePaymentOption = false; foreach ($orders as $order) { if ($order->getSelectedPaymentOption() instanceof InvoicePaymentOption || $order->getSelectedPaymentOption() instanceof CashPaymentOption || $order->getSelectedPaymentOption() instanceof PinPaymentOption) { /** @var CustomerOptions|null */ $customerOptions = $this->customerOptionsRepository->find($order->getCustomer()); if ($customerOptions === null || !$customerOptions->geenExtraBon) { $foundInvoicePaymentOption = true; break; } } } if (!$foundInvoicePaymentOption) { $event->stopPropagation(); } } if ($printerOptions->onlyOnDelivery) { $isDelivery = false; foreach ($orders as $order) { $orderPickupDelivery = $this->em->getRepository(OrderPickupDelivery::class)->find($order); if ($orderPickupDelivery !== null && $orderPickupDelivery->getDelivery()) { $isDelivery = true; break; } foreach ($order->getDiscounts() as $discount) { $code = $discount->getDiscount()->getCode(); if ($code !== null && strtolower($code) === 'kortingsb10pro') { $isDelivery = true; break; } } } if (!$isDelivery) { $event->stopPropagation(); } } }}