src/Entity/User.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Metadata\ApiResource;
  4. use App\Repository\UserRepository;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\DBAL\Types\Types;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  10. use Symfony\Component\Security\Core\User\UserInterface;
  11. use Symfony\Component\Uid\Uuid;
  12. use Symfony\Component\Validator\Constraints as Assert;
  13. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  14. #[ORM\Entity(repositoryClassUserRepository::class)]
  15. #[ORM\Table(name'`user`')]
  16. #[ApiResource]
  17. #[UniqueEntity(
  18.     fields: ['email'],
  19.     message'Cet e-mail est déjà utilisé. Veuillez en essayer un autre.'
  20. )]
  21. #[UniqueEntity(fields: ['username'], message'This username is already taken.')]
  22. class User implements UserInterfacePasswordAuthenticatedUserInterface
  23. {
  24.     #[ORM\Id]
  25.     #[ORM\GeneratedValue]
  26.     #[ORM\Column]
  27.     private ?int $id null;
  28.     #[ORM\Column(length180uniquetrue)]
  29.     private ?string $email null;
  30.     #[ORM\Column(length180nullabletrue)]
  31.     private ?string $username null;
  32.     #[ORM\Column(length255)]
  33.     private ?string $nom null;
  34.     #[ORM\Column(length255)]
  35.     private ?string $prenom null;
  36.     #[ORM\Column(length255,nullabletrue)]
  37.     private ?string $telephone1 null;
  38.     #[ORM\Column(length400nullabletrue)]
  39.     private ?string $adresse null;
  40.     #[ORM\ManyToOne(targetEntityEntreprise::class, inversedBy'employes')]
  41.     #[ORM\JoinColumn(nullabletrue)]
  42.     #[ORM\JoinColumn(onDelete'SET NULL')]
  43.     private $entreprise;
  44.     #[ORM\OneToMany(mappedBy'employe'targetEntityPointage::class)]
  45.     private $pointages;
  46.     #[ORM\Column]
  47.     private array $roles = ['ROLE_USER'];
  48.    
  49.     #[ORM\Column(type'string'length255)]
  50.    // #[Assert\NotBlank]
  51.     //#[Assert\Length(min: 6, max: 4096)]
  52.     private ?string $password null;
  53.     // Ce champ est uniquement utilisé pour la confirmation en mémoire
  54.     #[Assert\NotBlank(groups: ['registration'])]
  55.     #[Assert\EqualTo(propertyPath'password'message'Les mots de passe doivent correspondre.')]
  56.     private ?string $plainPassword null;
  57.     
  58.     #[ORM\Column(nullabletrue)]
  59.     private ?int $credits null;
  60.     #[ORM\Column(type'string'nullabletrue)]
  61.     private ?string $resetToken null;
  62.     #[ORM\Column(type'datetime'nullabletrue)]
  63.     private ?\DateTimeInterface $resetTokenExpiresAt null;
  64.     #[ORM\OneToMany(mappedBy'userCredit'targetEntityCredits::class)]
  65.     private Collection $creditsTransaction;
  66.     
  67.     #[ORM\OneToMany(mappedBy'userabonnee'targetEntityAbonnement::class, cascade: ['remove'])]
  68.        private Collection $abonnements;
  69.     #[ORM\Column(type"boolean"options: ["default" => false])]
  70.     private bool $isVerified false;
  71.     #[ORM\Column(nullabletrue)]
  72.     private ?int $pointfidelite 0;
  73.     
  74.     #[ORM\Column(type'string'length255nullabletrue)]
  75.     private ?string $confirmationToken null;
  76.     
  77.     #[ORM\OneToMany(mappedBy'users'targetEntityLivraisons::class)]
  78.     private Collection $livraisons;
  79.     #[ORM\OneToMany(mappedBy'usersfinancement'targetEntityFinancement::class)]
  80.     private Collection $financements;
  81.     
  82.     #[ORM\Column(length50nullabletrue)]
  83. private ?string $contractType null;
  84. #[ORM\Column(length50nullabletrue)]
  85. private ?string $employeeId null;
  86. public function getEmployeeId(): ?string
  87. {
  88.     return $this->employeeId;
  89. }
  90. public function setEmployeeId(?string $employeeId): self
  91. {
  92.     $this->employeeId $employeeId;
  93.     return $this;
  94. }
  95.     private ?TypeAbonnement $typeabonnement null;
  96. #[ORM\Column(type'datetime_immutable'nullabletrue)]
  97. public function getContractType(): ?string
  98. {
  99.     return $this->contractType;
  100. }
  101. public function setContractType(?string $contractType): self
  102. {
  103.     $this->contractType $contractType;
  104.     return $this;
  105. }
  106. private ?\DateTimeImmutable $hireDate null;
  107. public function getHireDate(): ?\DateTimeImmutable
  108. {
  109.     return $this->hireDate;
  110. }
  111. public function setHireDate(?\DateTimeImmutable $hireDate): self
  112. {
  113.     $this->hireDate $hireDate;
  114.     return $this;
  115. }
  116.     #[ORM\Column(type'float'nullabletrue)]
  117. private ?float $salary null;
  118. public function getSalary(): ?float
  119. {
  120.     return $this->salary;
  121. }
  122. public function setSalary(?float $salary): self
  123. {
  124.     $this->salary $salary;
  125.     return $this;
  126. }
  127.       // Getters and setters
  128.       public function getIsVerified(): bool
  129.       {
  130.           return $this->isVerified;
  131.       }
  132.   
  133.       public function setIsVerified(bool $isVerified): self
  134.       {
  135.           $this->isVerified $isVerified;
  136.   
  137.           return $this;
  138.       }
  139.  public function getConfirmationToken(): ?string
  140.                                                                          {
  141.                                                                              return $this->confirmationToken;
  142.                                                                          }
  143.     public function setConfirmationToken(?string $confirmationToken): self
  144.     {
  145.         $this->confirmationToken $confirmationToken;
  146.         return $this;
  147.     }
  148.    
  149.     public function getResetToken(): ?string
  150.     {
  151.         return $this->resetToken;
  152.     }
  153.     public function setResetToken(?string $resetToken): self
  154.     {
  155.         $this->resetToken $resetToken;
  156.         return $this;
  157.     }
  158.     public function getResetTokenExpiresAt(): ?\DateTimeInterface
  159.     {
  160.         return $this->resetTokenExpiresAt;
  161.     }
  162.     public function setResetTokenExpiresAt(?\DateTimeInterface $expiresAt): self
  163.     {
  164.         $this->resetTokenExpiresAt $expiresAt;
  165.         return $this;
  166.     }
  167.     public function __construct()
  168.     {
  169.         $this->livraisons = new ArrayCollection();
  170.         $this->creditsTransaction = new ArrayCollection();
  171.         $this->abonnements = new ArrayCollection();
  172.         $this->financements = new ArrayCollection();
  173.         $this->pointages = new ArrayCollection();
  174.     }
  175.     public function getId(): ?int
  176.     {
  177.         return $this->id;
  178.     }
  179.     public function getEmail(): ?string
  180.     {
  181.         return $this->email;
  182.     }
  183.     public function setEmail(string $email): self
  184.     {
  185.         $this->email $email;
  186.         return $this;
  187.     }
  188.    
  189.     /**
  190.      * @deprecated since Symfony 5.3, use getUserIdentifier instead
  191.      */
  192.     public function getUsername(): string
  193.     {
  194.         return (string) $this->username;
  195.     }
  196.     public function getRoles(): array
  197.     {
  198.         // Assure que ROLE_USER est toujours présent
  199.         if (!in_array('ROLE_USER'$this->rolestrue)) {
  200.             $this->roles[] = 'ROLE_USER';
  201.         }
  202.         return array_unique($this->roles);
  203.     }
  204.     public function setRoles(array $roles): self
  205.     {
  206.         $this->roles $roles;
  207.         return $this;
  208.     }
  209.     /**
  210.      * @see PasswordAuthenticatedUserInterface
  211.      */
  212.     public function getPassword(): string
  213.     {
  214.         return $this->password;
  215.     }
  216.     public function setPassword(string $password): self
  217.     {
  218.         $this->password $password;
  219.         return $this;
  220.     }
  221.     /**
  222.      * Returning a salt is only needed, if you are not using a modern
  223.      * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  224.      *
  225.      * @see UserInterface
  226.      */
  227.     public function getSalt(): ?string
  228.     {
  229.         return null;
  230.     }
  231.     /**
  232.      * @see UserInterface
  233.      */
  234.     public function eraseCredentials(): void
  235.     {
  236.         // If you store any temporary, sensitive data on the user, clear it here
  237.         // $this->plainPassword = null;
  238.     }
  239.     public function setUsername(string $username)
  240.     {
  241.         $this->username $username;
  242.         return $this;
  243.     }
  244.     public function getNom(): ?string
  245.     {
  246.         return $this->nom;
  247.     }
  248.     public function setNom(string $nom)
  249.     {
  250.         $this->nom $nom;
  251.         return $this;
  252.     }
  253.     public function getPrenom(): ?string
  254.     {
  255.         return $this->prenom;
  256.     }
  257.     public function setPrenom(string $prenom)
  258.     {
  259.         $this->prenom $prenom;
  260.         return $this;
  261.     }
  262.     public function getAdresse(): ?string
  263.     {
  264.         return $this->adresse;
  265.     }
  266.     public function setAdresse(string $adresse)
  267.     {
  268.         $this->adresse $adresse;
  269.         return $this;
  270.     }
  271.     /**
  272.      * @return Collection<int, Livraisons>
  273.      */
  274.     public function getLivraisons(): Collection
  275.     {
  276.         return $this->livraisons;
  277.     }
  278.     public function addLivraison(Livraisons $livraison)
  279.     {
  280.         if (!$this->livraisons->contains($livraison)) {
  281.             $this->livraisons->add($livraison);
  282.             $livraison->setIdClient($this);
  283.         }
  284.         return $this;
  285.     }
  286.     public function removeLivraison(Livraisons $livraison)
  287.     {
  288.         if ($this->livraisons->removeElement($livraison)) {
  289.             // set the owning side to null (unless already changed)
  290.             if ($livraison->getIdClient() === $this) {
  291.                 $livraison->setIdClient(null);
  292.             }
  293.         }
  294.         return $this;
  295.     }
  296.     public function getCredits(): ?int
  297.     {
  298.         return $this->credits;
  299.     }
  300.     public function setCredits(?int $credits)
  301.     {
  302.         $this->credits $credits;
  303.         return $this;
  304.     }
  305.     public function __toString(): string
  306.     {
  307.         return $this->prenom.' '.$this->nom// Return a string representation
  308.     }
  309.     public function getTelephone1(): ?string
  310.     {
  311.         return $this->telephone1;
  312.     }
  313.     public function setTelephone1(string $telephone1): self
  314.     {
  315.         $this->telephone1 $telephone1;
  316.         return $this;
  317.     }
  318.    
  319.     /**
  320.      * @return Collection<int, Credits>
  321.      */
  322.     public function getCreditsTransaction(): Collection
  323.     {
  324.         return $this->creditsTransaction;
  325.     }
  326.     public function addCreditsTransaction(Credits $creditsTransaction): static
  327.     {
  328.         if (!$this->creditsTransaction->contains($creditsTransaction)) {
  329.             $this->creditsTransaction->add($creditsTransaction);
  330.             $creditsTransaction->setUserCredit($this);
  331.         }
  332.         return $this;
  333.     }
  334.     public function removeCreditsTransaction(Credits $creditsTransaction): static
  335.     {
  336.         if ($this->creditsTransaction->removeElement($creditsTransaction)) {
  337.             // set the owning side to null (unless already changed)
  338.             if ($creditsTransaction->getUserCredit() === $this) {
  339.                 $creditsTransaction->setUserCredit(null);
  340.             }
  341.         }
  342.         return $this;
  343.     }
  344.     /**
  345.      * @return Collection<int, Abonnement>
  346.      */
  347.     public function getAbonnements(): Collection
  348.     {
  349.         return $this->abonnements;
  350.     }
  351.     public function addAbonnement(Abonnement $abonnement): static
  352.     {
  353.         if (!$this->abonnements->contains($abonnement)) {
  354.             $this->abonnements->add($abonnement);
  355.             $abonnement->setUserabonnee($this);
  356.         }
  357.         return $this;
  358.     }
  359.     public function removeAbonnement(Abonnement $abonnement): static
  360.     {
  361.         if ($this->abonnements->removeElement($abonnement)) {
  362.             // set the owning side to null (unless already changed)
  363.             if ($abonnement->getUserabonnee() === $this) {
  364.                 $abonnement->setUserabonnee(null);
  365.             }
  366.         }
  367.         return $this;
  368.     }
  369.     public function isIsVerified(): ?bool
  370.     {
  371.         return $this->isVerified;
  372.     }
  373.     public function getPointfidelite(): ?int
  374.     {
  375.         return $this->pointfidelite;
  376.     }
  377.     public function setPointfidelite(?int $pointfidelite): static
  378.     {
  379.         $this->pointfidelite $pointfidelite;
  380.         return $this;
  381.     }
  382. public function isOkAbonnement(): bool
  383. {
  384.     $abonnements $this->getAbonnements();
  385.     if (count($abonnements) === 0) {
  386.         return false;
  387.     }
  388.     foreach ($abonnements as $subscription) {
  389.         if (strtolower(trim($subscription->getStatutabonnement())) !== 'ongoing') {
  390.             continue;
  391.         }
  392.         $dateResiliation $subscription->getDateresiliation();
  393.         if ($dateResiliation !== null) {
  394.             // On compare uniquement la DATE (pas l'heure)
  395.             $today = (new \DateTime())->setTime(000);
  396.             $endDate = (clone $dateResiliation)->setTime(235959);
  397.             if ($endDate $today) {
  398.                 continue; // abonnement expiré
  399.             }
  400.         }
  401.         $typeAbonnement $subscription->getTypeabonnement();
  402.         if (!$typeAbonnement) {
  403.             continue;
  404.         }
  405.         $caracteristiques $typeAbonnement->getCaracteristiquestypeabonnement();
  406.         if (!$caracteristiques) {
  407.             return true// pas de limite
  408.         }
  409.         $maxLivraisons $caracteristiques->getNbreliv();
  410.         if ($maxLivraisons !== null &&
  411.             $subscription->getNombrelivraison() >= $maxLivraisons
  412.         ) {
  413.             return false;
  414.         }
  415.         // ✅ abonnement valide trouvé
  416.         return true;
  417.     }
  418.     return false;
  419. }
  420.    
  421.     public function isOkAbonnementOLD(): bool
  422.     {
  423.         // Retrieve the user's subscription
  424.         $abonnement $this->getAbonnements();
  425.         if (!$abonnement) {
  426.             return false// No subscription found
  427.         }
  428.         // Check if the subscription has expired
  429.         $now = new \DateTime();
  430.         foreach ($abonnement as $subscription) {
  431.           //  $dateResiliation = $subscription->getDateResiliation();
  432.             // Faites quelque chose avec $dateResiliation
  433.         
  434.             if ($subscription->getStatutabonnement() == 'ongoing') {
  435.                 $typeabonnement=$subscription->getTypeabonnement();
  436.                 break;
  437.             }
  438.              }
  439.              
  440.              if (!isset($typeabonnement) || ($subscription->getDateresiliation() < new \DateTimeImmutable())) {
  441.                 return false;
  442.             }
  443.             
  444.     
  445.            if (in_array($typeabonnement->getId(), [123]) 
  446.            && $subscription->getNombrelivraison() === $typeabonnement->getCaracteristiquestypeabonnement()->getNbreliv()) {
  447.            return false;
  448.        }
  449.        
  450.    
  451.    
  452.         return true// Subscription is valid
  453.     }
  454.     public function getActiveAbonnement(): ?Abonnement
  455.     {
  456.         $now = new \DateTimeImmutable();
  457.     
  458.         foreach ($this->abonnements as $abonnement) {
  459.             if ($abonnement->getDateResiliation() > $now ) {
  460.                 return $abonnement;
  461.             }
  462.         }
  463.     
  464.         return null// Aucun abonnement actif
  465.     }
  466.     public function getNombreEmployes():  int
  467. {
  468.     
  469.     // Récupérer le nombre total (plus performant que count($users))
  470.     $count $userRepository->createQueryBuilder('u')
  471.         ->select('COUNT(u.id)')
  472.         ->join('u.entreprise''e')
  473.         ->where('e.createdBy = :creator')
  474.         ->setParameter('creator'$this->getUser())
  475.         ->getQuery()
  476.         ->getSingleScalarResult();
  477.     return (int)$count;
  478.     
  479. }
  480.     public function getUserIdentifier(): string
  481.     {
  482.         return $this->email ?? $this->username// Retourne d'abord l'email, sinon le username
  483.     }
  484.   
  485.     public function getEntreprise(): ?Entreprise
  486.     {
  487.         return $this->entreprise;
  488.     }
  489.     public function setEntreprise(?Entreprise $entreprise): static
  490.     {
  491.         $this->entreprise $entreprise;
  492.         return $this;
  493.     }
  494.     /**
  495.      * @return Collection<int, Pointage>
  496.      */
  497.     public function getPointages(): Collection
  498.     {
  499.         return $this->pointages;
  500.     }
  501.     public function addPointage(Pointage $pointage): static
  502.     {
  503.         if (!$this->pointages->contains($pointage)) {
  504.             $this->pointages->add($pointage);
  505.             $pointage->setEmploye($this);
  506.         }
  507.         return $this;
  508.     }
  509.     public function removePointage(Pointage $pointage): static
  510.     {
  511.         if ($this->pointages->removeElement($pointage)) {
  512.             // set the owning side to null (unless already changed)
  513.             if ($pointage->getEmploye() === $this) {
  514.                 $pointage->setEmploye(null);
  515.             }
  516.         }
  517.         return $this;
  518.     }
  519.     public function getTypeabonnement(): ?TypeAbonnement
  520.     {
  521.         return $this->typeabonnement;
  522.     }
  523.     public function setTypeabonnement(?TypeAbonnement $typeabonnement): static
  524.     {
  525.         $this->typeabonnement $typeabonnement;
  526.         return $this;
  527.     }
  528.     public function getFinancement(): ?Financement
  529.     {
  530.         return $this->financement;
  531.     }
  532.     public function setFinancement(?Financement $financement): static
  533.     {
  534.         $this->financement $financement;
  535.         return $this;
  536.     }
  537.     /**
  538.      * @return Collection<int, Financement>
  539.      */
  540.     public function getFinancements(): Collection
  541.     {
  542.         return $this->financements;
  543.     }
  544.     public function addFinancement(Financement $financement): static
  545.     {
  546.         if (!$this->financements->contains($financement)) {
  547.             $this->financements->add($financement);
  548.             $financement->setUsersfinancement($this);
  549.         }
  550.         return $this;
  551.     }
  552.     public function removeFinancement(Financement $financement): static
  553.     {
  554.         if ($this->financements->removeElement($financement)) {
  555.             // set the owning side to null (unless already changed)
  556.             if ($financement->getUsersfinancement() === $this) {
  557.                 $financement->setUsersfinancement(null);
  558.             }
  559.         }
  560.         return $this;
  561.     }
  562.     // src/Entity/User.php
  563. public function getNomComplet(): string
  564. {
  565.     return $this->prenom ' ' $this->nom;
  566. }
  567. }