Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

235 rindas
9.1KB

  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 float $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. // sécurité liée à la possibilité de saisir des quantités en float pour les comptes ventes complémentaires
  61. if(!$orderShop->getUser() || !$orderShop->getUser()->getIsAdditionalSalesAccount()) {
  62. $orderProduct->setQuantityOrder(intval($orderProduct->getQuantityOrder()));
  63. }
  64. $this->addOrderProduct($orderShop, $orderProduct);
  65. }
  66. }
  67. $return['return'] = 'success';
  68. }
  69. }
  70. }
  71. return new JsonResponse($return);
  72. }
  73. public function addOrderProduct(OrderShopInterface $orderShop, OrderProductInterface $orderProduct): void
  74. {
  75. $this->quantityOrder = $orderProduct->getQuantityOrder();
  76. $orderProductArray = $this->getOrderShopContainer()->getBuilder()->addOrderProduct(
  77. $orderShop,
  78. $orderProduct
  79. );
  80. if (is_array($orderProductArray)) {
  81. foreach($orderProductArray as $orderProduct) {
  82. $this->orderProducts[] = $orderProduct;
  83. }
  84. }
  85. }
  86. /**
  87. * @Route("/reduction-cart/add/{id}", name="order_reduction_cart")
  88. */
  89. public function addReductionCart(Request $request): RedirectResponse
  90. {
  91. $id = $request->get('id');
  92. $orderShop = $this->getCartCurrent();
  93. $user = $this->getUserCurrent();
  94. $orderShopContainer = $this->getOrderShopContainer();
  95. $reductionCart = $this->getReductionCartContainer()->getStore()->getOneById($id);
  96. if ($orderShop && $user && $reductionCart
  97. && $orderShopContainer->getStore()->isReductionCartAllowAddToOrder($orderShop, $reductionCart)
  98. && !$orderShopContainer->getSolver()->isReductionCartAddedToOrder($orderShop, $reductionCart)) {
  99. $return = $orderShopContainer->getBuilder()->addReductionCart($orderShop, $reductionCart);
  100. if ($return) {
  101. $this->addFlash('success', 'Votre réduction panier a bien été ajoutée à votre panier.');
  102. } else {
  103. $this->addFlash(
  104. 'error',
  105. 'Vous ne pouvez pas effectuer cette action. Le montant de la commande est insuffisant.'
  106. );
  107. }
  108. } else {
  109. $this->addFlash('error', "Impossible d'effectuer cette action");
  110. }
  111. return $this->redirectToReferer($request);
  112. }
  113. /**
  114. * @Route("/order-reduction-cart/delete/{id}", name="delete_reduction_cart")
  115. */
  116. public function deleteReductionCart(Request $request): RedirectResponse
  117. {
  118. $entityManager = $this->getEntityManager();
  119. $id = $request->get('id');
  120. $orderReductionCart = $this->getOrderReductionCartContainer()->getStore()->getOneById((int) $id);
  121. $orderShop = $this->getCartCurrent();
  122. if ($orderReductionCart && $orderShop->getOrderReductionCarts() && $orderShop->getOrderReductionCarts(
  123. )->contains($orderReductionCart)) {
  124. $entityManager->remove($orderReductionCart);
  125. $entityManager->flush();
  126. $this->addFlash('success', 'La réduction a bien été supprimée de votre panier.');
  127. } else {
  128. $this->addFlash('error', 'Une erreur est survenue lors de la suppression de la réduction. ');
  129. }
  130. return $this->redirectToReferer($request);
  131. }
  132. /**
  133. * @Route("/reduction-credit/add/{id}", name="order_reduction_credit")
  134. */
  135. public function addReductionCredit(Request $request): RedirectResponse
  136. {
  137. $id = $request->get('id');
  138. $orderShop = $this->getCartCurrent();
  139. $user = $this->getUserCurrent();
  140. $orderShopContainer = $this->getOrderShopContainer();
  141. $reductionCredit = $this->getReductionCreditContainer()->getStore()->getOneById($id);
  142. if ($orderShop && $user && $reductionCredit
  143. && $orderShopContainer->getStore()->isReductionCreditAllowAddToOrder($orderShop, $reductionCredit)
  144. && !$orderShopContainer->getSolver()->isReductionCreditAddedToOrder($orderShop, $reductionCredit)) {
  145. $return = $orderShopContainer->getBuilder()->addReductionCredit($orderShop, $reductionCredit);
  146. if ($return) {
  147. $this->addFlash('success', 'Votre avoir a bien été ajouté à votre panier.');
  148. } else {
  149. $this->addFlash(
  150. 'error',
  151. 'Vous ne pouvez pas effectuer cette action. Le montant de la commande est insuffisant.'
  152. );
  153. }
  154. } else {
  155. $this->addFlash('error', "Impossible d'effectuer cette action");
  156. }
  157. return $this->redirectToReferer($request);
  158. }
  159. /**
  160. * @Route("/order-reduction-credit/delete/{id}", name="delete_reduction_credit")
  161. */
  162. public function deleteReductionCredit(Request $request): RedirectResponse
  163. {
  164. $entityManager = $this->getEntityManager();
  165. $id = $request->get('id');
  166. $orderReductionCredit = $this->getOrderReductionCreditContainer()->getStore()->getOneById((int)$id);
  167. $orderShop = $this->getCartCurrent();
  168. if ($orderReductionCredit && $orderShop->getOrderReductionCredits() && $orderShop->getOrderReductionCredits(
  169. )->contains($orderReductionCredit)) {
  170. $entityManager->remove($orderReductionCredit);
  171. $entityManager->flush();
  172. $this->addFlash('success', 'Votre avoir a bien été supprimé de votre panier.');
  173. } else {
  174. $this->addFlash('error', 'Une erreur est survenue lors de la suppression de votre avoir. ');
  175. }
  176. $referer = $this->getReferer($request);
  177. if ($referer) {
  178. return $this->redirect($referer);
  179. } else {
  180. return $this->redirectToRoute('frontend_order_cart', [
  181. 'section' => $this->getSectionCurrentSlug()
  182. ]);
  183. }
  184. }
  185. protected function redirectToReferer(Request $request): RedirectResponse
  186. {
  187. $referer = $this->getReferer($request);
  188. if ($referer) {
  189. return $this->redirect($referer);
  190. } else {
  191. return $this->redirectToRoute('frontend_order_cart', [
  192. 'section' => $this->getSectionCurrentSlug()
  193. ]);
  194. }
  195. }
  196. }