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\Repository\PersonRepository;
  5. use App\Service\Person\PersonService;
  6. use App\Service\Transaction\TransactionService;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Symfony\Component\Routing\RouterInterface;
  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, unique=true)
  59.      */
  60.     private $telegramUserId;
  61.     /**
  62.      * @ORM\Column(type="string", length=64, nullable=true, unique=true)
  63.      */
  64.     private $appLinkHash;
  65.     /**
  66.      * @ORM\Column(type="datetime", nullable=true)
  67.      */
  68.     private $appLinkCreatedAt;
  69.     /**
  70.      * @ORM\Column(type="string", length=64, nullable=true)
  71.      */
  72.     private $appUserId;
  73.     /**
  74.      * @ORM\Column(type="datetime", nullable=true)
  75.      */
  76.     private $appLinkedAt;
  77.     /**
  78.      * @ORM\Column(type="boolean", nullable=true, options={"default"=true})
  79.      */
  80.     private $canSendTelegramCampaigns = true;
  81.     /**
  82.      * @ORM\ManyToOne(targetEntity=Person::class)
  83.      */
  84.     private $messageReceiver;
  85.     /**
  86.      * @var array|null
  87.      */
  88.     private $balance = null;
  89.     /**
  90.      * @var array|null
  91.      */
  92.     private $balanceNoInvoices = null;
  93.     /**
  94.      * @ORM\Column(type="string", length=255, nullable=true)
  95.      */
  96.     private $surname;
  97.     /**
  98.      * @ORM\Column(type="datetime")
  99.      */
  100.     private $createdAt;
  101.     /**
  102.      * @var TransactionService
  103.      */
  104.     private $transactionService;
  105.     /**
  106.      * @ORM\Column(type="text", nullable=true)
  107.      */
  108.     private $emails;
  109.     /**
  110.      * @ORM\ManyToOne(targetEntity=Job::class)
  111.      */
  112.     private $job;
  113.     /**
  114.      * @ORM\Column(type="date", nullable=true)
  115.      */
  116.     private $birthday;
  117.     /**
  118.      * @ORM\Column(type="enum", options={"values"="person_enum", "default"="active"}))
  119.      */
  120.     private $status = \App\Enum\Person\Status::STATUS_ACTIVE;
  121.     /**
  122.      * @ORM\Column(type="boolean", nullable=true)
  123.      */
  124.     private $isUnsubscribed;
  125.     /**
  126.      * @ORM\Column(type="datetime", nullable=true)
  127.      */
  128.     private $tgChannelAddErrorDate;
  129.     /**
  130.      * @ORM\Column(type="text", nullable=true)
  131.      */
  132.     private $tgChannelAddResponse;
  133.     private ?RouterInterface $router = null;
  134.     public function __construct()
  135.     {
  136.         $this->createdAt = new \DateTime();
  137.     }
  138.     public function __toString()
  139.     {
  140.         return PersonService::getFirstLastNameWithOld($this) . " (" .
  141.             $this->getStudentLevelText() . ", " .
  142.             ($this->getCity() && trim($this->getCity()->getName()) ? $this->getCity()->getName() : "город не указан")
  143.             . ")";
  144.     }
  145.     public function getId(): ?int
  146.     {
  147.         return $this->id;
  148.     }
  149.     public function getFirstName(): ?string
  150.     {
  151.         return $this->firstName;
  152.     }
  153.     public function setFirstName(string $firstName): self
  154.     {
  155.         $this->firstName = $firstName;
  156.         return $this;
  157.     }
  158.     public function getLastName(): ?string
  159.     {
  160.         return $this->lastName;
  161.     }
  162.     public function setLastName(string $lastName): self
  163.     {
  164.         $this->lastName = $lastName;
  165.         return $this;
  166.     }
  167.     public function getStudent(): ?Student
  168.     {
  169.         return $this->student;
  170.     }
  171.     public function setStudent(?Student $student): self
  172.     {
  173.         $this->student = $student;
  174.         return $this;
  175.     }
  176.     public function getName(array $options = null): string
  177.     {
  178.         $options = array_merge(
  179.             [
  180.                 "lastNameFirst" => false
  181.             ], ($options ?? [])
  182.         );
  183.         $name = ($options["lastNameFirst"] ? $this->getLastFirstName() : $this->firstName . ' ' . $this->lastName );
  184.         return $name;
  185.     }
  186.     public function isSameName($name): bool
  187.     {
  188.         $nameParts = explode(' ', $name);
  189.         if (count($nameParts) === 2) {
  190.             $firstName = $nameParts[0];
  191.             $lastName = $nameParts[1];
  192.             $firstName = mb_strtolower($firstName, 'UTF-8');
  193.             $lastName = mb_strtolower($lastName, 'UTF-8');
  194.             $currentFirstName = mb_strtolower($this->firstName, 'UTF-8');
  195.             $currentLastName = mb_strtolower($this->lastName, 'UTF-8');
  196.             return ($firstName == $currentFirstName && $lastName == $currentLastName)
  197.                 || ($firstName == $currentLastName && $lastName == $currentFirstName);
  198.         } else {
  199.             return false;
  200.         }
  201.     }
  202.     public function getLastFirstName(): string
  203.     {
  204.         return $this->lastName . ' ' . $this->firstName;
  205.     }
  206.     public function getLastName2FirstName(): string
  207.     {
  208.         return $this->lastName2 . ' ' . $this->firstName;
  209.     }
  210.     public function getNameAndCityText(): string
  211.     {
  212.         return $this->getName() . " (" .
  213.             ($this->getCity() && trim($this->getCity()->getName()) ? $this->getCity()->getName() : "город не указан")
  214.             . ")";
  215.     }
  216.     public function getCityText(): string
  217.     {
  218.         return $this->getCity() ? $this->getCity()->getName() : "город не указан";
  219.     }
  220.     public function getCity(): ?City
  221.     {
  222.         return $this->city;
  223.     }
  224.     public function setCity(?City $city): self
  225.     {
  226.         $this->city = $city;
  227.         return $this;
  228.     }
  229.     public function getPhone(): ?string
  230.     {
  231.         return $this->phone;
  232.     }
  233.     public function setPhone(?string $phone): self
  234.     {
  235.         $this->phone = $phone;
  236.         return $this;
  237.     }
  238.     public function getEmail($skipErrors = true): ?string
  239.     {
  240.         $email = $this->email ?? "";
  241.         if (!$skipErrors && preg_match('/\s|[,]/', $email)) {
  242.             throw new \Exception("E-mail contains restricted characters: " . $email);
  243.         }
  244.         return $this->email;
  245.     }
  246.     public function setEmail(?string $email): self
  247.     {
  248.         $this->email = $email;
  249.         return $this;
  250.     }
  251.     public function getImage(): ?Image
  252.     {
  253.         return $this->image;
  254.     }
  255.     public function setImage(?Image $image): self
  256.     {
  257.         $this->image = $image;
  258.         return $this;
  259.     }
  260.     public function getComment(): ?string
  261.     {
  262.         return $this->comment;
  263.     }
  264.     public function setComment(?string $comment): self
  265.     {
  266.         $this->comment = $comment;
  267.         return $this;
  268.     }
  269.     public function addComment(string $text, bool $checkContains = true,
  270.                                bool $ucfirst = false, bool $addDot = false): void
  271.     {
  272.         if ($ucfirst) {
  273.             $text = mb_ucfirst($text, 'UTF-8');
  274.         }
  275.         if ($addDot && mb_substr($text, -1, null, 'UTF-8') !== '.') {
  276.             $text .= '.';
  277.         }
  278.         $newComment = trim($this->comment ?? "");
  279.         if (!$checkContains || mb_strpos($newComment , $text, null, 'UTF-8') === false) {
  280.             $newComment = $newComment
  281.                 . ($addDot && $newComment && mb_substr($newComment, -1, null, 'UTF-8') !== '.' ? '.' : '')
  282.                 . ($newComment ?  "\n" : "")
  283.                 . $text;
  284.             $this->setComment($newComment);
  285.         }
  286.     }
  287.     public function getLastName2(): ?string
  288.     {
  289.         return $this->lastName2;
  290.     }
  291.     public function setLastName2(?string $lastName2): self
  292.     {
  293.         $this->lastName2 = $lastName2;
  294.         return $this;
  295.     }
  296.     public function getTelegramUserId(): ?string
  297.     {
  298.         return $this->telegramUserId;
  299.     }
  300.     public function setTelegramUserId(?string $telegramUserId): self
  301.     {
  302.         $this->telegramUserId = $telegramUserId;
  303.         return $this;
  304.     }
  305.     public function isCanSendTelegramCampaigns(): ?bool
  306.     {
  307.         if ($this->isUnsubscribed) {
  308.             return false;
  309.         }
  310.         return $this->canSendTelegramCampaigns;
  311.     }
  312.     public function setCanSendTelegramCampaigns(?bool $canSendTelegramCampaigns): self
  313.     {
  314.         $this->canSendTelegramCampaigns = $canSendTelegramCampaigns;
  315.         return $this;
  316.     }
  317.     public function getMessageReceiver(): ?self
  318.     {
  319.         return $this->messageReceiver;
  320.     }
  321.     public function setMessageReceiver(?self $messageReceiver): self
  322.     {
  323.         $this->messageReceiver = $messageReceiver;
  324.         return $this;
  325.     }
  326.     public function isStudent(): ?bool
  327.     {
  328.         return Status::isStudentStatus($this->getStudentStatus());
  329.     }
  330.     public function isSubscriber(): ?bool
  331.     {
  332.         return Status::isSubscriberStatus($this->getStudentStatus());
  333.     }
  334.     public function isActiveSubscriberStatus(): bool
  335.     {
  336.         return Status::isActiveSubscriberStatus($this->getStudentStatus());
  337.     }
  338. //    public function getVirtualStudentStatus(): ?string
  339. //    {
  340. //        if ($this->getStatus() == \App\Enum\Person\Status::STATUS_DELETED) {
  341. //            return Status::STATUS_DELETED;
  342. //        }
  343. //
  344. //        if ($this->isStudent()) {
  345. //            return $this->getStudent()->getStatus();
  346. //        } elseif ($this->isSubscriber()) {
  347. //            return VirtualStatus::VIRTUAL_STATUS_SUBSCRIBER;
  348. //        }
  349. //        return null;
  350. //    }
  351.     public function getStudentStatus(): ?string
  352.     {
  353.         return $this->getStudent() ? $this->getStudent()->getStatus() : null;
  354.     }
  355.     public function getStudentStatusText(): ?string
  356.     {
  357.         return $this->getStudent() ? $this->getStudent()->getStatusText() : null;
  358.     }
  359.     public function getStudentStatusCssClass($prefix = null): ?string
  360.     {
  361.         return $this->getStudent() ? $this->getStudent()->getStatusCssClass($prefix) : null;
  362.     }
  363.     public function getSurname(): ?string
  364.     {
  365.         return $this->surname;
  366.     }
  367.     public function setSurname(?string $surname): self
  368.     {
  369.         $this->surname = $surname;
  370.         return $this;
  371.     }
  372.     public function getFullNameWithSurname(): string
  373.     {
  374.         return $this->getName() . ($this->surname ? " " . $this->surname : "");
  375.     }
  376.     public function getFullNameWithSurnameAndCityText(): string
  377.     {
  378.         return $this->getFullNameWithSurname() . " (" .
  379.             ($this->getCity() && trim($this->getCity()->getName()) ? $this->getCity()->getName() : "город не указан")
  380.             . ")";
  381.     }
  382.     public function getCreatedAt(): ?\DateTimeInterface
  383.     {
  384.         return $this->createdAt;
  385.     }
  386.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  387.     {
  388.         $this->createdAt = $createdAt;
  389.         return $this;
  390.     }
  391.     public function isCalendarEventStatusAllowed(CalendarEvent $calendarEvent): bool
  392.     {
  393.         if (!$calendarEvent->getRequireStudentStatus()) {
  394.             return true;
  395.         }
  396.         return $this->getStudentStatus() == $calendarEvent->getRequireStudentStatus();
  397.     }
  398.     public function getBalance($forceUpdate = false): array
  399.     {
  400.         if ($this->balance === null || $forceUpdate) {
  401.             $this->setBalances();
  402.         }
  403.         return $this->balance;
  404.     }
  405.     public function getBalanceNoInvoices($forceUpdate = false): array
  406.     {
  407.         if ($this->balanceNoInvoices === null || $forceUpdate) {
  408.             $this->setBalances();
  409.         }
  410.         return $this->balanceNoInvoices;
  411.     }
  412.     private function setBalances()
  413.     {
  414.         $balanceResult = $this->transactionService->getPersonBalance($this);
  415.         $this->balance = $balanceResult['balance'];
  416.         $this->balanceNoInvoices = $balanceResult['balanceNoInvoices'];
  417.     }
  418.     /**
  419.      * @param FinanceCategory|int|null $financeCategory
  420.      * @param $forceUpdate
  421.      * @return int
  422.      */
  423.     public function getBalanceByFinanceCategory($financeCategory,
  424.                                                 $forceUpdate = false): int
  425.     {
  426.         if (!$financeCategory) {
  427.             return 0;
  428.         }
  429.         $balance = $this->getBalance($forceUpdate);
  430.         if (!is_numeric($financeCategory)) {
  431.             $financeCategory = $financeCategory->getId();
  432.         }
  433.         return $balance[$financeCategory] ?? 0;
  434.     }
  435.     public function setTransactionService(TransactionService $transactionService): void
  436.     {
  437.         $this->transactionService = $transactionService;
  438.     }
  439.     public function getEmails(): ?string
  440.     {
  441.         return $this->emails;
  442.     }
  443.     public function setEmails(?string $emails): self
  444.     {
  445.         $this->emails = $emails;
  446.         return $this;
  447.     }
  448.     public function getJob(): ?Job
  449.     {
  450.         return $this->job;
  451.     }
  452.     public function setJob(?Job $job): self
  453.     {
  454.         $this->job = $job;
  455.         return $this;
  456.     }
  457.     public function getBirthday(): ?\DateTimeInterface
  458.     {
  459.         return $this->birthday;
  460.     }
  461.     public function setBirthday(?\DateTimeInterface $birthday): self
  462.     {
  463.         $this->birthday = $birthday;
  464.         return $this;
  465.     }
  466.     public function getStatus()
  467.     {
  468.         return $this->status;
  469.     }
  470.     public function setStatus($status): self
  471.     {
  472.         $this->status = $status;
  473.         return $this;
  474.     }
  475.     public function isIsUnsubscribed(): ?bool
  476.     {
  477.         return $this->isUnsubscribed;
  478.     }
  479.     public function setIsUnsubscribed(?bool $isUnsubscribed): self
  480.     {
  481.         if (!$isUnsubscribed) {
  482.             $this->setCanSendTelegramCampaigns(true);
  483.         }
  484.         $this->isUnsubscribed = $isUnsubscribed;
  485.         return $this;
  486.     }
  487.     public function getTgChannelAddErrorDate(): ?\DateTimeInterface
  488.     {
  489.         return $this->tgChannelAddErrorDate;
  490.     }
  491.     public function setTgChannelAddErrorDate(?\DateTimeInterface $tgChannelAddErrorDate): self
  492.     {
  493.         $this->tgChannelAddErrorDate = $tgChannelAddErrorDate;
  494.         return $this;
  495.     }
  496.     public function getTgChannelAddResponse(): ?string
  497.     {
  498.         return $this->tgChannelAddResponse;
  499.     }
  500.     public function setTgChannelAddResponse(?string $tgChannelAddResponse): self
  501.     {
  502.         $this->tgChannelAddResponse = $tgChannelAddResponse;
  503.         return $this;
  504.     }
  505.     public function getStudentLevelText(): string
  506.     {
  507.         if (!$this->getStudent()) {
  508.             return "нет уровня";
  509.         }
  510.         return $this->getStudent()->getLevelText();
  511.     }
  512.     public function archiveTelegramUserId()
  513.     {
  514.         $tgUserId = $this->getTelegramUserId();
  515.         $shouldArchive = !str_starts_with($tgUserId, "archived:");
  516.         if ($shouldArchive) {
  517.             $this->setTelegramUserId("archived:" . $tgUserId
  518.                 . "_" . date("Y-m-d_H-i-s")
  519.             );
  520.         }
  521.     }
  522.     public function getLearnerUrl(): string
  523.     {
  524.         return getenv("SERVER_URL")
  525.             . $this->router->generate("moderator_learner_edit",
  526.             ["id" => $this->getId()]);
  527.     }
  528.     public function getLearnerAnchorHtml(): string
  529.     {
  530.         $url = $this->getLearnerUrl();
  531.         $html = "<a href='" . $url . "'>" . $this . "</a>";
  532.         return $html;
  533.     }
  534.     public function setRouter(RouterInterface $router)
  535.     {
  536.         $this->router = $router;
  537.     }
  538.     public function getAppLinkHash(): ?string
  539.     {
  540.         return $this->appLinkHash;
  541.     }
  542.     public function setAppLinkHash(?string $appLinkHash): self
  543.     {
  544.         $this->appLinkHash = $appLinkHash;
  545.         return $this;
  546.     }
  547.     public function getAppLinkCreatedAt(): ?\DateTimeInterface
  548.     {
  549.         return $this->appLinkCreatedAt;
  550.     }
  551.     public function setAppLinkCreatedAt(?\DateTimeInterface $appLinkCreatedAt): self
  552.     {
  553.         $this->appLinkCreatedAt = $appLinkCreatedAt;
  554.         return $this;
  555.     }
  556.     public function getAppUserId(): ?string
  557.     {
  558.         return $this->appUserId;
  559.     }
  560.     public function setAppUserId(?string $appUserId): self
  561.     {
  562.         $this->appUserId = $appUserId;
  563.         return $this;
  564.     }
  565.     public function getAppLinkedAt(): ?\DateTimeInterface
  566.     {
  567.         return $this->appLinkedAt;
  568.     }
  569.     public function setAppLinkedAt(?\DateTimeInterface $appLinkedAt): self
  570.     {
  571.         $this->appLinkedAt = $appLinkedAt;
  572.         return $this;
  573.     }
  574.     public function isAppLinked(): bool
  575.     {
  576.         return (bool)$this->appUserId;
  577.     }
  578.     public function getAppLinkUrl(): ?string
  579.     {
  580.         if (!$this->appLinkHash) {
  581.             return null;
  582.         }
  583.         $base = rtrim((string)(getenv('APP_WEB_URL') ?: ($_ENV['APP_WEB_URL'] ?? $_SERVER['APP_WEB_URL'] ?? '')), '/');
  584.         if (!$base) {
  585.             $base = 'https://svetsoznaniya.ru';
  586.         }
  587.         return $base . '/crm-link/' . $this->appLinkHash;
  588.     }
  589. }