<?php
namespace App\Entity;
use App\Entity\Client\Client2;
use App\Entity\Notification\NotaryNotification;
use App\Enum\NotaryCompanyRegistrationStatus;
use App\Model\BitrixSerialize;
use App\Repository\NotaryRepository;
use App\Service\BitrixSender;
use App\ValueObject\NotaryJusticeMinistryNumber;
use App\ValueObject\NotaryNumberUis;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Exception;
use JMS\Serializer\Annotation as JMS;
/**
* @ORM\Entity(repositoryClass=NotaryRepository::class)
*/
class Notary extends User implements \JsonSerializable, BitrixSerialize
{
/** Конфигурация страниц краткого заключения по умолчанию */
private const ANNOUNCEMENT_CONFIG_DEFAULT = '1,2,3,-7';
/** Ничего не показывать **/
public const WELCOME_MODAL_NONE = 0;
/** Ново рег через инофонот **/
public const WELCOME_MODAL_INFO_NOT_NEW = 1;
/** Привязка существующей записи к инофонот **/
public const WELCOME_MODAL_INFO_NOT_CONNECTED = 2;
/**
* Стартовый невыводимый бонус для нотариусов
*/
public const START_BONUS_BALANCE = 0;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*
* @JMS\Type("string")
*/
private $name;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*
* @JMS\Type("string")
*/
private $refovodCode;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*
* @JMS\Type("string")
*/
private $surname;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*
* @JMS\Type("string")
*/
private $patronymic;
/**
* @ORM\Column(type="string", length=255)
*
* @JMS\Type("string")
*/
private $phone;
/**
* @ORM\OneToMany(targetEntity=Client::class, mappedBy="notary")
*
* @JMS\Exclude()
*/
private $clients;
/**
* @var Collection<int, Client2>
* @ORM\OneToMany(targetEntity=\App\Entity\Client\Client2::class, mappedBy="partner")
* @ORM\OrderBy({"id" = "DESC"})
* @JMS\Exclude()
*/
private $clients2;
/**
* @ORM\Column(type="string", length=255)
*
* @JMS\Type("string")
*/
private $notaryNumber;
/**
* @ORM\Column(type="integer", nullable=true)
*
* @JMS\Exclude()
*/
private $moneyBalance;
/**
* @ORM\Column(type="float", scale=2, nullable=true)
*
* @JMS\Exclude()
*/
private $bonusBalance;
/**
* @ORM\Column(type="float", scale=2, nullable=true)
*
* @JMS\Exclude()
*/
private $undrawableBonusBalance;
/**
* @ORM\Column(type="string", length=1255, nullable=true)
*
* @JMS\Type("string")
*/
private $address;
/**
* @ORM\OneToMany(targetEntity=BalanceOperation::class, mappedBy="notary")
*
* @JMS\Exclude()
*/
protected $balanceOperations;
/**
* @ORM\Column(type="integer", nullable=true)
*
* @JMS\Exclude()
*/
protected $bitrixCompanyId;
/**
* @ORM\OneToOne(targetEntity=Worker::class, mappedBy="notary", cascade={"persist", "remove"})
*
* @JMS\Exclude()
*/
private $worker;
/**
* @ORM\OneToOne(targetEntity=Referal::class, mappedBy="referalNotary")
*
* @JMS\Exclude()
*/
private $referal;
/**
* @ORM\OneToOne(targetEntity=Refovod::class, mappedBy="refovodNotary")
*
* @JMS\Exclude()
*/
private $refovod;
/**
* @ORM\OneToMany(targetEntity=NotaryNotification::class, mappedBy="notary")
*
* @JMS\Exclude()
*/
private $notaryNotifications;
/**
* @ORM\Column(type="boolean", nullable=true)
*
* @JMS\Exclude()
*/
private $isVip;
/**
* @ORM\Column(type="boolean")
*
* @JMS\Exclude()
*/
private $payLinkReferralEnabled = false;
/**
* @ORM\Column(type="datetime")
*
* @JMS\Exclude()
*/
private $registrationDateTime;
/**
* @ORM\Column(type="boolean", nullable=true)
*
* @JMS\Exclude()
*/
private $hideBanner;
/**
* @ORM\Column(type="boolean", options={"default" : false}, nullable=false)
*
* @JMS\Exclude()
*/
private $isDevelop = false;
/**
* @ORM\Column(type="string", length=1255, nullable=true)
*
* @JMS\Type("string")
*/
private $bonusProgramm;
/**
* @ORM\Column(type="string", length=255, options={"default" : "1,2,3,-7"}, nullable=false)
*
* @JMS\Type("string")
*/
private string $announcementConfig = self::ANNOUNCEMENT_CONFIG_DEFAULT;
/**
* @ORM\Column(type="string", length=1255, nullable=true, options={"comment": "Идентификатор нотариуса в единой информационной системе нотариата России, хотя изначально заводилось только как идентификатор в Инфонот/Экспресс"})
*
* @JMS\Exclude()
*/
private $infoNotAccountId = null;
/**
* @ORM\Column(type="string", length=255, nullable=true, options={"comment": "Номер регистрации нотариуса в министерстве юстиции"})
* @JMS\Exclude()
*/
private $justiceMinistryNumber = null;
/**
* @ORM\Column(type="integer", options={"default" : 0}, nullable=false)
*
* @JMS\Exclude()
*/
private int $showWelcomeModal = 0;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $connectToInfonotDateTime;
/**
* @ORM\ManyToOne(targetEntity=Service::class, inversedBy="notaries")
* @ORM\JoinColumn(nullable=true)
*/
private ?Service $service = null;
/**
* @ORM\OneToOne(targetEntity=NotaryAddress::class, mappedBy="notary", cascade={"persist", "remove"})
* @JMS\Exclude()
*/
private ?NotaryAddress $notaryAddress = null;
public function __construct()
{
$this->clients = new ArrayCollection();
$this->clients2 = new ArrayCollection();
$this->balanceOperations = new ArrayCollection();
$this->notaryNotifications = new ArrayCollection();
}
/**
* @return mixed
*/
public function getRegistrationDateTime(): ?\DateTimeInterface
{
return $this->registrationDateTime;
}
/**
* @param \DateTimeInterface $sendDateTime
* @return $this
*/
public function setRegistrationDateTime(\DateTimeInterface $sendDateTime): self
{
$this->registrationDateTime = $sendDateTime;
return $this;
}
/**
* @return Collection<int, Client2>
*/
public function getClients2(): Collection
{
return $this->clients2;
}
/**
* @return Collection|Client[]
*/
public function getClients(): Collection
{
return $this->clients;
}
public function addClient(Client $client): self
{
if (!$this->clients->contains($client)) {
$this->clients[] = $client;
$client->setNotary($this);
}
return $this;
}
public function removeClient(Client $client): self
{
if ($this->clients->contains($client)) {
$this->clients->removeElement($client);
// set the owning side to null (unless already changed)
if ($client->getNotary() === $this) {
$client->setNotary(null);
}
}
return $this;
}
public function getNotaryNumber(): ?string
{
return $this->notaryNumber;
}
public function setNotaryNumber(string $notaryNumber): self
{
$this->notaryNumber = $notaryNumber;
return $this;
}
public function getBalance(): ?float
{
return $this->getBonusBalance() + $this->getMoneyBalance() + $this->getUndrawableBonusBalance();
}
public function getMoneyBalance(): int
{
return $this->moneyBalance ?? 0;
}
public function setMoneyBalance(?int $moneyBalance): self
{
$this->moneyBalance = $moneyBalance;
return $this;
}
public function addMoneyToBalance(int $money): self
{
$this->moneyBalance = $this->getMoneyBalance() + $money;
return $this;
}
/**
* @return mixed
*/
public function getName()
{
return $this->name;
}
/**
* @param mixed $name
*/
public function setName($name): void
{
$this->name = $name;
}
/**
* @return mixed
*/
public function getSurname()
{
return $this->surname;
}
/**
* @param mixed $surname
*/
public function setSurname($surname): void
{
$this->surname = $surname;
}
/**
* @return mixed
*/
public function getPatronymic()
{
return $this->patronymic;
}
/**
* @param mixed $patronymic
*/
public function setPatronymic($patronymic): void
{
$this->patronymic = $patronymic;
}
/**
* @return mixed
*/
public function getPhone()
{
return $this->phone;
}
/**
* @param mixed $phone
*/
public function setPhone($phone): void
{
$this->phone = $phone;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
if (!in_array('ROLE_REFOVOD', $roles)) {
$roles[] = 'ROLE_NOTARY';
}
return array_unique($roles);
}
public function isWorker(): bool
{
return in_array('ROLE_WORKER', $this->getRoles());
}
/**
* @return float|null
*/
public function getUndrawableBonusBalance(): ?float
{
return $this->undrawableBonusBalance ?? 0;
}
/**
* @param ?float $undrawableBonusBalance
* @return Notary
*/
public function setUndrawableBonusBalance(?float $undrawableBonusBalance): self
{
$this->undrawableBonusBalance = $undrawableBonusBalance;
return $this;
}
public function jsonSerialize(): array
{
return [
'id' => $this->getId(),
'email' => $this->getEmail(),
'name' => $this->getName(),
'patronymic' => $this->getPatronymic(),
'surname' => $this->getSurname(),
'phone' => $this->getPhone(),
'refovodCode' => $this->getRefovodCode(),
'address' => $this->getAddress(),
];
}
public function getAddress(): ?string
{
return $this->address;
}
public function setAddress(?string $address): self
{
$this->address = $address;
return $this;
}
public function getRefovodCode(): ?string
{
return $this->refovodCode;
}
public function setRefovodCode(?string $refovodCode): self
{
$this->refovodCode = $refovodCode;
return $this;
}
/**
* @return Collection|BalanceOperation[]
*/
public function getBalanceOperations(): Collection
{
return $this->balanceOperations;
}
public function addBalanceOperation(BalanceOperation $balanceOperation): self
{
if (!$this->balanceOperations->contains($balanceOperation)) {
$this->balanceOperations[] = $balanceOperation;
$balanceOperation->setNotary($this);
}
return $this;
}
public function removeBalanceOperation(BalanceOperation $balanceOperation): self
{
if ($this->balanceOperations->contains($balanceOperation)) {
$this->balanceOperations->removeElement($balanceOperation);
// set the owning side to null (unless already changed)
if ($balanceOperation->getNotary() === $this) {
$balanceOperation->setNotary(null);
}
}
return $this;
}
/**
* @return mixed
*/
public function getBonusBalance()
{
return $this->bonusBalance ?? 0;
}
/**
* @param mixed $bonusBalance
*/
public function setBonusBalance($bonusBalance): void
{
$this->bonusBalance = $bonusBalance;
}
public function getBitrixCompanyId(): ?int
{
return $this->bitrixCompanyId;
}
public function setBitrixCompanyId(?int $bitrixCompanyId): self
{
$this->bitrixCompanyId = $bitrixCompanyId;
return $this;
}
/**
* @return array
*/
public function serializeBitrix(): array
{
return [
'NAME' => $this->getName(),
'SECOND_NAME' => $this->getPatronymic(),
'LAST_NAME' => $this->getSurname(),
'EMAIL' => [
"n0" => [
"VALUE" => $this->getEmail(),
"VALUE_TYPE" => "WORK",
]
],
'ADDRESS' => $this->getAddress(),
'PHONE' => [
'n0' => [
"VALUE" => $this->formatPhoneToB24(),
"VALUE_TYPE" => "WORK",
]
],
"NOTARY_NUMBER" => $this->getNotaryNumber(),
"TYPE_ID" => "CLIENT",
"UF_CRM_ISBANNED" => $this->isBanned(),
];
}
/**
* Сериализация для отправки данных компании
*
* @return array
*/
public function serializeBitrixCompany()
{
$data = [
"COMPANY_TYPE" => "1",
"TITLE" => 'Нотариус ' . $this->getFIO(),
"ADDRESS" => $this->getAddress(),
"HAS_EMAIL" => "Y",// Если есть емл.
"ASSIGNED_BY_ID" => BitrixSender::ASSIGNED_SUPER_ADMIN_IN_BITRIX,
'EMAIL' => [
"n0" => [
"VALUE" => $this->getEmail(),
"VALUE_TYPE" => "WORK",
]
],
"UF_CRM_NOTARYNUMBER" => $this->getNotaryNumberUis()?->value,
"UF_CRM_BONUSACCOUNT" => $this->getBonusBalance() ?? 0,
"UF_CRM_UNDRAWABLEBONUSACCOUNT" => $this->getUndrawableBonusBalance() ?? 0,
"UF_CRM_MAINACCOUNT" => $this->getBalance() ?? 0,
"UF_CRM_MONEYACCOUNT" => $this->getMoneyBalance() ?? 0,
"UF_CRM_ISBANNED" => $this->isBanned(),
"UF_CRM_JUSTICE_MINISTRY_NUMBER" => $this->getJusticeMinistryNumber()?->value,
"UF_CRM_SERVICE_ID" => $this->getId(),
];
// DEV-350
if ($this->isRealNotary()) {
$data['UF_CRM_REGISTRATION_STATUS'] = NotaryCompanyRegistrationStatus::REGISTERED->value;
}
if ($this->getPhone()) {
$data["HAS_PHONE"] = "Y";
$data['PHONE'] = [
'n0' => [
"VALUE" => $this->getPhone(),
"VALUE_TYPE" => "WORK",
],
];
}
$data["UF_CRM_REFERAL"] = "N";
if ($referal = $this->getReferal()) {
$data["UF_CRM_REFERAL"] = "Y";
$data["UF_CRM_REFERAL_REFERER"] = $referal->getRefovod()->getRefovodNotary()->getBitrixCompanyId();
}
$data["UF_CRM_REFERER"] = "N";
if ($refovod = $this->getRefovod()) {
$data["UF_CRM_REFERER"] = "Y";
$data['UF_CRM_REFERER_REFERALS'] = false;
if ($referals = $refovod->getReferals()) {
$data['UF_CRM_REFERER_REFERALS'] = false;
if (count($referals) > 0) {
foreach ($referals as $referal) {
$data['UF_CRM_REFERER_REFERALS'][] = $referal->getReferalNotary()->getBitrixCompanyId();
}
}
}
}
return $data;
}
public function getWorker(): ?Worker
{
return $this->worker;
}
public function setWorker(?Worker $worker): self
{
$this->worker = $worker;
// set (or unset) the owning side of the relation if necessary
$newNotary = null === $worker ? null : $this;
if ($worker->getNotary() !== $newNotary) {
$worker->setNotary($newNotary);
}
return $this;
}
public function getReferal(): ?Referal
{
return $this->referal;
}
public function setReferal(?Referal $referal): self
{
$this->referal = $referal;
// set (or unset) the owning side of the relation if necessary
$newReferalNotary = null === $referal ? null : $this;
if ($referal && $referal->getReferalNotary() !== $newReferalNotary) {
$referal->setReferalNotary($newReferalNotary);
}
return $this;
}
public function getRefovod(): ?Refovod
{
return $this->refovod;
}
public function setRefovod(?Refovod $refovod): self
{
$this->refovod = $refovod;
// set (or unset) the owning side of the relation if necessary
$newRefovodNotary = null === $refovod ? null : $this;
if ($refovod->getRefovodNotary() !== $newRefovodNotary) {
$refovod->setRefovodNotary($newRefovodNotary);
}
return $this;
}
/**
* @return Collection|NotaryNotification[]
*/
public function getNotaryNotifications(): Collection
{
return $this->notaryNotifications;
}
public function addNotaryNotification(NotaryNotification $notaryNotification): self
{
if (!$this->notaryNotifications->contains($notaryNotification)) {
$this->notaryNotifications[] = $notaryNotification;
$notaryNotification->setNotary($this);
}
return $this;
}
public function removeNotaryNotification(NotaryNotification $notaryNotification): self
{
if ($this->notaryNotifications->removeElement($notaryNotification)) {
// set the owning side to null (unless already changed)
if ($notaryNotification->getNotary() === $this) {
$notaryNotification->setNotary(null);
}
}
return $this;
}
public function getIsVip(): ?bool
{
return $this->isVip;
}
public function setIsVip(?bool $isVip): self
{
$this->isVip = $isVip;
return $this;
}
public function getPayLinkReferralEnabled(): ?bool
{
return $this->payLinkReferralEnabled;
}
public function setPayLinkReferralEnabled(bool $payLinkReferralEnabled): self
{
$this->payLinkReferralEnabled = $payLinkReferralEnabled;
return $this;
}
public function getHideBanner(): ?bool
{
return $this->hideBanner;
}
public function setHideBanner(?bool $hideBanner): self
{
$this->hideBanner = $hideBanner;
return $this;
}
/**
* @return int
*/
public function getShowWelcomeModal()
{
return $this->showWelcomeModal ?? 0;
}
/**
* @param mixed $showWelcomeModal
* @return Notary
*/
public function setShowWelcomeModal(int $showWelcomeModal)
{
$this->showWelcomeModal = $showWelcomeModal;
return $this;
}
/**
* @return bool
*/
public function isDevelop(): bool
{
return $this->isDevelop;
}
/**
* @param bool $isDevelop
* @return Notary
*/
public function setIsDevelop(bool $isDevelop): Notary
{
$this->isDevelop = $isDevelop;
return $this;
}
private function formatPhoneToB24(): string
{
$str = $this->getPhone() ?? '';
return preg_replace("/[^+0-9]/", '', $str);
}
/**
* @return mixed
*/
public function getBonusProgramm()
{
return $this->bonusProgramm ?? '';
}
/**
* @param mixed $bonusProgramm
* @return Notary
*/
public function setBonusProgramm($bonusProgramm)
{
$this->bonusProgramm = $bonusProgramm;
return $this;
}
/**
* @return mixed
* @deprecated Использовать getNotaryNumberUis
*/
public function getInfoNotAccountId()
{
return $this->infoNotAccountId;
}
/**
* @param mixed $infoNotAccountId
* @return Notary
* @deprecated Использовать setNotaryNumberUis
*/
public function setInfoNotAccountId($infoNotAccountId)
{
$this->infoNotAccountId = $infoNotAccountId;
return $this;
}
public function setNotaryNumberUis(NotaryNumberUis $notaryNumberUis): self
{
$this->infoNotAccountId = $notaryNumberUis->value;
if (!$this->notaryNumber) {
$this->notaryNumber = $notaryNumberUis->value;
}
return $this;
}
public function getNotaryNumberUis(): ?NotaryNumberUis
{
try {
return NotaryNumberUis::fromString($this->infoNotAccountId);
} catch (\Throwable $_) {
}
try {
return NotaryNumberUis::fromString($this->notaryNumber);
} catch (\Throwable $_) {
return null;
}
}
public function setJusticeMinistryNumber(NotaryJusticeMinistryNumber $number): self
{
$this->justiceMinistryNumber = $number->value;
return $this;
}
public function getJusticeMinistryNumber(): ?NotaryJusticeMinistryNumber
{
return $this->justiceMinistryNumber ? NotaryJusticeMinistryNumber::fromString($this->justiceMinistryNumber) : null;
}
public function getSurnameAndInitials(): string
{
return "{$this->getSurname()} " . mb_substr($this->getName(), 0, 1) . '.' . mb_substr($this->getPatronymic(), 0, 1);
}
public function getFIO(): string
{
return "{$this->getSurname()} {$this->getName()} {$this->getPatronymic()}";
}
public function jsonSerializeInfoNot(): array
{
return [
'id' => $this->getNotaryNumberUis()?->value,
'balance' => $this->getBalance(),
];
}
public function getConnectToInfonotDateTime(): ?\DateTimeInterface
{
return $this->connectToInfonotDateTime;
}
public function setConnectToInfonotDateTime(?\DateTimeInterface $connectToInfonotDateTime): self
{
$this->connectToInfonotDateTime = $connectToInfonotDateTime;
return $this;
}
public function getAnnouncementConfig(): string
{
return $this->announcementConfig;
}
public function setAnnouncementConfig(string $announcementConfig): self
{
$this->announcementConfig = $announcementConfig;
return $this;
}
public function getService(): ?Service
{
return $this->service;
}
public function setService(?Service $service): self
{
$this->service = $service;
return $this;
}
public function isRealNotary()
{
return !empty($this->getNotaryNumber())
|| !empty($this->getJusticeMinistryNumber());
}
public function getNotaryAddress(): ?NotaryAddress
{
return $this->notaryAddress;
}
public function setNotaryAddress(?NotaryAddress $notaryAddress): self
{
$this->notaryAddress = $notaryAddress;
// set (or unset) the owning side of the relation if necessary
$newNotary = null === $notaryAddress ? null : $this;
if ($notaryAddress && $notaryAddress->getNotary() !== $newNotary) {
$notaryAddress->setNotary($newNotary);
}
return $this;
}
}