src/Entity/Person.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Enum\Student\Status;
  4. use App\Enum\Student\VirtualStatus;
  5. use App\Repository\PersonRepository;
  6. use App\Service\Person\PersonService;
  7. use App\Service\Transaction\TransactionService;
  8. use Doctrine\ORM\Mapping as ORM;
  9. /**
  10.  * @ORM\Entity(repositoryClass=PersonRepository::class)
  11.  */
  12. class Person
  13. {
  14.     const PERSON_FIELD_NAME 'person_name';
  15.     /**
  16.      * @ORM\Id
  17.      * @ORM\GeneratedValue
  18.      * @ORM\Column(type="integer")
  19.      */
  20.     private $id;
  21.     /**
  22.      * @ORM\Column(type="string", length=255)
  23.      */
  24.     private $firstName;
  25.     /**
  26.      * @ORM\Column(type="string", length=255)
  27.      */
  28.     private $lastName;
  29.     /**
  30.      * @ORM\OneToOne(targetEntity=Student::class, inversedBy="person")
  31.      */
  32.     private $student;
  33.     /**
  34.      * @ORM\ManyToOne(targetEntity=City::class)
  35.      */
  36.     private $city;
  37.     /**
  38.      * @ORM\Column(type="string", length=255, nullable=true)
  39.      */
  40.     private $phone;
  41.     /**
  42.      * @ORM\Column(type="string", length=255, nullable=true)
  43.      */
  44.     private $email;
  45.     /**
  46.      * @ORM\OneToOne(targetEntity=Image::class, cascade={"persist", "remove"})
  47.      */
  48.     private $image;
  49.     /**
  50.      * @ORM\Column(type="text", nullable=true)
  51.      */
  52.     private $comment;
  53.     /**
  54.      * @ORM\Column(type="string", length=255, nullable=true)
  55.      */
  56.     private $lastName2;
  57.     /**
  58.      * @ORM\Column(type="string", length=255, nullable=true)
  59.      */
  60.     private $telegramUserId;
  61.     /**
  62.      * @ORM\Column(type="boolean", nullable=true, options={"default"=true})
  63.      */
  64.     private $canSendTelegramCampaigns true;
  65.     /**
  66.      * @ORM\ManyToOne(targetEntity=Person::class)
  67.      */
  68.     private $messageReceiver;
  69.     /**
  70.      * @ORM\Column(type="boolean", nullable=true)
  71.      */
  72.     private $isStudent;
  73.     /**
  74.      * @ORM\Column(type="boolean", nullable=true)
  75.      */
  76.     private $isSubscriber;
  77.     private $virtualStudentStatus VirtualStatus::VIRTUAL_STATUS_SUBSCRIBER;
  78.     /**
  79.      * @var array|null
  80.      */
  81.     private $balance null;
  82.     /**
  83.      * @var array|null
  84.      */
  85.     private $balanceNoInvoices null;
  86.     /**
  87.      * @ORM\Column(type="string", length=255, nullable=true)
  88.      */
  89.     private $surname;
  90.     /**
  91.      * @ORM\Column(type="datetime")
  92.      */
  93.     private $createdAt;
  94.     /**
  95.      * @var TransactionService
  96.      */
  97.     private $transactionService;
  98.     /**
  99.      * @ORM\Column(type="text", nullable=true)
  100.      */
  101.     private $emails;
  102.     /**
  103.      * @ORM\ManyToOne(targetEntity=Job::class)
  104.      */
  105.     private $job;
  106.     /**
  107.      * @ORM\Column(type="date", nullable=true)
  108.      */
  109.     private $birthday;
  110.     /**
  111.      * @ORM\Column(type="enum", options={"values"="person_enum", "default"="active"}))
  112.      */
  113.     private $status Status::STATUS_ACTIVE;
  114.     /**
  115.      * @ORM\Column(type="boolean", nullable=true)
  116.      */
  117.     private $isUnsubscribed;
  118.     /**
  119.      * @ORM\Column(type="datetime", nullable=true)
  120.      */
  121.     private $tgChannelAddErrorDate;
  122.     /**
  123.      * @ORM\Column(type="text", nullable=true)
  124.      */
  125.     private $tgChannelAddResponse;
  126.     public function __construct()
  127.     {
  128.         $this->createdAt = new \DateTime();
  129.     }
  130.     public function __toString()
  131.     {
  132.         return PersonService::getFirstLastNameWithOld($this) . " (" .
  133.             $this->getStudentLevelText() . ", " .
  134.             ($this->getCity() && trim($this->getCity()->getName()) ? $this->getCity()->getName() : "город не указан")
  135.             . ")";
  136.     }
  137.     public function getId(): ?int
  138.     {
  139.         return $this->id;
  140.     }
  141.     public function getFirstName(): ?string
  142.     {
  143.         return $this->firstName;
  144.     }
  145.     public function setFirstName(string $firstName): self
  146.     {
  147.         $this->firstName $firstName;
  148.         return $this;
  149.     }
  150.     public function getLastName(): ?string
  151.     {
  152.         return $this->lastName;
  153.     }
  154.     public function setLastName(string $lastName): self
  155.     {
  156.         $this->lastName $lastName;
  157.         return $this;
  158.     }
  159.     public function getStudent(): ?Student
  160.     {
  161.         return $this->student;
  162.     }
  163.     public function setStudent(?Student $student): self
  164.     {
  165.         $this->student $student;
  166.         return $this;
  167.     }
  168.     public function getName(array $options null): string
  169.     {
  170.         $options array_merge(
  171.             [
  172.                 "lastNameFirst" => false
  173.             ], ($options ?? [])
  174.         );
  175.         $name = ($options["lastNameFirst"] ? $this->getLastFirstName() : $this->firstName ' ' $this->lastName );
  176.         return $name;
  177.     }
  178.     public function isSameName($name): bool
  179.     {
  180.         $nameParts explode(' '$name);
  181.         if (count($nameParts) === 2) {
  182.             $firstName $nameParts[0];
  183.             $lastName $nameParts[1];
  184.             $firstName mb_strtolower($firstName'UTF-8');
  185.             $lastName mb_strtolower($lastName'UTF-8');
  186.             $currentFirstName mb_strtolower($this->firstName'UTF-8');
  187.             $currentLastName mb_strtolower($this->lastName'UTF-8');
  188.             return ($firstName == $currentFirstName && $lastName == $currentLastName)
  189.                 || ($firstName == $currentLastName && $lastName == $currentFirstName);
  190.         } else {
  191.             return false;
  192.         }
  193.     }
  194.     public function getLastFirstName(): string
  195.     {
  196.         return $this->lastName ' ' $this->firstName;
  197.     }
  198.     public function getLastName2FirstName(): string
  199.     {
  200.         return $this->lastName2 ' ' $this->firstName;
  201.     }
  202.     public function getNameAndCityText(): string
  203.     {
  204.         return $this->getName() . " (" .
  205.             ($this->getCity() && trim($this->getCity()->getName()) ? $this->getCity()->getName() : "город не указан")
  206.             . ")";
  207.     }
  208.     public function getCityText(): string
  209.     {
  210.         return $this->getCity() ? $this->getCity()->getName() : "город не указан";
  211.     }
  212.     public function getCity(): ?City
  213.     {
  214.         return $this->city;
  215.     }
  216.     public function setCity(?City $city): self
  217.     {
  218.         $this->city $city;
  219.         return $this;
  220.     }
  221.     public function getPhone(): ?string
  222.     {
  223.         return $this->phone;
  224.     }
  225.     public function setPhone(?string $phone): self
  226.     {
  227.         $this->phone $phone;
  228.         return $this;
  229.     }
  230.     public function getEmail($skipErrors true): ?string
  231.     {
  232.         $email $this->email ?? "";
  233.         if (!$skipErrors && preg_match('/\s|[,]/'$email)) {
  234.             throw new \Exception("E-mail contains restricted characters: " $email);
  235.         }
  236.         return $this->email;
  237.     }
  238.     public function setEmail(?string $email): self
  239.     {
  240.         $this->email $email;
  241.         return $this;
  242.     }
  243.     public function getImage(): ?Image
  244.     {
  245.         return $this->image;
  246.     }
  247.     public function setImage(?Image $image): self
  248.     {
  249.         $this->image $image;
  250.         return $this;
  251.     }
  252.     public function getComment(): ?string
  253.     {
  254.         return $this->comment;
  255.     }
  256.     public function setComment(?string $comment): self
  257.     {
  258.         $this->comment $comment;
  259.         return $this;
  260.     }
  261.     public function addComment(string $textbool $checkContains true,
  262.                                bool $ucfirst falsebool $addDot false): void
  263.     {
  264.         if ($ucfirst) {
  265.             $text mb_ucfirst($text'UTF-8');
  266.         }
  267.         if ($addDot && mb_substr($text, -1null'UTF-8') !== '.') {
  268.             $text .= '.';
  269.         }
  270.         $newComment trim($this->comment ?? "");
  271.         if (!$checkContains || mb_strpos($newComment $textnull'UTF-8') === false) {
  272.             $newComment $newComment
  273.                 . ($addDot && $newComment && mb_substr($newComment, -1null'UTF-8') !== '.' '.' '')
  274.                 . ($newComment ?  "\n" "")
  275.                 . $text;
  276.             $this->setComment($newComment);
  277.         }
  278.     }
  279.     public function getLastName2(): ?string
  280.     {
  281.         return $this->lastName2;
  282.     }
  283.     public function setLastName2(?string $lastName2): self
  284.     {
  285.         $this->lastName2 $lastName2;
  286.         return $this;
  287.     }
  288.     public function getTelegramUserId(): ?string
  289.     {
  290.         return $this->telegramUserId;
  291.     }
  292.     public function setTelegramUserId(?string $telegramUserId): self
  293.     {
  294.         $this->telegramUserId $telegramUserId;
  295.         return $this;
  296.     }
  297.     public function isCanSendTelegramCampaigns(): ?bool
  298.     {
  299.         if ($this->isUnsubscribed) {
  300.             return false;
  301.         }
  302.         return $this->canSendTelegramCampaigns;
  303.     }
  304.     public function setCanSendTelegramCampaigns(?bool $canSendTelegramCampaigns): self
  305.     {
  306.         $this->canSendTelegramCampaigns $canSendTelegramCampaigns;
  307.         return $this;
  308.     }
  309.     public function getMessageReceiver(): ?self
  310.     {
  311.         return $this->messageReceiver;
  312.     }
  313.     public function setMessageReceiver(?self $messageReceiver): self
  314.     {
  315.         $this->messageReceiver $messageReceiver;
  316.         return $this;
  317.     }
  318.     public function isIsStudent(): ?bool
  319.     {
  320.         return $this->isStudent;
  321.     }
  322.     public function setIsStudent(?bool $isStudent): self
  323.     {
  324.         $this->isStudent $isStudent;
  325.         return $this;
  326.     }
  327.     public function isIsSubscriber(): ?bool
  328.     {
  329.         return $this->isSubscriber;
  330.     }
  331.     public function setIsSubscriber(?bool $isSubscriber): self
  332.     {
  333.         $this->isSubscriber $isSubscriber;
  334.         return $this;
  335.     }
  336.     public function getVirtualStudentStatus(): ?string
  337.     {
  338.         if ($this->getStatus() == \App\Enum\Person\Status::STATUS_DELETED) {
  339.             return Status::STATUS_DELETED;
  340.         }
  341.         if ($this->isIsStudent()) {
  342.             return $this->getStudent()->getStatus();
  343.         } elseif ($this->isIsSubscriber()) {
  344.             return VirtualStatus::VIRTUAL_STATUS_SUBSCRIBER;
  345.         }
  346.         return null;
  347.     }
  348.     public function setVirtualStudentStatus(?string $virtualStatus): void
  349.     {
  350. //        throw new \Exception("Logic was moved to controller");
  351.     }
  352.     public function getVirtualStudentStatusText(): ?string
  353.     {
  354.         if (Status::isCorrect($this->getVirtualStudentStatus())) {
  355.             if ($this->getStatus() == \App\Enum\Person\Status::STATUS_DELETED) {
  356.                 return \App\Enum\Person\Status::getText($this->getStatus());
  357.             }
  358.             return $this->getStudent()->getStatusText();
  359.         }
  360.         return VirtualStatus::getText($this->getVirtualStudentStatus());
  361.     }
  362.     public function getVirtualStudentStatusCssClass($prefix null): ?string
  363.     {
  364. //        try {
  365.             if (Status::isCorrect($this->getVirtualStudentStatus())) {
  366.                 if ($this->getStatus() == \App\Enum\Person\Status::STATUS_DELETED) {
  367.                     return \App\Enum\Person\Status::getCssClass($this->getStatus(), $prefix);
  368.                 }
  369.                 return $this->getStudent()->getStatusCssClass($prefix);
  370.             }
  371.             return VirtualStatus::getCssClass($this->getVirtualStudentStatus(), $prefix);
  372. //        } catch (\Throwable $e) {
  373. //            dd($this->getVirtualStudentStatus(), $this->getStudent());
  374. //        }
  375.     }
  376.     public function getSurname(): ?string
  377.     {
  378.         return $this->surname;
  379.     }
  380.     public function setSurname(?string $surname): self
  381.     {
  382.         $this->surname $surname;
  383.         return $this;
  384.     }
  385.     public function getFullNameWithSurname(): string
  386.     {
  387.         return $this->getName() . ($this->surname " " $this->surname "");
  388.     }
  389.     public function getFullNameWithSurnameAndCityText(): string
  390.     {
  391.         return $this->getFullNameWithSurname() . " (" .
  392.             ($this->getCity() && trim($this->getCity()->getName()) ? $this->getCity()->getName() : "город не указан")
  393.             . ")";
  394.     }
  395.     public function getCreatedAt(): ?\DateTimeInterface
  396.     {
  397.         return $this->createdAt;
  398.     }
  399.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  400.     {
  401.         $this->createdAt $createdAt;
  402.         return $this;
  403.     }
  404.     public function isCalendarEventStatusAllowed(CalendarEvent $calendarEvent): bool
  405.     {
  406.         if (!$calendarEvent->getRequireStudentStatus()) {
  407.             return true;
  408.         }
  409.         return $this->getVirtualStudentStatus() == $calendarEvent->getRequireStudentStatus();
  410.     }
  411.     public function getBalance($forceUpdate false): array
  412.     {
  413.         if ($this->balance === null || $forceUpdate) {
  414.             $this->setBalances();
  415.         }
  416.         return $this->balance;
  417.     }
  418.     public function getBalanceNoInvoices($forceUpdate false): array
  419.     {
  420.         if ($this->balanceNoInvoices === null || $forceUpdate) {
  421.             $this->setBalances();
  422.         }
  423.         return $this->balanceNoInvoices;
  424.     }
  425.     private function setBalances()
  426.     {
  427.         $balanceResult $this->transactionService->getPersonBalance($this);
  428.         $this->balance $balanceResult['balance'];
  429.         $this->balanceNoInvoices $balanceResult['balanceNoInvoices'];
  430.     }
  431.     /**
  432.      * @param FinanceCategory|int|null $financeCategory
  433.      * @param $forceUpdate
  434.      * @return int
  435.      */
  436.     public function getBalanceByFinanceCategory($financeCategory,
  437.                                                 $forceUpdate false): int
  438.     {
  439.         if (!$financeCategory) {
  440.             return 0;
  441.         }
  442.         $balance $this->getBalance($forceUpdate);
  443.         if (!is_numeric($financeCategory)) {
  444.             $financeCategory $financeCategory->getId();
  445.         }
  446.         return $balance[$financeCategory] ?? 0;
  447.     }
  448.     public function setTransactionService(TransactionService $transactionService): void
  449.     {
  450.         $this->transactionService $transactionService;
  451.     }
  452.     public function getEmails(): ?string
  453.     {
  454.         return $this->emails;
  455.     }
  456.     public function setEmails(?string $emails): self
  457.     {
  458.         $this->emails $emails;
  459.         return $this;
  460.     }
  461.     public function getJob(): ?Job
  462.     {
  463.         return $this->job;
  464.     }
  465.     public function setJob(?Job $job): self
  466.     {
  467.         $this->job $job;
  468.         return $this;
  469.     }
  470.     public function getBirthday(): ?\DateTimeInterface
  471.     {
  472.         return $this->birthday;
  473.     }
  474.     public function setBirthday(?\DateTimeInterface $birthday): self
  475.     {
  476.         $this->birthday $birthday;
  477.         return $this;
  478.     }
  479.     public function getStatus()
  480.     {
  481.         return $this->status;
  482.     }
  483.     public function setStatus($status): self
  484.     {
  485.         $this->status $status;
  486.         return $this;
  487.     }
  488.     public function isIsUnsubscribed(): ?bool
  489.     {
  490.         return $this->isUnsubscribed;
  491.     }
  492.     public function setIsUnsubscribed(?bool $isUnsubscribed): self
  493.     {
  494.         if (!$isUnsubscribed) {
  495.             $this->setCanSendTelegramCampaigns(true);
  496.         }
  497.         $this->isUnsubscribed $isUnsubscribed;
  498.         return $this;
  499.     }
  500.     public function getTgChannelAddErrorDate(): ?\DateTimeInterface
  501.     {
  502.         return $this->tgChannelAddErrorDate;
  503.     }
  504.     public function setTgChannelAddErrorDate(?\DateTimeInterface $tgChannelAddErrorDate): self
  505.     {
  506.         $this->tgChannelAddErrorDate $tgChannelAddErrorDate;
  507.         return $this;
  508.     }
  509.     public function getTgChannelAddResponse(): ?string
  510.     {
  511.         return $this->tgChannelAddResponse;
  512.     }
  513.     public function setTgChannelAddResponse(?string $tgChannelAddResponse): self
  514.     {
  515.         $this->tgChannelAddResponse $tgChannelAddResponse;
  516.         return $this;
  517.     }
  518.     public function getStudentLevelText(): string
  519.     {
  520.         if (!$this->getStudent()) {
  521.             return "нет уровня";
  522.         }
  523.         return $this->getStudent()->getLevelText();
  524.     }
  525. }