['employee:read', 'site:read']], denormalizationContext: ['groups' => ['employee:write']], paginationEnabled: false, security: "is_granted('ROLE_ADMIN')", processor: EmployeeWriteProcessor::class, )] #[ORM\Entity(repositoryClass: EmployeeRepository::class)] #[ORM\Table(name: 'employees')] class Employee { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column(type: 'integer')] #[Groups(['absence:read', 'employee:read'])] private ?int $id = null; #[ORM\Column(type: 'string', length: 100)] #[Groups(['absence:read', 'employee:read', 'employee:write'])] private string $firstName = ''; #[ORM\Column(type: 'string', length: 100)] #[Groups(['absence:read', 'employee:read', 'employee:write'])] private string $lastName = ''; #[ApiProperty(readableLink: true)] #[ORM\ManyToOne(targetEntity: Site::class)] #[ORM\JoinColumn(nullable: true)] #[Groups(['employee:read', 'employee:write'])] private ?Site $site = null; #[ApiProperty(readableLink: true)] #[ORM\ManyToOne(targetEntity: Contract::class)] #[ORM\JoinColumn(nullable: false)] #[Groups(['employee:read', 'employee:write'])] private ?Contract $contract = null; #[ORM\Column(type: 'integer', options: ['default' => 0])] #[Groups(['employee:read', 'employee:write'])] private int $displayOrder = 0; #[ORM\Column(type: 'datetime_immutable')] private DateTimeImmutable $createdAt; /** * @var Collection */ #[ORM\OneToMany(mappedBy: 'employee', targetEntity: EmployeeContractPeriod::class)] private Collection $contractPeriods; #[Groups(['employee:write'])] private ?string $contractNature = null; #[Groups(['employee:write'])] private ?string $contractStartDate = null; #[Groups(['employee:write'])] private ?string $contractEndDate = null; public function __construct() { $this->createdAt = new DateTimeImmutable(); $this->contractPeriods = new ArrayCollection(); } public function getId(): ?int { return $this->id; } public function getFirstName(): string { return $this->firstName; } public function setFirstName(string $firstName): self { $this->firstName = $firstName; return $this; } public function getLastName(): string { return $this->lastName; } public function setLastName(string $lastName): self { $this->lastName = $lastName; return $this; } public function getSite(): ?Site { return $this->site; } public function setSite(?Site $site): self { $this->site = $site; return $this; } public function getContract(): ?Contract { return $this->contract; } public function setContract(?Contract $contract): self { $this->contract = $contract; return $this; } public function getCreatedAt(): DateTimeImmutable { return $this->createdAt; } public function getDisplayOrder(): int { return $this->displayOrder; } public function setDisplayOrder(int $displayOrder): self { $this->displayOrder = $displayOrder; return $this; } public function getContractNature(): ?string { return $this->contractNature; } public function setContractNature(?string $contractNature): self { $this->contractNature = $contractNature; return $this; } public function getContractStartDate(): ?string { return $this->contractStartDate; } public function setContractStartDate(?string $contractStartDate): self { $this->contractStartDate = $contractStartDate; return $this; } public function getContractEndDate(): ?string { return $this->contractEndDate; } public function setContractEndDate(?string $contractEndDate): self { $this->contractEndDate = $contractEndDate; return $this; } #[Groups(['employee:read'])] public function getCurrentContractNature(): string { return $this->resolveCurrentContractPeriod()?->getContractNatureEnum()->value ?? ContractNature::CDI->value; } #[Groups(['employee:read'])] public function getCurrentContractStartDate(): ?string { return $this->resolveCurrentContractPeriod()?->getStartDate()->format('Y-m-d'); } #[Groups(['employee:read'])] public function getCurrentContractEndDate(): ?string { return $this->resolveCurrentContractPeriod()?->getEndDate()?->format('Y-m-d'); } /** * @return list */ #[Groups(['employee:read'])] public function getContractHistory(): array { $periods = $this->contractPeriods->toArray(); usort( $periods, static fn (EmployeeContractPeriod $a, EmployeeContractPeriod $b): int => $b->getStartDate() <=> $a->getStartDate() ); return array_map( static function (EmployeeContractPeriod $period): ContractHistoryItem { $contract = $period->getContract(); return new ContractHistoryItem( contractId: $contract?->getId(), contractName: $contract?->getName(), weeklyHours: $contract?->getWeeklyHours(), contractNature: $period->getContractNatureEnum()->value, startDate: $period->getStartDate()->format('Y-m-d'), endDate: $period->getEndDate()?->format('Y-m-d'), ); }, $periods ); } private function resolveCurrentContractPeriod(): ?EmployeeContractPeriod { $today = new DateTimeImmutable('today'); $current = null; foreach ($this->contractPeriods as $period) { if ($period->getStartDate() > $today) { continue; } $endDate = $period->getEndDate(); if (null !== $endDate && $endDate < $today) { continue; } if (null === $current || $period->getStartDate() > $current->getStartDate()) { $current = $period; } } return $current; } }