feat : ajout de la gestion Congé

This commit is contained in:
2026-03-05 14:09:50 +01:00
parent fc2b184c50
commit 20a651895f
55 changed files with 4171 additions and 144 deletions

View File

@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace App\ApiResource;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use App\State\EmployeeLeaveSummaryProvider;
#[ApiResource(
operations: [
new Get(
uriTemplate: '/employees/{id}/leave-summary',
security: "is_granted('ROLE_USER')",
provider: EmployeeLeaveSummaryProvider::class
),
],
paginationEnabled: false
)]
final class EmployeeLeaveSummary
{
public int $year = 0;
public bool $isSupported = false;
public string $ruleCode = '';
public float $acquiredDays = 0.0;
public float $remainingDays = 0.0;
public float $takenDays = 0.0;
public float $acquiredSaturdays = 0.0;
public float $remainingSaturdays = 0.0;
public float $takenSaturdays = 0.0;
public float $fractionedDays = 0.0;
public float $accruingDays = 0.0;
}

View File

@@ -0,0 +1,188 @@
<?php
declare(strict_types=1);
namespace App\Command;
use App\Entity\Employee;
use App\Entity\EmployeeLeaveBalance;
use App\Enum\ContractType;
use App\Enum\LeaveRuleCode;
use App\Repository\EmployeeLeaveBalanceRepository;
use App\Repository\EmployeeRepository;
use App\Service\Leave\LeaveBalanceComputationService;
use DateTimeImmutable;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
#[AsCommand(
name: 'app:leave:rollover',
description: 'Create yearly leave opening balances (idempotent).'
)]
final class LeaveRolloverCommand extends Command
{
public function __construct(
private readonly EmployeeRepository $employeeRepository,
private readonly EmployeeLeaveBalanceRepository $leaveBalanceRepository,
private readonly LeaveBalanceComputationService $leaveBalanceComputationService,
private readonly EntityManagerInterface $entityManager,
) {
parent::__construct();
}
protected function configure(): void
{
$this->addOption(
'force',
null,
InputOption::VALUE_NONE,
'Run rollover regardless of business date (manual recovery mode).'
);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$today = new DateTimeImmutable('today');
$force = (bool) $input->getOption('force');
if (!$force && !$this->isBusinessRolloverDate($today)) {
$io->success('No rollover today: business date is neither 01/01 nor 01/06.');
return Command::SUCCESS;
}
$created = 0;
$skipped = 0;
foreach ($this->employeeRepository->findAll() as $employee) {
if (!$employee instanceof Employee) {
continue;
}
$ruleCode = $this->resolveRuleCode($employee);
if (null === $ruleCode) {
++$skipped;
continue;
}
if (!$force && !$this->shouldProcessRuleToday($ruleCode, $today)) {
++$skipped;
continue;
}
$targetYear = $this->resolveTargetYear($ruleCode, $today);
$existing = $this->leaveBalanceRepository->findOneByEmployeeRuleAndYear($employee, $ruleCode, $targetYear);
if (null !== $existing) {
++$skipped;
continue;
}
[$carryDays, $carrySaturdays] = $this->resolveCarry($employee, $ruleCode, $targetYear);
$balance = new EmployeeLeaveBalance()
->setEmployee($employee)
->setRuleCode($ruleCode)
->setYear($targetYear)
->setOpeningDays($carryDays)
->setOpeningSaturdays($carrySaturdays)
->setAccruedDays(0.0)
->setAccruedSaturdays(0.0)
->setTakenDays(0.0)
->setTakenSaturdays(0.0)
->setClosingDays($carryDays)
->setClosingSaturdays($carrySaturdays)
->setIsLocked(false)
;
$this->entityManager->persist($balance);
++$created;
}
$this->entityManager->flush();
$io->success(sprintf(
'Leave rollover done: %d created, %d skipped.',
$created,
$skipped
));
return Command::SUCCESS;
}
private function resolveRuleCode(Employee $employee): ?LeaveRuleCode
{
$type = $employee->getContract()?->getType();
if (null === $type || ContractType::INTERIM === $type) {
return null;
}
if (ContractType::FORFAIT === $type) {
return LeaveRuleCode::FORFAIT_218;
}
return LeaveRuleCode::CDI_CDD_NON_FORFAIT;
}
private function resolveTargetYear(LeaveRuleCode $ruleCode, DateTimeImmutable $today): int
{
if (LeaveRuleCode::FORFAIT_218 === $ruleCode) {
return (int) $today->format('Y');
}
$year = (int) $today->format('Y');
$month = (int) $today->format('n');
return $month >= 6 ? $year + 1 : $year;
}
private function isBusinessRolloverDate(DateTimeImmutable $today): bool
{
return in_array($today->format('m-d'), ['01-01', '06-01'], true);
}
private function shouldProcessRuleToday(LeaveRuleCode $ruleCode, DateTimeImmutable $today): bool
{
$day = $today->format('m-d');
if (LeaveRuleCode::FORFAIT_218 === $ruleCode) {
return '01-01' === $day;
}
return '06-01' === $day;
}
/**
* @return array{float, float}
*/
private function resolveCarry(Employee $employee, LeaveRuleCode $ruleCode, int $targetYear): array
{
$previousYear = $targetYear - 1;
$previous = $this->leaveBalanceRepository->findOneByEmployeeRuleAndYear($employee, $ruleCode, $previousYear);
if (null !== $previous) {
$carryDays = $previous->getClosingDays();
$carrySaturdays = LeaveRuleCode::CDI_CDD_NON_FORFAIT === $ruleCode
? $previous->getClosingSaturdays()
: 0.0;
} else {
[$carryDays, $carrySaturdays] = $this->leaveBalanceComputationService
->computeDynamicClosingForYear($employee, $ruleCode, $previousYear)
;
}
[$from, $to] = $this->leaveBalanceComputationService->resolvePeriodBounds($ruleCode, $previousYear);
$hasSettlement = $this->leaveBalanceComputationService
->hasPaidLeaveSettledClosureBetween($employee, $from, $to)
;
if ($hasSettlement) {
return [0.0, 0.0];
}
return [$carryDays, $carrySaturdays];
}
}

View File

@@ -0,0 +1,97 @@
<?php
declare(strict_types=1);
namespace App\DataFixtures;
use App\Entity\Absence;
use App\Entity\AbsenceType;
use App\Entity\Employee;
use App\Enum\HalfDay;
use DateTime;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Persistence\ObjectManager;
final class AbsenceFixtures extends Fixture implements DependentFixtureInterface
{
public function load(ObjectManager $manager): void
{
$this->createAbsence(
$manager,
$this->getReference(FixtureReferences::EMPLOYEE_STANDARD, Employee::class),
$this->getReference(FixtureReferences::ABSENCE_TYPE_CONGE, AbsenceType::class),
'2026-03-03',
HalfDay::AM,
'2026-03-03',
HalfDay::PM,
'CP standard non forfait'
);
$this->createAbsence(
$manager,
$this->getReference(FixtureReferences::EMPLOYEE_4H, Employee::class),
$this->getReference(FixtureReferences::ABSENCE_TYPE_CONGE, AbsenceType::class),
'2026-03-04',
HalfDay::AM,
'2026-03-04',
HalfDay::PM,
'CP employe 4h'
);
$this->createAbsence(
$manager,
$this->getReference(FixtureReferences::EMPLOYEE_FORFAIT, Employee::class),
$this->getReference(FixtureReferences::ABSENCE_TYPE_AUTRE, AbsenceType::class),
'2026-03-05',
HalfDay::AM,
'2026-03-05',
HalfDay::AM,
'Absence forfait demi-journee'
);
$this->createAbsence(
$manager,
$this->getReference(FixtureReferences::EMPLOYEE_INTERIM, Employee::class),
$this->getReference(FixtureReferences::ABSENCE_TYPE_ABSENT, AbsenceType::class),
'2026-03-06',
HalfDay::AM,
'2026-03-06',
HalfDay::PM,
'Absence interim'
);
$manager->flush();
}
public function getDependencies(): array
{
return [
EmployeeFixtures::class,
AbsenceTypeFixtures::class,
];
}
private function createAbsence(
ObjectManager $manager,
Employee $employee,
AbsenceType $type,
string $startDate,
HalfDay $startHalf,
string $endDate,
HalfDay $endHalf,
string $comment
): void {
$absence = new Absence()
->setEmployee($employee)
->setType($type)
->setStartDate(new DateTime($startDate))
->setStartHalf($startHalf)
->setEndDate(new DateTime($endDate))
->setEndHalf($endHalf)
->setComment($comment)
;
$manager->persist($absence);
}
}

View File

@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
namespace App\DataFixtures;
use App\Entity\AbsenceType;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
final class AbsenceTypeFixtures extends Fixture
{
public function load(ObjectManager $manager): void
{
$definitions = [
[FixtureReferences::ABSENCE_TYPE_RTT, 'R', 'RTT', '#feba01', false],
[FixtureReferences::ABSENCE_TYPE_CONGE, 'C', 'CONGÉ', '#008000', true],
[FixtureReferences::ABSENCE_TYPE_MALADIE, 'M', 'MALADIE', '#ed07da', true],
[FixtureReferences::ABSENCE_TYPE_AT, 'AT', 'ACCIDENT DE TRAVAIL', '#e41111', true],
[FixtureReferences::ABSENCE_TYPE_FORMATION, 'F', 'FORMATION', '#6c72e0', true],
[FixtureReferences::ABSENCE_TYPE_EXCEPTIONNEL, 'EX', 'CONGE EXCEPTIONNEL', '#8834ae', true],
[FixtureReferences::ABSENCE_TYPE_ABSENT, 'ABS', 'ABSENT', '#823a21', false],
[FixtureReferences::ABSENCE_TYPE_AUTRE, 'AUT', 'AUTRE', '#f07d60', true],
];
foreach ($definitions as [$reference, $code, $label, $color, $countAsWorkedHours]) {
$absenceType = new AbsenceType()
->setCode($code)
->setLabel($label)
->setColor($color)
->setCountAsWorkedHours($countAsWorkedHours)
;
$manager->persist($absenceType);
$this->addReference($reference, $absenceType);
}
$manager->flush();
}
}

View File

