src/Controller/EmployeeLogHourController.php line 112

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Employee;
  4. use App\Entity\EmployeeHourLog;
  5. use App\Enum\EmployeeHourLog\InOut;
  6. use App\Repository\EmployeeHourLogRepository;
  7. use App\Service\EmployeeService;
  8. use Doctrine\Persistence\ManagerRegistry;
  9. use Dompdf\Dompdf;
  10. use Dompdf\Options;
  11. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpFoundation\JsonResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. /**
  18.  * @Route("/employee_log_hour")
  19.  */
  20. class EmployeeLogHourController extends AbstractController
  21. {
  22.     /** @var ManagerRegistry $entityManager */
  23.     private $entityManager;
  24.     /** @var EmployeeService $employeeService */
  25.     private $employeeService;
  26.     /** @var EmployeeHourLogRepository $employeeHourLogRepository */
  27.     private $employeeHourLogRepository;
  28.     public function __construct(
  29.         ManagerRegistry           $entityManager,
  30.         EmployeeService           $employeeService,
  31.         EmployeeHourLogRepository $employeeHourLogRepository,
  32.     )
  33.     {
  34.         $this->entityManager $entityManager->getManager();
  35.         $this->employeeService $employeeService;
  36.         $this->employeeHourLogRepository $employeeHourLogRepository;
  37.     }
  38.     /**
  39.      * @Route("/", name="employee_log_hour_index", methods={"GET"})
  40.      * @IsGranted("EMPLOYEE_LOG_HOUR_INDEX")
  41.      */
  42.     public function employee()
  43.     {
  44.     }
  45.     /**
  46.      * @Route("/calendar", name="employee_log_hour_calendar", methods={"GET"})
  47.      * @IsGranted("EMPLOYEE_LOG_HOUR_CALENDAR", subject="", message="Permission requise")
  48.      */
  49.     public function calendar()
  50.     {
  51.         return $this->render(
  52.             'employee_log_hour/calendar.html.twig',
  53.             [
  54.                 'title' => 'Calendar',
  55.                 'menu' => 'employee_log_hour_calendar',
  56.                 'employees' => $this->employeeService->getActiveAndHourlyEmployees(),
  57.                 'employees_log_hour' => $this->employeeService->getEmployeesHourLog()
  58.             ]
  59.         );
  60.     }
  61.     /**
  62.      * @Route("/log", name="employee_log_hour_new", methods={"GET"})
  63.      * @IsGranted("EMPLOYEE_LOG_HOUR_NEW", subject="", message="Permission requise: EMPLOYEE_LOG_HOUR_NEW")
  64.      */
  65.     public function log(Request $request): Response
  66.     {
  67.         $date $request->get('date');
  68.         $logDate $date ?? (new \DateTime())->format('d/m/Y');
  69.         return $this->render(
  70.             'employee_log_hour/log.html.twig',
  71.             [
  72.                 'title' => 'Pointages',
  73.                 'employees' => $this->employeeService->getActiveAndHourlyEmployeesWithLog(false$logDate),
  74.                 'date' => $logDate,
  75.                 'menu' => 'employee_log_hour_new',
  76.             ]
  77.         );
  78.     }
  79.     /**
  80.      * @Route("/log_new", name="employee_log_hour_new_new", methods={"GET"})
  81.      * @IsGranted("EMPLOYEE_LOG_HOUR_NEW", subject="", message="Permission requise: EMPLOYEE_LOG_HOUR_NEW")
  82.      */
  83.     public function log_new(Request $request): Response
  84.     {
  85.         $date $request->get('date');
  86.         $logDate $date ?? (new \DateTime())->format('d/m/Y');
  87.         return $this->render(
  88.             'employee_log_hour/log_new.html.twig',
  89.             [
  90.                 'title' => 'Pointages',
  91.                 'employees' => $this->employeeService->getActiveAndHourlyEmployeesWithLog(false$logDate),
  92.                 'date' => $logDate,
  93.                 'menu' => 'employee_log_hour_new',
  94.             ]
  95.         );
  96.     }
  97.     /**
  98.      * @Route("/new", name="employee_log_hour_add", methods={"GET","POST"})
  99.      * @IsGranted("EMPLOYEE_LOG_HOUR_NEW", subject="", message="Permission requise")
  100.      */
  101.     public function addLog(Request $request): JsonResponse
  102.     {
  103.         $employeeId = (int)$request->get('employeeId');
  104.         $logDate = (string)$request->get('logDate');
  105.         $logTime = (string)$request->get('logTime');
  106.         $inOut = (int)$request->get('inOut');
  107.         $employee $this->employeeService->findOneBy(['id' => $employeeId]);
  108.         if (!$employee instanceof Employee) {
  109.             return new JsonResponse(
  110.                 [
  111.                     'message' => 'Aucun employée trouvé',
  112.                     'status' => false,
  113.                     'history' => []
  114.                 ]
  115.             );
  116.         }
  117.         $logDateTime \DateTime::createFromFormat('d/m/Y H:i'sprintf('%s %s'$logDate$logTime));
  118.         $employeeLogData $this->employeeService->getActiveAndHourlyEmployeesWithLog($employee$logDateTime->format('Y-m-d'));
  119.         $checkIfExist $this->employeeHourLogRepository->findBy(
  120.             [
  121.                 'employee' => $employee,
  122.                 'date' => $logDateTime
  123.             ]
  124.         );
  125.         if ($checkIfExist) {
  126.             return new JsonResponse(
  127.                 [
  128.                     'message' => 'Cet enregistrement existe déja',
  129.                     'status' => false,
  130.                     'historyHtml' => $employeeLogData[0]['historyHtml']
  131.                 ]
  132.             );
  133.         }
  134.         $lastLog $this->employeeHourLogRepository->findLastLog($employee$logDateTime);
  135.         if (!empty($lastLog)) {
  136.             $lastLog $lastLog[0];
  137.             /** @var EmployeeHourLog $lastLog */
  138.             if ($inOut === $lastLog->getInOut()->value) {
  139.                 return new JsonResponse(
  140.                     [
  141.                         'message' => 'L\'employée est déja en état : ' InOut::from($inOut)->title(),
  142.                         'status' => false,
  143.                         'historyHtml' => $employeeLogData[0]['historyHtml']
  144.                     ]
  145.                 );
  146.             }
  147.             if ($logDateTime $lastLog->getDate()) {
  148.                 return new JsonResponse(
  149.                     [
  150.                         'message' => 'l\'enregistrement est inférieur au dernier enregistrement saisie : ' $lastLog->getDate()->format('H:i'),
  151.                         'status' => false,
  152.                         'historyHtml' => $employeeLogData[0]['historyHtml']
  153.                     ]
  154.                 );
  155.             }
  156.         }
  157.         $employeeLogHour = new EmployeeHourLog();
  158.         $employeeLogHour
  159.             ->setEmployee($employee)
  160.             ->setInOut(InOut::from($inOut))
  161.             ->setDate($logDateTime);
  162.         $this->entityManager->persist($employeeLogHour);
  163.         $this->entityManager->persist($employee);
  164.         $this->entityManager->flush();
  165.         $this->entityManager->refresh($employee);
  166.         return new JsonResponse(
  167.             [
  168.                 'message' => 'Enregistrement ajouté avec succés',
  169.                 'status' => true,
  170.                 'historyHtml' => $this->employeeService->htmlEmployeeHourLog($employee$logDateTime)
  171.             ]
  172.         );
  173.     }
  174.     /**
  175.      * @Route("/new_new", name="employee_log_hour_add_new", methods={"GET","POST"})
  176.      * @IsGranted("EMPLOYEE_LOG_HOUR_NEW", subject="", message="Permission requise")
  177.      */
  178.     public function addLogNew(Request $request): JsonResponse
  179.     {
  180.         $employeeId = (int)$request->get('employeeId');
  181.         $logDate = (string)$request->get('logDate');
  182.         $logs $request->request->all('logs');
  183.         $employee $this->employeeService->findOneBy(['id' => $employeeId]);
  184.         if (!$employee instanceof Employee) {
  185.             return new JsonResponse(
  186.                 [
  187.                     'message' => 'Aucun employée trouvé',
  188.                     'status' => false,
  189.                     'history' => []
  190.                 ]
  191.             );
  192.         }
  193.         if (empty($logs) || !is_array($logs)) {
  194.             return new JsonResponse(
  195.                 [
  196.                     'message' => 'Aucun pointage saisi',
  197.                     'status' => false,
  198.                     'historyHtml' => ''
  199.                 ]
  200.             );
  201.         }
  202.         $logDateObject \DateTime::createFromFormat('d/m/Y'$logDate);
  203.         if (!$logDateObject instanceof \DateTime) {
  204.             return new JsonResponse(
  205.                 [
  206.                     'message' => 'Date invalide',
  207.                     'status' => false,
  208.                     'historyHtml' => ''
  209.                 ]
  210.             );
  211.         }
  212.         $employeeLogData $this->employeeService->getActiveAndHourlyEmployeesWithLog($employee$logDateObject->format('Y-m-d'));
  213.         $historyHtml $employeeLogData[0]['historyHtml'] ?? '';
  214.         $previousDateTime null;
  215.         $previousInOut null;
  216.         $preparedLogs = [];
  217.         foreach ($logs as $index => $log) {
  218.             $expectedStep $index 1;
  219.             $step = isset($log['step']) ? (int)$log['step'] : 0;
  220.             $time = isset($log['time']) ? trim((string)$log['time']) : '';
  221.             if ($step !== $expectedStep) {
  222.                 return new JsonResponse(
  223.                     [
  224.                         'message' => 'Les pointages doivent être envoyés dans l’ordre de 1 à 6, sans rupture.',
  225.                         'status' => false,
  226.                         'historyHtml' => $historyHtml
  227.                     ]
  228.                 );
  229.             }
  230.             if ($step || $step 6) {
  231.                 return new JsonResponse(
  232.                     [
  233.                         'message' => 'Le numéro de pointage est invalide.',
  234.                         'status' => false,
  235.                         'historyHtml' => $historyHtml
  236.                     ]
  237.                 );
  238.             }
  239.             if (!preg_match('/^([01]\\d|2[0-3]):[0-5]\\d$/'$time)) {
  240.                 return new JsonResponse(
  241.                     [
  242.                         'message' => 'Heure invalide au pointage ' $step,
  243.                         'status' => false,
  244.                         'historyHtml' => $historyHtml
  245.                     ]
  246.                 );
  247.             }
  248.             $logDateTime \DateTime::createFromFormat('d/m/Y H:i'sprintf('%s %s'$logDate$time));
  249.             if (!$logDateTime instanceof \DateTime) {
  250.                 return new JsonResponse(
  251.                     [
  252.                         'message' => 'Date ou heure invalide au pointage ' $step,
  253.                         'status' => false,
  254.                         'historyHtml' => $historyHtml
  255.                     ]
  256.                 );
  257.             }
  258.             if ($previousDateTime instanceof \DateTime && $logDateTime <= $previousDateTime) {
  259.                 return new JsonResponse(
  260.                     [
  261.                         'message' => 'Les heures doivent être saisies dans un ordre chronologique strictement croissant.',
  262.                         'status' => false,
  263.                         'historyHtml' => $historyHtml
  264.                     ]
  265.                 );
  266.             }
  267.             $inOut InOut::from($step === 2);
  268.             if ($previousInOut instanceof InOut && $previousInOut->value === $inOut->value) {
  269.                 return new JsonResponse(
  270.                     [
  271.                         'message' => 'L’enchaînement entrée / sortie est invalide au pointage ' $step,
  272.                         'status' => false,
  273.                         'historyHtml' => $historyHtml
  274.                     ]
  275.                 );
  276.             }
  277.             $checkIfExist $this->employeeHourLogRepository->findBy(
  278.                 [
  279.                     'employee' => $employee,
  280.                     'date' => $logDateTime
  281.                 ]
  282.             );
  283.             if ($checkIfExist) {
  284.                 return new JsonResponse(
  285.                     [
  286.                         'message' => 'Un pointage existe déjà à ' $logDateTime->format('H:i'),
  287.                         'status' => false,
  288.                         'historyHtml' => $historyHtml
  289.                     ]
  290.                 );
  291.             }
  292.             $lastLog $this->employeeHourLogRepository->findLastLog($employee$logDateTime);
  293.             if (!empty($lastLog)) {
  294.                 $lastLog $lastLog[0];
  295.                 /** @var EmployeeHourLog $lastLog */
  296.                 if ($lastLog->getInOut()->value === $inOut->value) {
  297.                     return new JsonResponse(
  298.                         [
  299.                             'message' => 'L\'employée est déjà en état : ' $inOut->title(),
  300.                             'status' => false,
  301.                             'historyHtml' => $historyHtml
  302.                         ]
  303.                     );
  304.                 }
  305.                 if ($logDateTime $lastLog->getDate()) {
  306.                     return new JsonResponse(
  307.                         [
  308.                             'message' => 'L\'enregistrement est inférieur au dernier enregistrement saisi : ' $lastLog->getDate()->format('H:i'),
  309.                             'status' => false,
  310.                             'historyHtml' => $historyHtml
  311.                         ]
  312.                     );
  313.                 }
  314.             }
  315.             $preparedLogs[] = [
  316.                 'date' => $logDateTime,
  317.                 'inOut' => $inOut,
  318.             ];
  319.             $previousDateTime $logDateTime;
  320.             $previousInOut $inOut;
  321.         }
  322.         foreach ($preparedLogs as $preparedLog) {
  323.             $employeeLogHour = new EmployeeHourLog();
  324.             $employeeLogHour
  325.                 ->setEmployee($employee)
  326.                 ->setInOut($preparedLog['inOut'])
  327.                 ->setDate($preparedLog['date']);
  328.             $this->entityManager->persist($employeeLogHour);
  329.         }
  330.         $this->entityManager->persist($employee);
  331.         $this->entityManager->flush();
  332.         $this->entityManager->refresh($employee);
  333.         $lastPreparedLog end($preparedLogs);
  334.         $lastDate $lastPreparedLog['date'] ?? $logDateObject;
  335.         return new JsonResponse(
  336.             [
  337.                 'message' => count($preparedLogs) . ' pointage(s) ajouté(s) avec succès',
  338.                 'status' => true,
  339.                 'historyHtml' => $this->employeeService->htmlEmployeeHourLog($employee$lastDate)
  340.             ]
  341.         );
  342.     }
  343.     /**
  344.      * @Route("/remove", name="employee_log_hour_remove", methods={"GET","POST"})
  345.      * @IsGranted("EMPLOYEE_LOG_HOUR_REMOVE", subject="", message="Permission requise")
  346.      */
  347.     public function removeLog(Request $request): JsonResponse
  348.     {
  349.         $employeeHourLog $this->employeeHourLogRepository->find((int)$request->get('id'));
  350.         $this>$this->entityManager->remove($employeeHourLog);
  351.         $this->entityManager->flush();
  352.         return new JsonResponse(
  353.             [
  354.                 'message' => 'Enregistrement supprimé avec succés',
  355.                 'status' => true,
  356.             ]
  357.         );
  358.     }
  359.     /**
  360.      * @Route("/log_sheet", name="employee_log_sheet", methods={"GET","POST"})
  361.      * @IsGranted("EMPLOYEE_LOG_HOUR_PRINT", subject="", message="Permission requise")
  362.      */
  363.     public function print_prepare(
  364.         Request $request
  365.     )
  366.     {
  367.         $date $request->get('date');
  368.         $logDate $date ?? (new \DateTime())->format('d/m/Y');
  369.         $employees $this->employeeService->getActiveAndHourlyEmployeesWithLog(false$logDate);
  370.         $pdfOptions = new Options();
  371.         $pdfOptions->setDefaultFont('Arial');
  372.         $pdfOptions->setIsRemoteEnabled(true);
  373.         $pdfOptions->setIsHtml5ParserEnabled(true);
  374.         $pdfOptions->setDefaultPaperSize('A4');
  375.         $dompdf = new Dompdf($pdfOptions);
  376.         $logDate \DateTime::createFromFormat('d/m/Y'$logDate)->format('d-m-Y');
  377.         $html $this->renderView(
  378.             'employee_log_hour/sheet_pdf.html.twig',
  379.             [
  380.                 'title' => $fileName sprintf(
  381.                     '%s-%s.pdf',
  382.                     'Liste du pointage : ',
  383.                     $logDate
  384.                 ),
  385.                 'employees' => $employees,
  386.                 'logDate' => $logDate
  387.             ]
  388.         );
  389.         $dompdf->loadHtml($html);
  390.         $dompdf->setPaper('A4''portrait');
  391.         $dompdf->render();
  392.         $this->injectPageCount($dompdf);
  393.         $dompdf->stream(
  394.             $fileName,
  395.             [
  396.                 "Attachment" => false,
  397.             ]
  398.         );
  399.         return new Response(''200, [
  400.             'Content-Type' => 'application/pdf',
  401.         ]);
  402.     }
  403.     private function injectPageCount(Dompdf $dompdf)
  404.     {
  405.         $canvas $dompdf->getCanvas();
  406.         $pdf $canvas->get_cpdf();
  407.         foreach ($pdf->objects as &$o) {
  408.             if ($o['t'] === 'contents') {
  409.                 $o['c'] = str_replace('PG'$canvas->get_page_count(), $o['c']);
  410.             }
  411.         }
  412.     }
  413.     /**
  414.      * @Route("/absence_day", name="absence_day", methods={"GET"})
  415.      * @param Request $request
  416.      * @return Response
  417.      */
  418.     public function absence_day(Request $request): Response
  419.     {
  420.         $date $request->get('date');
  421.         $startDate $endDate $date ?? (new \DateTime())->format('d/m/Y');
  422.         $absenceList $this->employeeService->absenceInDate($startDate$endDate);
  423.         //group by position
  424.         $absenceListByDepartment = [];
  425.         foreach ($absenceList as $absentEmployee) {
  426.             /** @var Employee $absentEmployee */
  427.             $department $absentEmployee->getLastHistory()->getDepartment();
  428.             if (empty($absenceListByDepartment[$department->getId()])) {
  429.                 $absenceListByDepartment[$department->getId()] = [];
  430.                 $absenceListByDepartment[$department->getId()]['department'] = $department;
  431.             }
  432.             $absenceListByDepartment[$department->getId()]['absenceList'][$absentEmployee->getId()] = $absentEmployee;
  433.         }
  434.         asort($absenceListByDepartment);
  435.         return $this->render(
  436.             'employee_log_hour/absence.html.twig',
  437.             [
  438.                 'title' => 'Absence du : ' $startDate,
  439.                 'menu' => 'employee_log_hour_absence',
  440.                 'absenceListByDepartment' => $absenceListByDepartment,
  441.                 'count' => count($absenceList),
  442.             ]
  443.         );
  444.     }
  445.     /**
  446.      * @Route("/presence_day", name="presence_day", methods={"GET"})
  447.      * @param Request $request
  448.      * @return Response
  449.      */
  450.     public function presence_day(Request $request): Response
  451.     {
  452.         $date $request->get('date');
  453.         $startDate $endDate $date ?? (new \DateTime())->format('d/m/Y');
  454.         $presenceList $this->employeeService->presenceInDate($startDate$endDate);
  455.         //group by position
  456.         $presenceListByDepartment = [];
  457.         foreach ($presenceList as $presentEmployee) {
  458.             /** @var Employee $presentEmployee */
  459.             $department $presentEmployee->getLastHistory()->getDepartment();
  460.             if (empty($presenceListByDepartment[$department->getId()])) {
  461.                 $presenceListByDepartment[$department->getId()] = [];
  462.                 $presenceListByDepartment[$department->getId()]['department'] = $department;
  463.             }
  464.             $presenceListByDepartment[$department->getId()]['presenceList'][$presentEmployee->getId()] = $presentEmployee;
  465.         }
  466.         asort($presenceListByDepartment);
  467.         return $this->render(
  468.             'employee_log_hour/presence.html.twig',
  469.             [
  470.                 'title' => 'Présence du : ' $startDate,
  471.                 'menu' => 'employee_log_hour_presence',
  472.                 'presenceListByDepartment' => $presenceListByDepartment,
  473.                 'count' => count($presenceList),
  474.             ]
  475.         );
  476.     }
  477. }