a41bd632cf
Auto Tag Develop / tag (push) Successful in 11s
## Correctifs RH (branche fix/retour-rh) ### Vue Jour (Heures) - Mode saisie/présence, libellé de contrat et sauvegarde résolus **à la date affichée** (et non au contrat courant). Corrige les salariés passés 39h/35h → Forfait. ### RTT — heures supplémentaires - Proratisation du **plafond 25%/50%** pour les embauches en milieu de semaine (la bande +25% se décale au lieu de rester bloquée à 43h). Témoin Dylan : 4h à 25% + 3h à 50%. ### Récap salaire (PDF mensuel) - Forfait : congés imputés **N-1** non affichés et comptés en présence. - Colonne « Heures payés » **scindée 25% / 50%** (en-tête fusionné). - **Exclusion des salariés sans contrat** sur le mois (ex. Marine, contrat terminé). ### Exports heures annuelles (par salarié + tous) - **Tous les jours sous contrat** affichés, même vides/non saisis (corrige les lignes manquantes). - Samedis/dimanches en **gris plus foncé**. ### Panier de nuit - **Ne s'applique pas aux conducteurs** (vue semaine + récap salaire). ## Tests - 11 tests ajoutés. Suite verte hors un test legacy pré-existant dépendant de la date (`EmployeeRttSummaryProviderTest::testNoQueryParamsKeepsLegacyYearDefaulting`, non modifié par cette branche). ## À noter (hors scope) - L'export heures annuelles *tous salariés* peut dépasser `memory_limit=256M` (Dompdf) — limitation **pré-existante**, non corrigée ici. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Reviewed-on: #21 Co-authored-by: tristan <tristan@yuno.malio.fr> Co-committed-by: tristan <tristan@yuno.malio.fr>
150 lines
6.6 KiB
PHP
150 lines
6.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\State;
|
|
|
|
use ApiPlatform\Metadata\Operation;
|
|
use ApiPlatform\State\ProviderInterface;
|
|
use App\ApiResource\WorkHourDayContext;
|
|
use App\Dto\WorkHours\DayContextRow;
|
|
use App\Entity\User;
|
|
use App\Repository\Contract\AbsenceReadRepositoryInterface;
|
|
use App\Repository\Contract\EmployeeScopedRepositoryInterface;
|
|
use App\Repository\Contract\FormationReadRepositoryInterface;
|
|
use App\Service\Contracts\EmployeeContractResolver;
|
|
use App\Service\WorkHours\AbsenceSegmentsResolver;
|
|
use App\Service\WorkHours\HolidayVirtualHoursResolver;
|
|
use App\Service\WorkHours\WorkedHoursCreditPolicy;
|
|
use DateTimeImmutable;
|
|
use Symfony\Bundle\SecurityBundle\Security;
|
|
use Symfony\Component\HttpFoundation\RequestStack;
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
|
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
|
|
|
final readonly class WorkHourDayContextProvider implements ProviderInterface
|
|
{
|
|
public function __construct(
|
|
private Security $security,
|
|
private RequestStack $requestStack,
|
|
private EmployeeScopedRepositoryInterface $employeeRepository,
|
|
private AbsenceReadRepositoryInterface $absenceRepository,
|
|
private FormationReadRepositoryInterface $formationRepository,
|
|
private EmployeeContractResolver $contractResolver,
|
|
private AbsenceSegmentsResolver $absenceSegmentsResolver,
|
|
private WorkedHoursCreditPolicy $workedHoursCreditPolicy,
|
|
private HolidayVirtualHoursResolver $holidayVirtualHoursResolver,
|
|
) {}
|
|
|
|
public function provide(Operation $operation, array $uriVariables = [], array $context = []): WorkHourDayContext
|
|
{
|
|
$user = $this->security->getUser();
|
|
// Endpoint protégé: on exige un utilisateur authentifié.
|
|
if (!$user instanceof User) {
|
|
throw new AccessDeniedHttpException('Authentication required.');
|
|
}
|
|
|
|
$workDate = $this->resolveWorkDate();
|
|
$employees = $this->employeeRepository->findScoped($user);
|
|
$absences = $this->absenceRepository->findByDateAndEmployees($workDate, $employees);
|
|
$formations = $this->formationRepository->findByDateAndEmployees($workDate, $employees);
|
|
|
|
$rowsByEmployeeId = [];
|
|
foreach ($employees as $employee) {
|
|
$employeeId = $employee->getId();
|
|
if (!$employeeId) {
|
|
continue;
|
|
}
|
|
|
|
// On initialise toutes les lignes, même sans absence ce jour-là.
|
|
$contract = $this->contractResolver->resolveForEmployeeAndDate($employee, $workDate);
|
|
$workDaysMinutes = $this->contractResolver->resolveWorkDaysMinutesForEmployeeAndDate($employee, $workDate);
|
|
$contractNature = null !== $contract
|
|
? $this->contractResolver->resolveNatureForEmployeeAndDate($employee, $workDate)->value
|
|
: null;
|
|
$rowsByEmployeeId[$employeeId] = new DayContextRow(
|
|
employeeId: $employeeId,
|
|
hasContractAtDate: null !== $contract,
|
|
isDriverContract: $this->contractResolver->resolveIsDriverForEmployeeAndDate($employee, $workDate),
|
|
virtualHolidayMinutes: $this->holidayVirtualHoursResolver->resolveVirtualCredit($contract, $workDate, false, $workDaysMinutes),
|
|
contractNature: $contractNature,
|
|
trackingMode: $contract?->getTrackingMode(),
|
|
weeklyHours: $contract?->getWeeklyHours(),
|
|
contractType: $contract?->getType()->value,
|
|
contractName: $contract?->getName(),
|
|
);
|
|
}
|
|
|
|
$dateKey = $workDate->format('Y-m-d');
|
|
foreach ($absences as $absence) {
|
|
$employeeId = $absence->getEmployee()?->getId();
|
|
// Ignore les absences orphelines ou hors scope utilisateur.
|
|
if (!$employeeId || !isset($rowsByEmployeeId[$employeeId])) {
|
|
continue;
|
|
}
|
|
|
|
[$absentMorning, $absentAfternoon] = $this->absenceSegmentsResolver->resolveForDate($absence, $dateKey);
|
|
// Pas de segment absent sur ce jour: rien à injecter dans la ligne.
|
|
if (!$absentMorning && !$absentAfternoon) {
|
|
continue;
|
|
}
|
|
|
|
// Calcule le crédit d'heures selon la politique métier (type d'absence + contrat).
|
|
$creditedMinutes = $this->workedHoursCreditPolicy->computeCreditedMinutes($absence, $dateKey, $absentMorning, $absentAfternoon);
|
|
$creditedPresenceUnits = $this->workedHoursCreditPolicy->computeCreditedPresenceUnits($absence, $dateKey, $absentMorning, $absentAfternoon);
|
|
$rowsByEmployeeId[$employeeId]->addAbsence(
|
|
label: $absence->getType()?->getLabel(),
|
|
color: $absence->getType()?->getColor(),
|
|
morning: $absentMorning,
|
|
afternoon: $absentAfternoon,
|
|
creditedMinutes: $creditedMinutes,
|
|
creditedPresenceUnits: $creditedPresenceUnits
|
|
);
|
|
}
|
|
|
|
foreach ($formations as $formation) {
|
|
$employeeId = $formation->getEmployee()?->getId();
|
|
if (!$employeeId || !isset($rowsByEmployeeId[$employeeId])) {
|
|
continue;
|
|
}
|
|
$rowsByEmployeeId[$employeeId]->setFormation('Formation');
|
|
}
|
|
|
|
// If an absence is declared on the day, the absence dictates the hours credited
|
|
// (via WorkedHoursCreditPolicy). The holiday virtual credit must not stack on top.
|
|
foreach ($rowsByEmployeeId as $row) {
|
|
if ($row->absentMorning || $row->absentAfternoon) {
|
|
$row->virtualHolidayMinutes = 0;
|
|
}
|
|
}
|
|
|
|
$response = new WorkHourDayContext();
|
|
$response->workDate = $dateKey;
|
|
$response->rows = array_map(
|
|
static fn (DayContextRow $row): array => $row->toArray(),
|
|
array_values($rowsByEmployeeId)
|
|
);
|
|
|
|
return $response;
|
|
}
|
|
|
|
private function resolveWorkDate(): DateTimeImmutable
|
|
{
|
|
$query = $this->requestStack->getCurrentRequest()?->query;
|
|
$raw = (string) ($query?->get('workDate') ?? '');
|
|
|
|
// Sans paramètre, on cible la date du jour.
|
|
if ('' === $raw) {
|
|
return new DateTimeImmutable('today');
|
|
}
|
|
|
|
$date = DateTimeImmutable::createFromFormat('Y-m-d', $raw);
|
|
// Validation stricte du format pour éviter les ambiguïtés de parsing.
|
|
if (!$date || $date->format('Y-m-d') !== $raw) {
|
|
throw new UnprocessableEntityHttpException('workDate must use Y-m-d format.');
|
|
}
|
|
|
|
return $date;
|
|
}
|
|
}
|