src/Entity/Partner.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\PartnerInfo\PartnerCompany;
  4. use App\Entity\PartnerInfo\PartnerInfo;
  5. use App\Entity\PartnerInfo\PartnerIP;
  6. use App\Entity\Price\PartnerComplexPrice;
  7. use App\Enum\Bitrix\CompanyType;
  8. use App\Enum\NotaryCompanyRegistrationStatus;
  9. use App\Model\BitrixSerialize;
  10. use App\Repository\PartnerRepository;
  11. use App\Service\BitrixSender;
  12. use Doctrine\Common\Collections\ArrayCollection;
  13. use Doctrine\Common\Collections\Collection;
  14. use Doctrine\ORM\Mapping as ORM;
  15. /**
  16.  * @ORM\Entity(repositoryClass=PartnerRepository::class)
  17.  */
  18. class Partner extends User
  19. {
  20.     /**
  21.      * @ORM\Column(type="integer")
  22.      *
  23.      * По-умолчанию для партнеров направление 3 - для оценщика
  24.      * @var int
  25.      */
  26.     protected $selectedServiceGroupId 3;
  27.     /**
  28.      * @ORM\Column(type="string", length=255, nullable=false)
  29.      *
  30.      */
  31.     private $publicName;
  32.     /**
  33.      * @ORM\OneToMany(targetEntity=Worker::class, mappedBy="partner")
  34.      */
  35.     private $workers;
  36.     /**
  37.      * @ORM\ManyToOne(targetEntity=Region::class, inversedBy="partners")
  38.      * @ORM\JoinColumn(nullable=false)
  39.      */
  40.     private $region;
  41.     /**
  42.      * @ORM\OneToMany(targetEntity=PartnerComplexPrice::class, mappedBy="partner")
  43.      */
  44.     private $partnerComplexPrices;
  45.     /**
  46.      * @ORM\OneToOne(targetEntity=PartnerInfo::class, cascade={"persist", "remove"})
  47.      */
  48.     private $information;
  49.     /**
  50.      * @ORM\OneToMany(targetEntity=WorkerMailRequest::class, mappedBy="partner", orphanRemoval=true)
  51.      */
  52.     private $workerMailRequests;
  53.     /**
  54.      * @ORM\Column(type="integer", nullable=true)
  55.      */
  56.     private $bitrixId;
  57.     /**
  58.      * @ORM\ManyToOne(targetEntity=Service::class, inversedBy="partners")
  59.      * @ORM\JoinColumn(nullable=true)
  60.      */
  61.     private ?Service $service null;
  62.     public function __construct()
  63.     {
  64.         $this->workers = new ArrayCollection();
  65.         $this->partnerComplexPrices = new ArrayCollection();
  66.         $this->workerMailRequests = new ArrayCollection();
  67.     }
  68.     /**
  69.      * @return Collection|Worker[]
  70.      */
  71.     public function getWorkers(): Collection
  72.     {
  73.         return $this->workers;
  74.     }
  75.     public function addWorker(Worker $worker): self
  76.     {
  77.         if (!$this->workers->contains($worker)) {
  78.             $this->workers[] = $worker;
  79.             $worker->setPartner($this);
  80.         }
  81.         return $this;
  82.     }
  83.     public function removeWorker(Worker $worker): self
  84.     {
  85.         if ($this->workers->contains($worker)) {
  86.             $this->workers->removeElement($worker);
  87.             // set the owning side to null (unless already changed)
  88.             if ($worker->getPartner() === $this) {
  89.                 $worker->setPartner(null);
  90.             }
  91.         }
  92.         return $this;
  93.     }
  94.     public function getRegion(): ?Region
  95.     {
  96.         return $this->region;
  97.     }
  98.     public function setRegion(?Region $region): self
  99.     {
  100.         $this->region $region;
  101.         return $this;
  102.     }
  103.     /**
  104.      * @return Collection|PartnerComplexPrice[]
  105.      */
  106.     public function getPartnerComplexPrices(): Collection
  107.     {
  108.         return $this->partnerComplexPrices;
  109.     }
  110.     public function addPartnerComplexPrice(PartnerComplexPrice $partnerComplexPrice): self
  111.     {
  112.         if (!$this->partnerComplexPrices->contains($partnerComplexPrice)) {
  113.             $this->partnerComplexPrices[] = $partnerComplexPrice;
  114.             $partnerComplexPrice->setPartner($this);
  115.         }
  116.         return $this;
  117.     }
  118.     public function removePartnerComplexPrice(PartnerComplexPrice $partnerComplexPrice): self
  119.     {
  120.         if ($this->partnerComplexPrices->contains($partnerComplexPrice)) {
  121.             $this->partnerComplexPrices->removeElement($partnerComplexPrice);
  122.             // set the owning side to null (unless already changed)
  123.             if ($partnerComplexPrice->getPartner() === $this) {
  124.                 $partnerComplexPrice->setPartner(null);
  125.             }
  126.         }
  127.         return $this;
  128.     }
  129.     /**
  130.      * @return mixed
  131.      */
  132.     public function getPublicName()
  133.     {
  134.         return $this->publicName;
  135.     }
  136.     /**
  137.      * @param mixed $publicName
  138.      */
  139.     public function setPublicName($publicName): void
  140.     {
  141.         $this->publicName $publicName;
  142.     }
  143.     public function getInformation(): ?PartnerInfo
  144.     {
  145.         return $this->information;
  146.     }
  147.     public function setInformation(?PartnerInfo $information): self
  148.     {
  149.         $this->information $information;
  150.         return $this;
  151.     }
  152.     /**
  153.      * @return Collection|WorkerMailRequest[]
  154.      */
  155.     public function getWorkerMailRequests(): Collection
  156.     {
  157.         return $this->workerMailRequests;
  158.     }
  159.     public function addWorkerMailRequest(WorkerMailRequest $workerMailRequest): self
  160.     {
  161.         if (!$this->workerMailRequests->contains($workerMailRequest)) {
  162.             $this->workerMailRequests[] = $workerMailRequest;
  163.             $workerMailRequest->setPartner($this);
  164.         }
  165.         return $this;
  166.     }
  167.     public function removeWorkerMailRequest(WorkerMailRequest $workerMailRequest): self
  168.     {
  169.         if ($this->workerMailRequests->contains($workerMailRequest)) {
  170.             $this->workerMailRequests->removeElement($workerMailRequest);
  171.             // set the owning side to null (unless already changed)
  172.             if ($workerMailRequest->getPartner() === $this) {
  173.                 $workerMailRequest->setPartner(null);
  174.             }
  175.         }
  176.         return $this;
  177.     }
  178.     public function serializeBitrixCompany(): array
  179.     {
  180.         $information $this->getInformation();
  181.         $data = [
  182.             "UF_CRM_SERVICE_ID"   => $this->getId(),
  183.             "TITLE"               => $this->getPublicName(),
  184.             "HAS_EMAIL"           => "Y",// Если есть емл.
  185.             "ASSIGNED_BY_ID"      => BitrixSender::ASSIGNED_SUPER_ADMIN_IN_BITRIX,
  186.             'EMAIL'               => [
  187.                 "n0" => [
  188.                     "VALUE"      => $this->getEmail(),
  189.                     "VALUE_TYPE" => "WORK",
  190.                 ]
  191.             ],
  192.             "UF_CRM_REGION" => $this->getRegion()->getBitrixId(),
  193.             ...($information?->serializeBitrix() ?: [])
  194.         ];
  195.         $workers array_map(fn(Worker $worker) => $worker$this->getWorkers()->toArray());
  196.         $workersBitrixId array_map(
  197.             fn(Worker $worker) => $worker->getBitrixId(),
  198.             array_filter($workers, static fn(Worker $worker) => (bool)$worker->getBitrixId())
  199.         );
  200.         $ordersBitrixId array_reduce(
  201.             $workers
  202.             static function (array $idsWorker $worker) {
  203.                 array_push($ids, ...array_map(
  204.                     fn(Order $order) => $order->getBitrixId(),
  205.                     $worker->getOrders(filterFn: static fn(Order $order) => (bool)$order->getBitrixId())
  206.                 ));
  207.                 return $ids;
  208.             }, 
  209.             []
  210.         );
  211.         $data['UF_CRM_PARTNEREMPLOYEES'] = $workersBitrixId;
  212.         $data['UF_CRM_PARTNEREMPLOYEEDEALS'] = $ordersBitrixId;
  213.         $data['UF_CRM_REGISTRATION_STATUS'] = NotaryCompanyRegistrationStatus::REGISTERED->value;
  214.         if ($information?->getPhone()) {
  215.             $data['HAS_PHONE'] = 'Y';
  216.             $data['PHONE'] = [
  217.                 'n0' => [
  218.                     'VALUE'      => $information->getPhone(),
  219.                     'VALUE_TYPE' => 'WORK',
  220.                 ]
  221.             ];
  222.         }
  223.         return $data;
  224.     }
  225.     public function getBitrixId(): ?int
  226.     {
  227.         return $this->bitrixId;
  228.     }
  229.     public function setBitrixId(?int $bitrixId): self
  230.     {
  231.         $this->bitrixId $bitrixId;
  232.         return $this;
  233.     }
  234.     public function getService(): ?Service
  235.     {
  236.         return $this->service;
  237.     }
  238.     public function setService(?Service $service): self
  239.     {
  240.         $this->service $service;
  241.         return $this;
  242.     }
  243. }