src/Controller/Order/OrderController.php line 471

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Order;
  3. use App\Entity\Addressing\Country;
  4. use App\Entity\Addressing\County;
  5. use App\Entity\Customer\Customer;
  6. use App\Entity\Order\Order;
  7. use App\Entity\Partner\Partner;
  8. use App\Entity\Shipping\Shipment;
  9. use App\Entity\VignetteInvoice\VignetteInvoice;
  10. use App\Entity\VignettePaymentMethod\VignettePaymentMethod;
  11. use App\Manager\Order\OrderManager;
  12. use App\Model\AddressModel;
  13. use App\Model\FilterOrderModel;
  14. use App\Model\OrderModel;
  15. use App\Model\ShopUserModel;
  16. use App\Model\VignetteInvoiceModel;
  17. use App\Service\CustomerService;
  18. use App\Service\ExportService;
  19. use App\Service\InvoiceApiService;
  20. use App\Service\InvoiceService;
  21. use App\Service\Order\AdminGridService;
  22. use App\Service\OrderService;
  23. use App\Service\SystemService;
  24. use App\Service\VignetteApiService;
  25. use Doctrine\Common\Persistence\ObjectManager;
  26. use FOS\RestBundle\View\View;
  27. use \Sylius\Bundle\CoreBundle\Controller\OrderController as BaseOrderController;
  28. use Sylius\Bundle\ResourceBundle\Controller\AuthorizationCheckerInterface;
  29. use Sylius\Bundle\ResourceBundle\Controller\EventDispatcherInterface;
  30. use Sylius\Bundle\ResourceBundle\Controller\FlashHelperInterface;
  31. use Sylius\Bundle\ResourceBundle\Controller\NewResourceFactoryInterface;
  32. use Sylius\Bundle\ResourceBundle\Controller\RedirectHandlerInterface;
  33. use Sylius\Bundle\ResourceBundle\Controller\RequestConfigurationFactoryInterface;
  34. use Sylius\Bundle\ResourceBundle\Controller\ResourceDeleteHandlerInterface;
  35. use Sylius\Bundle\ResourceBundle\Controller\ResourceFormFactoryInterface;
  36. use Sylius\Bundle\ResourceBundle\Controller\ResourcesCollectionProviderInterface;
  37. use Sylius\Bundle\ResourceBundle\Controller\ResourceUpdateHandlerInterface;
  38. use Sylius\Bundle\ResourceBundle\Controller\SingleResourceProviderInterface;
  39. use Sylius\Bundle\ResourceBundle\Controller\StateMachineInterface;
  40. use Sylius\Bundle\ResourceBundle\Controller\ViewHandlerInterface;
  41. use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
  42. use Sylius\Component\Core\OrderPaymentStates;
  43. use Sylius\Component\Order\Model\OrderInterface;
  44. use Sylius\Component\Resource\Exception\UpdateHandlingException;
  45. use Sylius\Component\Resource\Factory\FactoryInterface;
  46. use Sylius\Component\Resource\Metadata\MetadataInterface;
  47. use Sylius\Component\Resource\Repository\RepositoryInterface;
  48. use Sylius\Component\Resource\ResourceActions;
  49. use Symfony\Component\HttpFoundation\JsonResponse;
  50. use Symfony\Component\HttpFoundation\RedirectResponse;
  51. use Symfony\Component\HttpFoundation\Request;
  52. use Symfony\Component\HttpFoundation\Response;
  53. use Symfony\Component\HttpKernel\Exception\HttpException;
  54. class OrderController extends BaseOrderController
  55. {
  56.     public function __construct(MetadataInterface $metadata,
  57.                                 RequestConfigurationFactoryInterface $requestConfigurationFactory,
  58.                                 ViewHandlerInterface $viewHandlerRepositoryInterface $repository,
  59.                                 FactoryInterface $factoryNewResourceFactoryInterface $newResourceFactory,
  60.                                 ObjectManager $managerSingleResourceProviderInterface $singleResourceProvider,
  61.                                 ResourcesCollectionProviderInterface $resourcesFinder,
  62.                                 ResourceFormFactoryInterface $resourceFormFactory,
  63.                                 RedirectHandlerInterface $redirectHandler,
  64.                                 FlashHelperInterface $flashHelper,
  65.                                 AuthorizationCheckerInterface $authorizationChecker,
  66.                                 EventDispatcherInterface $eventDispatcher,
  67.                                 StateMachineInterface $stateMachine,
  68.                                 ResourceUpdateHandlerInterface $resourceUpdateHandler,
  69.                                 ResourceDeleteHandlerInterface $resourceDeleteHandler)
  70.     {
  71.         parent::__construct($metadata$requestConfigurationFactory$viewHandler$repository$factory$newResourceFactory$manager$singleResourceProvider$resourcesFinder$resourceFormFactory$redirectHandler$flashHelper$authorizationChecker$eventDispatcher$stateMachine$resourceUpdateHandler$resourceDeleteHandler);
  72.     }
  73.     public function showAction(Request $request): Response
  74.     {
  75.         $configuration $this->requestConfigurationFactory->create($this->metadata$request);
  76.         $this->isGrantedOr403($configurationResourceActions::SHOW);
  77.         $resource $this->findOr404($configuration);
  78.         $this->eventDispatcher->dispatch(ResourceActions::SHOW$configuration$resource);
  79.         $view View::create($resource);
  80.         if ($configuration->isHtmlRequest()) {
  81.             $view
  82.                 ->setTemplate($configuration->getTemplate(ResourceActions::SHOW '.html'))
  83.                 ->setTemplateVar($this->metadata->getName())
  84.                 ->setData([
  85.                     'configuration' => $configuration,
  86.                     'metadata' => $this->metadata,
  87.                     'resource' => $resource,
  88.                     $this->metadata->getName() => $resource,
  89.                 ])
  90.             ;
  91.         }
  92.         return $this->viewHandler->handle($configuration$view);
  93.     }
  94.     public function changeOrderPaymentStatus(Request $request)
  95.     {
  96.         $message 'Fail';
  97.         $orderId $request->get('orderId');
  98.         $paymentStatus $request->get('orderPaymentStatusSelected');
  99.         if ($orderId && $paymentStatus) {
  100.             $order $this->getDoctrine()->getRepository(Order::class)->find($orderId);
  101.             if ($order) {
  102.                 $orderModel = new OrderModel($this->getDoctrine()->getManager(), $this->container);
  103.                 $orderModel->updatePaymentStatus($order$paymentStatus);
  104.                 if ($paymentStatus == OrderPaymentStates::STATE_PAID) {
  105.                     $ipnService $this->container->get('app.service.payment.ipn');
  106.                     $ipnService->issueManually($order);
  107.                     $message 'Status updatat cu succes. Se incearca emiterea produselor';
  108.                 } else {
  109.                     $message 'Succes';
  110.                 }
  111.             }
  112.         }
  113.         return new Response(json_encode($message));
  114.     }
  115.     public function changeCustomer(Request $request)
  116.     {
  117.         $orderId $request->request->get('order_id');
  118.         $customerId $request->request->get('customer_id');
  119.         if (!$orderId || !$customerId) {
  120.             return new Response('error');
  121.         }
  122.         $order $this->getDoctrine()->getRepository(Order::class)->find($orderId);
  123.         $customer $this->getDoctrine()->getRepository(Customer::class)->find($customerId);
  124.         OrderService::changeOrderCustomer($this->getDoctrine()->getManager(), $order$customer);
  125.         return new Response('ok');
  126.     }
  127.     public function getAvailableCustomers(Request $request)
  128.     {
  129.         $searchTerm $request->request->has('search') ? $request->request->get('search') : null;
  130.         $order $this->getDoctrine()->getRepository(Customer::class)->createQueryBuilder('o')->where('o.email LIKE :search')->orWhere('o.firstName LIKE :search')->orWhere('o.lastName LIKE :search')->setParameter(':search''%'$searchTerm .'%')->getQuery()->getArrayResult();
  131.         return new Response(json_encode($order));
  132.     }
  133.     public function exportOrderBySmsView()
  134.     {
  135.         return $this->render('@templates/AdminCustom/Order/export-order-sms.html.twig');
  136.     }
  137.     public function exportShipment(Request $request)
  138.     {
  139.         $shipmentId $request->get('shipment_id');
  140.         if ($shipmentId) {
  141.             $shipment $this->getDoctrine()->getRepository(Shipment::class)->find($shipmentId);
  142.             if ($shipment) {
  143.                 $exportService = new ExportService($this->repository ,$request$this->manager);
  144.                 $exportService->exportShipment($shipment);
  145.             }
  146.         }
  147.         echo "<script>window.close();</script>";
  148.         return new Response('success');
  149.     }
  150.     public function exportShipments(Request $request)
  151.     {
  152.         $repository $this->getDoctrine()->getRepository(Shipment::class);
  153.         $exportService = new ExportService($repository$request$this->manager);
  154.         $shipments $exportService->getData();
  155.         $exportService->exportShipments($shipments);
  156.         echo "<script>window.close();</script>";
  157.         return new Response('success');
  158.     }
  159.     public function exportOrderBySmsAction(Request $request)
  160.     {
  161.         $params $request->request->all();
  162.         $exportService = new ExportService($this->repository ,$request$this->manager);
  163.         $result $exportService->exportOrdersBySms($params);
  164.         if ($result) {
  165.             $this->addFlash('export-order-sms''success');
  166.         } else {
  167.             $this->addFlash('export-order-sms''fail');
  168.         }
  169.         return $this->redirectToRoute('app_admin_export_order_by_sms_view');
  170.     }
  171.     public function updateAction(Request $request): Response
  172.     {
  173.         $configuration $this->requestConfigurationFactory->create($this->metadata$request);
  174.         $this->isGrantedOr403($configurationResourceActions::UPDATE);
  175.         $resource $this->findOr404($configuration);
  176.         $form $this->resourceFormFactory->create($configuration$resource);
  177.         if (in_array($request->getMethod(), ['POST''PUT''PATCH'], true) && $form->handleRequest($request)->isValid()) {
  178.             $resource $form->getData();
  179.             /** @var ResourceControllerEvent $event */
  180.             $event $this->eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE$configuration$resource);
  181.             if ($event->isStopped() && !$configuration->isHtmlRequest()) {
  182.                 throw new HttpException($event->getErrorCode(), $event->getMessage());
  183.             }
  184.             if ($event->isStopped()) {
  185.                 $this->flashHelper->addFlashFromEvent($configuration$event);
  186.                 $eventResponse $event->getResponse();
  187.                 if (null !== $eventResponse) {
  188.                     return $eventResponse;
  189.                 }
  190.                 return $this->redirectHandler->redirectToResource($configuration$resource);
  191.             }
  192.             try {
  193.                 $this->resourceUpdateHandler->handle($resource$configuration$this->manager);
  194.             } catch (UpdateHandlingException $exception) {
  195.                 if (!$configuration->isHtmlRequest()) {
  196.                     return $this->viewHandler->handle(
  197.                         $configuration,
  198.                         View::create($form$exception->getApiResponseCode())
  199.                     );
  200.                 }
  201.                 $this->flashHelper->addErrorFlash($configuration$exception->getFlash());
  202.                 return $this->redirectHandler->redirectToReferer($configuration);
  203.             }
  204.             if ($configuration->isHtmlRequest()) {
  205.                 $this->flashHelper->addSuccessFlash($configurationResourceActions::UPDATE$resource);
  206.             }
  207.             $postEvent $this->eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE$configuration$resource);
  208.             if (!$configuration->isHtmlRequest()) {
  209.                 $view $configuration->getParameters()->get('return_content'false) ? View::create($resourceResponse::HTTP_OK) : View::create(nullResponse::HTTP_NO_CONTENT);
  210.                 return $this->viewHandler->handle($configuration$view);
  211.             }
  212.             $postEventResponse $postEvent->getResponse();
  213.             if (null !== $postEventResponse) {
  214.                 return $postEventResponse;
  215.             }
  216.             return $this->redirectHandler->redirectToResource($configuration$resource);
  217.         }
  218.         if (!$configuration->isHtmlRequest()) {
  219.             return $this->viewHandler->handle($configurationView::create($formResponse::HTTP_BAD_REQUEST));
  220.         }
  221.         $initializeEvent $this->eventDispatcher->dispatchInitializeEvent(ResourceActions::UPDATE$configuration$resource);
  222.         $initializeEventResponse $initializeEvent->getResponse();
  223.         if (null !== $initializeEventResponse) {
  224.             return $initializeEventResponse;
  225.         }
  226.         $data null;
  227.         $country null;
  228.         if ($resource instanceof OrderInterface) {
  229.             $data AddressModel::getAddressData($resource->getBillingAddress(), $this->getDoctrine()->getManager());
  230.             $country $this->getDoctrine()->getRepository(Country::class)->findOneBy(array('code' => $resource->getBillingAddress()->getCountryCode()));
  231.         }
  232.         $view View::create()
  233.             ->setData([
  234.                 'configuration' => $configuration,
  235.                 'metadata' => $this->metadata,
  236.                 'resource' => $resource,
  237.                 $this->metadata->getName() => $resource,
  238.                 'form' => $form->createView(),
  239.                 'data' => $data,
  240.                 'countries' => $this->getDoctrine()->getRepository(Country::class)->findAll(),
  241.                 'counties' => $this->getDoctrine()->getRepository(County::class)->findAllCountiesByCountry($country),
  242.             ])
  243.             ->setTemplate($configuration->getTemplate(ResourceActions::UPDATE '.html'))
  244.         ;
  245.         return $this->viewHandler->handle($configuration$view);
  246.     }
  247.     public function indexAction(Request $request): Response
  248.     {
  249.         $sendInvoiceStatus $request->get('sendInvoiceStatus');
  250.         $orderId $request->get('orderId');
  251.         $configuration $this->requestConfigurationFactory->create($this->metadata$request);
  252.         $this->isGrantedOr403($configurationResourceActions::INDEX);
  253.         $resources $this->resourcesCollectionProvider->get($configuration$this->repository);
  254.         $this->eventDispatcher->dispatchMultiple(ResourceActions::INDEX$configuration$resources);
  255.         $view View::create($resources);
  256.         if ($configuration->isHtmlRequest()) {
  257.             $view
  258.                 ->setTemplate($configuration->getTemplate(ResourceActions::INDEX '.html'))
  259.                 ->setTemplateVar($this->metadata->getPluralName())
  260.                 ->setData([
  261.                     'configuration' => $configuration,
  262.                     'metadata' => $this->metadata,
  263.                     'resources' => $resources,
  264.                     'orderId' => $orderId,
  265.                     'sendInvoiceStatus' => $sendInvoiceStatus,
  266.                     $this->metadata->getPluralName() => $resources,
  267.                 ]);
  268.         }
  269.         return $this->viewHandler->handle($configuration$view);
  270.     }
  271.     public function shopIndexAction(Request $request): Response
  272.     {
  273.         if (!CustomerService::retrieveCustomerFromShopUser($this->getUser())) {
  274.             return $this->redirectToRoute('app_shop_old_homepage');
  275.         }
  276.         $paymentMethods = array();
  277.         $allPaymentMethods $this->getDoctrine()->getRepository(VignettePaymentMethod::class)->findAll();
  278.         foreach ($allPaymentMethods as $paymentMethod) {
  279.             $paymentMethods[$paymentMethod->getId()] = $paymentMethod->getName();
  280.         }
  281.         return $this->render('@templates/Account/Order/list.html.twig',
  282.             [
  283.                 'paymentMethods' => $paymentMethods,
  284.                 'states' => OrderManager::getAllOrderStates(),
  285.                 'paymentStates' => OrderManager::getAllOrderPaymentStates()
  286.             ]);
  287.     }
  288.     public function retrieveShopData(Request $request)
  289.     {
  290.         $customer CustomerService::retrieveCustomerFromShopUser($this->getUser());
  291.         $adminGridService $this->container->get('app.service.order.shop_grid');
  292.         if ($customer) {
  293.             return new Response(json_encode($adminGridService->retrieveOrderGridData($customer)));
  294.         } else {
  295.             return $this->redirectToRoute('sylius_shop_homepage');
  296.         }
  297.     }
  298.     public function retrieveData(Request $request): JsonResponse
  299.     {
  300.         $partnerId SystemService::retrievePartnerId($this->getUser());
  301.         $userId SystemService::retrieveUserId($this->getUser());
  302.         /** @var AdminGridService $adminGridService */
  303.         $adminGridService $this->container->get('app.service.order.admin_grid');
  304.         return new JsonResponse($adminGridService->retrieveOrderGridData($partnerId$userId));
  305.     }
  306.     public function exportWithDateRanges(Request $request): RedirectResponse
  307.     {
  308.         $adminGridService $this->container->get('app.service.order.admin_grid');
  309.         $from $request->get('from');
  310.         $to $request->get('to');
  311.         if ($data $adminGridService->retrieveAllOrderWithDateRanges($from$to)) {
  312.             ExportService::exportArray($data);
  313.         } else {
  314.             $this->addFlash('error''A intervenit o eroare. Verificati intervalul de date selectat.');
  315.         }
  316.         return $this->redirectToRoute('sylius_admin_order_index');
  317.     }
  318.     public function retrieveProducts(Request $request)
  319.     {
  320.         $order $this->getOrderRepository()->find($request->get('id'));
  321.         return new Response(json_encode(OrderService::retrieveProductsForGridOrder($order)));
  322.     }
  323.     public function filterOrders(Request $request)
  324.     {
  325.         $result json_encode('fail');
  326.         $exportService = new ExportService($this->repository$request$this->manager);
  327.         $orders $exportService->getDataForExportOrders($request->request->all());
  328.         if ($orders) {
  329.             $result =  $exportService->exportOrdersJs($orders);
  330.         }
  331.         return new Response(json_encode($result));
  332.     }
  333.     public function exportAction(Request $request)
  334.     {
  335.         $exportService = new ExportService($this->repository$request$this->manager);
  336.         $orders $exportService->getData();
  337.         if ($orders && !empty($orders)) {
  338.             $exportService->exportOrders($orders);
  339.         }
  340.         echo "<script>window.close();</script>";
  341.         return new Response('success');
  342.     }
  343.     /**
  344.      * @param Request $request
  345.      * @return Response
  346.      */
  347.     public function exportOrderAction(Request $request)
  348.     {
  349.         $filterOderModel = new FilterOrderModel($this->manager$this);
  350.         $response = new Response('success');
  351.         $orders $filterOderModel->filterOrders($request->request->all());
  352.         $exportService = new ExportService($this->repository$request$this->manager);
  353.         if ($orders) {
  354.             $exportService->exportOrdersWithSqlData($orders);
  355.         } else {
  356.             $response = new Response('fail');
  357.         }
  358.         return $response;
  359.     }
  360.     public function exportOpOrder(Request  $request)
  361.     {
  362.         $orderId $request->get('orderId');
  363.         $exportService = new ExportService($this->repository$request$this->getDoctrine()->getManager());
  364.         $order $this->getDoctrine()->getRepository(Order::class)->find($orderId);
  365.         if ($order) {
  366.             $exportService->exportOpOrder($order);
  367.         }
  368.         echo "<script>window.close();</script>";
  369.         return new Response('success');
  370.     }
  371.     public function cancelAction(Request $request): Response
  372.     {
  373.         $orderId $request->get('orderId');
  374.         if (ShopUserModel::checkIfUserIsAdmin($this->getUser(), $this->getDoctrine()->getManager())) {
  375.             $order $this->getDoctrine()->getRepository(Order::class)->find($orderId);
  376.             if (OrderModel::checkIfOrderCanBeCanceled($order)) {
  377.                 OrderModel::cancelOrder($order$this->getDoctrine()->getManager());
  378.                 $this->addFlash('success''Comanda anulata cu succes');
  379.             }
  380.         } else {
  381.             $this->addFlash('error''A intervenit o eroare');
  382.         }
  383.         return $this->redirectToRoute('sylius_admin_order_show', [
  384.             'id' => $orderId
  385.         ]);
  386.     }
  387.     public function showDetailsModal(Request $request)
  388.     {
  389.         $order $this->getOrderRepository()->find($request->get('orderId'));
  390.         $orderData OrderService::retrieveProductsForGridOrder($order);
  391.         $render $this->get('twig')->render('@templates/Account/Order/Modal/_order_details.html.twig', ['orderData' => $orderData]);
  392.         return new Response($render);
  393.     }
  394.     public function partnerIndexAction(Request $request)
  395.     {
  396.         $partnerId SystemService::retrievePartnerId($this->getUser());
  397.         $userId SystemService::retrieveUserId($this->getUser());
  398.         $paymentMethods = array();
  399.         $allPaymentMethods $this->getDoctrine()->getRepository(VignettePaymentMethod::class)->findAll();
  400.         foreach ($allPaymentMethods as $paymentMethod) {
  401.             $paymentMethods[$paymentMethod->getId()] = $paymentMethod->getName();
  402.         }
  403.         return $this->render('@templates/AdminCustom/Grid/Order/index.html.twig',
  404.             [
  405.                 'paymentMethods' => $paymentMethods,
  406.                 'states' => OrderManager::getAllOrderStates(),
  407.                 'paymentStates' => OrderManager::getAllOrderPaymentStates(),
  408.                 'partnerId' => $partnerId,
  409.                 'userId' => $userId,
  410.             ]);
  411.     }
  412.     public function checkIframeOrderItemsStatus(Request $request): Response
  413.     {
  414.         $token $request->get('token');
  415.         $orderId $request->get('ext_order_id');
  416.         $partner $this->getDoctrine()->getRepository(Partner::class)->findOneBy(array('publicToken' => $token));
  417.         $order $this->getOrderRepository()->find($orderId);
  418.         if (!$order || !$order->hasPartner()) {
  419.             return new JsonResponse([
  420.                 'message' => 'Invalid order id',
  421.                 'code' => 403
  422.             ]);
  423.         } elseif (!$partner || !$partner->getId() == $order->getPartner()->getId()) {
  424.             return new JsonResponse([
  425.                 'message' => 'Invalid token or order partner missmatch',
  426.                 'code' => 402
  427.             ]);
  428.         } else {
  429.             return new JsonResponse(OrderService::retrieveProductsStatusByOrder($order$this->container->get('router')));
  430.         }
  431.     }
  432. }