|
- <?php
-
- namespace Lc\CaracoleBundle\Controller\Order;
-
- use Lc\CaracoleBundle\Controller\AbstractController;
- use Lc\CaracoleBundle\Form\Order\OrderProductsType;
- use Lc\CaracoleBundle\Model\Order\OrderProductInterface;
- use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;
- use Symfony\Component\HttpFoundation\JsonResponse;
- use Symfony\Component\HttpFoundation\RedirectResponse;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\Routing\Annotation\Route;
-
- /**
- * @Route("/{section}/panier", name="frontend_cart_")
- */
- class CartController extends AbstractController
- {
- protected ProductFamilyInterface $productFamily;
- protected array $orderProducts = [];
-
- public function addProductFamily(Request $request): JsonResponse
- {
- $user = $this->getUserCurrent();
- $visitor = $this->getVisitorCurrent();
-
- $return = [];
- $data = $request->request->all();
-
- if (isset($data['order_products']['id_product_family'])) {
- $idProductFamily = $data['order_products']['id_product_family'];
- $this->productFamily = $this->getProductFamilyContainer()->getStore()->getOneById($idProductFamily);
-
- // alerte si cookies non acceptés
- if (!$user && !$visitor) {
- $this->addFlash(
- 'error',
- 'Vous devez <a href="' . $this->getRouter()->generate(
- 'frontend_page',
- ['devAlias' => 'politique-de-confidentialite']
- ) . '">accepter les cookies</a> ou vous <a href="' . $this->getRouter()->generate(
- 'fos_user_security_login'
- ) . '">connecter</a> pour ajouter un produit.'
- );
- return false;
- }
-
- if ($this->productFamily) {
- $form = $this->createForm(
- OrderProductsType::class,
- ['id_product_family' => $this->productFamily->getId()]
- );
- $form->handleRequest($request);
-
- if ($form->isSubmitted() && $form->isValid()) {
-
- $orderShop = $this->getOrderShopContainer()->getBuilder()->createIfNotExist(
- $this->getSectionCurrent(),
- $this->getUserCurrent(),
- $this->getVisitorCurrent()
- );
-
- $data = $form->getData();
- foreach ($data as $orderProduct) {
- if ($orderProduct instanceof OrderProductInterface) {
- if ($orderProduct->getQuantityOrder() > 0) {
- $addOrderProduct = $this->getOrderShopContainer()->getBuilder()->addOrderProduct(
- $orderShop,
- $orderProduct
- );
- }
- if (isset($addOrderProduct) && $addOrderProduct && $orderProduct->getQuantityOrder() > 0) {
- $this->orderProducts[] = $orderProduct;
- }
- }
- }
- }
- }
- }
-
- return new JsonResponse($return);
- }
-
- /**
- * @Route("/order-reduction-cart/delete/{id}", name="delete_reduction_cart")
- */
- public function deleteReductionCart(Request $request): RedirectResponse
- {
- $entityManager = $this->getEntityManager();
- $id = $request->get('id');
- $orderReductionCart = $this->getOrderReductionCartContainer()->getStore()->getOneById((int) $id);
- $orderShop = $this->getCartCurrent();
-
- if ($orderReductionCart && $orderShop->getOrderReductionCarts() && $orderShop->getOrderReductionCarts(
- )->contains($orderReductionCart)) {
- $entityManager->remove($orderReductionCart);
- $entityManager->flush();
-
- $this->addFlash('success', 'La réduction a bien été supprimée de votre panier.');
- } else {
- $this->addFlash('error', 'Une erreur est survenue lors de la suppression de la réduction. ');
- }
-
- return $this->redirectToReferer($request);
- }
-
- /**
- * @Route("/reduction-credit/add/{id}", name="order_reduction_credit")
- */
- public function addReductionCredit(Request $request): RedirectResponse
- {
- $id = $request->get('id');
- $orderShop = $this->getCartCurrent();
- $user = $this->getUserCurrent();
- $orderShopContainer = $this->getOrderShopContainer();
- $reductionCredit = $this->getReductionCreditContainer()->getStore()->getOneById($id);
-
- if ($orderShop && $user && $reductionCredit
- && $orderShopContainer->getStore()->isReductionCreditAllowAddToOrder($orderShop, $reductionCredit)
- && !$orderShopContainer->getSolver()->isReductionCreditAddedToOrder($orderShop, $reductionCredit)) {
- $return = $orderShopContainer->getBuilder()->addReductionCredit($orderShop, $reductionCredit);
-
- if ($return) {
- $this->addFlash('success', 'Votre avoir a bien été ajouté à votre panier.');
- } else {
- $this->addFlash(
- 'error',
- 'Vous ne pouvez pas effectuer cette action. Le montant de la commande est insuffisant.'
- );
- }
- } else {
- $this->addFlash('error', "Impossible d'effectuer cette action");
- }
-
- return $this->redirectToReferer($request);
- }
-
- /**
- * @Route("/order-reduction-credit/delete/{id}", name="delete_reduction_credit")
- */
- public function deleteReductionCredit(Request $request): RedirectResponse
- {
- $entityManager = $this->getEntityManager();
- $id = $request->get('id');
- $orderReductionCredit = $this->getOrderReductionCreditContainer()->getStore()->getOneById((int)$id);
- $orderShop = $this->getCartCurrent();
-
- if ($orderReductionCredit && $orderShop->getOrderReductionCredits() && $orderShop->getOrderReductionCredits(
- )->contains($orderReductionCredit)) {
- $entityManager->remove($orderReductionCredit);
- $entityManager->flush();
-
- $this->addFlash('success', 'Votre avoir a bien été supprimé de votre panier.');
- } else {
- $this->addFlash('error', 'Une erreur est survenue lors de la suppression de votre avoir. ');
- }
-
- $referer = $this->getReferer($request);
-
- if ($referer) {
- return $this->redirect($referer);
- } else {
- return $this->redirectToRoute('frontend_order_cart', [
- 'section' => $this->getSectionCurrentSlug()
- ]);
- }
- }
-
- protected function redirectToReferer(Request $request): RedirectResponse
- {
- $referer = $this->getReferer($request);
-
- if ($referer) {
- return $this->redirect($referer);
- } else {
- return $this->redirectToRoute('frontend_order_cart', [
- 'section' => $this->getSectionCurrentSlug()
- ]);
- }
- }
- }
|