@@ -0,0 +1,55 @@
<?php
declare(strict_types=1);
namespace App\DataFixtures;
use App\Entity\Contract;
use App\Enum\TrackingMode;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
final class ContractFixtures extends Fixture
{
public function load(ObjectManager $manager): void
{
$contract35 = new Contract()
->setName('35h')
->setTrackingMode(TrackingMode::TIME)
->setWeeklyHours(35)
->setIsActive(true)
;
$contract4h = new Contract()
->setName('4h')
->setTrackingMode(TrackingMode::TIME)
->setWeeklyHours(4)
->setIsActive(true)
;
$forfait = new Contract()
->setName('Forfait')
->setTrackingMode(TrackingMode::PRESENCE)
->setWeeklyHours(null)
->setIsActive(true)
;
$interim = new Contract()
->setName('Interim')
->setTrackingMode(TrackingMode::TIME)
->setWeeklyHours(35)
->setIsActive(true)
;
$manager->persist($contract35);
$manager->persist($contract4h);
$manager->persist($forfait);
$manager->persist($interim);
$manager->flush();
$this->addReference(FixtureReferences::CONTRACT_35, $contract35);
$this->addReference(FixtureReferences::CONTRACT_4H, $contract4h);
$this->addReference(FixtureReferences::CONTRACT_FORFAIT, $forfait);
$this->addReference(FixtureReferences::CONTRACT_INTERIM, $interim);
}
}

View File

@@ -0,0 +1,91 @@
<?php
declare(strict_types=1);
namespace App\DataFixtures;
use App\Entity\Contract;
use App\Entity\Employee;
use App\Entity\EmployeeContractPeriod;
use App\Enum\ContractNature;
use DateTimeImmutable;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Persistence\ObjectManager;
final class EmployeeContractPeriodFixtures extends Fixture implements DependentFixtureInterface
{
public function load(ObjectManager $manager): void
{
$this->createPeriod(
$manager,
$this->getReference(FixtureReferences::EMPLOYEE_STANDARD, Employee::class),
$this->getReference(FixtureReferences::CONTRACT_35, Contract::class),
'2025-01-01',
null,
ContractNature::CDI,
false
);
$this->createPeriod(
$manager,
$this->getReference(FixtureReferences::EMPLOYEE_4H, Employee::class),
$this->getReference(FixtureReferences::CONTRACT_4H, Contract::class),
'2026-01-01',
'2026-12-31',
ContractNature::CDD,
false
);
$this->createPeriod(
$manager,
$this->getReference(FixtureReferences::EMPLOYEE_FORFAIT, Employee::class),
$this->getReference(FixtureReferences::CONTRACT_FORFAIT, Contract::class),
'2024-01-01',
null,
ContractNature::CDI,
false
);
$this->createPeriod(
$manager,
$this->getReference(FixtureReferences::EMPLOYEE_INTERIM, Employee::class),
$this->getReference(FixtureReferences::CONTRACT_INTERIM, Contract::class),
'2026-02-01',
'2026-06-30',
ContractNature::INTERIM,
false
);
$manager->flush();
}
public function getDependencies(): array
{
return [
EmployeeFixtures::class,
ContractFixtures::class,
];
}
private function createPeriod(
ObjectManager $manager,
Employee $employee,
Contract $contract,
string $startDate,
?string $endDate,
ContractNature $nature,
bool $paidLeaveSettled
): void {
$period = new EmployeeContractPeriod()
->setEmployee($employee)
->setContract($contract)
->setStartDate(new DateTimeImmutable($startDate))
->setEndDate(null === $endDate ? null : new DateTimeImmutable($endDate))
->setContractNature($nature)
->setPaidLeaveSettled($paidLeaveSettled)
;
$manager->persist($period);
}
}

View File

@@ -0,0 +1,71 @@
<?php
declare(strict_types=1);
namespace App\DataFixtures;
use App\Entity\Contract;
use App\Entity\Employee;
use App\Entity\Site;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Persistence\ObjectManager;
final class EmployeeFixtures extends Fixture implements DependentFixtureInterface
{
public function load(ObjectManager $manager): void
{
$site = $this->getReference(FixtureReferences::SITE_MAIN, Site::class);
$employeeStandard = new Employee()
->setFirstName('Alice')
->setLastName('Martin')
->setSite($site)
->setContract($this->getReference(FixtureReferences::CONTRACT_35, Contract::class))
->setDisplayOrder(1)
;
$employee4h = new Employee()
->setFirstName('Bruno')
->setLastName('Petit')
->setSite($site)
->setContract($this->getReference(FixtureReferences::CONTRACT_4H, Contract::class))
->setDisplayOrder(2)
;
$employeeForfait = new Employee()
->setFirstName('Chloe')
->setLastName('Durand')
->setSite($site)
->setContract($this->getReference(FixtureReferences::CONTRACT_FORFAIT, Contract::class))
->setDisplayOrder(3)
;
$employeeInterim = new Employee()
->setFirstName('David')
->setLastName('Leroy')
->setSite($site)
->setContract($this->getReference(FixtureReferences::CONTRACT_INTERIM, Contract::class))
->setDisplayOrder(4)
;
$manager->persist($employeeStandard);
$manager->persist($employee4h);
$manager->persist($employeeForfait);
$manager->persist($employeeInterim);
$manager->flush();
$this->addReference(FixtureReferences::EMPLOYEE_STANDARD, $employeeStandard);
$this->addReference(FixtureReferences::EMPLOYEE_4H, $employee4h);
$this->addReference(FixtureReferences::EMPLOYEE_FORFAIT, $employeeForfait);
$this->addReference(FixtureReferences::EMPLOYEE_INTERIM, $employeeInterim);
}
public function getDependencies(): array
{
return [
SiteFixtures::class,
ContractFixtures::class,
];
}
}

View File

@@ -0,0 +1,80 @@
<?php
declare(strict_types=1);
namespace App\DataFixtures;
use App\Entity\Employee;
use App\Entity\EmployeeLeaveBalance;
use App\Enum\LeaveRuleCode;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Persistence\ObjectManager;
final class EmployeeLeaveBalanceFixtures extends Fixture implements DependentFixtureInterface
{
public function load(ObjectManager $manager): void
{
$this->createOpeningBalance(
$manager,
$this->getReference(FixtureReferences::EMPLOYEE_STANDARD, Employee::class),
LeaveRuleCode::CDI_CDD_NON_FORFAIT,
2026,
0.0,
0.0
);
$this->createOpeningBalance(
$manager,
$this->getReference(FixtureReferences::EMPLOYEE_4H, Employee::class),
LeaveRuleCode::CDI_CDD_NON_FORFAIT,
2026,
0.0,
0.0
);
$this->createOpeningBalance(
$manager,
$this->getReference(FixtureReferences::EMPLOYEE_FORFAIT, Employee::class),
LeaveRuleCode::FORFAIT_218,
2026,
0.0,
0.0
);
$manager->flush();
}
public function getDependencies(): array
{
return [
EmployeeFixtures::class,
];
}
private function createOpeningBalance(
ObjectManager $manager,
Employee $employee,
LeaveRuleCode $ruleCode,
int $year,
float $openingDays,
float $openingSaturdays
): void {
$balance = new EmployeeLeaveBalance()
->setEmployee($employee)
->setRuleCode($ruleCode)
->setYear($year)
->setOpeningDays($openingDays)
->setOpeningSaturdays($openingSaturdays)
->setAccruedDays(0.0)
->setAccruedSaturdays(0.0)
->setTakenDays(0.0)
->setTakenSaturdays(0.0)
->setClosingDays($openingDays)
->setClosingSaturdays($openingSaturdays)
->setIsLocked(false)
;
$manager->persist($balance);
}
}

View File

@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace App\DataFixtures;
final class FixtureReferences
{
public const SITE_MAIN = 'site.main';
public const CONTRACT_35 = 'contract.35h';
public const CONTRACT_4H = 'contract.4h';
public const CONTRACT_FORFAIT = 'contract.forfait';
public const CONTRACT_INTERIM = 'contract.interim';
public const ABSENCE_TYPE_RTT = 'absence_type.rtt';
public const ABSENCE_TYPE_CONGE = 'absence_type.conge';
public const ABSENCE_TYPE_MALADIE = 'absence_type.maladie';
public const ABSENCE_TYPE_AT = 'absence_type.at';
public const ABSENCE_TYPE_FORMATION = 'absence_type.formation';
public const ABSENCE_TYPE_EXCEPTIONNEL = 'absence_type.exceptionnel';
public const ABSENCE_TYPE_ABSENT = 'absence_type.absent';
public const ABSENCE_TYPE_AUTRE = 'absence_type.autre';
public const EMPLOYEE_STANDARD = 'employee.standard_non_forfait';
public const EMPLOYEE_4H = 'employee.four_hours';
public const EMPLOYEE_FORFAIT = 'employee.forfait';
public const EMPLOYEE_INTERIM = 'employee.interim';
public const USER_ADMIN_EMILIE = 'user.admin.emilie';
}

View File

@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace App\DataFixtures;
use App\Entity\Site;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
final class SiteFixtures extends Fixture
{
public function load(ObjectManager $manager): void
{
$site = new Site()
->setName('SITE TEST')
->setColor('#75aed9')
->setDisplayOrder(1)
;
$manager->persist($site);
$manager->flush();
$this->addReference(FixtureReferences::SITE_MAIN, $site);
}
}

View File

@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace App\DataFixtures;
use App\Entity\User;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
final class UserFixtures extends Fixture
{
public function load(ObjectManager $manager): void
{
$emilie = new User()
->setUsername('emilie')
->setRoles(['ROLE_ADMIN'])
->setPassword('$2y$13$swd6WU4z7Z4MeZwl9hHaFez8xB/GMZxyrCDQUlFEY55JQUzTW0bPO')
;
$manager->persist($emilie);
$manager->flush();
$this->addReference(FixtureReferences::USER_ADMIN_EMILIE, $emilie);
}
}

View File

@@ -52,7 +52,7 @@ use Symfony\Component\Serializer\Attribute\Groups;
paginationEnabled: false,
)]
#[ApiFilter(DateFilter::class, properties: ['startDate', 'endDate'])]
#[ApiFilter(SearchFilter::class, properties: ['employee.site' => 'exact'])]
#[ApiFilter(SearchFilter::class, properties: ['employee' => 'exact', 'employee.site' => 'exact'])]
#[ORM\Entity(repositoryClass: AbsenceRepository::class)]
#[ORM\Table(name: 'absences')]
class Absence

View File

