<?php
namespace AWEcommerceBundle\Controller\Front;
use AppBundle\Entity\Ecommerce\ProductVariantCustom;
use AWCmsBundle\Exception\CmsException;
use AWCmsBundle\Manager\SiteManager;
use AppBundle\Entity\Ecommerce\Address;
use AppBundle\Entity\Ecommerce\Order;
use AppBundle\Entity\Ecommerce\OrderItem;
use AWCmsBundle\Security\CurrentUserProvider;
use AWEcommerceBundle\Entity\Setting;
use AWEcommerceBundle\Entity\Shipping\DelivengoShippingMethod;
use AWEcommerceBundle\Entity\Shipping\ShippingMethod;
use AWEcommerceBundle\Event\OrderEvent;
use AWEcommerceBundle\Exception\OutOfStockException;
use AWEcommerceBundle\Manager\OrderItemManager;
use AWEcommerceBundle\Manager\OrderManager;
use AWEcommerceBundle\Service\Shipping\DelivengoShippingService;
use AWEcommerceBundle\Service\Shipping\SendCloudShippingService;
use AWEcommerceBundle\Service\Shipping\ShippingService;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use Knp\Snappy\Pdf;
use Psr\Log\LoggerInterface;
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Intl\Intl;
use Symfony\Component\Process\Process;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* Class OrderController
* @Route("/order")
* @package AWEcommerceBundle\Controller\Admin
*/
class OrderController extends AbstractController
{
/**
* @Route("/init/{variant}/{quantity}", name="aw_ec_order_init")
* @param OrderManager $orderManager
* @param Request $request
* @param EventDispatcherInterface $eventDispatcher
* @param TranslatorInterface $translator
* @param ProductVariantCustom $variant
* @param CurrentUserProvider $currentUserProvider
* @param int $quantity
* @return RedirectResponse|Response
*/
public function initOrderAction(
OrderManager $orderManager,
Request $request,
EventDispatcherInterface $eventDispatcher,
TranslatorInterface $translator,
ProductVariantCustom $variant,
CurrentUserProvider $currentUserProvider,
int $quantity)
{
$user = $currentUserProvider->get();
try{
$orderManager->addProductVariantToUser($user, $variant, $quantity, $request->get('customText'));
}catch (OutOfStockException $exception) {
return new Response($translator->trans('api.ecommerce.order_item.error.out_of_stock'), Response::HTTP_BAD_REQUEST);
}
return $this->redirectToRoute('aw_ec_cart_index');
}
/**
* @Route("/download/{orderId}", requirements={"orderId" = "\d+"}, name="aw_ec_order_download")
*
* @param SiteManager $siteManager
* @param $orderId
* @return Response
*/
public function downloadAction(SiteManager $siteManager, Pdf $pdf, $orderId)
{
$order = $this->getDoctrine()->getRepository(Order::class)->find($orderId);
$this->denyAccessUnlessGranted('edit', $order);
$settings = null;
$site = $siteManager->getSite();
$settings = $this->getDoctrine()->getManager()->getRepository(Setting::class)->findOneBy(['site' => $site]);
/** @var \AppBundle\Entity\Ecommerce\Order $order */
if($order->getBillingNumber() == null)
throw new Exception('This order doesn\'t have a number');
$html = $this->renderView('@AWEcommerce/billing/show.html.twig', array(
'order' => $order,
'settings', $settings
));
return new Response(
$pdf->getOutputFromHtml($html),
200,
array(
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="'.$order->getBillingNumber().'.pdf"'
)
);
}
/**
* @Route("/label/pdf/{order}", requirements={"order" = "\d+"}, name="aw_ec_shipping_label_pdf")
* @param ShippingService $shippingService
* @param SendCloudShippingService $sendCloudShippingService
* @param DelivengoShippingService $delivengoShippingService
* @param OrderManager $orderManager
* @param Order $order
* @return Response
* @throws CmsException
*/
public function downloadLabelPdfAction(
ShippingService $shippingService,
SendCloudShippingService $sendCloudShippingService,
DelivengoShippingService $delivengoShippingService,
OrderManager $orderManager,
$order
)
{
/** @var Order $order */
$order = $this->getDoctrine()->getRepository(Order::class)->find($order);
if(!$shippingService->shippingServiceExists(SendCloudShippingService::CONNECTOR_NAME)){
throw new CmsException('Sendcloud is not enabled for this site. Please talk with administrators.', Response::HTTP_BAD_REQUEST);
}
//TODO refactor
if(!$order->getUsedShippingMethod()
or ($order->getUsedShippingMethod()->getServiceName() != SendCloudShippingService::CONNECTOR_NAME and $order->getUsedShippingMethod()->getServiceName() != DelivengoShippingMethod::SERVICE_NAME)){
throw new CmsException('This order doesn\'t use the sendcloud service.', Response::HTTP_BAD_REQUEST);
}
$this->denyAccessUnlessGranted('edit', $order);
if($order->getUsedShippingMethod()->getServiceName() == DelivengoShippingMethod::SERVICE_NAME){
return $this->printDelivengo($orderManager, $delivengoShippingService, $order);
}
else{
return $this->printSendCloud($orderManager, $sendCloudShippingService, $order);
}
}
/**
* @param OrderManager $orderManager
* @param DelivengoShippingService $delivengoShippingService
* @param Order $order
* @return Response
*/
protected function printDelivengo(
OrderManager $orderManager,
DelivengoShippingService $delivengoShippingService,
Order $order
): Response
{
$pdf = $delivengoShippingService->shippingPdf($order);
$orderManager->updateState($order, Order::STATE_SENT);
return new Response(
$pdf,
Response::HTTP_OK,
[
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="label-' . $order->getNumber() . '.pdf"'
]
);
}
/**
* @param OrderManager $orderManager
* @param SendCloudShippingService $sendCloudShippingService
* @param Order $order
* @return Response
*/
protected function printSendCloud(
OrderManager $orderManager,
SendCloudShippingService $sendCloudShippingService,
Order $order): Response
{
if (!$order->getShippingApiId()) {
$sendCloudShippingService->createParcel($order, false);
}
$pdfUrl = $sendCloudShippingService->getPrinterInfo($order);
if (!$pdfUrl) {
throw new ResourceNotFoundException("Label not found. Please, revise on https://panel.sendcloud.sc", Response::HTTP_NOT_FOUND);
}
$orderManager->updateState($order, Order::STATE_SENT);
return new Response(
file_get_contents($pdfUrl),
Response::HTTP_OK,
[
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="label-' . $order->getNumber() . '.pdf"'
]
);
}
}