<?php
namespace App\Entity;
use App\DataProcessor\UserDataProcessor;
use App\DataProvider\UserByEmailProvider;
use App\DataProvider\UserDataGetProvider;
use App\Entity\Interface\NotificationInterface;
use App\Repository\ClientRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use ApiPlatform\Metadata as Api;
#[ORM\Entity(repositoryClass: ClientRepository::class)]
#[Api\ApiResource(
normalizationContext: ['groups' => ['user.read', 'timestamp.read']],
denormalizationContext: ['groups' => ['user.write']]
)]
#[Api\Get(
security: "is_granted('IS_AUTHENTICATED_FULLY')",
provider: UserDataGetProvider::class
)]
#[Api\Get(
uriTemplate: '/clients/by-email/{email}',
uriVariables: ['email'],
security: "is_granted('IS_AUTHENTICATED_FULLY')",
provider: UserByEmailProvider::class
)]
#[Api\Post(processor: UserDataProcessor::class)]
#[Api\Patch(
security: "is_granted('IS_AUTHENTICATED_FULLY')"
)]
#[Api\GetCollection(
security: "is_granted('IS_AUTHENTICATED_FULLY')",
)]
class Client extends User implements NotificationInterface
{
#[ORM\Column(type: 'string', length: 255, nullable: true)]
#[Groups(["user.read", "user.write"])]
private $companyName;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
#[Groups(["user.read", "user.write"])]
private $companyCode;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
#[Groups(["user.read", "user.write"])]
private $vatCode;
#[ORM\OneToMany(mappedBy: 'client', targetEntity: Purchase::class)]
private $purchases;
#[ORM\OneToMany(mappedBy: 'client', targetEntity: Delivery::class)]
private $deliveries;
#[ORM\OneToMany(mappedBy: 'client', targetEntity: Invoice::class)]
private $invoices;
public function toArray(): array
{
return [
'id' => $this->getId(),
'email' => $this->getEmail(),
'firstName' => $this->getFirstName(),
'lastName' => $this->getLastName(),
'isActive' => $this->getIsActive(),
'privacyPolicy' => $this->getPrivacyPolicy(),
'termsConditions' => $this->getTermsConditions(),
'newsletterSubscription' => $this->getNewsletterSubscription(),
'phoneCountryCode' => $this->getPhoneCountryCode(),
'phoneNo' => $this->getPhoneNo(),
'profileImage' => $this->getProfileImage(),
'jobPosition' => $this->getJobPosition(),
'userHeaderColor' => $this->getUserHeaderColor(),
'userSideNavDark' => $this->getUserSideNavDark(),
'userFoldedMenu' => $this->getUserFoldedMenu(),
'routePermissions' => $this->getRoutePermissions(),
'locale' => $this->getLocale(),
'comment' => $this->getComment(),
'address' => $this->getAddress(),
'country' => $this->getCountry(),
'dateOfBirth' => $this->getDateOfBirth(),
'isVerified' => $this->isVerified(),
'zipCode' => $this->getZipCode(),
'isLocked' => $this->isIsLocked(),
'verificationCode' => $this->getVerificationCode(),
'createdAt' => $this->getCreatedAt(),
'updatedAt' => $this->getUpdatedAt(),
'companyName' => $this->getCompanyName(),
'companyCode' => $this->getCompanyCode(),
'vatCode' => $this->getVatCode(),
];
}
public function __construct()
{
parent::__construct();
$this->purchases = new ArrayCollection();
$this->deliveries = new ArrayCollection();
$this->invoices = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function isLegal(): bool
{
return $this->companyName !== null;
}
public function getCompanyName(): ?string
{
return $this->companyName;
}
public function setCompanyName(?string $companyName): self
{
$this->companyName = $companyName;
return $this;
}
public function getCompanyCode(): ?string
{
return $this->companyCode;
}
public function setCompanyCode(?string $companyCode): self
{
$this->companyCode = $companyCode;
return $this;
}
public function getVatCode(): ?string
{
return $this->vatCode;
}
public function setVatCode(?string $vatCode): self
{
$this->vatCode = $vatCode;
return $this;
}
/**
* @return Collection<int, Purchase>
*/
public function getPurchases(): Collection
{
return $this->purchases;
}
public function addPurchase(Purchase $purchase): self
{
if (!$this->purchases->contains($purchase)) {
$this->purchases[] = $purchase;
$purchase->setClient($this);
}
return $this;
}
/**
* @return Collection<int, Invoice>
*/
public function getInvoices(): Collection
{
return $this->invoices;
}
public function addInvoice(Invoice $invoice): self
{
if (!$this->invoices->contains($invoice)) {
$this->invoices[] = $invoice;
$invoice->setClient($this);
}
return $this;
}
public function removePurchase(Purchase $purchase): self
{
if ($this->purchases->removeElement($purchase)) {
// set the owning side to null (unless already changed)
if ($purchase->getClient() === $this) {
$purchase->setClient(null);
}
}
return $this;
}
/**
* @return Collection<int, Delivery>
*/
public function getDeliveries(): Collection
{
return $this->deliveries;
}
public function addDelivery(Delivery $delivery): self
{
if (!$this->deliveries->contains($delivery)) {
$this->deliveries[] = $delivery;
$delivery->setClient($this);
}
return $this;
}
public function removeDelivery(Delivery $delivery): self
{
if ($this->deliveries->removeElement($delivery)) {
// set the owning side to null (unless already changed)
if ($delivery->getClient() === $this) {
$delivery->setClient(null);
}
}
return $this;
}
public function removeInvoice(Invoice $invoice): self
{
if ($this->invoices->removeElement($invoice)) {
// set the owning side to null (unless already changed)
if ($invoice->getClient() === $this) {
$invoice->setClient(null);
}
}
return $this;
}
}