fix : ajout de la feature Heures
This commit is contained in:
@@ -7,6 +7,7 @@ namespace App\Repository;
|
||||
use App\Entity\Absence;
|
||||
use App\Entity\Employee;
|
||||
use DateTimeImmutable;
|
||||
use DateTimeInterface;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
@@ -72,4 +73,29 @@ final class AbsenceRepository extends ServiceEntityRepository
|
||||
// @var list<Absence> $absences
|
||||
return $qb->getQuery()->getResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<Absence>
|
||||
*/
|
||||
public function findByEmployeeAndDateRange(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 >= :from')
|
||||
->andWhere('a.startDate <= :to')
|
||||
->setParameter('employee', $employee)
|
||||
->setParameter('from', $fromDate)
|
||||
->setParameter('to', $toDate)
|
||||
->orderBy('a.startDate', 'ASC')
|
||||
;
|
||||
|
||||
// @var list<Absence> $absences
|
||||
return $qb->getQuery()->getResult();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,17 +8,22 @@ use ApiPlatform\Metadata\DeleteOperationInterface;
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProcessorInterface;
|
||||
use App\Entity\Absence;
|
||||
use App\Enum\HalfDay;
|
||||
use App\Repository\AbsenceRepository;
|
||||
use App\Repository\WorkHourRepository;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use DateInterval;
|
||||
use DatePeriod;
|
||||
use DateTime;
|
||||
use DateTimeImmutable;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
||||
|
||||
final readonly class AbsenceWriteProcessor implements ProcessorInterface
|
||||
{
|
||||
public function __construct(
|
||||
#[Autowire(service: 'api_platform.doctrine.orm.state.persist_processor')]
|
||||
private ProcessorInterface $persistProcessor,
|
||||
#[Autowire(service: 'api_platform.doctrine.orm.state.remove_processor')]
|
||||
private ProcessorInterface $removeProcessor,
|
||||
private EntityManagerInterface $entityManager,
|
||||
private AbsenceRepository $absenceRepository,
|
||||
private WorkHourRepository $workHourRepository,
|
||||
) {}
|
||||
|
||||
@@ -33,14 +38,140 @@ final readonly class AbsenceWriteProcessor implements ProcessorInterface
|
||||
return $data;
|
||||
}
|
||||
|
||||
if ($this->workHourRepository->hasValidatedInRange($employee, $data->getStartDate(), $data->getEndDate())) {
|
||||
if ($operation instanceof DeleteOperationInterface) {
|
||||
if ($this->workHourRepository->hasValidatedInRange($employee, $data->getStartDate(), $data->getEndDate())) {
|
||||
throw new ConflictHttpException('Impossible de modifier une absence sur une période validée.');
|
||||
}
|
||||
|
||||
$this->entityManager->remove($data);
|
||||
$this->entityManager->flush();
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
$segments = $this->expandAbsenceRange($data);
|
||||
if ([] === $segments) {
|
||||
throw new UnprocessableEntityHttpException('La période de l\'absence est invalide.');
|
||||
}
|
||||
|
||||
$from = DateTimeImmutable::createFromInterface($segments[0]['date']);
|
||||
$to = DateTimeImmutable::createFromInterface($segments[count($segments) - 1]['date']);
|
||||
|
||||
if ($this->workHourRepository->hasValidatedInRange($employee, $from, $to)) {
|
||||
throw new ConflictHttpException('Impossible de modifier une absence sur une période validée.');
|
||||
}
|
||||
|
||||
if ($operation instanceof DeleteOperationInterface) {
|
||||
return $this->removeProcessor->process($data, $operation, $uriVariables, $context);
|
||||
$existing = $this->absenceRepository->findByEmployeeAndDateRange($employee, $from, $to);
|
||||
foreach ($existing as $existingAbsence) {
|
||||
if ($existingAbsence->getId() === $data->getId()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
throw new ConflictHttpException('Cette période chevauche déjà une absence existante.');
|
||||
}
|
||||
|
||||
return $this->persistProcessor->process($data, $operation, $uriVariables, $context);
|
||||
$first = array_shift($segments);
|
||||
if (null === $first) {
|
||||
throw new UnprocessableEntityHttpException('La période de l\'absence est invalide.');
|
||||
}
|
||||
|
||||
$data
|
||||
->setStartDate($this->toMutableDate($first['date']))
|
||||
->setEndDate($this->toMutableDate($first['date']))
|
||||
->setStartHalf($first['startHalf'])
|
||||
->setEndHalf($first['endHalf'])
|
||||
;
|
||||
$this->entityManager->persist($data);
|
||||
|
||||
foreach ($segments as $segment) {
|
||||
$absence = new Absence()
|
||||
->setEmployee($employee)
|
||||
->setType($data->getType())
|
||||
->setComment($data->getComment())
|
||||
->setStartDate($this->toMutableDate($segment['date']))
|
||||
->setEndDate($this->toMutableDate($segment['date']))
|
||||
->setStartHalf($segment['startHalf'])
|
||||
->setEndHalf($segment['endHalf'])
|
||||
;
|
||||
|
||||
$this->entityManager->persist($absence);
|
||||
}
|
||||
|
||||
$this->entityManager->flush();
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<array{date: DateTimeImmutable, startHalf: HalfDay, endHalf: HalfDay}>
|
||||
*/
|
||||
private function expandAbsenceRange(Absence $absence): array
|
||||
{
|
||||
$start = DateTimeImmutable::createFromInterface($absence->getStartDate());
|
||||
$end = DateTimeImmutable::createFromInterface($absence->getEndDate());
|
||||
|
||||
if ($start > $end) {
|
||||
throw new UnprocessableEntityHttpException('La date de fin ne peut pas être avant la date de début.');
|
||||
}
|
||||
|
||||
if (
|
||||
$start->format('Y-m-d') === $end->format('Y-m-d')
|
||||
&& HalfDay::PM === $absence->getStartHalf()
|
||||
&& HalfDay::AM === $absence->getEndHalf()
|
||||
) {
|
||||
throw new UnprocessableEntityHttpException('La demi-journée de fin ne peut pas être avant la demi-journée de début.');
|
||||
}
|
||||
|
||||
$days = new DatePeriod($start, new DateInterval('P1D'), $end->modify('+1 day'));
|
||||
|
||||
$segments = [];
|
||||
foreach ($days as $day) {
|
||||
$isFirst = $day->format('Y-m-d') === $start->format('Y-m-d');
|
||||
$isLast = $day->format('Y-m-d') === $end->format('Y-m-d');
|
||||
$isSame = $isFirst && $isLast;
|
||||
|
||||
if ($isSame) {
|
||||
$segments[] = [
|
||||
'date' => $day,
|
||||
'startHalf' => $absence->getStartHalf(),
|
||||
'endHalf' => $absence->getEndHalf(),
|
||||
];
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($isFirst && HalfDay::PM === $absence->getStartHalf()) {
|
||||
$segments[] = [
|
||||
'date' => $day,
|
||||
'startHalf' => HalfDay::PM,
|
||||
'endHalf' => HalfDay::PM,
|
||||
];
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($isLast && HalfDay::AM === $absence->getEndHalf()) {
|
||||
$segments[] = [
|
||||
'date' => $day,
|
||||
'startHalf' => HalfDay::AM,
|
||||
'endHalf' => HalfDay::AM,
|
||||
];
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$segments[] = [
|
||||
'date' => $day,
|
||||
'startHalf' => HalfDay::AM,
|
||||
'endHalf' => HalfDay::PM,
|
||||
];
|
||||
}
|
||||
|
||||
return $segments;
|
||||
}
|
||||
|
||||
private function toMutableDate(DateTimeImmutable $date): DateTime
|
||||
{
|
||||
return DateTime::createFromImmutable($date);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user