<?php
namespace App\Controller;
use App\Entity\Report;
use App\Form\BuildAllReportsType;
use App\Form\ReportGeneratorType;
use App\Form\ReportLogosType;
use App\Form\UploadFileType;
use App\Repository\AnswerRepository;
use App\Repository\ReportRepository;
use App\Service\BuildReportsByClientsService;
use App\Service\ProcessResultService;
use App\Service\S3TXTManager;
use Doctrine\ORM\EntityRepository;
use Exception;
use FontLib\Table\Type\name;
use Psr\Log\LoggerInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\Annotation\Route;
class BuildReportsByClientsController extends AbstractController
{
/**
* @Route("/upload/reports/by/clients", name="upload_reports_by_clients")
*/
public function index(
Request $request,
BuildReportsByClientsService $srvBuildReport,
S3TXTManager $s3TXTManager): Response
{
$form = $this->createForm(UploadFileType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
/** @var \Symfony\Component\HttpFoundation\File\UploadedFile $file */
$file = $form['txt']->getData();
$fileName = md5(uniqid()).'.'.$file->guessExtension();
$cvDir = $srvBuildReport->getTxtDir();
$s3Path = $cvDir . $fileName;
if ($s3TXTManager->uploadFileTXT_ToS3($file, $s3Path)) {
return $this->redirect($this->generateUrl('select_reports_by_clients', ['names' => $s3Path]));
} else {
$this->addFlash('error', 'Failed to upload the file to S3.');
}
return $this->redirect($this->generateUrl('select_reports_by_clients',
['names'=>$fileName]));
}
return $this->render('build_reports_by_clients/index.html.twig', [
'controller_name' => 'BuildReportsByClientsController',
'form' => $form->createView()
]);
}
/**
* @Route("/select/reports/by/clients", name="select_reports_by_clients")
*/
public function selectReportByClient(Request $request,
BuildReportsByClientsService $srvReportsByClients,
ReportRepository $rr,
AnswerRepository $answerRepository,
LoggerInterface $logger,
ProcessResultService $processResultService): Response
{
$clients = $srvReportsByClients->nameGrabber($request->get('names'));
if (!$clients) {
//manejar Exception estandar
throw new Exception("no se escontraron clientes en este txt", 1);
}
$form = $this->createForm(BuildAllReportsType::class, null, [
'data'=> [
'clients'=>$clients['clients'],
'plans'=>$rr->findAllBySubsetionNotAutomatic()]
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
try {
$pending = $srvReportsByClients->confirmGeneration($form->getData());
$pending = $processResultService->processResult($pending, $clients['rowAndColum']);
return $this->render('build_reports_by_clients/generator_loading.html.twig', [
'pendingReport'=> $pending,
'answerRepository' => $answerRepository
]);
} catch (\Exception $ex) {
$logger->error($ex->getFile().' '.$ex->getLine());
$form->addError(new FormError($ex->getMessage()));
// return $this->json($ex->getMessage(), Response::HTTP_CONFLICT);
}
}
return $this->render('build_reports_by_clients/select_reports_by_clients.html.twig', [
'form' => $form->createView(),
'clients' => count($clients)
]);
}
/**
* @Route("/find/plans/{report}", name="find_plans")
*/
public function find_plans($report, AnswerRepository $answerRepository): Response
{
try {
if (!isset($report)) {
return $this->json(['message'=> 'reporte no valido', Response::HTTP_CONFLICT]);
}
$optionPlans= [];
foreach ($answerRepository->findPlanByReporNotAutomatic($report) as $value) {
$optionPlans[$value['id']] = $value['answer'].': '.$value['descriptiveAnswer'];
}
if (count($optionPlans)>0) {
$optionPlans[0] = 'Automatico: El sistema lo determina segun los marcadores geneticos';
}
} catch (\Throwable $th) {
return $this->json(['message'=> $th->getMessage(), Response::HTTP_CONFLICT]);
}
return $this->json(['data'=> $optionPlans, Response::HTTP_OK]);
}
}