<?php
namespace App\Controller\higotrigo;
use App\Entity\ClientContact;
use App\Entity\HtFile;
use App\MDS\AvexpressBundle\Entity\AveFiles;
use App\Entity\HtInvoice;
use App\Entity\HtProforma;
use App\Form\HtFileType;
use App\MDS\GreenPatioBundle\Entity\Reservation;
use App\MDS\GreenPatioBundle\Entity\ReservationLoungeSimple;
use App\Repository\HtFileRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use App\Entity\Client;
use App\Entity\SettingsCompany;
use App\Entity\Regions;
use App\Entity\Country;
use App\Entity\Cities;
use App\Entity\HtExtra;
use App\Entity\HtItem;
use App\Entity\User;
use App\Service\HtService;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/higotrigo/ht/file")
*/
class HtFileController extends AbstractController
{
/**
* @Route("/", name="app_ht_file_index", methods={"GET"})
*/
public function index(HtFileRepository $htFileRepository): Response
{
return $this->render('higotrigo/ht_file/index.html.twig', [
'ht_files' => $htFileRepository->findAll(),
]);
}
/**
* @Route("/new", name="app_ht_file_new", methods={"GET", "POST"})
*/
public function new(Request $request, HtFileRepository $htFileRepository): Response
{
$htFile = new HtFile();
$form = $this->createForm(HtFileType::class, $htFile);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user = $this->getUser();
$htFile->setCreatedId($user);
$htFile->setUpdatedId($user);
$htFile = $this->_addUserToExtras($htFile, $user);
$htFileRepository->add($htFile, true);
return $this->redirectToRoute('app_ht_file_edit', ['id' => $htFile->getId()], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('higotrigo/ht_file/new.html.twig', [
'ht_file' => $htFile,
'invoices' => null,
'form' => $form,
]);
}
/**
* @Route("/{id}", name="app_ht_file_show", methods={"GET"})
*/
public function show(HtFile $htFile): Response
{
return $this->render('higotrigo/ht_file/show.html.twig', [
'ht_file' => $htFile,
]);
}
/**
* @Route("/{id}/edit", name="app_ht_file_edit", methods={"GET", "POST"})
*/
public function edit(Request $request, HtFile $htFile, HtFileRepository $htFileRepository, HtService $htService, EntityManagerInterface $em): Response
{
$form = $this->createForm(HtFileType::class, $htFile);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user = $this->getUser();
$htFile->setUpdatedId($user);
$htFile = $this->_addUserToExtras($htFile, $user);
$htFileRepository->add($htFile, true);
// return $this->redirectToRoute('app_ht_file_index', [], Response::HTTP_SEE_OTHER);
}
$invoices = $em->getRepository(HtInvoice::class)->findByHtFile($htFile);
$proformas = $em->getRepository(HtProforma::class)->findByHtFile($htFile->getId());
/* Obtengo usuario logueado */
$user_logueado = $this->get('security.token_storage')->getToken()->getUser();
$gpFile = in_array($user_logueado->getUserrol(),[9,23,24,37,47,53]) ? $em->getRepository(Reservation::class)->findOneById($htFile->getReservation()) : null;
$aveFile = !(empty($gpFile)) ? (in_array($user_logueado->getUserrol(),[9,23,24,37,47,53]) ? $em->getRepository(AveFiles::class)->findOneByReservation($gpFile) : null) : null;
return $this->renderForm('higotrigo/ht_file/edit.html.twig', [
'ht_file' => $htFile,
'invoices' => $invoices,
'proformas' => $proformas,
'gpFile' => $gpFile,
'aveFile' => $aveFile,
'form' => $form,
'totalData' => $htService->CalcTotals($htFile),
]);
}
/**
* @Route("/{id}/edit/ajax", name="app_ht_file_edit_ajax", methods={"POST"})
*/
public function editAjax(Request $request, HtFile $htFile, HtFileRepository $htFileRepository): JsonResponse
{
$form = $this->createForm(HtFileType::class, $htFile);
$form->handleRequest($request);
$msg = 'Datos guardados correctamente';
$data = [];
if ($form->isSubmitted() && $form->isValid()) {
try {
$user = $this->getUser();
$htFile->setUpdatedId($user);
$htFile = $this->_addUserToExtras($htFile, $user);
$htFileRepository->add($htFile, true);
} catch (\Throwable $th) {
$msg = 'Ha ocurrido un error, contacta con tu informatico de confianza';
}
}else{
$msg = 'Error al enviar los datos';
$data = $form->getErrors(true, false);
}
return $this->json([
'msg' => $msg,
'data' => $data,
], JsonResponse::HTTP_OK);
}
/**
* @Route("/{id}", name="app_ht_file_delete", methods={"POST"})
*/
public function delete(Request $request, HtFile $htFile, HtFileRepository $htFileRepository): Response
{
if ($this->isCsrfTokenValid('delete'.$htFile->getId(), $request->request->get('_token'))) {
$htFileRepository->remove($htFile, true);
}
return $this->redirectToRoute('app_ht_file_index', [], Response::HTTP_SEE_OTHER);
}
/**
* @Route("/htexternal/{id}/summary", name="app_ht_file_summary", methods={"GET", "POST"})
*/
public function summary(HtFile $htFile, EntityManagerInterface $em): Response
{
$arrayElem = array();
$htItems = $htFile->getHtItems();
if(!empty($htItems)) {
foreach ($htItems as $htItem) {
$arrayEscandallos = [];
$arrayMenus = [];
$lounge = empty($htItem->getLoungeGp()) ? $htItem->getLoungeOther() : $htItem->getLoungeGp()->getName();
$lounge = empty($lounge) ? 'No se ha indicado la sala' : $lounge;
if (!empty($htItem->getHtMenus())) {
foreach (($htItem->getHtMenus()) as $htMenu){
foreach (($htMenu->getEscandallo()) as $escan){
$arrayEscandallos[] = array('escandallo' => $escan, 'nota' => $escan->getHtNotes()[0],);
}
$arrayMenus[] = array('htMenu' => $htMenu, 'arrayEscandallos' => $arrayEscandallos,);
}
}
$arrayElem[$htItem->getDateStart()->format('YmdHi').str_pad($htItem->getId(), 6, "0", STR_PAD_LEFT)] = array(
'htItem'=>$htItem,
'htMenus'=> $arrayMenus,
'lounge'=>$lounge,
);
}
}
ksort($arrayElem);
$reserva = null; $lngMont = null; $lngDesMont = null;
$reservaIdNotLinked = $htFile->getReservation();
if (!empty($reservaIdNotLinked)){ $reserva = $em->getRepository(Reservation::class)->findOneById($reservaIdNotLinked->getId()); }
if (!empty($reserva)){
$lngMont = $em->getRepository(ReservationLoungeSimple::class)->findOneBy( array( 'idReservation' => $reserva->getId(), 'type' => 'Montaje', ) );
$lngDesMont = $em->getRepository(ReservationLoungeSimple::class)->findOneBy( array( 'idReservation' => $reserva->getId(), 'type' => 'Desmontaje', ) );
}
return $this->renderForm('higotrigo/ht_file/summary-htfile-pdf.html.twig', [
'ht_file' => $htFile,
'arrayElem' => $arrayElem,
'reserva' => $reserva,
'montaje' => $lngMont,
'desmontaje' => $lngDesMont,
]);
}
/**
* Recorremos los extras para aƱadirle el usuario que los crea y edita
*/
private function _addUserToExtras(HtFile $htFile, User $user) : HtFile {
$htItems = $htFile->getHtItems();
foreach ($htItems as $htItem) {
$htFile->removeHtItem($htItem); // Borrar el item para volverlo a insertar al final con los cambios de extras
$htExtras = $htItem->getHtExtras();
foreach ($htExtras as $htExtra) {
$htItem->removeHtExtra($htExtra);
if($htExtra->getCreatedId() == NULL){
$htExtra->setCreatedId($user);
}
$htExtra->setUpdatedId($user);
$htItem->addHtExtra($htExtra);
}
$htFile->addHtItem($htItem);
}
return $htFile;
}
/**
* Comprobar si el htFile tiene htItems y si estos tienen htExtras para aƱadirselos para que la vista funcione bien.
*/
private function _addHtItemAndHtExtra(HtFile $htFile) : HtFile {
$htItems = $htFile->getHtItems();
if($htItems){
foreach ($htItems as $htItem) {
$htExtras = $htItem->getHtExtras();
if($htExtras === null || $htExtras->isEmpty()){
$htFile->removeHtItem($htItem);
$htExtra = new HtExtra();
$htItem->addHtExtra($htExtra);
$htFile->addHtItem($htItem);
}
}
}
return $htFile;
}
}