fix : wip

This commit is contained in:
2026-02-18 17:59:57 +01:00
parent 4256702add
commit c2e118dc33
47 changed files with 2689 additions and 345 deletions

View File

@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace App\ApiResource;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GetCollection;
use App\State\ScopedEmployeeProvider;
#[ApiResource(
operations: [
new GetCollection(
uriTemplate: '/employees/scoped',
normalizationContext: ['groups' => ['employee:read', 'site:read']],
security: "is_granted('ROLE_USER')",
provider: ScopedEmployeeProvider::class,
paginationEnabled: false
),
]
)]
final class ScopedEmployee {}

View File

@@ -30,7 +30,9 @@ final class WorkHourBulkUpsert
* afternoonFrom?:?string,
* afternoonTo?:?string,
* eveningFrom?:?string,
* eveningTo?:?string
* eveningTo?:?string,
* isPresentMorning?:bool,
* isPresentAfternoon?:bool
* }>
*/
public array $entries = [];

View File

@@ -0,0 +1,53 @@
<?php
declare(strict_types=1);
namespace App\ApiResource;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use App\State\WorkHourWeeklySummaryProvider;
#[ApiResource(
operations: [
new Get(
uriTemplate: '/work-hours/weekly-summary',
security: "is_granted('ROLE_USER')",
provider: WorkHourWeeklySummaryProvider::class
),
],
paginationEnabled: false
)]
final class WorkHourWeeklySummary
{
public string $weekStart = '';
public string $weekEnd = '';
/** @var list<string> */
public array $days = [];
/**
* @var list<array{
* employeeId:int,
* firstName:string,
* lastName:string,
* siteName:?string,
* contractName:?string,
* trackingMode:?string,
* daily:list<array{
* date:string,
* dayMinutes:int,
* nightMinutes:int,
* totalMinutes:int,
* present:?float
* }>,
* weeklyDayMinutes:int,
* weeklyNightMinutes:int,
* weeklyTotalMinutes:int,
* weeklyPresenceCount:float,
* weeklyOvertime25Minutes:int,
* weeklyOvertime50Minutes:int
* }>
*/
public array $rows = [];
}

View File

@@ -9,6 +9,7 @@ use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use App\Repository\AbsenceRepository;
use DateTimeInterface;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Attribute\Groups;
@@ -26,7 +27,7 @@ use Symfony\Component\Serializer\Attribute\Groups;
)]
#[ApiFilter(DateFilter::class, properties: ['startDate', 'endDate'])]
#[ApiFilter(SearchFilter::class, properties: ['employee.site' => 'exact'])]
#[ORM\Entity]
#[ORM\Entity(repositoryClass: AbsenceRepository::class)]
#[ORM\Table(name: 'absences')]
class Absence
{

103
src/Entity/Contract.php Normal file
View File

@@ -0,0 +1,103 @@
<?php
declare(strict_types=1);
namespace App\Entity;
use ApiPlatform\Metadata\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Attribute\Groups;
#[ApiResource(
normalizationContext: ['groups' => ['contract:read']],
denormalizationContext: ['groups' => ['contract:write']],
paginationEnabled: false,
security: "is_granted('ROLE_ADMIN')"
)]
#[ORM\Entity]
#[ORM\Table(name: 'contracts')]
class Contract
{
public const string TRACKING_TIME = 'TIME';
public const string TRACKING_PRESENCE = 'PRESENCE';
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
#[Groups(['contract:read', 'employee:read'])]
private ?int $id = null;
#[ORM\Column(type: 'string', length: 120)]
#[Groups(['contract:read', 'contract:write', 'employee:read'])]
private string $name = '';
#[ORM\Column(type: 'string', length: 20)]
#[Groups(['contract:read', 'contract:write', 'employee:read'])]
private string $trackingMode = self::TRACKING_TIME;
#[ORM\Column(type: 'integer', nullable: true)]
#[Groups(['contract:read', 'contract:write', 'employee:read'])]
private ?int $weeklyHours = null;
#[ORM\Column(type: 'boolean', options: ['default' => true])]
#[Groups(['contract:read', 'contract:write'])]
private bool $isActive = true;
public function getId(): ?int
{
return $this->id;
}
public function getName(): string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getTrackingMode(): string
{
return $this->trackingMode;
}
public function setTrackingMode(string $trackingMode): self
{
$this->trackingMode = $trackingMode;
return $this;
}
public function getWeeklyHours(): ?int
{
return $this->weeklyHours;
}
public function setWeeklyHours(?int $weeklyHours): self
{
$this->weeklyHours = $weeklyHours;
return $this;
}
public function isActive(): bool
{
return $this->isActive;
}
public function getIsActive(): bool
{
return $this->isActive;
}
public function setIsActive(bool $isActive): self
{
$this->isActive = $isActive;
return $this;
}
}

