src/Entity/Plan/PlanDay.php line 50

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Plan;
  3. use ApiPlatform\Core\Annotation\ApiFilter;
  4. use ApiPlatform\Core\Annotation\ApiResource;
  5. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\DateFilter;
  6. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  7. use App\Dto\PlanDayFillInput;
  8. use App\Dto\PlanDayFillOutput;
  9. use App\Entity\User;
  10. use App\Repository\PlanDayRepository;
  11. use Doctrine\ORM\Mapping as ORM;
  12. use Symfony\Component\Serializer\Annotation\Groups;
  13. use Symfony\Component\Validator\Constraints as Assert;
  14. /**
  15.  * Запланированный день календаря для пользователя.
  16.  */
  17. #[ApiResource(
  18.     collectionOperations: [
  19.         'get' => ['security' => "is_granted('ROLE_HR') or is_granted('ROLE_MASTER_MANAGER')"],
  20.         'post' => ['security' => "is_granted('ROLE_HR') or is_granted('ROLE_MASTER_MANAGER')"],
  21.         self::SET_PLAN_DAY_FOR_ALL_USERS => [
  22.             'method' => 'POST',
  23.             'status' => 202,
  24.             'path' => 'plan-days/fill',
  25.             'input' => PlanDayFillInput::class,
  26.             'output' => PlanDayFillOutput::class,
  27.             'openapi_context' => [
  28.                 'summary' => 'Mass set planDays for all Users',
  29.             ],
  30.         ],
  31.     ],
  32.     itemOperations: [
  33.         'get' => ['security' => "is_granted('ROLE_HR') or is_granted('ROLE_MASTER_MANAGER')"],
  34.         'put' => ['security' => "is_granted('ROLE_HR') or is_granted('ROLE_MASTER_MANAGER')"],
  35.         'delete' => ['security' => "is_granted('ROLE_HR') or is_granted('ROLE_MASTER_MANAGER')"],
  36.     ],
  37. )]
  38. #[ApiFilter(filterClassSearchFilter::class, properties: [
  39.     'user.slug' => SearchFilter::STRATEGY_EXACT,
  40.     'nonWorkingType' => SearchFilter::STRATEGY_EXACT,
  41. ])]
  42. #[ApiFilter(DateFilter::class, properties: ['day'])]
  43. #[ORM\Entity(repositoryClassPlanDayRepository::class)]
  44. #[ORM\Table(name'plan_day')]
  45. #[ORM\Index(name'idx_plan_day_day'columns: ['day'])]
  46. #[ORM\Index(name'idx_plan_day_non_working_type'columns: ['non_working_type'])]
  47. class PlanDay
  48. {
  49.     public const WORKING_TYPE_WORKDAY 'workday';
  50.     public const WORKING_TYPE_TIME_OFF 'time_off';
  51.     public const WORKING_TYPE_VACATION 'vacation';
  52.     public const WORKING_TYPE_SICK_LEAVE 'sick_leave';
  53.     public const WORKING_TYPE_NOT_WORKDAY 'not_workday';
  54.     public const WORKING_TYPE_NOT_PLANNED 'not_planned';
  55.     public const SET_PLAN_DAY_FOR_ALL_USERS 'set-for-all-users';
  56.     public const CACHE_MESSAGE_PREFIX 'planDayMessage.';
  57.     public const DAY_TYPES = [
  58.         'workday' => self::WORKING_TYPE_WORKDAY,
  59.         'time_off' => self::WORKING_TYPE_TIME_OFF,
  60.         'vacation' => self::WORKING_TYPE_VACATION,
  61.         'sick_leave' => self::WORKING_TYPE_SICK_LEAVE,
  62.         'not_workday' => self::WORKING_TYPE_NOT_WORKDAY,
  63.         'not_planned' => self::WORKING_TYPE_NOT_PLANNED,
  64.     ];
  65.     public const DAY_TYPES_LABELS = [
  66.         self::WORKING_TYPE_WORKDAY => 'workday',
  67.         self::WORKING_TYPE_TIME_OFF => 'time_off',
  68.         self::WORKING_TYPE_VACATION => 'vacation',
  69.         self::WORKING_TYPE_SICK_LEAVE => 'sick_leave',
  70.         self::WORKING_TYPE_NOT_WORKDAY => 'not_workday',
  71.         self::WORKING_TYPE_NOT_PLANNED => 'not_planned',
  72.     ];
  73.     public const READ_GROUP 'PlanDay:read';
  74.     public const WRITE_GROUP 'PlanDay:write';
  75.     #[ORM\Id]
  76.     #[ORM\GeneratedValue]
  77.     #[ORM\Column(type'integer')]
  78.     private ?int $id;
  79.     /* Дата */
  80.     #[Assert\NotBlank]
  81.     #[Groups([self::READ_GROUPself::WRITE_GROUP])]
  82.     #[ORM\Column(type'date')]
  83.     private ?\DateTime $day;
  84.     /* Норма рабочих часов */
  85.     #[Assert\NotBlank]
  86.     #[Groups([self::READ_GROUPself::WRITE_GROUP])]
  87.     #[ORM\Column(type'float')]
  88.     private ?float $normTime 0;
  89.     /* тип дня (рабочий, отпуск, больничный) */
  90.     #[Groups([self::READ_GROUPself::WRITE_GROUP])]
  91.     #[Assert\Choice(choicesself::DAY_TYPES)]
  92.     #[ORM\Column(type'string'length255nullabletrue)]
  93.     private ?string $nonWorkingType self::WORKING_TYPE_WORKDAY;
  94.     #[Assert\NotBlank]
  95.     #[Groups([self::READ_GROUPself::WRITE_GROUP])]
  96.     #[ORM\ManyToOne(targetEntity'App\Entity\User'inversedBy'planDay'cascade: ['persist'])]
  97.     #[ORM\JoinColumn(name'hub_user_id'nullablefalse)]
  98.     private User $user;
  99.     public function getId(): ?int
  100.     {
  101.         return $this->id;
  102.     }
  103.     public function getDay(): ?\DateTime
  104.     {
  105.         return $this->day;
  106.     }
  107.     public function setDay(\DateTime $day): self
  108.     {
  109.         $this->day $day;
  110.         return $this;
  111.     }
  112.     public function getNormTime(): ?float
  113.     {
  114.         return $this->normTime;
  115.     }
  116.     public function setNormTime(float $normTime): self
  117.     {
  118.         $this->normTime $normTime;
  119.         return $this;
  120.     }
  121.     public function getNonWorkingType(): ?string
  122.     {
  123.         return $this->nonWorkingType;
  124.     }
  125.     public function setNonWorkingType(?string $nonWorkingType): self
  126.     {
  127.         $this->nonWorkingType $nonWorkingType;
  128.         return $this;
  129.     }
  130.     public function getUser(): ?User
  131.     {
  132.         return $this->user;
  133.     }
  134.     public function setUser(User $user): self
  135.     {
  136.         $this->user $user;
  137.         return $this;
  138.     }
  139.     public function __toString(): string
  140.     {
  141.         $day $this->getDay();
  142.         $dayStr = ($day instanceof \DateTimeInterface) ? $day->format(\DateTimeInterface::ATOM) : '';
  143.         return $dayStr ' ' $this->normTime ' ' $this->nonWorkingType;
  144.     }
  145. }