src/Entity/Attendance.php line 34

  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Metadata\ApiResource;
  4. use ApiPlatform\Metadata\Get;
  5. use ApiPlatform\Metadata\GetCollection;
  6. use ApiPlatform\Metadata\Post;
  7. use App\Dto\CreateAttendanceInput;
  8. use App\Kimia;
  9. use App\Repository\AttendanceRepository;
  10. use App\State\Attendance\SaveAttendanceProcessor;
  11. use App\Trait\DateTrait;
  12. use DateTimeImmutable;
  13. use DateTimeInterface;
  14. use Doctrine\DBAL\Types\Types;
  15. use Doctrine\ORM\Mapping as ORM;
  16. use Symfony\Component\Serializer\Annotation\Groups;
  17. #[ORM\Entity(repositoryClassAttendanceRepository::class)]
  18. #[ApiResource(
  19.     operations: [
  20.         new GetCollection(
  21.             normalizationContext: ['groups' => ['attendance:list']]
  22.         ),
  23.         new Get(),
  24.         new Post(
  25.             inputCreateAttendanceInput::class,
  26.             processorSaveAttendanceProcessor::class
  27.         ),
  28.     ],
  29.     normalizationContext: ['groups' => ['attendance:list''attendance:view']]
  30. )]
  31. class Attendance
  32. {
  33.     use DateTrait;
  34.     const TYPE_EMPLOYEE "EMP";
  35.     const TYPE_CLIENT "CLT";
  36.     #[ORM\Id]
  37.     #[ORM\GeneratedValue]
  38.     #[ORM\Column]
  39.     #[Groups(['attendance:list'])]
  40.     private ?int $id null;
  41.     #[ORM\ManyToOne(inversedBy'attendances')]
  42.     #[ORM\JoinColumn(nullabletrue)]
  43.     private ?Employee $employee null;
  44.     #[ORM\ManyToOne(inversedBy'attendances')]
  45.     private ?Client $client null;
  46.     #[ORM\ManyToOne]
  47.     #[ORM\JoinColumn(nullablefalse)]
  48.     private ?Establishment $establishment null;
  49.     #[ORM\Column(typeTypes::DATETIME_MUTABLEnullabletrue)]
  50.     #[Groups(['attendance:list'])]
  51.     private ?DateTimeInterface $arrivedAt null;
  52.     #[ORM\Column(typeTypes::DATETIME_MUTABLEnullabletrue)]
  53.     #[Groups(['attendance:list'])]
  54.     private ?DateTimeInterface $departedAt null;
  55.     #[ORM\Column(length255)]
  56.     #[Groups(['attendance:list'])]
  57.     private ?string $status null;
  58.     #[ORM\Column(typeTypes::TEXTnullabletrue)]
  59.     #[Groups(['attendance:list'])]
  60.     private ?string $rapport null;
  61.     #[ORM\Column(length255nullabletrue)]
  62.     private ?string $initiator null;
  63.     public function __construct()
  64.     {
  65.         if (is_null($this->createdAt)) $this->createdAt = new DateTimeImmutable();
  66.         $this->updatedAt = new DateTimeImmutable();
  67.     }
  68.     public function getId(): ?int
  69.     {
  70.         return $this->id;
  71.     }
  72.     public function getEmployee(): ?Employee
  73.     {
  74.         return $this->employee;
  75.     }
  76.     public function setEmployee(?Employee $employee): self
  77.     {
  78.         $this->employee $employee;
  79.         return $this;
  80.     }
  81.     public function getClient(): ?Client
  82.     {
  83.         return $this->client;
  84.     }
  85.     public function setClient(?Client $client): static
  86.     {
  87.         $this->client $client;
  88.         return $this;
  89.     }
  90.     #[Groups(['attendance:list'])]
  91.     public function getActor(): Employee|Client|null
  92.     {
  93.         if (!is_null($this->getEmployee()))
  94.             return $this->getEmployee();
  95.         else
  96.             return $this->getClient();
  97.     }
  98.     public function getArrivedAt(): ?DateTimeInterface
  99.     {
  100.         return $this->arrivedAt;
  101.     }
  102.     public function setArrivedAt(?DateTimeInterface $arrivedAt): self
  103.     {
  104.         $this->arrivedAt $arrivedAt;
  105.         return $this;
  106.     }
  107.     public function getDepartedAt(): ?DateTimeInterface
  108.     {
  109.         return $this->departedAt;
  110.     }
  111.     public function setDepartedAt(?DateTimeInterface $departedAt): self
  112.     {
  113.         $this->departedAt $departedAt;
  114.         return $this;
  115.     }
  116.     public function getRapport(): ?string
  117.     {
  118.         return $this->rapport;
  119.     }
  120.     public function setRapport(?string $rapport): self
  121.     {
  122.         $this->rapport $rapport;
  123.         return $this;
  124.     }
  125.     public function getStatus(): ?string
  126.     {
  127.         return $this->status;
  128.     }
  129.     public static function getStatusList(): array
  130.     {
  131.         return [
  132.             'Absent' => Kimia::ATTENDANCE_ABSENT,
  133.             'Présent' => Kimia::ATTENDANCE_PRESENT,
  134.         ];
  135.     }
  136.     public static function getStatusListForView(): array
  137.     {
  138.         return [
  139.             Kimia::ATTENDANCE_ABSENT => "<span class='badge badge-soft-danger'>Absent</span>",
  140.             Kimia::ATTENDANCE_PRESENT => "<span class='badge badge-soft-success'>Présent</span>",
  141.         ];
  142.     }
  143.     public function setStatus(string $status): self
  144.     {
  145.         $this->status $status;
  146.         return $this;
  147.     }
  148.     public function getEstablishment(): ?Establishment
  149.     {
  150.         return $this->establishment;
  151.     }
  152.     public function setEstablishment(?Establishment $establishment): self
  153.     {
  154.         $this->establishment $establishment;
  155.         return $this;
  156.     }
  157.     public function __toString(): string
  158.     {
  159.         return "Présence de {$this->getActor()?->getFullname()} du {$this->getCreatedAt()->format('d/m/Y')}";
  160.     }
  161.     public function getInitiator(): ?string
  162.     {
  163.         return $this->initiator;
  164.     }
  165.     public function setInitiator(?string $initiator): static
  166.     {
  167.         $this->initiator $initiator;
  168.         return $this;
  169.     }
  170. }