src/Entity/Candidate.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Enum\Candidate\RegistrationType;
  4. use App\Enum\Candidate\Status;
  5. use App\Repository\CandidateRepository;
  6. use Doctrine\ORM\Mapping as ORM;
  7. /**
  8.  * @ORM\Entity(repositoryClass=CandidateRepository::class)
  9.  */
  10. class Candidate
  11. {
  12.     const CANDIDATE_FIELD_STATUS 'candidate_status';
  13.     const CANDIDATE_FIELD_PERSON 'candidate_person';
  14.     const CANDIDATE_FIELD_AUTHOR 'candidate_author';
  15.     const CANDIDATE_FIELD_CALENDAR_EVENT 'candidate_calendar_event';
  16.     const CANDIDATE_FIELD_CREATED_AT 'candidate_created_at';
  17.     const CANDIDATE_FIELD_APPROVED_AT 'candidate_approved_at';
  18.     /**
  19.      * @ORM\Id
  20.      * @ORM\GeneratedValue
  21.      * @ORM\Column(type="integer")
  22.      */
  23.     private $id;
  24.     /**
  25.      * @ORM\Column(type="enum", options={"values": "candidate_enum"})
  26.      */
  27.     private $status Status::STATUS_DRAFT;
  28.     /**
  29.      * @ORM\ManyToOne(targetEntity=User::class)
  30.      * @ORM\JoinColumn(nullable=true)
  31.      */
  32.     private $author;
  33.     /**
  34.      * @ORM\ManyToOne(targetEntity=Person::class)
  35.      * @ORM\JoinColumn(nullable=true)
  36.      */
  37.     private $person;
  38.     /**
  39.      * @ORM\ManyToOne(targetEntity=CalendarEvent::class)
  40.      */
  41.     private $calendarEvent;
  42.     /**
  43.      * @ORM\Column(type="datetime")
  44.      */
  45.     private $createdAt;
  46.     /**
  47.      * @ORM\Column(type="text", nullable=true)
  48.      */
  49.     private $viewerComment;
  50.     /**
  51.      * @ORM\Column(type="datetime", nullable=true)
  52.      */
  53.     private $approvedAt;
  54.     /**
  55.      * @ORM\ManyToOne(targetEntity=Image::class, cascade={"persist", "remove"})
  56.      */
  57.     private $image;
  58.     /**
  59.      * @ORM\Column(type="text", nullable=true)
  60.      */
  61.     private $comment;
  62.     /**
  63.      * @ORM\Column(type="boolean", nullable=true)
  64.      */
  65.     private $paymentDeferment;
  66.     /**
  67.      * @ORM\ManyToOne(targetEntity=User::class)
  68.      */
  69.     private $selectedAuthor;
  70.     /**
  71.      * @ORM\Column(type="text", nullable=true)
  72.      */
  73.     private $recommendations;
  74.     /**
  75.      * @ORM\ManyToOne(targetEntity=User::class)
  76.      */
  77.     private $curator;
  78.     /**
  79.      * @ORM\Column(type="datetime")
  80.      */
  81.     private $updatedAt;
  82.     /**
  83.      * Если кандидат был УЖЕ одобрен вне системы.
  84.      * @ORM\Column(type="string", length=255, nullable=true)
  85.      */
  86.     private $reviewApproveComment;
  87.     /**
  88.      * @ORM\Column(type="boolean", nullable=true)
  89.      */
  90.     private $accessByReceipt;
  91.     /**
  92.      * @ORM\Column(type="text", nullable=true)
  93.      */
  94.     private $declineComment;
  95.     /**
  96.      * @ORM\Column(type="datetime", nullable=true)
  97.      */
  98.     private $leavedAt;
  99.     /**
  100.      * @ORM\OneToOne(targetEntity=Invoice::class)
  101.      */
  102.     private $invoice;
  103.     /**
  104.      * Не-персистентное поле для загрузки нового изображения через форму
  105.      */
  106.     private $newImage;
  107.     /**
  108.      * @ORM\Column(type="enum", nullable=true, options={"values"="candidate_enum"})
  109.      */
  110.     private $registrationType;
  111.     public function __construct()
  112.     {
  113.         $this->createdAt = new \DateTime();
  114.         $this->updatedAt = new \DateTime();
  115.     }
  116.     public function __toString()
  117.     {
  118.         $ce $this->getCalendarEvent() ?: null;
  119.         return ($this->getPerson() ? $this->getPerson()->__toString() : "–") . " – "
  120.             . ($ce $ce->getNameText() : "–");
  121.     }
  122.     public function getId(): ?int
  123.     {
  124.         return $this->id;
  125.     }
  126.     public function getStatus()
  127.     {
  128.         return $this->status;
  129.     }
  130.     public function isStatusApproved(): bool
  131.     {
  132.         return $this->status === Status::STATUS_APPROVED;
  133.     }
  134.     public function isStatusDraft(): bool
  135.     {
  136.         return $this->status === Status::STATUS_DRAFT;
  137.     }
  138.     public function isStatusNew(): bool
  139.     {
  140.         return $this->status === Status::STATUS_NEW;
  141.     }
  142.     public function getCalendarEventListedVirtualStatus(bool $hasPayments): string
  143.     {
  144.         if ($this->isStatusApproved()) {
  145.             if ($hasPayments) {
  146.                 return "approved";
  147.             } else {
  148.                 return "unpaid";
  149.             }
  150.         } else if ($this->isStatusLeaved()) {
  151.             return "leaved";
  152.         } else {
  153.             return "not_active";
  154.         }
  155.     }
  156.     public function getCalendarEventListedVirtualStatusText(bool $hasPayments): string
  157.     {
  158.         if ($this->isStatusApproved()) {
  159.             if ($hasPayments) {
  160.                 return "Зачислен";
  161.             } else if ($this->accessByReceipt) {
  162.                 return "Зачислен по квитанции";
  163.             } else {
  164.                 return "Не оплачен";
  165.             }
  166.         } else if ($this->isStatusLeaved()) {
  167.             return "Выбыл с мероприятия";
  168.         } else if ($this->isStatusRefused()) {
  169.             return "Не пошел";
  170.         } else {
  171.             return "Не активен";
  172.         }
  173.     }
  174.     public function getCalendarEventListedVirtualStatusCssClass(bool $hasPayments): string
  175.     {
  176.         if ($this->isStatusApproved()) {
  177.             if ($hasPayments || $this->accessByReceipt) {
  178.                 return "success";
  179.             } else {
  180.                 return "light";
  181.             }
  182.         } else if ($this->isStatusLeaved()) {
  183.             return "warning";
  184.         } else {
  185.             return "danger";
  186.         }
  187.     }
  188.     public function isStatusDeclined(): bool
  189.     {
  190.         return $this->status === Status::STATUS_DECLINED;
  191.     }
  192.     public function isStatusRefused(): bool
  193.     {
  194.         return $this->status === Status::STATUS_REFUSED;
  195.     }
  196.     public function isStatusDeleted(): bool
  197.     {
  198.         return $this->status === Status::STATUS_DELETED;
  199.     }
  200.     public function setStatus($status): self
  201.     {
  202.         if ($this->status != Status::STATUS_APPROVED &&
  203.             $this->status != Status::STATUS_REFUSED &&
  204.             $status == Status::STATUS_REFUSED) {
  205.             throw new \InvalidArgumentException("Нельзя поменять статус кандидата на Не пошел, "
  206.             "текущий статус должен быть Принят, т.к. под статусом Не пошел подразумевается, что кандидат прошел отбор.");
  207.         }
  208.         $this->status $status;
  209.         if ($status == Status::STATUS_APPROVED && !$this->getApprovedAt()) {
  210.             $this->setApprovedAt(new \DateTime());
  211.         } elseif ($status == Status::STATUS_LEAVED && !$this->getLeavedAt()) {
  212.             $this->setLeavedAt(new \DateTime());
  213.         }
  214.         return $this;
  215.     }
  216.     public function getAuthor(): ?User
  217.     {
  218.         // if ($this->getSelectedAuthor()) {
  219.         //     return $this->getSelectedAuthor();
  220.         // }
  221.         return $this->author;
  222.     }
  223.     public function setAuthor(?User $author): self
  224.     {
  225.         $this->author $author;
  226.         return $this;
  227.     }
  228.     public function getPerson(): ?Person
  229.     {
  230.         return $this->person;
  231.     }
  232.     public function setPerson(?Person $person): self
  233.     {
  234.         $this->person $person;
  235.         return $this;
  236.     }
  237.     public function getCalendarEvent(): ?CalendarEvent
  238.     {
  239.         return $this->calendarEvent;
  240.     }
  241.     public function setCalendarEvent(?CalendarEvent $calendarEvent): self
  242.     {
  243.         $this->calendarEvent $calendarEvent;
  244.         return $this;
  245.     }
  246.     public function getCreatedAt(): ?\DateTimeInterface
  247.     {
  248.         return $this->createdAt;
  249.     }
  250.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  251.     {
  252.         $this->createdAt $createdAt;
  253.         return $this;
  254.     }
  255.     public function getViewerComment(): ?string
  256.     {
  257.         if (!$this->viewerComment) {
  258.             return $this->viewerComment;
  259.         }
  260.         return mb_ucfirst($this->viewerComment'UTF-8');
  261.     }
  262.     public function setViewerComment(?string $viewerComment): self
  263.     {
  264.         $this->viewerComment $viewerComment;
  265.         return $this;
  266.     }
  267.     public function getAllComments(): string
  268.     {
  269.         $comments "";
  270.         if ($this->getComment()) {
  271.             $comments $this->getComment();
  272.             $comments str_ends_with($comments'.') ? $comments : ($comments '.');
  273.         }
  274.         if ($this->getReviewApproveComment()) {
  275.             if ($comments) {
  276.                 $comments .= "\n";
  277.             }
  278.             $reviewApproveComment $this->getReviewApproveComment();
  279.             $reviewApproveComment str_ends_with($reviewApproveComment'.')
  280.                 ? $reviewApproveComment : ($reviewApproveComment '.');
  281.             $comments .= $reviewApproveComment;
  282.         }
  283.         return $comments;
  284.     }
  285.     public function getApprovedAt(): ?\DateTimeInterface
  286.     {
  287.         return $this->approvedAt;
  288.     }
  289.     public function setApprovedAt(?\DateTimeInterface $approvedAt): self
  290.     {
  291.         $this->approvedAt $approvedAt;
  292.         return $this;
  293.     }
  294.     public function getImage(): ?Image
  295.     {
  296.         return $this->image;
  297.     }
  298.     public function setImage(?Image $image): self
  299.     {
  300.         $this->image $image;
  301.         return $this;
  302.     }
  303.     public function getComment(): ?string
  304.     {
  305.         if (!$this->comment) {
  306.             return $this->comment;
  307.         }
  308.         return mb_ucfirst($this->comment'UTF-8');
  309.     }
  310.     public function setComment(?string $comment): self
  311.     {
  312.         $this->comment $comment;
  313.         return $this;
  314.     }
  315.     public function addComment(string $textbool $checkContains true,
  316.                                bool $ucfirst falsebool $addDot false): void
  317.     {
  318.         if ($ucfirst) {
  319.             $text mb_ucfirst($text'UTF-8');
  320.         }
  321.         if ($addDot && mb_substr($text, -1null'UTF-8') !== '.') {
  322.             $text .= '.';
  323.         }
  324.         $newComment trim($this->comment ?? "");
  325.         if (!$checkContains || mb_strpos($newComment $textnull'UTF-8') === false) {
  326.             $newComment $newComment
  327.                 . ($addDot && $newComment && mb_substr($newComment, -1null'UTF-8') !== '.' '.' '')
  328.                 . ($newComment ?  "\n" "")
  329.                 . $text;
  330.             $this->setComment($newComment);
  331.         }
  332.     }
  333.     public function getStatusText(): string
  334.     {
  335.         return Status::getText($this->status);
  336.     }
  337.     public function getVirtualStatusText(): string
  338.     {
  339.         if ($this->isVirtualStatusNotRequired()) {
  340.             return Status::getText(Status::STATUS_NOT_REQUIRED);
  341.         }
  342.         return Status::getText($this->status);
  343.     }
  344.     public function getCssClass(): string
  345.     {
  346.         return Status::getCssClass($this->status);
  347.     }
  348.     public function isStatusLeaved(): bool
  349.     {
  350.         return $this->status === Status::STATUS_LEAVED;
  351.     }
  352.     public function isVirtualStatusNotRequired(): bool
  353.     {
  354.         return Status::isVirtualStatusNotRequired(
  355.             $this->status,
  356.             $this->getCalendarEvent(),
  357.             $this->getPerson()
  358.         );
  359.     }
  360.     public function getVirtualCssClass(): string
  361.     {
  362.         if ($this->isVirtualStatusNotRequired()) {
  363.             return Status::getCssClass(Status::STATUS_NOT_REQUIRED);
  364.         }
  365.         return Status::getCssClass($this->status);
  366.     }
  367.     public function isPaymentDeferment(): ?bool
  368.     {
  369.         return $this->paymentDeferment;
  370.     }
  371.     public function setPaymentDeferment(?bool $paymentDeferment): self
  372.     {
  373.         $this->paymentDeferment $paymentDeferment;
  374.         return $this;
  375.     }
  376.     public function getSelectedAuthor(): ?User
  377.     {
  378.         return $this->selectedAuthor;
  379.     }
  380.     public function setSelectedAuthor(?User $selectedAuthor): self
  381.     {
  382.         $this->selectedAuthor $selectedAuthor;
  383.         return $this;
  384.     }
  385.     public function getRecommendations(): ?string
  386.     {
  387.         if (!$this->recommendations) {
  388.             return $this->recommendations;
  389.         }
  390.         return mb_ucfirst($this->recommendations'UTF-8');
  391.     }
  392.     public function setRecommendations(?string $recommendations): self
  393.     {
  394.         $this->recommendations $recommendations;
  395.         return $this;
  396.     }
  397.     public function getCurator(): ?User
  398.     {
  399.         return $this->curator;
  400.     }
  401.     public function setCurator(?User $curator): self
  402.     {
  403.         $this->curator $curator;
  404.         return $this;
  405.     }
  406.     public function getUpdatedAt(): ?\DateTimeInterface
  407.     {
  408.         if (!$this->updatedAt) {
  409.             return $this->getCreatedAt();
  410.         }
  411.         return $this->updatedAt;
  412.     }
  413.     public function setUpdatedAt(\DateTimeInterface $updatedAt): self
  414.     {
  415.         $this->updatedAt $updatedAt;
  416.         return $this;
  417.     }
  418.     public function isTimeToSendReviewResultMessage(): array
  419.     {
  420. //        return [
  421. //            "result" => true,
  422. //            'waitMinutes' => null,
  423. //        ];
  424.         $result = [
  425.             "result" => false,
  426.             'waitMinutes' => null,
  427.         ];
  428.         $minutesLeftSinceUpdate = (int)ceil((time() - $this->getUpdatedAt()->getTimestamp()) / 60);
  429.         if ($this->getCalendarEvent()->getHoursLeftBeforeStart() >= &&
  430.             $minutesLeftSinceUpdate 1
  431.         ) {
  432.             $result['result'] = false;
  433.             $result['waitMinutes'] = $minutesLeftSinceUpdate;
  434.         } else {
  435.             $result['result'] = true;
  436.         }
  437.         return $result;
  438.     }
  439.     public function getReviewApproveComment(): ?string
  440.     {
  441.         if (!$this->reviewApproveComment) {
  442.             return $this->reviewApproveComment;
  443.         }
  444.         return mb_ucfirst($this->reviewApproveComment'UTF-8');
  445.     }
  446.     public function setReviewApproveComment(?string $reviewApproveComment): self
  447.     {
  448.         $reviewApproveComment trim($reviewApproveComment);
  449.         if (!$reviewApproveComment) {
  450.             return $this;
  451.         }
  452.         $this->reviewApproveComment $reviewApproveComment;
  453.         return $this;
  454.     }
  455.     public function isAccessByReceipt(): ?bool
  456.     {
  457.         return $this->accessByReceipt;
  458.     }
  459.     public function setAccessByReceipt(?bool $accessByReceipt): self
  460.     {
  461.         $this->accessByReceipt $accessByReceipt;
  462.         return $this;
  463.     }
  464.     public function getDeclineComment(): ?string
  465.     {
  466.         if (!$this->declineComment) {
  467.             return $this->declineComment;
  468.         }
  469.         return mb_ucfirst($this->declineComment'UTF-8');
  470.     }
  471.     public function setDeclineComment(?string $declineComment): self
  472.     {
  473.         $this->declineComment $declineComment;
  474.         return $this;
  475.     }
  476.     public function getLeavedAt(): ?\DateTimeInterface
  477.     {
  478.         return $this->leavedAt;
  479.     }
  480.     public function setLeavedAt(?\DateTimeInterface $leavedAt): self
  481.     {
  482.         $this->leavedAt $leavedAt;
  483.         return $this;
  484.     }
  485.     public function getInvoice(): ?Invoice
  486.     {
  487.         return $this->invoice;
  488.     }
  489.     public function setInvoice(?Invoice $invoice): self
  490.     {
  491.         $this->invoice $invoice;
  492.         return $this;
  493.     }
  494.     public function getNewImage(): ?string
  495.     {
  496.         return $this->newImage;
  497.     }
  498.     public function setNewImage(?string $newImage): self
  499.     {
  500.         $this->newImage $newImage;
  501.         return $this;
  502.     }
  503.     public function getTextForEntityLog(): string
  504.     {
  505.         $result "Имя не указано";
  506.         if ($this->getPerson()) {
  507.             $result $this->getPerson()->__toString();
  508.         }
  509.         if ($this->getCalendarEvent()) {
  510.             $result .= " – " $this->getCalendarEvent()->__toString();
  511.         }
  512.         return $result;
  513.     }
  514.     public function getName(): ?string
  515.     {
  516.         return $this->getPerson()->getName();
  517.     }
  518.     public function getRegistrationType()
  519.     {
  520.         return $this->registrationType;
  521.     }
  522.     public function setRegistrationType($registrationType): self
  523.     {
  524.         $this->registrationType $registrationType;
  525.         return $this;
  526.     }
  527.     public function getRegistrationTypeText(): ?string
  528.     {
  529.         return RegistrationType::getText($this->getRegistrationType());
  530.     }
  531.     public function getCalendarEventName(): ?string
  532.     {
  533.         return $this->getCalendarEvent() ? $this->getCalendarEvent()->getName() : null;
  534.     }
  535. }