You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
7.9KB

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