@@ -75,6 +75,9 @@ class Employee
#[Groups(['employee:write'])]
private ?string $contractEndDate = null;
#[Groups(['employee:write'])]
private ?bool $contractPaidLeaveSettled = null;
public function __construct()
{
$this->createdAt = new DateTimeImmutable();
@@ -187,6 +190,18 @@ class Employee
return $this;
}
public function getContractPaidLeaveSettled(): ?bool
{
return $this->contractPaidLeaveSettled;
}
public function setContractPaidLeaveSettled(?bool $contractPaidLeaveSettled): self
{
$this->contractPaidLeaveSettled = $contractPaidLeaveSettled;
return $this;
}
#[Groups(['employee:read'])]
public function getCurrentContractNature(): string
{

View File

@@ -37,6 +37,9 @@ class EmployeeContractPeriod
#[ORM\Column(type: 'string', length: 20, options: ['default' => ContractNature::CDI->value])]
private string $contractNature = ContractNature::CDI->value;
#[ORM\Column(type: 'boolean', options: ['default' => false])]
private bool $paidLeaveSettled = false;
#[ORM\Column(type: 'datetime_immutable')]
private DateTimeImmutable $createdAt;
@@ -121,4 +124,16 @@ class EmployeeContractPeriod
{
return $this->createdAt;
}
public function isPaidLeaveSettled(): bool
{
return $this->paidLeaveSettled;
}
public function setPaidLeaveSettled(bool $paidLeaveSettled): self
{
$this->paidLeaveSettled = $paidLeaveSettled;
return $this;
}
}

View File

@@ -0,0 +1,238 @@
<?php
declare(strict_types=1);
namespace App\Entity;
use App\Enum\LeaveRuleCode;
use App\Repository\EmployeeLeaveBalanceRepository;
use DateTimeImmutable;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: EmployeeLeaveBalanceRepository::class)]
#[ORM\Table(name: 'employee_leave_balances', options: ['comment' => 'Soldes de conges par employe et exercice (ouverture, mouvements, cloture).'])]
#[ORM\UniqueConstraint(name: 'uniq_employee_leave_balance', columns: ['employee_id', 'rule_code', 'year'])]
#[ORM\Index(columns: ['employee_id', 'year'], name: 'idx_leave_balance_employee_year')]
class EmployeeLeaveBalance
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private ?int $id = null;
#[ORM\ManyToOne(targetEntity: Employee::class)]
#[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
private ?Employee $employee = null;
#[ORM\Column(length: 64, options: ['comment' => 'Code de regle de calcul des conges (CDI_CDD_NON_FORFAIT, FORFAIT_218, ...).'])]
private string $ruleCode = '';
#[ORM\Column(type: 'integer', options: ['comment' => 'Annee d exercice de reference (ex: 2026).'])]
private int $year = 0;
#[ORM\Column(type: 'float', options: ['comment' => 'Report N-1 en jours a l ouverture de l exercice.'])]
private float $openingDays = 0.0;
#[ORM\Column(type: 'float', options: ['comment' => 'Report N-1 en samedis a l ouverture (0 pour forfait).'])]
private float $openingSaturdays = 0.0;
#[ORM\Column(type: 'float', options: ['comment' => 'Droits jours acquis sur l exercice courant.'])]
private float $accruedDays = 0.0;
#[ORM\Column(type: 'float', options: ['comment' => 'Droits samedis acquis sur l exercice courant.'])]
private float $accruedSaturdays = 0.0;
#[ORM\Column(type: 'float', options: ['comment' => 'Jours de conges consommes sur l exercice.'])]
private float $takenDays = 0.0;
#[ORM\Column(type: 'float', options: ['comment' => 'Samedis consommes sur l exercice.'])]
private float $takenSaturdays = 0.0;
#[ORM\Column(type: 'float', options: ['comment' => 'Solde de cloture jours sur l exercice.'])]
private float $closingDays = 0.0;
#[ORM\Column(type: 'float', options: ['comment' => 'Solde de cloture samedis sur l exercice.'])]
private float $closingSaturdays = 0.0;
#[ORM\Column(type: 'boolean', options: ['default' => false, 'comment' => 'Indique si le solde de l exercice est fige (verrouille RH).'])]
private bool $isLocked = false;
#[ORM\Column(type: 'datetime_immutable')]
private DateTimeImmutable $createdAt;
#[ORM\Column(type: 'datetime_immutable')]
private DateTimeImmutable $updatedAt;
public function __construct()
{
$now = new DateTimeImmutable();
$this->createdAt = $now;
$this->updatedAt = $now;
}
public function getId(): ?int
{
return $this->id;
}
public function getEmployee(): ?Employee
{
return $this->employee;
}
public function setEmployee(Employee $employee): self
{
$this->employee = $employee;
return $this;
}
public function getRuleCode(): string
{
return $this->ruleCode;
}
public function setRuleCode(LeaveRuleCode|string $ruleCode): self
{
$this->ruleCode = $ruleCode instanceof LeaveRuleCode ? $ruleCode->value : $ruleCode;
return $this;
}
public function getYear(): int
{
return $this->year;
}
public function setYear(int $year): self
{
$this->year = $year;
return $this;
}
public function getOpeningDays(): float
{
return $this->openingDays;
}
public function setOpeningDays(float $openingDays): self
{
$this->openingDays = $openingDays;
return $this;
}
public function getOpeningSaturdays(): float
{
return $this->openingSaturdays;
}
public function setOpeningSaturdays(float $openingSaturdays): self
{
$this->openingSaturdays = $openingSaturdays;
return $this;
}
public function getAccruedDays(): float
{
return $this->accruedDays;
}
public function setAccruedDays(float $accruedDays): self
{
$this->accruedDays = $accruedDays;
return $this;
}
public function getAccruedSaturdays(): float
{
return $this->accruedSaturdays;
}
public function setAccruedSaturdays(float $accruedSaturdays): self
{
$this->accruedSaturdays = $accruedSaturdays;
return $this;
}
public function getTakenDays(): float
{
return $this->takenDays;
}
public function setTakenDays(float $takenDays): self
{
$this->takenDays = $takenDays;
return $this;
}
public function getTakenSaturdays(): float
{
return $this->takenSaturdays;
}
public function setTakenSaturdays(float $takenSaturdays): self
{
$this->takenSaturdays = $takenSaturdays;
return $this;
}
public function getClosingDays(): float
{
return $this->closingDays;
}
public function setClosingDays(float $closingDays): self
{
$this->closingDays = $closingDays;
return $this;
}
public function getClosingSaturdays(): float
{
return $this->closingSaturdays;
}
public function setClosingSaturdays(float $closingSaturdays): self
{
$this->closingSaturdays = $closingSaturdays;
return $this;
}
public function isLocked(): bool
{
return $this->isLocked;
}
public function setIsLocked(bool $isLocked): self
{
$this->isLocked = $isLocked;
return $this;
}
public function getCreatedAt(): DateTimeImmutable
{
return $this->createdAt;
}
public function getUpdatedAt(): DateTimeImmutable
{
return $this->updatedAt;
}
public function touch(): self
{
$this->updatedAt = new DateTimeImmutable();
return $this;
}
}

View File

@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace App\Enum;
enum LeaveRuleCode: string
{
case UNSUPPORTED = 'UNSUPPORTED';
case CDI_CDD_NON_FORFAIT = 'CDI_CDD_NON_FORFAIT';
case FORFAIT_218 = 'FORFAIT_218';
}

View File

@@ -99,4 +99,29 @@ final class AbsenceRepository extends ServiceEntityRepository implements Absence
// @var list<Absence> $absences
return $qb->getQuery()->getResult();
}
/**
* @return list<Absence>
*/
public function findByEmployeeAndOverlappingDateRange(Employee $employee, DateTimeInterface $from, DateTimeInterface $to): array
{
$fromDate = DateTimeImmutable::createFromInterface($from);
$toDate = DateTimeImmutable::createFromInterface($to);
$qb = $this->createQueryBuilder('a')
->leftJoin('a.employee', 'e')
->leftJoin('a.type', 't')
->addSelect('e', 't')
->andWhere('a.employee = :employee')
->andWhere('a.startDate <= :to')
->andWhere('a.endDate >= :from')
->setParameter('employee', $employee)
->setParameter('from', $fromDate)
->setParameter('to', $toDate)
->orderBy('a.startDate', 'ASC')
;
// @var list<Absence> $absences
return $qb->getQuery()->getResult();
}
}

View File

@@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace App\Repository\Contract;
use App\Entity\Employee;
use App\Entity\EmployeeContractPeriod;
use DateTimeImmutable;
interface EmployeeContractPeriodReadRepositoryInterface
{
public function findOneCoveringDate(Employee $employee, DateTimeImmutable $date): ?EmployeeContractPeriod;
}

View File

@@ -6,6 +6,7 @@ namespace App\Repository;
use App\Entity\Employee;
use App\Entity\EmployeeContractPeriod;
use App\Repository\Contract\EmployeeContractPeriodReadRepositoryInterface;
use DateTimeImmutable;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
@@ -13,7 +14,7 @@ use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<EmployeeContractPeriod>
*/
final class EmployeeContractPeriodRepository extends ServiceEntityRepository
final class EmployeeContractPeriodRepository extends ServiceEntityRepository implements EmployeeContractPeriodReadRepositoryInterface
{
public function __construct(ManagerRegistry $registry)
{
@@ -72,4 +73,56 @@ final class EmployeeContractPeriodRepository extends ServiceEntityRepository
->execute()
;
}
public function hasPaidLeaveSettledClosureBetween(
Employee $employee,
DateTimeImmutable $from,
DateTimeImmutable $to
): bool {
$count = $this->createQueryBuilder('p')
->select('COUNT(p.id)')
->andWhere('p.employee = :employee')
->andWhere('p.paidLeaveSettled = :paidLeaveSettled')
->andWhere('p.endDate IS NOT NULL')
->andWhere('p.endDate >= :from')
->andWhere('p.endDate <= :to')
->setParameter('employee', $employee)
->setParameter('paidLeaveSettled', true)
->setParameter('from', $from)
->setParameter('to', $to)
->getQuery()
->getSingleScalarResult()
;
return (int) $count > 0;
}
public function findLatestPaidLeaveSettledClosureDateBetween(
Employee $employee,
DateTimeImmutable $from,
DateTimeImmutable $to
): ?DateTimeImmutable {
$result = $this->createQueryBuilder('p')
->select('p.endDate AS endDate')
->andWhere('p.employee = :employee')
->andWhere('p.paidLeaveSettled = :paidLeaveSettled')
->andWhere('p.endDate IS NOT NULL')
->andWhere('p.endDate >= :from')
->andWhere('p.endDate <= :to')
->setParameter('employee', $employee)
->setParameter('paidLeaveSettled', true)
->setParameter('from', $from)
->setParameter('to', $to)
->orderBy('p.endDate', 'DESC')
->setMaxResults(1)
->getQuery()
->getOneOrNullResult()
;
if (!is_array($result) || !isset($result['endDate']) || !$result['endDate'] instanceof DateTimeImmutable) {
return null;
}
return $result['endDate'];
}
}

View File

