src/Entity/Security/User.php line 36

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Security;
  3. use App\Common\Entity\DefaultEntity;
  4. use App\Entity\JIT\Comment;
  5. use App\Entity\JIT\Feeling;
  6. use App\Entity\JIT\PContact;
  7. use App\Entity\JIT\Person;
  8. use App\Entity\JIT\Property;
  9. use App\Entity\JIT\Tenancy;
  10. use App\Repository\Security\UserRepository;
  11. use DateTime;
  12. use DateTimeInterface;
  13. use Doctrine\Common\Collections\ArrayCollection;
  14. use Doctrine\Common\Collections\Collection;
  15. use Doctrine\ORM\Mapping as ORM;
  16. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  17. use Symfony\Component\HttpFoundation\File\File;
  18. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  19. use Symfony\Component\Security\Core\User\UserInterface;
  20. use Symfony\Component\String\Slugger\AsciiSlugger;
  21. use Symfony\Component\Validator\Constraints as Assert;
  22. /**
  23.  * @ORM\Entity(repositoryClass=UserRepository::class)
  24.  * @ORM\Table(name="`pr_security_user`")
  25.  * @UniqueEntity(
  26.  *     fields={"username"},
  27.  *     message="_TT_ Ce nom d'utilisateur est déja pris"
  28.  * )
  29.  * @UniqueEntity("email")
  30.  * @UniqueEntity("phone")
  31.  * @ORM\HasLifecycleCallbacks
  32.  */
  33. class User extends DefaultEntity implements UserInterfacePasswordAuthenticatedUserInterface {
  34.     /**
  35.      * @ORM\Id
  36.      * @ORM\GeneratedValue
  37.      * @ORM\Column(type="integer")
  38.      */
  39.     private $id;
  40.     /**
  41.      * @ORM\Column(type="string", length=255)
  42.      */
  43.     private $username;
  44.     /**
  45.      * @ORM\Column(type="string", length=255)
  46.      */
  47.     private $slug;
  48.     /**
  49.      * @ORM\Column(type="string", length=255)
  50.      */
  51.     private $fullname;
  52.     /**
  53.      * @ORM\Column(type="string", length=255, nullable=true)
  54.      */
  55.     private $password;
  56.     /**
  57.      * @ORM\Column(type="string", length=255, nullable=true)
  58.      */
  59.     private $code;
  60.     /**
  61.      * @ORM\Column(type="datetime", nullable=true)
  62.      */
  63.     private $createAt;
  64.     /**
  65.      * @ORM\Column(type="datetime", nullable=true)
  66.      */
  67.     private $lmd;
  68.     /**
  69.      * @ORM\Column(type="datetime", nullable=true)
  70.      */
  71.     private $fad;
  72.     /**
  73.      * @ORM\Column(type="datetime", nullable=true)
  74.      */
  75.     private $lad;
  76.     /**
  77.      * @ORM\Column(type="datetime", nullable=true)
  78.      */
  79.     private $lmp;
  80.     /**
  81.      * @ORM\Column(type="datetime", nullable=true)
  82.      */
  83.     private $lrp;
  84.     /**
  85.      * @ORM\Column(type="text", nullable=true)
  86.      */
  87.     private $description;
  88.     /**
  89.      * @ORM\Column(type="string", length=10, nullable=true)
  90.      */
  91.     private $language;
  92.     /**
  93.      * @ORM\Column(type="string", length=255, nullable=true)
  94.      */
  95.     private $picture;
  96.     /**
  97.      * @ORM\Column(type="boolean", nullable=true)
  98.      */
  99.     private $enabled;
  100.     /**
  101.      * @ORM\Column(type="json", nullable=true)
  102.      */
  103.     private $userroles = [];
  104.     /**
  105.      * @ORM\ManyToOne(targetEntity=Profile::class, inversedBy="users")
  106.      */
  107.     private $profile;
  108.     /**
  109.      * @ORM\Column(type="string", length=255)
  110.      * @Assert\Email(
  111.      *     message = "The email '{{ value }}' is not a valid email."
  112.      * )
  113.      */
  114.     private $email;
  115.     /**
  116.      * @var File
  117.      */
  118.     private $image;
  119.     /**
  120.      * @var string
  121.      */
  122.     private $defaultPassword;
  123.     /**
  124.      * @ORM\Column(type="boolean", nullable=true)
  125.      */
  126.     private $privatePassword;
  127.     /**
  128.      * @ORM\Column(type="string", length=255, nullable=true)
  129.      */
  130.     private $phone;
  131.     private $updateLad;
  132.     /**
  133.      * @ORM\OneToMany(targetEntity=Person::class, mappedBy="account", cascade={"persist","remove"})
  134.      */
  135.     private $persons;
  136.     /**
  137.      * @ORM\OneToMany(targetEntity=PContact::class, mappedBy="account", cascade={"persist","remove"})
  138.      */
  139.     private $pContacts;
  140.     /**
  141.      * @ORM\OneToMany(targetEntity=Comment::class, mappedBy="user")
  142.      */
  143.     private $comments;
  144.     /**
  145.      * @ORM\OneToMany(targetEntity=Feeling::class, mappedBy="user")
  146.      */
  147.     private $feelings;
  148.     public function __construct() {
  149.         $this->enabled true;
  150.         $this->persons = new ArrayCollection();
  151.         $this->pContacts = new ArrayCollection();
  152.         $this->comments = new ArrayCollection();
  153.         $this->feelings = new ArrayCollection();
  154.     }
  155.     /**
  156.      * @ORM\PrePersist()
  157.      */
  158.     public function prePersist() {
  159.         parent::prePersist();
  160.         $this->createAt = new DateTime();
  161. //        $this->enabled = false;
  162. //        $this->setUsernameValue(); :: Not her because it is in controller to resolve the problem of unicity
  163.     }
  164.     /**
  165.      * @ORM\PreUpdate()
  166.      */
  167.     public function preUpdate() {
  168.         parent::preUpdate();
  169.         if (!$this->updateLad) {
  170.             $this->lmd = new DateTime();
  171.         }
  172.         $this->setUsernameValue();
  173.         if ($this->password === null) {
  174.             $this->setDefaultPassword();
  175.         }
  176.     }
  177.     public function getId(): ?int {
  178.         return $this->id;
  179.     }
  180.     public function getUsername(): ?string {
  181.         return $this->username;
  182.     }
  183.     public function setUsername(string $username): self {
  184.         $this->username $username;
  185.         return $this;
  186.     }
  187.     private function setUsernameValue(): void {
  188.         if ($this->username === null) {
  189.             $this->username $this->generateUsername();
  190.         }
  191.     }
  192.     public function generateUsername(): ?string {
  193.         if ($this->fullname !== null) {
  194.             $worrds explode(' 'trim($this->fullname));
  195.             $username "";
  196.             $nbw count($worrds) - 1;
  197.             if ($nbw >= 1) {
  198.                 $username .= $worrds[$nbw];
  199.             }
  200.             $username .= '.' $worrds[0];
  201.             return $this->sluggerUsername($username);
  202.         } else {
  203.             return null;
  204.         }
  205.     }
  206.     private function sluggerUsername($username): string {
  207.         $slugger = new AsciiSlugger();
  208.         $table = array(
  209.             'Š' => 'S''š' => 's''Đ' => 'Dj''đ' => 'dj''Ž' => 'Z''ž' => 'z''Č' => 'C''č' => 'c''Ć' => 'C''ć' => 'c',
  210.             'À' => 'A''Á' => 'A''Â' => 'A''Ã' => 'A''Ä' => 'A''Å' => 'A''Æ' => 'A''Ç' => 'C''È' => 'E''É' => 'E',
  211.             'Ê' => 'E''Ë' => 'E''Ì' => 'I''Í' => 'I''Î' => 'I''Ï' => 'I''Ñ' => 'N''Ò' => 'O''Ó' => 'O''Ô' => 'O',
  212.             'Õ' => 'O''Ö' => 'O''Ø' => 'O''Ù' => 'U''Ú' => 'U''Û' => 'U''Ü' => 'U''Ý' => 'Y''Þ' => 'B''ß' => 'Ss',
  213.             'à' => 'a''á' => 'a''â' => 'a''ã' => 'a''ä' => 'a''å' => 'a''æ' => 'a''ç' => 'c''è' => 'e''é' => 'e',
  214.             'ê' => 'e''ë' => 'e''ì' => 'i''í' => 'i''î' => 'i''ï' => 'i''ð' => 'o''ñ' => 'n''ò' => 'o''ó' => 'o',
  215.             'ô' => 'o''õ' => 'o''ö' => 'o''ø' => 'o''ù' => 'u''ú' => 'u''û' => 'u''ý' => 'y''ý' => 'y''þ' => 'b',
  216.             'ÿ' => 'y''Ŕ' => 'R''ŕ' => 'r',
  217.         );
  218.         $fusername strtr($username$table);
  219.         return $slugger->slug(strtolower($fusername), '.');
  220.     }
  221.     public function getSlug(): ?string {
  222.         return $this->slug;
  223.     }
  224.     protected function setSlug(string $slug): void {
  225.         $this->slug $slug;
  226.     }
  227.     public function getPassword(): ?string {
  228.         return $this->password;
  229.     }
  230.     public function setPassword(?string $password): self {
  231.         $this->password $password;
  232.         return $this;
  233.     }
  234.     public function setDefaultPassword(): void {
  235.         $this->defaultPassword $this->password_generate();
  236. //        $passwordEncoder = new \Symfony\Component\Security\Core\Encoder\UserPasswordEncoder($encoderFactory);
  237. //        $this->password = $passwordEncoder->encodePassword($this, $this->defaultPassword);
  238.         $this->password $this->defaultPassword;
  239.         $this->privatePassword false;
  240.     }
  241.     public function getDefaultPassword(): ?string {
  242.         return $this->defaultPassword;
  243.     }
  244.     private function password_generate($length 7): string {
  245.         $data '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcefghijklmnopqrstuvwxyz';
  246.         return substr(str_shuffle($data), 0$length);
  247.     }
  248.     public function getCode(): ?string {
  249.         return $this->code;
  250.     }
  251.     public function setCode(?string $code): self {
  252.         $this->code $code;
  253.         return $this;
  254.     }
  255.     public function getCreateAt(): ?DateTimeInterface {
  256.         return $this->createAt;
  257.     }
  258.     public function getLmd(): ?DateTimeInterface {
  259.         return $this->lmd;
  260.     }
  261.     protected function setLmd(?DateTimeInterface $lmd): self {
  262.         $this->lmd $lmd;
  263.         return $this;
  264.     }
  265.     public function getFad(): ?DateTimeInterface {
  266.         return $this->fad;
  267.     }
  268.     public function setFad(?DateTimeInterface $fad): self {
  269.         $this->fad $fad;
  270.         return $this;
  271.     }
  272.     public function getLad(): ?DateTimeInterface {
  273.         return $this->lad;
  274.     }
  275.     public function setLad(?DateTimeInterface $lad): self {
  276.         $this->lad $lad;
  277.         return $this;
  278.     }
  279.     public function getUpdateLad() {
  280.         return $this->updateLad;
  281.     }
  282.     public function setUpdateLad($updateLad): void {
  283.         $this->updateLad $updateLad;
  284.     }
  285.     public function getLmp(): ?DateTimeInterface {
  286.         return $this->lmp;
  287.     }
  288.     public function setLmp(?DateTimeInterface $lmp): self {
  289.         $this->lmp $lmp;
  290.         return $this;
  291.     }
  292.     public function getLrp(): ?DateTimeInterface {
  293.         return $this->lrp;
  294.     }
  295.     public function setLrp(?DateTimeInterface $lrp): self {
  296.         $this->lrp $lrp;
  297.         return $this;
  298.     }
  299.     public function getDescription(): ?string {
  300.         return $this->description;
  301.     }
  302.     public function setDescription(?string $description): self {
  303.         $this->description $description;
  304.         return $this;
  305.     }
  306.     public function getLanguage(): ?string {
  307.         return $this->language;
  308.     }
  309.     public function setLanguage(?string $language): self {
  310.         $this->language $language;
  311.         return $this;
  312.     }
  313.     public function getPicture(): ?string {
  314.         if ($this->picture !== null) {
  315.             return $this->picture;
  316. //        } else if ($this->gender == 'F') {
  317. //            return 'img_avatar_f.png';
  318.         } else {
  319.             return 'img_avatar_h.png';
  320.         }
  321.     }
  322.     public function getPicture2(): ?string {
  323.         if ($this->picture !== null && $this->picture !== 'img_avatar_h.png' && $this->picture !== 'img_avatar_f.png') {
  324.             return $this->picture;
  325. //        } else if ($this->gender == 'F') {
  326. ////            return 'img_avatar_f.png';
  327. //            $i = $this->id % 36 +1;
  328. //            return 'avatar/avatarf'.$i.'.png';
  329.         } else {
  330.             $i $this->id 36 1;
  331.             return 'avatar/avatarh' $i '.png';
  332.         }
  333.     }
  334.     public function getPicture3(): ?string {
  335.         if ($this->getPerson() !== null) {
  336.             return $this->getPerson()->getImage();
  337.         } else {
  338.             return 'img_avatar_h.png';
  339.         }
  340.     }
  341.     public function setPicture(?string $picture): self {
  342.         $this->picture $picture;
  343.         return $this;
  344.     }
  345.     public function isEnabled(): ?bool {
  346.         return $this->enabled;
  347.     }
  348.     public function setEnabled(?bool $enabled): self {
  349.         $this->enabled $enabled;
  350.         return $this;
  351.     }
  352.     public function getUserroles(): ?array {
  353.         return $this->userroles;
  354.     }
  355.     public function setUserroles(?array $userroles): self {
  356.         $this->userroles $userroles;
  357.         return $this;
  358.     }
  359.     public function addUserrole(string $userrole): self {
  360.         if ($this->userroles === null) {
  361.             $this->userroles = [$userrole];
  362.         } else {
  363.             if (!in_array($userrole$this->userroles)) {
  364.                 $this->userroles[] = $userrole;
  365.             }
  366.         }
  367.         return $this;
  368.     }
  369.     public function removeUserrole(string $userrole): self {
  370.         if ($this->userroles !== null && in_array($userrole$this->userroles)) {
  371.             $this->userroles array_diff($this->userroles, [$userrole]);
  372.         }
  373.         return $this;
  374.     }
  375.     public function getProfile(): ?Profile {
  376.         return $this->profile;
  377.     }
  378.     public function setProfile(?Profile $profile): self {
  379.         $this->profile $profile;
  380.         return $this;
  381.     }
  382.     public function getFullname(): ?string {
  383.         return $this->fullname;
  384.     }
  385.     public function setFullname(string $fullname): self {
  386.         $this->fullname $fullname;
  387.         return $this;
  388.     }
  389.     public function getName(): ?string {
  390.         return $this->getFullname();
  391.     }
  392.     public function getEmail(): ?string {
  393.         return $this->email;
  394.     }
  395.     public function setEmail(string $email): self {
  396.         $this->email $email;
  397.         return $this;
  398.     }
  399.     public function getClass(): ?string {
  400.         return self::class;
  401.     }
  402.     /**
  403.      * @see UserInterface
  404.      */
  405.     public function getRoles(): array {
  406.         $roles $this->userroles;
  407.         if ($this->profile !== null) {
  408.             $roles array_merge($roles$this->profile->getRoles());
  409.         }
  410.         // guarantee every user at least has ROLE_USER
  411.         $roles[] = 'ROLE_USER';
  412.         return array_unique($roles);
  413.     }
  414.     /**
  415.      * @see UserInterface
  416.      */
  417.     public function getSalt() {
  418.         // not needed when using the "bcrypt" algorithm in security.yaml
  419.     }
  420.     /**
  421.      * @see UserInterface
  422.      */
  423.     public function eraseCredentials() {
  424.         // If you store any temporary, sensitive data on the user, clear it here
  425.         // $this->plainPassword = null;
  426.     }
  427.     public function getImage(): ?File {
  428.         return $this->image;
  429.     }
  430.     public function setImage(File $image): self {
  431.         $this->image $image;
  432.         return $this;
  433.     }
  434.     public function lock(): self {
  435.         $this->enabled false;
  436.         return $this;
  437.     }
  438.     public function unlock(): self {
  439.         $this->enabled true;
  440.         return $this;
  441.     }
  442.     public function getPhone(): ?string {
  443.         return $this->phone;
  444.     }
  445.     public function setPhone(?string $phone): self {
  446.         $this->phone $phone;
  447.         return $this;
  448.     }
  449.     public function getProfileName(): ?string {
  450.         if ($this->profile !== null) {
  451.             return $this->profile->getName();
  452.         }
  453.         return "";
  454.     }
  455.     public function getPrivatePassword() {
  456.         return $this->privatePassword;
  457.     }
  458.     public function setPrivatePassword($privatePassword): void {
  459.         $this->privatePassword $privatePassword;
  460.     }
  461.     public function getUserIdentifier(): ?string {
  462.         return $this->username;
  463.     }
  464.     /**
  465.      * @return Collection<int, Person>
  466.      */
  467.     public function getPersons(): Collection {
  468.         return $this->persons;
  469.     }
  470.     public function addPerson(Person $person): self {
  471.         if (!$this->persons->contains($person)) {
  472.             $this->persons[] = $person;
  473.             $person->setAccount($this);
  474.         }
  475.         return $this;
  476.     }
  477.     public function removePerson(Person $person): self {
  478.         if ($this->persons->removeElement($person)) {
  479.             // set the owning side to null (unless already changed)
  480.             if ($person->getAccount() === $this) {
  481.                 $person->setAccount(null);
  482.             }
  483.         }
  484.         return $this;
  485.     }
  486.     public function getPerson(): ?Person {
  487.         if ($this->persons->isEmpty()) {
  488.             return null;
  489.         } else {
  490.             return $this->persons[0];
  491.         }
  492.     }
  493.     public function getPContact(): ?PContact {
  494.         if (!$this->pContacts->isEmpty()) {
  495.             return $this->pContacts[0];
  496.         } else {
  497.             return null;
  498.         }
  499.     }
  500.     /**
  501.      * @return Collection<int, PContact>
  502.      */
  503.     public function getPContacts(): Collection {
  504.         return $this->pContacts;
  505.     }
  506.     public function addPContact(PContact $pContact): self {
  507.         if (!$this->pContacts->contains($pContact)) {
  508.             $this->pContacts[] = $pContact;
  509.             $pContact->setAccount($this);
  510.         }
  511.         return $this;
  512.     }
  513.     public function removePContact(PContact $pContact): self {
  514.         if ($this->pContacts->removeElement($pContact)) {
  515.             // set the owning side to null (unless already changed)
  516.             if ($pContact->getAccount() === $this) {
  517.                 $pContact->setAccount(null);
  518.             }
  519.         }
  520.         return $this;
  521.     }
  522.     /**
  523.      * @return Collection<int, Comment>
  524.      */
  525.     public function getComments(): Collection {
  526.         return $this->comments;
  527.     }
  528.     public function addComment(Comment $comment): self {
  529.         if (!$this->comments->contains($comment)) {
  530.             $this->comments[] = $comment;
  531.             $comment->setUser($this);
  532.         }
  533.         return $this;
  534.     }
  535.     public function removeComment(Comment $comment): self {
  536.         if ($this->comments->removeElement($comment)) {
  537.             // set the owning side to null (unless already changed)
  538.             if ($comment->getUser() === $this) {
  539.                 $comment->setUser(null);
  540.             }
  541.         }
  542.         return $this;
  543.     }
  544.     /**
  545.      * @return Collection<int, Feeling>
  546.      */
  547.     public function getFeelings(): Collection {
  548.         return $this->feelings;
  549.     }
  550.     public function addFeeling(Feeling $feeling): self {
  551.         if (!$this->feelings->contains($feeling)) {
  552.             $this->feelings[] = $feeling;
  553.             $feeling->setUser($this);
  554.         }
  555.         return $this;
  556.     }
  557.     public function removeFeeling(Feeling $feeling): self {
  558.         if ($this->feelings->removeElement($feeling)) {
  559.             // set the owning side to null (unless already changed)
  560.             if ($feeling->getUser() === $this) {
  561.                 $feeling->setUser(null);
  562.             }
  563.         }
  564.         return $this;
  565.     }
  566.     /**
  567.      * @return Collection<int, Person>
  568.      */
  569.     public function getMyEmployees(): Collection {
  570.         $myEmployees = new ArrayCollection();
  571.         foreach ($this->persons as $person) {
  572.             if ($person->isEmployee()) {
  573.                 if (!$myEmployees->contains($person)) {
  574.                     $myEmployees[] = $person;
  575.                 }
  576.             }
  577.             foreach ($person->getMyAgencies() as $agency) {
  578.                 foreach ($agency->getPersons() as $oPerson) {
  579.                     if ($oPerson->isEmployee()) {
  580.                         if (!$myEmployees->contains($oPerson)) {
  581.                             $myEmployees[] = $oPerson;
  582.                         }
  583.                     }
  584.                 }
  585.             }
  586.         }
  587.         return $myEmployees;
  588.     }
  589.     /**
  590.      * @return Collection<int, Person>
  591.      */
  592.     public function getMyOwners(): Collection {
  593.         $myOwners = new ArrayCollection();
  594.         foreach ($this->persons as $person) {
  595.             if ($person->isOwner()) {
  596.                 if (!$myOwners->contains($person)) {
  597.                     $myOwners[] = $person;
  598.                 }
  599.             } else if ($person->isEmployee()) {
  600.                 foreach ($person->getMyOwners() as $oPerson) {
  601.                     if ($oPerson->isOwner()) {
  602.                         if (!$myOwners->contains($oPerson)) {
  603.                             $myOwners[] = $oPerson;
  604.                         }
  605.                     }
  606.                 }
  607.             }
  608.         }
  609.         return $myOwners;
  610.     }
  611.     /**
  612.      * @return Collection<int, Person>
  613.      */
  614.     public function getMyAOwners(): Collection {
  615.         $myOwners = new ArrayCollection();
  616. //        foreach ($this->getMyEmployees() as $person) {
  617. //            if ($person->isOwner()) {
  618. //                if (!$myOwners->contains($person)) {
  619. //                    $myOwners[] = $person;
  620. //                }
  621. //            }
  622. //
  623. //            if ($person->isEmployee()) {
  624. //                foreach ($person->getMyOwners() as $oPerson) {
  625. //                    if ($oPerson->isOwner()) {
  626. //                        if (!$myOwners->contains($oPerson)) {
  627. //                            $myOwners[] = $oPerson;
  628. //                        }
  629. //                    }
  630. //                }
  631. //            }
  632. //        }
  633.         foreach ($this->persons as $person) {
  634.             if ($person->isOwner()) {
  635.                 if (!$myOwners->contains($person)) {
  636.                     $myOwners[] = $person;
  637.                 }
  638.             } else if ($person->isEmployee()) {
  639.                 foreach ($person->getMyOwners() as $oPerson) {
  640.                     if ($oPerson->isOwner()) {
  641.                         if (!$myOwners->contains($oPerson)) {
  642.                             $myOwners[] = $oPerson;
  643.                         }
  644.                     }
  645.                 }
  646.             }
  647.         }
  648.         foreach ($this->getMyEmployees() as $person) {
  649.             foreach ($person->getMyOwners() as $oPerson) {
  650.                 if ($oPerson->isOwner()) {
  651.                     if (!$myOwners->contains($oPerson)) {
  652.                         $myOwners[] = $oPerson;
  653.                     }
  654.                 }
  655.             }
  656.         }
  657.         return $myOwners;
  658.     }
  659.     /**
  660.      * Mes locataires
  661.      * @return Collection<int, Person>
  662.      */
  663.     public function getMyTenants(): Collection {
  664.         $myTenants = new ArrayCollection();
  665.         foreach ($this->persons as $person) {
  666.             if ($person->isTenant()) {
  667.                 if (!$myTenants->contains($person)) {
  668.                     $myTenants[] = $person;
  669.                 }
  670.             } else if ($person->isEmployee() || $person->isOwner()) {
  671.                 foreach ($person->getMyTenants() as $oPerson) {
  672.                     if ($oPerson->isTenant()) {
  673.                         if (!$myTenants->contains($oPerson)) {
  674.                             $myTenants[] = $oPerson;
  675.                         }
  676.                     }
  677.                 }
  678.             }
  679.         }
  680.         return $myTenants;
  681.     }
  682.     /**
  683.      * Tous les locataires de mon agence
  684.      * @return Collection<int, Person>
  685.      */
  686.     public function getMyATenants(): Collection {
  687.         $myTenants $this->getMyTenants();
  688.         foreach ($this->getMyEmployees() as $person) {
  689.             foreach ($person->getMyTenants() as $oPerson) {
  690.                 if (!$myTenants->contains($oPerson)) {
  691.                     $myTenants[] = $oPerson;
  692.                 }
  693.             }
  694.         }
  695.         return $myTenants;
  696.     }
  697.     /**
  698.      * Affiche toutes les personnes qu'on peut voir
  699.      * @return Collection<int, Person>
  700.      */
  701.     public function getAllPersons(): Collection {
  702.         $persons $this->getMyEmployees();
  703.         foreach ($this->getMyAOwners() as $person) {
  704.             if (!$persons->contains($person)) {
  705.                 $persons->add($person);
  706.             }
  707.         }
  708.         foreach ($this->getMyATenants() as $person) {
  709.             if (!$persons->contains($person)) {
  710.                 $persons->add($person);
  711.             }
  712.         }
  713.         return $persons;
  714.     }
  715.     /**
  716.      * Affiche toutes les donneurs d'ordre (propriétaires et locataires) qu'on peut voir
  717.      * @return Collection<int, Person>
  718.      */
  719.     public function getAllDnd(): Collection {
  720.         $persons $this->getMyAOwners();
  721.         foreach ($this->getMyATenants() as $person) {
  722.             if (!$persons->contains($person)) {
  723.                 $persons->add($person);
  724.             }
  725.         }
  726.         return $persons;
  727.     }
  728.     /**
  729.      * @return Collection<int, Agency>
  730.      */
  731.     public function getMyAgencies(): Collection {
  732.         $myAgencies = new ArrayCollection();
  733.         foreach ($this->getPersons() as $person) {
  734.             if ($person->isEmployee()) {
  735.                 foreach ($person->getMyAgencies() as $agency) {
  736.                     if (!$myAgencies->contains($agency)) {
  737.                         $myAgencies[] = $agency;
  738.                     }
  739.                 }
  740.             }
  741.         }
  742.         return $myAgencies;
  743.     }
  744.     /**
  745.      * Retourne toutes les agences auquelles appartient l'utilisateur connecté
  746.      * @return Collection<int, Agency>
  747.      */
  748.     public function getAAgencies(): Collection {
  749.         $AAgencies = new ArrayCollection();
  750.         foreach ($this->getPersons() as $person) {
  751.             $agency $person->getAgency();
  752.             if ($agency != null && !$AAgencies->contains($agency)) {
  753.                 $AAgencies[] = $agency;
  754.             }
  755.             if ($person->isEmployee()) {
  756.                 foreach ($person->getMyAgencies() as $agency) {
  757.                     if (!$AAgencies->contains($agency)) {
  758.                         $AAgencies[] = $agency;
  759.                     }
  760.                 }
  761.             }
  762.         }
  763.         return $AAgencies;
  764.     }
  765.     public function hasAgency(): bool {
  766.         foreach ($this->persons as $person) {
  767.             if ($person->hasAgency()) {
  768.                 return true;
  769.             }
  770.         }
  771.         return false;
  772.     }
  773.     public function hasAffair(): bool {
  774.         foreach ($this->persons as $person) {
  775.             if ($person->hasAffair()) {
  776.                 return true;
  777.             }
  778.         }
  779.         return false;
  780.     }
  781.     public function isEmployee(): bool {
  782.         foreach ($this->persons as $person) {
  783.             if ($person->isEmployee()) {
  784.                 return true;
  785.             }
  786.         }
  787.         return false;
  788.     }
  789.     public function isOwner(): bool {
  790.         foreach ($this->persons as $person) {
  791.             if ($person->isOwner()) {
  792.                 return true;
  793.             }
  794.         }
  795.         return false;
  796.     }
  797.     public function isTenant(): bool {
  798.         foreach ($this->persons as $person) {
  799.             if ($person->isTenant()) {
  800.                 return true;
  801.             }
  802.         }
  803.         return false;
  804.     }
  805.     /**
  806.      * @return Collection<int, Account>
  807.      */
  808.     public function getAccounts(): Collection {
  809.         $accounts = new ArrayCollection();
  810.         foreach ($this->getPersons() as $person) {
  811.             foreach ($person->getAccounts() as $account) {
  812.                 if (!$accounts->contains($account)) {
  813.                     $accounts[] = $account;
  814.                 }
  815.             }
  816.         }
  817.         return $accounts;
  818.     }
  819.     /**
  820.      * @return Collection<int, Tenancy>
  821.      * Les locations (gérées par/appartenants aux) agences de l'utilisateur connnecté
  822.      */
  823.     public function getATenancies(): Collection {
  824.         $tenancies = new ArrayCollection();
  825.         if ($this->isEmployee()) {
  826.             foreach ($this->getAMTenancies() as $person) {
  827.                 foreach ($person->getATenancies() as $tenancy) {
  828.                     if (!$tenancies->contains($tenancy)) {
  829.                         $tenancies->add($tenancy);
  830.                     }
  831.                 }
  832.             }
  833.         } else if ($this->isOwner()) {
  834.             foreach ($this->persons as $person) {
  835.                 foreach ($person->getAOTenancies() as $tenancy) {
  836.                     if (!$tenancies->contains($tenancy)) {
  837.                         $tenancies->add($tenancy);
  838.                     }
  839.                 }
  840.             }
  841.         } else if ($this->isTenant()) {
  842.             foreach ($this->persons as $person) {
  843.                 foreach ($person->getATenancies() as $tenancy) {
  844.                     if (!$tenancies->contains($tenancy)) {
  845.                         $tenancies->add($tenancy);
  846.                     }
  847.                 }
  848.             }
  849.         }
  850.         return $tenancies;
  851.     }
  852.     /**
  853.      * @return Collection<int, Property>
  854.      * Les proprités gérées/appartenant par les agences de l'utilisateur connnecté
  855.      */
  856.     public function getAProperties(): Collection {
  857.         $properties = new ArrayCollection();
  858.         if ($this->isProfileAgent()) {
  859. //            foreach ($this->getMyEmployees() as $person) {
  860. //                foreach ($person->getAProperties() as $property) {
  861. //                    if (!$properties->contains($property)) {
  862. //                        $properties->add($property);
  863. //                    }
  864. //                }
  865. //            }
  866.             foreach ($this->getAAgencies() as $agency) {
  867.                 foreach ($agency->getPersons() as $person) {
  868.                     foreach ($person->getARProperties2() as $property) {
  869.                         if (!$properties->contains($property)) {
  870.                             $properties->add($property);
  871.                         }
  872.                     }
  873.                 }
  874.             }
  875.         } else if ($this->isProfileOwner()) {
  876.             foreach ($this->persons as $person) {
  877.                 foreach ($person->getARProperties2() as $property) {
  878.                     if (!$properties->contains($property)) {
  879.                         $properties->add($property);
  880.                     }
  881.                 }
  882.             }
  883.         }
  884.         return $properties;
  885.     }
  886.     /**
  887.      * @return Collection<int, Property>
  888.      * Les proprités vacants gérées par les agences de l'utilisateur connnecté
  889.      */
  890.     public function getVProperties(): Collection {
  891.         $properties = new ArrayCollection();
  892.         if ($this->isProfileAgent()) {
  893.             foreach ($this->getAAgencies() as $agency) {
  894.                 foreach ($agency->getPersons() as $person) {
  895.                     foreach ($person->getARVProperties2() as $property) {
  896.                         if (!$properties->contains($property)) {
  897.                             $properties->add($property);
  898.                         }
  899.                     }
  900.                 }
  901.             }
  902.         } else if ($this->isOwner()) {
  903.             foreach ($this->persons as $person) {
  904.                 foreach ($person->getARVProperties2() as $property) {
  905.                     if (!$properties->contains($property)) {
  906.                         $properties->add($property);
  907.                     }
  908.                 }
  909.             }
  910.         }
  911.         return $properties;
  912.     }
  913.     /**
  914.      * @return Collection<int, Property>
  915.      * Les proprités louables gérées/appartenant par les agences de l'utilisateur connnecté
  916.      */
  917.     public function getARProperties(): Collection {
  918.         $properties = new ArrayCollection();
  919.         if ($this->isEmployee()) {
  920.             foreach ($this->getMyEmployees() as $person) {
  921. //                foreach ($person->getAProperties() as $property) {
  922. //                    if ($property->isRented() && !$properties->contains($property)) {
  923. //                        $properties->add($property);
  924. //                    }
  925. //                }
  926.                 foreach ($person->getARProperties() as $property) {
  927.                     if (!$properties->contains($property)) {
  928.                         $properties->add($property);
  929.                     }
  930.                 }
  931.             }
  932.         } else if ($this->isOwner()) {
  933.             foreach ($this->persons as $person) {
  934. //                foreach ($person->getProperties() as $property) {
  935. //                    if ($property->isRented() && !$properties->contains($property)) {
  936. //                        $properties->add($property);
  937. //                    }
  938. //                }
  939.                 foreach ($person->getARProperties() as $property) {
  940.                     if (!$properties->contains($property)) {
  941.                         $properties->add($property);
  942.                     }
  943.                 }
  944.             }
  945.         }
  946.         return $properties;
  947.     }
  948.     /**
  949.      * @return Collection<int, Property>
  950.      * Les proprités principales gérées/appartenant par les agences de l'utilisateur connnecté
  951.      */
  952.     public function getAMProperties(): Collection {
  953.         $properties = new ArrayCollection();
  954.         if ($this->isProfileAgent()) {
  955.             foreach ($this->getMyEmployees() as $person) {
  956.                 foreach ($person->getAProperties() as $property) {
  957.                     if ($property->isMainProperty() && !$properties->contains($property)) {
  958.                         $properties->add($property);
  959.                     }
  960.                 }
  961.             }
  962.         } else if ($this->isProfileOwner()) {
  963.             foreach ($this->persons as $person) {
  964.                 foreach ($person->getProperties() as $property) {
  965.                     if ($property->isMainProperty() && !$properties->contains($property)) {
  966.                         $properties->add($property);
  967.                     }
  968.                 }
  969.             }
  970.         }
  971.         return $properties;
  972.     }
  973.     /**
  974.      * @return Collection<int, Incident>
  975.      * Les incidents des 3 derniers mois
  976.      */
  977.     public function getL3MIncidents(): Collection {
  978.         $incidents = new ArrayCollection();
  979.         if ($this->isProfileAgent()) {
  980.             foreach ($this->getAAgencies() as $agency) {
  981.                 foreach ($agency->getPersons() as $person) {
  982.                     foreach ($person->getARProperties2() as $property) {
  983.                         foreach ($property->getIncidents() as $incident) {
  984.                             if ($incident->isL3M() && !$incidents->contains($incident)) {
  985.                                 $incidents->add($incident);
  986.                             }
  987.                         }
  988.                     }
  989.                     foreach ($person->getIncidents() as $incident) {
  990.                         if ($incident->isL3M() && !$incidents->contains($incident)) {
  991.                             $incidents->add($incident);
  992.                         }
  993.                     }
  994.                 }
  995.             }
  996.         } else if ($this->isOwner()) {
  997.             foreach ($this->persons as $person) {
  998.                 foreach ($person->getProperties() as $property) {
  999.                     foreach ($property->getIncidents() as $incident) {
  1000.                         if ($incident->isL3M() && !$incidents->contains($incident)) {
  1001.                             $incidents->add($incident);
  1002.                         }
  1003.                     }
  1004.                 }
  1005.                 foreach ($person->getIncidents() as $incident) {
  1006.                     if ($incident->isL3M() && !$incidents->contains($incident)) {
  1007.                         $incidents->add($incident);
  1008.                     }
  1009.                 }
  1010.             }
  1011.         }
  1012.         return $incidents;
  1013.     }
  1014.     public function getYearlyAccounting(): int {
  1015.         $amount 0;
  1016.         if ($this->isEmployee()) {
  1017.             //Toutes les prestations
  1018.             //Toutes les loyers
  1019.             foreach ($this->getMyEmployees() as $person) {
  1020.                 foreach ($person->getAMTenancies() as $tenancy) {
  1021.                     $amount += $tenancy->getYPrc();
  1022.                 }
  1023.             }
  1024.         } else if ($this->isOwner()) {
  1025.             foreach ($this->persons as $person) {
  1026.                 foreach ($person->getAOTenancies() as $tenancy) {
  1027.                     $amount += $tenancy->getYPrc();
  1028.                 }
  1029.             }
  1030.         } else if ($this->isTenant()) {
  1031.             foreach ($this->persons as $person) {
  1032.                 foreach ($person->getTenancies() as $tenancy) {
  1033.                     $amount += $tenancy->getYPrc();
  1034.                 }
  1035.             }
  1036.         }
  1037.         return $amount;
  1038.     }
  1039.     public function getYearlyCollect(): int {
  1040.         $amount 0;
  1041.         if ($this->isEmployee()) {
  1042.             //Toutes les prestations
  1043.             //Toutes les loyers
  1044.             foreach ($this->getMyEmployees() as $person) {
  1045.                 foreach ($person->getAMTenancies() as $tenancy) {
  1046.                     $amount += $tenancy->getYPay();
  1047.                 }
  1048.             }
  1049.         } else if ($this->isOwner()) {
  1050.             foreach ($this->persons as $person) {
  1051.                 foreach ($person->getAOTenancies() as $tenancy) {
  1052.                     $amount += $tenancy->getYPay();
  1053.                 }
  1054.             }
  1055.         } else if ($this->isTenant()) {
  1056.             foreach ($this->persons as $person) {
  1057.                 foreach ($person->getTenancies() as $tenancy) {
  1058.                     $amount += $tenancy->getYPay();
  1059.                 }
  1060.             }
  1061.         }
  1062.         return $amount;
  1063.     }
  1064.     public function getUnpayAccounting(): int {
  1065.         $amount 0;
  1066.         if ($this->isEmployee()) {
  1067.             //Toutes les prestations
  1068.             //Toutes les loyers
  1069.             foreach ($this->getMyEmployees() as $person) {
  1070.                 foreach ($person->getAMTenancies() as $tenancy) {
  1071.                     $amount += $tenancy->getYUPay();
  1072.                 }
  1073.             }
  1074.         } else if ($this->isOwner()) {
  1075.             foreach ($this->persons as $person) {
  1076.                 foreach ($person->getAOTenancies() as $tenancy) {
  1077.                     $amount += $tenancy->getYUPay();
  1078.                 }
  1079.             }
  1080.         } else if ($this->isTenant()) {
  1081.             foreach ($this->persons as $person) {
  1082.                 foreach ($person->getTenancies() as $tenancy) {
  1083.                     $amount += $tenancy->getYUPay();
  1084.                 }
  1085.             }
  1086.         }
  1087.         return $amount;
  1088.     }
  1089.     public function getYAccountingStatistics(): array {
  1090.         $stats Tenancy::getStatisticsTemplate();
  1091.         if ($this->isEmployee()) {
  1092.             //Toutes les prestations
  1093.             //Toutes les loyers
  1094.             foreach ($this->getMyEmployees() as $person) {
  1095.                 foreach ($person->getAMTenancies() as $tenancy) {
  1096.                     $stats Tenancy::addStatisticsTemplate($stats$tenancy->getYStatistics());
  1097.                 }
  1098.             }
  1099.         } else if ($this->isOwner()) {
  1100.             foreach ($this->persons as $person) {
  1101.                 foreach ($person->getAOTenancies() as $tenancy) {
  1102.                     $stats Tenancy::addStatisticsTemplate($stats$tenancy->getYStatistics());
  1103.                 }
  1104.             }
  1105.         } else if ($this->isTenant()) {
  1106.             foreach ($this->persons as $person) {
  1107.                 foreach ($person->getTenancies() as $tenancy) {
  1108.                     $stats Tenancy::addStatisticsTemplate($stats$tenancy->getYStatistics());
  1109.                 }
  1110.             }
  1111.         }
  1112.         return $stats;
  1113.     }
  1114.     public function getMpropertyStatistics(): array {
  1115.         $stats Property::getStatisticsTemplate();
  1116.         foreach ($this->getAMProperties() as $property) {
  1117.             $stats Property::addStatistics($stats$property->getMStatistics());
  1118.         }
  1119.         return $stats;
  1120.     }
  1121.     public function getCPayInvoice(): int {
  1122.         $amount 0;
  1123.         if ($this->isEmployee()) {
  1124.             //Toutes les prestations
  1125.             //Toutes les loyers
  1126.             foreach ($this->getMyEmployees() as $person) {
  1127.                 foreach ($person->getAMTenancies() as $tenancy) {
  1128.                     $amount += $tenancy->getYPayInvoices()->count();
  1129.                 }
  1130.             }
  1131.         } else if ($this->isOwner()) {
  1132.             foreach ($this->persons as $person) {
  1133.                 foreach ($person->getAOTenancies() as $tenancy) {
  1134.                     $amount += $tenancy->getYPayInvoices()->count();
  1135.                 }
  1136.             }
  1137.         } else if ($this->isTenant()) {
  1138.             foreach ($this->persons as $person) {
  1139.                 foreach ($person->getTenancies() as $tenancy) {
  1140.                     $amount += $tenancy->getYPayInvoices()->count();
  1141.                 }
  1142.             }
  1143.         }
  1144.         return $amount;
  1145.     }
  1146.     public function getAllDocuments() {
  1147.         
  1148.     }
  1149.     public function getCUnpayInvoice(): int {
  1150.         $amount 0;
  1151.         if ($this->isEmployee()) {
  1152.             //Toutes les prestations
  1153.             //Toutes les loyers
  1154.             foreach ($this->getMyEmployees() as $person) {
  1155.                 foreach ($person->getAMTenancies() as $tenancy) {
  1156.                     $amount += $tenancy->getYUPayInvoices()->count();
  1157.                 }
  1158.             }
  1159.         } else if ($this->isOwner()) {
  1160.             foreach ($this->persons as $person) {
  1161.                 foreach ($person->getAOTenancies() as $tenancy) {
  1162.                     $amount += $tenancy->getYUPayInvoices()->count();
  1163.                 }
  1164.             }
  1165.         } else if ($this->isTenant()) {
  1166.             foreach ($this->persons as $person) {
  1167.                 foreach ($person->getTenancies() as $tenancy) {
  1168.                     $amount += $tenancy->getYUPayInvoices()->count();
  1169.                 }
  1170.             }
  1171.         }
  1172.         return $amount;
  1173.     }
  1174.     public function isSelfOwner(): bool {
  1175.         return $this->profile !== null && $this->profile->getId() ==  Profile::PROFILE_OWNER;
  1176.     }
  1177.     public function isVisitor(): bool {
  1178.         return $this->profile == null || $this->profile->getId()  == Profile::PROFILE_VISITOR;
  1179.     }
  1180.     public function isProfileOwner(): bool {
  1181.         return $this->profile !== null && $this->profile->getId() == Profile::PROFILE_OWNER;
  1182.     }
  1183.     public function isProfileVisitor(): bool {
  1184.         return $this->profile == null || $this->profile->getId() == Profile::PROFILE_VISITOR;
  1185.     }
  1186.     public function isProfileTenant(): bool {
  1187.         return $this->profile !== null && $this->profile->getId() == Profile::PROFILE_TENANT;
  1188.     }
  1189.     public function isProfileAgent(): bool {
  1190.         return $this->profile !== null && ($this->profile->getId() == Profile::PROFILE_AGENT || $this->profile->getId() == || $this->profile->getId() == 2);
  1191.     }
  1192.     public function isProfileDemarcheur(): bool {
  1193.         return $this->profile !== null && $this->profile->getId() == Profile::PROFILE_DEMARCHEUR;
  1194.     }
  1195. }