<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use App\Entity\Helpers\BaseEntity;
use App\Repository\PaperRepository;
use App\Entity\Helpers\TraitStockable;
use App\Entity\Helpers\TraitPaperModel;
use App\Entity\Helpers\TraitPriceUnity;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
#[ORM\Entity(repositoryClass: PaperRepository::class)]
class Paper extends BaseEntity
{
use TraitPaperModel;
use TraitPriceUnity;
use TraitStockable;
const TYPE_REEL = 0;
const TYPE_BLOCK = 1;
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;
#[ORM\Column(type: 'string', length: 255)]
private $reference;
#[ORM\Column(type: 'string', length: 255)]
private $referenceInternal;
#[ORM\Column(type: 'integer')]
private $quantityMin = 0;
#[ORM\Column(type: 'decimal', precision: 15, scale: 4)]
private $priceUnityInitial = 0;
#[ORM\ManyToOne(targetEntity: Provider::class, inversedBy: 'papers')]
#[ORM\JoinColumn(nullable: false)]
private $provider;
#[ORM\OneToMany(mappedBy: 'paper', targetEntity: PaperProduct::class, orphanRemoval: true, cascade: ['all'])]
private $paperProducts;
#[ORM\OneToMany(mappedBy: 'paper', targetEntity: PaperStock::class, orphanRemoval: true, cascade: ['all'])]
private $paperStocks;
#[ORM\OneToMany(mappedBy: 'paper', targetEntity: PaperStockGroup::class, orphanRemoval: true, cascade: ['all'])]
private $paperStockGroups;
#[ORM\Column(type: 'integer')]
private $paperQuality = 0;
#[ORM\Column(type: 'integer')]
private $paperQualityPercent = 0;
#[ORM\ManyToOne(targetEntity: PaperCategory::class, inversedBy: 'papers')]
private $paperCategory;
#[ORM\Column(type: 'boolean')]
private $outOfStock = false;
#[ORM\Column(type: 'date_immutable', nullable: true)]
private $outOfStockAt;
#[ORM\Column(type: 'integer', nullable: true)]
private $outOfStockQuantity;
#[ORM\Column(type: 'date_immutable', nullable: true)]
private $lowestOutOfStockAt;
#[ORM\Column(type: 'integer', nullable: true)]
private $lowestOutOfStockQuantity;
public function __construct()
{
parent::__construct();
$this->paperProducts = new ArrayCollection();
$this->paperStocks = new ArrayCollection();
$this->paperStockGroups = new ArrayCollection();
}
public function __toString()
{
$name = $this->getReference();
if ($this->getArchived()) $name .= " (archivé)";
return $name;
}
public function getOutOfStockAtDisplay()
{
return $this->getOutOfStockDisplay($this->getOutOfStockAt());
}
public function getLowestOutOfStockAtDisplay()
{
return $this->getOutOfStockDisplay($this->getLowestOutOfStockAt());
}
public function getOutOfStockDisplay($outOfStockAt)
{
if (!$outOfStockAt) return null;
$outOfStockAt = $outOfStockAt->setTime(0,0,0);
$now = (new \DateTimeImmutable())->setTime(0,0,0);
$interval = $now->diff($outOfStockAt);
return $interval->format('%a') . 'J';
}
public function getPaperQualityDisplay()
{
return $this->getPaperQuality() == Provider::QUALITY_PEFC ? 'PEFC' : 'FSC';
}
public function setQuantity(int $quantity): self
{
$this->quantity = $quantity;
$this->setOutOfStock($quantity <= $this->getQuantityMin());
return $this;
}
public function setPriceUnity(string $priceUnity): self
{
$this->priceUnity = $priceUnity;
if(null === $this->getPriceUnityInitial()) $this->priceUnityInitial = $priceUnity;
return $this;
}
public function isStockInDanger(): bool
{
return $this->getOutOfStock() || $this->getOutOfStockAt();
}
public function getStatusDisplay(): string
{
if($this->getOutOfStock()) return 'Rupture';
return $this->getOutOfStockAt() ? 'Rupture le ' . $this->getOutOfStockAt()->format('d/m/Y') : 'En stock';
}
// ##################################################################### //
// ##################################################################### //
// ##################################################################### //
public function getId(): ?int
{
return $this->id;
}
public function getReference(): ?string
{
return $this->reference;
}
public function setReference(string $reference): self
{
$this->reference = $reference;
return $this;
}
public function getReferenceInternal(): ?string
{
return $this->referenceInternal;
}
public function setReferenceInternal(string $referenceInternal): self
{
$this->referenceInternal = $referenceInternal;
return $this;
}
public function getQuantityMin(): ?int
{
return $this->quantityMin;
}
public function setQuantityMin(int $quantityMin): self
{
$this->quantityMin = $quantityMin;
return $this;
}
public function getOutOfStock(): ?bool
{
return $this->outOfStock;
}
public function setOutOfStock(bool $outOfStock): self
{
$this->outOfStock = $outOfStock;
return $this;
}
public function getOutOfStockAt(): ?\DateTimeImmutable
{
return $this->outOfStockAt;
}
public function setOutOfStockAt(?\DateTimeImmutable $outOfStockAt): self
{
$this->outOfStockAt = $outOfStockAt;
return $this;
}
public function getPriceUnityInitial(): ?string
{
return $this->priceUnityInitial;
}
public function setPriceUnityInitial(string $priceUnityInitial): self
{
$this->priceUnityInitial = $priceUnityInitial;
return $this;
}
public function getProvider(): ?Provider
{
return $this->provider;
}
public function setProvider(?Provider $provider): self
{
$this->provider = $provider;
$this->setPaperQuality($this->getProvider()->getPaperQuality());
$this->setPaperQualityPercent($this->getProvider()->getPaperQualityPercent());
return $this;
}
/**
* @return Collection|PaperProduct[]
*/
public function getPaperProducts(): Collection
{
return $this->paperProducts;
}
public function addPaperProduct(PaperProduct $paperProduct): self
{
if (!$this->paperProducts->contains($paperProduct)) {
$this->paperProducts[] = $paperProduct;
$paperProduct->setPaper($this);
}
return $this;
}
public function removePaperProduct(PaperProduct $paperProduct): self
{
if ($this->paperProducts->removeElement($paperProduct)) {
// set the owning side to null (unless already changed)
if ($paperProduct->getPaper() === $this) {
$paperProduct->setPaper(null);
}
}
return $this;
}
/**
* @return Collection|PaperStock[]
*/
public function getPaperStocks(): Collection
{
return $this->paperStocks;
}
public function addPaperStock(PaperStock $paperStock): self
{
if (!$this->paperStocks->contains($paperStock)) {
$this->paperStocks[] = $paperStock;
$paperStock->setPaper($this);
}
return $this;
}
public function removePaperStock(PaperStock $paperStock): self
{
if ($this->paperStocks->removeElement($paperStock)) {
// set the owning side to null (unless already changed)
if ($paperStock->getPaper() === $this) {
$paperStock->setPaper(null);
}
}
return $this;
}
/**
* @return Collection|PaperStockGroup[]
*/
public function getPaperStockGroups(): Collection
{
return $this->paperStockGroups;
}
public function addPaperStockGroup(PaperStockGroup $paperStockGroup): self
{
if (!$this->paperStockGroups->contains($paperStockGroup)) {
$this->paperStockGroups[] = $paperStockGroup;
$paperStockGroup->setPaper($this);
}
return $this;
}
public function removePaperStockGroup(PaperStockGroup $paperStockGroup): self
{
if ($this->paperStockGroups->removeElement($paperStockGroup)) {
// set the owning side to null (unless already changed)
if ($paperStockGroup->getPaper() === $this) {
$paperStockGroup->setPaper(null);
}
}
return $this;
}
public function getPaperQuality(): ?int
{
return $this->paperQuality;
}
public function setPaperQuality(int $paperQuality): self
{
$this->paperQuality = $paperQuality;
return $this;
}
public function getPaperQualityPercent(): ?int
{
return $this->paperQualityPercent;
}
public function setPaperQualityPercent(int $paperQualityPercent): self
{
$this->paperQualityPercent = $paperQualityPercent;
return $this;
}
public function getPaperCategory(): ?PaperCategory
{
return $this->paperCategory;
}
public function setPaperCategory(?PaperCategory $paperCategory): self
{
$this->paperCategory = $paperCategory;
return $this;
}
public function getOutOfStockQuantity(): ?int
{
return $this->outOfStockQuantity;
}
public function setOutOfStockQuantity(?int $outOfStockQuantity): self
{
$this->outOfStockQuantity = $outOfStockQuantity;
return $this;
}
public function getLowestOutOfStockAt(): ?\DateTimeImmutable
{
return $this->lowestOutOfStockAt;
}
public function setLowestOutOfStockAt(?\DateTimeImmutable $lowestOutOfStockAt): self
{
$this->lowestOutOfStockAt = $lowestOutOfStockAt;
return $this;
}
public function getLowestOutOfStockQuantity(): ?int
{
return $this->lowestOutOfStockQuantity;
}
public function setLowestOutOfStockQuantity(?int $lowestOutOfStockQuantity): self
{
$this->lowestOutOfStockQuantity = $lowestOutOfStockQuantity;
return $this;
}
}