182 lines
8.4KB

  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. $addOrderProduct = $this->orderUtils->addOrderProduct($orderShop, $orderProduct) ;
  66. if($addOrderProduct && $orderProduct->getQuantityOrder() > 0) {
  67. $this->orderProducts[] = $orderProduct ;
  68. }
  69. }
  70. }
  71. }
  72. }
  73. }
  74. return new JsonResponse($return) ;
  75. }
  76. public function redirectToReferer($request) {
  77. $referer = $request->headers->get('referer');
  78. if($referer) {
  79. return $this->redirect($referer);
  80. }
  81. else {
  82. return $this->redirectToRoute('frontend_order_cart');
  83. }
  84. }
  85. public function deleteReductionCart(Request $request)
  86. {
  87. $id = $request->get('id') ;
  88. $orderReductionCartRepository = $this->em->getRepository($this->em->getClassMetadata(OrderReductionCartInterface::class)->getName()) ;
  89. $orderReductionCart = $orderReductionCartRepository->findOneById((int) $id) ;
  90. $orderShop = $this->orderUtils->getCartCurrent() ;
  91. if($orderReductionCart && $orderShop->getOrderReductionCarts() && $orderShop->getOrderReductionCarts()->contains($orderReductionCart)) {
  92. $this->em->remove($orderReductionCart) ;
  93. $this->em->flush() ;
  94. $this->addFlash('success', 'La réduction a bien été supprimée de votre panier.') ;
  95. }
  96. else {
  97. $this->addFlash('error', 'Une erreur est survenue lors de la suppression de la réduction. ') ;
  98. }
  99. return $this->redirectToReferer($request) ;
  100. }
  101. public function addReductionCredit(Request $request)
  102. {
  103. $id = $request->get('id') ;
  104. $orderShop = $this->orderUtils->getCartCurrent() ;
  105. $user = $this->security->getUser() ;
  106. $reductionCreditRepository = $this->em->getRepository(ReductionCredit::class) ;
  107. $reductionCredit = $reductionCreditRepository->find($id) ;
  108. if($orderShop && $user && $reductionCredit
  109. && $this->orderUtils->isReductionCreditAllowAddToOrder($orderShop, $reductionCredit)
  110. && !$this->orderUtils->isReductionCreditAddedToOrder($orderShop, $reductionCredit)) {
  111. $return = $this->orderUtils->createOrderReductionCredit($orderShop, $reductionCredit) ;
  112. if($return) {
  113. $this->addFlash('success', 'Votre avoir a bien été ajouté à votre panier.') ;
  114. }
  115. else {
  116. $this->addFlash('error', 'Vous ne pouvez pas effectuer cette action. Le montant de la commande est insuffisant.') ;
  117. }
  118. }
  119. else {
  120. $this->addFlash('error', "Impossible d'effectuer cette action");
  121. }
  122. return $this->redirectToReferer($request) ;
  123. }
  124. public function deleteReductionCredit(Request $request)
  125. {
  126. $id = $request->get('id') ;
  127. $orderReductionCreditRepository = $this->em->getRepository($this->em->getClassMetadata(OrderReductionCreditInterface::class)->getName()) ;
  128. $orderReductionCredit = $orderReductionCreditRepository->findOneById((int) $id) ;
  129. $orderShop = $this->orderUtils->getCartCurrent() ;
  130. if($orderReductionCredit && $orderShop->getOrderReductionCredits() && $orderShop->getOrderReductionCredits()->contains($orderReductionCredit)) {
  131. $this->em->remove($orderReductionCredit) ;
  132. $this->em->flush() ;
  133. $this->addFlash('success', 'Votre avoir a bien été supprimé de votre panier.') ;
  134. }
  135. else {
  136. $this->addFlash('error', 'Une erreur est survenue lors de la suppression de votre avoir. ') ;
  137. }
  138. $referer = $request->headers->get('referer');
  139. if($referer) {
  140. return $this->redirect($referer);
  141. }
  142. else {
  143. return $this->redirectToRoute('frontend_order_cart');
  144. }
  145. }
  146. }