fix : wip

This commit is contained in:
2026-02-19 17:44:37 +01:00
parent c2e118dc33
commit 13274ff297
31 changed files with 1539 additions and 126 deletions

View File

@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace App\ApiResource;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use App\State\WorkHourDayContextProvider;
#[ApiResource(
operations: [
new Get(
uriTemplate: '/work-hours/day-context',
security: "is_granted('ROLE_USER')",
provider: WorkHourDayContextProvider::class
),
],
paginationEnabled: false
)]
final class WorkHourDayContext
{
public string $workDate = '';
/**
* @var list<array{
* employeeId:int,
* absenceLabel:?string,
* absenceHalf:?string,
* absentMorning:bool,
* absentAfternoon:bool,
* creditedMinutes:int,
* creditedPresenceUnits:float
* }>
*/
public array $rows = [];
}

View File

@@ -45,8 +45,10 @@ final class WorkHourWeeklySummary
* weeklyNightMinutes:int,
* weeklyTotalMinutes:int,
* weeklyPresenceCount:float,
* weeklyOvertimeTotalMinutes:int,
* weeklyOvertime25Minutes:int,
* weeklyOvertime50Minutes:int
* weeklyOvertime50Minutes:int,
* weeklyRecoveryMinutes:int
* }>
*/
public array $rows = [];

View File

@@ -0,0 +1,50 @@
<?php
declare(strict_types=1);
namespace App\Doctrine;
use ApiPlatform\Doctrine\Orm\Extension\QueryCollectionExtensionInterface;
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Metadata\Operation;
use App\Entity\Absence;
use App\Entity\User;
use App\Security\EmployeeScopeService;
use Doctrine\ORM\QueryBuilder;
use Symfony\Bundle\SecurityBundle\Security;
final readonly class AbsenceCollectionExtension implements QueryCollectionExtensionInterface
{
public function __construct(
private Security $security,
private EmployeeScopeService $employeeScopeService,
) {}
public function applyToCollection(
QueryBuilder $queryBuilder,
QueryNameGeneratorInterface $queryNameGenerator,
string $resourceClass,
?Operation $operation = null,
array $context = []
): void {
if (Absence::class !== $resourceClass) {
return;
}
$user = $this->security->getUser();
if (!$user instanceof User) {
$queryBuilder->andWhere('1 = 0');
return;
}
$rootAlias = $queryBuilder->getRootAliases()[0];
$employeeAlias = 'absence_employee_scope';
$queryBuilder->leftJoin(sprintf('%s.employee', $rootAlias), $employeeAlias)
->addSelect($employeeAlias)
;
$this->employeeScopeService->applyEmployeeScope($queryBuilder, $employeeAlias, 'absence_scope', $user);
}
}

View File

@@ -0,0 +1,84 @@
<?php
declare(strict_types=1);
namespace App\Dto\WorkHours;
final class DayContextRow
{
public function __construct(
public int $employeeId,
public ?string $absenceLabel = null,
public ?string $absenceHalf = null,
public bool $absentMorning = false,
public bool $absentAfternoon = false,
public int $creditedMinutes = 0,
public float $creditedPresenceUnits = 0.0,
) {}
public function addAbsence(
?string $label,
bool $morning,
bool $afternoon,
int $creditedMinutes,
float $creditedPresenceUnits
): void {
// Fusionne plusieurs absences du même jour sur la ligne salarié.
$this->absentMorning = $this->absentMorning || $morning;
$this->absentAfternoon = $this->absentAfternoon || $afternoon;
// Garde un libellé lisible: unique si possible, sinon "Absences multiples".
if (null === $this->absenceLabel) {
$this->absenceLabel = $label;
} elseif ($label !== $this->absenceLabel) {
$this->absenceLabel = 'Absences multiples';
}
// AM/PM seulement pour les demi-journées, null pour journée complète.
$this->absenceHalf = $this->resolveHalfLabel($this->absentMorning, $this->absentAfternoon);
// Cumule les minutes créditées par les absences "comptées comme travaillées".
$this->creditedMinutes += $creditedMinutes;
// Cumule les unités de présence créditées (0.5 par demi-journée).
$this->creditedPresenceUnits += $creditedPresenceUnits;
}
/**
* @return array{
* employeeId:int,
* absenceLabel:?string,
* absenceHalf:?string,
* absentMorning:bool,
* absentAfternoon:bool,
* creditedMinutes:int,
* creditedPresenceUnits:float
* }
*/
public function toArray(): array
{
return [
'employeeId' => $this->employeeId,
'absenceLabel' => $this->absenceLabel,
'absenceHalf' => $this->absenceHalf,
'absentMorning' => $this->absentMorning,
'absentAfternoon' => $this->absentAfternoon,
'creditedMinutes' => $this->creditedMinutes,
'creditedPresenceUnits' => $this->creditedPresenceUnits,
];
}
private function resolveHalfLabel(bool $morning, bool $afternoon): ?string
{
// Matin + après-midi => journée complète, pas de libellé AM/PM.
if ($morning && $afternoon) {
return null;
}
if ($morning) {
return 'AM';
}
if ($afternoon) {
return 'PM';
}
return null;
}
}

