Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

188 lines
8.8KB

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