@@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
namespace App\Repository;
use App\Entity\Employee;
use App\Entity\EmployeeLeaveBalance;
use App\Enum\LeaveRuleCode;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<EmployeeLeaveBalance>
*/
final class EmployeeLeaveBalanceRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, EmployeeLeaveBalance::class);
}
public function findOneByEmployeeRuleAndYear(
Employee $employee,
LeaveRuleCode|string $ruleCode,
int $year
): ?EmployeeLeaveBalance {
$ruleCodeValue = $ruleCode instanceof LeaveRuleCode ? $ruleCode->value : $ruleCode;
return $this->createQueryBuilder('b')
->andWhere('b.employee = :employee')
->andWhere('b.ruleCode = :ruleCode')
->andWhere('b.year = :year')
->setParameter('employee', $employee)
->setParameter('ruleCode', $ruleCodeValue)
->setParameter('year', $year)
->setMaxResults(1)
->getQuery()
->getOneOrNullResult()
;
}
public function findEarliestYearForEmployee(Employee $employee): ?int
{
$result = $this->createQueryBuilder('b')
->select('MIN(b.year) AS year')
->andWhere('b.employee = :employee')
->setParameter('employee', $employee)
->getQuery()
->getOneOrNullResult()
;
if (!is_array($result) || !array_key_exists('year', $result) || null === $result['year']) {
return null;
}
return (int) $result['year'];
}
}

View File

@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace App\Service\Contracts;
use App\Enum\ContractNature;
use DateTimeImmutable;
final readonly class EmployeeContractChangeRequest
{
public function __construct(
public ?ContractNature $contractNature,
public ?DateTimeImmutable $contractStartDate,
public ?DateTimeImmutable $contractEndDate,
public ?bool $contractPaidLeaveSettled,
) {}
public function hasPeriodChangeRequest(): bool
{
return null !== $this->contractNature
|| null !== $this->contractStartDate
|| null !== $this->contractEndDate
|| null !== $this->contractPaidLeaveSettled;
}
public function isCloseOnlyRequest(bool $contractChanged): bool
{
return !$contractChanged
&& null === $this->contractStartDate
&& null === $this->contractNature
&& null !== $this->contractEndDate;
}
}

View File

@@ -0,0 +1,48 @@
<?php
declare(strict_types=1);
namespace App\Service\Contracts;
use App\Entity\Employee;
use App\Enum\ContractNature;
use DateTimeImmutable;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
final class EmployeeContractChangeRequestFactory
{
public function fromEmployee(Employee $employee): EmployeeContractChangeRequest
{
return new EmployeeContractChangeRequest(
contractNature: $this->resolveContractNature($employee->getContractNature()),
contractStartDate: $this->parseOptionalYmd($employee->getContractStartDate(), 'contractStartDate'),
contractEndDate: $this->parseOptionalYmd($employee->getContractEndDate(), 'contractEndDate'),
contractPaidLeaveSettled: $employee->getContractPaidLeaveSettled(),
);
}
private function resolveContractNature(?string $raw): ?ContractNature
{
if (null === $raw || '' === trim($raw)) {
return null;
}
return ContractNature::tryFrom(trim($raw))
?? throw new UnprocessableEntityHttpException('contractNature must be one of CDI, CDD, INTERIM.');
}
private function parseOptionalYmd(?string $raw, string $field): ?DateTimeImmutable
{
if (null === $raw || '' === trim($raw)) {
return null;
}
$value = trim($raw);
$date = DateTimeImmutable::createFromFormat('Y-m-d', $value);
if (!$date || $date->format('Y-m-d') !== $value) {
throw new UnprocessableEntityHttpException(sprintf('%s must use Y-m-d format.', $field));
}
return $date;
}
}

View File

@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace App\Service\Contracts;
use App\Entity\Contract;
use App\Entity\Employee;
use App\Entity\EmployeeContractPeriod;
use App\Enum\ContractNature;
use DateTimeImmutable;
final class EmployeeContractPeriodBuilder
{
public function build(
Employee $employee,
Contract $contract,
DateTimeImmutable $startDate,
?DateTimeImmutable $endDate,
ContractNature $nature,
): EmployeeContractPeriod {
return new EmployeeContractPeriod()
->setEmployee($employee)
->setContract($contract)
->setStartDate($startDate)
->setEndDate($endDate)
->setContractNature($nature)
;
}
}

View File

@@ -0,0 +1,96 @@
<?php
declare(strict_types=1);
namespace App\Service\Contracts;
use App\Entity\Contract;
use App\Entity\Employee;
use App\Entity\EmployeeContractPeriod;
use App\Enum\ContractNature;
use App\Repository\EmployeeContractPeriodRepository;
use DateTimeImmutable;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
final readonly class EmployeeContractPeriodManager implements EmployeeContractPeriodManagerInterface
{
public function __construct(
private EntityManagerInterface $entityManager,
private EmployeeContractPeriodRepository $periodRepository,
private EmployeeContractPeriodBuilder $periodBuilder,
private EmployeeContractPeriodValidator $periodValidator,
) {}
public function ensureContractPeriodExists(
Employee $employee,
Contract $contract,
DateTimeImmutable $startDate,
?DateTimeImmutable $endDate,
ContractNature $nature,
): void {
$this->periodValidator->assertPeriodDates($startDate, $endDate, $nature);
$covered = $this->periodRepository->findOneCoveringDate($employee, $startDate);
if (null !== $covered) {
return;
}
$this->persistNewPeriod($employee, $contract, $startDate, $endDate, $nature);
$this->entityManager->flush();
}
public function closeCurrentPeriod(
?EmployeeContractPeriod $todayPeriod,
DateTimeImmutable $requestedEndDate,
bool $paidLeaveSettled
): void {
if (null === $todayPeriod) {
throw new UnprocessableEntityHttpException('No active contract period to close.');
}
$this->periodValidator->assertCloseEndDateCanBeApplied(
$todayPeriod->getStartDate(),
$todayPeriod->getEndDate(),
$requestedEndDate,
$todayPeriod->getContractNatureEnum()
);
$todayPeriod->setEndDate($requestedEndDate);
$todayPeriod->setPaidLeaveSettled($paidLeaveSettled);
$this->entityManager->flush();
}
public function createNextPeriod(
Employee $employee,
Contract $contract,
DateTimeImmutable $startDate,
?DateTimeImmutable $endDate,
ContractNature $nature,
?EmployeeContractPeriod $todayPeriod
): void {
$this->periodValidator->assertPeriodDates($startDate, $endDate, $nature);
if (null !== $todayPeriod) {
$this->periodValidator->assertNextStartDateCompatible($startDate, $todayPeriod);
if (null === $todayPeriod->getEndDate()) {
$todayPeriod->setEndDate($startDate->modify('-1 day'));
}
}
$this->persistNewPeriod($employee, $contract, $startDate, $endDate, $nature);
$this->entityManager->flush();
}
private function persistNewPeriod(
Employee $employee,
Contract $contract,
DateTimeImmutable $startDate,
?DateTimeImmutable $endDate,
ContractNature $nature,
): void {
$period = $this->periodBuilder->build($employee, $contract, $startDate, $endDate, $nature);
$this->entityManager->persist($period);
}
}

View File

@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace App\Service\Contracts;
use App\Entity\Contract;
use App\Entity\Employee;
use App\Entity\EmployeeContractPeriod;
use App\Enum\ContractNature;
use DateTimeImmutable;
interface EmployeeContractPeriodManagerInterface
{
public function ensureContractPeriodExists(
Employee $employee,
Contract $contract,
DateTimeImmutable $startDate,
?DateTimeImmutable $endDate,
ContractNature $nature,
): void;
public function closeCurrentPeriod(
?EmployeeContractPeriod $todayPeriod,
DateTimeImmutable $requestedEndDate,
bool $paidLeaveSettled
): void;
public function createNextPeriod(
Employee $employee,
Contract $contract,
DateTimeImmutable $startDate,
?DateTimeImmutable $endDate,
ContractNature $nature,
?EmployeeContractPeriod $todayPeriod
): void;
}

View File

@@ -0,0 +1,63 @@
<?php
declare(strict_types=1);
namespace App\Service\Contracts;
use App\Entity\EmployeeContractPeriod;
use App\Enum\ContractNature;
use DateTimeImmutable;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
final class EmployeeContractPeriodValidator
{
public function assertPeriodDates(
DateTimeImmutable $startDate,
?DateTimeImmutable $endDate,
ContractNature $nature,
bool $allowCdiEndDate = false
): void {
if (null !== $endDate && $endDate < $startDate) {
throw new UnprocessableEntityHttpException('contractEndDate cannot be before contractStartDate.');
}
if ($nature->requiresEndDate() && null === $endDate) {
throw new UnprocessableEntityHttpException('contractEndDate is required for CDD and INTERIM.');
}
if (!$allowCdiEndDate && ContractNature::CDI === $nature && null !== $endDate) {
throw new UnprocessableEntityHttpException('contractEndDate must be empty for CDI.');
}
}
public function assertCloseEndDateCanBeApplied(
DateTimeImmutable $startDate,
?DateTimeImmutable $currentEndDate,
DateTimeImmutable $requestedEndDate,
ContractNature $nature
): void {
$this->assertPeriodDates($startDate, $requestedEndDate, $nature, true);
if (null !== $currentEndDate && $requestedEndDate > $currentEndDate) {
throw new UnprocessableEntityHttpException('contractEndDate cannot be increased on current contract.');
}
}
public function assertNextStartDateCompatible(
DateTimeImmutable $startDate,
EmployeeContractPeriod $currentPeriod
): void {
$currentEndDate = $currentPeriod->getEndDate();
if (null === $currentEndDate) {
if ($startDate <= $currentPeriod->getStartDate()) {
throw new UnprocessableEntityHttpException('contractStartDate must be after current contract start date.');
}
return;
}
if ($startDate <= $currentEndDate) {
throw new UnprocessableEntityHttpException('contractStartDate must be after current contract end date.');
}
}
}

View File

