201 行
8.4KB

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