View File

@@ -4,7 +4,9 @@ declare(strict_types=1);
namespace App\Entity;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use App\Repository\EmployeeRepository;
use DateTimeImmutable;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Attribute\Groups;
@@ -15,7 +17,7 @@ use Symfony\Component\Serializer\Attribute\Groups;
paginationEnabled: false,
security: "is_granted('ROLE_ADMIN')"
)]
#[ORM\Entity]
#[ORM\Entity(repositoryClass: EmployeeRepository::class)]
#[ORM\Table(name: 'employees')]
class Employee
{
@@ -33,12 +35,18 @@ class Employee
#[Groups(['absence:read', 'employee:read', 'employee:write'])]
private string $lastName = '';
#[ApiPlatform\Metadata\ApiProperty(readableLink: true)]
#[ApiProperty(readableLink: true)]
#[ORM\ManyToOne(targetEntity: Site::class)]
#[ORM\JoinColumn(nullable: true)]
#[Groups(['employee:read', 'employee:write'])]
private ?Site $site = null;
#[ApiProperty(readableLink: true)]
#[ORM\ManyToOne(targetEntity: Contract::class)]
#[ORM\JoinColumn(nullable: false)]
#[Groups(['employee:read', 'employee:write'])]
private ?Contract $contract = null;
#[ORM\Column(type: 'integer', options: ['default' => 0])]
#[Groups(['employee:read', 'employee:write'])]
private int $displayOrder = 0;
@@ -92,6 +100,18 @@ class Employee
return $this;
}
public function getContract(): ?Contract
{
return $this->contract;
}
public function setContract(?Contract $contract): self
{
$this->contract = $contract;
return $this;
}
public function getCreatedAt(): DateTimeImmutable
{
return $this->createdAt;

View File

@@ -11,6 +11,8 @@ use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Patch;
use App\Repository\WorkHourRepository;
use DateTimeInterface;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Attribute\Groups;
@@ -26,11 +28,16 @@ use Symfony\Component\Serializer\Attribute\Groups;
normalizationContext: ['groups' => ['work_hour:read', 'employee:read', 'site:read']],
security: "is_granted('WORK_HOUR_VIEW', object)"
),
new Patch(
normalizationContext: ['groups' => ['work_hour:read', 'employee:read', 'site:read']],
denormalizationContext: ['groups' => ['work_hour:validate']],
security: "is_granted('ROLE_ADMIN')"
),
],
)]
#[ApiFilter(DateFilter::class, properties: ['workDate'])]
#[ApiFilter(SearchFilter::class, properties: ['employee' => 'exact', 'employee.site' => 'exact'])]
#[ORM\Entity]
#[ORM\Entity(repositoryClass: WorkHourRepository::class)]
#[ORM\Table(name: 'work_hours')]
#[ORM\UniqueConstraint(name: 'uniq_work_hours_employee_date', fields: ['employee', 'workDate'])]
class WorkHour
@@ -75,6 +82,18 @@ class WorkHour
#[Groups(['work_hour:read'])]
private ?string $eveningTo = null;
#[ORM\Column(type: 'boolean', options: ['default' => false])]
#[Groups(['work_hour:read'])]
private bool $isPresentMorning = false;
#[ORM\Column(type: 'boolean', options: ['default' => false])]
#[Groups(['work_hour:read'])]
private bool $isPresentAfternoon = false;
#[ORM\Column(type: 'boolean', options: ['default' => false])]
#[Groups(['work_hour:read', 'work_hour:validate'])]
private bool $isValid = false;
public function getId(): ?int
{
return $this->id;
@@ -175,4 +194,55 @@ class WorkHour
return $this;
}
public function isPresentMorning(): bool
{
return $this->isPresentMorning;
}
public function getIsPresentMorning(): bool
{
return $this->isPresentMorning;
}
public function setIsPresentMorning(bool $isPresentMorning): self
{
$this->isPresentMorning = $isPresentMorning;
return $this;
}
public function isPresentAfternoon(): bool
{
return $this->isPresentAfternoon;
}
public function getIsPresentAfternoon(): bool
{
return $this->isPresentAfternoon;
}
public function setIsPresentAfternoon(bool $isPresentAfternoon): self
{
$this->isPresentAfternoon = $isPresentAfternoon;
return $this;
}
public function isValid(): bool
{
return $this->isValid;
}
public function getIsValid(): bool
{
return $this->isValid;
}
public function setIsValid(bool $isValid): self
{
$this->isValid = $isValid;
return $this;
}
}

