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 setStatus($status): self
  197.     {
  198.         if ($this->status != Status::STATUS_APPROVED &&
  199.             $this->status != Status::STATUS_REFUSED &&
  200.             $status == Status::STATUS_REFUSED) {
  201.             throw new \InvalidArgumentException("Нельзя поменять статус кандидата на Не пошел, "
  202.             "текущий статус должен быть Принят, т.к. под статусом Не пошел подразумевается, что кандидат прошел отбор.");
  203.         }
  204.         $this->status $status;
  205.         if ($status == Status::STATUS_APPROVED && !$this->getApprovedAt()) {
  206.             $this->setApprovedAt(new \DateTime());
  207.         } elseif ($status == Status::STATUS_LEAVED && !$this->getLeavedAt()) {
  208.             $this->setLeavedAt(new \DateTime());
  209.         }
  210.         return $this;
  211.     }
  212.     public function getAuthor(): ?User
  213.     {
  214.         // if ($this->getSelectedAuthor()) {
  215.         //     return $this->getSelectedAuthor();
  216.         // }
  217.         return $this->author;
  218.     }
  219.     public function setAuthor(?User $author): self
  220.     {
  221.         $this->author $author;
  222.         return $this;
  223.     }
  224.     public function getPerson(): ?Person
  225.     {
  226.         return $this->person;
  227.     }
  228.     public function setPerson(?Person $person): self
  229.     {
  230.         $this->person $person;
  231.         return $this;
  232.     }
  233.     public function getCalendarEvent(): ?CalendarEvent
  234.     {
  235.         return $this->calendarEvent;
  236.     }
  237.     public function setCalendarEvent(?CalendarEvent $calendarEvent): self
  238.     {
  239.         $this->calendarEvent $calendarEvent;
  240.         return $this;
  241.     }
  242.     public function getCreatedAt(): ?\DateTimeInterface
  243.     {
  244.         return $this->createdAt;
  245.     }
  246.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  247.     {
  248.         $this->createdAt $createdAt;
  249.         return $this;
  250.     }
  251.     public function getViewerComment(): ?string
  252.     {
  253.         if (!$this->viewerComment) {
  254.             return $this->viewerComment;
  255.         }
  256.         return mb_ucfirst($this->viewerComment'UTF-8');
  257.     }
  258.     public function setViewerComment(?string $viewerComment): self
  259.     {
  260.         $this->viewerComment $viewerComment;
  261.         return $this;
  262.     }
  263.     public function getAllComments(): string
  264.     {
  265.         $comments "";
  266.         if ($this->getComment()) {
  267.             $comments $this->getComment();
  268.             $comments str_ends_with($comments'.') ? $comments : ($comments '.');
  269.         }
  270.         if ($this->getReviewApproveComment()) {
  271.             if ($comments) {
  272.                 $comments .= "\n";
  273.             }
  274.             $reviewApproveComment $this->getReviewApproveComment();
  275.             $reviewApproveComment str_ends_with($reviewApproveComment'.')
  276.                 ? $reviewApproveComment : ($reviewApproveComment '.');
  277.             $comments .= $reviewApproveComment;
  278.         }
  279.         return $comments;
  280.     }
  281.     public function getApprovedAt(): ?\DateTimeInterface
  282.     {
  283.         return $this->approvedAt;
  284.     }
  285.     public function setApprovedAt(?\DateTimeInterface $approvedAt): self
  286.     {
  287.         $this->approvedAt $approvedAt;
  288.         return $this;
  289.     }
  290.     public function getImage(): ?Image
  291.     {
  292.         return $this->image;
  293.     }
  294.     public function setImage(?Image $image): self
  295.     {
  296.         $this->image $image;
  297.         return $this;
  298.     }
  299.     public function getComment(): ?string
  300.     {
  301.         if (!$this->comment) {
  302.             return $this->comment;
  303.         }
  304.         return mb_ucfirst($this->comment'UTF-8');
  305.     }
  306.     public function setComment(?string $comment): self
  307.     {
  308.         $this->comment $comment;
  309.         return $this;
  310.     }
  311.     public function addComment(string $textbool $checkContains true,
  312.                                bool $ucfirst falsebool $addDot false): void
  313.     {
  314.         if ($ucfirst) {
  315.             $text mb_ucfirst($text'UTF-8');
  316.         }
  317.         if ($addDot && mb_substr($text, -1null'UTF-8') !== '.') {
  318.             $text .= '.';
  319.         }
  320.         $newComment trim($this->comment ?? "");
  321.         if (!$checkContains || mb_strpos($newComment $textnull'UTF-8') === false) {
  322.             $newComment $newComment
  323.                 . ($addDot && $newComment && mb_substr($newComment, -1null'UTF-8') !== '.' '.' '')
  324.                 . ($newComment ?  "\n" "")
  325.                 . $text;
  326.             $this->setComment($newComment);
  327.         }
  328.     }
  329.     public function getStatusText(): string
  330.     {
  331.         return Status::getText($this->status);
  332.     }
  333.     public function getVirtualStatusText(): string
  334.     {
  335.         if ($this->isVirtualStatusNotRequired()) {
  336.             return Status::getText(Status::STATUS_NOT_REQUIRED);
  337.         }
  338.         return Status::getText($this->status);
  339.     }
  340.     public function getCssClass(): string
  341.     {
  342.         return Status::getCssClass($this->status);
  343.     }
  344.     public function isStatusLeaved(): bool
  345.     {
  346.         return $this->status === Status::STATUS_LEAVED;
  347.     }
  348.     public function isVirtualStatusNotRequired(): bool
  349.     {
  350.         return Status::isVirtualStatusNotRequired(
  351.             $this->status,
  352.             $this->getCalendarEvent(),
  353.             $this->getPerson()
  354.         );
  355.     }
  356.     public function getVirtualCssClass(): string
  357.     {
  358.         if ($this->isVirtualStatusNotRequired()) {
  359.             return Status::getCssClass(Status::STATUS_NOT_REQUIRED);
  360.         }
  361.         return Status::getCssClass($this->status);
  362.     }
  363.     public function isPaymentDeferment(): ?bool
  364.     {
  365.         return $this->paymentDeferment;
  366.     }
  367.     public function setPaymentDeferment(?bool $paymentDeferment): self
  368.     {
  369.         $this->paymentDeferment $paymentDeferment;
  370.         return $this;
  371.     }
  372.     public function getSelectedAuthor(): ?User
  373.     {
  374.         return $this->selectedAuthor;
  375.     }
  376.     public function setSelectedAuthor(?User $selectedAuthor): self
  377.     {
  378.         $this->selectedAuthor $selectedAuthor;
  379.         return $this;
  380.     }
  381.     public function getRecommendations(): ?string
  382.     {
  383.         if (!$this->recommendations) {
  384.             return $this->recommendations;
  385.         }
  386.         return mb_ucfirst($this->recommendations'UTF-8');
  387.     }
  388.     public function setRecommendations(?string $recommendations): self
  389.     {
  390.         $this->recommendations $recommendations;
  391.         return $this;
  392.     }
  393.     public function getCurator(): ?User
  394.     {
  395.         return $this->curator;
  396.     }
  397.     public function setCurator(?User $curator): self
  398.     {
  399.         $this->curator $curator;
  400.         return $this;
  401.     }
  402.     public function getUpdatedAt(): ?\DateTimeInterface
  403.     {
  404.         if (!$this->updatedAt) {
  405.             return $this->getCreatedAt();
  406.         }
  407.         return $this->updatedAt;
  408.     }
  409.     public function setUpdatedAt(\DateTimeInterface $updatedAt): self
  410.     {
  411.         $this->updatedAt $updatedAt;
  412.         return $this;
  413.     }
  414.     public function isTimeToSendReviewResultMessage(): array
  415.     {
  416. //        return [
  417. //            "result" => true,
  418. //            'waitMinutes' => null,
  419. //        ];
  420.         $result = [
  421.             "result" => false,
  422.             'waitMinutes' => null,
  423.         ];
  424.         $minutesLeftSinceUpdate = (int)ceil((time() - $this->getUpdatedAt()->getTimestamp()) / 60);
  425.         if ($this->getCalendarEvent()->getHoursLeftBeforeStart() >= &&
  426.             $minutesLeftSinceUpdate 1
  427.         ) {
  428.             $result['result'] = false;
  429.             $result['waitMinutes'] = $minutesLeftSinceUpdate;
  430.         } else {
  431.             $result['result'] = true;
  432.         }
  433.         return $result;
  434.     }
  435.     public function getReviewApproveComment(): ?string
  436.     {
  437.         if (!$this->reviewApproveComment) {
  438.             return $this->reviewApproveComment;
  439.         }
  440.         return mb_ucfirst($this->reviewApproveComment'UTF-8');
  441.     }
  442.     public function setReviewApproveComment(?string $reviewApproveComment): self
  443.     {
  444.         $reviewApproveComment trim($reviewApproveComment);
  445.         if (!$reviewApproveComment) {
  446.             return $this;
  447.         }
  448.         $this->reviewApproveComment $reviewApproveComment;
  449.         return $this;
  450.     }
  451.     public function isAccessByReceipt(): ?bool
  452.     {
  453.         return $this->accessByReceipt;
  454.     }
  455.     public function setAccessByReceipt(?bool $accessByReceipt): self
  456.     {
  457.         $this->accessByReceipt $accessByReceipt;
  458.         return $this;
  459.     }
  460.     public function getDeclineComment(): ?string
  461.     {
  462.         if (!$this->declineComment) {
  463.             return $this->declineComment;
  464.         }
  465.         return mb_ucfirst($this->declineComment'UTF-8');
  466.     }
  467.     public function setDeclineComment(?string $declineComment): self
  468.     {
  469.         $this->declineComment $declineComment;
  470.         return $this;
  471.     }
  472.     public function getLeavedAt(): ?\DateTimeInterface
  473.     {
  474.         return $this->leavedAt;
  475.     }
  476.     public function setLeavedAt(?\DateTimeInterface $leavedAt): self
  477.     {
  478.         $this->leavedAt $leavedAt;
  479.         return $this;
  480.     }
  481.     public function getInvoice(): ?Invoice
  482.     {
  483.         return $this->invoice;
  484.     }
  485.     public function setInvoice(?Invoice $invoice): self
  486.     {
  487.         $this->invoice $invoice;
  488.         return $this;
  489.     }
  490.     public function getNewImage(): ?string
  491.     {
  492.         return $this->newImage;
  493.     }
  494.     public function setNewImage(?string $newImage): self
  495.     {
  496.         $this->newImage $newImage;
  497.         return $this;
  498.     }
  499.     public function getTextForEntityLog(): string
  500.     {
  501.         $result "Имя не указано";
  502.         if ($this->getPerson()) {
  503.             $result $this->getPerson()->__toString();
  504.         }
  505.         if ($this->getCalendarEvent()) {
  506.             $result .= " – " $this->getCalendarEvent()->__toString();
  507.         }
  508.         return $result;
  509.     }
  510.     public function getName(): ?string
  511.     {
  512.         return $this->getPerson()->getName();
  513.     }
  514.     public function getRegistrationType()
  515.     {
  516.         return $this->registrationType;
  517.     }
  518.     public function setRegistrationType($registrationType): self
  519.     {
  520.         $this->registrationType $registrationType;
  521.         return $this;
  522.     }
  523.     public function getRegistrationTypeText(): ?string
  524.     {
  525.         return RegistrationType::getText($this->getRegistrationType());
  526.     }
  527. }