173 líneas
8.0KB

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