Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

CartController.php 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace Lc\ShopBundle\Controller\Frontend ;
  3. use App\Entity\ReductionCredit;
  4. use App\Form\Frontend\OrderProductsType;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Lc\ShopBundle\Context\MerchantUtilsInterface;
  7. use Lc\ShopBundle\Context\OrderProductInterface;
  8. use Lc\ShopBundle\Context\OrderReductionCartInterface;
  9. use Lc\ShopBundle\Context\OrderReductionCreditInterface;
  10. use Lc\ShopBundle\Context\OrderUtilsInterface;
  11. use Lc\ShopBundle\Context\ProductFamilyInterface;
  12. use Lc\ShopBundle\Context\ReductionCartInterface;
  13. use Lc\ShopBundle\Model\OrderReductionCart;
  14. use Lc\ShopBundle\Model\ProductFamily;
  15. use Lc\ShopBundle\Services\UserUtils;
  16. use Lc\ShopBundle\Services\UtilsManager;
  17. use Symfony\Component\HttpFoundation\JsonResponse;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\Routing\Generator\UrlGenerator;
  20. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  21. use Symfony\Component\Security\Core\Security;
  22. class CartController extends BaseController
  23. {
  24. protected $orderUtils ;
  25. protected $userUtils ;
  26. protected $priceUtils ;
  27. protected $sectionUtils ;
  28. protected $router ;
  29. protected $productFamilyRepository ;
  30. protected $orderProductRepository ;
  31. protected $productFamily ;
  32. protected $orderProducts = [] ;
  33. public function __construct(EntityManagerInterface $em, Security $security, MerchantUtilsInterface $merchantUtils,
  34. UrlGeneratorInterface $router, UtilsManager $utilsManager)
  35. {
  36. parent::__construct($em, $security, $merchantUtils);
  37. $this->orderUtils = $utilsManager->getOrderUtils() ;
  38. $this->userUtils = $utilsManager->getUserUtils() ;
  39. $this->priceUtils = $utilsManager->getPriceUtils() ;
  40. $this->sectionUtils = $utilsManager->getSectionUtils() ;
  41. $this->router = $router ;
  42. $this->productFamilyRepository = $this->em->getRepository($this->em->getClassMetaData(ProductFamilyInterface::class)->getName()) ;
  43. $this->orderProductRepository = $this->em->getRepository($this->em->getClassMetaData(OrderProductInterface::class)->getName()) ;
  44. }
  45. public function addProductFamily(Request $request)
  46. {
  47. $user = $this->security->getUser() ;
  48. $visitor = $this->userUtils->getVisitorCurrent() ;
  49. $return = [] ;
  50. $data = $request->request->all() ;
  51. if(isset($data['order_products']['id_product_family'])) {
  52. $idProductFamily = $data['order_products']['id_product_family'] ;
  53. $this->productFamily = $this->productFamilyRepository->find($idProductFamily) ;
  54. // alerte si cookies non acceptés
  55. if (!$user && !$visitor) {
  56. $this->addFlash('error', 'Vous devez <a href="'.$this->router->generate('frontend_page', ['devAlias' => 'politique-de-confidentialite']).'">accepter les cookies</a> ou vous <a href="'.$this->router->generate('fos_user_security_login').'">connecter</a> pour ajouter un produit.');
  57. return false ;
  58. }
  59. if($this->productFamily) {
  60. $form = $this->createForm(OrderProductsType::class, ['id_product_family' => $this->productFamily->getId()]);
  61. $form->handleRequest($request);
  62. if ($form->isSubmitted() && $form->isValid()) {
  63. $orderShop = $this->orderUtils->getCartCurrent(true) ;
  64. $data = $form->getData() ;
  65. foreach($data as $orderProduct) {
  66. if($orderProduct instanceof OrderProductInterface) {
  67. if($orderProduct->getQuantityOrder() > 0) {
  68. $addOrderProduct = $this->orderUtils->addOrderProduct($orderShop, $orderProduct) ;
  69. }
  70. if(isset($addOrderProduct) && $addOrderProduct && $orderProduct->getQuantityOrder() > 0) {
  71. $this->orderProducts[] = $orderProduct ;
  72. }
  73. }
  74. }
  75. }
  76. }
  77. }
  78. return new JsonResponse($return) ;
  79. }
  80. public function redirectToReferer($request) {
  81. $referer = $request->headers->get('referer');
  82. if($referer) {
  83. return $this->redirect($referer);
  84. }
  85. else {
  86. return $this->redirectToRoute('frontend_order_cart');
  87. }
  88. }
  89. public function deleteReductionCart(Request $request)
  90. {
  91. $id = $request->get('id') ;
  92. $orderReductionCartRepository = $this->em->getRepository($this->em->getClassMetadata(OrderReductionCartInterface::class)->getName()) ;
  93. $orderReductionCart = $orderReductionCartRepository->findOneById((int) $id) ;
  94. $orderShop = $this->orderUtils->getCartCurrent() ;
  95. if($orderReductionCart && $orderShop->getOrderReductionCarts() && $orderShop->getOrderReductionCarts()->contains($orderReductionCart)) {
  96. $this->em->remove($orderReductionCart) ;
  97. $this->em->flush() ;
  98. $this->addFlash('success', 'La réduction a bien été supprimée de votre panier.') ;
  99. }
  100. else {
  101. $this->addFlash('error', 'Une erreur est survenue lors de la suppression de la réduction. ') ;
  102. }
  103. return $this->redirectToReferer($request) ;
  104. }
  105. public function addReductionCredit(Request $request)
  106. {
  107. $id = $request->get('id') ;
  108. $orderShop = $this->orderUtils->getCartCurrent() ;
  109. $user = $this->security->getUser() ;
  110. $reductionCreditRepository = $this->em->getRepository(ReductionCredit::class) ;
  111. $reductionCredit = $reductionCreditRepository->find($id) ;
  112. if($orderShop && $user && $reductionCredit
  113. && $this->orderUtils->isReductionCreditAllowAddToOrder($orderShop, $reductionCredit)
  114. && !$this->orderUtils->isReductionCreditAddedToOrder($orderShop, $reductionCredit)) {
  115. $return = $this->orderUtils->createOrderReductionCredit($orderShop, $reductionCredit) ;
  116. if($return) {
  117. $this->addFlash('success', 'Votre avoir a bien été ajouté à votre panier.') ;
  118. }
  119. else {
  120. $this->addFlash('error', 'Vous ne pouvez pas effectuer cette action. Le montant de la commande est insuffisant.') ;
  121. }
  122. }
  123. else {
  124. $this->addFlash('error', "Impossible d'effectuer cette action");
  125. }
  126. return $this->redirectToReferer($request) ;
  127. }
  128. public function deleteReductionCredit(Request $request)
  129. {
  130. $id = $request->get('id') ;
  131. $orderReductionCreditRepository = $this->em->getRepository($this->em->getClassMetadata(OrderReductionCreditInterface::class)->getName()) ;
  132. $orderReductionCredit = $orderReductionCreditRepository->findOneById((int) $id) ;
  133. $orderShop = $this->orderUtils->getCartCurrent() ;
  134. if($orderReductionCredit && $orderShop->getOrderReductionCredits() && $orderShop->getOrderReductionCredits()->contains($orderReductionCredit)) {
  135. $this->em->remove($orderReductionCredit) ;
  136. $this->em->flush() ;
  137. $this->addFlash('success', 'Votre avoir a bien été supprimé de votre panier.') ;
  138. }
  139. else {
  140. $this->addFlash('error', 'Une erreur est survenue lors de la suppression de votre avoir. ') ;
  141. }
  142. $referer = $request->headers->get('referer');
  143. if($referer) {
  144. return $this->redirect($referer);
  145. }
  146. else {
  147. return $this->redirectToRoute('frontend_order_cart');
  148. }
  149. }
  150. }