<?php
/**
* @author Léo BANNHOLTZER (contact@scaledev.fr)
* @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
* @license commercial
*/
declare(strict_types=1);
namespace Bluue\StocksBundle\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use App\DoctrineExtensions\SoftDeleteable\Traits\UserSoftDeleteableEntity;
use App\DoctrineExtensions\Timestampable\Traits\UserTimestampableEntity;
use App\Entity\User;
use Bluue\StocksBundle\Repository\WarehouseRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\Uuid;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ApiResource(
* formats={"json"},
* security="is_granted('ROLE__STOCKS__SMALL_ADMIN')",
* normalizationContext={"groups"= {"getWarehouse"}},
* denormalizationContext={"groups"={"writeWarehouse"}},
* itemOperations={
* "get",
* "get_locations"={
* "method"="GET",
* "path"="/warehouse/{id}/locations",
* "normalization_context"={"groups"={"getLocations"}}
* },
* "get_sub_locations"={
* "method"="GET",
* "path"="/warehouse/{id}/subLocations",
* "normalization_context"={"groups"={"getSubLocations"}}
* }
* }
* )
* @ORM\Entity(repositoryClass=WarehouseRepository::class)
* @ORM\Table(name="stocks_bundle__warehouse",
* indexes={
* @ORM\Index(name="name", columns={"name"}),
* @ORM\Index(name="deleted_at", columns={"deleted_at"}),
* @ORM\Index(name="created_at", columns={"created_at"}),
* @ORM\Index(name="updated_at", columns={"updated_at"})
* })
* @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false, hardDelete=false)
*/
class Warehouse
{
use UserTimestampableEntity;
use UserSoftDeleteableEntity;
/**
* @ORM\Id
* @ORM\Column(type="uuid")
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class=UuidGenerator::class)
* @Groups({"getWarehouse", "getLocations", "getSubLocations"})
*/
private ?Uuid $id = null;
/**
* @ORM\Column(type="string", length=50)
* @Groups({"getWarehouse", "getLocations", "getSubLocations", "writeWarehouse"})
*/
private ?string $name;
/**
* @ORM\ManyToOne(targetEntity=Location::class, fetch="EXTRA_LAZY")
*/
private ?Location $defaultLocation = null;
/**
* @ORM\ManyToOne(targetEntity=SubLocation::class, fetch="EXTRA_LAZY")
*/
private ?SubLocation $defaultSubLocation = null;
/**
* @ORM\OneToMany(targetEntity=Location::class, mappedBy="warehouse", fetch="EXTRA_LAZY")
* @Groups({"getLocations", "getSubLocations"})
*/
private Collection $locations;
/**
* @ORM\OneToMany(targetEntity=StockProduct::class, mappedBy="warehouse", fetch="EXTRA_LAZY")
*/
private Collection $stockProducts;
/**
* @ORM\ManyToMany(targetEntity=User::class, fetch="EXTRA_LAZY")
* @ORM\JoinTable(
* name="stocks_bundle__warehouse_user",
* joinColumns={@ORM\JoinColumn(name="warehouse_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")}
* )
*/
private Collection $users;
public function __construct()
{
$this->locations = new ArrayCollection();
$this->stockProducts = new ArrayCollection();
$this->users = new ArrayCollection();
}
public function getId(): ?Uuid
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function addLocation(Location $location): self
{
if (!$this->locations->contains($location)) {
$this->locations[] = $location;
$location->setWarehouse($this);
}
return $this;
}
/**
* @return Collection|Location[]
*/
public function getLocations(): Collection
{
return $this->locations;
}
public function removeLocation(Location $location): self
{
if ($this->locations->removeElement($location)) {
if ($location->getWarehouse() === $this) {
$location->setWarehouse(null);
}
}
return $this;
}
public function addStockProduct(StockProduct $stockProduct): self
{
if (!$this->stockProducts->contains($stockProduct)) {
$this->stockProducts[] = $stockProduct;
$stockProduct->setWarehouse($this);
}
return $this;
}
/**
* @return Collection|StockProduct[]
*/
public function getStockProducts(): Collection
{
return $this->stockProducts;
}
public function removeStockProduct(StockProduct $stockProduct): self
{
if ($this->stockProducts->removeElement($stockProduct)) {
if ($stockProduct->getWarehouse() === $this) {
$stockProduct->setWarehouse(null);
}
}
return $this;
}
public function getUsers(): Collection
{
return $this->users;
}
public function addUser(User $user): self
{
if (!$this->users->contains($user)) {
$this->users[] = $user;
}
return $this;
}
public function removeUser(User $user): self
{
$this->users->removeElement($user);
return $this;
}
/**
* @return Location|null
*/
public function getDefaultLocation(): ?Location
{
return $this->defaultLocation;
}
/**
* @param Location|null $defaultLocation
* @return Warehouse
*/
public function setDefaultLocation(?Location $defaultLocation): self
{
$this->defaultLocation = $defaultLocation;
return $this;
}
/**
* @return SubLocation|null
*/
public function getDefaultSubLocation(): ?SubLocation
{
return $this->defaultSubLocation;
}
/**
* @param SubLocation|null $defaultSubLocation
* @return Warehouse
*/
public function setDefaultSubLocation(?SubLocation $defaultSubLocation): self
{
$this->defaultSubLocation = $defaultSubLocation;
return $this;
}
}