<?php
namespace App\Entity\Plan;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\DateFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use App\Dto\PlanDayFillInput;
use App\Dto\PlanDayFillOutput;
use App\Entity\User;
use App\Repository\PlanDayRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Запланированный день календаря для пользователя.
*/
#[ApiResource(
collectionOperations: [
'get' => ['security' => "is_granted('ROLE_HR') or is_granted('ROLE_MASTER_MANAGER')"],
'post' => ['security' => "is_granted('ROLE_HR') or is_granted('ROLE_MASTER_MANAGER')"],
self::SET_PLAN_DAY_FOR_ALL_USERS => [
'method' => 'POST',
'status' => 202,
'path' => 'plan-days/fill',
'input' => PlanDayFillInput::class,
'output' => PlanDayFillOutput::class,
'openapi_context' => [
'summary' => 'Mass set planDays for all Users',
],
],
],
itemOperations: [
'get' => ['security' => "is_granted('ROLE_HR') or is_granted('ROLE_MASTER_MANAGER')"],
'put' => ['security' => "is_granted('ROLE_HR') or is_granted('ROLE_MASTER_MANAGER')"],
'delete' => ['security' => "is_granted('ROLE_HR') or is_granted('ROLE_MASTER_MANAGER')"],
],
)]
#[ApiFilter(filterClass: SearchFilter::class, properties: [
'user.slug' => SearchFilter::STRATEGY_EXACT,
'nonWorkingType' => SearchFilter::STRATEGY_EXACT,
])]
#[ApiFilter(DateFilter::class, properties: ['day'])]
#[ORM\Entity(repositoryClass: PlanDayRepository::class)]
#[ORM\Table(name: 'plan_day')]
#[ORM\Index(name: 'idx_plan_day_day', columns: ['day'])]
#[ORM\Index(name: 'idx_plan_day_non_working_type', columns: ['non_working_type'])]
class PlanDay
{
public const WORKING_TYPE_WORKDAY = 'workday';
public const WORKING_TYPE_TIME_OFF = 'time_off';
public const WORKING_TYPE_VACATION = 'vacation';
public const WORKING_TYPE_SICK_LEAVE = 'sick_leave';
public const WORKING_TYPE_NOT_WORKDAY = 'not_workday';
public const WORKING_TYPE_NOT_PLANNED = 'not_planned';
public const SET_PLAN_DAY_FOR_ALL_USERS = 'set-for-all-users';
public const CACHE_MESSAGE_PREFIX = 'planDayMessage.';
public const DAY_TYPES = [
'workday' => self::WORKING_TYPE_WORKDAY,
'time_off' => self::WORKING_TYPE_TIME_OFF,
'vacation' => self::WORKING_TYPE_VACATION,
'sick_leave' => self::WORKING_TYPE_SICK_LEAVE,
'not_workday' => self::WORKING_TYPE_NOT_WORKDAY,
'not_planned' => self::WORKING_TYPE_NOT_PLANNED,
];
public const DAY_TYPES_LABELS = [
self::WORKING_TYPE_WORKDAY => 'workday',
self::WORKING_TYPE_TIME_OFF => 'time_off',
self::WORKING_TYPE_VACATION => 'vacation',
self::WORKING_TYPE_SICK_LEAVE => 'sick_leave',
self::WORKING_TYPE_NOT_WORKDAY => 'not_workday',
self::WORKING_TYPE_NOT_PLANNED => 'not_planned',
];
public const READ_GROUP = 'PlanDay:read';
public const WRITE_GROUP = 'PlanDay:write';
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private ?int $id;
/* Дата */
#[Assert\NotBlank]
#[Groups([self::READ_GROUP, self::WRITE_GROUP])]
#[ORM\Column(type: 'date')]
private ?\DateTime $day;
/* Норма рабочих часов */
#[Assert\NotBlank]
#[Groups([self::READ_GROUP, self::WRITE_GROUP])]
#[ORM\Column(type: 'float')]
private ?float $normTime = 0;
/* тип дня (рабочий, отпуск, больничный) */
#[Groups([self::READ_GROUP, self::WRITE_GROUP])]
#[Assert\Choice(choices: self::DAY_TYPES)]
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private ?string $nonWorkingType = self::WORKING_TYPE_WORKDAY;
#[Assert\NotBlank]
#[Groups([self::READ_GROUP, self::WRITE_GROUP])]
#[ORM\ManyToOne(targetEntity: 'App\Entity\User', inversedBy: 'planDay', cascade: ['persist'])]
#[ORM\JoinColumn(name: 'hub_user_id', nullable: false)]
private User $user;
public function getId(): ?int
{
return $this->id;
}
public function getDay(): ?\DateTime
{
return $this->day;
}
public function setDay(\DateTime $day): self
{
$this->day = $day;
return $this;
}
public function getNormTime(): ?float
{
return $this->normTime;
}
public function setNormTime(float $normTime): self
{
$this->normTime = $normTime;
return $this;
}
public function getNonWorkingType(): ?string
{
return $this->nonWorkingType;
}
public function setNonWorkingType(?string $nonWorkingType): self
{
$this->nonWorkingType = $nonWorkingType;
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(User $user): self
{
$this->user = $user;
return $this;
}
public function __toString(): string
{
$day = $this->getDay();
$dayStr = ($day instanceof \DateTimeInterface) ? $day->format(\DateTimeInterface::ATOM) : '';
return $dayStr . ' ' . $this->normTime . ' ' . $this->nonWorkingType;
}
}