<?php
namespace App\EventSubscriber;
use App\Payment\InvoicePaymentOption;
use App\Payment\CashPaymentOption;
use App\Payment\PinPaymentOption;
use App\Entity\OrderPickupDelivery;
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 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)
{
$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();
}
}
}
}