View File

@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace App\Dto\WorkHours;
final class WorkMetrics
{
public function __construct(
public int $dayMinutes = 0,
public int $nightMinutes = 0,
public int $totalMinutes = 0,
) {}
public function addCreditedMinutes(int $creditedMinutes): void
{
// Ignore les valeurs nulles ou négatives pour ne pas biaiser les totaux.
if ($creditedMinutes <= 0) {
return;
}
// Le crédit absence alimente les heures de jour et le total.
$this->dayMinutes += $creditedMinutes;
$this->totalMinutes += $creditedMinutes;
}
}

View File

@@ -9,12 +9,39 @@ use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use App\Enum\HalfDay;
use App\Repository\AbsenceRepository;
use App\State\AbsenceWriteProcessor;
use DateTimeInterface;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Attribute\Groups;
#[ApiResource(
operations: [
new GetCollection(
security: "is_granted('ROLE_USER')"
),
new Get(
security: "is_granted('ABSENCE_VIEW', object)"
),
new Post(
securityPostDenormalize: "is_granted('ABSENCE_EDIT', object)",
processor: AbsenceWriteProcessor::class
),
new Patch(
security: "is_granted('ABSENCE_EDIT', object)",
processor: AbsenceWriteProcessor::class
),
new Delete(
security: "is_granted('ABSENCE_EDIT', object)",
processor: AbsenceWriteProcessor::class
),
],
normalizationContext: [
'groups' => ['absence:read', 'employee:read', 'absence_type:read'],
'datetime_format' => 'Y-m-d',
@@ -23,7 +50,6 @@ use Symfony\Component\Serializer\Attribute\Groups;
'datetime_format' => 'Y-m-d',
],
paginationEnabled: false,
security: "is_granted('ROLE_ADMIN')",
)]
#[ApiFilter(DateFilter::class, properties: ['startDate', 'endDate'])]
#[ApiFilter(SearchFilter::class, properties: ['employee.site' => 'exact'])]
@@ -53,17 +79,17 @@ class Absence
#[Groups(['absence:read'])]
private DateTimeInterface $startDate;
#[ORM\Column(type: 'string', length: 2, options: ['default' => 'AM'])]
#[ORM\Column(type: 'string', length: 2, enumType: HalfDay::class, options: ['default' => 'AM'])]
#[Groups(['absence:read'])]
private string $startHalf = 'AM';
private HalfDay $startHalf = HalfDay::AM;
#[ORM\Column(type: 'date')]
#[Groups(['absence:read'])]
private DateTimeInterface $endDate;
#[ORM\Column(type: 'string', length: 2, options: ['default' => 'PM'])]
#[ORM\Column(type: 'string', length: 2, enumType: HalfDay::class, options: ['default' => 'PM'])]
#[Groups(['absence:read'])]
private string $endHalf = 'PM';
private HalfDay $endHalf = HalfDay::PM;
#[ORM\Column(type: 'text', nullable: true)]
#[Groups(['absence:read'])]
@@ -122,24 +148,24 @@ class Absence
return $this;
}
public function getStartHalf(): string
public function getStartHalf(): HalfDay
{
return $this->startHalf;
}
public function setStartHalf(string $startHalf): self
public function setStartHalf(HalfDay $startHalf): self
{
$this->startHalf = $startHalf;
return $this;
}
public function getEndHalf(): string
public function getEndHalf(): HalfDay
{
return $this->endHalf;
}
public function setEndHalf(string $endHalf): self
public function setEndHalf(HalfDay $endHalf): self
{
$this->endHalf = $endHalf;

View File

@@ -5,13 +5,34 @@ declare(strict_types=1);
namespace App\Entity;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Attribute\Groups;
#[ApiResource(
operations: [
new GetCollection(
security: "is_granted('ROLE_USER')"
),
new Get(
security: "is_granted('ROLE_USER')"
),
new Post(
security: "is_granted('ROLE_ADMIN')"
),
new Patch(
security: "is_granted('ROLE_ADMIN')"
),
new Delete(
security: "is_granted('ROLE_ADMIN')"
),
],
normalizationContext: ['groups' => ['absence_type:read']],
paginationEnabled: false,
security: "is_granted('ROLE_ADMIN')"
)]
#[ORM\Entity]
#[ORM\Table(name: 'absence_types')]
@@ -35,6 +56,10 @@ class AbsenceType
#[Groups(['absence:read', 'absence_type:read'])]
private string $color = '';
#[ORM\Column(type: 'boolean', options: ['default' => false])]
#[Groups(['absence:read', 'absence_type:read'])]
private bool $countAsWorkedHours = false;
public function getId(): ?int
{
return $this->id;
@@ -75,4 +100,21 @@ class AbsenceType
return $this;
}
public function isCountAsWorkedHours(): bool
{
return $this->countAsWorkedHours;
}
public function getCountAsWorkedHours(): bool
{
return $this->countAsWorkedHours;
}
public function setCountAsWorkedHours(bool $countAsWorkedHours): self
{
$this->countAsWorkedHours = $countAsWorkedHours;
return $this;
}
}

