<?php
namespace App\EventListener;
use App\Service\MailerService;
use ApiPlatform\Core\EventListener\EventPriorities;
use App\Entity\OrderProduct;
use App\Entity\Orders;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
class OrderCreatedListener implements EventSubscriberInterface
{
private $mailerService;
private EntityManagerInterface $entityManager;
private $validator;
public function __construct(MailerService $mailerService, EntityManagerInterface $entityManager, ValidatorInterface $validator)
{
$this->mailerService = $mailerService;
$this->entityManager = $entityManager;
$this->validator = $validator;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => ['updateOrdersSum', EventPriorities::POST_WRITE],
KernelEvents::CONTROLLER => 'onControllerEvent',
];
}
public function updateOrdersSum(ViewEvent $event)
{
$order = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
if (!$order instanceof Orders AND ( $method == 'POST' OR $method == 'PUT')) {
return;
}
// $order = $this->entityManager->getRepository(Orders::class)->findOneBy(['id' => $MainOrder->getOrder()->getId()]);
// $order->setSynced(false);
// $this->entityManager->flush();
}
public function onControllerEvent(ControllerEvent $event)
{
$controller = $event->getController();
$request = $event->getRequest();
$method = $request->getMethod();
$id = $request->attributes->get('id');
// var_dump($id);
if($method == 'DELETE'){
$orderProduct = $this->entityManager->getRepository(OrderProduct::class)->findOneBy(['id' => $id]);
if(!is_null( $orderProduct ) AND !is_null( $orderProduct->getOrder() ))
$this->countSumOrdered($orderProduct->getOrder()->getId(), $id );
}
// echo( $controller instanceof); die;
// Перевірка, що контролер є екземпляром вашого контролера, який обробляє видалення
// if ($controller instanceof ) {
// // Ваша логіка для обробки видалення OrderProduct
// }
}
public function countSumOrdered($id, $delete_id=false){
$order = $this->entityManager->getRepository(Orders::class)->findOneBy(['id' => $id]);
$orderProducts = $this->entityManager->getRepository(OrderProduct::class)->findBy(['order' => $id]);
// var_dump($delete_id);
$totalSumOrder = 0;
foreach($orderProducts as $orderProduct){
// var_dump($orderProduct->getId());
if($delete_id AND $delete_id == $orderProduct->getId()){
continue;
}
$count = (float) $orderProduct->getCount();
$price = (float) $orderProduct->getPriceForOne();
$discount = (float) $orderProduct->getDiscount();
$totalSum = ($count * $price) - $discount;
$totalSumOrder += round($totalSum, 2);
}
// $order->setDescription($order->getId());
$order->setSumOrdered((float)$totalSumOrder);
$this->entityManager->flush();
// die;
}
}