src/Controller/EmployeeController.php line 286

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Dto\EmployeePictureDto;
  4. use App\Entity\Employee;
  5. use App\Entity\EmployeeHistory;
  6. use App\Enum\EmployeeHistory\SalaryType;
  7. use App\Form\EmployeeType;
  8. use App\Helper\All;
  9. use App\Repository\EmployeeDepartmentRepository;
  10. use App\Repository\EmployeeRepository;
  11. use App\Service\EmployeeAdvanceService;
  12. use App\Service\EmployeeService;
  13. use Doctrine\Persistence\ManagerRegistry;
  14. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  15. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  16. use Symfony\Component\Asset\Packages;
  17. use Symfony\Component\HttpFoundation\JsonResponse;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\Response;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. use Vich\UploaderBundle\Templating\Helper\UploaderHelper;
  22. /**
  23.  * @Route("/employee")
  24.  */
  25. class EmployeeController extends AbstractController
  26. {
  27.     /** @var EmployeeRepository $employeeRepository */
  28.     private $employeeRepository;
  29.     /** @var ManagerRegistry $entityManager */
  30.     private $entityManager;
  31.     /** @var EmployeeService $employeeService */
  32.     private $employeeService;
  33.     public function __construct(
  34.         EmployeeRepository $employeeRepository,
  35.         ManagerRegistry    $entityManager,
  36.         EmployeeService    $employeeService
  37.     )
  38.     {
  39.         $this->employeeRepository $employeeRepository;
  40.         $this->entityManager $entityManager->getManager();
  41.         $this->employeeService $employeeService;
  42.     }
  43.     /**
  44.      * @Route("/production_plan", name="employee_production_plan", methods={"GET"})
  45.      */
  46.     public function production_plan()
  47.     {
  48.         return $this->render(
  49.             'employee/production_plan.html.twig',
  50.             [
  51.                 'title' => "Plan de production",
  52.                 'menu' => 'employee_production_plan',
  53.                 'employees' => $this->employeeRepository->getOnlyMachinary(),
  54.             ]
  55.         );
  56.     }
  57.     /**
  58.      * @Route("/grid_view", name="employee_index_grid", methods={"GET"})
  59.      * @IsGranted("EMPLOYEE_INDEX_GRID")
  60.      */
  61.     public function employee_grid(): Response
  62.     {
  63.         $employees $this->employeeRepository->activeAndHourlyEmployees();
  64.         //group by position
  65.         $listByDepartment = [];
  66.         foreach ($employees as $employee) {
  67.             /** @var Employee $employee */
  68.             $department $employee->getLastHistory()->getDepartment();
  69.             if (empty($listByDepartment[$department->getId()])) {
  70.                 $listByDepartment[$department->getId()] = [];
  71.                 $listByDepartment[$department->getId()]['department'] = $department;
  72.             }
  73.             $listByDepartment[$department->getId()]['list'][$employee->getId()] = $employee;
  74.         }
  75.         asort($listByDepartment);
  76.         return $this->render(
  77.             'employee_log_hour/index_grid.html.twig',
  78.             [
  79.                 'title' => "Liste des Employé(e)s",
  80.                 'menu' => 'employee_index_grid',
  81.                 'listByDepartment' => $listByDepartment,
  82.                 'count' => count($listByDepartment),
  83.             ]
  84.         );
  85.     }
  86.     /**
  87.      * @Route("/", name="employee_index", methods={"GET"})
  88.      * @IsGranted("EMPLOYEE_INDEX")
  89.      */
  90.     public function employee(EmployeeDepartmentRepository $employeeDepartmentRepository): Response
  91.     {
  92.         return $this->render(
  93.             'employee/index.html.twig',
  94.             [
  95.                 'title' => "Liste des Employé(e)s",
  96.                 'menu' => 'employee_index',
  97.                 'departments' => $employeeDepartmentRepository->findAll()
  98.             ]
  99.         );
  100.     }
  101.     /**
  102.      * @Route("/list", name="employee_list", methods={"GET","POST"})
  103.      * @IsGranted("EMPLOYEE_INDEX")
  104.      * @param Request $request
  105.      * @param UploaderHelper $uploaderHelper
  106.      * @param Packages $packages
  107.      * @return JsonResponse
  108.      */
  109.     public function listTable(
  110.         Request        $request,
  111.         UploaderHelper $uploaderHelper,
  112.         Packages       $packages
  113.     ): JsonResponse
  114.     {
  115.         $query $request->get('search')['value'];
  116.         $tableColumns = [
  117.             "",
  118.             "e.id",
  119.             "e.registration_number",
  120.             "e.firstName",
  121.             "e.lastName",
  122.             "e.cin",
  123.             "e.phoneNumber",
  124.             "eh.startDate",
  125.             "eh.endDate",
  126.             "ehd.name",
  127.             "ehp.name",
  128.             "eh.price",
  129.             "e.cnss",
  130.             "e.dateCnss",
  131.             "",
  132.             ""
  133.         ];
  134.         $orderByColumn = !empty($tableColumns[$request->get('order')[0]['column']]) ? $tableColumns[$request->get(
  135.             'order'
  136.         )[0]['column']] : 'e.id';
  137.         $orderDirection $request->get('order')[0]['dir'];
  138.         $limit $request->get('length') != '-1' $request->get('length') : '';
  139.         $offset $request->get('start');
  140.         $draw intval($request->get('draw'));
  141.         $filters = !empty($request->get('filters')) && is_array($request->get('filters')) ? $request->get('filters') : '';
  142.         $employees $this->employeeRepository->getListTable(
  143.             $query,
  144.             $filters,
  145.             $orderByColumn,
  146.             $orderDirection,
  147.             $limit,
  148.             $offset
  149.         );
  150.         $data = [];
  151.         foreach ($employees['data'] as $employee) {
  152.             /** @var Employee $employee */
  153.             $picture $uploaderHelper->asset($employee'pictureFile') . '?v4';
  154.             if (empty($employee->getPicture())) {
  155.                 $picture $packages->getUrl('assets/theme/media/employees/blank.png');
  156.             }
  157.             $picture '
  158. <div class="d-flex align-items-center">
  159.     <div class="symbol symbol-50 flex-shrink-0">
  160.     <img src="' $picture '" alt="' $employee->getFullName() . '" class="zoom-image" data-image-zoom="' $picture '">
  161.     </div>
  162. </div>
  163. ';
  164.             $buttons '';
  165.             $show_link $this->generateUrl('employee_show', ['id' => $employee->getId()]);
  166.             $buttons .= '<a href="' $show_link '"><i class="flaticon-search-1"> Afficher</i> </a>';
  167.             $editPositionUrl '';
  168.             $startDate $endDate '-';
  169.             $departement '';
  170.             $price '******';
  171.             if ($employee->getLastHistory() instanceof EmployeeHistory) {
  172.                 $employeeHistory $employee->getLastHistoryOrNull();
  173.                 /** @var EmployeeHistory $employeeHistory */
  174.                 if (in_array('ROLE_ADMIN'$this->getUser()->getRoles()) || SalaryType::HOURLY === $employeeHistory->getSalaryType()) {
  175.                     $price $employee->getLastHistory()->getPrice();
  176.                 }
  177.                 $startDate $employee->getLastHistoryOrNull()->getStartDate()->format('d/m/Y');
  178.                 if (!empty($employee->getLastHistoryOrNull()->getEndDate())) {
  179.                     $endDate $employee->getLastHistoryOrNull()->getEndDate()->format('d/m/Y');
  180.                 }
  181.                 $departement $employeeHistory->getDepartment()->getName();
  182.                 $position $employeeHistory->getPosition()->getName();
  183.                 $editPositionUrl sprintf('<a href="%s">%s</a>'$this->generateUrl('employee_history_edit', ['id' => $employee->getLastHistoryOrNull()->getId()]), $position);
  184.             }
  185.             $dateCnss $employee->getDateCnss();
  186.             if (!empty($dateCnss)) {
  187.                 $dateCnss $dateCnss->format('d/m/Y');
  188.             }
  189.             $data[] = [
  190.                 $picture,
  191.                 $employee->getId(),
  192.                 $employee->getRegistrationNumber(),
  193.                 $employee->getFirstName(),
  194.                 $employee->getLastName(),
  195.                 $employee->getCin(),
  196.                 $employee->getPhoneNumber(),
  197.                 $startDate,
  198.                 $endDate,
  199.                 $departement,
  200.                 $editPositionUrl,
  201.                 $price,
  202.                 $employee->getCnss(),
  203.                 $dateCnss,
  204.                 All::statusBadge($employee->isActiveEmployee()),
  205.                 $buttons,
  206.             ];
  207.         }
  208.         $data = [
  209.             "draw" => $draw,
  210.             "recordsTotal" => intval($employees['recordsTotal']),
  211.             "recordsFiltered" => intval($employees['recordsFiltered']),
  212.             "data" => $data,
  213.         ];
  214.         return new JsonResponse($data);
  215.     }
  216.     /**
  217.      * @Route("/new", name="employee_new", methods={"GET","POST"})
  218.      * @IsGranted("EMPLOYEE_NEW", subject="", message="Permission requise")
  219.      */
  220.     public function new(
  221.         Request $request
  222.     ): Response
  223.     {
  224.         $employee = new Employee();
  225.         $form $this->createForm(EmployeeType::class, $employee);
  226.         $form->handleRequest($request);
  227.         if ($form->isSubmitted() && $form->isValid()) {
  228.             $pictureFile $request->files->get('pictureFile');
  229.             if (!empty($pictureFile)) {
  230.                 $pictureDto = new EmployeePictureDto($pictureFile$employee);
  231.                 $this->employeeService->updatePicture($pictureDto);
  232.                 $this->entityManager->flush();
  233.                 $this->addFlash('success''Photo ajouté avec succès');
  234.             } elseif (!empty($request->get('no_picture'))) {
  235.                 //if no picture remove the actual
  236.                 $employee->setPicture(null);
  237.             }
  238.             $this->entityManager->persist($employee);
  239.             $this->entityManager->flush();
  240.             $this->addFlash(
  241.                 'success',
  242.                 "L'employé a bien été ajouté !"
  243.             );
  244.             return $this->redirectToRoute('employee_index');
  245.         }
  246.         return $this->render(
  247.             'employee/new_edit.html.twig',
  248.             [
  249.                 'title' => 'Ajouter',
  250.                 'menu' => 'employee_new',
  251.                 'employee' => $employee,
  252.                 'form' => $form->createView(),
  253.             ]
  254.         );
  255.     }
  256.     /**
  257.      * @Route("/{id}/edit", name="employee_edit", methods={"GET","POST"})
  258.      * @IsGranted("EMPLOYEE_EDIT", subject="", message="Permission requise")
  259.      */
  260.     public function edit(
  261.         Request  $request,
  262.         Employee $employee
  263.     ): Response
  264.     {
  265.         $form $this->createForm(EmployeeType::class, $employee);
  266.         $form->handleRequest($request);
  267.         if ($form->isSubmitted() && $form->isValid()) {
  268.             $pictureFile $request->files->get('pictureFile');
  269.             if (!empty($pictureFile)) {
  270.                 $pictureDto = new EmployeePictureDto($pictureFile$employee);
  271.                 $this->employeeService->updatePicture($pictureDto);
  272.                 $this->entityManager->flush();
  273.                 $this->addFlash('success''Photo mis à jour avec succès');
  274.             } elseif (!empty($request->get('no_picture'))) {
  275.                 //if no picture remove the actual
  276.                 $employee->setPicture(null);
  277.             }
  278.             $this->entityManager->persist($employee);
  279.             $this->entityManager->flush();
  280.             $this->addFlash(
  281.                 'success',
  282.                 "L'employé a bien été edité !"
  283.             );
  284.             return $this->redirectToRoute('employee_show', ['id' => $employee->getId()]);
  285.         }
  286.         return $this->render(
  287.             'employee/new_edit.html.twig',
  288.             [
  289.                 'title' => sprintf('%s: %s''Fiche'$employee->getFullName()),
  290.                 'menu' => 'employee_new',
  291.                 'employee' => $employee,
  292.                 'form' => $form->createView(),
  293.             ]
  294.         );
  295.     }
  296.     /**
  297.      * @Route("/{id}", name="employee_show", methods={"GET"}, requirements={"id":"\d+"})
  298.      * @IsGranted("EMPLOYEE_SHOW", subject="", message="Permission requise")
  299.      */
  300.     public function show(Employee $employeeEmployeeAdvanceService $employeeAdvanceService)
  301.     {
  302.         return $this->render(
  303.             'employee/show.html.twig',
  304.             [
  305.                 'title' => sprintf('%s: %s''Fiche'$employee->getFullName()),
  306.                 'menu' => 'employee_new',
  307.                 'employee' => $employee,
  308.                 'form' => $this->createForm(EmployeeType::class, $employee, ['disabled' => true])->createView(),
  309.                 'readonly' => true,
  310.                 //'employee_log_hour' => $this->employeeService->htmlHourLogForEmployee($employee)
  311.             ]
  312.         );
  313.     }
  314. }