View File

@@ -0,0 +1,49 @@
<?php
declare(strict_types=1);
namespace App\Repository;
use App\Entity\Absence;
use App\Entity\Employee;
use DateTimeImmutable;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Absence>
*/
final class AbsenceRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Absence::class);
}
/**
* @param list<Employee> $employees
*
* @return list<Absence>
*/
public function findForPrint(DateTimeImmutable $from, DateTimeImmutable $to, array $employees): array
{
if ([] === $employees) {
return [];
}
$qb = $this->createQueryBuilder('a')
->leftJoin('a.employee', 'e')
->leftJoin('a.type', 't')
->addSelect('e', 't')
->andWhere('a.startDate <= :to')
->andWhere('a.endDate >= :from')
->andWhere('a.employee IN (:employees)')
->setParameter('from', $from)
->setParameter('to', $to)
->setParameter('employees', $employees)
;
/** @var list<Absence> $absences */
return $qb->getQuery()->getResult();
}
}

View File

@@ -0,0 +1,103 @@
<?php
declare(strict_types=1);
namespace App\Repository;
use App\Entity\Employee;
use App\Entity\User;
use App\Security\EmployeeScopeService;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Employee>
*/
final class EmployeeRepository extends ServiceEntityRepository
{
public function __construct(
ManagerRegistry $registry,
private readonly EmployeeScopeService $employeeScopeService,
) {
parent::__construct($registry, Employee::class);
}
/**
* @param list<int> $employeeIds
*
* @return array<int, Employee>
*/
public function findAccessibleByIds(array $employeeIds, User $user): array
{
if ([] === $employeeIds) {
return [];
}
$qb = $this->createQueryBuilder('e')
->andWhere('e.id IN (:ids)')
->setParameter('ids', $employeeIds)
;
$this->employeeScopeService->applyEmployeeScope($qb, 'e', 'employee_repo_scope', $user);
/** @var list<Employee> $employees */
$employees = $qb->getQuery()->getResult();
$byId = [];
foreach ($employees as $employee) {
$employeeId = $employee->getId();
if ($employeeId) {
$byId[$employeeId] = $employee;
}
}
return $byId;
}
/**
* @return list<Employee>
*/
public function findScoped(User $user): array
{
$qb = $this->createQueryBuilder('e')
->leftJoin('e.site', 's')
->addSelect('s')
->orderBy('s.name', 'ASC')
->addOrderBy('e.displayOrder', 'ASC')
->addOrderBy('e.lastName', 'ASC')
->addOrderBy('e.firstName', 'ASC')
;
$this->employeeScopeService->applyEmployeeScope($qb, 'e', 'employee_scoped_list', $user);
/** @var list<Employee> $employees */
return $qb->getQuery()->getResult();
}
/**
* @param list<int> $siteIds
*
* @return list<Employee>
*/
public function findForPrintBySiteIds(array $siteIds): array
{
$qb = $this->createQueryBuilder('e')
->leftJoin('e.site', 's')
->addSelect('s')
->orderBy('s.displayOrder', 'ASC')
->addOrderBy('s.name', 'ASC')
->addOrderBy('e.displayOrder', 'ASC')
->addOrderBy('e.lastName', 'ASC')
->addOrderBy('e.firstName', 'ASC')
;
if ([] !== $siteIds) {
$qb->andWhere('s.id IN (:siteIds)')
->setParameter('siteIds', $siteIds)
;
}
/** @var list<Employee> $employees */
return $qb->getQuery()->getResult();
}
}

View File

