<?php
// src/Controller/PoolGraphExportController.php
namespace App\Controller\Front;
use App\Controller\CustomController;
use App\Entity\HttParsing;
use App\Entity\PoolProgram;
use App\Entity\PoolProgramType;
use App\Form\Front\GraphExportType;
use App\Service\CurrencyService;
use App\Service\Export\HttReportPdf;
use App\Service\HttGraph\GraphAmortisation;
use App\Service\HttGraph\GraphBreakdownByInterestRate;
use App\Service\HttGraph\GraphBreakdownByRepaymentType;
use App\Service\HttGraph\GraphBreakdownByTypeOfDebtor;
use App\Service\HttGraph\GraphConcentrationRisks;
use App\Service\HttGraph\GraphCurrencyBreakdownCoverAssets;
use App\Service\HttGraph\GraphCurrencyBreakdownCoveredBonds;
use App\Service\HttGraph\GraphGeographicBreakdownDomestic;
use App\Service\HttGraph\GraphGeographicBreakdownInternational;
use App\Service\HttGraph\GraphInterestRateBreakdown;
use App\Service\HttGraph\GraphLoanSeasoningBar;
use App\Service\HttGraph\GraphLoanSizeInformationCommercial;
use App\Service\HttGraph\GraphLoanSizeInformationResidential;
use App\Service\HttGraph\GraphLtvDistributionIndexed;
use App\Service\HttGraph\GraphLtvDistributionUnindexed;
use App\Service\HttGraph\GraphOvercollateralisation;
use App\Service\HttGraph\GraphTypeTenureCommercial;
use App\Service\HttGraph\GraphTypeTenureResidential;
use App\Service\HttParser\PoolType\PoolTypeConfigMortgage;
use App\Service\HttParser\PoolType\PoolTypeConfigPublic;
use App\Service\HttParser\PoolType\PoolTypeConfigShipping;
use App\Service\HttService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Mime\FileinfoMimeTypeGuesser;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
class PoolGraphExportController extends CustomController
{
protected CurrencyService $currencyService;
public function __construct(
EntityManagerInterface $em,
RequestStack $requestStack,
CurrencyService $currencyService
)
{
parent::__construct($em, $requestStack);
$this->currencyService = $currencyService;
}
/**
* @Route("/pool/{id}/graph/{parsingId}/export/maker", name="public_pool_graph_export_maker")
* @ParamConverter("parsing", options={"mapping": {"parsingId": "id"}})
*/
public function exportMaker(Request $request, PoolProgram $pool, HttParsing $parsing, HttService $httService)
{
$this->denyAccessUnlessGranted('view', $pool);
$this->denyAccessUnlessGranted('view', $parsing);
$form = $this->createForm(GraphExportType::class, null, ['method' => 'get', 'parsing' => $parsing]);
$form->handleRequest($request);
if ($form->isSubmitted()) {
if($form->isValid()){
$this->session->set('exportData', $form->getData());
return $this->redirectToRoute('public_pool_graph_export_step1', ['id' => $pool->getId(), 'parsingId' => $parsing->getId()]);
}
}else{
$form->setData([
'generalInformation' => true,
'contactsDetail' => true,
'poolDetail' => true,
'httOverview' => true,
'httGeneral' => true,
'httMortgage' => true,
'httPublic' => true,
'httShipping' => true,
'bonds' => true,
'httGenerals' => ['a', 'b', 'c', 'd'],
'httMortgages' => [
PoolTypeConfigMortgage::SECTION_GEOGRAPHIC_BREAKDOWN_INTERNATIONAL,
PoolTypeConfigMortgage::SECTION_BREAKDOWN_BY_INTERESTS_RATE,
PoolTypeConfigMortgage::SECTION_BREAKDOWN_BY_REPAYMENT_TYPE,
PoolTypeConfigMortgage::SECTION_LOAN_SEASONING_BAR,
PoolTypeConfigMortgage::SECTION_LOAN_SIZE_INFORMATION_RESIDENTIAL,
PoolTypeConfigMortgage::SECTION_LTV_DISTRIBUTION_INDEXED,
PoolTypeConfigMortgage::SECTION_TYPE_OF_TENURE_RESIDENTIAL,
],
'httPublics' => [
PoolTypeConfigPublic::SECTION_GEOGRAPHIC_BREAKDOWN_INTERNATIONAL,
PoolTypeConfigPublic::SECTION_BREAKDOWN_BY_INTERESTS_RATE,
PoolTypeConfigPublic::SECTION_BREAKDOWN_BY_REPAYMENT_TYPE,
PoolTypeConfigPublic::SECTION_LOAN_SIZE_INFORMATION_RESIDENTIAL,
PoolTypeConfigPublic::SECTION_CONCENTRATION_RISK,
PoolTypeConfigPublic::SECTION_BREAKDOWN_BY_TYPE_OF_DEBTOR,
],
'httShippings' => [
PoolTypeConfigShipping::SECTION_GEOGRAPHIC_BREAKDOWN_INTERNATIONAL,
PoolTypeConfigShipping::SECTION_BREAKDOWN_BY_INTERESTS_RATE,
PoolTypeConfigShipping::SECTION_BREAKDOWN_BY_REPAYMENT_TYPE,
PoolTypeConfigShipping::SECTION_LOAN_SEASONING_BAR,
PoolTypeConfigShipping::SECTION_LOAN_SIZE_INFORMATION_RESIDENTIAL,
PoolTypeConfigShipping::SECTION_LTV_DISTRIBUTION_INDEXED,
PoolTypeConfigShipping::SECTION_BREAKDOWN_BY_TYPE_OF_SHIP,
],
]);
}
$httService->setParsing($parsing);
return $this->render('front/pool/graph-export/maker.html.twig', [
'httService' => $httService,
'parsing' => $parsing,
'pool' => $pool,
'issuer' => $pool->getIssuer(),
'form' => $form->createView(),
]);
}
/**
* @Route("/pool/{id}/graph/{parsingId}/export/step1", name="public_pool_graph_export_step1")
* @ParamConverter("parsing", options={"mapping": {"parsingId": "id"}})
*/
public function step1(Request $request, PoolProgram $pool, HttParsing $parsing, HttService $httService)
{
$exportData = $this->session->get('exportData');
if(!$exportData){
$this->redirectToRoute('public_pool_graph_export_maker', ['id' => $pool->getId(), 'parsingId' => $parsing->getId()]);
}
$httService->setParsing($parsing);
return $this->render('front/pool/graph-export/step1.html.twig', [
'httService' => $httService,
'parsing' => $parsing,
'pool' => $pool,
'issuer' => $pool->getIssuer(),
'exportData' => $exportData,
]);
}
/**
* @Route("/pool/{id}/graph/{parsingId}/export/step1/send-data", name="public_pool_graph_export_step1_send_data")
* @ParamConverter("parsing", options={"mapping": {"parsingId": "id"}})
*/
public function step1SendData(Request $request, PoolProgram $pool, HttParsing $parsing, HttService $httService, HttReportPdf $exportService)
{
try{
$data = $request->get('data');
$exportService->saveDataStep1($data);
return new JsonResponse(['success' => true]);
}catch(\Exception $e){
return new JsonResponse(['success' => false, 'errorMessage' => $e->getMessage(), 'stack' => $e->getTraceAsString()]);
}
}
/**
* @Route("/pool/{id}/graph/{parsingId}/export/step2", name="public_pool_graph_export_step2")
* @ParamConverter("parsing", options={"mapping": {"parsingId": "id"}})
*/
public function step2(Request $request, PoolProgram $pool, HttParsing $parsing, HttService $httService, HttReportPdf $exportService, GraphGeographicBreakdownInternational $graphService)
{
$exportData = $this->session->get('exportData');
if(!$exportData){
$this->redirectToRoute('public_pool_graph_export_maker', ['id' => $pool->getId(), 'parsingId' => $parsing->getId()]);
}
$httService->setParsing($parsing);
return $this->render('front/pool/graph-export/step2.html.twig', [
'httService' => $httService,
'parsing' => $parsing,
'pool' => $pool,
'issuer' => $pool->getIssuer(),
'exportData' => $exportData,
'graphService' => $graphService,
]);
}
/**
* @Route("/pool/{id}/graph/{parsingId}/export/step2/send-data", name="public_pool_graph_export_step2_send_data")
* @ParamConverter("parsing", options={"mapping": {"parsingId": "id"}})
*/
public function step2SendData(Request $request, PoolProgram $pool, HttParsing $parsing, HttService $httService, HttReportPdf $exportService)
{
try{
$data = $request->get('data');
$exportService->saveDataStep2($data);
$exportService
->setExportOptions($this->session->get('exportData'))
->setPool($pool)
->setParsing($parsing)
->export();
return new JsonResponse(['success' => true]);
}catch(\Exception $e){
return new JsonResponse(['success' => false, 'errorMessage' => $e->getMessage(), 'stack' => $e->getTraceAsString()]);
}
}
/**
* @Route("/pool/{id}/graph/{parsingId}/export/download", name="public_pool_graph_export_download")
* @ParamConverter("parsing", options={"mapping": {"parsingId": "id"}})
*/
public function download(Request $request, PoolProgram $pool, HttParsing $parsing, HttService $httService, HttReportPdf $exportService)
{
$exportData = $this->session->get('exportData');
if(!$exportData){
$this->redirectToRoute('public_pool_graph_export_maker', ['id' => $pool->getId(), 'parsingId' => $parsing->getId()]);
}
$httService->setParsing($parsing);
return $this->render('front/pool/graph-export/download.html.twig', [
'httService' => $httService,
'parsing' => $parsing,
'pool' => $pool,
'issuer' => $pool->getIssuer(),
]);
}
/**
* @Route("/pool/{id}/graph/{parsingId}/export/download-file", name="public_pool_graph_export_download_file")
* @ParamConverter("parsing", options={"mapping": {"parsingId": "id"}})
*/
public function downloadFile(Request $request, PoolProgram $pool, HttParsing $parsing, HttReportPdf $exportService)
{
$exportService->setParsing($parsing);
$response = new BinaryFileResponse($exportService->getFilePath());
$mimeTypeGuesser = new FileinfoMimeTypeGuesser();
if($mimeTypeGuesser->isGuesserSupported()){
$response->headers->set('Content-Type', $mimeTypeGuesser->guessMimeType($exportService->getFilePath()));
}else{
$response->headers->set('Content-Type', 'text/plain');
}
$response->setContentDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
'Report.pdf'
);
return $response;
}
}