<?php
namespace App\Controller\higotrigo;
use App\Entity\Escandallo;
use App\Entity\EscandalloProduct;
use App\Entity\FamilyEscandallo;
use App\Entity\HtMenu;
use App\Entity\Product;
use App\Form\EscandalloType;
use App\Repository\EscandalloRepository;
use App\Repository\FamilyEscandalloRepository;
use Doctrine\ORM\EntityManagerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* @Route("/higotrigo/escandallo")
*/
class EscandalloController extends AbstractController
{
private $translator;
public function __construct(TranslatorInterface $translator) {
$this->translator = $translator;
}
/**
* @Route("/", name="app_escandallo_index", methods={"GET"})
*/
public function index(EscandalloRepository $escandalloRepository): Response
{
return $this->render('higotrigo/escandallo/index.html.twig', [
'escandallos' => $escandalloRepository->findBy([], ['nombre' => 'ASC']),
]);
}
/**
* @Route("/new", name="app_escandallo_new", methods={"GET", "POST"})
*/
public function new(Request $request, EscandalloRepository $escandalloRepository): Response
{
$escandallo = new Escandallo();
$escandallo->setEscandalloPax(10); // Por defecto tiene que aparecer como 10
$escandallo->setCosteDirectoPerc(25); // Por defecto tiene que aparecer como 25
$escandallo->addEscandalloProduct(new EscandalloProduct); // Con esto aparece una fila de producto por defecto activa
$form = $this->createForm(EscandalloType::class, $escandallo);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user = $this->getUser();
$escandallo->setCreatedId($user);
$escandallo->setUpdatedId($user);
//Recorremos los escandalloProducts para aƱadirle el usuario que los crea
$escandalloProducts = $escandallo->getEscandalloProducts();
foreach ($escandalloProducts as $product) {
$escandallo->removeEscandalloProduct($product);
$product->setCreatedId($user);
$product->setUpdatedId($user);
$escandallo->addEscandalloProduct($product);
}
$file = $form->get('foto')->getData();
if ($file) {
$fileName = md5(rand() * time());
$camino = "assets/document/higotrigo/escandallo/";
if (!file_exists($camino)) { mkdir($camino,0777,true); }
$extension = $file->guessExtension();
$file_id = $fileName.'.'.$extension;
$file->move($camino, $file_id);
$escandallo->setFoto($file_id);
}
$escandalloRepository->add($escandallo, true);
return $this->redirectToRoute('app_escandallo_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('higotrigo/escandallo/new.html.twig', [
'escandallo' => $escandallo,
'form' => $form,
]);
}
/**
* @Route("/{id}", name="app_escandallo_show", methods={"GET"})
*/
public function show(Escandallo $escandallo): Response
{
return $this->render('higotrigo/escandallo/show.html.twig', [
'escandallo' => $escandallo,
]);
}
/**
* @Route("/{id}/edit", name="app_escandallo_edit", methods={"GET", "POST"})
*/
public function edit(Request $request, Escandallo $escandallo, EscandalloRepository $escandalloRepository, EntityManagerInterface $em): Response
{
$form = $this->createForm(EscandalloType::class, $escandallo);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user = $this->getUser();
$escandallo->setUpdatedId($user);
//Recorremos los escandalloProducts para aƱadirle el usuario que los edita
$escandalloProducts = $escandallo->getEscandalloProducts();
foreach ($escandalloProducts as $product) {
$escandallo->removeEscandalloProduct($product);
if($product->getCreatedId() == NULL){
$product->setCreatedId($user);
}
$product->setUpdatedId($user);
$escandallo->addEscandalloProduct($product);
}
$file = $form->get('foto')->getData();
if ($file) {
$fileName = md5(rand() * time());
$camino = "assets/document/higotrigo/escandallo/";
if (!file_exists($camino)) { mkdir($camino,0777,true); }
$extension = $file->guessExtension();
$file_id = $fileName.'.'.$extension;
$file->move($camino, $file_id);
$escandallo->setFoto($file_id);
}
$escandalloRepository->add($escandallo, true);
return $this->redirectToRoute('app_escandallo_index', [], Response::HTTP_SEE_OTHER);
}
$products = $em->getRepository(Product::class)->findAll(); //Para poder poner el precio del producto en la vista
$escandallos = $escandalloRepository->findAll(); //Para poder poner el precio del escandallo
return $this->renderForm('higotrigo/escandallo/edit.html.twig', [
'escandallo' => $escandallo,
'form' => $form,
'escandallos' => $escandallos,
'products' => $products,
]);
}
/**
* @Route("/{id}", name="app_escandallo_delete", methods={"POST"})
*/
public function delete(Request $request, Escandallo $escandallo, EscandalloRepository $escandalloRepository): Response
{
if ($this->isCsrfTokenValid('delete'.$escandallo->getId(), $request->request->get('_token'))) {
$escandalloRepository->remove($escandallo, true);
}
return $this->redirectToRoute('app_escandallo_index', [], Response::HTTP_SEE_OTHER);
}
/**
* @Route("/{id}/duplicate", name="app_escandallo_duplicate", methods={"GET", "POST"})
*/
public function duplicate(Escandallo $escandallo, EscandalloRepository $escandalloRepository): Response
{
$user = $this->getUser();
$newEscandallo = new Escandallo();
$newEscandallo->setFamilyEscandallo($escandallo->getFamilyEscandallo());
$newEscandallo->setEscandalloPax($escandallo->getEscandalloPax());
$newEscandallo->setTotalTenPax($escandallo->getTotalTenPax());
$newEscandallo->setCostePax($escandallo->getCostePax());
$newEscandallo->setPvp($escandallo->getPvp());
$newEscandallo->setPvpNotVat($escandallo->getPvpNotVat());
$newEscandallo->setCosteDirectoPerc($escandallo->getCosteDirectoPerc());
$newEscandallo->setBeneficio($escandallo->getBeneficio());
$newEscandallo->setFoto($escandallo->getFoto());
$newEscandallo->setDecription($escandallo->getDecription());
$newEscandallo->setObservaciones($escandallo->getObservaciones());
$newEscandallo->setNombre('DUPLICADO DE: '.$escandallo->getNombre());
$newEscandallo->setCreatedId($user);
$newEscandallo->setUpdatedId($user);
$escandalloProducts = $escandallo->getEscandalloProducts();
foreach ($escandalloProducts as $item){
$newEscandalloProduct = new EscandalloProduct();
$newEscandalloProduct->setCantidad($item->getCantidad());
$newEscandalloProduct->setCodigo($item->getCodigo());
$newEscandalloProduct->setCoste($item->getCoste());
// $newEscandalloProduct->setEscandallo($item->getEscandallo());
$newEscandalloProduct->setProduct($item->getProduct());
$newEscandalloProduct->setEscandalloChild($item->getEscandalloChild());
$newEscandalloProduct->setCreatedId($user);
$newEscandalloProduct->setUpdatedId($user);
$newEscandallo->addEscandalloProduct($newEscandalloProduct);
}
$escandalloRepository->add($newEscandallo, true);
return $this->redirectToRoute('app_escandallo_edit', ['id' => $newEscandallo->getId()], Response::HTTP_SEE_OTHER);
}
/**
* @Route("/{id}/price", name="app_escandallo_get_price", methods={"GET"})
*/
public function getPreice(Escandallo $escandallo): JsonResponse
{
$return = array( 'price' => $escandallo->getCostePax(), );
$response = new JsonResponse($return);
return $response;
}
/**
* @Route("/{id}/escandallo-by-family", name="app_escandallo_escandallo_by_family", methods={"GET"})
*/
function getEscandallosByFamily($id, EscandalloRepository $escandalloRepository, FamilyEscandalloRepository $familyEscandalloRepository, SerializerInterface $serializerInterface) : JsonResponse {
$familyEscandallo = $familyEscandalloRepository->findById($id);
if($familyEscandallo){
$escandallos = $escandalloRepository->findBy(['familyEscandallo' => $familyEscandallo]);
}else{
$escandallos = $escandalloRepository->findAll();
}
$data = $serializerInterface->serialize($escandallos, 'json', [
AbstractNormalizer::GROUPS => ['escandallo:read']
]);
return $this->json(json_decode($data), JsonResponse::HTTP_OK);
}
/**
* @Route("/{id}/details", name="app_escandallo_get_details", methods={"GET"})
*/
function getEscandalloDetails(Escandallo $escandallo, EscandalloRepository $escandalloRepository, SerializerInterface $serializerInterface) : JsonResponse {
$data = $serializerInterface->serialize($escandallo, 'json', [
AbstractNormalizer::GROUPS => ['escandallo:read']
]);
return $this->json(json_decode($data), JsonResponse::HTTP_OK);
}
/**
* @Route("/{escandalloId}/add-to-menu/{menuId}", name="app_escandallo_add_to_menu", methods={"GET"})
* @ParamConverter("escandallo", class="App\Entity\Escandallo", options={"id" = "escandalloId"})
* @ParamConverter("htMenu", class="App\Entity\HtMenu", options={"id" = "menuId"})
*/
function addToMenu(Escandallo $escandallo, HtMenu $htMenu, EscandalloRepository $escandalloRepository, SerializerInterface $serializerInterface) : JsonResponse {
$escandallo->addHtMenu($htMenu);
$escandalloRepository->add($escandallo, true);
$data = $serializerInterface->serialize($escandallo, 'json', [
AbstractNormalizer::GROUPS => ['escandallo:read']
]);
return $this->json(json_decode($data), JsonResponse::HTTP_OK);
}
/**
* @Route("/{escandalloId}/remove-to-menu/{menuId}", name="app_escandallo_remove_to_menu", methods={"GET"})
* @ParamConverter("escandallo", class="App\Entity\Escandallo", options={"id" = "escandalloId"})
* @ParamConverter("htMenu", class="App\Entity\HtMenu", options={"id" = "menuId"})
*/
function removeToMenu(Escandallo $escandallo, HtMenu $htMenu, EscandalloRepository $escandalloRepository, SerializerInterface $serializerInterface) : JsonResponse {
$escandallo->removeHtMenu($htMenu);
$escandalloRepository->add($escandallo, true);
$data = $serializerInterface->serialize($escandallo, 'json', [
AbstractNormalizer::GROUPS => ['escandallo:read']
]);
return $this->json(json_decode($data), JsonResponse::HTTP_OK);
}
}