<?php
namespace App\Entity;
use App\Repository\ContactRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
/**
* @ORM\Entity(repositoryClass=ContactRepository::class)
* @ORM\Table(name="contact", indexes={
* @ORM\Index(name="idx_contact_active", columns={"is_active"}),
* @ORM\Index(name="idx_contact_client", columns={"linked_client_id"}),
* @ORM\Index(name="idx_contact_supplier", columns={"linked_supplier_id"}),
* @ORM\Index(name="idx_contact_created_at", columns={"created_at"})
* })
*/
class Contact
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private ?int $id = null;
/**
* @ORM\Column(type="string", length=150, nullable=true)
* @Assert\Length(max=150)
*/
private ?string $firstName = null;
/**
* @ORM\Column(type="string", length=150, nullable=true)
* @Assert\Length(max=150)
*/
private ?string $lastName = null;
/**
* @ORM\Column(type="string", length=30, nullable=true)
* @Assert\Length(max=30)
*/
private ?string $phone = null;
/**
* @ORM\Column(name="second_phone", type="string", length=30, nullable=true)
* @Assert\Length(max=30)
*/
private ?string $secondPhone = null;
/**
* @ORM\Column(type="string", length=180, nullable=true)
* @Assert\Email
* @Assert\Length(max=180)
*/
private ?string $email = null;
/**
* @ORM\Column(name="company_name", type="string", length=255, nullable=true)
* @Assert\Length(max=255)
*/
private ?string $companyName = null;
/**
* @ORM\Column(name="job_title", type="string", length=180, nullable=true)
* @Assert\Length(max=180)
*/
private ?string $jobTitle = null;
/**
* @ORM\Column(type="text", nullable=true)
*/
private ?string $address = null;
/**
* @ORM\Column(name="address2", type="text", nullable=true)
*/
private ?string $address2 = null;
/**
* @ORM\Column(type="string", length=150, nullable=true)
* @Assert\Length(max=150)
*/
private ?string $city = null;
/**
* @ORM\Column(type="string", length=150, nullable=true)
* @Assert\Length(max=150)
*/
private ?string $region = null;
/**
* @ORM\Column(name="zip_code", type="string", length=40, nullable=true)
* @Assert\Length(max=40)
*/
private ?string $zipCode = null;
/**
* @ORM\Column(type="string", length=120, nullable=true)
* @Assert\Length(max=120)
*/
private ?string $country = null;
/**
* @ORM\ManyToOne(targetEntity=User::class, inversedBy="contacts")
* @ORM\JoinColumn(name="linked_client_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
*/
private ?User $linkedClient = null;
/**
* @ORM\ManyToOne(targetEntity=Supplier::class, inversedBy="contacts")
* @ORM\JoinColumn(name="linked_supplier_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
*/
private ?Supplier $linkedSupplier = null;
/**
* @ORM\Column(name="is_active", type="boolean", options={"default": true})
*/
private bool $isActive = true;
/**
* @ORM\Column(type="text", nullable=true)
*/
private ?string $notes = null;
/**
* @ORM\ManyToOne(targetEntity=User::class)
* @ORM\JoinColumn(name="converted_client_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
*/
private ?User $convertedClient = null;
/**
* @ORM\Column(name="converted_at", type="datetime", nullable=true)
*/
private ?\DateTimeInterface $convertedAt = null;
/**
* @ORM\ManyToOne(targetEntity=User::class)
* @ORM\JoinColumn(name="converted_by_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
*/
private ?User $convertedBy = null;
/**
* @ORM\ManyToOne(targetEntity=User::class)
* @ORM\JoinColumn(name="created_by_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
*/
private ?User $createdBy = null;
/**
* @ORM\ManyToOne(targetEntity=User::class)
* @ORM\JoinColumn(name="updated_by_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
*/
private ?User $updatedBy = null;
/**
* @ORM\Column(name="created_at", type="datetime")
*/
private ?\DateTimeInterface $createdAt = null;
/**
* @ORM\Column(name="updated_at", type="datetime", nullable=true)
*/
private ?\DateTimeInterface $updatedAt = null;
/**
* @ORM\OneToMany(targetEntity=Comment::class, mappedBy="contact", cascade={"remove"})
*/
private Collection $comments;
public function __construct()
{
$this->comments = new ArrayCollection();
}
/**
* @Assert\Callback
*/
public function validateLinking(ExecutionContextInterface $context): void
{
if ($this->linkedClient && $this->linkedSupplier) {
$context->buildViolation('Un contact ne peut pas être lié à un client et à un fournisseur en même temps.')
->atPath('linkedClient')
->addViolation();
}
}
public function getId(): ?int
{
return $this->id;
}
public function getFirstName(): ?string
{
return $this->firstName;
}
public function setFirstName(?string $firstName): self
{
$this->firstName = $firstName;
return $this;
}
public function getLastName(): ?string
{
return $this->lastName;
}
public function setLastName(?string $lastName): self
{
$this->lastName = $lastName;
return $this;
}
public function getFullName(): string
{
$parts = array_filter([
trim((string) $this->firstName),
trim((string) $this->lastName),
]);
return trim(implode(' ', $parts));
}
public function getDisplayName(): string
{
$fullName = $this->getFullName();
if ($fullName !== '') {
return $fullName;
}
if ($this->companyName) {
return trim((string) $this->companyName);
}
return 'Contact';
}
public function getPhone(): ?string
{
return $this->phone;
}
public function setPhone(?string $phone): self
{
$this->phone = $phone;
return $this;
}
public function getSecondPhone(): ?string
{
return $this->secondPhone;
}
public function setSecondPhone(?string $secondPhone): self
{
$this->secondPhone = $secondPhone;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(?string $email): self
{
$this->email = $email;
return $this;
}
public function getCompanyName(): ?string
{
return $this->companyName;
}
public function setCompanyName(?string $companyName): self
{
$this->companyName = $companyName;
return $this;
}
public function getJobTitle(): ?string
{
return $this->jobTitle;
}
public function setJobTitle(?string $jobTitle): self
{
$this->jobTitle = $jobTitle;
return $this;
}
public function getAddress(): ?string
{
return $this->address;
}
public function setAddress(?string $address): self
{
$this->address = $address;
return $this;
}
public function getAddress2(): ?string
{
return $this->address2;
}
public function setAddress2(?string $address2): self
{
$this->address2 = $address2;
return $this;
}
public function getCity(): ?string
{
return $this->city;
}
public function setCity(?string $city): self
{
$this->city = $city;
return $this;
}
public function getRegion(): ?string
{
return $this->region;
}
public function setRegion(?string $region): self
{
$this->region = $region;
return $this;
}
public function getZipCode(): ?string
{
return $this->zipCode;
}
public function setZipCode(?string $zipCode): self
{
$this->zipCode = $zipCode;
return $this;
}
public function getCountry(): ?string
{
return $this->country;
}
public function setCountry(?string $country): self
{
$this->country = $country;
return $this;
}
public function getLinkedClient(): ?User
{
return $this->linkedClient;
}
public function setLinkedClient(?User $linkedClient): self
{
$this->linkedClient = $linkedClient;
if ($linkedClient !== null) {
$this->linkedSupplier = null;
}
return $this;
}
public function getLinkedSupplier(): ?Supplier
{
return $this->linkedSupplier;
}
public function setLinkedSupplier(?Supplier $linkedSupplier): self
{
$this->linkedSupplier = $linkedSupplier;
if ($linkedSupplier !== null) {
$this->linkedClient = null;
}
return $this;
}
public function getLinkType(): string
{
if ($this->linkedClient) {
return 'client';
}
if ($this->linkedSupplier) {
return 'supplier';
}
return 'free';
}
public function isActive(): bool
{
return $this->isActive;
}
public function setIsActive(bool $isActive): self
{
$this->isActive = $isActive;
return $this;
}
public function getNotes(): ?string
{
return $this->notes;
}
public function setNotes(?string $notes): self
{
$this->notes = $notes;
return $this;
}
public function getConvertedClient(): ?User
{
return $this->convertedClient;
}
public function setConvertedClient(?User $convertedClient): self
{
$this->convertedClient = $convertedClient;
return $this;
}
public function getConvertedAt(): ?\DateTimeInterface
{
return $this->convertedAt;
}
public function setConvertedAt(?\DateTimeInterface $convertedAt): self
{
$this->convertedAt = $convertedAt;
return $this;
}
public function getConvertedBy(): ?User
{
return $this->convertedBy;
}
public function setConvertedBy(?User $convertedBy): self
{
$this->convertedBy = $convertedBy;
return $this;
}
public function getCreatedBy(): ?User
{
return $this->createdBy;
}
public function setCreatedBy(?User $createdBy): self
{
$this->createdBy = $createdBy;
return $this;
}
public function getUpdatedBy(): ?User
{
return $this->updatedBy;
}
public function setUpdatedBy(?User $updatedBy): self
{
$this->updatedBy = $updatedBy;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
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->setContact($this);
}
return $this;
}
public function removeComment(Comment $comment): self
{
if ($this->comments->removeElement($comment)) {
if ($comment->getContact() === $this) {
$comment->setContact(null);
}
}
return $this;
}
}