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