feat : ajout des suspensions et des jours de présence
Some checks failed
Auto Tag Develop / tag (push) Has been cancelled
Some checks failed
Auto Tag Develop / tag (push) Has been cancelled
This commit is contained in:
@@ -31,4 +31,7 @@ final class EmployeeLeaveSummary
|
||||
public float $takenSaturdays = 0.0;
|
||||
public float $fractionedDays = 0.0;
|
||||
public float $accruingDays = 0.0;
|
||||
|
||||
/** @var array<string, float> YYYY-MM => count (0.5 for half-days) */
|
||||
public array $presenceDaysByMonth = [];
|
||||
}
|
||||
|
||||
@@ -23,5 +23,9 @@ final class ContractHistoryItem
|
||||
public ?string $endDate,
|
||||
#[Groups(['employee:read'])]
|
||||
public ?string $comment = null,
|
||||
#[Groups(['employee:read'])]
|
||||
public ?int $periodId = null,
|
||||
#[Groups(['employee:read'])]
|
||||
public array $suspensions = [],
|
||||
) {}
|
||||
}
|
||||
|
||||
140
src/Entity/ContractSuspension.php
Normal file
140
src/Entity/ContractSuspension.php
Normal file
@@ -0,0 +1,140 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use ApiPlatform\Metadata\ApiResource;
|
||||
use ApiPlatform\Metadata\GetCollection;
|
||||
use ApiPlatform\Metadata\Patch;
|
||||
use ApiPlatform\Metadata\Post;
|
||||
use App\Repository\ContractSuspensionRepository;
|
||||
use App\State\ContractSuspensionWriteProcessor;
|
||||
use DateTimeImmutable;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Serializer\Attribute\Context;
|
||||
use Symfony\Component\Serializer\Attribute\Groups;
|
||||
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
|
||||
|
||||
#[ApiResource(
|
||||
operations: [
|
||||
new GetCollection(),
|
||||
new Post(processor: ContractSuspensionWriteProcessor::class),
|
||||
new Patch(processor: ContractSuspensionWriteProcessor::class),
|
||||
],
|
||||
normalizationContext: ['groups' => ['suspension:read']],
|
||||
denormalizationContext: ['groups' => ['suspension:write']],
|
||||
paginationEnabled: false,
|
||||
security: "is_granted('ROLE_ADMIN')",
|
||||
)]
|
||||
#[ORM\Entity(repositoryClass: ContractSuspensionRepository::class)]
|
||||
#[ORM\Table(name: 'contract_suspensions')]
|
||||
#[ORM\Index(columns: ['contract_period_id', 'start_date'], name: 'idx_suspension_period_start')]
|
||||
class ContractSuspension
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column(type: 'integer')]
|
||||
#[Groups(['suspension:read', 'employee:read'])]
|
||||
private ?int $id = null;
|
||||
|
||||
#[ORM\ManyToOne(targetEntity: EmployeeContractPeriod::class, inversedBy: 'suspensions')]
|
||||
#[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
|
||||
private ?EmployeeContractPeriod $contractPeriod = null;
|
||||
|
||||
#[Groups(['suspension:write'])]
|
||||
private ?int $contractPeriodId = null;
|
||||
|
||||
#[ORM\Column(type: 'date_immutable')]
|
||||
#[Groups(['suspension:read', 'suspension:write', 'employee:read'])]
|
||||
#[Context([DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'])]
|
||||
private DateTimeImmutable $startDate;
|
||||
|
||||
#[ORM\Column(type: 'date_immutable', nullable: true)]
|
||||
#[Groups(['suspension:read', 'suspension:write', 'employee:read'])]
|
||||
#[Context([DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'])]
|
||||
private ?DateTimeImmutable $endDate = null;
|
||||
|
||||
#[ORM\Column(type: 'text', nullable: true)]
|
||||
#[Groups(['suspension:read', 'suspension:write', 'employee:read'])]
|
||||
private ?string $comment = null;
|
||||
|
||||
#[ORM\Column(type: 'datetime_immutable')]
|
||||
private DateTimeImmutable $createdAt;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->createdAt = new DateTimeImmutable();
|
||||
$this->startDate = new DateTimeImmutable('today');
|
||||
}
|
||||
|
||||
public function getContractPeriodId(): ?int
|
||||
{
|
||||
return $this->contractPeriodId;
|
||||
}
|
||||
|
||||
public function setContractPeriodId(?int $contractPeriodId): self
|
||||
{
|
||||
$this->contractPeriodId = $contractPeriodId;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getContractPeriod(): ?EmployeeContractPeriod
|
||||
{
|
||||
return $this->contractPeriod;
|
||||
}
|
||||
|
||||
public function setContractPeriod(?EmployeeContractPeriod $contractPeriod): self
|
||||
{
|
||||
$this->contractPeriod = $contractPeriod;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getStartDate(): DateTimeImmutable
|
||||
{
|
||||
return $this->startDate;
|
||||
}
|
||||
|
||||
public function setStartDate(DateTimeImmutable $startDate): self
|
||||
{
|
||||
$this->startDate = $startDate;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getEndDate(): ?DateTimeImmutable
|
||||
{
|
||||
return $this->endDate;
|
||||
}
|
||||
|
||||
public function setEndDate(?DateTimeImmutable $endDate): self
|
||||
{
|
||||
$this->endDate = $endDate;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getComment(): ?string
|
||||
{
|
||||
return $this->comment;
|
||||
}
|
||||
|
||||
public function setComment(?string $comment): self
|
||||
{
|
||||
$this->comment = $comment;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getCreatedAt(): DateTimeImmutable
|
||||
{
|
||||
return $this->createdAt;
|
||||
}
|
||||
}
|
||||
@@ -263,6 +263,36 @@ class Employee
|
||||
return $this->resolveCurrentContractPeriod()?->getEndDate()?->format('Y-m-d');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<array{id: null|int, startDate: string, endDate: null|string, comment: null|string}>
|
||||
*/
|
||||
#[Groups(['employee:read'])]
|
||||
public function getCurrentSuspensions(): array
|
||||
{
|
||||
$currentPeriod = $this->resolveCurrentContractPeriod();
|
||||
if (null === $currentPeriod) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return array_values(array_map(
|
||||
static fn (ContractSuspension $s): array => [
|
||||
'id' => $s->getId(),
|
||||
'startDate' => $s->getStartDate()->format('Y-m-d'),
|
||||
'endDate' => $s->getEndDate()?->format('Y-m-d'),
|
||||
'comment' => $s->getComment(),
|
||||
],
|
||||
$currentPeriod->getSuspensions()->toArray()
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection<int, EmployeeContractPeriod>
|
||||
*/
|
||||
public function getContractPeriods(): Collection
|
||||
{
|
||||
return $this->contractPeriods;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<ContractHistoryItem>
|
||||
*/
|
||||
@@ -279,6 +309,16 @@ class Employee
|
||||
static function (EmployeeContractPeriod $period): ContractHistoryItem {
|
||||
$contract = $period->getContract();
|
||||
|
||||
$suspensionData = array_map(
|
||||
static fn (ContractSuspension $s): array => [
|
||||
'id' => $s->getId(),
|
||||
'startDate' => $s->getStartDate()->format('Y-m-d'),
|
||||
'endDate' => $s->getEndDate()?->format('Y-m-d'),
|
||||
'comment' => $s->getComment(),
|
||||
],
|
||||
$period->getSuspensions()->toArray()
|
||||
);
|
||||
|
||||
return new ContractHistoryItem(
|
||||
contractId: $contract?->getId(),
|
||||
contractName: $contract?->getName(),
|
||||
@@ -287,6 +327,8 @@ class Employee
|
||||
startDate: $period->getStartDate()->format('Y-m-d'),
|
||||
endDate: $period->getEndDate()?->format('Y-m-d'),
|
||||
comment: $period->getComment(),
|
||||
periodId: $period->getId(),
|
||||
suspensions: $suspensionData,
|
||||
);
|
||||
},
|
||||
$periods
|
||||
|
||||
@@ -7,6 +7,8 @@ namespace App\Entity;
|
||||
use App\Enum\ContractNature;
|
||||
use App\Repository\EmployeeContractPeriodRepository;
|
||||
use DateTimeImmutable;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
#[ORM\Entity(repositoryClass: EmployeeContractPeriodRepository::class)]
|
||||
@@ -43,13 +45,20 @@ class EmployeeContractPeriod
|
||||
#[ORM\Column(type: 'text', nullable: true)]
|
||||
private ?string $comment = null;
|
||||
|
||||
/**
|
||||
* @var Collection<int, ContractSuspension>
|
||||
*/
|
||||
#[ORM\OneToMany(mappedBy: 'contractPeriod', targetEntity: ContractSuspension::class, cascade: ['persist', 'remove'])]
|
||||
private Collection $suspensions;
|
||||
|
||||
#[ORM\Column(type: 'datetime_immutable')]
|
||||
private DateTimeImmutable $createdAt;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->createdAt = new DateTimeImmutable();
|
||||
$this->startDate = new DateTimeImmutable('today');
|
||||
$this->createdAt = new DateTimeImmutable();
|
||||
$this->startDate = new DateTimeImmutable('today');
|
||||
$this->suspensions = new ArrayCollection();
|
||||
}
|
||||
|
||||
public function getId(): ?int
|
||||
@@ -151,4 +160,12 @@ class EmployeeContractPeriod
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection<int, ContractSuspension>
|
||||
*/
|
||||
public function getSuspensions(): Collection
|
||||
{
|
||||
return $this->suspensions;
|
||||
}
|
||||
}
|
||||
|
||||
20
src/Repository/ContractSuspensionRepository.php
Normal file
20
src/Repository/ContractSuspensionRepository.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Repository;
|
||||
|
||||
use App\Entity\ContractSuspension;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
* @extends ServiceEntityRepository<ContractSuspension>
|
||||
*/
|
||||
class ContractSuspensionRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, ContractSuspension::class);
|
||||
}
|
||||
}
|
||||
@@ -138,6 +138,48 @@ final class WorkHourRepository extends ServiceEntityRepository implements WorkHo
|
||||
return $qb->getQuery()->getOneOrNullResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, float> YYYY-MM => presence day count (0.5 for half-days)
|
||||
*/
|
||||
public function countPresenceDaysByMonth(Employee $employee, DateTimeImmutable $from, DateTimeImmutable $to): array
|
||||
{
|
||||
$sql = <<<'SQL'
|
||||
SELECT TO_CHAR(work_date, 'YYYY-MM') AS month,
|
||||
SUM(
|
||||
CASE
|
||||
WHEN (morning_from IS NOT NULL OR is_present_morning = true)
|
||||
AND (afternoon_from IS NOT NULL OR is_present_afternoon = true)
|
||||
THEN 1.0
|
||||
WHEN (morning_from IS NOT NULL OR is_present_morning = true)
|
||||
OR (afternoon_from IS NOT NULL OR is_present_afternoon = true)
|
||||
THEN 0.5
|
||||
ELSE 0
|
||||
END
|
||||
) AS cnt
|
||||
FROM work_hours
|
||||
WHERE employee_id = :employee
|
||||
AND work_date >= :from
|
||||
AND work_date <= :to
|
||||
AND (morning_from IS NOT NULL OR is_present_morning = true
|
||||
OR afternoon_from IS NOT NULL OR is_present_afternoon = true)
|
||||
GROUP BY month
|
||||
SQL;
|
||||
|
||||
$conn = $this->getEntityManager()->getConnection();
|
||||
$rows = $conn->fetchAllAssociative($sql, [
|
||||
'employee' => $employee->getId(),
|
||||
'from' => $from->format('Y-m-d'),
|
||||
'to' => $to->format('Y-m-d'),
|
||||
]);
|
||||
|
||||
$result = [];
|
||||
foreach ($rows as $row) {
|
||||
$result[(string) $row['month']] = (float) $row['cnt'];
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function hasPendingSiteValidationForSiteAndDate(int $siteId, DateTimeInterface $date): bool
|
||||
{
|
||||
$workDate = DateTimeImmutable::createFromInterface($date);
|
||||
|
||||
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
namespace App\Service\Leave;
|
||||
|
||||
use App\Entity\Absence;
|
||||
use App\Entity\ContractSuspension;
|
||||
use App\Entity\Employee;
|
||||
use App\Enum\LeaveRuleCode;
|
||||
use App\Repository\AbsenceRepository;
|
||||
@@ -29,6 +30,7 @@ final readonly class LeaveBalanceComputationService
|
||||
private EmployeeContractPeriodRepository $periodRepository,
|
||||
private EmployeeLeaveBalanceRepository $leaveBalanceRepository,
|
||||
private PublicHolidayServiceInterface $publicHolidayService,
|
||||
private SuspensionDaysCalculator $suspensionDaysCalculator,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -67,7 +69,18 @@ final readonly class LeaveBalanceComputationService
|
||||
$fractionedDays = $this->resolveFractionedDays($employee, $ruleCode, $year);
|
||||
|
||||
if (LeaveRuleCode::FORFAIT_218 === $ruleCode) {
|
||||
$acquiredDays = $carryDays + (float) max(0, $this->countBusinessDays($from, $to) - self::FORFAIT_TARGET_WORKED_DAYS) + $fractionedDays;
|
||||
$totalBusinessDays = $this->countBusinessDays($from, $to);
|
||||
$baseAcquiredDays = (float) max(0, $totalBusinessDays - self::FORFAIT_TARGET_WORKED_DAYS);
|
||||
$suspensions = $this->resolveSuspensionsForEmployeePeriod($employee, $from, $to);
|
||||
$acquiredDays = $carryDays + $baseAcquiredDays + $fractionedDays;
|
||||
if ([] !== $suspensions) {
|
||||
$totalMonths = $this->countFractionalMonths($from, $to);
|
||||
$suspendedMonths = $this->countSuspendedFractionalMonths($from, $to, $suspensions);
|
||||
if ($totalMonths > 0) {
|
||||
$ratio = max(0.0, ($totalMonths - $suspendedMonths) / $totalMonths);
|
||||
$acquiredDays = $carryDays + $baseAcquiredDays * $ratio + $fractionedDays;
|
||||
}
|
||||
}
|
||||
$absences = $this->absenceRepository->findByEmployeeAndOverlappingDateRange($employee, $effectiveFrom, $to);
|
||||
[$takenDays] = $this->computeTakenAbsences($absences, $effectiveFrom, $to, false, false);
|
||||
$previousRemainingDays = max(0.0, $acquiredDays - $takenDays);
|
||||
@@ -76,17 +89,20 @@ final readonly class LeaveBalanceComputationService
|
||||
continue;
|
||||
}
|
||||
|
||||
$suspensions = $this->resolveSuspensionsForEmployeePeriod($employee, $from, $to);
|
||||
$generatedDays = $this->computeAccruedDays(
|
||||
$this->resolveAnnualDays($employee),
|
||||
$this->resolveDaysAccrualPerMonth($employee),
|
||||
$effectiveFrom,
|
||||
$to
|
||||
$to,
|
||||
$suspensions
|
||||
);
|
||||
$generatedSaturdays = $this->computeAccruedDays(
|
||||
$this->resolveAnnualSaturdays($employee),
|
||||
$this->resolveSaturdayAccrualPerMonth($employee),
|
||||
$effectiveFrom,
|
||||
$to
|
||||
$to,
|
||||
$suspensions
|
||||
);
|
||||
|
||||
$absences = $this->absenceRepository->findByEmployeeAndOverlappingDateRange($employee, $effectiveFrom, $to);
|
||||
@@ -262,7 +278,8 @@ final readonly class LeaveBalanceComputationService
|
||||
float $annualCap,
|
||||
float $accrualPerMonth,
|
||||
DateTimeImmutable $periodStart,
|
||||
DateTimeImmutable $periodEnd
|
||||
DateTimeImmutable $periodEnd,
|
||||
array $suspensions = []
|
||||
): float {
|
||||
if ($accrualPerMonth <= 0.0 || $periodEnd < $periodStart) {
|
||||
return 0.0;
|
||||
@@ -280,6 +297,10 @@ final readonly class LeaveBalanceComputationService
|
||||
}
|
||||
|
||||
$coveredDays = ((int) $monthEnd->diff($monthStart)->format('%a')) + 1;
|
||||
if ([] !== $suspensions) {
|
||||
$suspendedDays = $this->suspensionDaysCalculator->countSuspendedDaysInMonth($monthStart, $monthEnd, $suspensions);
|
||||
$coveredDays = max(0, $coveredDays - $suspendedDays);
|
||||
}
|
||||
$daysInMonth = (int) $cursor->format('t');
|
||||
$coveredMonths += $coveredDays / $daysInMonth;
|
||||
|
||||
@@ -404,6 +425,80 @@ final readonly class LeaveBalanceComputationService
|
||||
return [$takenDays, $takenSaturdays];
|
||||
}
|
||||
|
||||
private function countFractionalMonths(DateTimeImmutable $from, DateTimeImmutable $to): float
|
||||
{
|
||||
$from = $this->normalizeDate($from);
|
||||
$to = $this->normalizeDate($to);
|
||||
$months = 0.0;
|
||||
$cursor = $from->modify('first day of this month')->setTime(0, 0);
|
||||
|
||||
while ($cursor <= $to) {
|
||||
$monthStart = $cursor > $from ? $cursor : $from;
|
||||
$monthEnd = $cursor->modify('last day of this month')->setTime(0, 0);
|
||||
if ($monthEnd > $to) {
|
||||
$monthEnd = $to;
|
||||
}
|
||||
$coveredDays = ((int) $monthEnd->diff($monthStart)->format('%a')) + 1;
|
||||
$daysInMonth = (int) $cursor->format('t');
|
||||
$months += $coveredDays / $daysInMonth;
|
||||
|
||||
$cursor = $cursor->modify('first day of next month');
|
||||
}
|
||||
|
||||
return $months;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<ContractSuspension> $suspensions
|
||||
*/
|
||||
private function countSuspendedFractionalMonths(DateTimeImmutable $from, DateTimeImmutable $to, array $suspensions): float
|
||||
{
|
||||
$from = $this->normalizeDate($from);
|
||||
$to = $this->normalizeDate($to);
|
||||
$months = 0.0;
|
||||
$cursor = $from->modify('first day of this month')->setTime(0, 0);
|
||||
|
||||
while ($cursor <= $to) {
|
||||
$monthStart = $cursor > $from ? $cursor : $from;
|
||||
$monthEnd = $cursor->modify('last day of this month')->setTime(0, 0);
|
||||
if ($monthEnd > $to) {
|
||||
$monthEnd = $to;
|
||||
}
|
||||
$daysInMonth = (int) $cursor->format('t');
|
||||
$suspendedDays = $this->suspensionDaysCalculator->countSuspendedDaysInMonth($monthStart, $monthEnd, $suspensions);
|
||||
$months += $suspendedDays / $daysInMonth;
|
||||
|
||||
$cursor = $cursor->modify('first day of next month');
|
||||
}
|
||||
|
||||
return $months;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<ContractSuspension>
|
||||
*/
|
||||
private function resolveSuspensionsForEmployeePeriod(Employee $employee, DateTimeImmutable $from, DateTimeImmutable $to): array
|
||||
{
|
||||
$suspensions = [];
|
||||
foreach ($employee->getContractPeriods() as $period) {
|
||||
$periodStart = $period->getStartDate();
|
||||
$periodEnd = $period->getEndDate();
|
||||
|
||||
if ($periodStart > $to) {
|
||||
continue;
|
||||
}
|
||||
if ($periodEnd instanceof DateTimeImmutable && $periodEnd < $from) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($period->getSuspensions() as $suspension) {
|
||||
$suspensions[] = $suspension;
|
||||
}
|
||||
}
|
||||
|
||||
return $suspensions;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{bool, bool}
|
||||
*/
|
||||
|
||||
77
src/Service/Leave/SuspensionDaysCalculator.php
Normal file
77
src/Service/Leave/SuspensionDaysCalculator.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Leave;
|
||||
|
||||
use App\Entity\ContractSuspension;
|
||||
use DateTimeImmutable;
|
||||
|
||||
final class SuspensionDaysCalculator
|
||||
{
|
||||
/**
|
||||
* Count calendar days suspended within a month window [monthStart, monthEnd].
|
||||
*
|
||||
* @param list<ContractSuspension> $suspensions
|
||||
*/
|
||||
public function countSuspendedDaysInMonth(
|
||||
DateTimeImmutable $monthStart,
|
||||
DateTimeImmutable $monthEnd,
|
||||
array $suspensions
|
||||
): int {
|
||||
$total = 0;
|
||||
|
||||
foreach ($suspensions as $suspension) {
|
||||
$sStart = $suspension->getStartDate();
|
||||
$sEnd = $suspension->getEndDate() ?? $monthEnd;
|
||||
|
||||
$overlapStart = $sStart > $monthStart ? $sStart : $monthStart;
|
||||
$overlapEnd = $sEnd < $monthEnd ? $sEnd : $monthEnd;
|
||||
|
||||
if ($overlapStart > $overlapEnd) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$total += ((int) $overlapEnd->diff($overlapStart)->format('%a')) + 1;
|
||||
}
|
||||
|
||||
return $total;
|
||||
}
|
||||
|
||||
/**
|
||||
* Count business days (Mon-Fri, excl. public holidays) suspended within a period.
|
||||
*
|
||||
* @param list<ContractSuspension> $suspensions
|
||||
* @param array<string, string> $publicHolidays map of Y-m-d => label
|
||||
*/
|
||||
public function countSuspendedBusinessDays(
|
||||
DateTimeImmutable $periodStart,
|
||||
DateTimeImmutable $periodEnd,
|
||||
array $suspensions,
|
||||
array $publicHolidays
|
||||
): int {
|
||||
$total = 0;
|
||||
|
||||
foreach ($suspensions as $suspension) {
|
||||
$sStart = $suspension->getStartDate();
|
||||
$sEnd = $suspension->getEndDate() ?? $periodEnd;
|
||||
|
||||
$overlapStart = $sStart > $periodStart ? $sStart : $periodStart;
|
||||
$overlapEnd = $sEnd < $periodEnd ? $sEnd : $periodEnd;
|
||||
|
||||
if ($overlapStart > $overlapEnd) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for ($cursor = $overlapStart; $cursor <= $overlapEnd; $cursor = $cursor->modify('+1 day')) {
|
||||
$weekDay = (int) $cursor->format('N');
|
||||
$dayKey = $cursor->format('Y-m-d');
|
||||
if ($weekDay <= 5 && !isset($publicHolidays[$dayKey])) {
|
||||
++$total;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $total;
|
||||
}
|
||||
}
|
||||
122
src/State/ContractSuspensionWriteProcessor.php
Normal file
122
src/State/ContractSuspensionWriteProcessor.php
Normal file
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\State;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProcessorInterface;
|
||||
use App\Entity\ContractSuspension;
|
||||
use App\Entity\EmployeeContractPeriod;
|
||||
use DateTimeImmutable;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
||||
|
||||
final readonly class ContractSuspensionWriteProcessor implements ProcessorInterface
|
||||
{
|
||||
public function __construct(
|
||||
#[Autowire(service: 'api_platform.doctrine.orm.state.persist_processor')]
|
||||
private ProcessorInterface $persistProcessor,
|
||||
private EntityManagerInterface $entityManager,
|
||||
) {}
|
||||
|
||||
public function process(
|
||||
mixed $data,
|
||||
Operation $operation,
|
||||
array $uriVariables = [],
|
||||
array $context = []
|
||||
): mixed {
|
||||
if (!$data instanceof ContractSuspension) {
|
||||
return $this->persistProcessor->process($data, $operation, $uriVariables, $context);
|
||||
}
|
||||
|
||||
$period = $data->getContractPeriod();
|
||||
|
||||
if (!$period instanceof EmployeeContractPeriod && null !== $data->getContractPeriodId()) {
|
||||
$period = $this->entityManager->find(EmployeeContractPeriod::class, $data->getContractPeriodId());
|
||||
if ($period instanceof EmployeeContractPeriod) {
|
||||
$data->setContractPeriod($period);
|
||||
}
|
||||
}
|
||||
|
||||
if (!$period instanceof EmployeeContractPeriod) {
|
||||
throw new UnprocessableEntityHttpException('contractPeriodId is required.');
|
||||
}
|
||||
|
||||
$this->validate($data, $period);
|
||||
|
||||
return $this->persistProcessor->process($data, $operation, $uriVariables, $context);
|
||||
}
|
||||
|
||||
private function validate(ContractSuspension $suspension, EmployeeContractPeriod $period): void
|
||||
{
|
||||
// Compare as Y-m-d strings to avoid timezone issues between Doctrine and API Platform DateTimeImmutable
|
||||
$startDate = $suspension->getStartDate()->format('Y-m-d');
|
||||
$endDate = $suspension->getEndDate()?->format('Y-m-d');
|
||||
$periodStart = $period->getStartDate()->format('Y-m-d');
|
||||
$periodEnd = $period->getEndDate()?->format('Y-m-d');
|
||||
|
||||
if (null !== $periodEnd && $periodEnd < new DateTimeImmutable('today')->format('Y-m-d')) {
|
||||
throw new UnprocessableEntityHttpException('Impossible de suspendre une période de contrat clôturée.');
|
||||
}
|
||||
|
||||
if (null !== $endDate && $endDate < $startDate) {
|
||||
throw new UnprocessableEntityHttpException('La date de fin doit être postérieure à la date de début.');
|
||||
}
|
||||
|
||||
if ($startDate < $periodStart) {
|
||||
throw new UnprocessableEntityHttpException('La suspension ne peut pas commencer avant le début du contrat.');
|
||||
}
|
||||
|
||||
if (null !== $periodEnd) {
|
||||
if ($startDate > $periodEnd) {
|
||||
throw new UnprocessableEntityHttpException('La suspension ne peut pas commencer après la fin du contrat.');
|
||||
}
|
||||
if (null !== $endDate && $endDate > $periodEnd) {
|
||||
throw new UnprocessableEntityHttpException('La suspension ne peut pas se terminer après la fin du contrat.');
|
||||
}
|
||||
}
|
||||
|
||||
$this->validateNoOverlap($suspension, $period);
|
||||
}
|
||||
|
||||
private function validateNoOverlap(ContractSuspension $suspension, EmployeeContractPeriod $period): void
|
||||
{
|
||||
$start = $suspension->getStartDate()->format('Y-m-d');
|
||||
$end = $suspension->getEndDate()?->format('Y-m-d');
|
||||
|
||||
foreach ($period->getSuspensions() as $existing) {
|
||||
if ($existing->getId() === $suspension->getId() && null !== $suspension->getId()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$existingStart = $existing->getStartDate()->format('Y-m-d');
|
||||
$existingEnd = $existing->getEndDate()?->format('Y-m-d');
|
||||
|
||||
if (null === $end && null === $existingEnd) {
|
||||
throw new UnprocessableEntityHttpException('Les suspensions ne peuvent pas se chevaucher.');
|
||||
}
|
||||
|
||||
if (null === $end) {
|
||||
if ($start <= $existingEnd) {
|
||||
throw new UnprocessableEntityHttpException('Les suspensions ne peuvent pas se chevaucher.');
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (null === $existingEnd) {
|
||||
if ($existingStart <= $end) {
|
||||
throw new UnprocessableEntityHttpException('Les suspensions ne peuvent pas se chevaucher.');
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($start <= $existingEnd && $end >= $existingStart) {
|
||||
throw new UnprocessableEntityHttpException('Les suspensions ne peuvent pas se chevaucher.');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProviderInterface;
|
||||
use App\ApiResource\EmployeeLeaveSummary;
|
||||
use App\Entity\Absence;
|
||||
use App\Entity\ContractSuspension;
|
||||
use App\Entity\Employee;
|
||||
use App\Entity\User;
|
||||
use App\Enum\ContractNature;
|
||||
@@ -17,8 +18,10 @@ use App\Repository\AbsenceRepository;
|
||||
use App\Repository\EmployeeContractPeriodRepository;
|
||||
use App\Repository\EmployeeLeaveBalanceRepository;
|
||||
use App\Repository\EmployeeRepository;
|
||||
use App\Repository\WorkHourRepository;
|
||||
use App\Security\EmployeeScopeService;
|
||||
use App\Service\Leave\LeaveBalanceComputationService;
|
||||
use App\Service\Leave\SuspensionDaysCalculator;
|
||||
use App\Service\PublicHolidayServiceInterface;
|
||||
use DateTimeImmutable;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
@@ -50,6 +53,8 @@ final readonly class EmployeeLeaveSummaryProvider implements ProviderInterface
|
||||
private EmployeeLeaveBalanceRepository $leaveBalanceRepository,
|
||||
private LeaveBalanceComputationService $leaveBalanceComputationService,
|
||||
private PublicHolidayServiceInterface $publicHolidayService,
|
||||
private SuspensionDaysCalculator $suspensionDaysCalculator,
|
||||
private WorkHourRepository $workHourRepository,
|
||||
) {}
|
||||
|
||||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): EmployeeLeaveSummary
|
||||
@@ -97,6 +102,9 @@ final readonly class EmployeeLeaveSummaryProvider implements ProviderInterface
|
||||
$summary->remainingDays = $yearSummary['remainingDays'] + $fractionedDays;
|
||||
$summary->remainingSaturdays = $yearSummary['remainingSaturdays'];
|
||||
|
||||
[$periodFrom, $periodTo] = $this->resolvePeriodBounds($employee, $year);
|
||||
$summary->presenceDaysByMonth = $this->workHourRepository->countPresenceDaysByMonth($employee, $periodFrom, $periodTo);
|
||||
|
||||
return $summary;
|
||||
}
|
||||
|
||||
@@ -170,12 +178,14 @@ final readonly class EmployeeLeaveSummaryProvider implements ProviderInterface
|
||||
|
||||
$accrualCalculationEnd = $this->resolveAccrualCalculationEndDate($leavePolicy['ruleCode'], $year, $to, $employee);
|
||||
$takenCalculationEnd = $this->resolveTakenCalculationEndDate($to, $employee);
|
||||
$suspensions = $this->resolveSuspensionsForPeriod($employee, $effectiveFrom, $to);
|
||||
$generatedDays = $leavePolicy['accrualPerMonth'] > 0.0
|
||||
? $this->computeAccruedDaysFromStart(
|
||||
$leavePolicy['acquiredDays'],
|
||||
$leavePolicy['accrualPerMonth'],
|
||||
$effectiveFrom,
|
||||
$accrualCalculationEnd
|
||||
$accrualCalculationEnd,
|
||||
$suspensions
|
||||
)
|
||||
: 0.0;
|
||||
$generatedSaturdays = $leavePolicy['saturdayAccrualPerMonth'] > 0.0
|
||||
@@ -183,7 +193,8 @@ final readonly class EmployeeLeaveSummaryProvider implements ProviderInterface
|
||||
$leavePolicy['acquiredSaturdays'],
|
||||
$leavePolicy['saturdayAccrualPerMonth'],
|
||||
$effectiveFrom,
|
||||
$accrualCalculationEnd
|
||||
$accrualCalculationEnd,
|
||||
$suspensions
|
||||
)
|
||||
: 0.0;
|
||||
$absences = $this->absenceRepository->findByEmployeeAndOverlappingDateRange($employee, $from, $to);
|
||||
@@ -224,7 +235,16 @@ final readonly class EmployeeLeaveSummaryProvider implements ProviderInterface
|
||||
$previousRemainingSaturdays = $remainingAcquiredSaturdays + $remainingGeneratedSaturdays;
|
||||
} else {
|
||||
// Forfait: no "en cours d'acquisition" counter, all rights are in acquired.
|
||||
$acquiredDays = $carryDays + $leavePolicy['acquiredDays'];
|
||||
$acquiredDays = $carryDays + $leavePolicy['acquiredDays'];
|
||||
$suspensions = $this->resolveSuspensionsForPeriod($employee, $from, $to);
|
||||
if ([] !== $suspensions) {
|
||||
$totalMonths = $this->countFractionalMonths($from, $to);
|
||||
$suspendedMonths = $this->countSuspendedFractionalMonths($from, $to, $suspensions);
|
||||
if ($totalMonths > 0) {
|
||||
$ratio = max(0.0, ($totalMonths - $suspendedMonths) / $totalMonths);
|
||||
$acquiredDays = $carryDays + $leavePolicy['acquiredDays'] * $ratio;
|
||||
}
|
||||
}
|
||||
$accruingDays = 0.0;
|
||||
$remainingDays = max(0.0, $acquiredDays - $takenDays);
|
||||
$acquiredSaturdays = 0.0;
|
||||
@@ -334,7 +354,8 @@ final readonly class EmployeeLeaveSummaryProvider implements ProviderInterface
|
||||
float $acquiredDays,
|
||||
float $accrualPerMonth,
|
||||
DateTimeImmutable $periodStart,
|
||||
?DateTimeImmutable $periodEnd
|
||||
?DateTimeImmutable $periodEnd,
|
||||
array $suspensions = []
|
||||
): float {
|
||||
if ($accrualPerMonth <= 0.0) {
|
||||
return $acquiredDays;
|
||||
@@ -356,6 +377,10 @@ final readonly class EmployeeLeaveSummaryProvider implements ProviderInterface
|
||||
}
|
||||
|
||||
$coveredDays = ((int) $monthEnd->diff($monthStart)->format('%a')) + 1;
|
||||
if ([] !== $suspensions) {
|
||||
$suspendedDays = $this->suspensionDaysCalculator->countSuspendedDaysInMonth($monthStart, $monthEnd, $suspensions);
|
||||
$coveredDays = max(0, $coveredDays - $suspendedDays);
|
||||
}
|
||||
$daysInMonth = (int) $cursor->format('t');
|
||||
$coveredMonths += $coveredDays / $daysInMonth;
|
||||
|
||||
@@ -706,6 +731,80 @@ final readonly class EmployeeLeaveSummaryProvider implements ProviderInterface
|
||||
return [$takenDays, $takenSaturdays];
|
||||
}
|
||||
|
||||
private function countFractionalMonths(DateTimeImmutable $from, DateTimeImmutable $to): float
|
||||
{
|
||||
$from = $this->normalizeDate($from);
|
||||
$to = $this->normalizeDate($to);
|
||||
$months = 0.0;
|
||||
$cursor = $from->modify('first day of this month')->setTime(0, 0);
|
||||
|
||||
while ($cursor <= $to) {
|
||||
$monthStart = $cursor > $from ? $cursor : $from;
|
||||
$monthEnd = $cursor->modify('last day of this month')->setTime(0, 0);
|
||||
if ($monthEnd > $to) {
|
||||
$monthEnd = $to;
|
||||
}
|
||||
$coveredDays = ((int) $monthEnd->diff($monthStart)->format('%a')) + 1;
|
||||
$daysInMonth = (int) $cursor->format('t');
|
||||
$months += $coveredDays / $daysInMonth;
|
||||
|
||||
$cursor = $cursor->modify('first day of next month');
|
||||
}
|
||||
|
||||
return $months;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<ContractSuspension> $suspensions
|
||||
*/
|
||||
private function countSuspendedFractionalMonths(DateTimeImmutable $from, DateTimeImmutable $to, array $suspensions): float
|
||||
{
|
||||
$from = $this->normalizeDate($from);
|
||||
$to = $this->normalizeDate($to);
|
||||
$months = 0.0;
|
||||
$cursor = $from->modify('first day of this month')->setTime(0, 0);
|
||||
|
||||
while ($cursor <= $to) {
|
||||
$monthStart = $cursor > $from ? $cursor : $from;
|
||||
$monthEnd = $cursor->modify('last day of this month')->setTime(0, 0);
|
||||
if ($monthEnd > $to) {
|
||||
$monthEnd = $to;
|
||||
}
|
||||
$daysInMonth = (int) $cursor->format('t');
|
||||
$suspendedDays = $this->suspensionDaysCalculator->countSuspendedDaysInMonth($monthStart, $monthEnd, $suspensions);
|
||||
$months += $suspendedDays / $daysInMonth;
|
||||
|
||||
$cursor = $cursor->modify('first day of next month');
|
||||
}
|
||||
|
||||
return $months;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<ContractSuspension>
|
||||
*/
|
||||
private function resolveSuspensionsForPeriod(Employee $employee, DateTimeImmutable $from, DateTimeImmutable $to): array
|
||||
{
|
||||
$suspensions = [];
|
||||
foreach ($employee->getContractPeriods() as $period) {
|
||||
$periodStart = $period->getStartDate();
|
||||
$periodEnd = $period->getEndDate();
|
||||
|
||||
if ($periodStart > $to) {
|
||||
continue;
|
||||
}
|
||||
if ($periodEnd instanceof DateTimeImmutable && $periodEnd < $from) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($period->getSuspensions() as $suspension) {
|
||||
$suspensions[] = $suspension;
|
||||
}
|
||||
}
|
||||
|
||||
return $suspensions;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{bool, bool}
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user