@@ -0,0 +1,384 @@
<?php
declare(strict_types=1);
namespace App\Service\Leave;
use App\Entity\Absence;
use App\Entity\Employee;
use App\Enum\LeaveRuleCode;
use App\Repository\AbsenceRepository;
use App\Repository\EmployeeContractPeriodRepository;
use App\Service\PublicHolidayServiceInterface;
use DateTimeImmutable;
use Throwable;
final readonly class LeaveBalanceComputationService
{
private const int FORFAIT_TARGET_WORKED_DAYS = 218;
private const float STANDARD_ANNUAL_DAYS = 25.0;
private const float STANDARD_ANNUAL_SATURDAYS = 5.0;
private const float STANDARD_ACCRUAL_PER_MONTH = self::STANDARD_ANNUAL_DAYS / 12.0;
private const float STANDARD_SATURDAY_ACCRUAL_PER_MONTH = self::STANDARD_ANNUAL_SATURDAYS / 12.0;
private const float FOUR_HOUR_ANNUAL_DAYS = 10.0;
private const float FOUR_HOUR_ACCRUAL_PER_MONTH = 0.83;
public function __construct(
private AbsenceRepository $absenceRepository,
private EmployeeContractPeriodRepository $periodRepository,
private PublicHolidayServiceInterface $publicHolidayService,
) {}
/**
* @return array{float, float}
*/
public function computeDynamicClosingForYear(Employee $employee, LeaveRuleCode $ruleCode, int $targetYear): array
{
$firstYear = $this->resolveFirstComputationYear($employee, $ruleCode, $targetYear);
if ($targetYear < $firstYear) {
return [0.0, 0.0];
}
$previousRemainingDays = 0.0;
$previousRemainingSaturdays = 0.0;
for ($year = $firstYear; $year <= $targetYear; ++$year) {
[$from, $to] = $this->resolvePeriodBounds($ruleCode, $year);
$carryDays = 0.0;
$carrySaturdays = 0.0;
if ($year > $firstYear) {
[$previousFrom, $previousTo] = $this->resolvePeriodBounds($ruleCode, $year - 1);
$hasSettlementOnPreviousYear = $this->periodRepository->hasPaidLeaveSettledClosureBetween($employee, $previousFrom, $previousTo);
if (!$hasSettlementOnPreviousYear) {
$carryDays = $previousRemainingDays;
$carrySaturdays = LeaveRuleCode::CDI_CDD_NON_FORFAIT === $ruleCode ? $previousRemainingSaturdays : 0.0;
}
}
$effectiveFrom = $this->resolveEffectivePeriodStart($employee, $from, $to);
if ($effectiveFrom > $from) {
$carryDays = 0.0;
$carrySaturdays = 0.0;
}
if (LeaveRuleCode::FORFAIT_218 === $ruleCode) {
$acquiredDays = $carryDays + (float) max(0, $this->countBusinessDays($from, $to) - self::FORFAIT_TARGET_WORKED_DAYS);
$absences = $this->absenceRepository->findByEmployeeAndOverlappingDateRange($employee, $effectiveFrom, $to);
[$takenDays] = $this->computeTakenAbsences($absences, $effectiveFrom, $to, false, false);
$previousRemainingDays = max(0.0, $acquiredDays - $takenDays);
$previousRemainingSaturdays = 0.0;
continue;
}
$generatedDays = $this->computeAccruedDays(
$this->resolveAnnualDays($employee),
$this->resolveDaysAccrualPerMonth($employee),
$effectiveFrom,
$to
);
$generatedSaturdays = $this->computeAccruedDays(
$this->resolveAnnualSaturdays($employee),
$this->resolveSaturdayAccrualPerMonth($employee),
$effectiveFrom,
$to
);
$absences = $this->absenceRepository->findByEmployeeAndOverlappingDateRange($employee, $effectiveFrom, $to);
[$takenDays, $takenSaturdays] = $this->computeTakenAbsences($absences, $effectiveFrom, $to, true, true);
$takenFromAcquired = min(max(0.0, $carryDays), $takenDays);
$remainingAcquired = $carryDays - $takenFromAcquired;
$remainingToImpute = max(0.0, $takenDays - $takenFromAcquired);
$remainingGenerated = $generatedDays - $remainingToImpute;
$takenFromAcquiredSaturdays = min(max(0.0, $carrySaturdays), $takenSaturdays);
$remainingAcquiredSaturdays = $carrySaturdays - $takenFromAcquiredSaturdays;
$remainingSaturdaysToImpute = max(0.0, $takenSaturdays - $takenFromAcquiredSaturdays);
$remainingGeneratedSaturdays = $generatedSaturdays - $remainingSaturdaysToImpute;
$previousRemainingDays = $remainingAcquired + $remainingGenerated;
$previousRemainingSaturdays = $remainingAcquiredSaturdays + $remainingGeneratedSaturdays;
}
return [$previousRemainingDays, $previousRemainingSaturdays];
}
/**
* @return array{DateTimeImmutable, DateTimeImmutable}
*/
public function resolvePeriodBounds(LeaveRuleCode $ruleCode, int $year): array
{
if (LeaveRuleCode::FORFAIT_218 === $ruleCode) {
return [
new DateTimeImmutable(sprintf('%d-01-01', $year)),
new DateTimeImmutable(sprintf('%d-12-31', $year)),
];
}
return [
new DateTimeImmutable(sprintf('%d-06-01', $year - 1)),
new DateTimeImmutable(sprintf('%d-05-31', $year)),
];
}
public function hasPaidLeaveSettledClosureBetween(
Employee $employee,
DateTimeImmutable $from,
DateTimeImmutable $to
): bool {
return $this->periodRepository->hasPaidLeaveSettledClosureBetween($employee, $from, $to);
}
private function resolveFirstComputationYear(Employee $employee, LeaveRuleCode $ruleCode, int $fallbackYear): int
{
$history = $employee->getContractHistory();
if ([] === $history) {
return $fallbackYear;
}
$oldestStartDate = null;
foreach ($history as $item) {
$start = DateTimeImmutable::createFromFormat('Y-m-d', $item->startDate);
if (!$start) {
continue;
}
if (null === $oldestStartDate || $start < $oldestStartDate) {
$oldestStartDate = $start;
}
}
if (null === $oldestStartDate) {
return $fallbackYear;
}
if (LeaveRuleCode::FORFAIT_218 === $ruleCode) {
return (int) $oldestStartDate->format('Y');
}
$startYear = (int) $oldestStartDate->format('Y');
$startMonth = (int) $oldestStartDate->format('n');
return $startMonth >= 6 ? $startYear + 1 : $startYear;
}
private function resolveEffectivePeriodStart(
Employee $employee,
DateTimeImmutable $from,
DateTimeImmutable $to
): DateTimeImmutable {
$latestSettledClosure = $this->periodRepository->findLatestPaidLeaveSettledClosureDateBetween($employee, $from, $to);
$start = $from;
if (null !== $latestSettledClosure) {
$nextDay = $latestSettledClosure->modify('+1 day');
if ($nextDay > $start) {
$start = $nextDay;
}
}
$earliestContractStart = $this->resolveEarliestContractStartWithinRange($employee, $from, $to);
if (null !== $earliestContractStart && $earliestContractStart > $start) {
$start = $earliestContractStart;
}
return $start;
}
private function resolveEarliestContractStartWithinRange(
Employee $employee,
DateTimeImmutable $from,
DateTimeImmutable $to
): ?DateTimeImmutable {
$earliest = null;
foreach ($employee->getContractHistory() as $period) {
$start = DateTimeImmutable::createFromFormat('Y-m-d', $period->startDate);
if (!$start) {
continue;
}
$end = null;
if (null !== $period->endDate && '' !== trim($period->endDate)) {
$end = DateTimeImmutable::createFromFormat('Y-m-d', $period->endDate);
}
if ($start > $to) {
continue;
}
if ($end instanceof DateTimeImmutable && $end < $from) {
continue;
}
$candidate = $start < $from ? $from : $start;
if (null === $earliest || $candidate < $earliest) {
$earliest = $candidate;
}
}
return $earliest;
}
private function resolveAnnualDays(Employee $employee): float
{
return 4 === $employee->getContract()?->getWeeklyHours()
? self::FOUR_HOUR_ANNUAL_DAYS
: self::STANDARD_ANNUAL_DAYS;
}
private function resolveAnnualSaturdays(Employee $employee): float
{
return 4 === $employee->getContract()?->getWeeklyHours()
? 0.0
: self::STANDARD_ANNUAL_SATURDAYS;
}
private function resolveDaysAccrualPerMonth(Employee $employee): float
{
return 4 === $employee->getContract()?->getWeeklyHours()
? self::FOUR_HOUR_ACCRUAL_PER_MONTH
: self::STANDARD_ACCRUAL_PER_MONTH;
}
private function resolveSaturdayAccrualPerMonth(Employee $employee): float
{
return 4 === $employee->getContract()?->getWeeklyHours()
? 0.0
: self::STANDARD_SATURDAY_ACCRUAL_PER_MONTH;
}
private function computeAccruedDays(
float $annualCap,
float $accrualPerMonth,
DateTimeImmutable $periodStart,
DateTimeImmutable $periodEnd
): float {
if ($accrualPerMonth <= 0.0 || $periodEnd < $periodStart) {
return 0.0;
}
$monthsElapsed = ((int) $periodEnd->format('Y') - (int) $periodStart->format('Y')) * 12
+ ((int) $periodEnd->format('n') - (int) $periodStart->format('n'))
+ 1;
return min($annualCap, $monthsElapsed * $accrualPerMonth);
}
private function countBusinessDays(DateTimeImmutable $from, DateTimeImmutable $to): int
{
$publicHolidays = $this->buildPublicHolidayMap($from, $to);
$count = 0;
for ($cursor = $from; $cursor <= $to; $cursor = $cursor->modify('+1 day')) {
$weekDay = (int) $cursor->format('N');
$dayKey = $cursor->format('Y-m-d');
if ($weekDay <= 5 && !isset($publicHolidays[$dayKey])) {
++$count;
}
}
return $count;
}
/**
* @return array<string, string>
*/
private function buildPublicHolidayMap(DateTimeImmutable $from, DateTimeImmutable $to): array
{
$map = [];
$startYear = (int) $from->format('Y');
$endYear = (int) $to->format('Y');
try {
for ($year = $startYear; $year <= $endYear; ++$year) {
$holidays = $this->publicHolidayService->getHolidaysDayByYears('metropole', (string) $year);
foreach ($holidays as $date => $label) {
$map[(string) $date] = (string) $label;
}
}
} catch (Throwable) {
return [];
}
return $map;
}
/**
* @param list<Absence> $absences
*
* @return array{float, float}
*/
private function computeTakenAbsences(
array $absences,
DateTimeImmutable $from,
DateTimeImmutable $to,
bool $countOnlyCp,
bool $splitSaturdays
): array {
$takenDays = 0.0;
$takenSaturdays = 0.0;
foreach ($absences as $absence) {
if ($countOnlyCp) {
$typeCode = strtoupper((string) $absence->getType()?->getCode());
if ('C' !== $typeCode) {
continue;
}
}
if (null === $absence->getType()) {
continue;
}
$start = DateTimeImmutable::createFromInterface($absence->getStartDate());
$end = DateTimeImmutable::createFromInterface($absence->getEndDate());
$rangeStart = $start < $from ? $from : $start;
$rangeEnd = $end > $to ? $to : $end;
if ($rangeEnd < $rangeStart) {
continue;
}
for ($cursor = $rangeStart; $cursor <= $rangeEnd; $cursor = $cursor->modify('+1 day')) {
[$am, $pm] = $this->resolveSegmentsForDate($absence, $cursor->format('Y-m-d'));
$dayAmount = ($am ? 0.5 : 0.0) + ($pm ? 0.5 : 0.0);
if ($dayAmount <= 0.0) {
continue;
}
$isSaturday = $splitSaturdays && '6' === $cursor->format('N');
if ($isSaturday) {
$takenSaturdays += $dayAmount;
} else {
$takenDays += $dayAmount;
}
}
}
return [$takenDays, $takenSaturdays];
}
/**
* @return array{bool, bool}
*/
private function resolveSegmentsForDate(Absence $absence, string $date): array
{
$startYmd = DateTimeImmutable::createFromInterface($absence->getStartDate())->format('Y-m-d');
$endYmd = DateTimeImmutable::createFromInterface($absence->getEndDate())->format('Y-m-d');
$startHalf = $absence->getStartHalf()->value;
$endHalf = $absence->getEndHalf()->value;
$isSingleDay = $startYmd === $endYmd;
$isStartDay = $date === $startYmd;
$isEndDay = $date === $endYmd;
if ($isSingleDay) {
return ['AM' === $startHalf, 'PM' === $endHalf];
}
if ($isStartDay) {
return ['AM' === $startHalf, true];
}
if ($isEndDay) {
return [true, 'PM' === $endHalf];
}
return [true, true];
}
}

