No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

229 líneas
8.7KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Controller\Order;
  3. use Lc\CaracoleBundle\Controller\AbstractController;
  4. use Lc\CaracoleBundle\Form\Order\OrderProductsType;
  5. use Lc\CaracoleBundle\Model\Order\OrderProductInterface;
  6. use Lc\CaracoleBundle\Model\Order\OrderShopInterface;
  7. use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. use Symfony\Component\HttpFoundation\RedirectResponse;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. /**
  13. * @Route("/{section}/panier", name="frontend_cart_")
  14. */
  15. class CartController extends AbstractController
  16. {
  17. protected ?ProductFamilyInterface $productFamily = null;
  18. protected int $quantityOrder = 1;
  19. protected array $orderProducts = [];
  20. public function addProductFamily(Request $request): JsonResponse
  21. {
  22. $user = $this->getUserCurrent();
  23. $visitor = $this->getVisitorCurrent();
  24. $return = [];
  25. $data = $request->request->all();
  26. if (isset($data['order_products']['id_product_family'])) {
  27. $idProductFamily = (int) $data['order_products']['id_product_family'];
  28. $this->productFamily = $this->getProductFamilyContainer()->getStore()->getOneById($idProductFamily);
  29. if(!$this->productFamily) {
  30. $return['return'] = 'error';
  31. }
  32. // alerte si cookies non acceptés
  33. if (!$user && !$visitor) {
  34. $this->addFlash(
  35. 'error',
  36. 'Vous devez <a href="' . $this->getRouter()->generate(
  37. 'frontend_page',
  38. ['devAlias' => 'politique-de-confidentialite']
  39. ) . '">accepter les cookies</a> ou vous <a href="' . $this->getRouter()->generate(
  40. 'fos_user_security_login'
  41. ) . '">connecter</a> pour ajouter un produit.'
  42. );
  43. $return['return'] = 'error';
  44. }
  45. if ($this->productFamily) {
  46. $form = $this->createForm(
  47. OrderProductsType::class,
  48. ['id_product_family' => $this->productFamily->getId()]
  49. );
  50. $form->handleRequest($request);
  51. if ($form->isSubmitted() && $form->isValid()) {
  52. $orderShop = $this->getOrderShopContainer()->getBuilder()->createIfNotExist(
  53. $this->getSectionCurrent(),
  54. $this->getUserCurrent(),
  55. $this->getVisitorCurrent()
  56. );
  57. $data = $form->getData();
  58. foreach ($data as $orderProduct) {
  59. if ($orderProduct instanceof OrderProductInterface) {
  60. $this->addOrderProduct($orderShop, $orderProduct);
  61. }
  62. }
  63. $return['return'] = 'success';
  64. }
  65. }
  66. }
  67. return new JsonResponse($return);
  68. }
  69. public function addOrderProduct(OrderShopInterface $orderShop, OrderProductInterface $orderProduct): void
  70. {
  71. $this->quantityOrder = $orderProduct->getQuantityOrder();
  72. $orderProductArray = $this->getOrderShopContainer()->getBuilder()->addOrderProduct(
  73. $orderShop,
  74. $orderProduct
  75. );
  76. if (is_array($orderProductArray)) {
  77. foreach($orderProductArray as $orderProduct) {
  78. $this->orderProducts[] = $orderProduct;
  79. }
  80. }
  81. }
  82. /**
  83. * @Route("/reduction-cart/add/{id}", name="order_reduction_cart")
  84. */
  85. public function addReductionCart(Request $request): RedirectResponse
  86. {
  87. $id = $request->get('id');
  88. $orderShop = $this->getCartCurrent();
  89. $user = $this->getUserCurrent();
  90. $orderShopContainer = $this->getOrderShopContainer();
  91. $reductionCart = $this->getReductionCartContainer()->getStore()->getOneById($id);
  92. if ($orderShop && $user && $reductionCart
  93. && $orderShopContainer->getStore()->isReductionCartAllowAddToOrder($orderShop, $reductionCart)
  94. && !$orderShopContainer->getSolver()->isReductionCartAddedToOrder($orderShop, $reductionCart)) {
  95. $return = $orderShopContainer->getBuilder()->addReductionCart($orderShop, $reductionCart);
  96. if ($return) {
  97. $this->addFlash('success', 'Votre réduction panier a bien été ajoutée à votre panier.');
  98. } else {
  99. $this->addFlash(
  100. 'error',
  101. 'Vous ne pouvez pas effectuer cette action. Le montant de la commande est insuffisant.'
  102. );
  103. }
  104. } else {
  105. $this->addFlash('error', "Impossible d'effectuer cette action");
  106. }
  107. return $this->redirectToReferer($request);
  108. }
  109. /**
  110. * @Route("/order-reduction-cart/delete/{id}", name="delete_reduction_cart")
  111. */
  112. public function deleteReductionCart(Request $request): RedirectResponse
  113. {
  114. $entityManager = $this->getEntityManager();
  115. $id = $request->get('id');
  116. $orderReductionCart = $this->getOrderReductionCartContainer()->getStore()->getOneById((int) $id);
  117. $orderShop = $this->getCartCurrent();
  118. if ($orderReductionCart && $orderShop->getOrderReductionCarts() && $orderShop->getOrderReductionCarts(
  119. )->contains($orderReductionCart)) {
  120. $entityManager->remove($orderReductionCart);
  121. $entityManager->flush();
  122. $this->addFlash('success', 'La réduction a bien été supprimée de votre panier.');
  123. } else {
  124. $this->addFlash('error', 'Une erreur est survenue lors de la suppression de la réduction. ');
  125. }
  126. return $this->redirectToReferer($request);
  127. }
  128. /**
  129. * @Route("/reduction-credit/add/{id}", name="order_reduction_credit")
  130. */
  131. public function addReductionCredit(Request $request): RedirectResponse
  132. {
  133. $id = $request->get('id');
  134. $orderShop = $this->getCartCurrent();
  135. $user = $this->getUserCurrent();
  136. $orderShopContainer = $this->getOrderShopContainer();
  137. $reductionCredit = $this->getReductionCreditContainer()->getStore()->getOneById($id);
  138. if ($orderShop && $user && $reductionCredit
  139. && $orderShopContainer->getStore()->isReductionCreditAllowAddToOrder($orderShop, $reductionCredit)
  140. && !$orderShopContainer->getSolver()->isReductionCreditAddedToOrder($orderShop, $reductionCredit)) {
  141. $return = $orderShopContainer->getBuilder()->addReductionCredit($orderShop, $reductionCredit);
  142. if ($return) {
  143. $this->addFlash('success', 'Votre avoir a bien été ajouté à votre panier.');
  144. } else {
  145. $this->addFlash(
  146. 'error',
  147. 'Vous ne pouvez pas effectuer cette action. Le montant de la commande est insuffisant.'
  148. );
  149. }
  150. } else {
  151. $this->addFlash('error', "Impossible d'effectuer cette action");
  152. }
  153. return $this->redirectToReferer($request);
  154. }
  155. /**
  156. * @Route("/order-reduction-credit/delete/{id}", name="delete_reduction_credit")
  157. */
  158. public function deleteReductionCredit(Request $request): RedirectResponse
  159. {
  160. $entityManager = $this->getEntityManager();
  161. $id = $request->get('id');
  162. $orderReductionCredit = $this->getOrderReductionCreditContainer()->getStore()->getOneById((int)$id);
  163. $orderShop = $this->getCartCurrent();
  164. if ($orderReductionCredit && $orderShop->getOrderReductionCredits() && $orderShop->getOrderReductionCredits(
  165. )->contains($orderReductionCredit)) {
  166. $entityManager->remove($orderReductionCredit);
  167. $entityManager->flush();
  168. $this->addFlash('success', 'Votre avoir a bien été supprimé de votre panier.');
  169. } else {
  170. $this->addFlash('error', 'Une erreur est survenue lors de la suppression de votre avoir. ');
  171. }
  172. $referer = $this->getReferer($request);
  173. if ($referer) {
  174. return $this->redirect($referer);
  175. } else {
  176. return $this->redirectToRoute('frontend_order_cart', [
  177. 'section' => $this->getSectionCurrentSlug()
  178. ]);
  179. }
  180. }
  181. protected function redirectToReferer(Request $request): RedirectResponse
  182. {
  183. $referer = $this->getReferer($request);
  184. if ($referer) {
  185. return $this->redirect($referer);
  186. } else {
  187. return $this->redirectToRoute('frontend_order_cart', [
  188. 'section' => $this->getSectionCurrentSlug()
  189. ]);
  190. }
  191. }
  192. }