src/Evo/Infrastructure/MappingORM/LegalRepresentative.php line 32

Open in your IDE?
  1. <?php
  2. namespace Evo\Infrastructure\MappingORM;
  3. use ApiPlatform\Core\Annotation\ApiFilter;
  4. use ApiPlatform\Core\Annotation\ApiResource;
  5. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  6. use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
  7. use App\Enum\LegalRepresentativeQualityTypeEnum;
  8. use App\Enum\PersonTypeEnum;
  9. use Doctrine\Common\Collections\ArrayCollection;
  10. use Doctrine\Common\Collections\Collection;
  11. use Doctrine\ORM\Mapping as ORM;
  12. use Symfony\Component\Serializer\Annotation\Groups;
  13. use Symfony\Component\Serializer\Annotation\MaxDepth;
  14. use Symfony\Component\Validator\Constraints as Assert;
  15. /**
  16.  * @ApiResource(
  17.  *     denormalizationContext={"groups"={"legal_representative"}},
  18.  *     normalizationContext={"groups"={"legal_representative"}, "enable_max_depth"=true},
  19.  *     itemOperations={"get", "put", "delete"},
  20.  *     collectionOperations={"get", "post", "findByUser"={
  21.  *          "method"="get",
  22.  *          "route_name"="app_legalrepresentative_byuser"
  23.  *     }}
  24.  *     )
  25.  * @ORM\Entity(repositoryClass="App\Repository\LegalRepresentativeRepository")
  26.  * @ApiFilter(SearchFilter::class, properties={"person": "exact","person.familyName": "partial", "person.givenName": "partial" ,"organizations": "exact"})
  27.  * @ApiFilter(PropertyFilter::class)
  28.  */
  29. class LegalRepresentative
  30. {
  31.     /**
  32.      * @Groups({
  33.      *     "legal_representative",
  34.      *     "read_organization",
  35.      *     "read_person",
  36.      *     "associate",
  37.      *     "pappers_activity"
  38.      * })
  39.      * @ORM\Id()
  40.      * @ORM\GeneratedValue()
  41.      * @ORM\Column(type="integer")
  42.      */
  43.     private ?int $id null;
  44.     /**
  45.      * @Assert\Choice(callback={"App\Enum\LegalRepresentativeQualityTypeEnum", "getChoices"})
  46.      * @Groups({"legal_representative", "write_organization", "read_organization", "read_search", "associate", "pappers_activity"})
  47.      * @ORM\Column(type="string", length=150, nullable=true)
  48.      */
  49.     private ?string $quality null;
  50.     /**
  51.      * @Groups({"legal_representative", "write_organization", "read_organization", "read_search", "associate"})
  52.      * @ORM\Column(type="string", length=255, nullable=true)
  53.      * @Assert\Email
  54.      */
  55.     private ?string $email null;
  56.     /**
  57.      * @Groups({"legal_representative", "write_organization", "read_organization", "read_search", "associate"})
  58.      * @ORM\Column(type="string", length=255, nullable=true)
  59.      */
  60.     private ?string $phoneNumber null;
  61.     /*
  62.      * SAS, SASU : Président obligatoire + Directeurs généraux en option
  63.      * SCI SARL EURL : un gérant à minima (pas de président) + possibilité de cogérant
  64.      */
  65.     /**
  66.      * @Groups({"legal_representative", "write_organization", "read_organization", "associate"})
  67.      * @MaxDepth(1)
  68.      * @ORM\ManyToMany(targetEntity="Evo\Infrastructure\MappingORM\Organization", mappedBy="legalRepresentatives")
  69.      *
  70.      * @deprecated Renommer "organization" car un RL n'est désormais lié qu'à une seule société
  71.      */
  72.     private ?Collection $organizations;
  73.     /**
  74.      * @Groups({"legal_representative", "write_organization", "read_organization", "associate"})
  75.      * @ORM\Column(type="float", nullable=true)
  76.      *
  77.      * @deprecated
  78.      */
  79.     private ?float $shareCapital null;
  80.     /**
  81.      * @Groups({"legal_representative", "write_organization", "read_organization", "associate"})
  82.      * @MaxDepth(1)
  83.      * @ORM\OneToOne(targetEntity="Evo\Infrastructure\MappingORM\Associate", mappedBy="legalRepresentative", cascade={"persist"})
  84.      */
  85.     private ?Associate $associate null;
  86.     /**
  87.      * @Groups({"legal_representative", "write_organization", "read_organization", "read_search", "associate"})
  88.      * @ORM\ManyToOne(targetEntity="Evo\Infrastructure\MappingORM\LegalRepresentative", inversedBy="children", cascade={"persist"})
  89.      */
  90.     private $parent;
  91.     /**
  92.      * @Groups({"legal_representative", "write_organization", "read_organization", "pappers_activity"})
  93.      * @ORM\OneToMany(targetEntity="Evo\Infrastructure\MappingORM\LegalRepresentative", mappedBy="parent")
  94.      */
  95.     private ?Collection $children;
  96.     /**
  97.      * @Groups({"legal_representative", "write_organization", "read_organization", "read_search", "read_export_organization", "associate", "admin_orga_list", "pappers_activity"})
  98.      * @ORM\ManyToOne(targetEntity="Evo\Infrastructure\MappingORM\Person", inversedBy="legalRepresentatives", cascade={"persist"})
  99.      */
  100.     private ?Person $person null;
  101.     /**
  102.      * @Groups({"legal_representative", "write_organization", "read_organization", "associate"})
  103.      * @ORM\Column(type="string", length=150, nullable=true)
  104.      */
  105.     private ?string $contributionType null;
  106.     /**
  107.      * @Groups({"legal_representative", "write_organization", "read_organization", "associate"})
  108.      * @ORM\Column(type="float", nullable=true)
  109.      */
  110.     private ?float $contributionAmount null;
  111.     private $ratio;
  112.     /**
  113.      * @Groups({"legal_representative", "write_organization", "read_organization", "associate"})
  114.      * @ORM\Column(type="boolean", nullable=true)
  115.      */
  116.     private $isAssociate;
  117.     /**
  118.      * @ORM\OneToMany(targetEntity=ActivityLog::class, mappedBy="legalRepresentative")
  119.      */
  120.     private Collection $activityLogs;
  121.     public function __construct()
  122.     {
  123.         $this->organizations = new ArrayCollection();
  124.         $this->children = new ArrayCollection();
  125.         $this->quality LegalRepresentativeQualityTypeEnum::MANAGER;
  126.         $this->activityLogs = new ArrayCollection();
  127.     }
  128.     public function getId(): ?int
  129.     {
  130.         return $this->id;
  131.     }
  132.     /**
  133.      * @return Organization[]|Collection
  134.      *
  135.      * @deprecated Renommer "organization" car un RL n'est désormais lié qu'à une seule société
  136.      */
  137.     public function getOrganizations()
  138.     {
  139.         return $this->organizations;
  140.     }
  141.     public function addOrganization(Organization $organization): self
  142.     {
  143.         if (!$this->organizations->contains($organization)) {
  144.             $this->organizations[] = $organization;
  145.             $organization->addLegalRepresentative($this);
  146.         }
  147.         return $this;
  148.     }
  149.     public function removeOrganization(Organization $organization): self
  150.     {
  151.         if ($this->organizations->contains($organization)) {
  152.             $this->organizations->removeElement($organization);
  153.             $organization->removeLegalRepresentative($this);
  154.         }
  155.         return $this;
  156.     }
  157.     public function getShareCapital(): ?float
  158.     {
  159.         return $this->shareCapital;
  160.     }
  161.     public function setShareCapital(?float $shareCapital): self
  162.     {
  163.         $this->shareCapital $shareCapital;
  164.         return $this;
  165.     }
  166.     public function getAssociate(): ?Associate
  167.     {
  168.         return $this->associate;
  169.     }
  170.     public function setAssociate(?Associate $associate): self
  171.     {
  172.         $this->associate $associate;
  173.         // set (or unset) the owning side of the relation if necessary
  174.         $newLegalRepresentative null === $associate null $this;
  175.         if ($associate && $newLegalRepresentative !== $associate->getLegalRepresentative()) {
  176.             $associate->setLegalRepresentative($newLegalRepresentative);
  177.         }
  178.         return $this;
  179.     }
  180.     public function addChildren(self $children): self
  181.     {
  182.         if (!$this->children->contains($children)) {
  183.             $this->children[] = $children;
  184.             $children->setParent($this);
  185.         }
  186.         return $this;
  187.     }
  188.     public function removeChildren(self $children): self
  189.     {
  190.         if ($this->children->contains($children)) {
  191.             $this->children->removeElement($children);
  192.             if ($children->getParent() === $this) {
  193.                 $children->setParent(null);
  194.             }
  195.         }
  196.         return $this;
  197.     }
  198.     public function getParent(): ?self
  199.     {
  200.         return $this->parent;
  201.     }
  202.     public function setParent(?self $parent): self
  203.     {
  204.         $this->parent $parent;
  205.         return $this;
  206.     }
  207.     public function getPhysicalRepresentative()
  208.     {
  209.         if ($this->getChildren()->toArray()) {
  210.             /** @var LegalRepresentative $representedBy */
  211.             foreach ($this->children as $representedBy) {
  212.                 if (null !== $representedBy->getPerson()) {
  213.                     if (PersonTypeEnum::PHYSICAL == $representedBy->getPerson()->getType()) {
  214.                         return $representedBy;
  215.                     }
  216.                     return $representedBy->getPhysicalRepresentative();
  217.                 }
  218.             }
  219.         }
  220.         return null;
  221.     }
  222.     /**
  223.      * @return Collection|self[]
  224.      */
  225.     public function getChildren(): Collection
  226.     {
  227.         return $this->children;
  228.     }
  229.     public function getDocuments(): Collection
  230.     {
  231.         $documentsPerson = [];
  232.         $documentsParent = [];
  233.         if (null !== $this->getPerson()) {
  234.             $documentsPerson $this->getPerson()->getDocuments()->toArray();
  235.         }
  236.         if (null !== $this->getParent()) {
  237.             $documentsParent $this->getParent()->getDocuments()->toArray();
  238.         }
  239.         return new ArrayCollection(array_merge_recursive($documentsPerson$documentsParent));
  240.     }
  241.     public function __toString()
  242.     {
  243.         $isAssociate $this->getIsAssociate() && LegalRepresentativeQualityTypeEnum::ASSOCIE_SEUL !== $this->getQuality() ? ' - Associé' ' ';
  244.         if (null !== $this->getPerson()) {
  245.             if (PersonTypeEnum::PHYSICAL == $this->getPerson()->getType()) {
  246.                 return $this->getPerson()->getFamilyName().' '.$this->getPerson()->getGivenName().' ( '.$this->getQuality().$isAssociate.' )';
  247.             }
  248.             return $this->getPerson()->getLegalName().' ('.$this->getPerson()->getGivenName().', '.$this->getQuality().$isAssociate.' )';
  249.         }
  250.         return $this->getQuality();
  251.     }
  252.     public function getQuality(): ?string
  253.     {
  254.         return $this->quality;
  255.     }
  256.     public function setQuality(?string $quality): self
  257.     {
  258.         $this->quality $quality;
  259.         return $this;
  260.     }
  261.     public function getEmail(): ?string
  262.     {
  263.         return $this->email;
  264.     }
  265.     public function getEmailFallbackUser(): ?string
  266.     {
  267.         if (null === $this->email || '' === $this->email) {
  268.             $organizations $this->getOrganizations();
  269.             /** @var Organization $organization */
  270.             foreach ($organizations as $organization) {
  271.                 $users $organization->getUsers();
  272.                 foreach ($users as $user) {
  273.                     if ($user->getPhoneNumber() === $this->getPhoneNumber()) {
  274.                         return $user->getEmail();
  275.                     }
  276.                 }
  277.             }
  278.         }
  279.         return $this->email;
  280.     }
  281.     public function setEmail(?string $email): self
  282.     {
  283.         $this->email $email;
  284.         return $this;
  285.     }
  286.     public function getPhoneNumber(): ?string
  287.     {
  288.         return $this->phoneNumber;
  289.     }
  290.     public function setPhoneNumber(?string $phoneNumber): self
  291.     {
  292.         $this->phoneNumber $phoneNumber;
  293.         return $this;
  294.     }
  295.     public function getPerson(): ?Person
  296.     {
  297.         return $this->person;
  298.     }
  299.     public function setPerson(?Person $person): self
  300.     {
  301.         $this->person $person;
  302.         return $this;
  303.     }
  304.     public function getContributionType(): ?string
  305.     {
  306.         return $this->contributionType;
  307.     }
  308.     public function setContributionType(?string $contributionType): self
  309.     {
  310.         $this->contributionType $contributionType;
  311.         return $this;
  312.     }
  313.     public function getContributionAmount(): ?float
  314.     {
  315.         return $this->contributionAmount;
  316.     }
  317.     public function setContributionAmount(?float $contributionAmount): self
  318.     {
  319.         $this->contributionAmount $contributionAmount;
  320.         return $this;
  321.     }
  322.     public function getRatio(): ?string
  323.     {
  324.         return $this->ratio;
  325.     }
  326.     public function setRatio($ratio): self
  327.     {
  328.         $this->ratio $ratio;
  329.         return $this;
  330.     }
  331.     public function getIsAssociate(): ?bool
  332.     {
  333.         return $this->isAssociate;
  334.     }
  335.     public function setIsAssociate(?bool $isAssociate): self
  336.     {
  337.         $this->isAssociate $isAssociate;
  338.         return $this;
  339.     }
  340.     public function getActivityLogs(): Collection
  341.     {
  342.         return $this->activityLogs;
  343.     }
  344.     public function addActivityLog(ActivityLog $activityLog): self
  345.     {
  346.         if (!$this->activityLogs->contains($activityLog)) {
  347.             $this->activityLogs[] = $activityLog;
  348.             $activityLog->setLegalRepresentative($this);
  349.         }
  350.         return $this;
  351.     }
  352.     public function removeActivityLog(ActivityLog $activityLog): self
  353.     {
  354.         // set the owning side to null (unless already changed)
  355.         if ($this->activityLogs->removeElement($activityLog) && $activityLog->getLegalRepresentative() === $this) {
  356.             $activityLog->setLegalRepresentative(null);
  357.         }
  358.         return $this;
  359.     }
  360.     public static function createFixture(array $data): self
  361.     {
  362.         $self = new self();
  363.         $self->quality $data['quality'];
  364.         $self->email $data['email'];
  365.         $self->phoneNumber $data['phoneNumber'];
  366.         $self->isAssociate $data['isAssociate'];
  367.         $self->person Person::createFixture($data['person']);
  368.         return $self;
  369.     }
  370.     // get top parent
  371.     public function getTopParent(): ?self
  372.     {
  373.         if (null !== $this->getParent()) {
  374.             return $this->getParent()->getTopParent();
  375.         }
  376.         return $this;
  377.     }
  378.     public function isAssociateOnly(): bool
  379.     {
  380.         return LegalRepresentativeQualityTypeEnum::ASSOCIE_SEUL === $this->getQuality();
  381.     }
  382. }