You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OrderController.php 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace Lc\ShopBundle\Controller\Admin;
  3. use App\Entity\OrderProduct;
  4. use App\Entity\Product;
  5. use Doctrine\DBAL\Types\FloatType;
  6. use Doctrine\ORM\EntityRepository;
  7. use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
  8. use Lc\ShopBundle\Context\AddressInterface;
  9. use Lc\ShopBundle\Context\ProductCategoryInterface;
  10. use Lc\ShopBundle\Context\ProductFamilyInterface;
  11. use Lc\ShopBundle\Context\ProductInterface;
  12. use Lc\ShopBundle\Context\TaxRateInterface;
  13. use Lc\ShopBundle\Context\UserInterface;
  14. use Lc\ShopBundle\Form\OrderProductType;
  15. use Lc\ShopBundle\Form\ProductFamilyCategoriesType;
  16. use Lc\ShopBundle\Form\ProductType;
  17. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  18. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  19. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  20. use Symfony\Component\Form\Extension\Core\Type\CollectionType;
  21. use Symfony\Component\Form\Extension\Core\Type\DateType;
  22. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  23. use Symfony\Component\Form\Extension\Core\Type\MoneyType;
  24. use Symfony\Component\Form\Extension\Core\Type\NumberType;
  25. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  26. use Symfony\Component\HttpFoundation\Response;
  27. class OrderController extends AdminController
  28. {
  29. public function updateEntity($entity)
  30. {
  31. foreach($entity->getOrderProducts() as $orderProduct) {
  32. $orderProduct->setCreatedBy($this->getUser()) ;
  33. $orderProduct->setUpdatedBy($this->getUser()) ;
  34. $orderProduct->setTaxRate($orderProduct->getProduct()->getProductFamily()->getTaxRate()) ;
  35. $orderProduct->setOrderShop($entity) ;
  36. $this->em->persist($orderProduct);
  37. $entity->addOrderProduct($orderProduct) ;
  38. }
  39. $this->setUpdated($entity);
  40. parent::updateEntity($entity);
  41. }
  42. public function persistEntity($entity)
  43. {
  44. parent::persistEntity($entity) ;
  45. }
  46. public function createEntityFormBuilder($entity, $view)
  47. {
  48. $formBuilder = parent::createEntityFormBuilder($entity, $view);
  49. $formBuilder->add('orderProducts', CollectionType::class, array(
  50. 'label' => 'Déclinaisons',
  51. 'entry_type' => OrderProductType::class,
  52. 'entry_options' => ['label' => false],
  53. 'allow_add' => true,
  54. 'allow_delete' => true,
  55. 'required' => true
  56. )
  57. );
  58. $userClass = $this->em->getClassMetadata(UserInterface::class);
  59. $formBuilder->add('user', EntityType::class, array(
  60. 'class' => $userClass->name
  61. ));
  62. $addressClass = $this->em->getClassMetadata(AddressInterface::class);
  63. $formBuilder->add('invoiceAddress', EntityType::class, array(
  64. 'class' => $addressClass->name,
  65. 'query_builder' => function (EntityRepository $er) use ($entity) {
  66. return $er->createQueryBuilder('a')
  67. ->where('a.user = :user')
  68. ->setParameter('user', $entity->getUser());
  69. },
  70. ));
  71. $formBuilder->add('deliveryAddress', EntityType::class, array(
  72. 'class' => $addressClass->name,
  73. 'query_builder' => function (EntityRepository $er) use ($entity) {
  74. return $er->createQueryBuilder('a')
  75. ->where('a.user = :user')
  76. ->setParameter('user', $entity->getUser());
  77. },
  78. ));
  79. return $formBuilder;
  80. }
  81. public function getUserViaFirstStepForm($entity)
  82. {
  83. $userClass = $this->em->getClassMetadata(UserInterface::class);
  84. $userChoiceForm = $this->createFormBuilder($entity)
  85. ->add('user', EntityType::class, array(
  86. 'class' => $userClass->name
  87. ))
  88. ->add('nextStep', SubmitType::class)
  89. ->getForm();
  90. $userChoiceForm->handleRequest($this->request);
  91. if ($userChoiceForm->isSubmitted() && $userChoiceForm->isValid()) {
  92. return $userChoiceForm->get('user')->getData();
  93. }
  94. $parameters = [
  95. 'form' => $userChoiceForm->createView(),
  96. 'formView' => 'default',
  97. 'entity' => $entity,
  98. ];
  99. return $this->executeDynamicMethod('render<EntityName>Template', ['new', $this->entity['templates']['new'], $parameters]);
  100. }
  101. protected function newAction()
  102. {
  103. $this->dispatch(EasyAdminEvents::PRE_NEW);
  104. $entity = $this->executeDynamicMethod('createNew<EntityName>Entity');
  105. $easyadmin = $this->request->attributes->get('easyadmin');
  106. $easyadmin['item'] = $entity;
  107. $this->request->attributes->set('easyadmin', $easyadmin);
  108. $fields = $this->entity['new']['fields'];
  109. $newForm = $this->executeDynamicMethod('create<EntityName>NewForm', [$entity, $fields]);
  110. $newForm->handleRequest($this->request);
  111. //ETAPE 1 Choix de l'utilisateur
  112. $user = $newForm->get('user')->getData();
  113. if($user == null){
  114. $user = $this->getUserViaFirstStepForm($entity);
  115. }
  116. if (!$user instanceof UserInterface) return $user;
  117. else{
  118. $entity->setUser($user);
  119. /* $newForm = $this->executeDynamicMethod('create<EntityName>NewForm', [$entity, $fields]);
  120. $newForm->handleRequest($this->request);*/
  121. //dump( $entity);
  122. //dump( $entity);
  123. if ($newForm->isSubmitted() && $newForm->isValid()) {
  124. dump($entity);
  125. die();
  126. //$this->dispatch(EasyAdminEvents::PRE_PERSIST, ['entity' => $entity]);
  127. $this->executeDynamicMethod('persist<EntityName>Entity', [$entity, $newForm]);
  128. $this->dispatch(EasyAdminEvents::POST_PERSIST, ['entity' => $entity]);
  129. return $this->redirectToReferrer();
  130. }
  131. $this->dispatch(EasyAdminEvents::POST_NEW, [
  132. 'entity_fields' => $fields,
  133. 'form' => $newForm,
  134. 'entity' => $entity
  135. ]);
  136. $parameters = [
  137. 'form' => $newForm->createView(),
  138. 'entity_fields' => $fields,
  139. 'entity' => $entity
  140. ];
  141. return $this->executeDynamicMethod('render<EntityName>Template', ['new', $this->entity['templates']['new'], $parameters]);
  142. }
  143. }
  144. }