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,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];
}
}