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