View File

@@ -0,0 +1,653 @@
<?php
declare(strict_types=1);
namespace App\State;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\ApiResource\EmployeeLeaveSummary;
use App\Entity\Absence;
use App\Entity\Employee;
use App\Entity\User;
use App\Enum\ContractNature;
use App\Enum\ContractType;
use App\Enum\LeaveRuleCode;
use App\Repository\AbsenceRepository;
use App\Repository\EmployeeContractPeriodRepository;
use App\Repository\EmployeeLeaveBalanceRepository;
use App\Repository\EmployeeRepository;
use App\Security\EmployeeScopeService;
use App\Service\Leave\LeaveBalanceComputationService;
use App\Service\PublicHolidayServiceInterface;
use DateTimeImmutable;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
use Throwable;
final readonly class EmployeeLeaveSummaryProvider implements ProviderInterface
{
private const int FORFAIT_TARGET_WORKED_DAYS = 218;
private const float CDI_NON_FORFAIT_STANDARD_ACQUIRED_DAYS = 25.0;
private const float CDI_NON_FORFAIT_STANDARD_ACQUIRED_SATURDAYS = 5.0;
private const float CDI_NON_FORFAIT_STANDARD_ACCRUAL_PER_MONTH = self::CDI_NON_FORFAIT_STANDARD_ACQUIRED_DAYS / 12.0;
private const float CDI_NON_FORFAIT_STANDARD_SATURDAY_ACCRUAL_PER_MONTH = self::CDI_NON_FORFAIT_STANDARD_ACQUIRED_SATURDAYS / 12.0;
private const float CDI_NON_FORFAIT_4H_ACQUIRED_DAYS = 10.0;
private const float CDI_NON_FORFAIT_4H_ACQUIRED_SATURDAYS = 0.0;
private const float CDI_NON_FORFAIT_4H_ACCRUAL_PER_MONTH = 0.83;
private const float CDI_NON_FORFAIT_4H_SATURDAY_ACCRUAL_PER_MONTH = 0.0;
public function __construct(
private Security $security,
private RequestStack $requestStack,
private EmployeeRepository $employeeRepository,
private EmployeeScopeService $employeeScopeService,
private AbsenceRepository $absenceRepository,
private EmployeeContractPeriodRepository $periodRepository,
private EmployeeLeaveBalanceRepository $leaveBalanceRepository,
private LeaveBalanceComputationService $leaveBalanceComputationService,
private PublicHolidayServiceInterface $publicHolidayService,
) {}
public function provide(Operation $operation, array $uriVariables = [], array $context = []): EmployeeLeaveSummary
{
$user = $this->security->getUser();
if (!$user instanceof User) {
throw new AccessDeniedHttpException('Authentication required.');
}
$employeeId = (int) ($uriVariables['id'] ?? 0);
if ($employeeId <= 0) {
throw new UnprocessableEntityHttpException('id must be a positive integer.');
}
$employee = $this->employeeRepository->find($employeeId);
if (!$employee instanceof Employee) {
throw new NotFoundHttpException('Employee not found.');
}
if (!$this->employeeScopeService->canAccessEmployee($user, $employee)) {
throw new AccessDeniedHttpException('Employee outside your scope.');
}
$year = $this->resolveYear($employee);
$summary = new EmployeeLeaveSummary();
$summary->year = $year;
$summary->ruleCode = LeaveRuleCode::UNSUPPORTED->value;
$yearSummary = $this->computeYearSummary($employee, $year);
if (null === $yearSummary) {
return $summary;
}
$summary->isSupported = true;
$summary->ruleCode = $yearSummary['ruleCode'];
$summary->acquiredDays = $yearSummary['acquiredDays'];
$summary->acquiredSaturdays = $yearSummary['acquiredSaturdays'];
$summary->fractionedDays = 0.0;
$summary->accruingDays = $yearSummary['accruingDays'];
$summary->takenDays = $yearSummary['takenDays'];
$summary->takenSaturdays = $yearSummary['takenSaturdays'];
$summary->remainingDays = $yearSummary['remainingDays'];
$summary->remainingSaturdays = $yearSummary['remainingSaturdays'];
return $summary;
}
/**
* @return null|array{
* ruleCode: string,
* acquiredDays: float,
* acquiredSaturdays: float,
* accruingDays: float,
* takenDays: float,
* takenSaturdays: float,
* remainingDays: float,
* remainingSaturdays: float
* }
*/
private function computeYearSummary(Employee $employee, int $targetYear): ?array
{
$firstYear = $this->resolveFirstComputationYear($employee);
if ($targetYear < $firstYear) {
$targetYear = $firstYear;
}
$previousRemainingDays = 0.0;
$previousRemainingSaturdays = 0.0;
$targetSummary = null;
for ($year = $firstYear; $year <= $targetYear; ++$year) {
[$from, $to] = $this->resolvePeriodBounds($employee, $year);
$leavePolicy = $this->resolveLeavePolicy($employee, $from, $to);
if (null === $leavePolicy) {
if ($year === $targetYear) {
return null;
}
continue;
}
$carryDays = 0.0;
$carrySaturdays = 0.0;
$openingBalance = $this->leaveBalanceRepository->findOneByEmployeeRuleAndYear(
$employee,
$leavePolicy['ruleCode'],
$year
);
if (null !== $openingBalance) {
$carryDays = $openingBalance->getOpeningDays();
$carrySaturdays = $leavePolicy['splitSaturdays'] ? $openingBalance->getOpeningSaturdays() : 0.0;
} elseif ($year > $firstYear) {
$ruleCode = LeaveRuleCode::from($leavePolicy['ruleCode']);
[$carryDays, $carrySaturdays] = $this->leaveBalanceComputationService
->computeDynamicClosingForYear($employee, $ruleCode, $year - 1)
;
[$previousFrom, $previousTo] = $this->leaveBalanceComputationService->resolvePeriodBounds($ruleCode, $year - 1);
$hasSettlement = $this->leaveBalanceComputationService
->hasPaidLeaveSettledClosureBetween($employee, $previousFrom, $previousTo)
;
if ($hasSettlement) {
$carryDays = 0.0;
$carrySaturdays = 0.0;
} elseif (!$leavePolicy['splitSaturdays']) {
$carrySaturdays = 0.0;
}
}
$effectiveFrom = $this->resolveEffectivePeriodStart($employee, $from, $to);
$hasShiftedStart = $effectiveFrom > $from;
if ($hasShiftedStart) {
$carryDays = 0.0;
$carrySaturdays = 0.0;
}
$calculationEnd = $this->resolveCalculationEndDate($leavePolicy['ruleCode'], $year, $to);
$generatedDays = $leavePolicy['accrualPerMonth'] > 0.0
? $this->computeAccruedDaysFromStart(
$leavePolicy['acquiredDays'],
$leavePolicy['accrualPerMonth'],
$effectiveFrom,
$calculationEnd
)
: 0.0;
$generatedSaturdays = $leavePolicy['saturdayAccrualPerMonth'] > 0.0
? $this->computeAccruedDaysFromStart(
$leavePolicy['acquiredSaturdays'],
$leavePolicy['saturdayAccrualPerMonth'],
$effectiveFrom,
$calculationEnd
)
: 0.0;
$absences = $this->absenceRepository->findByEmployeeAndOverlappingDateRange($employee, $from, $to);
[$takenDays, $takenSaturdays] = $this->computeTakenAbsences(
$absences,
$effectiveFrom,
$calculationEnd,
$leavePolicy['countOnlyCp'],
$leavePolicy['splitSaturdays']
);
if (LeaveRuleCode::CDI_CDD_NON_FORFAIT->value === $leavePolicy['ruleCode']) {
$availableAcquired = max(0.0, $carryDays);
$takenFromAcquired = min($availableAcquired, $takenDays);
$remainingAcquired = $carryDays - $takenFromAcquired;
$remainingToImpute = max(0.0, $takenDays - $takenFromAcquired);
$remainingGenerated = $generatedDays - $remainingToImpute;
$availableAcquiredSaturdays = max(0.0, $carrySaturdays);
$takenFromAcquiredSaturdays = min($availableAcquiredSaturdays, $takenSaturdays);
$remainingAcquiredSaturdays = $carrySaturdays - $takenFromAcquiredSaturdays;
$remainingSaturdaysToImpute = max(0.0, $takenSaturdays - $takenFromAcquiredSaturdays);
$remainingGeneratedSaturdays = $generatedSaturdays - $remainingSaturdaysToImpute;
$acquiredDays = $carryDays;
$accruingDays = $remainingGenerated + $remainingGeneratedSaturdays;
$remainingDays = $remainingAcquired;
$acquiredSaturdays = $carrySaturdays;
$remainingSaturdays = max(0.0, $remainingAcquiredSaturdays);
$previousRemainingDays = $remainingAcquired + $remainingGenerated;
$previousRemainingSaturdays = $remainingAcquiredSaturdays + $remainingGeneratedSaturdays;
} else {
// Forfait: no "en cours d'acquisition" counter, all rights are in acquired.
$acquiredDays = $carryDays + $leavePolicy['acquiredDays'];
$accruingDays = 0.0;
$remainingDays = max(0.0, $acquiredDays - $takenDays);
$acquiredSaturdays = 0.0;
$remainingSaturdays = 0.0;
$previousRemainingDays = $remainingDays;
$previousRemainingSaturdays = 0.0;
}
if ($year === $targetYear) {
$targetSummary = [
'ruleCode' => $leavePolicy['ruleCode'],
'acquiredDays' => $acquiredDays,
'acquiredSaturdays' => $acquiredSaturdays,
'accruingDays' => $accruingDays,
'takenDays' => $takenDays,
'takenSaturdays' => $takenSaturdays,
'remainingDays' => $remainingDays,
'remainingSaturdays' => $remainingSaturdays,
];
}
}
return $targetSummary;
}
private function resolveEffectivePeriodStart(
Employee $employee,
DateTimeImmutable $from,
DateTimeImmutable $to
): DateTimeImmutable {
$latestSettledClosure = $this->periodRepository->findLatestPaidLeaveSettledClosureDateBetween($employee, $from, $to);
$start = $from;
if (null !== $latestSettledClosure) {
$nextDay = $latestSettledClosure->modify('+1 day');
if ($nextDay > $start) {
$start = $nextDay;
}
}
$earliestContractStart = $this->resolveEarliestContractStartWithinRange($employee, $from, $to);
if (null !== $earliestContractStart && $earliestContractStart > $start) {
$start = $earliestContractStart;
}
return $start;
}
private function resolveEarliestContractStartWithinRange(
Employee $employee,
DateTimeImmutable $from,
DateTimeImmutable $to
): ?DateTimeImmutable {
$earliest = null;
foreach ($employee->getContractHistory() as $period) {
$start = DateTimeImmutable::createFromFormat('Y-m-d', $period->startDate);
if (!$start instanceof DateTimeImmutable) {
continue;
}
$end = null;
if (null !== $period->endDate && '' !== trim($period->endDate)) {
$end = DateTimeImmutable::createFromFormat('Y-m-d', $period->endDate);
}
if ($start > $to) {
continue;
}
if ($end instanceof DateTimeImmutable && $end < $from) {
continue;
}
$candidate = $start < $from ? $from : $start;
if (null === $earliest || $candidate < $earliest) {
$earliest = $candidate;
}
}
return $earliest;
}
private function resolveYear(Employee $employee): int
{
$raw = (string) ($this->requestStack->getCurrentRequest()?->query->get('year') ?? '');
if ('' === $raw) {
$today = new DateTimeImmutable('today');
if (ContractType::FORFAIT === $employee->getContract()?->getType()) {
return (int) $today->format('Y');
}
return $this->resolveCurrentLeaveYear($today);
}
if (!preg_match('/^\d{4}$/', $raw)) {
throw new UnprocessableEntityHttpException('year must use YYYY format.');
}
$year = (int) $raw;
if ($year < 2000 || $year > 2100) {
throw new UnprocessableEntityHttpException('year must be between 2000 and 2100.');
}
return $year;
}
private function computeAccruedDaysFromStart(
float $acquiredDays,
float $accrualPerMonth,
DateTimeImmutable $periodStart,
?DateTimeImmutable $periodEnd
): float {
if ($accrualPerMonth <= 0.0) {
return $acquiredDays;
}
if (!$periodEnd instanceof DateTimeImmutable || $periodEnd < $periodStart) {
return 0.0;
}
$monthsElapsed = ((int) $periodEnd->format('Y') - (int) $periodStart->format('Y')) * 12
+ ((int) $periodEnd->format('n') - (int) $periodStart->format('n'))
+ 1;
if ($monthsElapsed < 0) {
return 0.0;
}
return min($acquiredDays, $monthsElapsed * $accrualPerMonth);
}
private function resolveCalculationEndDate(
string $ruleCode,
int $year,
DateTimeImmutable $periodEnd
): ?DateTimeImmutable {
$today = new DateTimeImmutable('today');
$currentYear = LeaveRuleCode::FORFAIT_218->value === $ruleCode
? (int) $today->format('Y')
: $this->resolveCurrentLeaveYear($today);
if ($year < $currentYear) {
return $periodEnd;
}
if ($year > $currentYear) {
return null;
}
$lastDayPreviousMonth = $today
->modify('first day of this month')
->modify('-1 day')
;
return $lastDayPreviousMonth < $periodEnd ? $lastDayPreviousMonth : $periodEnd;
}
/**
* @return null|array{
* ruleCode: string,
* acquiredDays: float,
* acquiredSaturdays: float,
* accrualPerMonth: float,
* saturdayAccrualPerMonth: float,
* countOnlyCp: bool,
* splitSaturdays: bool
* }
*/
private function resolveLeavePolicy(Employee $employee, DateTimeImmutable $from, DateTimeImmutable $to): ?array
{
$type = $employee->getContract()?->getType();
if (ContractType::FORFAIT === $type) {
$businessDaysInPeriod = $this->countBusinessDays($from, $to);
return [
'ruleCode' => LeaveRuleCode::FORFAIT_218->value,
'acquiredDays' => (float) max(0, $businessDaysInPeriod - self::FORFAIT_TARGET_WORKED_DAYS),
'acquiredSaturdays' => 0.0,
'accrualPerMonth' => 0.0,
'saturdayAccrualPerMonth' => 0.0,
'countOnlyCp' => false,
'splitSaturdays' => false,
];
}
$nature = ContractNature::tryFrom($employee->getCurrentContractNature());
if (ContractNature::CDI !== $nature && ContractNature::CDD !== $nature) {
return null;
}
$weeklyHours = $employee->getContract()?->getWeeklyHours();
if (4 === $weeklyHours) {
return [
'ruleCode' => LeaveRuleCode::CDI_CDD_NON_FORFAIT->value,
'acquiredDays' => self::CDI_NON_FORFAIT_4H_ACQUIRED_DAYS,
'acquiredSaturdays' => self::CDI_NON_FORFAIT_4H_ACQUIRED_SATURDAYS,
'accrualPerMonth' => self::CDI_NON_FORFAIT_4H_ACCRUAL_PER_MONTH,
'saturdayAccrualPerMonth' => self::CDI_NON_FORFAIT_4H_SATURDAY_ACCRUAL_PER_MONTH,
'countOnlyCp' => true,
'splitSaturdays' => true,
];
}
return [
'ruleCode' => LeaveRuleCode::CDI_CDD_NON_FORFAIT->value,
'acquiredDays' => self::CDI_NON_FORFAIT_STANDARD_ACQUIRED_DAYS,
'acquiredSaturdays' => self::CDI_NON_FORFAIT_STANDARD_ACQUIRED_SATURDAYS,
'accrualPerMonth' => self::CDI_NON_FORFAIT_STANDARD_ACCRUAL_PER_MONTH,
'saturdayAccrualPerMonth' => self::CDI_NON_FORFAIT_STANDARD_SATURDAY_ACCRUAL_PER_MONTH,
'countOnlyCp' => true,
'splitSaturdays' => true,
];
}
private function countBusinessDays(DateTimeImmutable $from, DateTimeImmutable $to): int
{
$publicHolidays = $this->buildPublicHolidayMap($from, $to);
$count = 0;
for ($cursor = $from; $cursor <= $to; $cursor = $cursor->modify('+1 day')) {
$weekDay = (int) $cursor->format('N');
$dayKey = $cursor->format('Y-m-d');
if ($weekDay <= 5 && !isset($publicHolidays[$dayKey])) {
++$count;
}
}
return $count;
}
/**
* @return array<string, string>
*/
private function buildPublicHolidayMap(DateTimeImmutable $from, DateTimeImmutable $to): array
{
$map = [];
$startYear = (int) $from->format('Y');
$endYear = (int) $to->format('Y');
try {
for ($year = $startYear; $year <= $endYear; ++$year) {
$holidays = $this->publicHolidayService->getHolidaysDayByYears('metropole', (string) $year);
foreach ($holidays as $date => $label) {
$map[(string) $date] = (string) $label;
}
}
} catch (Throwable) {
return [];
}
return $map;
}
/**
* @return array{DateTimeImmutable, DateTimeImmutable}
*/
private function resolvePeriodBounds(Employee $employee, int $year): array
{
if (ContractType::FORFAIT === $employee->getContract()?->getType()) {
return $this->resolveForfaitYearBounds($employee, $year);
}
return $this->resolveLeavePeriodBounds($year);
}
/**
* @return array{DateTimeImmutable, DateTimeImmutable}
*/
private function resolveLeavePeriodBounds(int $leaveYear): array
{
// Exercice CP "2026" = du 1er juin 2025 au 31 mai 2026.
$from = new DateTimeImmutable(sprintf('%d-06-01', $leaveYear - 1));
$to = new DateTimeImmutable(sprintf('%d-05-31', $leaveYear));
return [$from, $to];
}
/**
* @return array{DateTimeImmutable, DateTimeImmutable}
*/
private function resolveForfaitYearBounds(Employee $employee, int $year): array
{
$from = new DateTimeImmutable(sprintf('%d-01-01', $year));
$to = new DateTimeImmutable(sprintf('%d-12-31', $year));
$contractStartRaw = $employee->getCurrentContractStartDate();
if (null !== $contractStartRaw && '' !== trim($contractStartRaw)) {
$contractStart = DateTimeImmutable::createFromFormat('Y-m-d', $contractStartRaw);
if ($contractStart instanceof DateTimeImmutable && $contractStart > $from) {
$from = $contractStart;
}
}
$contractEndRaw = $employee->getCurrentContractEndDate();
if (null !== $contractEndRaw && '' !== trim($contractEndRaw)) {
$contractEnd = DateTimeImmutable::createFromFormat('Y-m-d', $contractEndRaw);
if ($contractEnd instanceof DateTimeImmutable && $contractEnd < $to) {
$to = $contractEnd;
}
}
return [$from, $to];
}
private function resolveCurrentLeaveYear(DateTimeImmutable $today): int
{
$year = (int) $today->format('Y');
$month = (int) $today->format('n');
return $month >= 6 ? $year + 1 : $year;
}
private function resolveFirstComputationYear(Employee $employee): int
{
$isForfait = ContractType::FORFAIT === $employee->getContract()?->getType();
$fallbackYear = $isForfait
? (int) new DateTimeImmutable('today')->format('Y')
: $this->resolveCurrentLeaveYear(new DateTimeImmutable('today'));
$history = $employee->getContractHistory();
if ([] === $history) {
return $fallbackYear;
}
$oldestStartDate = null;
foreach ($history as $item) {
$start = DateTimeImmutable::createFromFormat('Y-m-d', $item->startDate);
if (!$start) {
continue;
}
if (null === $oldestStartDate || $start < $oldestStartDate) {
$oldestStartDate = $start;
}
}
if (null === $oldestStartDate) {
$oldestBalanceYear = $this->leaveBalanceRepository->findEarliestYearForEmployee($employee);
return null === $oldestBalanceYear ? $fallbackYear : min($fallbackYear, $oldestBalanceYear);
}
$firstYear = $isForfait
? (int) $oldestStartDate->format('Y')
: ((int) $oldestStartDate->format('n') >= 6
? (int) $oldestStartDate->format('Y') + 1
: (int) $oldestStartDate->format('Y'));
$oldestBalanceYear = $this->leaveBalanceRepository->findEarliestYearForEmployee($employee);
if (null !== $oldestBalanceYear && $oldestBalanceYear < $firstYear) {
return $oldestBalanceYear;
}
return $firstYear;
}
/**
* @param list<Absence> $absences
*
* @return array{float, float}
*/
private function computeTakenAbsences(
array $absences,
DateTimeImmutable $from,
?DateTimeImmutable $to,
bool $countOnlyCp,
bool $splitSaturdays
): array {
$takenDays = 0.0;
$takenSaturdays = 0.0;
if (!$to instanceof DateTimeImmutable || $to < $from) {
return [$takenDays, $takenSaturdays];
}
foreach ($absences as $absence) {
if ($countOnlyCp) {
$typeCode = strtoupper((string) $absence->getType()?->getCode());
if ('C' !== $typeCode) {
continue;
}
}
if (null === $absence->getType()) {
continue;
}
$start = DateTimeImmutable::createFromInterface($absence->getStartDate());
$end = DateTimeImmutable::createFromInterface($absence->getEndDate());
$rangeStart = $start < $from ? $from : $start;
$rangeEnd = $end > $to ? $to : $end;
if ($rangeEnd < $rangeStart) {
continue;
}
for ($cursor = $rangeStart; $cursor <= $rangeEnd; $cursor = $cursor->modify('+1 day')) {
[$am, $pm] = $this->resolveSegmentsForDate($absence, $cursor->format('Y-m-d'));
$dayAmount = ($am ? 0.5 : 0.0) + ($pm ? 0.5 : 0.0);
if ($dayAmount <= 0.0) {
continue;
}
$isSaturday = $splitSaturdays && '6' === $cursor->format('N');
if ($isSaturday && $splitSaturdays) {
$takenSaturdays += $dayAmount;
} else {
$takenDays += $dayAmount;
}
}
}
return [$takenDays, $takenSaturdays];
}
/**
* @return array{bool, bool}
*/
private function resolveSegmentsForDate(Absence $absence, string $date): array
{
$startDate = $absence->getStartDate()->format('Y-m-d');
$endDate = $absence->getEndDate()->format('Y-m-d');
$startHalf = $absence->getStartHalf()->value;
$endHalf = $absence->getEndHalf()->value;
$isStart = $date === $startDate;
$isEnd = $date === $endDate;
$isSingleDay = $startDate === $endDate;
if ($isSingleDay) {
return ['AM' === $startHalf, 'PM' === $endHalf];
}
if ($isStart) {
return ['AM' === $startHalf, true];
}
if ($isEnd) {
return [true, 'PM' === $endHalf];
}
return [true, true];
}
}