11
src/Enum/HalfDay.php Normal file
View File

@@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace App\Enum;
enum HalfDay: string
{
case AM = 'AM';
case PM = 'PM';
}

View File

@@ -43,7 +43,33 @@ final class AbsenceRepository extends ServiceEntityRepository
->setParameter('employees', $employees)
;
/** @var list<Absence> $absences */
// @var list<Absence> $absences
return $qb->getQuery()->getResult();
}
/**
* @param list<Employee> $employees
*
* @return list<Absence>
*/
public function findByDateAndEmployees(DateTimeImmutable $date, array $employees): array
{
if ([] === $employees) {
return [];
}
$qb = $this->createQueryBuilder('a')
->leftJoin('a.employee', 'e')
->leftJoin('a.type', 't')
->addSelect('e', 't')
->andWhere('a.startDate <= :date')
->andWhere('a.endDate >= :date')
->andWhere('a.employee IN (:employees)')
->setParameter('date', $date)
->setParameter('employees', $employees)
;
// @var list<Absence> $absences
return $qb->getQuery()->getResult();
}
}

View File

@@ -7,6 +7,7 @@ namespace App\Repository;
use App\Entity\Employee;
use App\Entity\WorkHour;
use DateTimeImmutable;
use DateTimeInterface;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
@@ -76,7 +77,27 @@ final class WorkHourRepository extends ServiceEntityRepository
->setParameter('employees', $employees)
;
/** @var list<WorkHour> $workHours */
// @var list<WorkHour> $workHours
return $qb->getQuery()->getResult();
}
public function hasValidatedInRange(Employee $employee, DateTimeInterface $from, DateTimeInterface $to): bool
{
$fromDate = DateTimeImmutable::createFromInterface($from);
$toDate = DateTimeImmutable::createFromInterface($to);
$qb = $this->createQueryBuilder('w')
->select('COUNT(w.id)')
->andWhere('w.employee = :employee')
->andWhere('w.workDate >= :from')
->andWhere('w.workDate <= :to')
->andWhere('w.isValid = :isValid')
->setParameter('employee', $employee)
->setParameter('from', $fromDate)
->setParameter('to', $toDate)
->setParameter('isValid', true)
;
return ((int) $qb->getQuery()->getSingleScalarResult()) > 0;
}
}

View File

@@ -0,0 +1,48 @@
<?php
declare(strict_types=1);
namespace App\Security\Voter;
use App\Entity\Absence;
use App\Entity\User;
use App\Security\EmployeeScopeService;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Vote;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
final class AbsenceVoter extends Voter
{
public const string VIEW = 'ABSENCE_VIEW';
public const string EDIT = 'ABSENCE_EDIT';
public function __construct(
private readonly Security $security,
private readonly EmployeeScopeService $employeeScopeService,
) {}
protected function supports(string $attribute, mixed $subject): bool
{
return in_array($attribute, [self::VIEW, self::EDIT], true) && $subject instanceof Absence;
}
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token, ?Vote $vote = null): bool
{
$user = $this->security->getUser();
if (!$user instanceof User) {
return false;
}
if (!$subject instanceof Absence) {
return false;
}
$employee = $subject->getEmployee();
if (null === $employee) {
return false;
}
return $this->employeeScopeService->canAccessEmployee($user, $employee);
}
}

View File

@@ -0,0 +1,50 @@
<?php
declare(strict_types=1);
namespace App\Service\WorkHours;
use App\Entity\Absence;
use App\Enum\HalfDay;
final class AbsenceSegmentsResolver
{
/**
* @return array{bool, bool}
*/
public function resolveForDate(Absence $absence, string $dateYmd): array
{
$startDate = $absence->getStartDate()->format('Y-m-d');
$endDate = $absence->getEndDate()->format('Y-m-d');
$startHalf = $absence->getStartHalf();
$endHalf = $absence->getEndHalf();
// Cas d'une absence sur une seule date: on déduit matin/après-midi depuis les bornes.
if ($startDate === $endDate) {
if (HalfDay::AM === $startHalf && HalfDay::AM === $endHalf) {
// Uniquement le matin absent.
return [true, false];
}
if (HalfDay::PM === $startHalf && HalfDay::PM === $endHalf) {
// Uniquement l'après-midi absent.
return [false, true];
}
// Sinon, on considère la journée complète absente.
return [true, true];
}
// Premier jour d'une absence multi-jours qui commence l'après-midi.
if ($dateYmd === $startDate && HalfDay::PM === $startHalf) {
return [false, true];
}
// Dernier jour d'une absence multi-jours qui se termine le matin.
if ($dateYmd === $endDate && HalfDay::AM === $endHalf) {
return [true, false];
}
// Les jours intermédiaires sont entièrement absents.
return [true, true];
}
}

