src/Controller/Vignette/VignetteController.php line 88

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller\Vignette;
  4. use App\Controller\BaseController;
  5. use App\Entity\Customer\Vehicle;
  6. use App\Entity\VignetteInvoice\VignetteInvoice;
  7. use App\Manager\BitbagStatic\BlockManager;
  8. use App\Entity\Order\Order;
  9. use App\Entity\Order\OrderItem;
  10. use App\Entity\Vignette\Vignette;
  11. use App\Entity\RoadAssistance\RoadAssistance;
  12. use App\Events\VignetteEvent;
  13. use App\Manager\CountryManager;
  14. use App\Manager\Partner\PartnerManager;
  15. use App\Manager\RoadAssistance\RoadAssistanceManager;
  16. use App\Manager\Vignette\VignetteManager;
  17. use App\Model\VignetteAvailabilityModel;
  18. use App\Model\VignetteCategoryModel;
  19. use App\Service\CustomerService;
  20. use App\Service\InvoiceApiService;
  21. use App\Service\InvoiceService;
  22. use App\Service\ShoppingCartService;
  23. use App\Service\StaticPagesBitbagService;
  24. use App\Service\SystemService;
  25. use App\Service\VignetteApiService;
  26. use App\Service\VignetteOrderService;
  27. use App\Validator\VignetteFormValidator;
  28. use DateTime;
  29. use Detection\MobileDetect;
  30. use Symfony\Component\HttpFoundation\Request;
  31. use Symfony\Component\HttpFoundation\Response;
  32. use Symfony\Contracts\Translation\TranslatorInterface;
  33. use function Sentry\captureException;
  34. final class VignetteController extends BaseController
  35. {
  36.     const OP_ID 1;
  37.     const REDIRECT_LOCALE_LIST = array('tr''bg''en''hu');
  38.     public function vignetteAction(Request $requestTranslatorInterface $translator): Response
  39.     {
  40.         $guid $request->get('guid');
  41.         $renewRoVignetteVehicleId $request->get('vehicleId');
  42.         $vehicle null;
  43.         $oldVignette null;
  44.         if (!empty($renewRoVignetteVehicleId)) {
  45.             $vehicle $this->getEntityManager()->getRepository(Vehicle::class)->find($renewRoVignetteVehicleId);
  46.             $customer CustomerService::retrieveCustomerFromShopUser($this->getUser());
  47.             if (!empty($vehicle) && $customer) {
  48.                 $vehicle->setRoVignetteCategory(null);
  49.                 if (empty($customer->getId()) || ($customer->getId() !== $vehicle->getCustomer()->getId())) {
  50.                     $vehicle null;
  51.                 } else {
  52.                     $oldVignette $this->getEntityManager()->getRepository(Vignette::class)->findLatestConfirmedRoVignetteByRegistrationPlate($vehicle->getRegistrationPlate());
  53.                 }
  54.             }
  55.         }
  56.         $desktopView SystemService::checkDesktopView($request);
  57.         $render '@templates/Front/vignette.html.twig';
  58.         if ($this->isMobile && !$desktopView) {
  59.             $render '@templates/Mobile/vignette.html.twig';
  60.         }
  61.         /* $locale = $request->getLocale();
  62.          * if(!$this->getUser()) {
  63.             if ($locale !== 'ro'){
  64.                 return $this->redirect('https://www.scala.ro/' . $locale . '/rovinieta');
  65.             }
  66.         }*/
  67.         $request->getSession()->remove('iframeSourceLink');
  68.         $vignetteCategories = (new VignetteCategoryModel($this->getEntityManager()))->getAllCategories();
  69.         return $this->render($render, [
  70.             'vignetteCategories' => $vignetteCategories,
  71.             'countries' => CountryManager::getActiveCountriesForRoVignette($this->getEntityManager()),
  72.             'guid' => $guid,
  73.             'vehicle' => $vehicle,
  74.             'oldValidFrom' => $oldVignette VignetteManager::retrieveRoVignetteOldValidUntil($oldVignette) : null,
  75.             'accordionBlocksAboutVignette' => StaticPagesBitbagService::getAllFilteredBlocks($this->getEntityManager(), BlockManager::BLOCK_ACCORDION_TEXT_VIGNETTE_CODE_PATTERN),
  76.             'vignetteKeywordsForSeo' => StaticPagesBitbagService::getFirstFilteredBlocks($this->getEntityManager(), BlockManager::BLOCK_SEO_META_VIGNETTE_KEYWORD_CODE_PATTERN),
  77.             'vignetteDescriptionForSeo' => StaticPagesBitbagService::getFirstFilteredBlocks($this->getEntityManager(), BlockManager::BLOCK_SEO_META_VIGNETTE_DESCRIPTION_CODE_PATTERN),
  78.             'roadAssistanceTranslations' => RoadAssistanceManager::getTranslationsForUI($translator)
  79.         ]);
  80.     }
  81.     public function removeVignetteFromOrder(Request $request): Response
  82.     {
  83.         $serializer $this->getContainer()->get('serializer');
  84.         $vignetteId $request->get('vignetteId');
  85.         $vignetteRepository $this->getDoctrine()->getRepository(Vignette::class);
  86.         $vignette $vignetteRepository->find($vignetteId);
  87.         $order $vignette->getOrder();
  88.         if ($order->getVignettes() && $order->getVignettes()->count() == 1) {
  89.             $response $serializer->serialize(array('status' => 0'message' => 'Numarul minim de viniete ramase pe comanda trebuie sa fie mai mare ca 1 dupa stergere'), 'json');
  90.             return new Response($response);
  91.         }
  92.         $order->removeVignette($vignette);
  93.         $orderItem $vignette->getOrderItem();
  94.         $this->getEntityManager()->getRepository(OrderItem::class)->deleteOrderItemById($orderItem->getId());
  95.         if ($sms $vignette->getSms()) {
  96.             $smsOrderItem $this->getEntityManager()->getRepository(OrderItem::class)->findOrderItemBySms($sms);
  97.             if ($smsOrderItem) {
  98.                 $this->getEntityManager()->getRepository(OrderItem::class)->deleteOrderItemById($smsOrderItem->getId());
  99.             }
  100.         }
  101.         $this->getDoctrine()->getManager()->flush();
  102.         $order->recalculateItemsTotal();
  103.         $order->recalculateAdjustmentsTotal();
  104.         $this->getDoctrine()->getManager()->flush();
  105.         $response $serializer->serialize(array('status' => 1'message' => 'success'), 'json');
  106.         return new Response($response);
  107.     }
  108.     public function loadVinBlock()
  109.     {
  110.         $template json_encode('error');
  111.         try {
  112.             $template $this->get('twig')
  113.                 ->render('@templates/Front/vignette/_vin_block.html.twig');
  114.             $detect = new MobileDetect();
  115.             if ($detect->isMobile()) {
  116.                 $template $this->get('twig')
  117.                     ->render('@templates/Mobile/vignette/_vin_block.html.twig');
  118.             }
  119.         } catch (\Throwable $exception) {
  120.             captureException($exception);
  121.         }
  122.         return new Response($template);
  123.     }
  124.     public function previewVignettesData(Request $request)
  125.     {
  126.         $response json_encode('fail');
  127.         $serializer $this->getContainer()->get('serializer');
  128.         $params $request->request->all();
  129.         $orderId $params['orderId'];
  130.         if ($orderId == null) {
  131.             $response $serializer->serialize('id comanda gol''json');
  132.             return new Response($response);
  133.         }
  134.         $orderRepository $this->getDoctrine()->getRepository(Order::class);
  135.         $order $orderRepository->find($orderId);
  136.         $customer $order->getCustomer();
  137.         $vignettes $order->getVignettes();
  138.         foreach ($vignettes as $key => $vignette) {
  139.             $validFrom $params['pick_date_op' $key];
  140.             $validFromDateTime = new DateTime($validFrom);
  141.             $vignette->setValidFrom($validFromDateTime);
  142.             $validUntil = new DateTime();
  143.             switch ($vignette->getAvailability()->getName()) {
  144.                 case '1 zi':
  145.                     $validUntil date('Y-m-d 23:59:59',strtotime($validFrom));
  146.                     break;
  147.                 case '7 zile':
  148.                     $validUntil date('Y-m-d 23:59:59',strtotime($validFrom ' + 6 days'));
  149.                     break;
  150.                 case '30 zile':
  151.                     $validUntil date('Y-m-d 23:59:59'strtotime($validFrom ' + 29 days'));
  152.                     break;
  153.                 case '90 zile':
  154.                     $validUntil date('Y-m-d 23:59:59'strtotime($validFrom ' + 89 days'));
  155.                     break;
  156.                 case '12 luni':
  157.                     $validUntil date('Y-m-d 23:59:59'strtotime($validFrom ' + 364 days'));
  158.                     break;
  159.                 default:
  160.                     break;
  161.             }
  162.             $validUntilDateTime = new DateTime($validUntil);
  163.             $vignette->setValidUntil($validUntilDateTime);
  164.         }
  165.         $vignettesApi VignetteApiService::getVignettesForInvoiceApi($vignettes);
  166.         $result InvoiceApiService::previewData($customer$vignettesApi);
  167.         $result json_decode($result);
  168.         if (isset($result->dload_url) && $result->dload_url) {
  169.             $response json_encode($result->dload_url);
  170.         }
  171.         return new Response($response);
  172.     }
  173.     public function getAvailabilitiesByCategoryAction(Request $request): Response
  174.     {
  175.         $id $request->get('id');
  176.         $availabilities null;
  177.         if ($id != null) {
  178.             $availabilities = (new VignetteAvailabilityModel($this->getEntityManager()))
  179.                 ->getAvailabilityForCategoryId($id);
  180.             $serializer $this->getSerializer();
  181.             $availabilities $serializer->serialize($availabilities'json');
  182.         }
  183.         return new Response($availabilities);
  184.     }
  185.     public function vignetteFormSubmitAction(Request $request): Response
  186.     {
  187.         $session $request->getSession();
  188.         $validator = new VignetteFormValidator($this->getEntityManager(), $this->getSerializer());
  189.         $request->request->set('validFrom'str_replace('-''/'$request->get('validFrom')));
  190.         $errors null;
  191.         if ($validator->checkForm($request)) {
  192.             $errors json_encode(array('success' => 1));
  193.         } else {
  194.             if ($session->has('vignetteErrors')) {
  195.                 $errors $session->get('vignetteErrors');
  196.             }
  197.         }
  198.         $session->set('previousUrl''rovinieta');
  199.         return new Response($errors);
  200.     }
  201.     public function vignetteFormSubmitApi(Request $requestTranslatorInterface $translator): Response
  202.     {
  203.         $response null;
  204.         $isRovinietaIframe $request->get('rovinietaIframe');
  205.         $request->request->set('validFrom'str_replace('-''/'$request->get('validFrom')));
  206.         $validRegistrationNumber VignetteOrderService::checkIfVignetteIsAlreadyInCart($request$this->getEntityManager());
  207.         if ($isRovinietaIframe && !$validRegistrationNumber) {
  208.             //clear cart
  209.             if (ShoppingCartService::removeVignetteFromCartByRegistrationPlate($request)){
  210.                 $validRegistrationNumber true;
  211.             }
  212.         }
  213.         if ($validRegistrationNumber) {
  214.             $vignetteEvent = new VignetteEvent();
  215.             $this->dispatcher->addListener('check.vignette', function (VignetteEvent $event) {
  216.                 $event->stopPropagation();
  217.             });
  218.             $this->dispatcher->dispatch($vignetteEventVignetteEvent::CHECK_VIGNETTE);
  219.             if ($vignetteEvent->isPropagationStopped()) {
  220.                 if ($request->request->has('vignetteSubmitForm')
  221.                     && $request->request->get('vignetteSubmitForm') == 1) {
  222.                     $response json_encode(array('success' => true));
  223.                     if (RoadAssistanceManager::isVehicleEligibleForRoadAssistance($request->request->all())
  224.                         && !RoadAssistanceManager::haveAnActiveRoadAssistanceForRegistrationPlate($request$this->getEntityManager())) {
  225.                         $roadAssistanceService $this->getContainer()->get('app.service.road_assistance.road_assistance');
  226.                         $roadAssistanceResponse $roadAssistanceService->storeNewRoadAssistanceInCart(RoadAssistance::FLAG_SOURCE_EXTRA_OPTION);
  227.                         if (!$roadAssistanceResponse['success']) {
  228.                             if ($roadAssistanceResponse['errorCode']) {
  229.                                 $this->addFlash('error'SystemService::retrieveMessage($roadAssistanceResponse['errorCode'], $translator));
  230.                             }
  231.                         }
  232.                     }
  233.                 } elseif ($request->request->has('specialErrors')) {
  234.                     $vignetteApiError $request->request->get('specialErrors');
  235.                     $response json_encode(array('vignetteApiError' => $vignetteApiError));
  236.                 }
  237.             }
  238.         } else {
  239.             $vignetteApiError = array('alreadyInCart' => 'Already in cart');
  240.             $apiStatusCode 400;
  241.             $response json_encode(array('vignetteApiError' => $vignetteApiError'apiStatusCode' => $apiStatusCode));
  242.         }
  243.         return new Response($response);
  244.     }
  245.     public function getMonth(Request $requestTranslatorInterface $translator): Response
  246.     {
  247.         $monthNumber $request->get('month_number');
  248.         $locale $request->get('locale');
  249.         $monthName null;
  250.         if ($monthNumber != null) {
  251.             $translationMonthKey 'app.ui.month_' $monthNumber;
  252.             $monthName =  $translator->trans($translationMonthKey, [], null$locale);
  253.             $serializer $this->getSerializer();
  254.             $monthName $serializer->serialize($monthName'json');
  255.         }
  256.         return new Response($monthName);
  257.     }
  258.     public function roVignetteIframe(Request $requestTranslatorInterface $translator)
  259.     {
  260.         $partnerService $this->getContainer()->get('app.service.partner.partner');
  261.         $partnerService->verifyToken($requestPartnerManager::CAN_ISSUE_RO_VIGNETTE_INDEX);
  262.         $token['partnerPublicToken'] = $request->get(PartnerManager::SESSION_PARTNER_PUBLIC_TOKEN_NAME);
  263.         $request->getSession()->set('iframeSourceLink'$this->generateUrl('app_shop_ro_vignette_iframe'$token));
  264.         $partner $partnerService->retrievePartner($request->get('partnerPublicToken'));
  265.         $hasAlertSms = ($partner->getRole())->getCanAlertSms();
  266.         $canIssueRoadAssistance = ($partner->getRole())->getCanIssueRoadAssistance();
  267.         $guid $request->get('guid');
  268.         $desktopView SystemService::checkDesktopView($request);
  269.         $render '@templates/Front/vignette/iframe.html.twig';
  270.         if ($this->isMobile && !$desktopView) {
  271.             $render '@templates/Mobile/vignette/iframe.html.twig';
  272.         }
  273.         $vignetteCategories = (new VignetteCategoryModel($this->getEntityManager()))->getAllCategories();
  274.         PartnerManager::checkQRCodeFlow($request);
  275.         return $this->render($render, [
  276.             'vignetteCategories' => $vignetteCategories,
  277.             'countries' => CountryManager::getActiveCountriesForRoVignette($this->getEntityManager()),
  278.             'guid' => $guid,
  279.             'hasAlertSms' => $hasAlertSms,
  280.             'canIssueRoadAssistance' => $canIssueRoadAssistance,
  281.             'accordionBlocksAboutVignette' => StaticPagesBitbagService::getAllFilteredBlocks($this->getEntityManager(), BlockManager::BLOCK_ACCORDION_TEXT_VIGNETTE_CODE_PATTERN),
  282.             'vignetteKeywordsForSeo' => StaticPagesBitbagService::getFirstFilteredBlocks($this->getEntityManager(), BlockManager::BLOCK_SEO_META_VIGNETTE_KEYWORD_CODE_PATTERN),
  283.             'vignetteDescriptionForSeo' => StaticPagesBitbagService::getFirstFilteredBlocks($this->getEntityManager(), BlockManager::BLOCK_SEO_META_VIGNETTE_DESCRIPTION_CODE_PATTERN),
  284.             'roadAssistanceTranslations' => RoadAssistanceManager::getTranslationsForUI($translator),
  285.             'publicToken' => $partner->getPublicToken()
  286.         ]);
  287.     }
  288.     public function showVignettePolicyForPartner(Request $request)
  289.     {
  290.         $response = new Response(json_encode(array('status' => 0)));
  291.         $invoice $this->getDoctrine()->getRepository(VignetteInvoice::class)->findOneBy(array('order' => base64_decode($request->get('order_id'))));
  292.         if ($invoice) {
  293.             $apiInvoiceService = new InvoiceApiService();
  294.             $apiInvoiceService->getPdfByInvoice($invoice);
  295.             if ( $response $apiInvoiceService->getLastResponse()) {
  296.                 $response json_decode($response);
  297.                 if ($response->invoice_id 0) {
  298.                     $url $response->dload_url;
  299.                     $filename 'Factura';
  300.                     InvoiceService::openPdf($url$filename);
  301.                     $response = new Response(json_encode(array('status' => 1)));
  302.                 }
  303.             }
  304.         }
  305.         return $response;
  306.     }
  307. }