[#SIRH-12] Export des heures d'un employé sur une année (#7)
Some checks failed
Auto Tag Develop / tag (push) Has been cancelled
Some checks failed
Auto Tag Develop / tag (push) Has been cancelled
| Numéro du ticket | Titre du ticket | |------------------|-----------------| | | | ## Description de la PR ## Modification du .env ## Check list - [ ] Pas de régression - [ ] TU/TI/TF rédigée - [ ] TU/TI/TF OK - [ ] CHANGELOG modifié Reviewed-on: #7 Co-authored-by: tristan <tristan@yuno.malio.fr> Co-committed-by: tristan <tristan@yuno.malio.fr>
This commit was merged in pull request #7.
This commit is contained in:
420
src/State/EmployeeYearlyHoursPrintProvider.php
Normal file
420
src/State/EmployeeYearlyHoursPrintProvider.php
Normal file
@@ -0,0 +1,420 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\State;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProviderInterface;
|
||||
use App\Dto\WorkHours\WorkMetrics;
|
||||
use App\Entity\Employee;
|
||||
use App\Entity\WorkHour;
|
||||
use App\Enum\TrackingMode;
|
||||
use App\Repository\AbsenceRepository;
|
||||
use App\Repository\EmployeeRepository;
|
||||
use App\Repository\WorkHourRepository;
|
||||
use App\Service\Contracts\EmployeeContractResolver;
|
||||
use App\Service\WorkHours\AbsenceSegmentsResolver;
|
||||
use App\Service\WorkHours\WorkedHoursCreditPolicy;
|
||||
use DateInterval;
|
||||
use DateTimeImmutable;
|
||||
use Dompdf\Dompdf;
|
||||
use Dompdf\Options;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
||||
use Twig\Environment;
|
||||
|
||||
class EmployeeYearlyHoursPrintProvider implements ProviderInterface
|
||||
{
|
||||
public function __construct(
|
||||
private Environment $twig,
|
||||
private readonly RequestStack $requestStack,
|
||||
private EmployeeRepository $employeeRepository,
|
||||
private WorkHourRepository $workHourRepository,
|
||||
private AbsenceRepository $absenceRepository,
|
||||
private EmployeeContractResolver $contractResolver,
|
||||
private AbsenceSegmentsResolver $absenceSegmentsResolver,
|
||||
private WorkedHoursCreditPolicy $workedHoursCreditPolicy,
|
||||
) {}
|
||||
|
||||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): Response
|
||||
{
|
||||
$request = $this->requestStack->getCurrentRequest();
|
||||
if (!$request) {
|
||||
return new Response('Missing request.', Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
$employeeId = (int) $request->query->get('employeeId', '0');
|
||||
if ($employeeId <= 0) {
|
||||
throw new UnprocessableEntityHttpException('employeeId must be a positive integer.');
|
||||
}
|
||||
|
||||
$employee = $this->employeeRepository->find($employeeId);
|
||||
if (!$employee instanceof Employee) {
|
||||
throw new NotFoundHttpException('Employee not found.');
|
||||
}
|
||||
|
||||
$yearRaw = (string) $request->query->get('year');
|
||||
if (!preg_match('/^\d{4}$/', $yearRaw)) {
|
||||
throw new UnprocessableEntityHttpException('year must use YYYY format.');
|
||||
}
|
||||
$year = (int) $yearRaw;
|
||||
|
||||
$from = new DateTimeImmutable("{$year}-01-01");
|
||||
$to = new DateTimeImmutable("{$year}-12-31");
|
||||
$days = $this->buildDays($from, $to);
|
||||
|
||||
$workHours = $this->workHourRepository->findByDateRangeAndEmployees($from, $to, [$employee]);
|
||||
$absences = $this->absenceRepository->findForPrint($from, $to, [$employee]);
|
||||
$contractMap = $this->contractResolver->resolveForEmployeesAndDays([$employee], $days);
|
||||
$driverMap = $this->contractResolver->resolveIsDriverForEmployeesAndDays([$employee], $days);
|
||||
|
||||
$workHourMap = $this->buildWorkHourMap($workHours);
|
||||
$absenceData = $this->buildAbsenceData($absences, $days, $employee);
|
||||
|
||||
$segments = $this->buildSegments(
|
||||
$employee,
|
||||
$days,
|
||||
$contractMap[$employee->getId()] ?? [],
|
||||
$driverMap[$employee->getId()] ?? [],
|
||||
$workHourMap[$employee->getId()] ?? [],
|
||||
$absenceData,
|
||||
);
|
||||
|
||||
$employeeName = trim(($employee->getLastName() ?? '').' '.($employee->getFirstName() ?? ''));
|
||||
|
||||
$options = new Options();
|
||||
$options->set('isRemoteEnabled', true);
|
||||
|
||||
$dompdf = new Dompdf($options);
|
||||
$html = $this->twig->render('employee-yearly-hours/print.html.twig', [
|
||||
'employeeName' => $employeeName,
|
||||
'year' => $year,
|
||||
'segments' => $segments,
|
||||
]);
|
||||
|
||||
$dompdf->loadHtml($html);
|
||||
$dompdf->setPaper('A4', 'portrait');
|
||||
$dompdf->render();
|
||||
|
||||
$filename = sprintf(
|
||||
'%s_%s_%d.pdf',
|
||||
$this->sanitizeFilename($employee->getLastName() ?? ''),
|
||||
$this->sanitizeFilename($employee->getFirstName() ?? ''),
|
||||
$year,
|
||||
);
|
||||
|
||||
return new Response($dompdf->output(), Response::HTTP_OK, [
|
||||
'Content-Type' => 'application/pdf',
|
||||
'Content-Disposition' => 'inline; filename="'.$filename.'"',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
private function buildDays(DateTimeImmutable $from, DateTimeImmutable $to): array
|
||||
{
|
||||
$days = [];
|
||||
$current = $from;
|
||||
|
||||
while ($current <= $to) {
|
||||
$days[] = $current->format('Y-m-d');
|
||||
$current = $current->add(new DateInterval('P1D'));
|
||||
}
|
||||
|
||||
return $days;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array<string, WorkHour>>
|
||||
*/
|
||||
private function buildWorkHourMap(array $workHours): array
|
||||
{
|
||||
$map = [];
|
||||
foreach ($workHours as $wh) {
|
||||
$employeeId = $wh->getEmployee()?->getId();
|
||||
if (!$employeeId) {
|
||||
continue;
|
||||
}
|
||||
$date = $wh->getWorkDate()->format('Y-m-d');
|
||||
$map[$employeeId][$date] = $wh;
|
||||
}
|
||||
|
||||
return $map;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{credited: array<string, int>, labels: array<string, string>, absentMorning: array<string, bool>, absentAfternoon: array<string, bool>, hasDayAbsence: array<string, bool>}
|
||||
*/
|
||||
private function buildAbsenceData(array $absences, array $days, Employee $employee): array
|
||||
{
|
||||
$credited = [];
|
||||
$labels = [];
|
||||
$absentMorning = [];
|
||||
$absentAfternoon = [];
|
||||
$hasDayAbsence = [];
|
||||
|
||||
foreach ($absences as $absence) {
|
||||
$absEmployeeId = $absence->getEmployee()?->getId();
|
||||
if ($absEmployeeId !== $employee->getId()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$start = $absence->getStartDate()->format('Y-m-d');
|
||||
$end = $absence->getEndDate()->format('Y-m-d');
|
||||
|
||||
foreach ($days as $date) {
|
||||
if ($date < $start || $date > $end) {
|
||||
continue;
|
||||
}
|
||||
|
||||
[$isMorning, $isAfternoon] = $this->absenceSegmentsResolver->resolveForDate($absence, $date);
|
||||
if ($isMorning || $isAfternoon) {
|
||||
$hasDayAbsence[$date] = true;
|
||||
$absentMorning[$date] = ($absentMorning[$date] ?? false) || $isMorning;
|
||||
$absentAfternoon[$date] = ($absentAfternoon[$date] ?? false) || $isAfternoon;
|
||||
if (!isset($labels[$date])) {
|
||||
$labels[$date] = $absence->getType()?->getLabel() ?? '';
|
||||
}
|
||||
}
|
||||
|
||||
$credited[$date] = ($credited[$date] ?? 0)
|
||||
+ $this->workedHoursCreditPolicy->computeCreditedMinutes($absence, $date, $isMorning, $isAfternoon);
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'credited' => $credited,
|
||||
'labels' => $labels,
|
||||
'absentMorning' => $absentMorning,
|
||||
'absentAfternoon' => $absentAfternoon,
|
||||
'hasDayAbsence' => $hasDayAbsence,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<array{mode: string, contractName: ?string, rows: list<array>}>
|
||||
*/
|
||||
private function buildSegments(
|
||||
Employee $employee,
|
||||
array $days,
|
||||
array $contractsByDate,
|
||||
array $driverByDate,
|
||||
array $workHoursByDate,
|
||||
array $absenceData,
|
||||
): array {
|
||||
$segments = [];
|
||||
$currentMode = null;
|
||||
$currentRows = [];
|
||||
$currentName = null;
|
||||
|
||||
foreach ($days as $date) {
|
||||
$contract = $contractsByDate[$date] ?? null;
|
||||
$isDriver = $driverByDate[$date] ?? false;
|
||||
$wh = $workHoursByDate[$date] ?? null;
|
||||
$hasData = null !== $wh || ($absenceData['hasDayAbsence'][$date] ?? false);
|
||||
|
||||
if (!$hasData) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$trackingMode = $contract?->getTrackingMode() ?? TrackingMode::TIME->value;
|
||||
$mode = $this->resolveSegmentMode($trackingMode, $isDriver);
|
||||
$contractName = $contract?->getName();
|
||||
|
||||
if ($mode !== $currentMode) {
|
||||
if (null !== $currentMode && [] !== $currentRows) {
|
||||
$segments[] = [
|
||||
'mode' => $currentMode,
|
||||
'contractName' => $currentName,
|
||||
'rows' => $currentRows,
|
||||
];
|
||||
}
|
||||
$currentMode = $mode;
|
||||
$currentRows = [];
|
||||
$currentName = $contractName;
|
||||
}
|
||||
|
||||
$creditedMinutes = $absenceData['credited'][$date] ?? 0;
|
||||
$absenceLabel = $absenceData['labels'][$date] ?? null;
|
||||
|
||||
$row = [
|
||||
'date' => new DateTimeImmutable($date)->format('d/m/Y'),
|
||||
'absenceLabel' => $absenceLabel,
|
||||
];
|
||||
|
||||
if ('presence' === $mode) {
|
||||
$absentMorning = $absenceData['absentMorning'][$date] ?? false;
|
||||
$absentAfternoon = $absenceData['absentAfternoon'][$date] ?? false;
|
||||
$morning = (($wh?->getIsPresentMorning() ?? false) && !$absentMorning) ? 0.5 : 0.0;
|
||||
$afternoon = (($wh?->getIsPresentAfternoon() ?? false) && !$absentAfternoon) ? 0.5 : 0.0;
|
||||
$total = $morning + $afternoon;
|
||||
|
||||
$row['presentMorning'] = $morning > 0;
|
||||
$row['presentAfternoon'] = $afternoon > 0;
|
||||
$row['total'] = $total > 0 ? (string) $total : '';
|
||||
} elseif ('driver' === $mode) {
|
||||
$dayMin = $wh?->getDayHoursMinutes() ?? 0;
|
||||
$nightMin = $wh?->getNightHoursMinutes() ?? 0;
|
||||
$workshopMin = $wh?->getWorkshopHoursMinutes() ?? 0;
|
||||
$totalMin = $dayMin + $nightMin + $workshopMin + $creditedMinutes;
|
||||
|
||||
$row['dayHours'] = $this->formatMinutes($dayMin);
|
||||
$row['nightHours'] = $this->formatMinutes($nightMin);
|
||||
$row['workshopHours'] = $this->formatMinutes($workshopMin);
|
||||
$row['total'] = $this->formatMinutes($totalMin);
|
||||
} else {
|
||||
$metrics = null !== $wh ? $this->computeMetrics($wh) : new WorkMetrics();
|
||||
$metrics->addCreditedMinutes($creditedMinutes);
|
||||
|
||||
$row['morningFrom'] = $wh?->getMorningFrom() ?? '';
|
||||
$row['morningTo'] = $wh?->getMorningTo() ?? '';
|
||||
$row['afternoonFrom'] = $wh?->getAfternoonFrom() ?? '';
|
||||
$row['afternoonTo'] = $wh?->getAfternoonTo() ?? '';
|
||||
$row['eveningFrom'] = $wh?->getEveningFrom() ?? '';
|
||||
$row['eveningTo'] = $wh?->getEveningTo() ?? '';
|
||||
$row['total'] = $this->formatMinutes($metrics->totalMinutes);
|
||||
}
|
||||
|
||||
$currentRows[] = $row;
|
||||
}
|
||||
|
||||
if (null !== $currentMode && [] !== $currentRows) {
|
||||
$segments[] = [
|
||||
'mode' => $currentMode,
|
||||
'contractName' => $currentName,
|
||||
'rows' => $currentRows,
|
||||
];
|
||||
}
|
||||
|
||||
return $segments;
|
||||
}
|
||||
|
||||
private function resolveSegmentMode(string $trackingMode, bool $isDriver): string
|
||||
{
|
||||
if ($isDriver) {
|
||||
return 'driver';
|
||||
}
|
||||
|
||||
if (TrackingMode::PRESENCE->value === $trackingMode) {
|
||||
return 'presence';
|
||||
}
|
||||
|
||||
return 'time';
|
||||
}
|
||||
|
||||
private function computeMetrics(WorkHour $workHour): WorkMetrics
|
||||
{
|
||||
$ranges = [
|
||||
[$workHour->getMorningFrom(), $workHour->getMorningTo()],
|
||||
[$workHour->getAfternoonFrom(), $workHour->getAfternoonTo()],
|
||||
[$workHour->getEveningFrom(), $workHour->getEveningTo()],
|
||||
];
|
||||
|
||||
$totalMinutes = 0;
|
||||
$nightMinutes = 0;
|
||||
|
||||
foreach ($ranges as [$from, $to]) {
|
||||
$totalMinutes += $this->intervalMinutes($from, $to);
|
||||
$nightMinutes += $this->nightIntervalMinutes($from, $to);
|
||||
}
|
||||
|
||||
$dayMinutes = max(0, $totalMinutes - $nightMinutes);
|
||||
|
||||
return new WorkMetrics(
|
||||
dayMinutes: $dayMinutes,
|
||||
nightMinutes: $nightMinutes,
|
||||
totalMinutes: $totalMinutes,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return null|array{int, int}
|
||||
*/
|
||||
private function resolveInterval(?string $from, ?string $to): ?array
|
||||
{
|
||||
$fromMinutes = $this->toMinutes($from);
|
||||
$toMinutes = $this->toMinutes($to);
|
||||
if (null === $fromMinutes || null === $toMinutes) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$end = $toMinutes <= $fromMinutes ? $toMinutes + 1440 : $toMinutes;
|
||||
|
||||
return [$fromMinutes, $end];
|
||||
}
|
||||
|
||||
private function toMinutes(?string $time): ?int
|
||||
{
|
||||
if (null === $time || '' === $time) {
|
||||
return null;
|
||||
}
|
||||
|
||||
[$hours, $minutes] = array_map('intval', explode(':', $time));
|
||||
|
||||
return ($hours * 60) + $minutes;
|
||||
}
|
||||
|
||||
private function intervalMinutes(?string $from, ?string $to): int
|
||||
{
|
||||
$interval = $this->resolveInterval($from, $to);
|
||||
if (null === $interval) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
[$start, $end] = $interval;
|
||||
|
||||
return max(0, $end - $start);
|
||||
}
|
||||
|
||||
private function nightIntervalMinutes(?string $from, ?string $to): int
|
||||
{
|
||||
$interval = $this->resolveInterval($from, $to);
|
||||
if (null === $interval) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
[$start, $end] = $interval;
|
||||
$windows = [[0, 360], [1260, 1440]];
|
||||
$total = 0;
|
||||
|
||||
for ($dayOffset = 0; $dayOffset <= 1; ++$dayOffset) {
|
||||
$shift = $dayOffset * 1440;
|
||||
foreach ($windows as [$windowStart, $windowEnd]) {
|
||||
$total += $this->overlap($start, $end, $windowStart + $shift, $windowEnd + $shift);
|
||||
}
|
||||
}
|
||||
|
||||
return $total;
|
||||
}
|
||||
|
||||
private function overlap(int $startA, int $endA, int $startB, int $endB): int
|
||||
{
|
||||
$start = max($startA, $startB);
|
||||
$end = min($endA, $endB);
|
||||
|
||||
return max(0, $end - $start);
|
||||
}
|
||||
|
||||
private function formatMinutes(int $minutes): string
|
||||
{
|
||||
if (0 === $minutes) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$h = intdiv($minutes, 60);
|
||||
$m = $minutes % 60;
|
||||
|
||||
return 0 === $m ? "{$h}h" : "{$h}h{$m}m";
|
||||
}
|
||||
|
||||
private function sanitizeFilename(string $name): string
|
||||
{
|
||||
$name = str_replace(' ', '_', $name);
|
||||
|
||||
return preg_replace('/[^a-zA-Z0-9_\-]/', '', $name) ?? $name;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user