<?php
namespace App\Controller;
use App\Entity\Cart;
use App\Entity\Setting;
use App\Form\CartType;
use App\Repository\AddonRepository;
use App\Repository\CartRepository;
use App\Repository\ProductRepository;
use App\Repository\SettingRepository;
use App\Repository\SubscriptionPlanPriceRepository;
use App\Service\CartService;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Routing\Annotation\Route;
class CartController extends AbstractController
{
private string $sessionId;
private Session $session;
public function __construct()
{
$session = new Session();
$this->session = $session;
$this->sessionId = $session->getId();
}
#[Route(path: [
'de' => '/de/mein-einkaufswagen',
'en' => '/en/my-cart',
'es' => '/es/mi-carreton'
], name: 'app_cart_index', methods: ['GET'])]
public function index(CartRepository $cartRepository, SettingRepository $settingRepository): Response
{
$session = new Session();
$session->start();
$this->sessionId = $session->getId();
$itemsInCart = $cartRepository->findBy(['session_id' => $this->sessionId]);
if ($this->getUser()) {
$itemsInCart = $cartRepository->findBy(['client' => $this->getUser()]);
}
/**
* @var Setting $settings
*/
$settings = $settingRepository->findSettings();
return $this->render('shop/cart/index.html.twig', [
'carts' => $itemsInCart,
'session_id' => $this->sessionId,
'vat_value' => $settings->getVat(),
]);
}
#[Route('/cart/new', name: 'app_cart_new', methods: ['GET', 'POST'])]
public function new(
Request $request,
CartRepository $cartRepository,
ProductRepository $productRepository,
SubscriptionPlanPriceRepository $subscriptionPlanPriceRepository,
AddonRepository $addonRepository,
EntityManagerInterface $entityManager
): Response {
$cart = new Cart();
$productId = $_REQUEST['product_id'];
$planId = $_REQUEST['plan'];
$plan = $subscriptionPlanPriceRepository->findOneBy(['subscription_plan' => $planId, 'product' => $productId]);
$product = $productRepository->find($productId);
$subscriptionPlanPrices = $subscriptionPlanPriceRepository->findBy(['product' => $product], []);
$amountSubscriptionPlanPrices = count($subscriptionPlanPrices);
$addons = $addonRepository->findBy(['product' => $product]);
$amountAddons = count($addons);
$form = $this->createForm(CartType::class, $cart);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
//dd($request->request);
$plan = $subscriptionPlanPriceRepository->find($request->request->get('plan'));
$basePrice = $plan->getSinglePriceMonthUser();
$totalPrice = $basePrice * $form['amount_users']->getData();
$cart->setProduct($product);
$cart->setAmountUsers($form['amount_users']->getData());
$cart->setTotalPriceMonth($totalPrice);
$cart->setSessionId($this->sessionId);
$cart->setClient($this->getUser() ?? null);
$cart->setRegDatetime(new DateTime());
$cartRepository->add($cart, true);
//Addons
$addons = $request->request->get('order');
if ($addons) {
foreach ($addons as $addon) {
$cart = new Cart();
$addonObject = $addonRepository->find($addon['addon']);
$basePrice = $addonObject->getSinglePriceMonthUser();
$totalPrice = $basePrice * $form['amount_users']->getData();
$cart->setAmountUsers($form['amount_users']->getData());
$cart->setTotalPriceMonth($totalPrice);
$cart->setProduct($product);
$cart->setAddon($addonObject);
$cart->setSessionId($this->sessionId);
$cart->setClient($this->getUser() ?? null);
$cart->setRegDatetime(new DateTime());
$entityManager->persist($cart);
$entityManager->flush();
}
}
$this->session->set('oldSessionId', $this->sessionId);
return $this->redirectToRoute('app_cart_index', [], Response::HTTP_SEE_OTHER);
}
$basePrice = $plan->getSinglePriceMonthUser();
$totalPrice = $basePrice * $plan->getMinUser();
return $this->renderForm('shop/cart/new.html.twig', [
'cart' => $cart,
'form' => $form,
'min' => $plan->getMinUser(),
'max' => $plan->getMaxUser(),
'base_price' => $basePrice,
'total_price' => $totalPrice,
'session_id' => $this->sessionId,
'product' => $product->getProductName(),
'product_id' => $product->getId(),
'plan' => $plan->getId(),
'addons' => $addons,
'amount_addons' => $amountAddons,
'amount_plans' => $amountSubscriptionPlanPrices,
]);
}
#[Route('/cart/{id}', name: 'app_cart_show', methods: ['GET'])]
public function show(Cart $cart): Response
{
return $this->render('shop/cart/show.html.twig', [
'cart' => $cart,
]);
}
#[Route('/cart/{id}/edit', name: 'app_cart_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, Cart $cart, CartRepository $cartRepository, EntityManagerInterface $entityManager, CartService $cartService): Response
{
$form = $this->createForm(CartType::class, $cart);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$amountUsers = $form['amount_users']->getData();
$basePriceProductMonthUser = $cartService->getBasePriceMonthUser($cart->getProduct(), $amountUsers);
$productsAndAddons = $cartRepository->findBy(['session_id' => $cart->getSessionId(), 'product' => $cart->getProduct()], ['id' => 'ASC']);
if ($cart->getClient()) {
$productsAndAddons = $cartRepository->findBy(['client' => $cart->getClient(), 'product' => $cart->getProduct()], ['id' => 'ASC']);
}
foreach ($productsAndAddons as $item) {
$basePrice = $basePriceProductMonthUser;
if ($item->getAddon()) {
$basePrice = $cartService->getBasePriceAddonMonthUser($item->getAddon());
}
$totalPriceMonth = $basePrice * $amountUsers;
$item->setAmountUsers($amountUsers);
$item->setTotalPriceMonth($totalPriceMonth);
$entityManager->persist($item);
}
$entityManager->flush();
return $this->redirectToRoute('app_cart_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('shop/cart/edit.html.twig', [
'cart' => $cart,
'form' => $form,
]);
}
#[Route('/cart/{id}/delete', name: 'app_cart_delete', methods: ['GET', 'POST'])]
public function delete($id, CartRepository $cartRepository): Response
{
$productId = $_REQUEST['product'];
$cartData = $cartRepository->findBy(['session_id' => $id, 'product' => $productId]);
foreach ($cartData as $cart) {
$cartRepository->remove($cart, true);
}
return $this->redirectToRoute('app_cart_index', [], Response::HTTP_SEE_OTHER);
}
#[Route('/cart/{id}/delete-addon', name: 'app_cart_delete_addon', methods: ['GET', 'POST'])]
public function deleteAddon($id, CartRepository $cartRepository): Response
{
$cart = $cartRepository->find($id);
$cartRepository->remove($cart, true);
return $this->redirectToRoute('app_cart_index', [], Response::HTTP_SEE_OTHER);
}
}