src/Entity/Candidate.php line 12

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