<?php
namespace App\Controller;
use App\Entity\Employee;
use App\Entity\EmployeeHistory;
use App\Enum\EmployeeHistory\SalaryType;
use App\Form\EmployeeHistoryType;
use App\Helper\All;
use App\Repository\EmployeeHistoryRepository;
use App\Repository\EmployeeRepository;
use App\Service\EmployeeService;
use Doctrine\Persistence\ManagerRegistry;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Asset\Packages;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/employee_history")
*/
class EmployeeHistoryController extends AbstractController
{
/** @var EmployeeHistoryRepository $employeeHistoryRepository */
private $employeeHistoryRepository;
/** @var ManagerRegistry $entityManager */
private $entityManager;
public function __construct(
EmployeeHistoryRepository $employeeHistoryRepository,
ManagerRegistry $entityManager,
)
{
$this->employeeHistoryRepository = $employeeHistoryRepository;
$this->entityManager = $entityManager->getManager();
}
/**
* @Route("/", name="employee_history_index", methods={"GET"})
* @IsGranted("EMPLOYEE_HISTORY_INDEX")
*/
public function index(): Response
{
return $this->render(
'employee_history/index.html.twig',
[
'title' => "Liste des Contrats",
'menu' => 'employee_history_index',
]
);
}
/**
* @Route("/list", name="employee_history_list", methods={"GET","POST"})
* @IsGranted("EMPLOYEE_HISTORY_INDEX")
* @param Request $request
* @return JsonResponse
*/
public function listTable(
Request $request,
): JsonResponse
{
$query = $request->get('search')['value'];
$tableColumns = [
"eh.id",
"e.firstName",
"e.lastName",
"ehd.name",
"ehp.name",
"eh.startDate",
"eh.endDate",
"eh.salaryType",
"eh.price",
"reason",
"",
"",
""
];
$orderByColumn = !empty($tableColumns[$request->get('order')[0]['column']]) ? $tableColumns[$request->get(
'order'
)[0]['column']] : 'eh.id';
$orderDirection = $request->get('order')[0]['dir'];
$limit = $request->get('length') != '-1' ? $request->get('length') : '';
$offset = $request->get('start');
$draw = intval($request->get('draw'));
$filters = !empty($request->get('filters')) && is_array($request->get('filters')) ? $request->get('filters') : '';
$histories = $this->employeeHistoryRepository->getListTable(
$query,
$filters,
$orderByColumn,
$orderDirection,
$limit,
$offset
);
$data = [];
foreach ($histories['data'] as $employeeHistory) {
/** @var EmployeeHistory $employeeHistory */
$buttons = '';
$show_link = $this->generateUrl('employee_history_edit', ['id' => $employeeHistory->getId()]);
$buttons .= '<a href="' . $show_link . '"><i class="flaticon-search-1"> Editer</i> </a>';
$price = '******';
if (in_array('ROLE_ADMIN', $this->getUser()->getRoles()) || SalaryType::HOURLY === $employeeHistory->getSalaryType()) {
$price = $employeeHistory->getPrice();
}
$data[] = [
$employeeHistory->getId(),
$employeeHistory->getEmploye()->getFirstName(),
$employeeHistory->getEmploye()->getLastName(),
$employeeHistory->getDepartment()->getName(),
$employeeHistory->getPosition()->getName(),
$employeeHistory->getStartDate() ? $employeeHistory->getStartDate()->format('d/m/Y') : '-',
$employeeHistory->getEndDate() ? $employeeHistory->getEndDate()->format('d/m/Y') : '-',
$employeeHistory->getSalaryType()->title(),
$price,
$employeeHistory->getReason(),
All::statusBadge(empty($employeeHistory->getEndDate()) || $employeeHistory->getEndDate() >= new \DateTime()),
$buttons,
];
}
$data = [
"draw" => $draw,
"recordsTotal" => intval($histories['recordsTotal']),
"recordsFiltered" => intval($histories['recordsFiltered']),
"data" => $data,
];
return new JsonResponse($data);
}
/**
* @Route("/new", name="employee_history_new", methods={"GET","POST"})
* @IsGranted("EMPLOYEE_HISTORY_NEW", subject="", message="Permission requise")
*/
public function new(
Request $request,
EmployeeRepository $employeeRepository
): Response
{
$employee = $employeeRepository->find($request->get('employee_id'));
if (!$employee instanceof Employee) {
die('No employee Founds');
}
if ($employee->isActiveEmployee()) {
$this->addFlash(
'danger',
"Afin d'ajouter une période il faut clôturer la dernière période travaillé"
);
return $this->redirectToRoute('employee_show', ['id' => $employee->getId()]);
}
$employeeHistory = new EmployeeHistory();
$employeeHistory->setEmploye($employee);
$form = $this->createForm(EmployeeHistoryType::class, $employeeHistory);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$ok = true;
if (!is_null($employeeHistory->getEndDate()) && $employeeHistory->getEndDate() < $employeeHistory->getStartDate()) {
$this->addFlash(
'danger',
"La date de fin doit etre >= la date de début !"
);
$ok = false;
}
$lastEmployeeHistory = $employee->getLastHistory();
if (!empty($employeeHistory) && !empty($lastEmployeeHistory) && $employeeHistory->getStartDate() <= $lastEmployeeHistory->getEndDate()) {
$this->addFlash(
'danger',
"La date de début doit etre >= la date de fin de la derniere periode !"
);
$ok = false;
}
if ($ok) {
$employee->setLastHistory($employeeHistory);
$this->entityManager->persist($employee);
$this->entityManager->persist($employeeHistory);
$this->entityManager->flush();
$this->addFlash(
'success',
"La période a bien été ajoutée !"
);
return $this->redirectToRoute('employee_show', ['id' => $employee->getId()]);
}
}
return $this->render(
'employee_history/new_edit.html.twig',
[
'title' => 'Ajouter',
'menu' => 'employee_new',
'employeeHistory' => $employeeHistory,
'form' => $form->createView(),
]
);
}
/**
* @Route("/{id}/edit", name="employee_history_edit", methods={"GET","POST"})
* @IsGranted("EMPLOYEE_HISTORY_EDIT", subject="", message="Permission requise")
*/
public function edit(
Request $request,
EmployeeHistory $employeeHistory,
EmployeeService $employeeService
): Response
{
$form = $this->createForm(EmployeeHistoryType::class, $employeeHistory);
$form
->remove('price')
->remove('salaryType');
$form->handleRequest($request);
if ($employeeHistory->getEmploye()->getLastHistoryOrNull() != $employeeHistory) {
die('Cannot old data');
}
if ($form->isSubmitted() && $form->isValid()) {
$ok = true;
if (!empty($employeeHistory->getEndDate()) && $employeeHistory->getEndDate() < $employeeHistory->getStartDate()) {
$this->addFlash(
'danger',
"La date de fin doit etre >= la date de début !"
);
$ok = false;
}
//TODO: NEED TO CHECK IF AN EXISTING LOG FOUNDS IN THIS PERIODE; IF THIS WE CANNOT EDIT IT UNLESS WE REMOVE THE LOG
//dd($employeeService->lastLogInDate($employeeHistory->getEmploye(), $employeeHistory->getStartDate()));
if ($ok) {
$employee = $employeeHistory->getEmploye();
$employee->setLastHistory($employeeHistory);
$this->entityManager->persist($employee);
$this->entityManager->persist($employeeHistory);
$this->entityManager->flush();
$this->addFlash(
'success',
"La période a bien été editée !"
);
return $this->redirectToRoute('employee_show', ['id' => $employeeHistory->getEmploye()->getId()]);
}
}
return $this->render(
'employee_history/new_edit.html.twig',
[
'title' => sprintf('%s: %s', 'Fiche', $employeeHistory->getEmploye()->getFullName()),
'menu' => 'employee_new',
'employeeHistory' => $employeeHistory,
'form' => $form->createView(),
]
);
}
}