src/EventSubscriber/ReceiptEventSubscriber.php line 34

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