<?php
namespace App\Controller\DeathInsurance;
use App\Controller\BaseController;
use App\Entity\Customer\TrackerActivity;
use App\Entity\DeathInsurance\IssueData;
use App\Entity\Order\Order;
use App\Manager\DeathInsurance\DeathInsuranceManager;
use App\Manager\Partner\PartnerManager;
use App\Service\CustomerService;
use App\Service\DeathInsurance\AdminGridService;
use App\Service\DeathInsurance\DeathInsuranceService;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class DeathInsuranceController extends BaseController
{
public function indexAction(Request $request)
{
$customer = CustomerService::retrieveCustomerFromShopUser($this->getUser());
$request->getSession()->remove('iframeSourceLink');
$trackerActivityService = $this->getContainer()->get('app.service.customer.tracker_activity');
$trackerActivityService->startTracking($customer, TrackerActivity::FROM_DIRECT_URL, TrackerActivity::TYPE_DEATH_INSURANCE_ACTIVITY);
$render = '@templates/DeathInsurance/death-insurance.html.twig';
return $this->render($render, [
'issueDataList' => $this->getEntityManager()->getRepository(IssueData::class)->findAllAsArray(),
]);
}
/**
* @param Request $request
* @return Response
*/
public function removeFromCart(Request $request): Response
{
$deathInsuranceService = $this->getContainer()->get('app.service.death_insurance.death_insurance');
$result = $deathInsuranceService->removeDeathInsuranceFromCartSessionByCnp($request->get('cnp'));
return new Response(json_encode(array('result' => $result)));
}
public function checkAlreadyActive(Request $request): Response
{
$deathInsuranceService = $this->getContainer()->get('app.service.road_assistance.road_assistance');
return new Response(json_encode(array('isActive' => $deathInsuranceService->isAlreadyActiveForCnpAndValidFrom($request->request->all()))));
}
public function retrieveIssueData(Request $request): JsonResponse
{
/** @var AdminGridService $adminGridService */
$adminGridService = $this->getContainer()->get('app.service.death_insurance_issue_data.admin_grid');
return new JsonResponse($adminGridService->retrieveDeathInsuranceIssueGridData());
}
public function submitData(Request $request) : Response
{
$customer = CustomerService::retrieveCustomerFromShopUser($this->getUser());
$deathInsuranceService = $this->getContainer()->get('app.service.death_insurance.death_insurance');
$deathInsuranceTrackerActivityService = $this->getContainer()->get('app.service.death_insurance.tracker_activity');
if (DeathInsuranceManager::isDeathInsuranceIframe($request)) {
$deathInsuranceService->removeDeathInsuranceFromCartSessionByCnp($request->get('cnp'));
}
$deathInsuranceResponse = $deathInsuranceService->storeNewDeathInsuranceInCart();
$deathInsuranceTrackerActivityService->setDeathInsuranceSubmittedToCart($customer, $deathInsuranceResponse);
return new Response(json_encode($deathInsuranceResponse));
}
/**
* @param Request $request
* @return Response
*/
public function storePolicy(Request $request) : Response
{
$response = ['isStored' => false];
$localFilePath = $this->getContainer()->getParameter('kernel.project_dir') . '/var/storage/nn_insurance/policy_production/';
$deathInsuranceService = $this->getContainer()->get('app.service.death_insurance.death_insurance');
if (!is_dir($localFilePath)) {
// Third parameter 'true' makes the directory recursive, creating parent directories as needed
mkdir($localFilePath, 0777, true);
}
$data = json_decode($request->getContent(), true);
$urlPolicy = $data['urlPolicy'];
$filename = $data['fileName'];
if ($filename && $urlPolicy) {
$stored = $deathInsuranceService->storeDeathInsurancePolicyOnServer($localFilePath, $filename, $urlPolicy);
$response = ['isStored' => $stored];
}
return new Response(json_encode($response));
}
public function showPolicyForPartner(Request $request): Response
{
$id = $request->get('order_id');
$id = base64_decode($id);
$order = $this->getDoctrine()->getRepository(Order::class)->find($id);
if ($order->hasDeathInsurance()) {
$deathInsurance = $order->getDeathInsurance();
foreach ($deathInsurance as $deathInsuranceItem) {
if ($deathInsuranceItem && $deathInsuranceItem->getPolicyNumber()) {
$fileName = 'Polita de asigurare ' . $deathInsuranceItem->getPolicyNumber() . '.pdf';
$filePath = $this->getContainer()->getParameter('kernel.project_dir') . '/var/storage/nn_insurance/_policy/' . $fileName;
if ($filePath && file_exists($filePath)) {
header('Content-Type: application/pdf');
$pdfContent = file_get_contents($filePath);
echo $pdfContent;
} else {
return new Response('File not found', 404);
}
}
}
}
return new Response('error');
}
public function deathInsuranceIframe(Request $request): Response
{
$partnerService = $this->getContainer()->get('app.service.partner.partner');
$partnerService->verifyToken($request, PartnerManager::CAN_ISSUE_DEATH_INSURANCE_INDEX);
$token['partnerPublicToken'] = $request->get(PartnerManager::SESSION_PARTNER_PUBLIC_TOKEN_NAME);
$request->getSession()->set('iframeSourceLink', $this->generateUrl('app_shop_death_insurance_iframe', $token));
PartnerManager::checkQRCodeFlow($request);
return $this->render('@templates/DeathInsurance/iframe.html.twig', [
'issueDataList' => $this->getEntityManager()->getRepository(IssueData::class)->findAllAsArray(),
'publicToken' => $token['partnerPublicToken'] ?? null,
]);
}
}