View File

@@ -9,9 +9,10 @@ use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProcessorInterface;
use App\Entity\Contract;
use App\Entity\Employee;
use App\Entity\EmployeeContractPeriod;
use App\Enum\ContractNature;
use App\Repository\EmployeeContractPeriodRepository;
use App\Repository\Contract\EmployeeContractPeriodReadRepositoryInterface;
use App\Service\Contracts\EmployeeContractChangeRequestFactory;
use App\Service\Contracts\EmployeeContractPeriodManagerInterface;
use DateTimeImmutable;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
@@ -25,7 +26,9 @@ final readonly class EmployeeWriteProcessor implements ProcessorInterface
#[Autowire(service: 'api_platform.doctrine.orm.state.remove_processor')]
private ProcessorInterface $removeProcessor,
private EntityManagerInterface $entityManager,
private EmployeeContractPeriodRepository $periodRepository,
private EmployeeContractPeriodReadRepositoryInterface $periodRepository,
private EmployeeContractChangeRequestFactory $changeRequestFactory,
private EmployeeContractPeriodManagerInterface $periodManager,
) {}
public function process(
@@ -51,22 +54,24 @@ final readonly class EmployeeWriteProcessor implements ProcessorInterface
return $result;
}
$today = new DateTimeImmutable('today');
$requestedContractNature = $this->resolveContractNature($data->getContractNature());
$requestedStartDate = $this->parseOptionalYmd($data->getContractStartDate(), 'contractStartDate');
$requestedEndDate = $this->parseOptionalYmd($data->getContractEndDate(), 'contractEndDate');
$today = new DateTimeImmutable('today');
$changeRequest = $this->changeRequestFactory->fromEmployee($data);
if ($isNew) {
$startDate = $requestedStartDate ?? new DateTimeImmutable('1970-01-01');
$nature = $requestedContractNature ?? ContractNature::CDI;
$this->assertPeriodDates($startDate, $requestedEndDate, $nature);
$this->ensureContractPeriodExists($data, $currentContract, $startDate, $requestedEndDate, $nature);
$startDate = $changeRequest->contractStartDate ?? new DateTimeImmutable('1970-01-01');
$nature = $changeRequest->contractNature ?? ContractNature::CDI;
$this->periodManager->ensureContractPeriodExists(
employee: $data,
contract: $currentContract,
startDate: $startDate,
endDate: $changeRequest->contractEndDate,
nature: $nature
);
return $result;
}
$hasPeriodChangeRequest = null !== $requestedContractNature || null !== $requestedStartDate || null !== $requestedEndDate;
if ($this->isSameContract($previousContract, $currentContract) && !$hasPeriodChangeRequest) {
if ($this->isSameContract($previousContract, $currentContract) && !$changeRequest->hasPeriodChangeRequest()) {
return $result;
}
@@ -75,50 +80,32 @@ final readonly class EmployeeWriteProcessor implements ProcessorInterface
$contractChanged = $currentPeriodContract instanceof Contract
? $currentPeriodContract->getId() !== $currentContract->getId()
: true;
$isCloseOnlyRequest = !$contractChanged
&& null === $requestedStartDate
&& null === $requestedContractNature
&& null !== $requestedEndDate;
$isCloseOnlyRequest = $changeRequest->isCloseOnlyRequest($contractChanged);
if ($isCloseOnlyRequest) {
if (null === $todayPeriod) {
throw new UnprocessableEntityHttpException('No active contract period to close.');
$requestedEndDate = $changeRequest->contractEndDate;
if (null === $requestedEndDate) {
throw new UnprocessableEntityHttpException('contractEndDate is required for close-only request.');
}
$currentNature = $todayPeriod->getContractNatureEnum();
$this->assertPeriodDates($todayPeriod->getStartDate(), $requestedEndDate, $currentNature, true);
$currentEndDate = $todayPeriod->getEndDate();
if (null !== $currentEndDate && $requestedEndDate > $currentEndDate) {
throw new UnprocessableEntityHttpException('contractEndDate cannot be increased on current contract.');
}
$todayPeriod->setEndDate($requestedEndDate);
$this->entityManager->flush();
$this->periodManager->closeCurrentPeriod(
$todayPeriod,
$requestedEndDate,
$changeRequest->contractPaidLeaveSettled ?? false
);
return $result;
}
$startDate = $requestedStartDate ?? $today;
$nature = $requestedContractNature ?? $todayPeriod?->getContractNatureEnum() ?? ContractNature::CDI;
$endDate = $requestedEndDate;
$this->assertPeriodDates($startDate, $endDate, $nature);
if (null !== $todayPeriod) {
$currentEndDate = $todayPeriod->getEndDate();
if (null === $currentEndDate) {
if ($startDate <= $todayPeriod->getStartDate()) {
throw new UnprocessableEntityHttpException('contractStartDate must be after current contract start date.');
}
$todayPeriod->setEndDate($startDate->modify('-1 day'));
} elseif ($startDate <= $currentEndDate) {
throw new UnprocessableEntityHttpException('contractStartDate must be after current contract end date.');
}
}
$this->createPeriod($data, $currentContract, $startDate, $endDate, $nature);
$this->entityManager->flush();
$startDate = $changeRequest->contractStartDate ?? $today;
$nature = $changeRequest->contractNature ?? $todayPeriod?->getContractNatureEnum() ?? ContractNature::CDI;
$this->periodManager->createNextPeriod(
employee: $data,
contract: $currentContract,
startDate: $startDate,
endDate: $changeRequest->contractEndDate,
nature: $nature,
todayPeriod: $todayPeriod
);
return $result;
}
@@ -143,82 +130,4 @@ final readonly class EmployeeWriteProcessor implements ProcessorInterface
return $first->getId() === $second->getId();
}
private function ensureContractPeriodExists(
Employee $employee,
Contract $contract,
DateTimeImmutable $startDate,
?DateTimeImmutable $endDate,
ContractNature $nature,
): void {
$covered = $this->periodRepository->findOneCoveringDate($employee, $startDate);
if (null !== $covered) {
return;
}
$this->createPeriod($employee, $contract, $startDate, $endDate, $nature);
$this->entityManager->flush();
}
private function createPeriod(
Employee $employee,
Contract $contract,
DateTimeImmutable $startDate,
?DateTimeImmutable $endDate,
ContractNature $nature,
): void {
$period = new EmployeeContractPeriod()
->setEmployee($employee)
->setContract($contract)
->setStartDate($startDate)
->setEndDate($endDate)
->setContractNature($nature)
;
$this->entityManager->persist($period);
}
private function resolveContractNature(?string $raw): ?ContractNature
{
if (null === $raw || '' === trim($raw)) {
return null;
}
return ContractNature::tryFrom(trim($raw))
?? throw new UnprocessableEntityHttpException('contractNature must be one of CDI, CDD, INTERIM.');
}
private function parseOptionalYmd(?string $raw, string $field): ?DateTimeImmutable
{
if (null === $raw || '' === trim($raw)) {
return null;
}
$value = trim($raw);
$date = DateTimeImmutable::createFromFormat('Y-m-d', $value);
if (!$date || $date->format('Y-m-d') !== $value) {
throw new UnprocessableEntityHttpException(sprintf('%s must use Y-m-d format.', $field));
}
return $date;
}
private function assertPeriodDates(
DateTimeImmutable $startDate,
?DateTimeImmutable $endDate,
ContractNature $nature,
bool $allowCdiEndDate = false
): void {
if (null !== $endDate && $endDate < $startDate) {
throw new UnprocessableEntityHttpException('contractEndDate cannot be before contractStartDate.');
}
if ($nature->requiresEndDate() && null === $endDate) {
throw new UnprocessableEntityHttpException('contractEndDate is required for CDD and INTERIM.');
}
if (!$allowCdiEndDate && ContractNature::CDI === $nature && null !== $endDate) {
throw new UnprocessableEntityHttpException('contractEndDate must be empty for CDI.');
}
}
}