src/Entity/TimeNormalization/WorkPosition.php line 50

Open in your IDE?
  1. <?php
  2. namespace App\Entity\TimeNormalization;
  3. use ApiPlatform\Core\Annotation\ApiFilter;
  4. use ApiPlatform\Core\Annotation\ApiResource;
  5. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\ExistsFilter;
  6. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  7. use App\Entity\Unit;
  8. use App\Entity\User;
  9. use App\Repository\WorkPositionRepository;
  10. use App\Validator\IntersectionOfPeriods;
  11. use App\Validator\OpenPeriodExist;
  12. use Doctrine\ORM\Mapping as ORM;
  13. use Symfony\Component\Serializer\Annotation\Groups;
  14. use Symfony\Component\Validator\Constraints as Assert;
  15. #[ApiResource(
  16.     collectionOperations: [
  17.         'get' => ['security' => "is_granted('ROLE_HR') or is_granted('ROLE_MASTER_MANAGER')"],
  18.         'post' => ['security' => "is_granted('ROLE_HR') or is_granted('ROLE_MASTER_MANAGER')",
  19.             'openapi_context' => WorkPosition::OPENAPI_DOC,
  20.         ],
  21.     ],
  22.     itemOperations: [
  23.         'get' => ['security' => "is_granted('ROLE_HR') or is_granted('ROLE_MASTER_MANAGER')"],
  24.         'put' => ['security' => "is_granted('ROLE_HR') or is_granted('ROLE_MASTER_MANAGER')",
  25.             'openapi_context' => WorkPosition::OPENAPI_DOC,
  26.         ],
  27.         'delete' => ['security' => "is_granted('ROLE_HR') or is_granted('ROLE_MASTER_MANAGER')",
  28.             'openapi_context' => WorkPosition::OPENAPI_DOC,
  29.         ],
  30.     ],
  31.     order: ['datePosition' => 'ASC'],
  32. )]
  33. #[ApiFilter(filterClassSearchFilter::class, properties: [
  34.     'user.slug' => SearchFilter::STRATEGY_EXACT,
  35.     'unit.slug' => SearchFilter::STRATEGY_EXACT,
  36.     'contractType' => SearchFilter::STRATEGY_EXACT,
  37. ])]
  38. #[ApiFilter(ExistsFilter::class, properties: ['datePositionEnd'])]
  39. #[ORM\Entity(repositoryClassWorkPositionRepository::class)]
  40. #[ORM\Table(name'work_position')]
  41. #[ORM\HasLifecycleCallbacks]
  42. #[ORM\Index(name'idx_date_position'columns: ['date_position'])]
  43. #[ORM\Index(name'idx_contract_type'columns: ['contract_type'])]
  44. #[ORM\Index(name'idx_date_position_end'columns: ['date_position_end'])]
  45. #[OpenPeriodExist(toDate'endBefore'filter: ['user'])]
  46. #[IntersectionOfPeriods(fromDate'datePosition'toDate'endBefore'filter: ['user'])]
  47. class WorkPosition
  48. {
  49.     public const CONTRACT_TYPE_LABOR 'labor_contract';
  50.     public const CONTRACT_TYPE_SELF 'self_employed_contract';
  51.     public const CONTRACT_TYPE_FOREIGNER 'foreigner_contract';
  52.     public const STATUS_IN_WORK 'in_work';
  53.     public const STATUS_DISSMISSES 'dismissed';
  54.     public const STATUS_UNPAID_LEAVE 'unpaid_leave';
  55.     public const STATUS_MATERNITY_LEAVE 'maternity_leave';
  56.     public const READ_GROUP 'WorkPosition:read';
  57.     public const WRITE_GROUP 'WorkPosition:write';
  58.     public const AUTOMATED_STATUSES = [
  59.         self::STATUS_IN_WORK,
  60.         self::STATUS_DISSMISSES,
  61.     ];
  62.     public const CONTRACT_TYPES = [
  63.         'labor_contract' => self::CONTRACT_TYPE_LABOR,
  64.         'self_employed_contract' => self::CONTRACT_TYPE_SELF,
  65.         'foreigner_contract' => self::CONTRACT_TYPE_FOREIGNER,
  66.     ];
  67.     public const STATUSES = [
  68.         'in_work' => self::STATUS_IN_WORK,
  69.         'dismissed' => self::STATUS_DISSMISSES,
  70.         'unpaid_leave' => self::STATUS_UNPAID_LEAVE,
  71.         'maternity_leave' => self::STATUS_MATERNITY_LEAVE,
  72.     ];
  73.     private const OPENAPI_DOC = [
  74.         'summary' => 'recalculate PlanDays for WorkPosition',
  75.         'description' => 'recalculate PlanDays for WorkPosition',
  76.         'parameters' => [['in' => 'query''type' => 'bool''name' => 'recalculatePlanDays''required' => false]],
  77.     ];
  78.     #[ORM\Id]
  79.     #[ORM\GeneratedValue]
  80.     #[ORM\Column(type'integer')]
  81.     private $id;
  82.     #[Assert\NotBlank]
  83.     #[Groups([self::READ_GROUPself::WRITE_GROUP])]
  84.     #[ORM\ManyToOne(targetEntityUser::class, inversedBy'workPositions'cascade: ['persist'])]
  85.     #[ORM\JoinColumn(nullablefalse)]
  86.     private User $user;
  87.     #[Assert\NotBlank]
  88.     #[Groups([self::READ_GROUPself::WRITE_GROUPUser::READ_GROUP])]
  89.     #[ORM\ManyToOne(targetEntityUnit::class, cascade: ['persist'])]
  90.     #[ORM\JoinColumn(nullablefalse)]
  91.     private Unit $unit;
  92.     #[Assert\NotBlank]
  93.     #[Groups([self::READ_GROUPself::WRITE_GROUPUser::READ_GROUP])]
  94.     #[ORM\Column(type'date')]
  95.     private ?\DateTimeInterface $datePosition null;
  96.     #[Assert\NotBlank]
  97.     #[Groups([self::READ_GROUPself::WRITE_GROUPUser::READ_GROUP])]
  98.     #[ORM\Column(type'string'length255)]
  99.     private ?string $jobTitle null;
  100.     #[Groups([self::READ_GROUPself::WRITE_GROUPUser::READ_GROUP])]
  101.     #[Assert\Choice(choices: [self::CONTRACT_TYPE_LABORself::CONTRACT_TYPE_SELFself::CONTRACT_TYPE_FOREIGNER])]
  102.     #[ORM\Column(type'string'length40options: ['default' => 'labor_contract'])]
  103.     private string $contractType self::CONTRACT_TYPE_LABOR;
  104.     #[Groups([self::READ_GROUPself::WRITE_GROUPUser::READ_GROUP])]
  105.     #[ORM\Column(type'float'nullabletrue)]
  106.     private ?float $normFullDay 8;
  107.     #[Groups([self::READ_GROUPself::WRITE_GROUPUser::READ_GROUP])]
  108.     #[ORM\Column(type'float'nullabletrue)]
  109.     private ?float $normShortDay 7;
  110.     /**
  111.      * дата последнего дня периода работы (вычисляется, не хранится в БД).
  112.      */
  113.     #[Assert\GreaterThan(propertyPath'datePosition'message'datePositionEnd must be greater than datePosition')]
  114.     #[Groups([self::READ_GROUPself::WRITE_GROUPUser::READ_GROUP])]
  115.     private ?\DateTimeInterface $datePositionEnd null;
  116.     /**
  117.      * дата окончания (не включается в период работы).
  118.      */
  119.     #[ORM\Column(name'date_position_end'type'date'nullabletrue)]
  120.     private ?\DateTimeInterface $endBefore null;
  121.     #[Groups([self::READ_GROUPself::WRITE_GROUPUser::READ_GROUP])]
  122.     #[Assert\Choice(choicesself::STATUSES)]
  123.     #[ORM\Column(type'string'length40options: ['default' => 'in_work'])]
  124.     private string $workingStatus self::STATUS_IN_WORK;
  125.     public function getId(): ?int
  126.     {
  127.         return $this->id;
  128.     }
  129.     public function getUser(): ?User
  130.     {
  131.         return $this->user;
  132.     }
  133.     /**
  134.      * @param User|null $user
  135.      */
  136.     public function setUser(User $user): WorkPosition
  137.     {
  138.         $this->user $user;
  139.         return $this;
  140.     }
  141.     public function getUnit(): ?Unit
  142.     {
  143.         return $this->unit;
  144.     }
  145.     /**
  146.      * @param Unit|null $unit
  147.      */
  148.     public function setUnit(Unit $unit): WorkPosition
  149.     {
  150.         $this->unit $unit;
  151.         return $this;
  152.     }
  153.     public function getDatePosition(): ?\DateTimeInterface
  154.     {
  155.         return $this->datePosition;
  156.     }
  157.     public function setDatePosition(\DateTimeInterface $datePosition): self
  158.     {
  159.         $this->datePosition $datePosition;
  160.         return $this;
  161.     }
  162.     public function getJobTitle(): ?string
  163.     {
  164.         return $this->jobTitle;
  165.     }
  166.     public function setJobTitle(string $jobTitle): self
  167.     {
  168.         $this->jobTitle $jobTitle;
  169.         return $this;
  170.     }
  171.     public function getContractType(): ?string
  172.     {
  173.         return $this->contractType;
  174.     }
  175.     public function setContractType(string $contractType): self
  176.     {
  177.         $this->contractType $contractType;
  178.         return $this;
  179.     }
  180.     public function getNormFullDay(): ?float
  181.     {
  182.         return $this->normFullDay;
  183.     }
  184.     public function setNormFullDay(?float $normFullDay): self
  185.     {
  186.         $this->normFullDay $normFullDay;
  187.         return $this;
  188.     }
  189.     public function getNormShortDay(): ?float
  190.     {
  191.         return $this->normShortDay;
  192.     }
  193.     public function setNormShortDay(?float $normShortDay): self
  194.     {
  195.         $this->normShortDay $normShortDay;
  196.         return $this;
  197.     }
  198.     public function getDatePositionEnd(): ?\DateTimeInterface
  199.     {
  200.         return $this->endBefore \DateTime::createFromInterface($this->endBefore)->modify('yesterday midnight') : null;
  201.     }
  202.     public function setDatePositionEnd(?\DateTimeInterface $datePositionEnd): self
  203.     {
  204.         $this->endBefore $datePositionEnd \DateTime::createFromInterface($datePositionEnd)->modify('tomorrow midnight') : null;
  205.         return $this;
  206.     }
  207.     public function getEndBefore(): ?\DateTimeInterface
  208.     {
  209.         return $this->endBefore;
  210.     }
  211.     public function setEndBefore(?\DateTimeInterface $endBefore): self
  212.     {
  213.         $this->endBefore $endBefore;
  214.         return $this;
  215.     }
  216.     public function getWorkingStatus(): string
  217.     {
  218.         return $this->workingStatus;
  219.     }
  220.     public function setWorkingStatus(string $workingStatus): WorkPosition
  221.     {
  222.         $this->workingStatus $workingStatus;
  223.         return $this;
  224.     }
  225.     #[ORM\PreUpdate]
  226.     #[ORM\PrePersist]
  227.     public function workingStatusCheck(): void
  228.     {
  229.         if (\in_array($this->workingStatusself::AUTOMATED_STATUSES)) {
  230.             $this->workingStatus $this->endBefore self::STATUS_DISSMISSES self::STATUS_IN_WORK;
  231.         }
  232.     }
  233. }