|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?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;
-
- 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="frontend_cart_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="frontend_order_cart_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="frontend_cart_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');
- }
- }
-
- protected function redirectToReferer(Request $request): RedirectResponse
- {
- $referer = $this->getReferer($request);
-
- if ($referer) {
- return $this->redirect($referer);
- } else {
- return $this->redirectToRoute('frontend_order_cart');
- }
- }
- }
|