<?php declare(strict_types=1);
namespace App\Customer\Entity;
use App\Authentication\Entity\User;
use App\Order\Entity\Order;
use DateTime;
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\Validator\Constraints as Assert;
use App\Framework\Entity\BlameableEntityTrait;
use App\Framework\Entity\TimestampableEntityTrait;
/**
* @ORM\Table(
* indexes={
* @ORM\Index(name="search", columns={"firstName", "lastName", "companyName"}, flags={"fulltext"}),
* @ORM\Index(name="search_email", columns={"email"}, flags={"fulltext"})
* }
* )
* @ORM\Entity(repositoryClass="App\Customer\Repository\CustomerRepository")
* @ORM\HasLifecycleCallbacks()
* @UniqueEntity(
* fields={"email"},
* message="error.user.email_used"
* )
*/
class Customer
{
use BlameableEntityTrait;
use TimestampableEntityTrait;
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private int $id;
/**
* @ORM\Column(type="string", length=100, nullable=true, name="firstName")
*/
private ?string $firstName = null;
/**
* @ORM\Column(type="string", length=100, nullable=true, name="lastName")
*/
private ?string $lastName = null;
/**
* @ORM\Column(type="string", length=100, nullable=true, name="companyName")
*/
private ?string $companyName = null;
/**
* @ORM\Column(type="string", length=100, unique=true, nullable=true)
*/
#[Assert\Email(message: 'El email \'{{ value }}\' no es valido.')]
private ?string $email = null;
/**
* @ORM\Column(type="boolean", name="isWholesaler")
*/
private bool $isWholesaler;
/**
* @ORM\OneToMany(targetEntity="App\Customer\Entity\StreetAddress", mappedBy="customer", cascade={"persist", "remove"})
*/
private Collection $streetAddresses;
/**
* @ORM\OneToMany(targetEntity="BillingAddress", mappedBy="customer", cascade={"persist", "remove"})
*/
private Collection $billingAddresses;
/**
* @ORM\OneToMany(targetEntity="App\Order\Entity\Order", mappedBy="customer")
*/
private Collection $orders;
/**
* @ORM\Column(type="boolean")
*/
private bool $active;
/**
* @ORM\ManyToOne(targetEntity="App\Authentication\Entity\User")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private ?User $user = null;
public function __construct()
{
$this->active = true;
$this->isWholesaler = false;
$this->streetAddresses = new ArrayCollection();
$this->billingAddresses = new ArrayCollection();
$this->orders = new ArrayCollection();
}
public function getId(): int
{
return $this->id;
}
public function getFirstName(): ?string
{
return $this->firstName;
}
public function getLastName(): ?string
{
return $this->lastName;
}
public function getFullName(): string
{
return ($this->isWholesaler && !empty($this->companyName)) ? $this->companyName : ($this->firstName . ' ' . $this->lastName);
}
public function getCompanyName(): ?string
{
return $this->companyName;
}
public function getEmail(): ?string
{
return $this->email;
}
public function getIsWholesaler(): bool
{
return $this->isWholesaler;
}
public function setId(int $id): void
{
$this->id = $id;
}
public function setFirstName(?string $firstName): void
{
$this->firstName = $firstName;
}
public function setLastName(?string $lastName): void
{
$this->lastName = $lastName;
}
public function setCompanyName(?string $companyName): void
{
$this->companyName = $companyName;
}
public function setEmail(?string $email): void
{
$this->email = $email;
}
public function setIsWholesaler(bool $isWholesaler): void
{
$this->isWholesaler = $isWholesaler;
}
public function getDisplayName(): ?string
{
if ($this->firstName && $this->lastName) {
return $this->lastName . ' ' . $this->firstName;
} elseif ($this->companyName) {
return $this->companyName;
}
return $this->firstName;
}
public function addShippingAddress(StreetAddress $streetAddresses): void
{
$this->streetAddresses[] = $streetAddresses;
}
public function removeShippingAddress(StreetAddress $streetAddresses): void
{
$this->streetAddresses->removeElement($streetAddresses);
}
/**
* @return StreetAddress[]|Collection
*/
public function getShippingAddresses(): Collection
{
return $this->streetAddresses;
}
public function addBillingAddress(BillingAddress $billingAddress): void
{
$this->billingAddresses[] = $billingAddress;
}
public function removeBillingAddress(BillingAddress $billingAddress): void
{
$this->billingAddresses->removeElement($billingAddress);
}
public function getBillingAddresses(): Collection
{
return $this->billingAddresses;
}
public function addOrder(Order $orders): void
{
$this->orders[] = $orders;
}
public function removeOrder(Order $orders): void
{
$this->orders->removeElement($orders);
}
public function getOrders(): Collection
{
return $this->orders;
}
public function setActive(bool $active): void
{
$this->active = $active;
}
public function getActive(): bool
{
return $this->active;
}
/**
* @param User|null $user
*/
public function setUser(?User $user): void
{
$this->user = $user;
}
public function getUser(): ?User
{
return $this->user;
}
public function hasUser(): bool
{
return $this->getUser() !== null;
}
}