src/Controller/EmployeeHistoryController.php line 64

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Employee;
  4. use App\Entity\EmployeeHistory;
  5. use App\Enum\EmployeeHistory\SalaryType;
  6. use App\Form\EmployeeHistoryType;
  7. use App\Helper\All;
  8. use App\Repository\EmployeeHistoryRepository;
  9. use App\Repository\EmployeeRepository;
  10. use App\Service\EmployeeService;
  11. use Doctrine\Persistence\ManagerRegistry;
  12. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\Asset\Packages;
  15. use Symfony\Component\HttpFoundation\JsonResponse;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. /**
  20.  * @Route("/employee_history")
  21.  */
  22. class EmployeeHistoryController extends AbstractController
  23. {
  24.     /** @var EmployeeHistoryRepository $employeeHistoryRepository */
  25.     private $employeeHistoryRepository;
  26.     /** @var ManagerRegistry $entityManager */
  27.     private $entityManager;
  28.     public function __construct(
  29.         EmployeeHistoryRepository $employeeHistoryRepository,
  30.         ManagerRegistry           $entityManager,
  31.     )
  32.     {
  33.         $this->employeeHistoryRepository $employeeHistoryRepository;
  34.         $this->entityManager $entityManager->getManager();
  35.     }
  36.     /**
  37.      * @Route("/", name="employee_history_index", methods={"GET"})
  38.      * @IsGranted("EMPLOYEE_HISTORY_INDEX")
  39.      */
  40.     public function index(): Response
  41.     {
  42.         return $this->render(
  43.             'employee_history/index.html.twig',
  44.             [
  45.                 'title' => "Liste des Contrats",
  46.                 'menu' => 'employee_history_index',
  47.             ]
  48.         );
  49.     }
  50.     /**
  51.      * @Route("/list", name="employee_history_list", methods={"GET","POST"})
  52.      * @IsGranted("EMPLOYEE_HISTORY_INDEX")
  53.      * @param Request $request
  54.      * @return JsonResponse
  55.      */
  56.     public function listTable(
  57.         Request $request,
  58.     ): JsonResponse
  59.     {
  60.         $query $request->get('search')['value'];
  61.         $tableColumns = [
  62.             "eh.id",
  63.             "e.firstName",
  64.             "e.lastName",
  65.             "ehd.name",
  66.             "ehp.name",
  67.             "eh.startDate",
  68.             "eh.endDate",
  69.             "eh.salaryType",
  70.             "eh.price",
  71.             "reason",
  72.             "",
  73.             "",
  74.             ""
  75.         ];
  76.         $orderByColumn = !empty($tableColumns[$request->get('order')[0]['column']]) ? $tableColumns[$request->get(
  77.             'order'
  78.         )[0]['column']] : 'eh.id';
  79.         $orderDirection $request->get('order')[0]['dir'];
  80.         $limit $request->get('length') != '-1' $request->get('length') : '';
  81.         $offset $request->get('start');
  82.         $draw intval($request->get('draw'));
  83.         $filters = !empty($request->get('filters')) && is_array($request->get('filters')) ? $request->get('filters') : '';
  84.         $histories $this->employeeHistoryRepository->getListTable(
  85.             $query,
  86.             $filters,
  87.             $orderByColumn,
  88.             $orderDirection,
  89.             $limit,
  90.             $offset
  91.         );
  92.         $data = [];
  93.         foreach ($histories['data'] as $employeeHistory) {
  94.             /** @var EmployeeHistory $employeeHistory */
  95.             $buttons '';
  96.             $show_link $this->generateUrl('employee_history_edit', ['id' => $employeeHistory->getId()]);
  97.             $buttons .= '<a href="' $show_link '"><i class="flaticon-search-1"> Editer</i> </a>';
  98.             $price '******';
  99.             if (in_array('ROLE_ADMIN'$this->getUser()->getRoles()) || SalaryType::HOURLY === $employeeHistory->getSalaryType()) {
  100.                 $price $employeeHistory->getPrice();
  101.             }
  102.             $data[] = [
  103.                 $employeeHistory->getId(),
  104.                 $employeeHistory->getEmploye()->getFirstName(),
  105.                 $employeeHistory->getEmploye()->getLastName(),
  106.                 $employeeHistory->getDepartment()->getName(),
  107.                 $employeeHistory->getPosition()->getName(),
  108.                 $employeeHistory->getStartDate() ? $employeeHistory->getStartDate()->format('d/m/Y') : '-',
  109.                 $employeeHistory->getEndDate() ? $employeeHistory->getEndDate()->format('d/m/Y') : '-',
  110.                 $employeeHistory->getSalaryType()->title(),
  111.                 $price,
  112.                 $employeeHistory->getReason(),
  113.                 All::statusBadge(empty($employeeHistory->getEndDate()) || $employeeHistory->getEndDate() >= new \DateTime()),
  114.                 $buttons,
  115.             ];
  116.         }
  117.         $data = [
  118.             "draw" => $draw,
  119.             "recordsTotal" => intval($histories['recordsTotal']),
  120.             "recordsFiltered" => intval($histories['recordsFiltered']),
  121.             "data" => $data,
  122.         ];
  123.         return new JsonResponse($data);
  124.     }
  125.     /**
  126.      * @Route("/new", name="employee_history_new", methods={"GET","POST"})
  127.      * @IsGranted("EMPLOYEE_HISTORY_NEW", subject="", message="Permission requise")
  128.      */
  129.     public function new(
  130.         Request            $request,
  131.         EmployeeRepository $employeeRepository
  132.     ): Response
  133.     {
  134.         $employee $employeeRepository->find($request->get('employee_id'));
  135.         if (!$employee instanceof Employee) {
  136.             die('No employee Founds');
  137.         }
  138.         if ($employee->isActiveEmployee()) {
  139.             $this->addFlash(
  140.                 'danger',
  141.                 "Afin d'ajouter une période il faut clôturer la dernière période travaillé"
  142.             );
  143.             return $this->redirectToRoute('employee_show', ['id' => $employee->getId()]);
  144.         }
  145.         $employeeHistory = new EmployeeHistory();
  146.         $employeeHistory->setEmploye($employee);
  147.         $form $this->createForm(EmployeeHistoryType::class, $employeeHistory);
  148.         $form->handleRequest($request);
  149.         if ($form->isSubmitted() && $form->isValid()) {
  150.             $ok true;
  151.             if (!is_null($employeeHistory->getEndDate()) && $employeeHistory->getEndDate() < $employeeHistory->getStartDate()) {
  152.                 $this->addFlash(
  153.                     'danger',
  154.                     "La date de fin doit etre >= la date de début !"
  155.                 );
  156.                 $ok false;
  157.             }
  158.             $lastEmployeeHistory $employee->getLastHistory();
  159.             if (!empty($employeeHistory) && !empty($lastEmployeeHistory) && $employeeHistory->getStartDate() <= $lastEmployeeHistory->getEndDate()) {
  160.                 $this->addFlash(
  161.                     'danger',
  162.                     "La date de début doit etre >= la date de fin de la derniere periode !"
  163.                 );
  164.                 $ok false;
  165.             }
  166.             if ($ok) {
  167.                 $employee->setLastHistory($employeeHistory);
  168.                 $this->entityManager->persist($employee);
  169.                 $this->entityManager->persist($employeeHistory);
  170.                 $this->entityManager->flush();
  171.                 $this->addFlash(
  172.                     'success',
  173.                     "La période a bien été ajoutée !"
  174.                 );
  175.                 return $this->redirectToRoute('employee_show', ['id' => $employee->getId()]);
  176.             }
  177.         }
  178.         return $this->render(
  179.             'employee_history/new_edit.html.twig',
  180.             [
  181.                 'title' => 'Ajouter',
  182.                 'menu' => 'employee_new',
  183.                 'employeeHistory' => $employeeHistory,
  184.                 'form' => $form->createView(),
  185.             ]
  186.         );
  187.     }
  188.     /**
  189.      * @Route("/{id}/edit", name="employee_history_edit", methods={"GET","POST"})
  190.      * @IsGranted("EMPLOYEE_HISTORY_EDIT", subject="", message="Permission requise")
  191.      */
  192.     public function edit(
  193.         Request         $request,
  194.         EmployeeHistory $employeeHistory,
  195.         EmployeeService $employeeService
  196.     ): Response
  197.     {
  198.         $form $this->createForm(EmployeeHistoryType::class, $employeeHistory);
  199.         $form
  200.             ->remove('price')
  201.             ->remove('salaryType');
  202.         $form->handleRequest($request);
  203.         if ($employeeHistory->getEmploye()->getLastHistoryOrNull() != $employeeHistory) {
  204.             die('Cannot old data');
  205.         }
  206.         if ($form->isSubmitted() && $form->isValid()) {
  207.             $ok true;
  208.             if (!empty($employeeHistory->getEndDate()) && $employeeHistory->getEndDate() < $employeeHistory->getStartDate()) {
  209.                 $this->addFlash(
  210.                     'danger',
  211.                     "La date de fin doit etre >= la date de début !"
  212.                 );
  213.                 $ok false;
  214.             }
  215.             //TODO: NEED TO CHECK IF AN EXISTING LOG FOUNDS IN THIS PERIODE; IF THIS WE CANNOT EDIT IT UNLESS WE REMOVE THE LOG
  216.             //dd($employeeService->lastLogInDate($employeeHistory->getEmploye(), $employeeHistory->getStartDate()));
  217.             if ($ok) {
  218.                 $employee $employeeHistory->getEmploye();
  219.                 $employee->setLastHistory($employeeHistory);
  220.                 $this->entityManager->persist($employee);
  221.                 $this->entityManager->persist($employeeHistory);
  222.                 $this->entityManager->flush();
  223.                 $this->addFlash(
  224.                     'success',
  225.                     "La période a bien été editée !"
  226.                 );
  227.                 return $this->redirectToRoute('employee_show', ['id' => $employeeHistory->getEmploye()->getId()]);
  228.             }
  229.         }
  230.         return $this->render(
  231.             'employee_history/new_edit.html.twig',
  232.             [
  233.                 'title' => sprintf('%s: %s''Fiche'$employeeHistory->getEmploye()->getFullName()),
  234.                 'menu' => 'employee_new',
  235.                 'employeeHistory' => $employeeHistory,
  236.                 'form' => $form->createView(),
  237.             ]
  238.         );
  239.     }
  240. }