View File

@@ -0,0 +1,91 @@
<?php
declare(strict_types=1);
namespace App\Service\WorkHours;
use App\Entity\Absence;
use App\Entity\Contract;
use DateMalformedStringException;
use DateTimeImmutable;
final class WorkedHoursCreditPolicy
{
/**
* @throws DateMalformedStringException
*/
public function computeCreditedMinutes(Absence $absence, string $dateYmd, bool $absentMorning, bool $absentAfternoon): int
{
$type = $absence->getType();
// Certaines absences ne doivent jamais générer d'heures créditées.
if (!$type?->getCountAsWorkedHours()) {
return 0;
}
$employee = $absence->getEmployee();
// Les contrats suivis en "présence" ne cumulent pas d'heures en minutes.
if (Contract::TRACKING_TIME !== $employee?->getContract()?->getTrackingMode()) {
return 0;
}
$weekday = (int) new DateTimeImmutable($dateYmd)->format('N');
// On applique la règle de crédit dépendante du contrat (35h / 39h / fallback).
$dayMinutes = $this->resolveContractDayMinutes($employee->getContract()?->getWeeklyHours(), $weekday);
if ($dayMinutes <= 0) {
return 0;
}
// Crédit en demi-journées: matin = 0.5, après-midi = 0.5.
$halfUnits = ($absentMorning ? 1 : 0) + ($absentAfternoon ? 1 : 0);
return (int) round(($dayMinutes / 2) * $halfUnits);
}
public function computeCreditedPresenceUnits(Absence $absence, bool $absentMorning, bool $absentAfternoon): float
{
$type = $absence->getType();
if (!$type?->getCountAsWorkedHours()) {
return 0.0;
}
$employee = $absence->getEmployee();
if (Contract::TRACKING_PRESENCE !== $employee?->getContract()?->getTrackingMode()) {
return 0.0;
}
$halfUnits = ($absentMorning ? 1 : 0) + ($absentAfternoon ? 1 : 0);
return $halfUnits * 0.5;
}
public function resolveContractDayMinutes(?int $weeklyHours, int $isoWeekDay): int
{
// Week-end non travaillé dans cette politique.
if ($isoWeekDay >= 6) {
return 0;
}
// Règle fixe: 35h => 7h/jour.
if (35 === $weeklyHours) {
return 7 * 60;
}
// Règle fixe: 39h => 8h lundi-jeudi, 7h le vendredi.
if (39 === $weeklyHours) {
return 5 === $isoWeekDay ? 7 * 60 : 8 * 60;
}
// Cas spécifique métier: contrat 4h/semaine réparti sur 2 jours => 2h/jour.
if (4 === $weeklyHours) {
return 2 * 60;
}
// Contrat non renseigné/invalide: aucun crédit.
if (null === $weeklyHours || $weeklyHours <= 0) {
return 0;
}
// Fallback générique: répartition homogène sur 5 jours ouvrés.
return (int) round(($weeklyHours * 60) / 5);
}
}

View File

