|
- <?php
-
- namespace Lc\ShopBundle\Controller\Admin;
-
- use Doctrine\ORM\EntityManagerInterface;
- use Doctrine\ORM\EntityRepository;
- use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
- use FOS\UserBundle\Model\UserManagerInterface;
- use Lc\ShopBundle\Context\AddressInterface;
- use Lc\ShopBundle\Context\MerchantUtilsInterface;
- use Lc\ShopBundle\Context\OrderProductInterface;
- use Lc\ShopBundle\Context\OrderUtilsInterface;
- use Lc\ShopBundle\Context\UserInterface;
- use Lc\ShopBundle\Form\Order\AddPoductToOrderType;
- use Lc\ShopBundle\Form\Order\OrderProductsType;
- use Lc\ShopBundle\Form\OrderProductType;
- use Lc\ShopBundle\Services\Utils;
- use Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport;
- use Symfony\Bridge\Doctrine\Form\Type\EntityType;
- use Symfony\Component\Form\Extension\Core\Type\CollectionType;
- use Symfony\Component\Form\Extension\Core\Type\SubmitType;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\Security\Core\Security;
-
-
- class OrderController extends AdminController
- {
- /*protected $orderUtils;
-
- public function __construct(Security $security, UserManagerInterface $userManager, EntityManagerInterface $em, Utils $utils, MerchantUtilsInterface $merchantUtils, MailjetTransport $mailjetTransport, OrderUtilsInterface $orderUtils)
- {
- $this->orderUtils = $orderUtils;
- parent::__construct($security, $userManager, $em, $utils, $merchantUtils, $mailjetTransport);
- }*/
-
- 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, $override = true)
- {
- $formBuilder = parent::createEntityFormBuilder($entity, $view, false);
-
- $userClass = $this->em->getClassMetadata(UserInterface::class);
- $addressClass = $this->em->getClassMetadata(AddressInterface::class);
-
-
- $formBuilder->add('user', EntityType::class, array(
- 'class' => $userClass->name
- ));
-
-
- $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());
- },
- ));
-
- $formBuilder = $this->overrideFormBuilder($formBuilder, $entity, $view);
-
-
- 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]);
- }
-
-
- public function addProductToOrderAction()
- {
-
- $id = $this->request->query->get('id');
- $easyadmin = $this->request->attributes->get('easyadmin');
- $orderShop = $easyadmin['item'];
-
- $orderProductClass = $this->em->getClassMetadata(OrderProductInterface::class);
- $formAddProductToOrder = $this->createForm(AddPoductToOrderType::class);
-
- $formAddProductToOrder->handleRequest($this->request);
- if ($formAddProductToOrder->get('product')->getData() == null) {
- $response['status'] = 'error';
- $response['message'] = 'Vous devez choisir un produit dans la liste';
- } else if ($formAddProductToOrder->get('quantity')->getData() == null) {
- $response['status'] = 'error';
- $response['message'] = 'Vous devez entrer une quantité';
-
- } else if ($formAddProductToOrder->isSubmitted() && $formAddProductToOrder->isValid()) {
- $orderProduct = new $orderProductClass->name;
-
- $orderProduct->setQuantityOrder($formAddProductToOrder->get('quantity')->getData());
- $orderProduct->setProduct($formAddProductToOrder->get('product')->getData());
-
- $this->orderUtils->addOrderProduct($orderShop, $orderProduct);
-
- $response['status'] = 'success';
- $response['message'] = 'Le produit a bien été ajouté à la commande';
- } else {
- $response['status'] = 'error';
- $response['message'] = 'Une erreur est survenue';
- }
- $response['data'] = $this->orderUtils->getOrderAsJsonObject($orderShop);;
-
- return new Response(json_encode($response));
- }
-
-
- public function orderProductsAction()
- {
-
- $id = $this->request->query->get('id');
- $easyadmin = $this->request->attributes->get('easyadmin');
- $orderShop = $easyadmin['item'];
-
- $formOrderProducts = $this->createForm(OrderProductsType::class, $orderShop);
-
- $formOrderProducts->handleRequest($this->request);
-
-
- if ($formOrderProducts->isSubmitted() && $formOrderProducts->isValid()) {
- // dump($formOrderProducts->get('orderProducts')->getData());
- foreach ($orderShop->getOrderProducts() as $orderProduct) {
-
-
- if ($orderProduct->getQuantityOrder() <= 0) {
- $response['niche'] = $orderProduct->getQuantityOrder();
- $this->em->remove($orderProduct);
- } else {
- //dump($orderProduct);
- $this->em->persist($orderProduct);
- }
- }
- //$this->em->persist($orderShop);
- $this->em->flush();
- $response['status'] = 'success';
- $response['message'] = 'La commande a bien été modifié';
- } else {
- $response['status'] = 'error';
- $response['message'] = 'Une erreur est survenue';
- }
- $response['data'] = $this->orderUtils->getOrderAsJsonObject($orderShop);;
- //die('nicicici');
- return new Response(json_encode($response));
- }
-
-
- public function renderOrderShopTemplate($actionName, $templatePath, array $parameters = [])
- {
- if ($actionName == 'show') {
- $formAddProductToOrder = $this->createForm(AddPoductToOrderType::class, null, array(
- 'action' => $this->generateUrl('easyadmin', [
- 'action' => 'addProductToOrder',
- 'entity' => $this->entity['name'],
- 'id' => $parameters['entity']->getId()
- ])
- ));
- $formOrderProducts = $this->createForm(OrderProductsType::class, null, array(
- 'action' => $this->generateUrl('easyadmin', [
- 'action' => 'orderProducts',
- 'entity' => $this->entity['name'],
- 'id' => $parameters['entity']->getId()
- ])
- ));
- $parameters['form_add_product_to_order'] = $formAddProductToOrder->createView();
- $parameters['form_order_products'] = $formOrderProducts->createView();
- }
- return parent::renderTemplate($actionName, $templatePath, $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 {
-
- $orderShop = $this->orderUtils->createOrderShop(array(
- 'user' => $user,
- 'merchant' => $this->merchantUtils->getMerchantUser()
- ));
-
- return $this->redirectToRoute('easyadmin', [
- 'action' => 'edit',
- 'entity' => $this->entity['name'],
- 'id' => $orderShop->getId()
- ]);
- }
- }
-
- /**
- * The method that is executed when the user performs a 'show' action on an entity.
- *
- * @return Response
- */
- public function showAction()
- {
- $this->dispatch(EasyAdminEvents::PRE_SHOW);
-
- $id = $this->request->query->get('id');
- $easyadmin = $this->request->attributes->get('easyadmin');
- $entity = $easyadmin['item'];
-
- $fields = $this->entity['show']['fields'];
- $deleteForm = $this->createDeleteForm($this->entity['name'], $id);
-
- $this->dispatch(EasyAdminEvents::POST_SHOW, [
- 'deleteForm' => $deleteForm,
- 'fields' => $fields,
- 'entity' => $entity,
- ]);
-
- $parameters = [
- 'entity' => $entity,
- 'fields' => $fields,
- 'delete_form' => $deleteForm->createView(),
- 'order' => $this->orderUtils->getOrderAsJsonObject($entity)
-
- ];
-
- return $this->executeDynamicMethod('render<EntityName>Template', ['show', $this->entity['templates']['show'], $parameters]);
- }
-
- /**
- * Réécriture de edit action pr rediriger vers le show */
- public function editAction()
- {
- $id = $this->request->query->get('id');
- $entity = $this->request->query->get('entity');
-
- return $this->redirectToRoute('easyadmin', [
- 'action' => 'show',
- 'entity' => $entity,
- 'id' => $id
- ]);
- }
-
- }
|