<?php
namespace App\Entity;
use App\Controller\Action\DeleteCustomerController;
use App\Controller\Action\DeleteSitesController;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use App\Controller\Action\SiteTreeAction;
use App\Traits\AutoTimestampTrait;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Serializer\Annotation\Groups;
#[ORM\Table(name: 'customer')]
#[ORM\Entity]
#[ORM\HasLifecycleCallbacks]
#[ApiResource(
normalizationContext: ['groups' => ['customer.get','user.get', 'discussion.get']],
operations: [
new Post(
security: 'user.isMasterAdmin()',
),
new Get(
name: '_api_/customers/tree',
uriTemplate: '/customers/tree',
controller: SiteTreeAction::class,
write: false,
read: false
),
new GetCollection(
paginationEnabled: false,
order: ['name' => 'ASC']
),
new Get(
),
new Put(
security: 'user.isMasterAdmin()',
),
new Delete(
security: 'user.isMasterAdmin()',
),
new Post(
name: '_api_/delete_customer',
uriTemplate: '/delete_customer',
controller: DeleteCustomerController::class,
write: false,
read: false
),
]
)]
class Customer
{
use AutoTimestampTrait;
#[ORM\Column(name: 'id', type: 'integer', nullable: false)]
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'IDENTITY')]
#[Groups(['customer.get', 'user.get'])]
private int $id;
#[ORM\Column(name: 'old_id', type: 'string', length: 255, nullable: true)]
private ?string $oldId = null;
#[ORM\Column(name: 'code', type: 'string', length: 255, nullable: true)]
#[Assert\NotBlank]
#[Groups(['customer.get', 'user.get'])]
private ?string $code = null;
#[ORM\Column(name: 'reference', type: 'string', length: 255, nullable: true)]
#[Groups(['customer.get', 'user.get'])]
private ?string $reference = null;
#[ORM\Column(name: 'name', type: 'string', length: 255, nullable: true)]
#[Assert\NotBlank]
#[Groups(['customer.get', 'user.get', 'discussion.get'])]
private ?string $name = null;
#[ORM\OneToMany(mappedBy: 'customer', targetEntity: User::class)]
private Collection $users;
// #[Groups(['customer.get'])]
#[ORM\OneToMany(mappedBy: 'customer', targetEntity: Site::class)]
private Collection $sites;
#[ORM\Column(nullable: true)]
#[Groups(['customer.get', 'user.get'])]
private ?int $year = null;
public function __construct()
{
$this->users = new ArrayCollection();
$this->sites = new ArrayCollection();
}
/**
* @return Collection<int, Site>
*/
public function getSites(): Collection
{
return $this->sites;
}
public function setSites($sites): self
{
$this->sites = $sites;
return $this;
}
public function addSite(Site $site): self
{
if (!$this->sites->contains($site)) {
$this->sites->add($site);
$site->setCustomer($this);
}
return $this;
}
public function removeSite(Site $site): self
{
if ($this->sites->removeElement($site)) {
// set the owning side to null (unless already changed)
if ($site->getCustomer() === $this) {
$site->setCustomer(null);
}
}
return $this;
}
public function getId(): ?int
{
return $this->id;
}
public function getOldId(): ?string
{
return $this->oldId;
}
public function setOldId(?string $oldId): self
{
$this->oldId = $oldId;
return $this;
}
public function getCode(): ?string
{
return $this->code;
}
public function setCode(?string $code): self
{
$this->code = $code;
return $this;
}
public function getReference(): ?string
{
return $this->reference;
}
public function setReference(?string $reference): self
{
$this->reference = $reference;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(?string $name): self
{
$this->name = $name;
return $this;
}
/**
* @return Collection<int, User>
*/
public function getUsers(): Collection
{
return $this->users;
}
public function addUser(User $user): self
{
if (!$this->users->contains($user)) {
$this->users->add($user);
$user->setCustomer($this);
}
return $this;
}
public function removeUser(User $user): self
{
if ($this->users->removeElement($user)) {
// set the owning side to null (unless already changed)
if ($user->getCustomer() === $this) {
$user->setCustomer(null);
}
}
return $this;
}
/*
public function getSites(): array
{
return $this->sites;
}
public function setSites($sites): self
{
$this->sites = $sites;
return $this;
}*/
public function getYear(): ?int
{
return $this->year;
}
public function setYear(?int $year): self
{
$this->year = $year;
return $this;
}
}