@@ -0,0 +1,82 @@
<?php
declare(strict_types=1);
namespace App\Repository;
use App\Entity\Employee;
use App\Entity\WorkHour;
use DateTimeImmutable;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<WorkHour>
*/
final class WorkHourRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, WorkHour::class);
}
/**
* @param list<Employee> $employees
*
* @return array<int, WorkHour>
*/
public function findByDateAndEmployeesIndexedByEmployeeId(DateTimeImmutable $workDate, array $employees): array
{
if ([] === $employees) {
return [];
}
$qb = $this->createQueryBuilder('w')
->leftJoin('w.employee', 'e')
->addSelect('e')
->andWhere('w.workDate = :workDate')
->andWhere('w.employee IN (:employees)')
->setParameter('workDate', $workDate)
->setParameter('employees', $employees)
;
/** @var list<WorkHour> $workHours */
$workHours = $qb->getQuery()->getResult();
$byEmployeeId = [];
foreach ($workHours as $workHour) {
$employeeId = $workHour->getEmployee()?->getId();
if ($employeeId) {
$byEmployeeId[$employeeId] = $workHour;
}
}
return $byEmployeeId;
}
/**
* @param list<Employee> $employees
*
* @return list<WorkHour>
*/
public function findByDateRangeAndEmployees(DateTimeImmutable $from, DateTimeImmutable $to, array $employees): array
{
if ([] === $employees) {
return [];
}
$qb = $this->createQueryBuilder('w')
->leftJoin('w.employee', 'e')
->addSelect('e')
->andWhere('w.workDate >= :from')
->andWhere('w.workDate <= :to')
->andWhere('w.employee IN (:employees)')
->setParameter('from', $from)
->setParameter('to', $to)
->setParameter('employees', $employees)
;
/** @var list<WorkHour> $workHours */
return $qb->getQuery()->getResult();
}
}

View File

@@ -6,12 +6,11 @@ namespace App\State;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\Entity\Absence;
use App\Entity\Employee;
use App\Repository\AbsenceRepository;
use App\Repository\EmployeeRepository;
use App\Service\PublicHolidayServiceInterface;
use DateInterval;
use DateTimeImmutable;
use Doctrine\ORM\EntityManagerInterface;
use Dompdf\Dompdf;
use Dompdf\Options;
use Symfony\Component\HttpFoundation\RequestStack;
@@ -27,7 +26,8 @@ class AbsencePrintProvider implements ProviderInterface
public function __construct(
private Environment $twig,
private readonly RequestStack $requestStack,
private EntityManagerInterface $entityManager,
private EmployeeRepository $employeeRepository,
private AbsenceRepository $absenceRepository,
private PublicHolidayServiceInterface $publicHolidayService,
) {}
@@ -109,50 +109,12 @@ class AbsencePrintProvider implements ProviderInterface
private function loadEmployees(array $siteIds): array
{
$qb = $this->entityManager
->getRepository(Employee::class)
->createQueryBuilder('e')
->leftJoin('e.site', 's')
->addSelect('s')
->orderBy('s.displayOrder', 'ASC')
->addOrderBy('s.name', 'ASC')
->addOrderBy('e.displayOrder', 'ASC')
->addOrderBy('e.lastName', 'ASC')
->addOrderBy('e.firstName', 'ASC')
;
if ([] !== $siteIds) {
$qb->andWhere('s.id IN (:siteIds)')
->setParameter('siteIds', $siteIds)
;
}
// @var list<Employee> $result
return $qb->getQuery()->getResult();
return $this->employeeRepository->findForPrintBySiteIds($siteIds);
}
private function loadAbsences(DateTimeImmutable $from, DateTimeImmutable $to, array $employees): array
{
if ([] === $employees) {
return [];
}
$qb = $this->entityManager
->getRepository(Absence::class)
->createQueryBuilder('a')
->leftJoin('a.employee', 'e')
->leftJoin('a.type', 't')
->addSelect('e', 't')
->andWhere('a.startDate <= :to')
->andWhere('a.endDate >= :from')
->andWhere('a.employee IN (:employees)')
->setParameter('from', $from)
->setParameter('to', $to)
->setParameter('employees', $employees)
;
// @var list<Absence> $result
return $qb->getQuery()->getResult();
return $this->absenceRepository->findForPrint($from, $to, $employees);
}
private function buildDays(DateTimeImmutable $from, DateTimeImmutable $to): array

View File

@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace App\State;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\Entity\User;
use App\Repository\EmployeeRepository;
use Symfony\Bundle\SecurityBundle\Security;
final readonly class ScopedEmployeeProvider implements ProviderInterface
{
public function __construct(
private Security $security,
private EmployeeRepository $employeeRepository,
) {}
public function provide(Operation $operation, array $uriVariables = [], array $context = []): array
{
$user = $this->security->getUser();
if (!$user instanceof User) {
return [];
}
return $this->employeeRepository->findScoped($user);
}
}

