<?php
namespace App\Controller\Telemedicine;
use App\Controller\BaseController;
use App\Entity\Order\Order;
use App\Entity\Telemedicine\Telemedicine;
use App\Entity\VignetteInvoice\VignetteInvoice;
use App\Manager\Cart\CartManager;
use App\Manager\Partner\PartnerManager;
use App\Manager\Telemedicine\TelemedicineManager;
use App\Service\AuthTokenService;
use App\Service\InvoiceApiService;
use App\Service\InvoiceService;
use App\Service\SystemService;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
class TelemedicineController extends BaseController
{
public function indexAction(): Response
{
return $this->render('@templates/Telemedicine/telemedicine.html.twig', [
'productPrice' => Telemedicine::SELL_PRICE
]);
}
public function submitData(Request $request): Response
{
$request->request->set('telemedicineValidFrom', date("Y-m-d H:i:s"));
$telemedicineResponse = $this->getContainer()->get('app.service.telemedicine.telemedicine')
->storeNewTelemedicineInCart(Telemedicine::FLAG_SOURCE_PRODUCT_PAGE);
return new JsonResponse($telemedicineResponse);
}
public function removeFromCart(): Response
{
$telemedicineService = $this->getContainer()->get('app.service.telemedicine.telemedicine');
$result = $telemedicineService->removeTelemedicineFromCartSession();
return new JsonResponse(['result' => $result]);
}
public function addToCartById(Order $order, Request $request): Response
{
$clientIp = $request->getClientIp();
$session = $request->getSession();
foreach ($order->getTelemedicine() as $telemedicine) {
if (!TelemedicineManager::isTelemedicineAlreadyInCart($session)) {
$cartItem = TelemedicineManager::constructTelemedicineForCartFromObject(
$telemedicine, $clientIp, Telemedicine::FLAG_SOURCE_REMARKETING
);
CartManager::setToCart($session, CartManager::TELEMEDICINE_PRODUCT_NAME, [$cartItem]);
}
}
$authTokenService = new AuthTokenService();
if ($token = $authTokenService->authenticateUserWithToken($request, $this->getEntityManager())) {
$this->get('security.token_storage')->setToken($token);
$event = new InteractiveLoginEvent($request, $token);
$this->dispatcher->dispatch($event, 'security.interactive_login');
$session = $request->getSession();
$session->set('_sylius.cart.FASHION_WEB', 180);
$session->save();
}
$utm = [
'utm_source' => $request->get('utm_source'),
'utm_medium' => $request->get('utm_medium'),
'utm_campaign' => $request->get('utm_campaign'),
'utm_term' => $request->get('utm_term'),
'utm_content' => $request->get('utm_content'),
'utm_id' => $request->get('utm_id')
];
return $this->render('@templates/ShopCustom/Redirect/wait_to_be_redirected.html.twig',[
'url' => $this->generateUrl('app_shop_customer_info', $utm)
]);
}
public function telemedicineIframe(Request $request): Response
{
$partnerService = $this->getContainer()->get('app.service.partner.partner');
$partnerService->verifyToken($request, PartnerManager::CAN_ISSUE_TELEMEDICINE_INDEX);
$partner = $partnerService->retrievePartner($request->get('partnerPublicToken'));
$request->getSession()->set('iframeSourceLink', $this->generateUrl('app_shop_telemedicine_iframe', [
'partnerPublicToken' => $partner->getPublicToken()
]));
PartnerManager::checkQRCodeFlow($request);
return $this->render('@templates/Front/Telemedicine/iframe.html.twig', [
'productPrice' => Telemedicine::SELL_PRICE,
'publicToken' => $partner->getPublicToken()
]);
}
public function showPolicyForPartner(Request $request): Response
{
$id = base64_decode($request->get('order_id'));
$em = $this->getEntityManager();
$order = $em->getRepository(Order::class)->find($id);
if (!$order->hasTelemedicine()) {
return new Response('error');
}
$apiInvoiceService = new InvoiceApiService();
$invoice = $em->getRepository(VignetteInvoice::class)
->findOneBy(['order' => $id, 'productType' => VignetteInvoice::PRODUCT_TELEMEDICINE]);
$pdfContent = $apiInvoiceService->getInvoicePDFViaBillingService($invoice->getApiInvoiceId());
if (!$pdfContent) {
return new Response('error');
}
$invoiceFileName = SystemService::retrieveMessage('invoice', $this->getContainer()->get('translator'));
$filename = "{$invoiceFileName} {$invoice->getSeries()} {$invoice->getNumber()}";
InvoiceService::openPDFContent($pdfContent, $filename);
return new Response();
}
}