src/Controller/OrderController.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Classe\Cart;
  4. use App\Entity\Order;
  5. use App\Entity\OrderDetail;
  6. use App\Form\OrderType;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. class OrderController extends AbstractController
  13. {
  14. /**
  15. * @Route("/commande/livraison", name="app_orde")
  16. */
  17. public function index(): Response
  18. {
  19. $addresses = $this->getUser()->getAddresses();
  20. if(count($addresses) == 0){
  21. return $this->redirectToRoute('app_account_address_form');
  22. }
  23. $form = $this->createForm(OrderType::class, null, [
  24. 'addresses' => $addresses, // ✅ CORRECT
  25. 'action' => $this->generateUrl('app_sumary')
  26. ]);
  27. return $this->render('order/index.html.twig', [
  28. 'deliverForm' => $form->createView(),
  29. ]);
  30. }
  31. /**
  32. * @Route("/commande/recapitilatif", name="app_sumary")
  33. */
  34. public function add(Request $request, Cart $cart, EntityManagerInterface $entityManager): Response
  35. {
  36. if($request->getMethod() != 'POST') {
  37. return $this->redirectToRoute('app_cart');
  38. }
  39. $products = $cart->getCart();
  40. $form = $this->createForm(OrderType::class, null, [
  41. 'addresses' => $this->getUser()->getAddresses()
  42. ]);
  43. $form->handleRequest($request);
  44. if($form->isSubmitted() && $form->isValid()) {
  45. $addressObj = $form->get('addresses')->getData();
  46. $address = $addressObj->getFirstName().' '.$addressObj->getLastName().'</br>';
  47. $address .= $addressObj->getAddress().'</br>';
  48. $address .= $addressObj->getPostal().' '.$addressObj->getCity().'</br>';
  49. $address .= $addressObj->getCountry().'</br>';
  50. $address .= $addressObj->getPhone().'</br>';
  51. $order = new Order();
  52. $order->setUser($this->getUser());
  53. $order->setCreatedAt(new \DateTime());
  54. $order->setState(1);
  55. $order->setCarrierName($form->get('carriers')->getData()->getName());
  56. $order->setCarrierPrice($form->get('carriers')->getData()->getPrice());
  57. $order->setDelivery($address);
  58. foreach($products as $product){
  59. $orderDetail = new OrderDetail();
  60. $orderDetail->setProductName($product['objet']->getName());
  61. $orderDetail->setProductIllustration($product['objet']->getIllustration());
  62. $orderDetail->setProductPrice($product['objet']->getPrice());
  63. $orderDetail->setProductTva($product['objet']->getTva());
  64. $orderDetail->setProductQuantity($product['qty']);
  65. $order->addOrderDetail($orderDetail);
  66. }
  67. $entityManager->persist($order);
  68. $entityManager->flush();
  69. }
  70. return $this->render('order/sumary.html.twig', [
  71. 'choices' =>$form->getData(),
  72. 'cart' => $products,
  73. 'order' => $order,
  74. 'totalwt' => $cart->getTotalwt()
  75. ]);
  76. }
  77. }