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.     /**
  112.      * @ORM\Column(type="string", length=64, nullable=true, unique=true)
  113.      */
  114.     private $appEnrollmentId;
  115.     public function __construct()
  116.     {
  117.         $this->createdAt = new \DateTime();
  118.         $this->updatedAt = new \DateTime();
  119.     }
  120.     public function __toString()
  121.     {
  122.         $ce = $this->getCalendarEvent() ?: null;
  123.         return ($this->getPerson() ? $this->getPerson()->__toString() : "–") . " – "
  124.             . ($ce ? $ce->getNameText() : "–");
  125.     }
  126.     public function getId(): ?int
  127.     {
  128.         return $this->id;
  129.     }
  130.     public function getStatus()
  131.     {
  132.         return $this->status;
  133.     }
  134.     public function isStatusApproved(): bool
  135.     {
  136.         return $this->status === Status::STATUS_APPROVED;
  137.     }
  138.     public function isStatusDraft(): bool
  139.     {
  140.         return $this->status === Status::STATUS_DRAFT;
  141.     }
  142.     public function isStatusNew(): bool
  143.     {
  144.         return $this->status === Status::STATUS_NEW;
  145.     }
  146.     public function getCalendarEventListedVirtualStatus(bool $hasPayments): string
  147.     {
  148.         if ($this->isStatusApproved()) {
  149.             if ($hasPayments) {
  150.                 return "approved";
  151.             } else {
  152.                 return "unpaid";
  153.             }
  154.         } else if ($this->isStatusLeaved()) {
  155.             return "leaved";
  156.         } else {
  157.             return "not_active";
  158.         }
  159.     }
  160.     public function getCalendarEventListedVirtualStatusText(bool $hasPayments): string
  161.     {
  162.         if ($this->isStatusApproved()) {
  163.             if ($hasPayments) {
  164.                 return "Зачислен";
  165.             } else if ($this->accessByReceipt) {
  166.                 return "Зачислен по квитанции";
  167.             } else {
  168.                 return "Не оплачен";
  169.             }
  170.         } else if ($this->isStatusLeaved()) {
  171.             return "Выбыл с мероприятия";
  172.         } else if ($this->isStatusRefused()) {
  173.             return "Не пошел";
  174.         } else {
  175.             return "Не активен";
  176.         }
  177.     }
  178.     public function getCalendarEventListedVirtualStatusCssClass(bool $hasPayments): string
  179.     {
  180.         if ($this->isStatusApproved()) {
  181.             if ($hasPayments || $this->accessByReceipt) {
  182.                 return "success";
  183.             } else {
  184.                 return "light";
  185.             }
  186.         } else if ($this->isStatusLeaved()) {
  187.             return "warning";
  188.         } else {
  189.             return "danger";
  190.         }
  191.     }
  192.     public function isStatusDeclined(): bool
  193.     {
  194.         return $this->status === Status::STATUS_DECLINED;
  195.     }
  196.     public function isStatusRefused(): bool
  197.     {
  198.         return $this->status === Status::STATUS_REFUSED;
  199.     }
  200.     public function isStatusDeleted(): bool
  201.     {
  202.         return $this->status === Status::STATUS_DELETED;
  203.     }
  204.     public function setStatus($status): self
  205.     {
  206.         if ($this->status != Status::STATUS_APPROVED &&
  207.             $this->status != Status::STATUS_REFUSED &&
  208.             $status == Status::STATUS_REFUSED) {
  209.             throw new \InvalidArgumentException("Нельзя поменять статус кандидата на Не пошел, "
  210.             . "текущий статус должен быть Принят, т.к. под статусом Не пошел подразумевается, что кандидат прошел отбор.");
  211.         }
  212.         $this->status = $status;
  213.         if ($status == Status::STATUS_APPROVED && !$this->getApprovedAt()) {
  214.             $this->setApprovedAt(new \DateTime());
  215.         } elseif ($status == Status::STATUS_LEAVED && !$this->getLeavedAt()) {
  216.             $this->setLeavedAt(new \DateTime());
  217.         }
  218.         return $this;
  219.     }
  220.     public function getAuthor(): ?User
  221.     {
  222.         // if ($this->getSelectedAuthor()) {
  223.         //     return $this->getSelectedAuthor();
  224.         // }
  225.         return $this->author;
  226.     }
  227.     public function setAuthor(?User $author): self
  228.     {
  229.         $this->author = $author;
  230.         return $this;
  231.     }
  232.     public function getPerson(): ?Person
  233.     {
  234.         return $this->person;
  235.     }
  236.     public function setPerson(?Person $person): self
  237.     {
  238.         $this->person = $person;
  239.         return $this;
  240.     }
  241.     public function getCalendarEvent(): ?CalendarEvent
  242.     {
  243.         return $this->calendarEvent;
  244.     }
  245.     public function setCalendarEvent(?CalendarEvent $calendarEvent): self
  246.     {
  247.         $this->calendarEvent = $calendarEvent;
  248.         return $this;
  249.     }
  250.     public function getCreatedAt(): ?\DateTimeInterface
  251.     {
  252.         return $this->createdAt;
  253.     }
  254.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  255.     {
  256.         $this->createdAt = $createdAt;
  257.         return $this;
  258.     }
  259.     public function getViewerComment(): ?string
  260.     {
  261.         if (!$this->viewerComment) {
  262.             return $this->viewerComment;
  263.         }
  264.         return mb_ucfirst($this->viewerComment, 'UTF-8');
  265.     }
  266.     public function setViewerComment(?string $viewerComment): self
  267.     {
  268.         $this->viewerComment = $viewerComment;
  269.         return $this;
  270.     }
  271.     public function getAllComments(): string
  272.     {
  273.         $comments = "";
  274.         if ($this->getComment()) {
  275.             $comments = $this->getComment();
  276.             $comments = str_ends_with($comments, '.') ? $comments : ($comments . '.');
  277.         }
  278.         if ($this->getReviewApproveComment()) {
  279.             if ($comments) {
  280.                 $comments .= "\n";
  281.             }
  282.             $reviewApproveComment = $this->getReviewApproveComment();
  283.             $reviewApproveComment = str_ends_with($reviewApproveComment, '.')
  284.                 ? $reviewApproveComment : ($reviewApproveComment . '.');
  285.             $comments .= $reviewApproveComment;
  286.         }
  287.         return $comments;
  288.     }
  289.     public function getApprovedAt(): ?\DateTimeInterface
  290.     {
  291.         return $this->approvedAt;
  292.     }
  293.     public function setApprovedAt(?\DateTimeInterface $approvedAt): self
  294.     {
  295.         $this->approvedAt = $approvedAt;
  296.         return $this;
  297.     }
  298.     public function getImage(): ?Image
  299.     {
  300.         return $this->image;
  301.     }
  302.     public function setImage(?Image $image): self
  303.     {
  304.         $this->image = $image;
  305.         return $this;
  306.     }
  307.     public function getComment(): ?string
  308.     {
  309.         if (!$this->comment) {
  310.             return $this->comment;
  311.         }
  312.         return mb_ucfirst($this->comment, 'UTF-8');
  313.     }
  314.     public function setComment(?string $comment): self
  315.     {
  316.         $this->comment = $comment;
  317.         return $this;
  318.     }
  319.     public function addComment(string $text, bool $checkContains = true,
  320.                                bool $ucfirst = false, bool $addDot = false): void
  321.     {
  322.         if ($ucfirst) {
  323.             $text = mb_ucfirst($text, 'UTF-8');
  324.         }
  325.         if ($addDot && mb_substr($text, -1, null, 'UTF-8') !== '.') {
  326.             $text .= '.';
  327.         }
  328.         $newComment = trim($this->comment ?? "");
  329.         if (!$checkContains || mb_strpos($newComment , $text, null, 'UTF-8') === false) {
  330.             $newComment = $newComment
  331.                 . ($addDot && $newComment && mb_substr($newComment, -1, null, 'UTF-8') !== '.' ? '.' : '')
  332.                 . ($newComment ?  "\n" : "")
  333.                 . $text;
  334.             $this->setComment($newComment);
  335.         }
  336.     }
  337.     public function getStatusText(): string
  338.     {
  339.         return Status::getText($this->status);
  340.     }
  341.     public function getVirtualStatusText(): string
  342.     {
  343.         if ($this->isVirtualStatusNotRequired()) {
  344.             return Status::getText(Status::STATUS_NOT_REQUIRED);
  345.         }
  346.         return Status::getText($this->status);
  347.     }
  348.     public function getCssClass(): string
  349.     {
  350.         return Status::getCssClass($this->status);
  351.     }
  352.     public function isStatusLeaved(): bool
  353.     {
  354.         return $this->status === Status::STATUS_LEAVED;
  355.     }
  356.     public function isVirtualStatusNotRequired(): bool
  357.     {
  358.         return Status::isVirtualStatusNotRequired(
  359.             $this->status,
  360.             $this->getCalendarEvent(),
  361.             $this->getPerson()
  362.         );
  363.     }
  364.     public function getVirtualCssClass(): string
  365.     {
  366.         if ($this->isVirtualStatusNotRequired()) {
  367.             return Status::getCssClass(Status::STATUS_NOT_REQUIRED);
  368.         }
  369.         return Status::getCssClass($this->status);
  370.     }
  371.     public function isPaymentDeferment(): ?bool
  372.     {
  373.         return $this->paymentDeferment;
  374.     }
  375.     public function setPaymentDeferment(?bool $paymentDeferment): self
  376.     {
  377.         $this->paymentDeferment = $paymentDeferment;
  378.         return $this;
  379.     }
  380.     public function getSelectedAuthor(): ?User
  381.     {
  382.         return $this->selectedAuthor;
  383.     }
  384.     public function setSelectedAuthor(?User $selectedAuthor): self
  385.     {
  386.         $this->selectedAuthor = $selectedAuthor;
  387.         return $this;
  388.     }
  389.     public function getRecommendations(): ?string
  390.     {
  391.         if (!$this->recommendations) {
  392.             return $this->recommendations;
  393.         }
  394.         return mb_ucfirst($this->recommendations, 'UTF-8');
  395.     }
  396.     public function setRecommendations(?string $recommendations): self
  397.     {
  398.         $this->recommendations = $recommendations;
  399.         return $this;
  400.     }
  401.     public function getCurator(): ?User
  402.     {
  403.         return $this->curator;
  404.     }
  405.     public function setCurator(?User $curator): self
  406.     {
  407.         $this->curator = $curator;
  408.         return $this;
  409.     }
  410.     public function getUpdatedAt(): ?\DateTimeInterface
  411.     {
  412.         if (!$this->updatedAt) {
  413.             return $this->getCreatedAt();
  414.         }
  415.         return $this->updatedAt;
  416.     }
  417.     public function setUpdatedAt(\DateTimeInterface $updatedAt): self
  418.     {
  419.         $this->updatedAt = $updatedAt;
  420.         return $this;
  421.     }
  422.     public function isTimeToSendReviewResultMessage(): array
  423.     {
  424. //        return [
  425. //            "result" => true,
  426. //            'waitMinutes' => null,
  427. //        ];
  428.         $result = [
  429.             "result" => false,
  430.             'waitMinutes' => null,
  431.         ];
  432.         $minutesLeftSinceUpdate = (int)ceil((time() - $this->getUpdatedAt()->getTimestamp()) / 60);
  433.         if ($this->getCalendarEvent()->getHoursLeftBeforeStart() >= 6 &&
  434.             $minutesLeftSinceUpdate < 1
  435.         ) {
  436.             $result['result'] = false;
  437.             $result['waitMinutes'] = 1 - $minutesLeftSinceUpdate;
  438.         } else {
  439.             $result['result'] = true;
  440.         }
  441.         return $result;
  442.     }
  443.     public function getReviewApproveComment(): ?string
  444.     {
  445.         if (!$this->reviewApproveComment) {
  446.             return $this->reviewApproveComment;
  447.         }
  448.         return mb_ucfirst($this->reviewApproveComment, 'UTF-8');
  449.     }
  450.     public function setReviewApproveComment(?string $reviewApproveComment): self
  451.     {
  452.         $reviewApproveComment = trim($reviewApproveComment);
  453.         if (!$reviewApproveComment) {
  454.             return $this;
  455.         }
  456.         $this->reviewApproveComment = $reviewApproveComment;
  457.         return $this;
  458.     }
  459.     public function isAccessByReceipt(): ?bool
  460.     {
  461.         return $this->accessByReceipt;
  462.     }
  463.     public function setAccessByReceipt(?bool $accessByReceipt): self
  464.     {
  465.         $this->accessByReceipt = $accessByReceipt;
  466.         return $this;
  467.     }
  468.     public function getDeclineComment(): ?string
  469.     {
  470.         if (!$this->declineComment) {
  471.             return $this->declineComment;
  472.         }
  473.         return mb_ucfirst($this->declineComment, 'UTF-8');
  474.     }
  475.     public function setDeclineComment(?string $declineComment): self
  476.     {
  477.         $this->declineComment = $declineComment;
  478.         return $this;
  479.     }
  480.     public function getLeavedAt(): ?\DateTimeInterface
  481.     {
  482.         return $this->leavedAt;
  483.     }
  484.     public function setLeavedAt(?\DateTimeInterface $leavedAt): self
  485.     {
  486.         $this->leavedAt = $leavedAt;
  487.         return $this;
  488.     }
  489.     public function getInvoice(): ?Invoice
  490.     {
  491.         return $this->invoice;
  492.     }
  493.     public function setInvoice(?Invoice $invoice): self
  494.     {
  495.         $this->invoice = $invoice;
  496.         return $this;
  497.     }
  498.     public function getNewImage(): ?string
  499.     {
  500.         return $this->newImage;
  501.     }
  502.     public function setNewImage(?string $newImage): self
  503.     {
  504.         $this->newImage = $newImage;
  505.         return $this;
  506.     }
  507.     public function getTextForEntityLog(): string
  508.     {
  509.         $result = "Имя не указано";
  510.         if ($this->getPerson()) {
  511.             $result = $this->getPerson()->__toString();
  512.         }
  513.         if ($this->getCalendarEvent()) {
  514.             $result .= " – " . $this->getCalendarEvent()->__toString();
  515.         }
  516.         return $result;
  517.     }
  518.     public function getName(): ?string
  519.     {
  520.         return $this->getPerson()->getName();
  521.     }
  522.     public function getRegistrationType()
  523.     {
  524.         return $this->registrationType;
  525.     }
  526.     public function setRegistrationType($registrationType): self
  527.     {
  528.         $this->registrationType = $registrationType;
  529.         return $this;
  530.     }
  531.     public function getRegistrationTypeText(): ?string
  532.     {
  533.         return RegistrationType::getText($this->getRegistrationType());
  534.     }
  535.     public function getCalendarEventName(): ?string
  536.     {
  537.         return $this->getCalendarEvent() ? $this->getCalendarEvent()->getName() : null;
  538.     }
  539.     public function getAppEnrollmentId(): ?string
  540.     {
  541.         return $this->appEnrollmentId;
  542.     }
  543.     public function setAppEnrollmentId(?string $appEnrollmentId): self
  544.     {
  545.         $this->appEnrollmentId = $appEnrollmentId ?: null;
  546.         return $this;
  547.     }
  548. }