<?php
namespace App\Controller;
use App\Entity\Employee;
use App\Entity\EmployeeHourLog;
use App\Enum\EmployeeHourLog\InOut;
use App\Repository\EmployeeHourLogRepository;
use App\Service\EmployeeService;
use Doctrine\Persistence\ManagerRegistry;
use Dompdf\Dompdf;
use Dompdf\Options;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/employee_log_hour")
*/
class EmployeeLogHourController extends AbstractController
{
/** @var ManagerRegistry $entityManager */
private $entityManager;
/** @var EmployeeService $employeeService */
private $employeeService;
/** @var EmployeeHourLogRepository $employeeHourLogRepository */
private $employeeHourLogRepository;
public function __construct(
ManagerRegistry $entityManager,
EmployeeService $employeeService,
EmployeeHourLogRepository $employeeHourLogRepository,
)
{
$this->entityManager = $entityManager->getManager();
$this->employeeService = $employeeService;
$this->employeeHourLogRepository = $employeeHourLogRepository;
}
/**
* @Route("/", name="employee_log_hour_index", methods={"GET"})
* @IsGranted("EMPLOYEE_LOG_HOUR_INDEX")
*/
public function employee()
{
}
/**
* @Route("/calendar", name="employee_log_hour_calendar", methods={"GET"})
* @IsGranted("EMPLOYEE_LOG_HOUR_CALENDAR", subject="", message="Permission requise")
*/
public function calendar()
{
return $this->render(
'employee_log_hour/calendar.html.twig',
[
'title' => 'Calendar',
'menu' => 'employee_log_hour_calendar',
'employees' => $this->employeeService->getActiveAndHourlyEmployees(),
'employees_log_hour' => $this->employeeService->getEmployeesHourLog()
]
);
}
/**
* @Route("/log", name="employee_log_hour_new", methods={"GET"})
* @IsGranted("EMPLOYEE_LOG_HOUR_NEW", subject="", message="Permission requise: EMPLOYEE_LOG_HOUR_NEW")
*/
public function log(Request $request): Response
{
$date = $request->get('date');
$logDate = $date ?? (new \DateTime())->format('d/m/Y');
return $this->render(
'employee_log_hour/log.html.twig',
[
'title' => 'Pointages',
'employees' => $this->employeeService->getActiveAndHourlyEmployeesWithLog(false, $logDate),
'date' => $logDate,
'menu' => 'employee_log_hour_new',
]
);
}
/**
* @Route("/new", name="employee_log_hour_add", methods={"GET","POST"})
* @IsGranted("EMPLOYEE_LOG_HOUR_NEW", subject="", message="Permission requise")
*/
public function addLog(Request $request): JsonResponse
{
$employeeId = (int)$request->get('employeeId');
$logDate = (string)$request->get('logDate');
$logTime = (string)$request->get('logTime');
$inOut = (int)$request->get('inOut');
$employee = $this->employeeService->findOneBy(['id' => $employeeId]);
if (!$employee instanceof Employee) {
return new JsonResponse(
[
'message' => 'Aucun employée trouvé',
'status' => false,
'history' => []
]
);
}
$logDateTime = \DateTime::createFromFormat('d/m/Y H:i', sprintf('%s %s', $logDate, $logTime));
$employeeLogData = $this->employeeService->getActiveAndHourlyEmployeesWithLog($employee, $logDateTime->format('Y-m-d'));
$checkIfExist = $this->employeeHourLogRepository->findBy(
[
'employee' => $employee,
'date' => $logDateTime
]
);
if ($checkIfExist) {
return new JsonResponse(
[
'message' => 'Cet enregistrement existe déja',
'status' => false,
'historyHtml' => $employeeLogData[0]['historyHtml']
]
);
}
$lastLog = $this->employeeHourLogRepository->findLastLog($employee, $logDateTime);
if (!empty($lastLog)) {
$lastLog = $lastLog[0];
/** @var EmployeeHourLog $lastLog */
if ($inOut === $lastLog->getInOut()->value) {
return new JsonResponse(
[
'message' => 'L\'employée est déja en état : ' . InOut::from($inOut)->title(),
'status' => false,
'historyHtml' => $employeeLogData[0]['historyHtml']
]
);
}
if ($logDateTime < $lastLog->getDate()) {
return new JsonResponse(
[
'message' => 'l\'enregistrement est inférieur au dernier enregistrement saisie : ' . $lastLog->getDate()->format('H:i'),
'status' => false,
'historyHtml' => $employeeLogData[0]['historyHtml']
]
);
}
}
$employeeLogHour = new EmployeeHourLog();
$employeeLogHour
->setEmployee($employee)
->setInOut(InOut::from($inOut))
->setDate($logDateTime);
$this->entityManager->persist($employeeLogHour);
$this->entityManager->persist($employee);
$this->entityManager->flush();
$this->entityManager->refresh($employee);
return new JsonResponse(
[
'message' => 'Enregistrement ajouté avec succés',
'status' => true,
'historyHtml' => $this->employeeService->htmlEmployeeHourLog($employee, $logDateTime)
]
);
}
/**
* @Route("/log_sheet", name="employee_log_sheet", methods={"GET","POST"})
* @IsGranted("EMPLOYEE_LOG_HOUR_PRINT", subject="", message="Permission requise")
*/
public function print_prepare(
Request $request
)
{
$date = $request->get('date');
$logDate = $date ?? (new \DateTime())->format('d/m/Y');
$employees = $this->employeeService->getActiveAndHourlyEmployeesWithLog(false, $logDate);
$pdfOptions = new Options();
$pdfOptions->setDefaultFont('Arial');
$pdfOptions->setIsRemoteEnabled(true);
$pdfOptions->setIsHtml5ParserEnabled(true);
$pdfOptions->setDefaultPaperSize('A4');
$dompdf = new Dompdf($pdfOptions);
$logDate = \DateTime::createFromFormat('d/m/Y', $logDate)->format('d-m-Y');
$html = $this->renderView(
'employee_log_hour/sheet_pdf.html.twig',
[
'title' => $fileName = sprintf(
'%s-%s.pdf',
'Liste du pointage : ',
$logDate
),
'employees' => $employees,
'logDate' => $logDate
]
);
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$this->injectPageCount($dompdf);
$dompdf->stream(
$fileName,
[
"Attachment" => false,
]
);
return new Response('', 200, [
'Content-Type' => 'application/pdf',
]);
}
private function injectPageCount(Dompdf $dompdf)
{
$canvas = $dompdf->getCanvas();
$pdf = $canvas->get_cpdf();
foreach ($pdf->objects as &$o) {
if ($o['t'] === 'contents') {
$o['c'] = str_replace('PG', $canvas->get_page_count(), $o['c']);
}
}
}
/**
* @Route("/absence_day", name="absence_day", methods={"GET"})
* @param Request $request
* @return Response
*/
public function absence_day(Request $request): Response
{
$date = $request->get('date');
$startDate = $endDate = $date ?? (new \DateTime())->format('d/m/Y');
$absenceList = $this->employeeService->absenceInDate($startDate, $endDate);
//group by position
$absenceListByDepartment = [];
foreach ($absenceList as $absentEmployee) {
/** @var Employee $absentEmployee */
$department = $absentEmployee->getLastHistory()->getDepartment();
if (empty($absenceListByDepartment[$department->getId()])) {
$absenceListByDepartment[$department->getId()] = [];
$absenceListByDepartment[$department->getId()]['department'] = $department;
}
$absenceListByDepartment[$department->getId()]['absenceList'][$absentEmployee->getId()] = $absentEmployee;
}
asort($absenceListByDepartment);
return $this->render(
'employee_log_hour/absence.html.twig',
[
'title' => 'Absence du : ' . $startDate,
'menu' => 'employee_log_hour_absence',
'absenceListByDepartment' => $absenceListByDepartment,
'count' => count($absenceList),
]
);
}
/**
* @Route("/presence_day", name="presence_day", methods={"GET"})
* @param Request $request
* @return Response
*/
public function presence_day(Request $request): Response
{
$date = $request->get('date');
$startDate = $endDate = $date ?? (new \DateTime())->format('d/m/Y');
$presenceList = $this->employeeService->presenceInDate($startDate, $endDate);
//group by position
$presenceListByDepartment = [];
foreach ($presenceList as $presentEmployee) {
/** @var Employee $presentEmployee */
$department = $presentEmployee->getLastHistory()->getDepartment();
if (empty($presenceListByDepartment[$department->getId()])) {
$presenceListByDepartment[$department->getId()] = [];
$presenceListByDepartment[$department->getId()]['department'] = $department;
}
$presenceListByDepartment[$department->getId()]['presenceList'][$presentEmployee->getId()] = $presentEmployee;
}
asort($presenceListByDepartment);
return $this->render(
'employee_log_hour/presence.html.twig',
[
'title' => 'Présence du : ' . $startDate,
'menu' => 'employee_log_hour_presence',
'presenceListByDepartment' => $presenceListByDepartment,
'count' => count($presenceList),
]
);
}
}