|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- <?php
-
- namespace Lc\ShopBundle\Controller\Admin;
-
- use App\Entity\OrderProduct;
- use App\Entity\Product;
- use Doctrine\DBAL\Types\FloatType;
- use Doctrine\ORM\EntityRepository;
- use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
- use Lc\ShopBundle\Context\AddressInterface;
- use Lc\ShopBundle\Context\PointSaleInterface;
- use Lc\ShopBundle\Context\ProductCategoryInterface;
- use Lc\ShopBundle\Context\ProductFamilyInterface;
- use Lc\ShopBundle\Context\ProductInterface;
- use Lc\ShopBundle\Context\TaxRateInterface;
- use Lc\ShopBundle\Context\UserInterface;
- use Lc\ShopBundle\Form\OrderProductType;
- use Lc\ShopBundle\Form\ProductFamilyCategoriesType;
- use Lc\ShopBundle\Form\ProductType;
- use Lc\ShopBundle\Model\PointSale;
- use Symfony\Bridge\Doctrine\Form\Type\EntityType;
- use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
- use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
- use Symfony\Component\Form\Extension\Core\Type\CollectionType;
- use Symfony\Component\Form\Extension\Core\Type\DateType;
- use Symfony\Component\Form\Extension\Core\Type\HiddenType;
- use Symfony\Component\Form\Extension\Core\Type\MoneyType;
- use Symfony\Component\Form\Extension\Core\Type\NumberType;
- use Symfony\Component\Form\Extension\Core\Type\SubmitType;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\HttpFoundation\Response;
-
- class OrderController extends AdminController
- {
- public function updateEntity($entity)
- {
-
- foreach ($entity->getOrderProducts() as $orderProduct) {
- //dump($orderProduct);
- $orderProduct->setCreatedBy($this->getUser());
- $orderProduct->setUpdatedBy($this->getUser());
- $orderProduct->setTaxRate($orderProduct->getProduct()->getProductFamily()->getTaxRate());
- $orderProduct->setOrderShop($entity);
-
- $this->em->persist($orderProduct);
-
- $entity->addOrderProduct($orderProduct);
- }
-
- $this->setUpdated($entity);
-
- parent::updateEntity($entity);
- }
-
- public function persistEntity($entity)
- {
- foreach ($entity->getOrderProducts() as $orderProduct) {
-
- $orderProduct->setUnit($orderProduct->getProduct()->getUnitInherited());
- $orderProduct->setTitle($orderProduct->getProduct()->getTitleInherited());
- $orderProduct->setPrice($orderProduct->getProduct()->getPriceInherited());
- if ($orderProduct->getProduct()->getProductFamily()->getTaxRate()) {
- $orderProduct->setTaxRate($orderProduct->getProduct()->getProductFamily()->getTaxRate());
- } else {
- $orderProduct->setTaxRate($this->security->getUser()->getMerchant()->getTaxRate());
- }
-
- $orderProduct->setOrderShop($entity);
- $orderProduct->setCreatedBy($this->getUser());
- $orderProduct->setUpdatedBy($this->getUser());
- $this->em->persist($orderProduct);
- $entity->addOrderProduct($orderProduct);
-
- }
-
-
- parent::persistEntity($entity);
- }
-
- public function createEntityFormBuilder($entity, $view)
- {
- $formBuilder = parent::createEntityFormBuilder($entity, $view);
-
- $formBuilder->add('orderProducts', CollectionType::class, array(
- 'label' => 'Déclinaisons',
- 'entry_type' => OrderProductType::class,
- 'entry_options' => ['label' => false],
- 'allow_add' => true,
- 'allow_delete' => true,
- 'required' => true
- )
- );
-
- $userClass = $this->em->getClassMetadata(UserInterface::class);
- $formBuilder->add('user', EntityType::class, array(
- 'class' => $userClass->name
- ));
-
-
- $addressClass = $this->em->getClassMetadata(AddressInterface::class);
-
- $formBuilder->add('invoiceAddress', EntityType::class, array(
- 'class' => $addressClass->name,
- 'placeholder' => '',
- 'query_builder' => function (EntityRepository $er) use ($entity) {
- return $er->createQueryBuilder('a')
- ->where('a.user = :user')
- ->setParameter('user', $entity->getUser());
- },
- ));
-
- return $formBuilder;
- }
-
-
- public function getUserViaFirstStepForm($entity)
- {
- $userClass = $this->em->getClassMetadata(UserInterface::class);
-
- $userChoiceForm = $this->createFormBuilder($entity)
- ->add('user', EntityType::class, array(
- 'class' => $userClass->name
- ))
- ->add('nextStep', SubmitType::class)
- ->getForm();
- $userChoiceForm->handleRequest($this->request);
- if ($userChoiceForm->isSubmitted() && $userChoiceForm->isValid()) {
- return $userChoiceForm->get('user')->getData();
- }
-
- $parameters = [
- 'form' => $userChoiceForm->createView(),
- 'formView' => 'default',
- 'entity' => $entity,
- ];
-
- return $this->executeDynamicMethod('render<EntityName>Template', ['new', $this->entity['templates']['new'], $parameters]);
- }
-
- protected function newAction()
- {
- $this->dispatch(EasyAdminEvents::PRE_NEW);
-
- $entity = $this->executeDynamicMethod('createNew<EntityName>Entity');
-
- $easyadmin = $this->request->attributes->get('easyadmin');
- $easyadmin['item'] = $entity;
- $this->request->attributes->set('easyadmin', $easyadmin);
-
- $fields = $this->entity['new']['fields'];
-
- $newForm = $this->executeDynamicMethod('create<EntityName>NewForm', [$entity, $fields]);
-
- $newForm->handleRequest($this->request);
-
- //ETAPE 1 Choix de l'utilisateur
- $user = $newForm->get('user')->getData();
-
- if ($user == null) {
- $user = $this->getUserViaFirstStepForm($entity);
- }
-
- if (!$user instanceof UserInterface) return $user;
- else {
- $entity->setUser($user);
-
- //réinitialise le formulaire pour charger les adresses en fonction de l'utilisateur sélectionné
- $newForm = $this->executeDynamicMethod('create<EntityName>NewForm', [$entity, $fields]);
- $newForm->handleRequest($this->request);
-
- //dump( $entity);
- //dump( $entity);
-
- if ($newForm->isSubmitted() && $newForm->isValid()) {
- /*dump($entity);
- die();*/
- //$this->dispatch(EasyAdminEvents::PRE_PERSIST, ['entity' => $entity]);
- $this->executeDynamicMethod('persist<EntityName>Entity', [$entity, $newForm]);
- $this->dispatch(EasyAdminEvents::POST_PERSIST, ['entity' => $entity]);
-
- return $this->redirectToReferrer();
- }
-
- $this->dispatch(EasyAdminEvents::POST_NEW, [
- 'entity_fields' => $fields,
- 'form' => $newForm,
- 'entity' => $entity
- ]);
-
- $parameters = [
- 'form' => $newForm->createView(),
- 'entity_fields' => $fields,
- 'entity' => $entity
- ];
-
- return $this->executeDynamicMethod('render<EntityName>Template', ['new', $this->entity['templates']['new'], $parameters]);
- }
- }
-
-
- }
|