CartController.php 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 $router ;
  28. protected $productFamilyRepository ;
  29. protected $orderProductRepository ;
  30. protected $productFamily ;
  31. protected $orderProducts = [] ;
  32. public function __construct(EntityManagerInterface $em, Security $security, MerchantUtilsInterface $merchantUtils,
  33. UrlGeneratorInterface $router, UtilsManager $utilsManager)
  34. {
  35. parent::__construct($em, $security, $merchantUtils);
  36. $this->orderUtils = $utilsManager->getOrderUtils() ;
  37. $this->userUtils = $utilsManager->getUserUtils() ;
  38. $this->priceUtils = $utilsManager->getPriceUtils() ;
  39. $this->router = $router ;
  40. $this->productFamilyRepository = $this->em->getRepository($this->em->getClassMetaData(ProductFamilyInterface::class)->getName()) ;
  41. $this->orderProductRepository = $this->em->getRepository($this->em->getClassMetaData(OrderProductInterface::class)->getName()) ;
  42. }
  43. public function addProductFamily(Request $request)
  44. {
  45. $user = $this->security->getUser() ;
  46. $visitor = $this->userUtils->getVisitorCurrent() ;
  47. $return = [] ;
  48. $data = $request->request->all() ;
  49. if(isset($data['order_products']['id_product_family'])) {
  50. $idProductFamily = $data['order_products']['id_product_family'] ;
  51. $this->productFamily = $this->productFamilyRepository->find($idProductFamily) ;
  52. // alerte si cookies non acceptés
  53. if (!$user && !$visitor) {
  54. $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.');
  55. return false ;
  56. }
  57. if($this->productFamily) {
  58. $form = $this->createForm(OrderProductsType::class, ['id_product_family' => $this->productFamily->getId()]);
  59. $form->handleRequest($request);
  60. if ($form->isSubmitted() && $form->isValid()) {
  61. $orderShop = $this->orderUtils->getCartCurrent() ;
  62. $data = $form->getData() ;
  63. foreach($data as $orderProduct) {
  64. if($orderProduct instanceof OrderProductInterface) {
  65. if($orderProduct->getQuantityOrder() > 0) {
  66. $addOrderProduct = $this->orderUtils->addOrderProduct($orderShop, $orderProduct) ;
  67. }
  68. if(isset($addOrderProduct) && $addOrderProduct && $orderProduct->getQuantityOrder() > 0) {
  69. $this->orderProducts[] = $orderProduct ;
  70. }
  71. }
  72. }
  73. }
  74. }
  75. }
  76. return new JsonResponse($return) ;
  77. }
  78. public function redirectToReferer($request) {
  79. $referer = $request->headers->get('referer');
  80. if($referer) {
  81. return $this->redirect($referer);
  82. }
  83. else {
  84. return $this->redirectToRoute('frontend_order_cart');
  85. }
  86. }
  87. public function deleteReductionCart(Request $request)
  88. {
  89. $id = $request->get('id') ;
  90. $orderReductionCartRepository = $this->em->getRepository($this->em->getClassMetadata(OrderReductionCartInterface::class)->getName()) ;
  91. $orderReductionCart = $orderReductionCartRepository->findOneById((int) $id) ;
  92. $orderShop = $this->orderUtils->getCartCurrent() ;
  93. if($orderReductionCart && $orderShop->getOrderReductionCarts() && $orderShop->getOrderReductionCarts()->contains($orderReductionCart)) {
  94. $this->em->remove($orderReductionCart) ;
  95. $this->em->flush() ;
  96. $this->addFlash('success', 'La réduction a bien été supprimée de votre panier.') ;
  97. }
  98. else {
  99. $this->addFlash('error', 'Une erreur est survenue lors de la suppression de la réduction. ') ;
  100. }
  101. return $this->redirectToReferer($request) ;
  102. }
  103. public function addReductionCredit(Request $request)
  104. {
  105. $id = $request->get('id') ;
  106. $orderShop = $this->orderUtils->getCartCurrent() ;
  107. $user = $this->security->getUser() ;
  108. $reductionCreditRepository = $this->em->getRepository(ReductionCredit::class) ;
  109. $reductionCredit = $reductionCreditRepository->find($id) ;
  110. if($orderShop && $user && $reductionCredit
  111. && $this->orderUtils->isReductionCreditAllowAddToOrder($orderShop, $reductionCredit)
  112. && !$this->orderUtils->isReductionCreditAddedToOrder($orderShop, $reductionCredit)) {
  113. $return = $this->orderUtils->createOrderReductionCredit($orderShop, $reductionCredit) ;
  114. if($return) {
  115. $this->addFlash('success', 'Votre avoir a bien été ajouté à votre panier.') ;
  116. }
  117. else {
  118. $this->addFlash('error', 'Vous ne pouvez pas effectuer cette action. Le montant de la commande est insuffisant.') ;
  119. }
  120. }
  121. else {
  122. $this->addFlash('error', "Impossible d'effectuer cette action");
  123. }
  124. return $this->redirectToReferer($request) ;
  125. }
  126. public function deleteReductionCredit(Request $request)
  127. {
  128. $id = $request->get('id') ;
  129. $orderReductionCreditRepository = $this->em->getRepository($this->em->getClassMetadata(OrderReductionCreditInterface::class)->getName()) ;
  130. $orderReductionCredit = $orderReductionCreditRepository->findOneById((int) $id) ;
  131. $orderShop = $this->orderUtils->getCartCurrent() ;
  132. if($orderReductionCredit && $orderShop->getOrderReductionCredits() && $orderShop->getOrderReductionCredits()->contains($orderReductionCredit)) {
  133. $this->em->remove($orderReductionCredit) ;
  134. $this->em->flush() ;
  135. $this->addFlash('success', 'Votre avoir a bien été supprimé de votre panier.') ;
  136. }
  137. else {
  138. $this->addFlash('error', 'Une erreur est survenue lors de la suppression de votre avoir. ') ;
  139. }
  140. $referer = $request->headers->get('referer');
  141. if($referer) {
  142. return $this->redirect($referer);
  143. }
  144. else {
  145. return $this->redirectToRoute('frontend_order_cart');
  146. }
  147. }
  148. }