<?php
namespace App\Controller\Comun;
use App\Entity\Comun\TareaEmpleada;
use App\Form\Comun\TareaEmpleadaType;
use App\Repository\Comun\TareaEmpleadaRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\Admin\Empleada;
use App\Entity\Comun\Tarea;
use App\Service\AccesoService;
#[Route('/comun/tarea/empleada')]
class TareaEmpleadaController extends AbstractController {
private $accesoService;
public function __construct(AccesoService $accesoService) {
$this->accesoService = $accesoService;
}
#[Route('/{tarea}', name: 'app_comun_tarea_empleada_index', methods: ['GET'])]
public function index(Tarea $tarea): Response {
return $this->render('comun/tarea_empleada/index.html.twig', [
'tarea' => $tarea,
'acceso'=> $this->accesoService->controlAccesosProyecto($tarea->getProyecto())
]);
}
#[Route('/new/{tarea}', name: 'app_comun_tarea_empleada_new', methods: ['GET', 'POST'])]
public function new(Request $request, Tarea $tarea, TareaEmpleadaRepository $tareaEmpleadaRepository): Response {
$em = $this->getDoctrine()->getManager();
$tareaEmpleada = new TareaEmpleada();
$form = $this->createForm(TareaEmpleadaType::class, $tareaEmpleada);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$empleada = $form->get('empleada')->getData();
$horasPrevistas = $form->get('horasPrevistas')->getData();
$tareaEmpleadaExistente = $tareaEmpleadaRepository->findOneBy(['empleada' => $empleada->getId(), 'tarea' => $tarea->getId()]);
if (!is_null($tareaEmpleadaExistente)) {
$horas = $tareaEmpleadaExistente->getHorasPrevistas();
$tareaEmpleadaExistente->setHorasPrevistas($horas + $horasPrevistas);
if ($tareaEmpleadaExistente->getHorasPrevistas() == 0) {
$tareaEmpleadaRepository->remove($tareaEmpleadaExistente);
} else {
$tareaEmpleadaRepository->add($tareaEmpleadaExistente);
}
} else if ($horasPrevistas > 0) {
$tareaEmpleada->setTarea($tarea);
$tareaEmpleadaRepository->add($tareaEmpleada);
}
$this->actualizarHoras($tarea, $tareaEmpleada->getEmpleada(), $tareaEmpleada->getHorasPrevistas());
}
return $this->renderForm('comun/tarea_empleada/form.html.twig', [
'tarea_empleada' => $tareaEmpleada,
'form' => $form,
]);
}
#[Route('/{id}/edit', name: 'app_comun_tarea_empleada_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, TareaEmpleada $tareaEmpleada, TareaEmpleadaRepository $tareaEmpleadaRepository): Response {
$form = $this->createForm(TareaEmpleadaType::class, $tareaEmpleada);
$form->handleRequest($request);
$session = $this->get("session");
if ($form->isSubmitted() && $form->isValid()) {
if ($tareaEmpleada->getHorasPrevistas() == 0) {
$tareaEmpleadaRepository->remove($tareaEmpleada);
} else {
$tareaEmpleadaRepository->add($tareaEmpleada);
$this->actualizarHoras($tareaEmpleada->getTarea(), $tareaEmpleada->getEmpleada(), ( $tareaEmpleada->getHorasPrevistas() - $session->get('horasOriginales')));
}
$session->set('horasOriginales', null);
} else {
$session->set('horasOriginales', $tareaEmpleada->getHorasPrevistas());
}
return $this->renderForm('comun/tarea_empleada/form.html.twig', [
'tarea_empleada' => $tareaEmpleada,
'form' => $form,
]);
}
#[Route('/delete/{id}', name: 'app_comun_tarea_empleada_delete', methods: ['GET', 'POST'])]
public function delete(Request $request, TareaEmpleada $tareaEmpleada, TareaEmpleadaRepository $tareaEmpleadaRepository): Response {
// faltan los fichajes
$proyecto = $tareaEmpleada->getTarea()->getProyecto();
$this->actualizarHoras($tareaEmpleada->getTarea(), $tareaEmpleada->getEmpleada(), ($tareaEmpleada->getHorasPrevistas()) * -1);
$tareaEmpleadaRepository->remove($tareaEmpleada);
return $this->redirect($this->generateUrl('app_comun_proyecto_show', ['id' => $proyecto->getId()]));
}
public function actualizarHoras(Tarea $tarea, Empleada $empleada, $horasPrevistas) {
$em = $this->getDoctrine()->getManager();
$tareaEmpleadaRepository = $em->getRepository("App:Comun\TareaEmpleada");
if (!is_null($tarea->getPadre())) {
$tareaEmpleadaExistente = $tareaEmpleadaRepository->findOneBy(['empleada' => $empleada->getId(), 'tarea' => $tarea->getPadre()->getId()]);
if (!is_null($tareaEmpleadaExistente)) {
$horas = $tareaEmpleadaExistente->getHorasPrevistas();
$tareaEmpleadaExistente->setHorasPrevistas($horas + $horasPrevistas);
if ($tareaEmpleadaExistente->getHorasPrevistas() == 0) {
$tareaEmpleadaRepository->remove($tareaEmpleadaExistente);
} else {
$tareaEmpleadaRepository->add($tareaEmpleadaExistente);
}
} else if ($horasPrevistas > 0) {
$tareaEmpleadaNew = new TareaEmpleada();
$tareaEmpleadaNew->setEmpleada($empleada);
$tareaEmpleadaNew->setHorasPrevistas($horasPrevistas);
$tareaEmpleadaNew->setTarea($tarea->getPadre());
$tareaEmpleadaRepository->add($tareaEmpleadaNew);
}
$this->actualizarHoras($tarea->getPadre(), $empleada, $horasPrevistas);
}
}
}