<?php
namespace App\Entity\Security;
use App\Common\Entity\DefaultEntity;
use App\Entity\JIT\Comment;
use App\Entity\JIT\Feeling;
use App\Entity\JIT\PContact;
use App\Entity\JIT\Person;
use App\Entity\JIT\Property;
use App\Entity\JIT\Tenancy;
use App\Repository\Security\UserRepository;
use DateTime;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\String\Slugger\AsciiSlugger;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass=UserRepository::class)
* @ORM\Table(name="`pr_security_user`")
* @UniqueEntity(
* fields={"username"},
* message="_TT_ Ce nom d'utilisateur est déja pris"
* )
* @UniqueEntity("email")
* @UniqueEntity("phone")
* @ORM\HasLifecycleCallbacks
*/
class User extends DefaultEntity implements UserInterface, PasswordAuthenticatedUserInterface {
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $username;
/**
* @ORM\Column(type="string", length=255)
*/
private $slug;
/**
* @ORM\Column(type="string", length=255)
*/
private $fullname;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $password;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $code;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $createAt;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $lmd;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $fad;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $lad;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $lmp;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $lrp;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $description;
/**
* @ORM\Column(type="string", length=10, nullable=true)
*/
private $language;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $picture;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $enabled;
/**
* @ORM\Column(type="json", nullable=true)
*/
private $userroles = [];
/**
* @ORM\ManyToOne(targetEntity=Profile::class, inversedBy="users")
*/
private $profile;
/**
* @ORM\Column(type="string", length=255)
* @Assert\Email(
* message = "The email '{{ value }}' is not a valid email."
* )
*/
private $email;
/**
* @var File
*/
private $image;
/**
* @var string
*/
private $defaultPassword;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $privatePassword;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $phone;
private $updateLad;
/**
* @ORM\OneToMany(targetEntity=Person::class, mappedBy="account", cascade={"persist","remove"})
*/
private $persons;
/**
* @ORM\OneToMany(targetEntity=PContact::class, mappedBy="account", cascade={"persist","remove"})
*/
private $pContacts;
/**
* @ORM\OneToMany(targetEntity=Comment::class, mappedBy="user")
*/
private $comments;
/**
* @ORM\OneToMany(targetEntity=Feeling::class, mappedBy="user")
*/
private $feelings;
public function __construct() {
$this->enabled = true;
$this->persons = new ArrayCollection();
$this->pContacts = new ArrayCollection();
$this->comments = new ArrayCollection();
$this->feelings = new ArrayCollection();
}
/**
* @ORM\PrePersist()
*/
public function prePersist() {
parent::prePersist();
$this->createAt = new DateTime();
// $this->enabled = false;
// $this->setUsernameValue(); :: Not her because it is in controller to resolve the problem of unicity
}
/**
* @ORM\PreUpdate()
*/
public function preUpdate() {
parent::preUpdate();
if (!$this->updateLad) {
$this->lmd = new DateTime();
}
$this->setUsernameValue();
if ($this->password === null) {
$this->setDefaultPassword();
}
}
public function getId(): ?int {
return $this->id;
}
public function getUsername(): ?string {
return $this->username;
}
public function setUsername(string $username): self {
$this->username = $username;
return $this;
}
private function setUsernameValue(): void {
if ($this->username === null) {
$this->username = $this->generateUsername();
}
}
public function generateUsername(): ?string {
if ($this->fullname !== null) {
$worrds = explode(' ', trim($this->fullname));
$username = "";
$nbw = count($worrds) - 1;
if ($nbw >= 1) {
$username .= $worrds[$nbw];
}
$username .= '.' . $worrds[0];
return $this->sluggerUsername($username);
} else {
return null;
}
}
private function sluggerUsername($username): string {
$slugger = new AsciiSlugger();
$table = array(
'Š' => 'S', 'š' => 's', 'Đ' => 'Dj', 'đ' => 'dj', 'Ž' => 'Z', 'ž' => 'z', 'Č' => 'C', 'č' => 'c', 'Ć' => 'C', 'ć' => 'c',
'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Å' => 'A', 'Æ' => 'A', 'Ç' => 'C', 'È' => 'E', 'É' => 'E',
'Ê' => 'E', 'Ë' => 'E', 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I', 'Ï' => 'I', 'Ñ' => 'N', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O',
'Õ' => 'O', 'Ö' => 'O', 'Ø' => 'O', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'U', 'Ý' => 'Y', 'Þ' => 'B', 'ß' => 'Ss',
'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' => 'a', 'å' => 'a', 'æ' => 'a', 'ç' => 'c', 'è' => 'e', 'é' => 'e',
'ê' => 'e', 'ë' => 'e', 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i', 'ð' => 'o', 'ñ' => 'n', 'ò' => 'o', 'ó' => 'o',
'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ø' => 'o', 'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ý' => 'y', 'ý' => 'y', 'þ' => 'b',
'ÿ' => 'y', 'Ŕ' => 'R', 'ŕ' => 'r',
);
$fusername = strtr($username, $table);
return $slugger->slug(strtolower($fusername), '.');
}
public function getSlug(): ?string {
return $this->slug;
}
protected function setSlug(string $slug): void {
$this->slug = $slug;
}
public function getPassword(): ?string {
return $this->password;
}
public function setPassword(?string $password): self {
$this->password = $password;
return $this;
}
public function setDefaultPassword(): void {
$this->defaultPassword = $this->password_generate();
// $passwordEncoder = new \Symfony\Component\Security\Core\Encoder\UserPasswordEncoder($encoderFactory);
// $this->password = $passwordEncoder->encodePassword($this, $this->defaultPassword);
$this->password = $this->defaultPassword;
$this->privatePassword = false;
}
public function getDefaultPassword(): ?string {
return $this->defaultPassword;
}
private function password_generate($length = 7): string {
$data = '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcefghijklmnopqrstuvwxyz';
return substr(str_shuffle($data), 0, $length);
}
public function getCode(): ?string {
return $this->code;
}
public function setCode(?string $code): self {
$this->code = $code;
return $this;
}
public function getCreateAt(): ?DateTimeInterface {
return $this->createAt;
}
public function getLmd(): ?DateTimeInterface {
return $this->lmd;
}
protected function setLmd(?DateTimeInterface $lmd): self {
$this->lmd = $lmd;
return $this;
}
public function getFad(): ?DateTimeInterface {
return $this->fad;
}
public function setFad(?DateTimeInterface $fad): self {
$this->fad = $fad;
return $this;
}
public function getLad(): ?DateTimeInterface {
return $this->lad;
}
public function setLad(?DateTimeInterface $lad): self {
$this->lad = $lad;
return $this;
}
public function getUpdateLad() {
return $this->updateLad;
}
public function setUpdateLad($updateLad): void {
$this->updateLad = $updateLad;
}
public function getLmp(): ?DateTimeInterface {
return $this->lmp;
}
public function setLmp(?DateTimeInterface $lmp): self {
$this->lmp = $lmp;
return $this;
}
public function getLrp(): ?DateTimeInterface {
return $this->lrp;
}
public function setLrp(?DateTimeInterface $lrp): self {
$this->lrp = $lrp;
return $this;
}
public function getDescription(): ?string {
return $this->description;
}
public function setDescription(?string $description): self {
$this->description = $description;
return $this;
}
public function getLanguage(): ?string {
return $this->language;
}
public function setLanguage(?string $language): self {
$this->language = $language;
return $this;
}
public function getPicture(): ?string {
if ($this->picture !== null) {
return $this->picture;
// } else if ($this->gender == 'F') {
// return 'img_avatar_f.png';
} else {
return 'img_avatar_h.png';
}
}
public function getPicture2(): ?string {
if ($this->picture !== null && $this->picture !== 'img_avatar_h.png' && $this->picture !== 'img_avatar_f.png') {
return $this->picture;
// } else if ($this->gender == 'F') {
//// return 'img_avatar_f.png';
// $i = $this->id % 36 +1;
// return 'avatar/avatarf'.$i.'.png';
} else {
$i = $this->id % 36 + 1;
return 'avatar/avatarh' . $i . '.png';
}
}
public function getPicture3(): ?string {
if ($this->getPerson() !== null) {
return $this->getPerson()->getImage();
} else {
return 'img_avatar_h.png';
}
}
public function setPicture(?string $picture): self {
$this->picture = $picture;
return $this;
}
public function isEnabled(): ?bool {
return $this->enabled;
}
public function setEnabled(?bool $enabled): self {
$this->enabled = $enabled;
return $this;
}
public function getUserroles(): ?array {
return $this->userroles;
}
public function setUserroles(?array $userroles): self {
$this->userroles = $userroles;
return $this;
}
public function addUserrole(string $userrole): self {
if ($this->userroles === null) {
$this->userroles = [$userrole];
} else {
if (!in_array($userrole, $this->userroles)) {
$this->userroles[] = $userrole;
}
}
return $this;
}
public function removeUserrole(string $userrole): self {
if ($this->userroles !== null && in_array($userrole, $this->userroles)) {
$this->userroles = array_diff($this->userroles, [$userrole]);
}
return $this;
}
public function getProfile(): ?Profile {
return $this->profile;
}
public function setProfile(?Profile $profile): self {
$this->profile = $profile;
return $this;
}
public function getFullname(): ?string {
return $this->fullname;
}
public function setFullname(string $fullname): self {
$this->fullname = $fullname;
return $this;
}
public function getName(): ?string {
return $this->getFullname();
}
public function getEmail(): ?string {
return $this->email;
}
public function setEmail(string $email): self {
$this->email = $email;
return $this;
}
public function getClass(): ?string {
return self::class;
}
/**
* @see UserInterface
*/
public function getRoles(): array {
$roles = $this->userroles;
if ($this->profile !== null) {
$roles = array_merge($roles, $this->profile->getRoles());
}
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
return array_unique($roles);
}
/**
* @see UserInterface
*/
public function getSalt() {
// not needed when using the "bcrypt" algorithm in security.yaml
}
/**
* @see UserInterface
*/
public function eraseCredentials() {
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function getImage(): ?File {
return $this->image;
}
public function setImage(File $image): self {
$this->image = $image;
return $this;
}
public function lock(): self {
$this->enabled = false;
return $this;
}
public function unlock(): self {
$this->enabled = true;
return $this;
}
public function getPhone(): ?string {
return $this->phone;
}
public function setPhone(?string $phone): self {
$this->phone = $phone;
return $this;
}
public function getProfileName(): ?string {
if ($this->profile !== null) {
return $this->profile->getName();
}
return "";
}
public function getPrivatePassword() {
return $this->privatePassword;
}
public function setPrivatePassword($privatePassword): void {
$this->privatePassword = $privatePassword;
}
public function getUserIdentifier(): ?string {
return $this->username;
}
/**
* @return Collection<int, Person>
*/
public function getPersons(): Collection {
return $this->persons;
}
public function addPerson(Person $person): self {
if (!$this->persons->contains($person)) {
$this->persons[] = $person;
$person->setAccount($this);
}
return $this;
}
public function removePerson(Person $person): self {
if ($this->persons->removeElement($person)) {
// set the owning side to null (unless already changed)
if ($person->getAccount() === $this) {
$person->setAccount(null);
}
}
return $this;
}
public function getPerson(): ?Person {
if ($this->persons->isEmpty()) {
return null;
} else {
return $this->persons[0];
}
}
public function getPContact(): ?PContact {
if (!$this->pContacts->isEmpty()) {
return $this->pContacts[0];
} else {
return null;
}
}
/**
* @return Collection<int, PContact>
*/
public function getPContacts(): Collection {
return $this->pContacts;
}
public function addPContact(PContact $pContact): self {
if (!$this->pContacts->contains($pContact)) {
$this->pContacts[] = $pContact;
$pContact->setAccount($this);
}
return $this;
}
public function removePContact(PContact $pContact): self {
if ($this->pContacts->removeElement($pContact)) {
// set the owning side to null (unless already changed)
if ($pContact->getAccount() === $this) {
$pContact->setAccount(null);
}
}
return $this;
}
/**
* @return Collection<int, Comment>
*/
public function getComments(): Collection {
return $this->comments;
}
public function addComment(Comment $comment): self {
if (!$this->comments->contains($comment)) {
$this->comments[] = $comment;
$comment->setUser($this);
}
return $this;
}
public function removeComment(Comment $comment): self {
if ($this->comments->removeElement($comment)) {
// set the owning side to null (unless already changed)
if ($comment->getUser() === $this) {
$comment->setUser(null);
}
}
return $this;
}
/**
* @return Collection<int, Feeling>
*/
public function getFeelings(): Collection {
return $this->feelings;
}
public function addFeeling(Feeling $feeling): self {
if (!$this->feelings->contains($feeling)) {
$this->feelings[] = $feeling;
$feeling->setUser($this);
}
return $this;
}
public function removeFeeling(Feeling $feeling): self {
if ($this->feelings->removeElement($feeling)) {
// set the owning side to null (unless already changed)
if ($feeling->getUser() === $this) {
$feeling->setUser(null);
}
}
return $this;
}
/**
* @return Collection<int, Person>
*/
public function getMyEmployees(): Collection {
$myEmployees = new ArrayCollection();
foreach ($this->persons as $person) {
if ($person->isEmployee()) {
if (!$myEmployees->contains($person)) {
$myEmployees[] = $person;
}
}
foreach ($person->getMyAgencies() as $agency) {
foreach ($agency->getPersons() as $oPerson) {
if ($oPerson->isEmployee()) {
if (!$myEmployees->contains($oPerson)) {
$myEmployees[] = $oPerson;
}
}
}
}
}
return $myEmployees;
}
/**
* @return Collection<int, Person>
*/
public function getMyOwners(): Collection {
$myOwners = new ArrayCollection();
foreach ($this->persons as $person) {
if ($person->isOwner()) {
if (!$myOwners->contains($person)) {
$myOwners[] = $person;
}
} else if ($person->isEmployee()) {
foreach ($person->getMyOwners() as $oPerson) {
if ($oPerson->isOwner()) {
if (!$myOwners->contains($oPerson)) {
$myOwners[] = $oPerson;
}
}
}
}
}
return $myOwners;
}
/**
* @return Collection<int, Person>
*/
public function getMyAOwners(): Collection {
$myOwners = new ArrayCollection();
// foreach ($this->getMyEmployees() as $person) {
// if ($person->isOwner()) {
// if (!$myOwners->contains($person)) {
// $myOwners[] = $person;
// }
// }
//
// if ($person->isEmployee()) {
// foreach ($person->getMyOwners() as $oPerson) {
// if ($oPerson->isOwner()) {
// if (!$myOwners->contains($oPerson)) {
// $myOwners[] = $oPerson;
// }
// }
// }
// }
// }
foreach ($this->persons as $person) {
if ($person->isOwner()) {
if (!$myOwners->contains($person)) {
$myOwners[] = $person;
}
} else if ($person->isEmployee()) {
foreach ($person->getMyOwners() as $oPerson) {
if ($oPerson->isOwner()) {
if (!$myOwners->contains($oPerson)) {
$myOwners[] = $oPerson;
}
}
}
}
}
foreach ($this->getMyEmployees() as $person) {
foreach ($person->getMyOwners() as $oPerson) {
if ($oPerson->isOwner()) {
if (!$myOwners->contains($oPerson)) {
$myOwners[] = $oPerson;
}
}
}
}
return $myOwners;
}
/**
* Mes locataires
* @return Collection<int, Person>
*/
public function getMyTenants(): Collection {
$myTenants = new ArrayCollection();
foreach ($this->persons as $person) {
if ($person->isTenant()) {
if (!$myTenants->contains($person)) {
$myTenants[] = $person;
}
} else if ($person->isEmployee() || $person->isOwner()) {
foreach ($person->getMyTenants() as $oPerson) {
if ($oPerson->isTenant()) {
if (!$myTenants->contains($oPerson)) {
$myTenants[] = $oPerson;
}
}
}
}
}
return $myTenants;
}
/**
* Tous les locataires de mon agence
* @return Collection<int, Person>
*/
public function getMyATenants(): Collection {
$myTenants = $this->getMyTenants();
foreach ($this->getMyEmployees() as $person) {
foreach ($person->getMyTenants() as $oPerson) {
if (!$myTenants->contains($oPerson)) {
$myTenants[] = $oPerson;
}
}
}
return $myTenants;
}
/**
* Affiche toutes les personnes qu'on peut voir
* @return Collection<int, Person>
*/
public function getAllPersons(): Collection {
$persons = $this->getMyEmployees();
foreach ($this->getMyAOwners() as $person) {
if (!$persons->contains($person)) {
$persons->add($person);
}
}
foreach ($this->getMyATenants() as $person) {
if (!$persons->contains($person)) {
$persons->add($person);
}
}
return $persons;
}
/**
* Affiche toutes les donneurs d'ordre (propriétaires et locataires) qu'on peut voir
* @return Collection<int, Person>
*/
public function getAllDnd(): Collection {
$persons = $this->getMyAOwners();
foreach ($this->getMyATenants() as $person) {
if (!$persons->contains($person)) {
$persons->add($person);
}
}
return $persons;
}
/**
* @return Collection<int, Agency>
*/
public function getMyAgencies(): Collection {
$myAgencies = new ArrayCollection();
foreach ($this->getPersons() as $person) {
if ($person->isEmployee()) {
foreach ($person->getMyAgencies() as $agency) {
if (!$myAgencies->contains($agency)) {
$myAgencies[] = $agency;
}
}
}
}
return $myAgencies;
}
/**
* Retourne toutes les agences auquelles appartient l'utilisateur connecté
* @return Collection<int, Agency>
*/
public function getAAgencies(): Collection {
$AAgencies = new ArrayCollection();
foreach ($this->getPersons() as $person) {
$agency = $person->getAgency();
if ($agency != null && !$AAgencies->contains($agency)) {
$AAgencies[] = $agency;
}
if ($person->isEmployee()) {
foreach ($person->getMyAgencies() as $agency) {
if (!$AAgencies->contains($agency)) {
$AAgencies[] = $agency;
}
}
}
}
return $AAgencies;
}
public function hasAgency(): bool {
foreach ($this->persons as $person) {
if ($person->hasAgency()) {
return true;
}
}
return false;
}
public function hasAffair(): bool {
foreach ($this->persons as $person) {
if ($person->hasAffair()) {
return true;
}
}
return false;
}
public function isEmployee(): bool {
foreach ($this->persons as $person) {
if ($person->isEmployee()) {
return true;
}
}
return false;
}
public function isOwner(): bool {
foreach ($this->persons as $person) {
if ($person->isOwner()) {
return true;
}
}
return false;
}
public function isTenant(): bool {
foreach ($this->persons as $person) {
if ($person->isTenant()) {
return true;
}
}
return false;
}
/**
* @return Collection<int, Account>
*/
public function getAccounts(): Collection {
$accounts = new ArrayCollection();
foreach ($this->getPersons() as $person) {
foreach ($person->getAccounts() as $account) {
if (!$accounts->contains($account)) {
$accounts[] = $account;
}
}
}
return $accounts;
}
/**
* @return Collection<int, Tenancy>
* Les locations (gérées par/appartenants aux) agences de l'utilisateur connnecté
*/
public function getATenancies(): Collection {
$tenancies = new ArrayCollection();
if ($this->isEmployee()) {
foreach ($this->getAMTenancies() as $person) {
foreach ($person->getATenancies() as $tenancy) {
if (!$tenancies->contains($tenancy)) {
$tenancies->add($tenancy);
}
}
}
} else if ($this->isOwner()) {
foreach ($this->persons as $person) {
foreach ($person->getAOTenancies() as $tenancy) {
if (!$tenancies->contains($tenancy)) {
$tenancies->add($tenancy);
}
}
}
} else if ($this->isTenant()) {
foreach ($this->persons as $person) {
foreach ($person->getATenancies() as $tenancy) {
if (!$tenancies->contains($tenancy)) {
$tenancies->add($tenancy);
}
}
}
}
return $tenancies;
}
/**
* @return Collection<int, Property>
* Les proprités gérées/appartenant par les agences de l'utilisateur connnecté
*/
public function getAProperties(): Collection {
$properties = new ArrayCollection();
if ($this->isProfileAgent()) {
// foreach ($this->getMyEmployees() as $person) {
// foreach ($person->getAProperties() as $property) {
// if (!$properties->contains($property)) {
// $properties->add($property);
// }
// }
// }
foreach ($this->getAAgencies() as $agency) {
foreach ($agency->getPersons() as $person) {
foreach ($person->getARProperties2() as $property) {
if (!$properties->contains($property)) {
$properties->add($property);
}
}
}
}
} else if ($this->isProfileOwner()) {
foreach ($this->persons as $person) {
foreach ($person->getARProperties2() as $property) {
if (!$properties->contains($property)) {
$properties->add($property);
}
}
}
}
return $properties;
}
/**
* @return Collection<int, Property>
* Les proprités vacants gérées par les agences de l'utilisateur connnecté
*/
public function getVProperties(): Collection {
$properties = new ArrayCollection();
if ($this->isProfileAgent()) {
foreach ($this->getAAgencies() as $agency) {
foreach ($agency->getPersons() as $person) {
foreach ($person->getARVProperties2() as $property) {
if (!$properties->contains($property)) {
$properties->add($property);
}
}
}
}
} else if ($this->isOwner()) {
foreach ($this->persons as $person) {
foreach ($person->getARVProperties2() as $property) {
if (!$properties->contains($property)) {
$properties->add($property);
}
}
}
}
return $properties;
}
/**
* @return Collection<int, Property>
* Les proprités louables gérées/appartenant par les agences de l'utilisateur connnecté
*/
public function getARProperties(): Collection {
$properties = new ArrayCollection();
if ($this->isEmployee()) {
foreach ($this->getMyEmployees() as $person) {
// foreach ($person->getAProperties() as $property) {
// if ($property->isRented() && !$properties->contains($property)) {
// $properties->add($property);
// }
// }
foreach ($person->getARProperties() as $property) {
if (!$properties->contains($property)) {
$properties->add($property);
}
}
}
} else if ($this->isOwner()) {
foreach ($this->persons as $person) {
// foreach ($person->getProperties() as $property) {
// if ($property->isRented() && !$properties->contains($property)) {
// $properties->add($property);
// }
// }
foreach ($person->getARProperties() as $property) {
if (!$properties->contains($property)) {
$properties->add($property);
}
}
}
}
return $properties;
}
/**
* @return Collection<int, Property>
* Les proprités principales gérées/appartenant par les agences de l'utilisateur connnecté
*/
public function getAMProperties(): Collection {
$properties = new ArrayCollection();
if ($this->isProfileAgent()) {
foreach ($this->getMyEmployees() as $person) {
foreach ($person->getAProperties() as $property) {
if ($property->isMainProperty() && !$properties->contains($property)) {
$properties->add($property);
}
}
}
} else if ($this->isProfileOwner()) {
foreach ($this->persons as $person) {
foreach ($person->getProperties() as $property) {
if ($property->isMainProperty() && !$properties->contains($property)) {
$properties->add($property);
}
}
}
}
return $properties;
}
/**
* @return Collection<int, Incident>
* Les incidents des 3 derniers mois
*/
public function getL3MIncidents(): Collection {
$incidents = new ArrayCollection();
if ($this->isProfileAgent()) {
foreach ($this->getAAgencies() as $agency) {
foreach ($agency->getPersons() as $person) {
foreach ($person->getARProperties2() as $property) {
foreach ($property->getIncidents() as $incident) {
if ($incident->isL3M() && !$incidents->contains($incident)) {
$incidents->add($incident);
}
}
}
foreach ($person->getIncidents() as $incident) {
if ($incident->isL3M() && !$incidents->contains($incident)) {
$incidents->add($incident);
}
}
}
}
} else if ($this->isOwner()) {
foreach ($this->persons as $person) {
foreach ($person->getProperties() as $property) {
foreach ($property->getIncidents() as $incident) {
if ($incident->isL3M() && !$incidents->contains($incident)) {
$incidents->add($incident);
}
}
}
foreach ($person->getIncidents() as $incident) {
if ($incident->isL3M() && !$incidents->contains($incident)) {
$incidents->add($incident);
}
}
}
}
return $incidents;
}
public function getYearlyAccounting(): int {
$amount = 0;
if ($this->isEmployee()) {
//Toutes les prestations
//Toutes les loyers
foreach ($this->getMyEmployees() as $person) {
foreach ($person->getAMTenancies() as $tenancy) {
$amount += $tenancy->getYPrc();
}
}
} else if ($this->isOwner()) {
foreach ($this->persons as $person) {
foreach ($person->getAOTenancies() as $tenancy) {
$amount += $tenancy->getYPrc();
}
}
} else if ($this->isTenant()) {
foreach ($this->persons as $person) {
foreach ($person->getTenancies() as $tenancy) {
$amount += $tenancy->getYPrc();
}
}
}
return $amount;
}
public function getYearlyCollect(): int {
$amount = 0;
if ($this->isEmployee()) {
//Toutes les prestations
//Toutes les loyers
foreach ($this->getMyEmployees() as $person) {
foreach ($person->getAMTenancies() as $tenancy) {
$amount += $tenancy->getYPay();
}
}
} else if ($this->isOwner()) {
foreach ($this->persons as $person) {
foreach ($person->getAOTenancies() as $tenancy) {
$amount += $tenancy->getYPay();
}
}
} else if ($this->isTenant()) {
foreach ($this->persons as $person) {
foreach ($person->getTenancies() as $tenancy) {
$amount += $tenancy->getYPay();
}
}
}
return $amount;
}
public function getUnpayAccounting(): int {
$amount = 0;
if ($this->isEmployee()) {
//Toutes les prestations
//Toutes les loyers
foreach ($this->getMyEmployees() as $person) {
foreach ($person->getAMTenancies() as $tenancy) {
$amount += $tenancy->getYUPay();
}
}
} else if ($this->isOwner()) {
foreach ($this->persons as $person) {
foreach ($person->getAOTenancies() as $tenancy) {
$amount += $tenancy->getYUPay();
}
}
} else if ($this->isTenant()) {
foreach ($this->persons as $person) {
foreach ($person->getTenancies() as $tenancy) {
$amount += $tenancy->getYUPay();
}
}
}
return $amount;
}
public function getYAccountingStatistics(): array {
$stats = Tenancy::getStatisticsTemplate();
if ($this->isEmployee()) {
//Toutes les prestations
//Toutes les loyers
foreach ($this->getMyEmployees() as $person) {
foreach ($person->getAMTenancies() as $tenancy) {
$stats = Tenancy::addStatisticsTemplate($stats, $tenancy->getYStatistics());
}
}
} else if ($this->isOwner()) {
foreach ($this->persons as $person) {
foreach ($person->getAOTenancies() as $tenancy) {
$stats = Tenancy::addStatisticsTemplate($stats, $tenancy->getYStatistics());
}
}
} else if ($this->isTenant()) {
foreach ($this->persons as $person) {
foreach ($person->getTenancies() as $tenancy) {
$stats = Tenancy::addStatisticsTemplate($stats, $tenancy->getYStatistics());
}
}
}
return $stats;
}
public function getMpropertyStatistics(): array {
$stats = Property::getStatisticsTemplate();
foreach ($this->getAMProperties() as $property) {
$stats = Property::addStatistics($stats, $property->getMStatistics());
}
return $stats;
}
public function getCPayInvoice(): int {
$amount = 0;
if ($this->isEmployee()) {
//Toutes les prestations
//Toutes les loyers
foreach ($this->getMyEmployees() as $person) {
foreach ($person->getAMTenancies() as $tenancy) {
$amount += $tenancy->getYPayInvoices()->count();
}
}
} else if ($this->isOwner()) {
foreach ($this->persons as $person) {
foreach ($person->getAOTenancies() as $tenancy) {
$amount += $tenancy->getYPayInvoices()->count();
}
}
} else if ($this->isTenant()) {
foreach ($this->persons as $person) {
foreach ($person->getTenancies() as $tenancy) {
$amount += $tenancy->getYPayInvoices()->count();
}
}
}
return $amount;
}
public function getAllDocuments() {
}
public function getCUnpayInvoice(): int {
$amount = 0;
if ($this->isEmployee()) {
//Toutes les prestations
//Toutes les loyers
foreach ($this->getMyEmployees() as $person) {
foreach ($person->getAMTenancies() as $tenancy) {
$amount += $tenancy->getYUPayInvoices()->count();
}
}
} else if ($this->isOwner()) {
foreach ($this->persons as $person) {
foreach ($person->getAOTenancies() as $tenancy) {
$amount += $tenancy->getYUPayInvoices()->count();
}
}
} else if ($this->isTenant()) {
foreach ($this->persons as $person) {
foreach ($person->getTenancies() as $tenancy) {
$amount += $tenancy->getYUPayInvoices()->count();
}
}
}
return $amount;
}
public function isSelfOwner(): bool {
return $this->profile !== null && $this->profile->getId() == Profile::PROFILE_OWNER;
}
public function isVisitor(): bool {
return $this->profile == null || $this->profile->getId() == Profile::PROFILE_VISITOR;
}
public function isProfileOwner(): bool {
return $this->profile !== null && $this->profile->getId() == Profile::PROFILE_OWNER;
}
public function isProfileVisitor(): bool {
return $this->profile == null || $this->profile->getId() == Profile::PROFILE_VISITOR;
}
public function isProfileTenant(): bool {
return $this->profile !== null && $this->profile->getId() == Profile::PROFILE_TENANT;
}
public function isProfileAgent(): bool {
return $this->profile !== null && ($this->profile->getId() == Profile::PROFILE_AGENT || $this->profile->getId() == 1 || $this->profile->getId() == 2);
}
public function isProfileDemarcheur(): bool {
return $this->profile !== null && $this->profile->getId() == Profile::PROFILE_DEMARCHEUR;
}
}