|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473 |
- <?php
-
- namespace Lc\ShopBundle\Controller\Backend;
-
- use App\Entity\OrderShop;
- use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
- use Lc\ShopBundle\Context\OrderProductInterface;
- use Lc\ShopBundle\Context\UserInterface;
- use Lc\ShopBundle\Form\Backend\Order\AddPoductToOrderType;
- use Lc\ShopBundle\Form\Backend\Order\OrderDeliveryAddressType;
- use Lc\ShopBundle\Form\Backend\Order\OrderInvoiceAddressType;
- use Lc\ShopBundle\Form\Backend\Order\OrderProductsType;
- use Lc\ShopBundle\Form\Backend\Order\OrderReductionCartType;
- use Lc\ShopBundle\Form\Backend\Order\OrderReductionCreditType;
- use Lc\ShopBundle\Form\Backend\Order\OrderStatusType;
- use Symfony\Bridge\Doctrine\Form\Type\EntityType;
- use Symfony\Component\Form\Extension\Core\Type\SubmitType;
- 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 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 orderInvoiceAddressAction()
- {
-
- $id = $this->request->query->get('id');
- $easyadmin = $this->request->attributes->get('easyadmin');
- $orderShop = $easyadmin['item'];
-
- $formOrderInvoiceAddress = $this->createForm(OrderInvoiceAddressType::class, $orderShop);
-
- $formOrderInvoiceAddress->handleRequest($this->request);
-
- if ($formOrderInvoiceAddress->isSubmitted() && $formOrderInvoiceAddress->isValid()) {
- $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);;
- return new Response(json_encode($response));
- }
-
- public function orderStatusAction()
- {
-
- $id = $this->request->query->get('id');
- $easyadmin = $this->request->attributes->get('easyadmin');
- $orderShop = $easyadmin['item'];
-
- $formOrderStatus = $this->createForm(OrderStatusType::class, $orderShop);
-
- $formOrderStatus->handleRequest($this->request);
-
- if ($formOrderStatus->isSubmitted() && $formOrderStatus->isValid()) {
-
- if($orderShop = $this->orderUtils->changeOrderStatus($formOrderStatus->get('orderStatus')->getData(), $orderShop)){
- $this->addFlash('success', 'La commande a bien été modifié.');
- }
- }else{
- $this->addFlash('error', 'Une erreur s\'est produite');
- }
-
- return $this->redirectToRoute('easyadmin', [
- 'action' => 'show',
- 'entity' => $this->request->query->get('entity'),
- 'id' => $id
- ]);
-
- }
-
-
- 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 {
- $this->em->persist($orderProduct);
- }
- }
- $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);;
- return new Response(json_encode($response));
- }
-
-
-
- public function orderReductionCartAction()
- {
-
- $id = $this->request->query->get('id');
- $easyadmin = $this->request->attributes->get('easyadmin');
- $orderShop = $easyadmin['item'];
-
- $formOrderReductionCart = $this->createForm(OrderReductionCartType::class, $orderShop);
-
- $formOrderReductionCart->handleRequest($this->request);
-
- if ($formOrderReductionCart->isSubmitted() && $formOrderReductionCart->isValid()) {
- $reductionCart = $formOrderReductionCart->get('reductionCart')->getData();
- $orderShop->reductionError = array();
- if($this->orderUtils->isReductionCartAllowToBeAddToOrder($orderShop, $reductionCart)){
- $orderReductionCart = $this->orderUtils->createOrderReductionCart($orderShop, $reductionCart);
- $this->em->persist($orderReductionCart);
- $this->em->flush();
-
-
- $response['status'] = 'success';
- $response['message'] = 'La réduction a bien été ajouté';
-
- }else{
- $response['status'] = 'error';
- $response['message'] = 'Cette réduction ne peut pas être appliqué sur cette commande';
-
- $response['message'] .= '<ul>';
- foreach ($orderShop->reductionError as $error) {
- $response['message'] .= '<li> <i>'.$this->translator->trans($error, array(), 'lcshop'). '</i></li>';
- }
- $response['message'] .= '</ul>';
- }
-
- } else {
- $response['status'] = 'error';
- $response['message'] = 'Une erreur est survenue';
- }
- $response['data'] = $this->orderUtils->getOrderAsJsonObject($orderShop);;
- return new Response(json_encode($response));
- }
-
-
- public function orderReductionCreditAction()
- {
-
- $id = $this->request->query->get('id');
- $easyadmin = $this->request->attributes->get('easyadmin');
- $orderShop = $easyadmin['item'];
-
- $formOrderReductionCredit = $this->createForm(OrderReductionCreditType::class, $orderShop);
-
- $formOrderReductionCredit->handleRequest($this->request);
-
- if ($formOrderReductionCredit->isSubmitted() && $formOrderReductionCredit->isValid()) {
- $reductionCredit = $formOrderReductionCredit->get('reductionCredit')->getData();
- $orderShop->reductionError = array();
- if($this->orderUtils->isReductionCreditAllowToBeAddToOrder($orderShop, $reductionCredit)){
- $orderReductionCredit = $this->orderUtils->createOrderReductionCredit($orderShop, $reductionCredit);
- $this->em->persist($orderReductionCredit);
- $this->em->flush();
-
-
- $response['status'] = 'success';
- $response['message'] = 'L\'avoir a bien été ajouté';
-
- }else{
- $response['status'] = 'error';
- $response['message'] = 'Cet avoir ne peut pas être appliqué sur cette commande';
-
- $response['message'] .= '<ul>';
- foreach ($orderShop->reductionError as $error) {
- $response['message'] .= '<li> <i>'.$this->translator->trans($error, array(), 'lcshop'). '</i></li>';
- }
- $response['message'] .= '</ul>';
- }
-
- } else {
- $response['status'] = 'error';
- $response['message'] = 'Une erreur est survenue';
- }
- $response['data'] = $this->orderUtils->getOrderAsJsonObject($orderShop);;
- return new Response(json_encode($response));
- }
-
-
- public function renderOrderShopTemplate($actionName, $templatePath, array $parameters = [])
- {
- //dump($this->em->getRepository(OrderShop::class)->getValidOrder());
-
- 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()
- ])
- ));
-
- $formOrderInvoiceAddress = $this->createForm(OrderInvoiceAddressType::class, null, array(
- 'data' => $parameters['entity'],
- 'action' => $this->generateUrl('easyadmin', [
- 'action' => 'orderInvoiceAddress',
- 'entity' => $this->entity['name'],
- 'id' => $parameters['entity']->getId()
- ])
- ));
-
- $formOrderStatus = $this->createForm(OrderStatusType::class, null, array(
- 'data' => $parameters['entity'],
- 'action' => $this->generateUrl('easyadmin', [
- 'action' => 'orderStatus',
- 'entity' => $this->entity['name'],
- 'id' => $parameters['entity']->getId()
- ])
- ));
-
- $formOrderReductionCart = $this->createForm(OrderReductionCartType::class, null, array(
- 'data' => $parameters['entity'],
- 'action' => $this->generateUrl('easyadmin', [
- 'action' => 'orderReductionCart',
- 'entity' => $this->entity['name'],
- 'id' => $parameters['entity']->getId()
- ])
- ));
- $formOrderReductionCredit = $this->createForm(OrderReductionCreditType::class, null, array(
- 'data' => $parameters['entity'],
- 'action' => $this->generateUrl('easyadmin', [
- 'action' => 'orderReductionCredit',
- 'entity' => $this->entity['name'],
- 'id' => $parameters['entity']->getId()
- ])
- ));
-
- if(!isset($parameters['form_order_delivery_address'])) {
- $formOrderDeliveryAddress = $this->createForm(OrderDeliveryAddressType::class, null, array(
- 'data' => $parameters['entity'],
- 'action' => $this->generateUrl('easyadmin', [
- 'action' => 'orderDeliveryAddress',
- 'entity' => $this->entity['name'],
- 'id' => $parameters['entity']->getId()
- ])
- ));
- $parameters['form_order_delivery_address'] = $formOrderDeliveryAddress->createView();
- }
- $parameters['form_order_reduction_credit'] = $formOrderReductionCredit->createView();
- $parameters['form_order_reduction_cart'] = $formOrderReductionCart->createView();
- $parameters['form_add_product_to_order'] = $formAddProductToOrder->createView();
- $parameters['form_order_products'] = $formOrderProducts->createView();
- $parameters['form_order_invoice_address'] = $formOrderInvoiceAddress->createView();
- $parameters['form_order_status'] = $formOrderStatus->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
- ]);
- }
-
- }
|