View File

@@ -8,10 +8,11 @@ use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProcessorInterface;
use App\ApiResource\WorkHourBulkUpsert;
use App\ApiResource\WorkHourBulkUpsertResult;
use App\Entity\Employee;
use App\Entity\Contract;
use App\Entity\User;
use App\Entity\WorkHour;
use App\Security\EmployeeScopeService;
use App\Repository\EmployeeRepository;
use App\Repository\WorkHourRepository;
use DateTimeImmutable;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\SecurityBundle\Security;
@@ -24,7 +25,8 @@ final readonly class WorkHourBulkUpsertProcessor implements ProcessorInterface
public function __construct(
private EntityManagerInterface $entityManager,
private Security $security,
private EmployeeScopeService $employeeScopeService,
private EmployeeRepository $employeeRepository,
private WorkHourRepository $workHourRepository,
) {}
public function process(
@@ -54,13 +56,15 @@ final readonly class WorkHourBulkUpsertProcessor implements ProcessorInterface
// Vérifie que tous les employés envoyés sont dans le scope de l'utilisateur courant.
$employeeIds = $this->extractEmployeeIds($data->entries);
$employeesById = $this->loadAccessibleEmployees($employeeIds, $user);
$employeesById = $this->employeeRepository->findAccessibleByIds($employeeIds, $user);
if (count($employeesById) !== count($employeeIds)) {
throw new AccessDeniedHttpException('At least one employee is unknown or outside your scope.');
}
$existingByEmployeeId = $this->loadExistingWorkHours($workDate, array_values($employeesById));
$existingByEmployeeId = $this->workHourRepository
->findByDateAndEmployeesIndexedByEmployeeId($workDate, array_values($employeesById))
;
$result = new WorkHourBulkUpsertResult();
@@ -71,8 +75,22 @@ final readonly class WorkHourBulkUpsertProcessor implements ProcessorInterface
throw new AccessDeniedHttpException(sprintf('Employee %d is outside your scope.', $employeeId));
}
$normalized = $this->normalizeEntry($entry, $employeeId);
$existing = $existingByEmployeeId[$employeeId] ?? null;
$isPresenceTracking = Contract::TRACKING_PRESENCE === $employee->getContract()?->getTrackingMode();
$normalized = $this->normalizeEntry($entry, $employeeId, $isPresenceTracking);
$existing = $existingByEmployeeId[$employeeId] ?? null;
if ($existing?->isValid()) {
if (!$this->isSameAsExisting($existing, $normalized)) {
throw new UnprocessableEntityHttpException(sprintf(
'Employee %d: validated work hour cannot be modified.',
$employeeId
));
}
++$result->processed;
continue;
}
if ($this->isEntryEmpty($normalized)) {
// Convention choisie: une ligne vide supprime l'enregistrement existant.
@@ -136,76 +154,6 @@ final readonly class WorkHourBulkUpsertProcessor implements ProcessorInterface
return array_values($ids);
}
/**
* @param list<int> $employeeIds
*
* @return array<int, Employee>
*/
private function loadAccessibleEmployees(array $employeeIds, User $user): array
{
if ([] === $employeeIds) {
return [];
}
$qb = $this->entityManager
->getRepository(Employee::class)
->createQueryBuilder('e')
->andWhere('e.id IN (:ids)')
->setParameter('ids', $employeeIds)
;
$this->employeeScopeService->applyEmployeeScope($qb, 'e', 'bulk_scope', $user);
/** @var list<Employee> $employees */
$employees = $qb->getQuery()->getResult();
$byId = [];
foreach ($employees as $employee) {
$employeeId = $employee->getId();
if ($employeeId) {
$byId[$employeeId] = $employee;
}
}
return $byId;
}
/**
* @param list<Employee> $employees
*
* @return array<int, WorkHour>
*/
private function loadExistingWorkHours(DateTimeImmutable $workDate, array $employees): array
{
if ([] === $employees) {
return [];
}
$qb = $this->entityManager
->getRepository(WorkHour::class)
->createQueryBuilder('w')
->leftJoin('w.employee', 'e')
->addSelect('e')
->andWhere('w.workDate = :workDate')
->andWhere('w.employee IN (:employees)')
->setParameter('workDate', $workDate)
->setParameter('employees', $employees)
;
/** @var list<WorkHour> $workHours */
$workHours = $qb->getQuery()->getResult();
$byEmployeeId = [];
foreach ($workHours as $workHour) {
$employeeId = $workHour->getEmployee()?->getId();
if ($employeeId) {
$byEmployeeId[$employeeId] = $workHour;
}
}
return $byEmployeeId;
}
/**
* @param array<string, mixed> $entry
*
@@ -215,23 +163,36 @@ final readonly class WorkHourBulkUpsertProcessor implements ProcessorInterface
* afternoonFrom:?string,
* afternoonTo:?string,
* eveningFrom:?string,
* eveningTo:?string
* eveningTo:?string,
* isPresentMorning:bool,
* isPresentAfternoon:bool
* }
*/
private function normalizeEntry(array $entry, int $employeeId): array
private function normalizeEntry(array $entry, int $employeeId, bool $isPresenceTracking): array
{
$normalized = [
'morningFrom' => $this->normalizeTime($entry['morningFrom'] ?? null, $employeeId, 'morningFrom'),
'morningTo' => $this->normalizeTime($entry['morningTo'] ?? null, $employeeId, 'morningTo'),
'afternoonFrom' => $this->normalizeTime($entry['afternoonFrom'] ?? null, $employeeId, 'afternoonFrom'),
'afternoonTo' => $this->normalizeTime($entry['afternoonTo'] ?? null, $employeeId, 'afternoonTo'),
'eveningFrom' => $this->normalizeTime($entry['eveningFrom'] ?? null, $employeeId, 'eveningFrom'),
'eveningTo' => $this->normalizeTime($entry['eveningTo'] ?? null, $employeeId, 'eveningTo'),
if ($isPresenceTracking) {
return [
'morningFrom' => null,
'morningTo' => null,
'afternoonFrom' => null,
'afternoonTo' => null,
'eveningFrom' => null,
'eveningTo' => null,
'isPresentMorning' => $this->normalizePresence($entry['isPresentMorning'] ?? false, $employeeId, 'isPresentMorning'),
'isPresentAfternoon' => $this->normalizePresence($entry['isPresentAfternoon'] ?? false, $employeeId, 'isPresentAfternoon'),
];
}
return [
'morningFrom' => $this->normalizeTime($entry['morningFrom'] ?? null, $employeeId, 'morningFrom'),
'morningTo' => $this->normalizeTime($entry['morningTo'] ?? null, $employeeId, 'morningTo'),
'afternoonFrom' => $this->normalizeTime($entry['afternoonFrom'] ?? null, $employeeId, 'afternoonFrom'),
'afternoonTo' => $this->normalizeTime($entry['afternoonTo'] ?? null, $employeeId, 'afternoonTo'),
'eveningFrom' => $this->normalizeTime($entry['eveningFrom'] ?? null, $employeeId, 'eveningFrom'),
'eveningTo' => $this->normalizeTime($entry['eveningTo'] ?? null, $employeeId, 'eveningTo'),
'isPresentMorning' => false,
'isPresentAfternoon' => false,
];
$this->validateRanges($normalized, $employeeId);
return $normalized;
}
private function normalizeTime(mixed $value, int $employeeId, string $field): ?string
@@ -260,77 +221,17 @@ final readonly class WorkHourBulkUpsertProcessor implements ProcessorInterface
return $time;
}
/**
* @param array{
* morningFrom:?string,
* morningTo:?string,
* afternoonFrom:?string,
* afternoonTo:?string,
* eveningFrom:?string,
* eveningTo:?string
* } $entry
*/
private function validateRanges(array $entry, int $employeeId): void
private function normalizePresence(mixed $value, int $employeeId, string $field): bool
{
$ranges = [
'morning' => [$entry['morningFrom'], $entry['morningTo']],
'afternoon' => [$entry['afternoonFrom'], $entry['afternoonTo']],
'evening' => [$entry['eveningFrom'], $entry['eveningTo']],
];
$normalizedRanges = [];
foreach ($ranges as $label => [$from, $to]) {
// On force des paires from/to complètes par créneau.
if ((null === $from) xor (null === $to)) {
throw new UnprocessableEntityHttpException(sprintf(
'Employee %d: %s range must contain both from and to.',
$employeeId,
$label
));
}
if (null === $from || null === $to) {
continue;
}
$fromMinutes = $this->toMinutes($from);
$toMinutes = $this->toMinutes($to);
if ($fromMinutes >= $toMinutes) {
throw new UnprocessableEntityHttpException(sprintf(
'Employee %d: %s from must be earlier than to.',
$employeeId,
$label
));
}
$normalizedRanges[] = [
'label' => $label,
'from' => $fromMinutes,
'to' => $toMinutes,
];
if (!is_bool($value)) {
throw new UnprocessableEntityHttpException(sprintf(
'Employee %d: %s must be a boolean.',
$employeeId,
$field
));
}
usort(
$normalizedRanges,
static fn (array $rangeA, array $rangeB): int => $rangeA['from'] <=> $rangeB['from']
);
$previous = null;
foreach ($normalizedRanges as $range) {
// Empêche deux créneaux qui se chevauchent sur une même journée.
if (null !== $previous && $range['from'] < $previous['to']) {
throw new UnprocessableEntityHttpException(sprintf(
'Employee %d: %s overlaps %s.',
$employeeId,
$range['label'],
$previous['label']
));
}
$previous = $range;
}
return $value;
}
/**
@@ -340,7 +241,9 @@ final readonly class WorkHourBulkUpsertProcessor implements ProcessorInterface
* afternoonFrom:?string,
* afternoonTo:?string,
* eveningFrom:?string,
* eveningTo:?string
* eveningTo:?string,
* isPresentMorning:bool,
* isPresentAfternoon:bool
* } $entry
*/
private function isEntryEmpty(array $entry): bool
@@ -350,7 +253,9 @@ final readonly class WorkHourBulkUpsertProcessor implements ProcessorInterface
&& null === $entry['afternoonFrom']
&& null === $entry['afternoonTo']
&& null === $entry['eveningFrom']
&& null === $entry['eveningTo'];
&& null === $entry['eveningTo']
&& false === $entry['isPresentMorning']
&& false === $entry['isPresentAfternoon'];
}
/**
@@ -360,7 +265,9 @@ final readonly class WorkHourBulkUpsertProcessor implements ProcessorInterface
* afternoonFrom:?string,
* afternoonTo:?string,
* eveningFrom:?string,
* eveningTo:?string
* eveningTo:?string,
* isPresentMorning:bool,
* isPresentAfternoon:bool
* } $entry
*/
private function hydrateWorkHour(WorkHour $workHour, array $entry): void
@@ -372,13 +279,34 @@ final readonly class WorkHourBulkUpsertProcessor implements ProcessorInterface
->setAfternoonTo($entry['afternoonTo'])
->setEveningFrom($entry['eveningFrom'])
->setEveningTo($entry['eveningTo'])
->setIsPresentMorning($entry['isPresentMorning'])
->setIsPresentAfternoon($entry['isPresentAfternoon'])
// Toute modification utilisateur repasse la ligne en attente de validation RH.
->setIsValid(false)
;
}
private function toMinutes(string $time): int
/**
* @param array{
* morningFrom:?string,
* morningTo:?string,
* afternoonFrom:?string,
* afternoonTo:?string,
* eveningFrom:?string,
* eveningTo:?string,
* isPresentMorning:bool,
* isPresentAfternoon:bool
* } $entry
*/
private function isSameAsExisting(WorkHour $workHour, array $entry): bool
{
[$hours, $minutes] = array_map('intval', explode(':', $time, 2));
return ($hours * 60) + $minutes;
return $workHour->getMorningFrom() === $entry['morningFrom']
&& $workHour->getMorningTo() === $entry['morningTo']
&& $workHour->getAfternoonFrom() === $entry['afternoonFrom']
&& $workHour->getAfternoonTo() === $entry['afternoonTo']
&& $workHour->getEveningFrom() === $entry['eveningFrom']
&& $workHour->getEveningTo() === $entry['eveningTo']
&& $workHour->getIsPresentMorning() === $entry['isPresentMorning']
&& $workHour->getIsPresentAfternoon() === $entry['isPresentAfternoon'];
}
}

