[#SIRH-34] fix RTT bascule ne fonctionne pas (#22)
Auto Tag Develop / tag (push) Successful in 7s
Auto Tag Develop / tag (push) Successful in 7s
La bascule app:rtt:rollover ne reprenait que les RTT acquis de l'exercice qui se terminait : le report d'ouverture déjà présent était perdu et les paiements n'étaient pas déduits. Le nouveau report reprend le solde de clôture = report d'ouverture(N-1) + acquis(N-1) − RTT payés(N-1), soit le "Disponible" affiché par EmployeeRttSummaryProvider. - nouveau RttClosingBalanceService (fold pur testé : invariant somme tranches = disponible, cascade déficit 50% avant 25%, récup CUSTOM non perdue) - RttRolloverCommand branché dessus + option --recompute (écrase les lignes existantes non verrouillées, pour reprise d'une bascule erronée) - test date-sensible EmployeeRttSummaryProviderTest rendu robuste - docs: doc/rtt-rollover.md, CLAUDE.md, documentation-content.ts Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> | Numéro du ticket | Titre du ticket | |------------------|-----------------| | | | ## Description de la PR ## Modification du .env ## Check list - [ ] Pas de régression - [ ] TU/TI/TF rédigée - [ ] TU/TI/TF OK - [ ] CHANGELOG modifié Reviewed-on: #22 Co-authored-by: tristan <tristan@yuno.malio.fr> Co-committed-by: tristan <tristan@yuno.malio.fr>
This commit was merged in pull request #22.
This commit is contained in:
@@ -10,7 +10,7 @@ use App\Enum\ContractType;
|
||||
use App\Enum\TrackingMode;
|
||||
use App\Repository\EmployeeRepository;
|
||||
use App\Repository\EmployeeRttBalanceRepository;
|
||||
use App\Service\Rtt\RttRecoveryComputationService;
|
||||
use App\Service\Rtt\RttClosingBalanceService;
|
||||
use DateTimeImmutable;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
@@ -32,7 +32,7 @@ final class RttRolloverCommand extends Command
|
||||
public function __construct(
|
||||
private readonly EmployeeRepository $employeeRepository,
|
||||
private readonly EmployeeRttBalanceRepository $rttBalanceRepository,
|
||||
private readonly RttRecoveryComputationService $rttRecoveryService,
|
||||
private readonly RttClosingBalanceService $rttClosingService,
|
||||
private readonly EntityManagerInterface $entityManager,
|
||||
#[Autowire(service: 'monolog.logger.cron')]
|
||||
private readonly LoggerInterface $logger,
|
||||
@@ -48,15 +48,22 @@ final class RttRolloverCommand extends Command
|
||||
InputOption::VALUE_NONE,
|
||||
'Run rollover regardless of business date (manual recovery mode).'
|
||||
);
|
||||
$this->addOption(
|
||||
'recompute',
|
||||
null,
|
||||
InputOption::VALUE_NONE,
|
||||
'Recompute and overwrite existing (non-locked) balances instead of skipping them.'
|
||||
);
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
$today = new DateTimeImmutable('today');
|
||||
$force = (bool) $input->getOption('force');
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
$today = new DateTimeImmutable('today');
|
||||
$force = (bool) $input->getOption('force');
|
||||
$recompute = (bool) $input->getOption('recompute');
|
||||
|
||||
$this->logger->info('app:rtt:rollover started.', ['date' => $today->format('Y-m-d'), 'force' => $force]);
|
||||
$this->logger->info('app:rtt:rollover started.', ['date' => $today->format('Y-m-d'), 'force' => $force, 'recompute' => $recompute]);
|
||||
|
||||
if (!$force && '06-01' !== $today->format('m-d')) {
|
||||
$message = 'No RTT rollover today: business date is not 01/06.';
|
||||
@@ -68,6 +75,7 @@ final class RttRolloverCommand extends Command
|
||||
|
||||
$targetYear = $this->resolveTargetYear($today);
|
||||
$created = 0;
|
||||
$updated = 0;
|
||||
$skipped = 0;
|
||||
|
||||
foreach ($this->employeeRepository->findAll() as $employee) {
|
||||
@@ -83,36 +91,53 @@ final class RttRolloverCommand extends Command
|
||||
}
|
||||
|
||||
$existing = $this->rttBalanceRepository->findOneByEmployeeAndYear($employee, $targetYear);
|
||||
if (null !== $existing) {
|
||||
if (null !== $existing && !$recompute) {
|
||||
$this->logger->info('Employee skipped: balance already exists.', ['employeeId' => $employee->getId(), 'year' => $targetYear]);
|
||||
++$skipped;
|
||||
|
||||
continue;
|
||||
}
|
||||
if (null !== $existing && $existing->isLocked()) {
|
||||
// Never overwrite a balance an RH user has validated/frozen.
|
||||
$this->logger->info('Employee skipped: balance is locked.', ['employeeId' => $employee->getId(), 'year' => $targetYear]);
|
||||
++$skipped;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
$previousYear = $targetYear - 1;
|
||||
$carry = $this->rttRecoveryService->computeTotalRecoveryForExercise($employee, $previousYear);
|
||||
// Closing of the previous exercise = opening report + earned − paid.
|
||||
$closing = $this->rttClosingService->computeClosingBalance($employee, $previousYear);
|
||||
} catch (Throwable $e) {
|
||||
$this->logger->error('Error computing carry for employee.', ['employeeId' => $employee->getId(), 'error' => $e->getMessage()]);
|
||||
$this->logger->error('Error computing closing balance for employee.', ['employeeId' => $employee->getId(), 'error' => $e->getMessage()]);
|
||||
++$skipped;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$balance = new EmployeeRttBalance()
|
||||
$balance = $existing ?? new EmployeeRttBalance()
|
||||
->setEmployee($employee)
|
||||
->setYear($targetYear)
|
||||
->setOpeningBase25Minutes($carry->base25Minutes)
|
||||
->setOpeningBonus25Minutes($carry->bonus25Minutes)
|
||||
->setOpeningBase50Minutes($carry->base50Minutes)
|
||||
->setOpeningBonus50Minutes($carry->bonus50Minutes)
|
||||
->setIsLocked(false)
|
||||
;
|
||||
|
||||
$this->entityManager->persist($balance);
|
||||
$this->logger->info('Balance created.', ['employeeId' => $employee->getId(), 'year' => $targetYear, 'carryMinutes' => $carry->totalMinutes]);
|
||||
++$created;
|
||||
$balance
|
||||
->setOpeningBase25Minutes($closing->base25Minutes)
|
||||
->setOpeningBonus25Minutes($closing->bonus25Minutes)
|
||||
->setOpeningBase50Minutes($closing->base50Minutes)
|
||||
->setOpeningBonus50Minutes($closing->bonus50Minutes)
|
||||
;
|
||||
|
||||
if (null === $existing) {
|
||||
$this->entityManager->persist($balance);
|
||||
$this->logger->info('Balance created.', ['employeeId' => $employee->getId(), 'year' => $targetYear, 'carryMinutes' => $closing->totalMinutes]);
|
||||
++$created;
|
||||
} else {
|
||||
$balance->touch();
|
||||
$this->logger->info('Balance recomputed.', ['employeeId' => $employee->getId(), 'year' => $targetYear, 'carryMinutes' => $closing->totalMinutes]);
|
||||
++$updated;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -124,7 +149,7 @@ final class RttRolloverCommand extends Command
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$message = sprintf('RTT rollover done: %d created, %d skipped.', $created, $skipped);
|
||||
$message = sprintf('RTT rollover done: %d created, %d recomputed, %d skipped.', $created, $updated, $skipped);
|
||||
$this->logger->info($message);
|
||||
$io->success($message);
|
||||
|
||||
|
||||
@@ -0,0 +1,164 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Rtt;
|
||||
|
||||
use App\Dto\Rtt\WeekRecoveryDetail;
|
||||
use App\Entity\Employee;
|
||||
use App\Repository\EmployeeRttBalanceRepository;
|
||||
use App\Repository\EmployeeRttPaymentRepository;
|
||||
use App\State\EmployeeRttSummaryProvider;
|
||||
|
||||
/**
|
||||
* Computes the closing RTT balance of an exercise — the amount that must become the
|
||||
* opening report of the next exercise.
|
||||
*
|
||||
* Closing = opening report (N) + net earned (N) − RTT paid (N).
|
||||
*
|
||||
* This mirrors the "disponible" exposed by {@see EmployeeRttSummaryProvider}
|
||||
* (carry + currentYearRecovery − totalPaid), so the report carried to N+1 always equals
|
||||
* the balance the RTT tab displayed for N. The previous rollover only took the earned
|
||||
* minutes and dropped both the incoming report and the payments.
|
||||
*/
|
||||
final readonly class RttClosingBalanceService
|
||||
{
|
||||
public function __construct(
|
||||
private RttRecoveryComputationService $recoveryService,
|
||||
private EmployeeRttBalanceRepository $balanceRepository,
|
||||
private EmployeeRttPaymentRepository $paymentRepository,
|
||||
) {}
|
||||
|
||||
public function computeClosingBalance(Employee $employee, int $exerciseYear): WeekRecoveryDetail
|
||||
{
|
||||
[$from, $to] = $this->recoveryService->resolveExerciseBounds($exerciseYear);
|
||||
$weeks = $this->recoveryService->buildWeeksForExercise($from, $to);
|
||||
$weekRanges = array_map(
|
||||
static fn (array $week): array => [
|
||||
'weekNumber' => (int) $week['weekNumber'],
|
||||
'start' => $week['start'],
|
||||
'end' => $week['end'],
|
||||
],
|
||||
$weeks
|
||||
);
|
||||
|
||||
// The exercise is fully closed at rollover time, so count every week up to its end.
|
||||
$byWeek = $this->recoveryService->computeRecoveryByWeek($employee, $weekRanges, $from, $to, $to);
|
||||
|
||||
$orderedDetails = [];
|
||||
foreach ($weekRanges as $week) {
|
||||
$key = $week['start']->format('Y-m-d');
|
||||
$orderedDetails[] = $byWeek[$key] ?? new WeekRecoveryDetail();
|
||||
}
|
||||
|
||||
$opening = $this->resolveOpeningReport($employee, $exerciseYear);
|
||||
$payments = $this->sumPayments($employee, $exerciseYear);
|
||||
|
||||
return $this->fold($opening, $orderedDetails, $payments);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pure accumulation of the closing balance per bucket.
|
||||
*
|
||||
* Guarantees `sum(buckets) === opening.total + Σ week.total − payments.total`,
|
||||
* i.e. the carried report matches the displayed disponible regardless of how the
|
||||
* deficit cascade or the custom-recovery remainder is distributed across buckets.
|
||||
*
|
||||
* @param list<WeekRecoveryDetail> $weeks chronological order
|
||||
*/
|
||||
public function fold(WeekRecoveryDetail $opening, array $weeks, WeekRecoveryDetail $payments): WeekRecoveryDetail
|
||||
{
|
||||
$b25 = $opening->base25Minutes;
|
||||
$bo25 = $opening->bonus25Minutes;
|
||||
$b50 = $opening->base50Minutes;
|
||||
$bo50 = $opening->bonus50Minutes;
|
||||
|
||||
foreach ($weeks as $week) {
|
||||
if ($week->totalMinutes >= 0) {
|
||||
$b25 += $week->base25Minutes;
|
||||
$bo25 += $week->bonus25Minutes;
|
||||
$b50 += $week->base50Minutes;
|
||||
$bo50 += $week->bonus50Minutes;
|
||||
|
||||
// Recovery not attributed to any 25/50 bucket (CUSTOM 1h=1h, rounding):
|
||||
// park it in the plain 25%-base bucket so the bucket sum keeps the total.
|
||||
$remainder = $week->totalMinutes
|
||||
- ($week->base25Minutes + $week->bonus25Minutes + $week->base50Minutes + $week->bonus50Minutes);
|
||||
$b25 += $remainder;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
// Deficit week: drain the 50%-tier before the 25%-tier (mirrors
|
||||
// EmployeeRttSummaryProvider's cumulative cascade).
|
||||
$deficit = -$week->totalMinutes;
|
||||
[$b50, $deficit] = $this->consume($b50, $deficit);
|
||||
[$bo50, $deficit] = $this->consume($bo50, $deficit);
|
||||
[$b25, $deficit] = $this->consume($b25, $deficit);
|
||||
$bo25 -= $deficit; // leftover may push the balance negative, as on screen
|
||||
}
|
||||
|
||||
$b25 -= $payments->base25Minutes;
|
||||
$bo25 -= $payments->bonus25Minutes;
|
||||
$b50 -= $payments->base50Minutes;
|
||||
$bo50 -= $payments->bonus50Minutes;
|
||||
|
||||
return new WeekRecoveryDetail(
|
||||
base25Minutes: $b25,
|
||||
bonus25Minutes: $bo25,
|
||||
base50Minutes: $b50,
|
||||
bonus50Minutes: $bo50,
|
||||
totalMinutes: $b25 + $bo25 + $b50 + $bo50,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The opening report of $year: the stored balance row when present, else the
|
||||
* dynamic fallback (earned in $year-1). Same resolution as
|
||||
* EmployeeRttSummaryProvider::resolveCarry.
|
||||
*/
|
||||
private function resolveOpeningReport(Employee $employee, int $year): WeekRecoveryDetail
|
||||
{
|
||||
$balance = $this->balanceRepository->findOneByEmployeeAndYear($employee, $year);
|
||||
if (null !== $balance) {
|
||||
return new WeekRecoveryDetail(
|
||||
base25Minutes: $balance->getOpeningBase25Minutes(),
|
||||
bonus25Minutes: $balance->getOpeningBonus25Minutes(),
|
||||
base50Minutes: $balance->getOpeningBase50Minutes(),
|
||||
bonus50Minutes: $balance->getOpeningBonus50Minutes(),
|
||||
totalMinutes: $balance->getTotalOpeningMinutes(),
|
||||
);
|
||||
}
|
||||
|
||||
return $this->recoveryService->computeTotalRecoveryForExercise($employee, $year - 1);
|
||||
}
|
||||
|
||||
private function sumPayments(Employee $employee, int $year): WeekRecoveryDetail
|
||||
{
|
||||
$b25 = $bo25 = $b50 = $bo50 = 0;
|
||||
foreach ($this->paymentRepository->findByEmployeeAndYear($employee, $year) as $payment) {
|
||||
$b25 += $payment->getBase25Minutes();
|
||||
$bo25 += $payment->getBonus25Minutes();
|
||||
$b50 += $payment->getBase50Minutes();
|
||||
$bo50 += $payment->getBonus50Minutes();
|
||||
}
|
||||
|
||||
return new WeekRecoveryDetail(
|
||||
base25Minutes: $b25,
|
||||
bonus25Minutes: $bo25,
|
||||
base50Minutes: $b50,
|
||||
bonus50Minutes: $bo50,
|
||||
totalMinutes: $b25 + $bo25 + $b50 + $bo50,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{int, int} [remaining bucket, remaining deficit]
|
||||
*/
|
||||
private function consume(int $bucket, int $deficit): array
|
||||
{
|
||||
$take = min($deficit, max(0, $bucket));
|
||||
|
||||
return [$bucket - $take, $deficit - $take];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user