<?php
namespace App\Entity;
use App\Repository\ContactRepository;
use Doctrine\Common\Collections\ArrayCollection;
use App\Entity\Payment\AbstractPayment;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass=ContactRepository::class)
* @UniqueEntity("email")
*/
class Contact
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=100, unique=true)
* @Assert\Email(message="L'email {{ value }} n'est pas un email valide.")
*/
private $email;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $nom;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $prenom;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $adresse;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $ville;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $codePostal;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $pays;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $telephone;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $company;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
protected $newsletter;
/**
* @ORM\Column(type="boolean", nullable=false, options={"default": false})
*/
private $isLoyal = false;
/**
* @ORM\Column(type="string", length=1, nullable=true)
*/
private $gender;
/**
* @ORM\OneToMany(targetEntity=Reservation::class, mappedBy="contact")
*/
private $reservations;
/**
* @ORM\OneToMany(targetEntity=Privatisation::class, mappedBy="contact")
*/
private $privatisations;
/**
* @ORM\OneToMany(targetEntity=Token::class, mappedBy="contact")
*/
private $tokens;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $commentaire;
/**
* @ORM\OneToMany(targetEntity=AbstractPayment::class, mappedBy="reservation")
*/
private $abstractPayments;
public function __construct()
{
$this->reservations = new ArrayCollection();
$this->privatisations = new ArrayCollection();
$this->tokens = new ArrayCollection();
$this->abstractPayments = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function getNom(): ?string
{
return ucfirst(strtolower($this->nom));
}
public function setNom(?string $nom): self
{
$this->nom = $nom;
return $this;
}
public function getPrenom(): ?string
{
return ucfirst(strtolower($this->prenom));
}
public function setPrenom(?string $prenom): self
{
$this->prenom = $prenom;
return $this;
}
public function getFullName(): string
{
return $this->getPrenom() . ' ' . $this->getNom();
}
public function getAdresse(): ?string
{
return $this->adresse;
}
public function setAdresse(?string $adresse): self
{
$this->adresse = $adresse;
return $this;
}
public function getVille(): ?string
{
return $this->ville;
}
public function setVille(?string $ville): self
{
$this->ville = $ville;
return $this;
}
public function getCodePostal(): ?string
{
return $this->codePostal;
}
public function setCodePostal(?string $codePostal): self
{
$this->codePostal = $codePostal;
return $this;
}
public function getPays(): ?string
{
return $this->pays;
}
public function setPays(?string $pays): self
{
$this->pays = $pays;
return $this;
}
public function getTelephone(): ?string
{
return $this->telephone;
}
public function setTelephone(?string $telephone): self
{
$this->telephone = $telephone;
return $this;
}
public function getCompany(): ?string
{
return $this->company;
}
public function setCompany(?string $company): self
{
$this->company = $company;
return $this;
}
public function getNewsletter(): ?bool
{
return $this->newsletter;
}
public function setNewsletter(?bool $newsletter): self
{
$this->newsletter = $newsletter;
return $this;
}
public function getIsLoyal(): bool
{
return $this->isLoyal;
}
public function setIsLoyal(bool $isLoyal): self
{
$this->isLoyal = $isLoyal;
return $this;
}
public function getGender(): ?string
{
return $this->gender;
}
public function setGender(?string $gender): self
{
$this->gender = $gender;
return $this;
}
/**
* @return Collection<int, Reservation>
*/
public function getReservations(): Collection
{
return $this->reservations;
}
public function getConfirmedReservations(): Collection
{
return $this->reservations->filter(function (Reservation $reservation) {
return $reservation->getConfirmed() && !$reservation->getDeleted();
});
}
public function addReservation(Reservation $reservation): self
{
if (!$this->reservations->contains($reservation)) {
$this->reservations[] = $reservation;
$reservation->setContact($this);
}
return $this;
}
public function removeReservation(Reservation $reservation): self
{
if ($this->reservations->removeElement($reservation)) {
// set the owning side to null (unless already changed)
if ($reservation->getContact() === $this) {
$reservation->setContact(null);
}
}
return $this;
}
/**
* @return Collection<int, Privatisation>
*/
public function getPrivatisations(): Collection
{
return $this->privatisations;
}
public function addPrivatisation(Privatisation $privatisation): self
{
if (!$this->privatisations->contains($privatisation)) {
$this->privatisations[] = $privatisation;
$privatisation->setContact($this);
}
return $this;
}
public function removePrivatisation(Privatisation $privatisation): self
{
if ($this->privatisations->removeElement($privatisation)) {
// set the owning side to null (unless already changed)
if ($privatisation->getContact() === $this) {
$privatisation->setContact(null);
}
}
return $this;
}
/**
* @return Collection<int, Token>
*/
public function getTokens(): Collection
{
return $this->tokens;
}
public function addToken(Token $token): self
{
if (!$this->tokens->contains($token)) {
$this->tokens[] = $token;
$token->setContact($this);
}
return $this;
}
public function removeToken(Token $token): self
{
if ($this->tokens->removeElement($token)) {
// set the owning side to null (unless already changed)
if ($token->getContact() === $this) {
$token->setContact(null);
}
}
return $this;
}
public function getCommentaire(): ?string
{
return $this->commentaire;
}
public function setCommentaire(?string $commentaire): self
{
$this->commentaire = $commentaire;
return $this;
}
public function calculateIsLoyal(): bool
{
// Count confirmed and non-deleted reservations
$confirmedReservations = $this->reservations->filter(function (Reservation $reservation) {
return $reservation->getConfirmed() && !$reservation->getDeleted();
});
$reservationCount = $confirmedReservations->count();
// Count purchased carte cadeau (exclude tokens generated from a reservation)
$carteCadeauCount = 0;
foreach ($this->tokens as $token) {
if ($token->getReservation()) {
continue;
}
if ($token->getCodePromo() && ($token->getCodePromo()->getResteAPayer() == 0 || $token->getCodePromo()->getResteAPayer() === null)) {
$carteCadeauCount++;
}
}
// Return true if at least 2 items total (reservations + carte cadeau)
return ($reservationCount + $carteCadeauCount) >= 2;
}
public function isLoyal(): bool
{
return $this->getIsLoyal();
}
/**
* @return Collection<int, AbstractPayment>
*/
public function getAbstractPayments(): Collection
{
return $this->abstractPayments;
}
public function addAbstractPayment(AbstractPayment $abstractPayment): self
{
if (!$this->abstractPayments->contains($abstractPayment)) {
$this->abstractPayments[] = $abstractPayment;
$abstractPayment->setContact($this);
}
return $this;
}
public function removeAbstractPayment(AbstractPayment $abstractPayment): self
{
if ($this->abstractPayments->removeElement($abstractPayment)) {
// set the owning side to null (unless already changed)
if ($abstractPayment->getContact() === $this) {
$abstractPayment->setContact(null);
}
}
return $this;
}
}