vendor/simpledot/ecommerce-bundle/AWEcommerceBundle/Controller/Front/OrderController.php line 70

Open in your IDE?
  1. <?php
  2. namespace AWEcommerceBundle\Controller\Front;
  3. use AppBundle\Entity\Ecommerce\ProductVariantCustom;
  4. use AWCmsBundle\Exception\CmsException;
  5. use AWCmsBundle\Manager\SiteManager;
  6. use AppBundle\Entity\Ecommerce\Address;
  7. use AppBundle\Entity\Ecommerce\Order;
  8. use AppBundle\Entity\Ecommerce\OrderItem;
  9. use AWCmsBundle\Security\CurrentUserProvider;
  10. use AWEcommerceBundle\Entity\Setting;
  11. use AWEcommerceBundle\Entity\Shipping\DelivengoShippingMethod;
  12. use AWEcommerceBundle\Entity\Shipping\ShippingMethod;
  13. use AWEcommerceBundle\Event\OrderEvent;
  14. use AWEcommerceBundle\Exception\OutOfStockException;
  15. use AWEcommerceBundle\Manager\OrderItemManager;
  16. use AWEcommerceBundle\Manager\OrderManager;
  17. use AWEcommerceBundle\Service\Shipping\DelivengoShippingService;
  18. use AWEcommerceBundle\Service\Shipping\SendCloudShippingService;
  19. use AWEcommerceBundle\Service\Shipping\ShippingService;
  20. use GuzzleHttp\Client;
  21. use GuzzleHttp\Exception\ClientException;
  22. use Knp\Snappy\Pdf;
  23. use Psr\Log\LoggerInterface;
  24. use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
  25. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  26. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  27. use Symfony\Component\Config\Definition\Exception\Exception;
  28. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  29. use Symfony\Component\HttpFoundation\JsonResponse;
  30. use Symfony\Component\HttpFoundation\RedirectResponse;
  31. use Symfony\Component\HttpFoundation\Request;
  32. use Symfony\Component\HttpFoundation\Response;
  33. use Symfony\Component\Intl\Intl;
  34. use Symfony\Component\Process\Process;
  35. use Symfony\Component\Routing\Exception\ResourceNotFoundException;
  36. use Symfony\Contracts\Translation\TranslatorInterface;
  37. /**
  38.  * Class OrderController
  39.  * @Route("/order")
  40.  * @package AWEcommerceBundle\Controller\Admin
  41.  */
  42. class OrderController extends AbstractController
  43. {
  44.     /**
  45.      * @Route("/init/{variant}/{quantity}", name="aw_ec_order_init")
  46.      * @param OrderManager $orderManager
  47.      * @param Request $request
  48.      * @param EventDispatcherInterface $eventDispatcher
  49.      * @param TranslatorInterface $translator
  50.      * @param ProductVariantCustom $variant
  51.      * @param CurrentUserProvider $currentUserProvider
  52.      * @param int $quantity
  53.      * @return RedirectResponse|Response
  54.      */
  55.     public function initOrderAction(
  56.         OrderManager $orderManager,
  57.         Request $request,
  58.         EventDispatcherInterface $eventDispatcher,
  59.         TranslatorInterface $translator,
  60.         ProductVariantCustom $variant,
  61.         CurrentUserProvider $currentUserProvider,
  62.         int $quantity)
  63.     {
  64.         $user $currentUserProvider->get();
  65.         
  66.         try{
  67.             $orderManager->addProductVariantToUser($user$variant$quantity$request->get('customText'));
  68.         }catch (OutOfStockException $exception) {
  69.             return new Response($translator->trans('api.ecommerce.order_item.error.out_of_stock'), Response::HTTP_BAD_REQUEST);
  70.         }
  71.         
  72.         return $this->redirectToRoute('aw_ec_cart_index');
  73.     }
  74.     
  75.     /**
  76.      * @Route("/download/{orderId}", requirements={"orderId" = "\d+"}, name="aw_ec_order_download")
  77.      *
  78.      * @param SiteManager $siteManager
  79.      * @param $orderId
  80.      * @return Response
  81.      */
  82.     public function downloadAction(SiteManager $siteManagerPdf $pdf$orderId)
  83.     {
  84.         $order $this->getDoctrine()->getRepository(Order::class)->find($orderId);
  85.         $this->denyAccessUnlessGranted('edit'$order);
  86.         $settings null;
  87.         $site $siteManager->getSite();
  88.         
  89.         $settings $this->getDoctrine()->getManager()->getRepository(Setting::class)->findOneBy(['site' => $site]);
  90.         
  91.         /** @var \AppBundle\Entity\Ecommerce\Order $order */
  92.         if($order->getBillingNumber() == null)
  93.             throw new Exception('This order doesn\'t have a number');
  94.         
  95.         
  96.         $html $this->renderView('@AWEcommerce/billing/show.html.twig', array(
  97.             'order'  => $order,
  98.             'settings'$settings
  99.         ));
  100.         
  101.         return new Response(
  102.             $pdf->getOutputFromHtml($html),
  103.             200,
  104.             array(
  105.                 'Content-Type'          => 'application/pdf',
  106.                 'Content-Disposition'   => 'attachment; filename="'.$order->getBillingNumber().'.pdf"'
  107.             )
  108.         );
  109.     }
  110.     
  111.     /**
  112.      * @Route("/label/pdf/{order}", requirements={"order" = "\d+"}, name="aw_ec_shipping_label_pdf")
  113.      * @param ShippingService $shippingService
  114.      * @param SendCloudShippingService $sendCloudShippingService
  115.      * @param DelivengoShippingService $delivengoShippingService
  116.      * @param OrderManager $orderManager
  117.      * @param Order $order
  118.      * @return Response
  119.      * @throws CmsException
  120.      */
  121.     public function downloadLabelPdfAction(
  122.         ShippingService $shippingService,
  123.         SendCloudShippingService $sendCloudShippingService,
  124.         DelivengoShippingService $delivengoShippingService,
  125.         OrderManager $orderManager,
  126.         $order
  127.     )
  128.     {
  129.         /** @var Order $order */
  130.         $order $this->getDoctrine()->getRepository(Order::class)->find($order);
  131.         
  132.         if(!$shippingService->shippingServiceExists(SendCloudShippingService::CONNECTOR_NAME)){
  133.             throw new CmsException('Sendcloud is not enabled for this site. Please talk with administrators.'Response::HTTP_BAD_REQUEST);
  134.         }
  135.         
  136.         //TODO refactor
  137.         if(!$order->getUsedShippingMethod()
  138.             or ($order->getUsedShippingMethod()->getServiceName() != SendCloudShippingService::CONNECTOR_NAME and $order->getUsedShippingMethod()->getServiceName() != DelivengoShippingMethod::SERVICE_NAME)){
  139.             throw new CmsException('This order doesn\'t use the sendcloud service.'Response::HTTP_BAD_REQUEST);
  140.         }
  141.         
  142.         $this->denyAccessUnlessGranted('edit'$order);
  143.         
  144.         if($order->getUsedShippingMethod()->getServiceName() == DelivengoShippingMethod::SERVICE_NAME){
  145.             return $this->printDelivengo($orderManager$delivengoShippingService$order);
  146.         }
  147.         else{
  148.             return $this->printSendCloud($orderManager$sendCloudShippingService$order);
  149.         }
  150.         
  151.         
  152.     }
  153.     
  154.     /**
  155.      * @param OrderManager $orderManager
  156.      * @param DelivengoShippingService $delivengoShippingService
  157.      * @param Order $order
  158.      * @return Response
  159.      */
  160.     protected function printDelivengo(
  161.         OrderManager $orderManager,
  162.         DelivengoShippingService $delivengoShippingService,
  163.         Order $order
  164.     ): Response
  165.     {
  166.         $pdf $delivengoShippingService->shippingPdf($order);
  167.         $orderManager->updateState($orderOrder::STATE_SENT);
  168.         return new Response(
  169.                 $pdf,
  170.                 Response::HTTP_OK,
  171.                 [
  172.                     'Content-Type' => 'application/pdf',
  173.                     'Content-Disposition' => 'attachment; filename="label-' $order->getNumber() . '.pdf"'
  174.                 ]
  175.             );
  176.     }
  177.     
  178.     /**
  179.      * @param OrderManager $orderManager
  180.      * @param SendCloudShippingService $sendCloudShippingService
  181.      * @param Order $order
  182.      * @return Response
  183.      */
  184.     protected function printSendCloud(
  185.         OrderManager $orderManager,
  186.         SendCloudShippingService $sendCloudShippingService,
  187.         Order $order): Response
  188.     {
  189.         if (!$order->getShippingApiId()) {
  190.             $sendCloudShippingService->createParcel($orderfalse);
  191.         }
  192.         $pdfUrl $sendCloudShippingService->getPrinterInfo($order);
  193.         if (!$pdfUrl) {
  194.             throw new ResourceNotFoundException("Label not found. Please, revise on https://panel.sendcloud.sc"Response::HTTP_NOT_FOUND);
  195.         }
  196.         $orderManager->updateState($orderOrder::STATE_SENT);
  197.         
  198.         return new Response(
  199.             file_get_contents($pdfUrl),
  200.             Response::HTTP_OK,
  201.             [
  202.                 'Content-Type' => 'application/pdf',
  203.                 'Content-Disposition' => 'attachment; filename="label-' $order->getNumber() . '.pdf"'
  204.             ]
  205.         );
  206.     }
  207. }