View File

@@ -0,0 +1,293 @@
<?php
declare(strict_types=1);
namespace App\State;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\ApiResource\WorkHourWeeklySummary;
use App\Entity\Employee;
use App\Entity\User;
use App\Entity\WorkHour;
use App\Repository\EmployeeRepository;
use App\Repository\WorkHourRepository;
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 WorkHourWeeklySummaryProvider implements ProviderInterface
{
public function __construct(
private Security $security,
private RequestStack $requestStack,
private EmployeeRepository $employeeRepository,
private WorkHourRepository $workHourRepository,
) {}
public function provide(Operation $operation, array $uriVariables = [], array $context = []): WorkHourWeeklySummary
{
$user = $this->security->getUser();
if (!$user instanceof User) {
throw new AccessDeniedHttpException('Authentication required.');
}
$anchorDate = $this->resolveAnchorDate();
[$weekStart, $weekEnd, $days] = $this->resolveWeek($anchorDate);
$employees = $this->employeeRepository->findScoped($user);
$workHours = $this->workHourRepository->findByDateRangeAndEmployees($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);
return $summary;
}
private function resolveAnchorDate(): DateTimeImmutable
{
$query = $this->requestStack->getCurrentRequest()?->query;
$raw = (string) ($query?->get('weekStart') ?? '');
if ('' === $raw) {
return new DateTimeImmutable('today');
}
$date = DateTimeImmutable::createFromFormat('Y-m-d', $raw);
if (!$date || $date->format('Y-m-d') !== $raw) {
throw new UnprocessableEntityHttpException('weekStart must use Y-m-d format.');
}
return $date;
}
/**
* @return array{DateTimeImmutable, DateTimeImmutable, list<string>}
*/
private function resolveWeek(DateTimeImmutable $anchorDate): array
{
$dayOfWeek = (int) $anchorDate->format('N');
$weekStart = $anchorDate->modify(sprintf('-%d days', $dayOfWeek - 1));
$weekEnd = $weekStart->modify('+6 days');
$days = [];
for ($i = 0; $i < 7; ++$i) {
$days[] = $weekStart->modify(sprintf('+%d days', $i))->format('Y-m-d');
}
return [$weekStart, $weekEnd, $days];
}
/**
* @param list<Employee> $employees
* @param list<WorkHour> $workHours
* @param list<string> $days
*
* @return list<array{
* employeeId:int,
* firstName:string,
* lastName:string,
* siteName:?string,
* contractName:?string,
* trackingMode:?string,
* daily:list<array{date:string, dayMinutes:int, nightMinutes:int, totalMinutes:int, present:?float}>,
* weeklyDayMinutes:int,
* weeklyNightMinutes:int,
* weeklyTotalMinutes:int,
* weeklyPresenceCount:float,
* weeklyOvertime25Minutes:int,
* weeklyOvertime50Minutes:int
* }>
*/
private function buildRows(array $employees, array $workHours, array $days): array
{
$metricsByEmployeeDate = [];
foreach ($workHours as $workHour) {
$employeeId = $workHour->getEmployee()?->getId();
if (!$employeeId) {
continue;
}
$dateKey = $workHour->getWorkDate()->format('Y-m-d');
$metricsByEmployeeDate[$employeeId][$dateKey] = [
'metrics' => $this->computeMetrics($workHour),
'isPresentMorning' => $workHour->getIsPresentMorning(),
'isPresentAfternoon' => $workHour->getIsPresentAfternoon(),
];
}
$rows = [];
foreach ($employees as $employee) {
$employeeId = $employee->getId();
if (!$employeeId) {
continue;
}
$weeklyDayMinutes = 0;
$weeklyNightMinutes = 0;
$weeklyTotalMinutes = 0;
$weeklyPresenceCount = 0.0;
$daily = [];
$isPresenceTracking = 'PRESENCE' === $employee->getContract()?->getTrackingMode();
foreach ($days as $date) {
$entry = $metricsByEmployeeDate[$employeeId][$date] ?? null;
$metrics = $entry['metrics'] ?? [
'dayMinutes' => 0,
'nightMinutes' => 0,
'totalMinutes' => 0,
];
$present = null;
if ($isPresenceTracking) {
$morning = ($entry['isPresentMorning'] ?? false) ? 0.5 : 0.0;
$afternoon = ($entry['isPresentAfternoon'] ?? false) ? 0.5 : 0.0;
$present = $morning + $afternoon;
}
$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'],
'present' => $present,
];
}
$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),
];
}
return $rows;
}
/**
* @return array{dayMinutes:int, nightMinutes:int, totalMinutes:int}
*/
private function computeMetrics(WorkHour $workHour): array
{
$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 [
'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 computeOvertime25Minutes(int $weeklyTotalMinutes): int
{
return max(0, min($weeklyTotalMinutes, 43 * 60) - (35 * 60));
}
private function computeOvertime50Minutes(int $weeklyTotalMinutes): int
{
return max(0, $weeklyTotalMinutes - (43 * 60));
}
}