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.     public function __construct()
  119.     {
  120.         $this->createdAt = new \DateTime();
  121.     }
  122.     public function __toString()
  123.     {
  124.         return PersonService::getFirstLastNameWithOld($this) . " (" .
  125.             ($this->getStudent() ? $this->getStudent()->getLevelText() : "нет уровня") . ", " .
  126.             ($this->getCity() && trim($this->getCity()->getName()) ? $this->getCity()->getName() : "город не указан")
  127.             . ")";
  128.     }
  129.     public function getId(): ?int
  130.     {
  131.         return $this->id;
  132.     }
  133.     public function getFirstName(): ?string
  134.     {
  135.         return $this->firstName;
  136.     }
  137.     public function setFirstName(string $firstName): self
  138.     {
  139.         $this->firstName $firstName;
  140.         return $this;
  141.     }
  142.     public function getLastName(): ?string
  143.     {
  144.         return $this->lastName;
  145.     }
  146.     public function setLastName(string $lastName): self
  147.     {
  148.         $this->lastName $lastName;
  149.         return $this;
  150.     }
  151.     public function getStudent(): ?Student
  152.     {
  153.         return $this->student;
  154.     }
  155.     public function setStudent(?Student $student): self
  156.     {
  157.         $this->student $student;
  158.         return $this;
  159.     }
  160.     public function getName(array $options null): string
  161.     {
  162.         $options array_merge(
  163.             [
  164.                 "lastNameFirst" => false
  165.             ], ($options ?? [])
  166.         );
  167.         $name = ($options["lastNameFirst"] ? $this->getLastFirstName() : $this->firstName ' ' $this->lastName );
  168.         return $name;
  169.     }
  170.     public function isSameName($name): bool
  171.     {
  172.         $nameParts explode(' '$name);
  173.         if (count($nameParts) === 2) {
  174.             $firstName $nameParts[0];
  175.             $lastName $nameParts[1];
  176.             $firstName mb_strtolower($firstName'UTF-8');
  177.             $lastName mb_strtolower($lastName'UTF-8');
  178.             $currentFirstName mb_strtolower($this->firstName'UTF-8');
  179.             $currentLastName mb_strtolower($this->lastName'UTF-8');
  180.             return ($firstName == $currentFirstName && $lastName == $currentLastName)
  181.                 || ($firstName == $currentLastName && $lastName == $currentFirstName);
  182.         } else {
  183.             return false;
  184.         }
  185.     }
  186.     public function getLastFirstName(): string
  187.     {
  188.         return $this->lastName ' ' $this->firstName;
  189.     }
  190.     public function getLastName2FirstName(): string
  191.     {
  192.         return $this->lastName2 ' ' $this->firstName;
  193.     }
  194.     public function getNameAndCityText(): string
  195.     {
  196.         return $this->getName() . " (" .
  197.             ($this->getCity() && trim($this->getCity()->getName()) ? $this->getCity()->getName() : "город не указан")
  198.             . ")";
  199.     }
  200.     public function getCityText(): string
  201.     {
  202.         return $this->getCity() ? $this->getCity()->getName() : "город не указан";
  203.     }
  204.     public function getCity(): ?City
  205.     {
  206.         return $this->city;
  207.     }
  208.     public function setCity(?City $city): self
  209.     {
  210.         $this->city $city;
  211.         return $this;
  212.     }
  213.     public function getPhone(): ?string
  214.     {
  215.         return $this->phone;
  216.     }
  217.     public function setPhone(?string $phone): self
  218.     {
  219.         $this->phone $phone;
  220.         return $this;
  221.     }
  222.     public function getEmail($skipErrors true): ?string
  223.     {
  224.         $email $this->email ?? "";
  225.         if (!$skipErrors && preg_match('/\s|[,]/'$email)) {
  226.             throw new \Exception("E-mail contains restricted characters: " $email);
  227.         }
  228.         return $this->email;
  229.     }
  230.     public function setEmail(?string $email): self
  231.     {
  232.         $this->email $email;
  233.         return $this;
  234.     }
  235.     public function getImage(): ?Image
  236.     {
  237.         return $this->image;
  238.     }
  239.     public function setImage(?Image $image): self
  240.     {
  241.         $this->image $image;
  242.         return $this;
  243.     }
  244.     public function getComment(): ?string
  245.     {
  246.         return $this->comment;
  247.     }
  248.     public function setComment(?string $comment): self
  249.     {
  250.         $this->comment $comment;
  251.         return $this;
  252.     }
  253.     public function addComment(string $textbool $checkContains true,
  254.                                bool $ucfirst falsebool $addDot false): void
  255.     {
  256.         if ($ucfirst) {
  257.             $text mb_ucfirst($text'UTF-8');
  258.         }
  259.         if ($addDot && mb_substr($text, -1null'UTF-8') !== '.') {
  260.             $text .= '.';
  261.         }
  262.         $newComment trim($this->comment ?? "");
  263.         if (!$checkContains || mb_strpos($newComment $textnull'UTF-8') === false) {
  264.             $newComment $newComment
  265.                 . ($addDot && $newComment && mb_substr($newComment, -1null'UTF-8') !== '.' '.' '')
  266.                 . ($newComment ?  "\n" "")
  267.                 . $text;
  268.             $this->setComment($newComment);
  269.         }
  270.     }
  271.     public function getLastName2(): ?string
  272.     {
  273.         return $this->lastName2;
  274.     }
  275.     public function setLastName2(?string $lastName2): self
  276.     {
  277.         $this->lastName2 $lastName2;
  278.         return $this;
  279.     }
  280.     public function getTelegramUserId(): ?string
  281.     {
  282.         return $this->telegramUserId;
  283.     }
  284.     public function setTelegramUserId(?string $telegramUserId): self
  285.     {
  286.         $this->telegramUserId $telegramUserId;
  287.         return $this;
  288.     }
  289.     public function isCanSendTelegramCampaigns(): ?bool
  290.     {
  291.         if ($this->isUnsubscribed) {
  292.             return false;
  293.         }
  294.         return $this->canSendTelegramCampaigns;
  295.     }
  296.     public function setCanSendTelegramCampaigns(?bool $canSendTelegramCampaigns): self
  297.     {
  298.         $this->canSendTelegramCampaigns $canSendTelegramCampaigns;
  299.         return $this;
  300.     }
  301.     public function getMessageReceiver(): ?self
  302.     {
  303.         return $this->messageReceiver;
  304.     }
  305.     public function setMessageReceiver(?self $messageReceiver): self
  306.     {
  307.         $this->messageReceiver $messageReceiver;
  308.         return $this;
  309.     }
  310.     public function isIsStudent(): ?bool
  311.     {
  312.         return $this->isStudent;
  313.     }
  314.     public function setIsStudent(?bool $isStudent): self
  315.     {
  316.         $this->isStudent $isStudent;
  317.         return $this;
  318.     }
  319.     public function isIsSubscriber(): ?bool
  320.     {
  321.         return $this->isSubscriber;
  322.     }
  323.     public function setIsSubscriber(?bool $isSubscriber): self
  324.     {
  325.         $this->isSubscriber $isSubscriber;
  326.         return $this;
  327.     }
  328.     public function getVirtualStudentStatus(): ?string
  329.     {
  330.         if ($this->getStatus() == \App\Enum\Person\Status::STATUS_DELETED) {
  331.             return Status::STATUS_DELETED;
  332.         }
  333.         if ($this->isIsStudent()) {
  334.             return $this->getStudent()->getStatus();
  335.         } elseif ($this->isIsSubscriber()) {
  336.             return VirtualStatus::VIRTUAL_STATUS_SUBSCRIBER;
  337.         }
  338.         return null;
  339.     }
  340.     public function setVirtualStudentStatus(?string $virtualStatus): void
  341.     {
  342. //        throw new \Exception("Logic was moved to controller");
  343.     }
  344.     public function getVirtualStudentStatusText(): ?string
  345.     {
  346.         if (Status::isCorrect($this->getVirtualStudentStatus())) {
  347.             if ($this->getStatus() == \App\Enum\Person\Status::STATUS_DELETED) {
  348.                 return \App\Enum\Person\Status::getText($this->getStatus());
  349.             }
  350.             return $this->getStudent()->getStatusText();
  351.         }
  352.         return VirtualStatus::getText($this->getVirtualStudentStatus());
  353.     }
  354.     public function getVirtualStudentStatusCssClass($prefix null): ?string
  355.     {
  356. //        try {
  357.             if (Status::isCorrect($this->getVirtualStudentStatus())) {
  358.                 if ($this->getStatus() == \App\Enum\Person\Status::STATUS_DELETED) {
  359.                     return \App\Enum\Person\Status::getCssClass($this->getStatus(), $prefix);
  360.                 }
  361.                 return $this->getStudent()->getStatusCssClass($prefix);
  362.             }
  363.             return VirtualStatus::getCssClass($this->getVirtualStudentStatus(), $prefix);
  364. //        } catch (\Throwable $e) {
  365. //            dd($this->getVirtualStudentStatus(), $this->getStudent());
  366. //        }
  367.     }
  368.     public function getSurname(): ?string
  369.     {
  370.         return $this->surname;
  371.     }
  372.     public function setSurname(?string $surname): self
  373.     {
  374.         $this->surname $surname;
  375.         return $this;
  376.     }
  377.     public function getFullNameWithSurname(): string
  378.     {
  379.         return $this->getName() . ($this->surname " " $this->surname "");
  380.     }
  381.     public function getFullNameWithSurnameAndCityText(): string
  382.     {
  383.         return $this->getFullNameWithSurname() . " (" .
  384.             ($this->getCity() && trim($this->getCity()->getName()) ? $this->getCity()->getName() : "город не указан")
  385.             . ")";
  386.     }
  387.     public function getCreatedAt(): ?\DateTimeInterface
  388.     {
  389.         return $this->createdAt;
  390.     }
  391.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  392.     {
  393.         $this->createdAt $createdAt;
  394.         return $this;
  395.     }
  396.     public function isCalendarEventStatusAllowed(CalendarEvent $calendarEvent): bool
  397.     {
  398.         if (!$calendarEvent->getRequireStudentStatus()) {
  399.             return true;
  400.         }
  401.         return $this->getVirtualStudentStatus() == $calendarEvent->getRequireStudentStatus();
  402.     }
  403.     public function getBalance($forceUpdate false): array
  404.     {
  405.         if ($this->balance === null || $forceUpdate) {
  406.             $this->setBalances();
  407.         }
  408.         return $this->balance;
  409.     }
  410.     public function getBalanceNoInvoices($forceUpdate false): array
  411.     {
  412.         if ($this->balanceNoInvoices === null || $forceUpdate) {
  413.             $this->setBalances();
  414.         }
  415.         return $this->balanceNoInvoices;
  416.     }
  417.     private function setBalances()
  418.     {
  419.         $balanceResult $this->transactionService->getPersonBalance($this);
  420.         $this->balance $balanceResult['balance'];
  421.         $this->balanceNoInvoices $balanceResult['balanceNoInvoices'];
  422.     }
  423.     /**
  424.      * @param FinanceCategory|int|null $financeCategory
  425.      * @param $forceUpdate
  426.      * @return int
  427.      */
  428.     public function getBalanceByFinanceCategory($financeCategory,
  429.                                                 $forceUpdate false): int
  430.     {
  431.         if (!$financeCategory) {
  432.             return 0;
  433.         }
  434.         $balance $this->getBalance($forceUpdate);
  435.         if (!is_numeric($financeCategory)) {
  436.             $financeCategory $financeCategory->getId();
  437.         }
  438.         return $balance[$financeCategory] ?? 0;
  439.     }
  440.     public function setTransactionService(TransactionService $transactionService): void
  441.     {
  442.         $this->transactionService $transactionService;
  443.     }
  444.     public function getEmails(): ?string
  445.     {
  446.         return $this->emails;
  447.     }
  448.     public function setEmails(?string $emails): self
  449.     {
  450.         $this->emails $emails;
  451.         return $this;
  452.     }
  453.     public function getJob(): ?Job
  454.     {
  455.         return $this->job;
  456.     }
  457.     public function setJob(?Job $job): self
  458.     {
  459.         $this->job $job;
  460.         return $this;
  461.     }
  462.     public function getBirthday(): ?\DateTimeInterface
  463.     {
  464.         return $this->birthday;
  465.     }
  466.     public function setBirthday(?\DateTimeInterface $birthday): self
  467.     {
  468.         $this->birthday $birthday;
  469.         return $this;
  470.     }
  471.     public function getStatus()
  472.     {
  473.         return $this->status;
  474.     }
  475.     public function setStatus($status): self
  476.     {
  477.         $this->status $status;
  478.         return $this;
  479.     }
  480.     public function isIsUnsubscribed(): ?bool
  481.     {
  482.         return $this->isUnsubscribed;
  483.     }
  484.     public function setIsUnsubscribed(?bool $isUnsubscribed): self
  485.     {
  486.         if (!$isUnsubscribed) {
  487.             $this->setCanSendTelegramCampaigns(true);
  488.         }
  489.         $this->isUnsubscribed $isUnsubscribed;
  490.         return $this;
  491.     }
  492. }