@@ -6,6 +6,7 @@ namespace App\State;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\Enum\HalfDay;
use App\Repository\AbsenceRepository;
use App\Repository\EmployeeRepository;
use App\Service\PublicHolidayServiceInterface;
@@ -164,13 +165,13 @@ class AbsencePrintProvider implements ProviderInterface
if ($isSameDay) {
if ($startHalf === $endHalf) {
$halfLabel = $startHalf;
$halfLabel = $startHalf->value;
}
} else {
if ($isStartDay && 'PM' === $startHalf) {
if ($isStartDay && HalfDay::PM === $startHalf) {
$halfLabel = 'PM';
}
if ($isEndDay && 'AM' === $endHalf) {
if ($isEndDay && HalfDay::AM === $endHalf) {
$halfLabel = 'AM';
}
}

View File

@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
namespace App\State;
use ApiPlatform\Metadata\DeleteOperationInterface;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProcessorInterface;
use App\Entity\Absence;
use App\Repository\WorkHourRepository;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
final readonly class AbsenceWriteProcessor implements ProcessorInterface
{
public function __construct(
#[Autowire(service: 'api_platform.doctrine.orm.state.persist_processor')]
private ProcessorInterface $persistProcessor,
#[Autowire(service: 'api_platform.doctrine.orm.state.remove_processor')]
private ProcessorInterface $removeProcessor,
private WorkHourRepository $workHourRepository,
) {}
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): mixed
{
if (!$data instanceof Absence) {
return $data;
}
$employee = $data->getEmployee();
if (null === $employee) {
return $data;
}
if ($this->workHourRepository->hasValidatedInRange($employee, $data->getStartDate(), $data->getEndDate())) {
throw new ConflictHttpException('Impossible de modifier une absence sur une période validée.');
}
if ($operation instanceof DeleteOperationInterface) {
return $this->removeProcessor->process($data, $operation, $uriVariables, $context);
}
return $this->persistProcessor->process($data, $operation, $uriVariables, $context);
}
}

View File

@@ -0,0 +1,110 @@
<?php
declare(strict_types=1);
namespace App\State;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\ApiResource\WorkHourDayContext;
use App\Dto\WorkHours\DayContextRow;
use App\Entity\User;
use App\Repository\AbsenceRepository;
use App\Repository\EmployeeRepository;
use App\Service\WorkHours\AbsenceSegmentsResolver;
use App\Service\WorkHours\WorkedHoursCreditPolicy;
use DateTimeImmutable;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
final readonly class WorkHourDayContextProvider implements ProviderInterface
{
public function __construct(
private Security $security,
private RequestStack $requestStack,
private EmployeeRepository $employeeRepository,
private AbsenceRepository $absenceRepository,
private AbsenceSegmentsResolver $absenceSegmentsResolver,
private WorkedHoursCreditPolicy $workedHoursCreditPolicy,
) {}
public function provide(Operation $operation, array $uriVariables = [], array $context = []): WorkHourDayContext
{
$user = $this->security->getUser();
// Endpoint protégé: on exige un utilisateur authentifié.
if (!$user instanceof User) {
throw new AccessDeniedHttpException('Authentication required.');
}
$workDate = $this->resolveWorkDate();
$employees = $this->employeeRepository->findScoped($user);
$absences = $this->absenceRepository->findByDateAndEmployees($workDate, $employees);
$rowsByEmployeeId = [];
foreach ($employees as $employee) {
$employeeId = $employee->getId();
if (!$employeeId) {
continue;
}
// On initialise toutes les lignes, même sans absence ce jour-là.
$rowsByEmployeeId[$employeeId] = new DayContextRow(employeeId: $employeeId);
}
$dateKey = $workDate->format('Y-m-d');
foreach ($absences as $absence) {
$employeeId = $absence->getEmployee()?->getId();
// Ignore les absences orphelines ou hors scope utilisateur.
if (!$employeeId || !isset($rowsByEmployeeId[$employeeId])) {
continue;
}
[$absentMorning, $absentAfternoon] = $this->absenceSegmentsResolver->resolveForDate($absence, $dateKey);
// Pas de segment absent sur ce jour: rien à injecter dans la ligne.
if (!$absentMorning && !$absentAfternoon) {
continue;
}
// Calcule le crédit d'heures selon la politique métier (type d'absence + contrat).
$creditedMinutes = $this->workedHoursCreditPolicy->computeCreditedMinutes($absence, $dateKey, $absentMorning, $absentAfternoon);
$creditedPresenceUnits = $this->workedHoursCreditPolicy->computeCreditedPresenceUnits($absence, $absentMorning, $absentAfternoon);
$rowsByEmployeeId[$employeeId]->addAbsence(
label: $absence->getType()?->getLabel(),
morning: $absentMorning,
afternoon: $absentAfternoon,
creditedMinutes: $creditedMinutes,
creditedPresenceUnits: $creditedPresenceUnits
);
}
$response = new WorkHourDayContext();
$response->workDate = $dateKey;
$response->rows = array_map(
static fn (DayContextRow $row): array => $row->toArray(),
array_values($rowsByEmployeeId)
);
return $response;
}
private function resolveWorkDate(): DateTimeImmutable
{
$query = $this->requestStack->getCurrentRequest()?->query;
$raw = (string) ($query?->get('workDate') ?? '');
// Sans paramètre, on cible la date du jour.
if ('' === $raw) {
return new DateTimeImmutable('today');
}
$date = DateTimeImmutable::createFromFormat('Y-m-d', $raw);
// Validation stricte du format pour éviter les ambiguïtés de parsing.
if (!$date || $date->format('Y-m-d') !== $raw) {
throw new UnprocessableEntityHttpException('workDate must use Y-m-d format.');
}
return $date;
}
}

View File

@@ -7,11 +7,16 @@ namespace App\State;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\ApiResource\WorkHourWeeklySummary;
use App\Dto\WorkHours\WorkMetrics;
use App\Entity\Absence;
use App\Entity\Employee;
use App\Entity\User;
use App\Entity\WorkHour;
use App\Repository\AbsenceRepository;
use App\Repository\EmployeeRepository;
use App\Repository\WorkHourRepository;
use App\Service\WorkHours\AbsenceSegmentsResolver;
use App\Service\WorkHours\WorkedHoursCreditPolicy;
use DateTimeImmutable;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\RequestStack;
@@ -25,11 +30,15 @@ final readonly class WorkHourWeeklySummaryProvider implements ProviderInterface
private RequestStack $requestStack,
private EmployeeRepository $employeeRepository,
private WorkHourRepository $workHourRepository,
private AbsenceRepository $absenceRepository,
private AbsenceSegmentsResolver $absenceSegmentsResolver,
private WorkedHoursCreditPolicy $workedHoursCreditPolicy,
) {}
public function provide(Operation $operation, array $uriVariables = [], array $context = []): WorkHourWeeklySummary
{
$user = $this->security->getUser();
// Endpoint protégé: résumé hebdo réservé aux utilisateurs authentifiés.
if (!$user instanceof User) {
throw new AccessDeniedHttpException('Authentication required.');
}
@@ -39,12 +48,13 @@ final readonly class WorkHourWeeklySummaryProvider implements ProviderInterface
$employees = $this->employeeRepository->findScoped($user);
$workHours = $this->workHourRepository->findByDateRangeAndEmployees($weekStart, $weekEnd, $employees);
$absences = $this->absenceRepository->findForPrint($weekStart, $weekEnd, $employees);
$summary = new WorkHourWeeklySummary();
$summary->weekStart = $weekStart->format('Y-m-d');
$summary->weekEnd = $weekEnd->format('Y-m-d');
$summary->days = $days;
$summary->rows = $this->buildRows($employees, $workHours, $days);
$summary->rows = $this->buildRows($employees, $workHours, $absences, $days);
return $summary;
}
@@ -54,11 +64,13 @@ final readonly class WorkHourWeeklySummaryProvider implements ProviderInterface
$query = $this->requestStack->getCurrentRequest()?->query;
$raw = (string) ($query?->get('weekStart') ?? '');
// Sans paramètre, on ancre la semaine sur aujourd'hui.
if ('' === $raw) {
return new DateTimeImmutable('today');
}
$date = DateTimeImmutable::createFromFormat('Y-m-d', $raw);
// Validation stricte du format attendu.
if (!$date || $date->format('Y-m-d') !== $raw) {
throw new UnprocessableEntityHttpException('weekStart must use Y-m-d format.');
}
@@ -71,6 +83,7 @@ final readonly class WorkHourWeeklySummaryProvider implements ProviderInterface
*/
private function resolveWeek(DateTimeImmutable $anchorDate): array
{
// Convention ISO: semaine de lundi (1) à dimanche (7).
$dayOfWeek = (int) $anchorDate->format('N');
$weekStart = $anchorDate->modify(sprintf('-%d days', $dayOfWeek - 1));
$weekEnd = $weekStart->modify('+6 days');
@@ -86,6 +99,7 @@ final readonly class WorkHourWeeklySummaryProvider implements ProviderInterface
/**
* @param list<Employee> $employees
* @param list<WorkHour> $workHours
* @param list<Absence> $absences
* @param list<string> $days
*
* @return list<array{
@@ -100,11 +114,13 @@ final readonly class WorkHourWeeklySummaryProvider implements ProviderInterface
* weeklyNightMinutes:int,
* weeklyTotalMinutes:int,
* weeklyPresenceCount:float,
* weeklyOvertimeTotalMinutes:int,
* weeklyOvertime25Minutes:int,
* weeklyOvertime50Minutes:int
* weeklyOvertime50Minutes:int,
* weeklyRecoveryMinutes:int
* }>
*/
private function buildRows(array $employees, array $workHours, array $days): array
private function buildRows(array $employees, array $workHours, array $absences, array $days): array
{
$metricsByEmployeeDate = [];
foreach ($workHours as $workHour) {
@@ -113,6 +129,7 @@ final readonly class WorkHourWeeklySummaryProvider implements ProviderInterface
continue;
}
// Pré-calcul des métriques par salarié/date pour simplifier l'agrégation finale.
$dateKey = $workHour->getWorkDate()->format('Y-m-d');
$metricsByEmployeeDate[$employeeId][$dateKey] = [
'metrics' => $this->computeMetrics($workHour),
@@ -121,6 +138,30 @@ final readonly class WorkHourWeeklySummaryProvider implements ProviderInterface
];
}
$creditedByEmployeeDate = [];
$creditedPresenceByEmployeeDate = [];
foreach ($absences as $absence) {
$employeeId = $absence->getEmployee()?->getId();
if (!$employeeId) {
continue;
}
$start = $absence->getStartDate()->format('Y-m-d');
$end = $absence->getEndDate()->format('Y-m-d');
foreach ($days as $date) {
// On ne crédite que les dates couvertes par l'intervalle d'absence.
if ($date < $start || $date > $end) {
continue;
}
[$absentMorning, $absentAfternoon] = $this->absenceSegmentsResolver->resolveForDate($absence, $date);
$creditedByEmployeeDate[$employeeId][$date] = ($creditedByEmployeeDate[$employeeId][$date] ?? 0)
+ $this->workedHoursCreditPolicy->computeCreditedMinutes($absence, $date, $absentMorning, $absentAfternoon);
$creditedPresenceByEmployeeDate[$employeeId][$date] = ($creditedPresenceByEmployeeDate[$employeeId][$date] ?? 0.0)
+ $this->workedHoursCreditPolicy->computeCreditedPresenceUnits($absence, $absentMorning, $absentAfternoon);
}
}
$rows = [];
foreach ($employees as $employee) {
$employeeId = $employee->getId();
@@ -133,62 +174,76 @@ final readonly class WorkHourWeeklySummaryProvider implements ProviderInterface
$weeklyTotalMinutes = 0;
$weeklyPresenceCount = 0.0;
$daily = [];
$isPresenceTracking = 'PRESENCE' === $employee->getContract()?->getTrackingMode();
// Les contrats au suivi "présence" ne manipulent pas les heures, mais des demi-journées.
$isPresenceTracking = 'PRESENCE' === $employee->getContract()?->getTrackingMode();
foreach ($days as $date) {
$entry = $metricsByEmployeeDate[$employeeId][$date] ?? null;
$metrics = $entry['metrics'] ?? [
'dayMinutes' => 0,
'nightMinutes' => 0,
'totalMinutes' => 0,
];
$entry = $metricsByEmployeeDate[$employeeId][$date] ?? null;
$metrics = $entry['metrics'] ?? new WorkMetrics();
$creditedMinutes = $creditedByEmployeeDate[$employeeId][$date] ?? 0;
// Les absences "comptées comme travaillées" alimentent le total du jour.
$metrics->addCreditedMinutes($creditedMinutes);
$present = null;
if ($isPresenceTracking) {
$morning = ($entry['isPresentMorning'] ?? false) ? 0.5 : 0.0;
$afternoon = ($entry['isPresentAfternoon'] ?? false) ? 0.5 : 0.0;
$present = $morning + $afternoon;
$morning = ($entry['isPresentMorning'] ?? false) ? 0.5 : 0.0;
$afternoon = ($entry['isPresentAfternoon'] ?? false) ? 0.5 : 0.0;
$creditedPresence = $creditedPresenceByEmployeeDate[$employeeId][$date] ?? 0.0;
$present = min(1.0, $morning + $afternoon + $creditedPresence);
}
$weeklyDayMinutes += $metrics['dayMinutes'];
$weeklyNightMinutes += $metrics['nightMinutes'];
$weeklyTotalMinutes += $metrics['totalMinutes'];
$weeklyDayMinutes += $metrics->dayMinutes;
$weeklyNightMinutes += $metrics->nightMinutes;
$weeklyTotalMinutes += $metrics->totalMinutes;
if (null !== $present) {
$weeklyPresenceCount += $present;
}
$daily[] = [
'date' => $date,
'dayMinutes' => $metrics['dayMinutes'],
'nightMinutes' => $metrics['nightMinutes'],
'totalMinutes' => $metrics['totalMinutes'],
'dayMinutes' => $metrics->dayMinutes,
'nightMinutes' => $metrics->nightMinutes,
'totalMinutes' => $metrics->totalMinutes,
'present' => $present,
];
}
$contractWeeklyHours = $employee->getContract()?->getWeeklyHours();
$weeklyOvertimeTotalMinutes = $isPresenceTracking
? 0
: $this->computeOvertimeTotalMinutes($weeklyTotalMinutes, $contractWeeklyHours);
$weeklyOvertime25Minutes = $isPresenceTracking
? 0
: $this->computeOvertime25BonusMinutes($weeklyTotalMinutes, $contractWeeklyHours);
$weeklyOvertime50Minutes = $isPresenceTracking
? 0
: $this->computeOvertime50BonusMinutes($weeklyTotalMinutes);
$weeklyRecoveryMinutes = $isPresenceTracking
? 0
: $weeklyOvertimeTotalMinutes + $weeklyOvertime25Minutes + $weeklyOvertime50Minutes;
$rows[] = [
'employeeId' => $employeeId,
'firstName' => $employee->getFirstName(),
'lastName' => $employee->getLastName(),
'siteName' => $employee->getSite()?->getName(),
'contractName' => $employee->getContract()?->getName(),
'trackingMode' => $employee->getContract()?->getTrackingMode(),
'daily' => $daily,
'weeklyDayMinutes' => $weeklyDayMinutes,
'weeklyNightMinutes' => $weeklyNightMinutes,
'weeklyTotalMinutes' => $weeklyTotalMinutes,
'weeklyPresenceCount' => $weeklyPresenceCount,
'weeklyOvertime25Minutes' => $isPresenceTracking ? 0 : $this->computeOvertime25Minutes($weeklyTotalMinutes),
'weeklyOvertime50Minutes' => $isPresenceTracking ? 0 : $this->computeOvertime50Minutes($weeklyTotalMinutes),
'employeeId' => $employeeId,
'firstName' => $employee->getFirstName(),
'lastName' => $employee->getLastName(),
'siteName' => $employee->getSite()?->getName(),
'contractName' => $employee->getContract()?->getName(),
'trackingMode' => $employee->getContract()?->getTrackingMode(),
'daily' => $daily,
'weeklyDayMinutes' => $weeklyDayMinutes,
'weeklyNightMinutes' => $weeklyNightMinutes,
'weeklyTotalMinutes' => $weeklyTotalMinutes,
'weeklyPresenceCount' => $weeklyPresenceCount,
'weeklyOvertimeTotalMinutes' => $weeklyOvertimeTotalMinutes,
'weeklyOvertime25Minutes' => $weeklyOvertime25Minutes,
'weeklyOvertime50Minutes' => $weeklyOvertime50Minutes,
'weeklyRecoveryMinutes' => $weeklyRecoveryMinutes,
];
}
return $rows;
}
/**
* @return array{dayMinutes:int, nightMinutes:int, totalMinutes:int}
*/
private function computeMetrics(WorkHour $workHour): array
private function computeMetrics(WorkHour $workHour): WorkMetrics
{
$ranges = [
[$workHour->getMorningFrom(), $workHour->getMorningTo()],
@@ -206,11 +261,11 @@ final readonly class WorkHourWeeklySummaryProvider implements ProviderInterface
$dayMinutes = max(0, $totalMinutes - $nightMinutes);
return [
'dayMinutes' => $dayMinutes,
'nightMinutes' => $nightMinutes,
'totalMinutes' => $totalMinutes,
];
return new WorkMetrics(
dayMinutes: $dayMinutes,
nightMinutes: $nightMinutes,
totalMinutes: $totalMinutes,
);
}
/**
@@ -224,6 +279,7 @@ final readonly class WorkHourWeeklySummaryProvider implements ProviderInterface
return null;
}
// Si fin <= début, on considère un passage à minuit.
$end = $toMinutes <= $fromMinutes ? $toMinutes + 1440 : $toMinutes;
return [$fromMinutes, $end];
@@ -260,9 +316,11 @@ final readonly class WorkHourWeeklySummaryProvider implements ProviderInterface
}
[$start, $end] = $interval;
$windows = [[0, 360], [1260, 1440]];
$total = 0;
// Fenêtres de nuit: 00:00-06:00 et 21:00-24:00.
$windows = [[0, 360], [1260, 1440]];
$total = 0;
// On projette aussi sur J+1 pour couvrir les shifts qui traversent minuit.
for ($dayOffset = 0; $dayOffset <= 1; ++$dayOffset) {
$shift = $dayOffset * 1440;
foreach ($windows as [$windowStart, $windowEnd]) {
@@ -281,13 +339,34 @@ final readonly class WorkHourWeeklySummaryProvider implements ProviderInterface
return max(0, $end - $start);
}
private function computeOvertime25Minutes(int $weeklyTotalMinutes): int
private function computeOvertimeTotalMinutes(int $weeklyTotalMinutes, ?int $contractWeeklyHours): int
{
return max(0, min($weeklyTotalMinutes, 43 * 60) - (35 * 60));
if (null === $contractWeeklyHours || $contractWeeklyHours <= 0) {
return 0;
}
// Règle métier: tout contrat < 35h est traité comme un 35h pour la base supp.
$referenceHours = max(35, $contractWeeklyHours);
return max(0, $weeklyTotalMinutes - ($referenceHours * 60));
}
private function computeOvertime50Minutes(int $weeklyTotalMinutes): int
private function computeOvertime25BonusMinutes(int $weeklyTotalMinutes, ?int $contractWeeklyHours): int
{
return max(0, $weeklyTotalMinutes - (43 * 60));
// Règle métier:
// - contrats <= 35h: 25% entre 35h et 43h
// - contrats >= 39h: 25% entre 39h et 43h
$startHours = (null !== $contractWeeklyHours && $contractWeeklyHours >= 39) ? 39 : 35;
$trancheMinutes = max(0, min($weeklyTotalMinutes, 43 * 60) - ($startHours * 60));
return (int) round($trancheMinutes * 0.25);
}
private function computeOvertime50BonusMinutes(int $weeklyTotalMinutes): int
{
// Bonus 50% appliqué au-delà de 43h.
$trancheMinutes = max(0, $weeklyTotalMinutes - (43 * 60));
return (int) round($trancheMinutes * 0.5);
}
}