<?php
declare(strict_types=1);
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\CustomerRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Ksante\CoreBundle\Entity\Channel;
use Ksante\CoreBundle\Entity\Traits\TimestampableTrait;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass=CustomerRepository::class)
*
* @ORM\Table(
* name="ksante_customer",
* uniqueConstraints={
*
* @ORM\UniqueConstraint(name="customer_for_channel",
* columns={"email", "channel_id", "external_id"})
* })
*
* @ORM\HasLifecycleCallbacks()
*
* @ApiResource(normalizationContext={"groups"={"CustomerGroup"}})
*/
class Customer
{
use TimestampableTrait;
/**
* @ORM\Id
*
* @ORM\GeneratedValue
*
* @ORM\Column(type="integer")
*
* @Groups({"CustomerGroup"})
*/
private $id;
/**
* @ORM\Column(type="string", nullable=false)
*
* @Groups({"customer:read", "CustomerGroup","read", "write", "create", "front-read"})
*/
private string $externalId;
/**
* @ORM\Column(type="string")
*
* @Groups({"CustomerGroup", "write", "create"})
*
* @Assert\NotBlank
*/
private ?string $firstName = null;
/**
* @ORM\Column(type="string")
*
* @Groups({"CustomerGroup","write", "create"})
*/
private ?string $lastName = null;
/**
* @ORM\Column(type="string")
*
* @Groups({"CustomerGroup","write", "create"})
*
* @Assert\Email
*
* @Assert\NotBlank
*/
private ?string $email = null;
/**
* @ORM\Column(type="string", nullable=true)
*
* @Groups({"CustomerGroup","write", "create"})
*/
private ?string $phoneNumber = null;
/**
* @ORM\Column(type="string", nullable=true)
*
* @Groups({"CustomerGroup","write", "create"})
*/
private string $activity;
/**
* @ORM\OneToMany(targetEntity=AppointmentResume::class, mappedBy="customer")
*/
private Collection $appointmentsResume;
/**
* @ORM\Column(type="integer", options={"default"=0})
*
* @Groups({"CustomerGroup", "write", "create"})
*/
private int $appointmentCredit = 0;
/**
* @ORM\Column(type="integer", options={"default"=0})
*
* @Groups({"CustomerGroup", "write", "create"})
*/
private int $pastAppointments = 0;
/**
* @ORM\Column(type="integer", options={"default"=0})
*
* @Groups({"CustomerGroup", "write", "create"})
*/
private int $cancelledAppointments = 0;
/**
* @ORM\ManyToOne(targetEntity=Diet::class, inversedBy="customers")
*
* @ORM\JoinColumn(nullable=true , referencedColumnName="id", onDelete="SET NULL")
*
* @Groups({"CustomerGroup","read", "write", "create"})
*/
private ?Diet $diet = null;
/**
* @ORM\ManyToOne(targetEntity=Channel::class)
*
* @ORM\JoinColumn(nullable=true , referencedColumnName="id")
*
* @Groups({"CustomerGroup", "write", "create","front-read"})
*/
private Channel $channel;
/**
* @ORM\OneToMany(targetEntity=Appointment::class, mappedBy="customer")
*/
private Collection $appointments;
public function __construct()
{
$this->appointments = new ArrayCollection();
$this->appointmentsResume = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getChannel(): Channel
{
return $this->channel;
}
public function setChannel(Channel $channel): self
{
$this->channel = $channel;
return $this;
}
public function getExternalId(): string
{
return $this->externalId;
}
public function setExternalId(string $externalId): self
{
$this->externalId = $externalId;
return $this;
}
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 getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function getPhone(): ?string
{
return $this->phoneNumber;
}
public function setPhone(?string $phone): self
{
$this->phoneNumber = $phone;
return $this;
}
public function getActivity(): ?string
{
return $this->activity;
}
public function setActivity(string $activity): self
{
$this->activity = $activity;
return $this;
}
public function getAppointmentCredit(): ?int
{
return $this->appointmentCredit;
}
public function setAppointmentCredit(?int $appointmentCredit): self
{
$this->appointmentCredit = $appointmentCredit;
return $this;
}
public function incrementAppointmentCredit(): void
{
++$this->appointmentCredit;
}
public function getPastAppointments(): ?int
{
return $this->pastAppointments;
}
public function setPastAppointments(?int $pastAppointments): self
{
$this->pastAppointments = $pastAppointments;
return $this;
}
public function getCancelledAppointments(): ?int
{
return $this->cancelledAppointments;
}
public function setCancelledAppointments(int $cancelledAppointments): self
{
$this->cancelledAppointments = $cancelledAppointments;
return $this;
}
public function getDiet(): ?Diet
{
return $this->diet;
}
public function setDiet(?Diet $diet): self
{
$this->diet = $diet;
return $this;
}
public function hasDiet(): bool
{
return null === $this->diet ? false : true;
}
/**
* @return Appointment[]|Collection
*/
public function getAppointments(): Collection
{
return $this->appointments;
}
public function addAppointment(Appointment $appointment): self
{
if (!$this->appointments->contains($appointment)) {
$this->appointments[] = $appointment;
$appointment->setCustomer($this);
}
return $this;
}
public function removeAppointment(Appointment $appointment): self
{
if ($this->appointments->removeElement($appointment)) {
// set the owning side to null (unless already changed)
if ($appointment->getCustomer() === $this) {
$appointment->setCustomer(null);
}
}
return $this;
}
/**
* @return AppointmentResume[]|Collection
*/
public function getAppointmentsResume(): Collection
{
return $this->appointments;
}
public function addAppointmentResume(AppointmentResume $appointmentResume): self
{
if ($this->appointmentsResume->contains($appointmentResume)) {
return $this;
}
$this->appointmentsResume[] = $appointmentResume;
$appointmentResume->setCustomer($this);
return $this;
}
public function removeAppointmentResume(AppointmentResume $appointmentResume): self
{
if ($this->appointmentsResume->removeElement($appointmentResume)) {
// set the owning side to null (unless already changed)
if ($appointmentResume->getCustomer() === $this) {
$appointmentResume->setCustomer(null);
}
}
return $this;
}
public function getLastAppointment(Channel $channel)
{
foreach ($this->getAppointments() as $appointment) {
if (Appointment::STATUS_PRE_BOOK == $appointment->getStatus() && $appointment->getChannel() === $channel) {
return $appointment;
}
}
return null;
}
}