Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

95 lines
4.3KB

  1. <?php
  2. namespace Lc\ShopBundle\Controller\Frontend ;
  3. use App\Form\Frontend\OrderProductsType;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Lc\ShopBundle\Context\MerchantUtilsInterface;
  6. use Lc\ShopBundle\Context\OrderProductInterface;
  7. use Lc\ShopBundle\Context\OrderReductionCartInterface;
  8. use Lc\ShopBundle\Context\OrderUtilsInterface;
  9. use Lc\ShopBundle\Context\ProductFamilyInterface;
  10. use Lc\ShopBundle\Context\ReductionCartInterface;
  11. use Lc\ShopBundle\Model\OrderReductionCart;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\Security\Core\Security;
  15. class CartController extends BaseController
  16. {
  17. protected $orderUtils ;
  18. protected $productFamilyRepository ;
  19. protected $orderProductRepository ;
  20. protected $productFamily ;
  21. protected $orderProducts = [] ;
  22. public function __construct(EntityManagerInterface $em, Security $security, MerchantUtilsInterface $merchantUtils, OrderUtilsInterface $orderUtils)
  23. {
  24. parent::__construct($em, $security, $merchantUtils);
  25. $this->orderUtils = $orderUtils ;
  26. $this->productFamilyRepository = $this->em->getRepository($this->em->getClassMetaData(ProductFamilyInterface::class)->getName()) ;
  27. $this->orderProductRepository = $this->em->getRepository($this->em->getClassMetaData(OrderProductInterface::class)->getName()) ;
  28. }
  29. public function addProductFamily(Request $request)
  30. {
  31. $return = [] ;
  32. $data = $request->request->all() ;
  33. if(isset($data['order_products']['id_product_family'])) {
  34. $idProductFamily = $data['order_products']['id_product_family'] ;
  35. $this->productFamily = $this->productFamilyRepository->find($idProductFamily) ;
  36. if($this->productFamily) {
  37. $form = $this->createForm(OrderProductsType::class, ['id_product_family' => $this->productFamily->getId()]);
  38. $form->handleRequest($request);
  39. if ($form->isSubmitted() && $form->isValid()) {
  40. $orderShop = $this->orderUtils->getCartCurrent() ;
  41. $data = $form->getData() ;
  42. foreach($data as $orderProduct) {
  43. if($orderProduct instanceof OrderProductInterface) {
  44. $this->orderUtils->addOrderProduct($orderShop, $orderProduct) ;
  45. if($orderProduct->getQuantityOrder() > 0) {
  46. $this->orderProducts[] = $orderProduct ;
  47. }
  48. }
  49. }
  50. }
  51. }
  52. }
  53. return new JsonResponse($return) ;
  54. }
  55. public function deleteReductionCart(Request $request)
  56. {
  57. $id = $request->get('id') ;
  58. $orderReductionCartRepository = $this->em->getRepository($this->em->getClassMetadata(OrderReductionCartInterface::class)->getName()) ;
  59. $orderReductionCart = $orderReductionCartRepository->findOneById((int) $id) ;
  60. $orderShop = $this->orderUtils->getCartCurrent() ;
  61. if($orderReductionCart && $orderShop->getOrderReductionCarts() && $orderShop->getOrderReductionCarts()->contains($orderReductionCart)) {
  62. $this->em->remove($orderReductionCart) ;
  63. $this->em->flush() ;
  64. $this->addFlash('success', 'La réduction a bien été supprimée de votre panier.') ;
  65. }
  66. else {
  67. $this->addFlash('error', 'Une erreur est survenue lors de la suppression de la réduction. ') ;
  68. }
  69. $referer = $request->headers->get('referer');
  70. if($referer) {
  71. return $this->redirect($referer);
  72. }
  73. else {
  74. return $this->redirectToRoute('frontend_order_cart');
  75. }
  76. }
  77. }