Gestion du changement de type de contrat + correction du calcule des RTT sur un contrat qui commence en milieu de semaine (#19)
Auto Tag Develop / tag (push) Has been cancelled

| Numéro du ticket | Titre du ticket |
|------------------|-----------------|
|                  |                 |

## Description de la PR

## Modification du .env

## Check list

- [x] Pas de régression
- [x] TU/TI/TF rédigée
- [x] TU/TI/TF OK
- [x] CHANGELOG modifié

Reviewed-on: #19
Co-authored-by: tristan <tristan@yuno.malio.fr>
Co-committed-by: tristan <tristan@yuno.malio.fr>
This commit was merged in pull request #19.
This commit is contained in:
2026-05-22 06:42:33 +00:00
committed by Autin
parent b541f9ded8
commit abdaf809f8
40 changed files with 5021 additions and 153 deletions
@@ -0,0 +1,110 @@
<?php
declare(strict_types=1);
namespace App\Service\Contracts;
use App\Dto\Contracts\ContractPhase;
use App\Entity\Employee;
use App\Entity\EmployeeContractPeriod;
use DateTimeImmutable;
use LogicException;
final readonly class EmployeeContractPhaseResolver
{
private ?DateTimeImmutable $dataStartDate;
public function __construct(string $dataStartDate = '')
{
$trimmed = trim($dataStartDate);
if ('' === $trimmed) {
$this->dataStartDate = null;
return;
}
$parsed = DateTimeImmutable::createFromFormat('!Y-m-d', $trimmed);
$this->dataStartDate = $parsed instanceof DateTimeImmutable ? $parsed : null;
}
/**
* @return list<ContractPhase>
*/
public function resolvePhases(Employee $employee): array
{
$periods = $employee->getContractPeriods()->toArray();
usort(
$periods,
static fn (EmployeeContractPeriod $a, EmployeeContractPeriod $b): int => $a->getStartDate() <=> $b->getStartDate()
);
$today = new DateTimeImmutable('today');
$phases = [];
$group = [];
$signature = null;
foreach ($periods as $period) {
$currentSignature = $this->signature($period);
if (null !== $signature && $currentSignature !== $signature) {
$phases[] = $this->buildPhase($group, $today);
$group = [];
}
$group[] = $period;
$signature = $currentSignature;
}
if ([] !== $group) {
$phases[] = $this->buildPhase($group, $today);
}
// Hide phases entirely before the application's data start date: no usable
// work-hour or absence data exists before that date, so exposing them would
// confuse HR (e.g. legacy contract periods predating the software launch).
if (null !== $this->dataStartDate) {
$dataStart = $this->dataStartDate;
$phases = array_values(array_filter(
$phases,
static fn (ContractPhase $phase): bool => null === $phase->endDate || $phase->endDate >= $dataStart,
));
}
// Most recent first.
return array_reverse($phases);
}
private function signature(EmployeeContractPeriod $period): string
{
$contract = $period->getContract();
$type = $contract?->getType()->value ?? '';
$hours = $contract?->getWeeklyHours() ?? -1;
$driver = $period->getIsDriver() ? '1' : '0';
return sprintf('%s|%d|%s', $type, $hours, $driver);
}
/**
* @param non-empty-list<EmployeeContractPeriod> $group
*/
private function buildPhase(array $group, DateTimeImmutable $today): ContractPhase
{
$first = $group[0];
$last = end($group);
$endDate = $last->getEndDate();
$isCurrent = null === $endDate || $endDate >= $today;
$contract = $first->getContract();
return new ContractPhase(
id: (int) $first->getId(),
contractType: $contract?->getType() ?? throw new LogicException('Phase requires a contract type'),
weeklyHours: $contract?->getWeeklyHours(),
isDriver: $first->getIsDriver(),
startDate: $first->getStartDate(),
endDate: $endDate,
periodIds: array_map(static fn (EmployeeContractPeriod $p): int => (int) $p->getId(), $group),
isCurrent: $isCurrent,
contractNature: $first->getContractNatureEnum(),
);
}
}
@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace App\Service\Exercise;
use DateTimeImmutable;
final readonly class ExerciseYearResolver
{
/**
* Convert a date to its leave/RTT exercise year.
*
* - Forfait: calendar year (Jan→Dec) — returns $date.Y.
* - Non-forfait: leave year (Juin N-1 → Mai N) — returns $date.Y+1 if month >= 6, else $date.Y.
*/
public function forDate(DateTimeImmutable $date, bool $isForfait = false): int
{
if ($isForfait) {
return (int) $date->format('Y');
}
return (int) $date->format('n') >= 6
? (int) $date->format('Y') + 1
: (int) $date->format('Y');
}
}
@@ -221,8 +221,9 @@ final readonly class RttRecoveryComputationService
continue;
}
$weekAnchorNature = $naturesByDate[$employeeId][$weekDays[0]] ?? ContractNature::CDI;
$weekAnchorContract = $employeeContractsByDate[$weekDays[0]] ?? null;
$weekAnchorDate = $this->resolveWeekAnchorDate($weekDays, $employeeContractsByDate);
$weekAnchorNature = $naturesByDate[$employeeId][$weekAnchorDate] ?? ContractNature::CDI;
$weekAnchorContract = $employeeContractsByDate[$weekAnchorDate] ?? null;
$isWeekPresenceTracking = TrackingMode::PRESENCE->value === $weekAnchorContract?->getTrackingMode();
$disableOvertimeBonuses = $this->hasDisabledOvertimeBonuses($weekAnchorContract, $weekAnchorNature);
$weekContractType = ContractType::resolve(
@@ -387,6 +388,27 @@ final readonly class RttRecoveryComputationService
return $total;
}
/**
* Date d'ancrage de la semaine pour résoudre le type/nature de contrat : premier jour
* de la semaine couvert par un contrat. Évite qu'une semaine d'embauche en milieu de
* semaine (premiers jours hors contrat) soit classée CUSTOM — ce qui désactiverait à
* tort les bonus 25 %/50 % d'un contrat 35h/39h. Fallback sur le 1er jour si aucun jour
* n'est contracté (semaine entièrement hors contrat → 0 de toute façon).
*
* @param list<string> $weekDays
* @param array<string, ?Contract> $contractsByDate
*/
private function resolveWeekAnchorDate(array $weekDays, array $contractsByDate): string
{
foreach ($weekDays as $date) {
if (null !== ($contractsByDate[$date] ?? null)) {
return $date;
}
}
return $weekDays[0];
}
/**
* @param list<string> $days
* @param array<string, ?Contract> $contractsByDate
@@ -413,10 +435,17 @@ final readonly class RttRecoveryComputationService
{
$total = 0;
foreach ($days as $date) {
$isoDay = (int) new DateTimeImmutable($date)->format('N');
$contract = $contractsByDate[$date] ?? null;
$hours = $contract?->getWeeklyHours();
$startHours = (null !== $hours && $hours >= 39) ? 39 : 35;
$isoDay = (int) new DateTimeImmutable($date)->format('N');
$contract = $contractsByDate[$date] ?? null;
$hours = $contract?->getWeeklyHours();
// Days without an active contract (pre-hire, post-termination, contract
// gaps) must NOT contribute to the weekly 25% overtime threshold —
// otherwise hiring mid-week artificially inflates the threshold and
// erases legitimate overtime.
if (null === $hours || $hours <= 0) {
continue;
}
$startHours = $hours >= 39 ? 39 : 35;
$total += $this->resolveDailyReferenceMinutes($startHours, $isoDay);
}